acenic.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248
  1. /*
  2. * acenic.c: Linux driver for the Alteon AceNIC Gigabit Ethernet card
  3. * and other Tigon based cards.
  4. *
  5. * Copyright 1998-2002 by Jes Sorensen, <jes@trained-monkey.org>.
  6. *
  7. * Thanks to Alteon and 3Com for providing hardware and documentation
  8. * enabling me to write this driver.
  9. *
  10. * A mailing list for discussing the use of this driver has been
  11. * setup, please subscribe to the lists if you have any questions
  12. * about the driver. Send mail to linux-acenic-help@sunsite.auc.dk to
  13. * see how to subscribe.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * Additional credits:
  21. * Pete Wyckoff <wyckoff@ca.sandia.gov>: Initial Linux/Alpha and trace
  22. * dump support. The trace dump support has not been
  23. * integrated yet however.
  24. * Troy Benjegerdes: Big Endian (PPC) patches.
  25. * Nate Stahl: Better out of memory handling and stats support.
  26. * Aman Singla: Nasty race between interrupt handler and tx code dealing
  27. * with 'testing the tx_ret_csm and setting tx_full'
  28. * David S. Miller <davem@redhat.com>: conversion to new PCI dma mapping
  29. * infrastructure and Sparc support
  30. * Pierrick Pinasseau (CERN): For lending me an Ultra 5 to test the
  31. * driver under Linux/Sparc64
  32. * Matt Domsch <Matt_Domsch@dell.com>: Detect Alteon 1000baseT cards
  33. * ETHTOOL_GDRVINFO support
  34. * Chip Salzenberg <chip@valinux.com>: Fix race condition between tx
  35. * handler and close() cleanup.
  36. * Ken Aaker <kdaaker@rchland.vnet.ibm.com>: Correct check for whether
  37. * memory mapped IO is enabled to
  38. * make the driver work on RS/6000.
  39. * Takayoshi Kouchi <kouchi@hpc.bs1.fc.nec.co.jp>: Identifying problem
  40. * where the driver would disable
  41. * bus master mode if it had to disable
  42. * write and invalidate.
  43. * Stephen Hack <stephen_hack@hp.com>: Fixed ace_set_mac_addr for little
  44. * endian systems.
  45. * Val Henson <vhenson@esscom.com>: Reset Jumbo skb producer and
  46. * rx producer index when
  47. * flushing the Jumbo ring.
  48. * Hans Grobler <grobh@sun.ac.za>: Memory leak fixes in the
  49. * driver init path.
  50. * Grant Grundler <grundler@cup.hp.com>: PCI write posting fixes.
  51. */
  52. #include <linux/module.h>
  53. #include <linux/moduleparam.h>
  54. #include <linux/types.h>
  55. #include <linux/errno.h>
  56. #include <linux/ioport.h>
  57. #include <linux/pci.h>
  58. #include <linux/dma-mapping.h>
  59. #include <linux/kernel.h>
  60. #include <linux/netdevice.h>
  61. #include <linux/etherdevice.h>
  62. #include <linux/skbuff.h>
  63. #include <linux/init.h>
  64. #include <linux/delay.h>
  65. #include <linux/mm.h>
  66. #include <linux/highmem.h>
  67. #include <linux/sockios.h>
  68. #include <linux/firmware.h>
  69. #include <linux/slab.h>
  70. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  71. #include <linux/if_vlan.h>
  72. #endif
  73. #ifdef SIOCETHTOOL
  74. #include <linux/ethtool.h>
  75. #endif
  76. #include <net/sock.h>
  77. #include <net/ip.h>
  78. #include <asm/system.h>
  79. #include <asm/io.h>
  80. #include <asm/irq.h>
  81. #include <asm/byteorder.h>
  82. #include <asm/uaccess.h>
  83. #define DRV_NAME "acenic"
  84. #undef INDEX_DEBUG
  85. #ifdef CONFIG_ACENIC_OMIT_TIGON_I
  86. #define ACE_IS_TIGON_I(ap) 0
  87. #define ACE_TX_RING_ENTRIES(ap) MAX_TX_RING_ENTRIES
  88. #else
  89. #define ACE_IS_TIGON_I(ap) (ap->version == 1)
  90. #define ACE_TX_RING_ENTRIES(ap) ap->tx_ring_entries
  91. #endif
  92. #ifndef PCI_VENDOR_ID_ALTEON
  93. #define PCI_VENDOR_ID_ALTEON 0x12ae
  94. #endif
  95. #ifndef PCI_DEVICE_ID_ALTEON_ACENIC_FIBRE
  96. #define PCI_DEVICE_ID_ALTEON_ACENIC_FIBRE 0x0001
  97. #define PCI_DEVICE_ID_ALTEON_ACENIC_COPPER 0x0002
  98. #endif
  99. #ifndef PCI_DEVICE_ID_3COM_3C985
  100. #define PCI_DEVICE_ID_3COM_3C985 0x0001
  101. #endif
  102. #ifndef PCI_VENDOR_ID_NETGEAR
  103. #define PCI_VENDOR_ID_NETGEAR 0x1385
  104. #define PCI_DEVICE_ID_NETGEAR_GA620 0x620a
  105. #endif
  106. #ifndef PCI_DEVICE_ID_NETGEAR_GA620T
  107. #define PCI_DEVICE_ID_NETGEAR_GA620T 0x630a
  108. #endif
  109. /*
  110. * Farallon used the DEC vendor ID by mistake and they seem not
  111. * to care - stinky!
  112. */
  113. #ifndef PCI_DEVICE_ID_FARALLON_PN9000SX
  114. #define PCI_DEVICE_ID_FARALLON_PN9000SX 0x1a
  115. #endif
  116. #ifndef PCI_DEVICE_ID_FARALLON_PN9100T
  117. #define PCI_DEVICE_ID_FARALLON_PN9100T 0xfa
  118. #endif
  119. #ifndef PCI_VENDOR_ID_SGI
  120. #define PCI_VENDOR_ID_SGI 0x10a9
  121. #endif
  122. #ifndef PCI_DEVICE_ID_SGI_ACENIC
  123. #define PCI_DEVICE_ID_SGI_ACENIC 0x0009
  124. #endif
  125. static DEFINE_PCI_DEVICE_TABLE(acenic_pci_tbl) = {
  126. { PCI_VENDOR_ID_ALTEON, PCI_DEVICE_ID_ALTEON_ACENIC_FIBRE,
  127. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, },
  128. { PCI_VENDOR_ID_ALTEON, PCI_DEVICE_ID_ALTEON_ACENIC_COPPER,
  129. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, },
  130. { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3C985,
  131. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, },
  132. { PCI_VENDOR_ID_NETGEAR, PCI_DEVICE_ID_NETGEAR_GA620,
  133. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, },
  134. { PCI_VENDOR_ID_NETGEAR, PCI_DEVICE_ID_NETGEAR_GA620T,
  135. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, },
  136. /*
  137. * Farallon used the DEC vendor ID on their cards incorrectly,
  138. * then later Alteon's ID.
  139. */
  140. { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_FARALLON_PN9000SX,
  141. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, },
  142. { PCI_VENDOR_ID_ALTEON, PCI_DEVICE_ID_FARALLON_PN9100T,
  143. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, },
  144. { PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_ACENIC,
  145. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, },
  146. { }
  147. };
  148. MODULE_DEVICE_TABLE(pci, acenic_pci_tbl);
  149. #define ace_sync_irq(irq) synchronize_irq(irq)
  150. #ifndef offset_in_page
  151. #define offset_in_page(ptr) ((unsigned long)(ptr) & ~PAGE_MASK)
  152. #endif
  153. #define ACE_MAX_MOD_PARMS 8
  154. #define BOARD_IDX_STATIC 0
  155. #define BOARD_IDX_OVERFLOW -1
  156. #if (defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)) && \
  157. defined(NETIF_F_HW_VLAN_RX)
  158. #define ACENIC_DO_VLAN 1
  159. #define ACE_RCB_VLAN_FLAG RCB_FLG_VLAN_ASSIST
  160. #else
  161. #define ACENIC_DO_VLAN 0
  162. #define ACE_RCB_VLAN_FLAG 0
  163. #endif
  164. #include "acenic.h"
  165. /*
  166. * These must be defined before the firmware is included.
  167. */
  168. #define MAX_TEXT_LEN 96*1024
  169. #define MAX_RODATA_LEN 8*1024
  170. #define MAX_DATA_LEN 2*1024
  171. #ifndef tigon2FwReleaseLocal
  172. #define tigon2FwReleaseLocal 0
  173. #endif
  174. /*
  175. * This driver currently supports Tigon I and Tigon II based cards
  176. * including the Alteon AceNIC, the 3Com 3C985[B] and NetGear
  177. * GA620. The driver should also work on the SGI, DEC and Farallon
  178. * versions of the card, however I have not been able to test that
  179. * myself.
  180. *
  181. * This card is really neat, it supports receive hardware checksumming
  182. * and jumbo frames (up to 9000 bytes) and does a lot of work in the
  183. * firmware. Also the programming interface is quite neat, except for
  184. * the parts dealing with the i2c eeprom on the card ;-)
  185. *
  186. * Using jumbo frames:
  187. *
  188. * To enable jumbo frames, simply specify an mtu between 1500 and 9000
  189. * bytes to ifconfig. Jumbo frames can be enabled or disabled at any time
  190. * by running `ifconfig eth<X> mtu <MTU>' with <X> being the Ethernet
  191. * interface number and <MTU> being the MTU value.
  192. *
  193. * Module parameters:
  194. *
  195. * When compiled as a loadable module, the driver allows for a number
  196. * of module parameters to be specified. The driver supports the
  197. * following module parameters:
  198. *
  199. * trace=<val> - Firmware trace level. This requires special traced
  200. * firmware to replace the firmware supplied with
  201. * the driver - for debugging purposes only.
  202. *
  203. * link=<val> - Link state. Normally you want to use the default link
  204. * parameters set by the driver. This can be used to
  205. * override these in case your switch doesn't negotiate
  206. * the link properly. Valid values are:
  207. * 0x0001 - Force half duplex link.
  208. * 0x0002 - Do not negotiate line speed with the other end.
  209. * 0x0010 - 10Mbit/sec link.
  210. * 0x0020 - 100Mbit/sec link.
  211. * 0x0040 - 1000Mbit/sec link.
  212. * 0x0100 - Do not negotiate flow control.
  213. * 0x0200 - Enable RX flow control Y
  214. * 0x0400 - Enable TX flow control Y (Tigon II NICs only).
  215. * Default value is 0x0270, ie. enable link+flow
  216. * control negotiation. Negotiating the highest
  217. * possible link speed with RX flow control enabled.
  218. *
  219. * When disabling link speed negotiation, only one link
  220. * speed is allowed to be specified!
  221. *
  222. * tx_coal_tick=<val> - number of coalescing clock ticks (us) allowed
  223. * to wait for more packets to arive before
  224. * interrupting the host, from the time the first
  225. * packet arrives.
  226. *
  227. * rx_coal_tick=<val> - number of coalescing clock ticks (us) allowed
  228. * to wait for more packets to arive in the transmit ring,
  229. * before interrupting the host, after transmitting the
  230. * first packet in the ring.
  231. *
  232. * max_tx_desc=<val> - maximum number of transmit descriptors
  233. * (packets) transmitted before interrupting the host.
  234. *
  235. * max_rx_desc=<val> - maximum number of receive descriptors
  236. * (packets) received before interrupting the host.
  237. *
  238. * tx_ratio=<val> - 7 bit value (0 - 63) specifying the split in 64th
  239. * increments of the NIC's on board memory to be used for
  240. * transmit and receive buffers. For the 1MB NIC app. 800KB
  241. * is available, on the 1/2MB NIC app. 300KB is available.
  242. * 68KB will always be available as a minimum for both
  243. * directions. The default value is a 50/50 split.
  244. * dis_pci_mem_inval=<val> - disable PCI memory write and invalidate
  245. * operations, default (1) is to always disable this as
  246. * that is what Alteon does on NT. I have not been able
  247. * to measure any real performance differences with
  248. * this on my systems. Set <val>=0 if you want to
  249. * enable these operations.
  250. *
  251. * If you use more than one NIC, specify the parameters for the
  252. * individual NICs with a comma, ie. trace=0,0x00001fff,0 you want to
  253. * run tracing on NIC #2 but not on NIC #1 and #3.
  254. *
  255. * TODO:
  256. *
  257. * - Proper multicast support.
  258. * - NIC dump support.
  259. * - More tuning parameters.
  260. *
  261. * The mini ring is not used under Linux and I am not sure it makes sense
  262. * to actually use it.
  263. *
  264. * New interrupt handler strategy:
  265. *
  266. * The old interrupt handler worked using the traditional method of
  267. * replacing an skbuff with a new one when a packet arrives. However
  268. * the rx rings do not need to contain a static number of buffer
  269. * descriptors, thus it makes sense to move the memory allocation out
  270. * of the main interrupt handler and do it in a bottom half handler
  271. * and only allocate new buffers when the number of buffers in the
  272. * ring is below a certain threshold. In order to avoid starving the
  273. * NIC under heavy load it is however necessary to force allocation
  274. * when hitting a minimum threshold. The strategy for alloction is as
  275. * follows:
  276. *
  277. * RX_LOW_BUF_THRES - allocate buffers in the bottom half
  278. * RX_PANIC_LOW_THRES - we are very low on buffers, allocate
  279. * the buffers in the interrupt handler
  280. * RX_RING_THRES - maximum number of buffers in the rx ring
  281. * RX_MINI_THRES - maximum number of buffers in the mini ring
  282. * RX_JUMBO_THRES - maximum number of buffers in the jumbo ring
  283. *
  284. * One advantagous side effect of this allocation approach is that the
  285. * entire rx processing can be done without holding any spin lock
  286. * since the rx rings and registers are totally independent of the tx
  287. * ring and its registers. This of course includes the kmalloc's of
  288. * new skb's. Thus start_xmit can run in parallel with rx processing
  289. * and the memory allocation on SMP systems.
  290. *
  291. * Note that running the skb reallocation in a bottom half opens up
  292. * another can of races which needs to be handled properly. In
  293. * particular it can happen that the interrupt handler tries to run
  294. * the reallocation while the bottom half is either running on another
  295. * CPU or was interrupted on the same CPU. To get around this the
  296. * driver uses bitops to prevent the reallocation routines from being
  297. * reentered.
  298. *
  299. * TX handling can also be done without holding any spin lock, wheee
  300. * this is fun! since tx_ret_csm is only written to by the interrupt
  301. * handler. The case to be aware of is when shutting down the device
  302. * and cleaning up where it is necessary to make sure that
  303. * start_xmit() is not running while this is happening. Well DaveM
  304. * informs me that this case is already protected against ... bye bye
  305. * Mr. Spin Lock, it was nice to know you.
  306. *
  307. * TX interrupts are now partly disabled so the NIC will only generate
  308. * TX interrupts for the number of coal ticks, not for the number of
  309. * TX packets in the queue. This should reduce the number of TX only,
  310. * ie. when no RX processing is done, interrupts seen.
  311. */
  312. /*
  313. * Threshold values for RX buffer allocation - the low water marks for
  314. * when to start refilling the rings are set to 75% of the ring
  315. * sizes. It seems to make sense to refill the rings entirely from the
  316. * intrrupt handler once it gets below the panic threshold, that way
  317. * we don't risk that the refilling is moved to another CPU when the
  318. * one running the interrupt handler just got the slab code hot in its
  319. * cache.
  320. */
  321. #define RX_RING_SIZE 72
  322. #define RX_MINI_SIZE 64
  323. #define RX_JUMBO_SIZE 48
  324. #define RX_PANIC_STD_THRES 16
  325. #define RX_PANIC_STD_REFILL (3*RX_PANIC_STD_THRES)/2
  326. #define RX_LOW_STD_THRES (3*RX_RING_SIZE)/4
  327. #define RX_PANIC_MINI_THRES 12
  328. #define RX_PANIC_MINI_REFILL (3*RX_PANIC_MINI_THRES)/2
  329. #define RX_LOW_MINI_THRES (3*RX_MINI_SIZE)/4
  330. #define RX_PANIC_JUMBO_THRES 6
  331. #define RX_PANIC_JUMBO_REFILL (3*RX_PANIC_JUMBO_THRES)/2
  332. #define RX_LOW_JUMBO_THRES (3*RX_JUMBO_SIZE)/4
  333. /*
  334. * Size of the mini ring entries, basically these just should be big
  335. * enough to take TCP ACKs
  336. */
  337. #define ACE_MINI_SIZE 100
  338. #define ACE_MINI_BUFSIZE ACE_MINI_SIZE
  339. #define ACE_STD_BUFSIZE (ACE_STD_MTU + ETH_HLEN + 4)
  340. #define ACE_JUMBO_BUFSIZE (ACE_JUMBO_MTU + ETH_HLEN + 4)
  341. /*
  342. * There seems to be a magic difference in the effect between 995 and 996
  343. * but little difference between 900 and 995 ... no idea why.
  344. *
  345. * There is now a default set of tuning parameters which is set, depending
  346. * on whether or not the user enables Jumbo frames. It's assumed that if
  347. * Jumbo frames are enabled, the user wants optimal tuning for that case.
  348. */
  349. #define DEF_TX_COAL 400 /* 996 */
  350. #define DEF_TX_MAX_DESC 60 /* was 40 */
  351. #define DEF_RX_COAL 120 /* 1000 */
  352. #define DEF_RX_MAX_DESC 25
  353. #define DEF_TX_RATIO 21 /* 24 */
  354. #define DEF_JUMBO_TX_COAL 20
  355. #define DEF_JUMBO_TX_MAX_DESC 60
  356. #define DEF_JUMBO_RX_COAL 30
  357. #define DEF_JUMBO_RX_MAX_DESC 6
  358. #define DEF_JUMBO_TX_RATIO 21
  359. #if tigon2FwReleaseLocal < 20001118
  360. /*
  361. * Standard firmware and early modifications duplicate
  362. * IRQ load without this flag (coal timer is never reset).
  363. * Note that with this flag tx_coal should be less than
  364. * time to xmit full tx ring.
  365. * 400usec is not so bad for tx ring size of 128.
  366. */
  367. #define TX_COAL_INTS_ONLY 1 /* worth it */
  368. #else
  369. /*
  370. * With modified firmware, this is not necessary, but still useful.
  371. */
  372. #define TX_COAL_INTS_ONLY 1
  373. #endif
  374. #define DEF_TRACE 0
  375. #define DEF_STAT (2 * TICKS_PER_SEC)
  376. static int link_state[ACE_MAX_MOD_PARMS];
  377. static int trace[ACE_MAX_MOD_PARMS];
  378. static int tx_coal_tick[ACE_MAX_MOD_PARMS];
  379. static int rx_coal_tick[ACE_MAX_MOD_PARMS];
  380. static int max_tx_desc[ACE_MAX_MOD_PARMS];
  381. static int max_rx_desc[ACE_MAX_MOD_PARMS];
  382. static int tx_ratio[ACE_MAX_MOD_PARMS];
  383. static int dis_pci_mem_inval[ACE_MAX_MOD_PARMS] = {1, 1, 1, 1, 1, 1, 1, 1};
  384. MODULE_AUTHOR("Jes Sorensen <jes@trained-monkey.org>");
  385. MODULE_LICENSE("GPL");
  386. MODULE_DESCRIPTION("AceNIC/3C985/GA620 Gigabit Ethernet driver");
  387. #ifndef CONFIG_ACENIC_OMIT_TIGON_I
  388. MODULE_FIRMWARE("acenic/tg1.bin");
  389. #endif
  390. MODULE_FIRMWARE("acenic/tg2.bin");
  391. module_param_array_named(link, link_state, int, NULL, 0);
  392. module_param_array(trace, int, NULL, 0);
  393. module_param_array(tx_coal_tick, int, NULL, 0);
  394. module_param_array(max_tx_desc, int, NULL, 0);
  395. module_param_array(rx_coal_tick, int, NULL, 0);
  396. module_param_array(max_rx_desc, int, NULL, 0);
  397. module_param_array(tx_ratio, int, NULL, 0);
  398. MODULE_PARM_DESC(link, "AceNIC/3C985/NetGear link state");
  399. MODULE_PARM_DESC(trace, "AceNIC/3C985/NetGear firmware trace level");
  400. MODULE_PARM_DESC(tx_coal_tick, "AceNIC/3C985/GA620 max clock ticks to wait from first tx descriptor arrives");
  401. MODULE_PARM_DESC(max_tx_desc, "AceNIC/3C985/GA620 max number of transmit descriptors to wait");
  402. MODULE_PARM_DESC(rx_coal_tick, "AceNIC/3C985/GA620 max clock ticks to wait from first rx descriptor arrives");
  403. MODULE_PARM_DESC(max_rx_desc, "AceNIC/3C985/GA620 max number of receive descriptors to wait");
  404. MODULE_PARM_DESC(tx_ratio, "AceNIC/3C985/GA620 ratio of NIC memory used for TX/RX descriptors (range 0-63)");
  405. static const char version[] __devinitconst =
  406. "acenic.c: v0.92 08/05/2002 Jes Sorensen, linux-acenic@SunSITE.dk\n"
  407. " http://home.cern.ch/~jes/gige/acenic.html\n";
  408. static int ace_get_settings(struct net_device *, struct ethtool_cmd *);
  409. static int ace_set_settings(struct net_device *, struct ethtool_cmd *);
  410. static void ace_get_drvinfo(struct net_device *, struct ethtool_drvinfo *);
  411. static const struct ethtool_ops ace_ethtool_ops = {
  412. .get_settings = ace_get_settings,
  413. .set_settings = ace_set_settings,
  414. .get_drvinfo = ace_get_drvinfo,
  415. };
  416. static void ace_watchdog(struct net_device *dev);
  417. static const struct net_device_ops ace_netdev_ops = {
  418. .ndo_open = ace_open,
  419. .ndo_stop = ace_close,
  420. .ndo_tx_timeout = ace_watchdog,
  421. .ndo_get_stats = ace_get_stats,
  422. .ndo_start_xmit = ace_start_xmit,
  423. .ndo_set_multicast_list = ace_set_multicast_list,
  424. .ndo_validate_addr = eth_validate_addr,
  425. .ndo_set_mac_address = ace_set_mac_addr,
  426. .ndo_change_mtu = ace_change_mtu,
  427. #if ACENIC_DO_VLAN
  428. .ndo_vlan_rx_register = ace_vlan_rx_register,
  429. #endif
  430. };
  431. static int __devinit acenic_probe_one(struct pci_dev *pdev,
  432. const struct pci_device_id *id)
  433. {
  434. struct net_device *dev;
  435. struct ace_private *ap;
  436. static int boards_found;
  437. dev = alloc_etherdev(sizeof(struct ace_private));
  438. if (dev == NULL) {
  439. printk(KERN_ERR "acenic: Unable to allocate "
  440. "net_device structure!\n");
  441. return -ENOMEM;
  442. }
  443. SET_NETDEV_DEV(dev, &pdev->dev);
  444. ap = netdev_priv(dev);
  445. ap->pdev = pdev;
  446. ap->name = pci_name(pdev);
  447. dev->features |= NETIF_F_SG | NETIF_F_IP_CSUM;
  448. #if ACENIC_DO_VLAN
  449. dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  450. #endif
  451. dev->watchdog_timeo = 5*HZ;
  452. dev->netdev_ops = &ace_netdev_ops;
  453. SET_ETHTOOL_OPS(dev, &ace_ethtool_ops);
  454. /* we only display this string ONCE */
  455. if (!boards_found)
  456. printk(version);
  457. if (pci_enable_device(pdev))
  458. goto fail_free_netdev;
  459. /*
  460. * Enable master mode before we start playing with the
  461. * pci_command word since pci_set_master() will modify
  462. * it.
  463. */
  464. pci_set_master(pdev);
  465. pci_read_config_word(pdev, PCI_COMMAND, &ap->pci_command);
  466. /* OpenFirmware on Mac's does not set this - DOH.. */
  467. if (!(ap->pci_command & PCI_COMMAND_MEMORY)) {
  468. printk(KERN_INFO "%s: Enabling PCI Memory Mapped "
  469. "access - was not enabled by BIOS/Firmware\n",
  470. ap->name);
  471. ap->pci_command = ap->pci_command | PCI_COMMAND_MEMORY;
  472. pci_write_config_word(ap->pdev, PCI_COMMAND,
  473. ap->pci_command);
  474. wmb();
  475. }
  476. pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &ap->pci_latency);
  477. if (ap->pci_latency <= 0x40) {
  478. ap->pci_latency = 0x40;
  479. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, ap->pci_latency);
  480. }
  481. /*
  482. * Remap the regs into kernel space - this is abuse of
  483. * dev->base_addr since it was means for I/O port
  484. * addresses but who gives a damn.
  485. */
  486. dev->base_addr = pci_resource_start(pdev, 0);
  487. ap->regs = ioremap(dev->base_addr, 0x4000);
  488. if (!ap->regs) {
  489. printk(KERN_ERR "%s: Unable to map I/O register, "
  490. "AceNIC %i will be disabled.\n",
  491. ap->name, boards_found);
  492. goto fail_free_netdev;
  493. }
  494. switch(pdev->vendor) {
  495. case PCI_VENDOR_ID_ALTEON:
  496. if (pdev->device == PCI_DEVICE_ID_FARALLON_PN9100T) {
  497. printk(KERN_INFO "%s: Farallon PN9100-T ",
  498. ap->name);
  499. } else {
  500. printk(KERN_INFO "%s: Alteon AceNIC ",
  501. ap->name);
  502. }
  503. break;
  504. case PCI_VENDOR_ID_3COM:
  505. printk(KERN_INFO "%s: 3Com 3C985 ", ap->name);
  506. break;
  507. case PCI_VENDOR_ID_NETGEAR:
  508. printk(KERN_INFO "%s: NetGear GA620 ", ap->name);
  509. break;
  510. case PCI_VENDOR_ID_DEC:
  511. if (pdev->device == PCI_DEVICE_ID_FARALLON_PN9000SX) {
  512. printk(KERN_INFO "%s: Farallon PN9000-SX ",
  513. ap->name);
  514. break;
  515. }
  516. case PCI_VENDOR_ID_SGI:
  517. printk(KERN_INFO "%s: SGI AceNIC ", ap->name);
  518. break;
  519. default:
  520. printk(KERN_INFO "%s: Unknown AceNIC ", ap->name);
  521. break;
  522. }
  523. printk("Gigabit Ethernet at 0x%08lx, ", dev->base_addr);
  524. printk("irq %d\n", pdev->irq);
  525. #ifdef CONFIG_ACENIC_OMIT_TIGON_I
  526. if ((readl(&ap->regs->HostCtrl) >> 28) == 4) {
  527. printk(KERN_ERR "%s: Driver compiled without Tigon I"
  528. " support - NIC disabled\n", dev->name);
  529. goto fail_uninit;
  530. }
  531. #endif
  532. if (ace_allocate_descriptors(dev))
  533. goto fail_free_netdev;
  534. #ifdef MODULE
  535. if (boards_found >= ACE_MAX_MOD_PARMS)
  536. ap->board_idx = BOARD_IDX_OVERFLOW;
  537. else
  538. ap->board_idx = boards_found;
  539. #else
  540. ap->board_idx = BOARD_IDX_STATIC;
  541. #endif
  542. if (ace_init(dev))
  543. goto fail_free_netdev;
  544. if (register_netdev(dev)) {
  545. printk(KERN_ERR "acenic: device registration failed\n");
  546. goto fail_uninit;
  547. }
  548. ap->name = dev->name;
  549. if (ap->pci_using_dac)
  550. dev->features |= NETIF_F_HIGHDMA;
  551. pci_set_drvdata(pdev, dev);
  552. boards_found++;
  553. return 0;
  554. fail_uninit:
  555. ace_init_cleanup(dev);
  556. fail_free_netdev:
  557. free_netdev(dev);
  558. return -ENODEV;
  559. }
  560. static void __devexit acenic_remove_one(struct pci_dev *pdev)
  561. {
  562. struct net_device *dev = pci_get_drvdata(pdev);
  563. struct ace_private *ap = netdev_priv(dev);
  564. struct ace_regs __iomem *regs = ap->regs;
  565. short i;
  566. unregister_netdev(dev);
  567. writel(readl(&regs->CpuCtrl) | CPU_HALT, &regs->CpuCtrl);
  568. if (ap->version >= 2)
  569. writel(readl(&regs->CpuBCtrl) | CPU_HALT, &regs->CpuBCtrl);
  570. /*
  571. * This clears any pending interrupts
  572. */
  573. writel(1, &regs->Mb0Lo);
  574. readl(&regs->CpuCtrl); /* flush */
  575. /*
  576. * Make sure no other CPUs are processing interrupts
  577. * on the card before the buffers are being released.
  578. * Otherwise one might experience some `interesting'
  579. * effects.
  580. *
  581. * Then release the RX buffers - jumbo buffers were
  582. * already released in ace_close().
  583. */
  584. ace_sync_irq(dev->irq);
  585. for (i = 0; i < RX_STD_RING_ENTRIES; i++) {
  586. struct sk_buff *skb = ap->skb->rx_std_skbuff[i].skb;
  587. if (skb) {
  588. struct ring_info *ringp;
  589. dma_addr_t mapping;
  590. ringp = &ap->skb->rx_std_skbuff[i];
  591. mapping = dma_unmap_addr(ringp, mapping);
  592. pci_unmap_page(ap->pdev, mapping,
  593. ACE_STD_BUFSIZE,
  594. PCI_DMA_FROMDEVICE);
  595. ap->rx_std_ring[i].size = 0;
  596. ap->skb->rx_std_skbuff[i].skb = NULL;
  597. dev_kfree_skb(skb);
  598. }
  599. }
  600. if (ap->version >= 2) {
  601. for (i = 0; i < RX_MINI_RING_ENTRIES; i++) {
  602. struct sk_buff *skb = ap->skb->rx_mini_skbuff[i].skb;
  603. if (skb) {
  604. struct ring_info *ringp;
  605. dma_addr_t mapping;
  606. ringp = &ap->skb->rx_mini_skbuff[i];
  607. mapping = dma_unmap_addr(ringp,mapping);
  608. pci_unmap_page(ap->pdev, mapping,
  609. ACE_MINI_BUFSIZE,
  610. PCI_DMA_FROMDEVICE);
  611. ap->rx_mini_ring[i].size = 0;
  612. ap->skb->rx_mini_skbuff[i].skb = NULL;
  613. dev_kfree_skb(skb);
  614. }
  615. }
  616. }
  617. for (i = 0; i < RX_JUMBO_RING_ENTRIES; i++) {
  618. struct sk_buff *skb = ap->skb->rx_jumbo_skbuff[i].skb;
  619. if (skb) {
  620. struct ring_info *ringp;
  621. dma_addr_t mapping;
  622. ringp = &ap->skb->rx_jumbo_skbuff[i];
  623. mapping = dma_unmap_addr(ringp, mapping);
  624. pci_unmap_page(ap->pdev, mapping,
  625. ACE_JUMBO_BUFSIZE,
  626. PCI_DMA_FROMDEVICE);
  627. ap->rx_jumbo_ring[i].size = 0;
  628. ap->skb->rx_jumbo_skbuff[i].skb = NULL;
  629. dev_kfree_skb(skb);
  630. }
  631. }
  632. ace_init_cleanup(dev);
  633. free_netdev(dev);
  634. }
  635. static struct pci_driver acenic_pci_driver = {
  636. .name = "acenic",
  637. .id_table = acenic_pci_tbl,
  638. .probe = acenic_probe_one,
  639. .remove = __devexit_p(acenic_remove_one),
  640. };
  641. static int __init acenic_init(void)
  642. {
  643. return pci_register_driver(&acenic_pci_driver);
  644. }
  645. static void __exit acenic_exit(void)
  646. {
  647. pci_unregister_driver(&acenic_pci_driver);
  648. }
  649. module_init(acenic_init);
  650. module_exit(acenic_exit);
  651. static void ace_free_descriptors(struct net_device *dev)
  652. {
  653. struct ace_private *ap = netdev_priv(dev);
  654. int size;
  655. if (ap->rx_std_ring != NULL) {
  656. size = (sizeof(struct rx_desc) *
  657. (RX_STD_RING_ENTRIES +
  658. RX_JUMBO_RING_ENTRIES +
  659. RX_MINI_RING_ENTRIES +
  660. RX_RETURN_RING_ENTRIES));
  661. pci_free_consistent(ap->pdev, size, ap->rx_std_ring,
  662. ap->rx_ring_base_dma);
  663. ap->rx_std_ring = NULL;
  664. ap->rx_jumbo_ring = NULL;
  665. ap->rx_mini_ring = NULL;
  666. ap->rx_return_ring = NULL;
  667. }
  668. if (ap->evt_ring != NULL) {
  669. size = (sizeof(struct event) * EVT_RING_ENTRIES);
  670. pci_free_consistent(ap->pdev, size, ap->evt_ring,
  671. ap->evt_ring_dma);
  672. ap->evt_ring = NULL;
  673. }
  674. if (ap->tx_ring != NULL && !ACE_IS_TIGON_I(ap)) {
  675. size = (sizeof(struct tx_desc) * MAX_TX_RING_ENTRIES);
  676. pci_free_consistent(ap->pdev, size, ap->tx_ring,
  677. ap->tx_ring_dma);
  678. }
  679. ap->tx_ring = NULL;
  680. if (ap->evt_prd != NULL) {
  681. pci_free_consistent(ap->pdev, sizeof(u32),
  682. (void *)ap->evt_prd, ap->evt_prd_dma);
  683. ap->evt_prd = NULL;
  684. }
  685. if (ap->rx_ret_prd != NULL) {
  686. pci_free_consistent(ap->pdev, sizeof(u32),
  687. (void *)ap->rx_ret_prd,
  688. ap->rx_ret_prd_dma);
  689. ap->rx_ret_prd = NULL;
  690. }
  691. if (ap->tx_csm != NULL) {
  692. pci_free_consistent(ap->pdev, sizeof(u32),
  693. (void *)ap->tx_csm, ap->tx_csm_dma);
  694. ap->tx_csm = NULL;
  695. }
  696. }
  697. static int ace_allocate_descriptors(struct net_device *dev)
  698. {
  699. struct ace_private *ap = netdev_priv(dev);
  700. int size;
  701. size = (sizeof(struct rx_desc) *
  702. (RX_STD_RING_ENTRIES +
  703. RX_JUMBO_RING_ENTRIES +
  704. RX_MINI_RING_ENTRIES +
  705. RX_RETURN_RING_ENTRIES));
  706. ap->rx_std_ring = pci_alloc_consistent(ap->pdev, size,
  707. &ap->rx_ring_base_dma);
  708. if (ap->rx_std_ring == NULL)
  709. goto fail;
  710. ap->rx_jumbo_ring = ap->rx_std_ring + RX_STD_RING_ENTRIES;
  711. ap->rx_mini_ring = ap->rx_jumbo_ring + RX_JUMBO_RING_ENTRIES;
  712. ap->rx_return_ring = ap->rx_mini_ring + RX_MINI_RING_ENTRIES;
  713. size = (sizeof(struct event) * EVT_RING_ENTRIES);
  714. ap->evt_ring = pci_alloc_consistent(ap->pdev, size, &ap->evt_ring_dma);
  715. if (ap->evt_ring == NULL)
  716. goto fail;
  717. /*
  718. * Only allocate a host TX ring for the Tigon II, the Tigon I
  719. * has to use PCI registers for this ;-(
  720. */
  721. if (!ACE_IS_TIGON_I(ap)) {
  722. size = (sizeof(struct tx_desc) * MAX_TX_RING_ENTRIES);
  723. ap->tx_ring = pci_alloc_consistent(ap->pdev, size,
  724. &ap->tx_ring_dma);
  725. if (ap->tx_ring == NULL)
  726. goto fail;
  727. }
  728. ap->evt_prd = pci_alloc_consistent(ap->pdev, sizeof(u32),
  729. &ap->evt_prd_dma);
  730. if (ap->evt_prd == NULL)
  731. goto fail;
  732. ap->rx_ret_prd = pci_alloc_consistent(ap->pdev, sizeof(u32),
  733. &ap->rx_ret_prd_dma);
  734. if (ap->rx_ret_prd == NULL)
  735. goto fail;
  736. ap->tx_csm = pci_alloc_consistent(ap->pdev, sizeof(u32),
  737. &ap->tx_csm_dma);
  738. if (ap->tx_csm == NULL)
  739. goto fail;
  740. return 0;
  741. fail:
  742. /* Clean up. */
  743. ace_init_cleanup(dev);
  744. return 1;
  745. }
  746. /*
  747. * Generic cleanup handling data allocated during init. Used when the
  748. * module is unloaded or if an error occurs during initialization
  749. */
  750. static void ace_init_cleanup(struct net_device *dev)
  751. {
  752. struct ace_private *ap;
  753. ap = netdev_priv(dev);
  754. ace_free_descriptors(dev);
  755. if (ap->info)
  756. pci_free_consistent(ap->pdev, sizeof(struct ace_info),
  757. ap->info, ap->info_dma);
  758. kfree(ap->skb);
  759. kfree(ap->trace_buf);
  760. if (dev->irq)
  761. free_irq(dev->irq, dev);
  762. iounmap(ap->regs);
  763. }
  764. /*
  765. * Commands are considered to be slow.
  766. */
  767. static inline void ace_issue_cmd(struct ace_regs __iomem *regs, struct cmd *cmd)
  768. {
  769. u32 idx;
  770. idx = readl(&regs->CmdPrd);
  771. writel(*(u32 *)(cmd), &regs->CmdRng[idx]);
  772. idx = (idx + 1) % CMD_RING_ENTRIES;
  773. writel(idx, &regs->CmdPrd);
  774. }
  775. static int __devinit ace_init(struct net_device *dev)
  776. {
  777. struct ace_private *ap;
  778. struct ace_regs __iomem *regs;
  779. struct ace_info *info = NULL;
  780. struct pci_dev *pdev;
  781. unsigned long myjif;
  782. u64 tmp_ptr;
  783. u32 tig_ver, mac1, mac2, tmp, pci_state;
  784. int board_idx, ecode = 0;
  785. short i;
  786. unsigned char cache_size;
  787. ap = netdev_priv(dev);
  788. regs = ap->regs;
  789. board_idx = ap->board_idx;
  790. /*
  791. * aman@sgi.com - its useful to do a NIC reset here to
  792. * address the `Firmware not running' problem subsequent
  793. * to any crashes involving the NIC
  794. */
  795. writel(HW_RESET | (HW_RESET << 24), &regs->HostCtrl);
  796. readl(&regs->HostCtrl); /* PCI write posting */
  797. udelay(5);
  798. /*
  799. * Don't access any other registers before this point!
  800. */
  801. #ifdef __BIG_ENDIAN
  802. /*
  803. * This will most likely need BYTE_SWAP once we switch
  804. * to using __raw_writel()
  805. */
  806. writel((WORD_SWAP | CLR_INT | ((WORD_SWAP | CLR_INT) << 24)),
  807. &regs->HostCtrl);
  808. #else
  809. writel((CLR_INT | WORD_SWAP | ((CLR_INT | WORD_SWAP) << 24)),
  810. &regs->HostCtrl);
  811. #endif
  812. readl(&regs->HostCtrl); /* PCI write posting */
  813. /*
  814. * Stop the NIC CPU and clear pending interrupts
  815. */
  816. writel(readl(&regs->CpuCtrl) | CPU_HALT, &regs->CpuCtrl);
  817. readl(&regs->CpuCtrl); /* PCI write posting */
  818. writel(0, &regs->Mb0Lo);
  819. tig_ver = readl(&regs->HostCtrl) >> 28;
  820. switch(tig_ver){
  821. #ifndef CONFIG_ACENIC_OMIT_TIGON_I
  822. case 4:
  823. case 5:
  824. printk(KERN_INFO " Tigon I (Rev. %i), Firmware: %i.%i.%i, ",
  825. tig_ver, ap->firmware_major, ap->firmware_minor,
  826. ap->firmware_fix);
  827. writel(0, &regs->LocalCtrl);
  828. ap->version = 1;
  829. ap->tx_ring_entries = TIGON_I_TX_RING_ENTRIES;
  830. break;
  831. #endif
  832. case 6:
  833. printk(KERN_INFO " Tigon II (Rev. %i), Firmware: %i.%i.%i, ",
  834. tig_ver, ap->firmware_major, ap->firmware_minor,
  835. ap->firmware_fix);
  836. writel(readl(&regs->CpuBCtrl) | CPU_HALT, &regs->CpuBCtrl);
  837. readl(&regs->CpuBCtrl); /* PCI write posting */
  838. /*
  839. * The SRAM bank size does _not_ indicate the amount
  840. * of memory on the card, it controls the _bank_ size!
  841. * Ie. a 1MB AceNIC will have two banks of 512KB.
  842. */
  843. writel(SRAM_BANK_512K, &regs->LocalCtrl);
  844. writel(SYNC_SRAM_TIMING, &regs->MiscCfg);
  845. ap->version = 2;
  846. ap->tx_ring_entries = MAX_TX_RING_ENTRIES;
  847. break;
  848. default:
  849. printk(KERN_WARNING " Unsupported Tigon version detected "
  850. "(%i)\n", tig_ver);
  851. ecode = -ENODEV;
  852. goto init_error;
  853. }
  854. /*
  855. * ModeStat _must_ be set after the SRAM settings as this change
  856. * seems to corrupt the ModeStat and possible other registers.
  857. * The SRAM settings survive resets and setting it to the same
  858. * value a second time works as well. This is what caused the
  859. * `Firmware not running' problem on the Tigon II.
  860. */
  861. #ifdef __BIG_ENDIAN
  862. writel(ACE_BYTE_SWAP_DMA | ACE_WARN | ACE_FATAL | ACE_BYTE_SWAP_BD |
  863. ACE_WORD_SWAP_BD | ACE_NO_JUMBO_FRAG, &regs->ModeStat);
  864. #else
  865. writel(ACE_BYTE_SWAP_DMA | ACE_WARN | ACE_FATAL |
  866. ACE_WORD_SWAP_BD | ACE_NO_JUMBO_FRAG, &regs->ModeStat);
  867. #endif
  868. readl(&regs->ModeStat); /* PCI write posting */
  869. mac1 = 0;
  870. for(i = 0; i < 4; i++) {
  871. int t;
  872. mac1 = mac1 << 8;
  873. t = read_eeprom_byte(dev, 0x8c+i);
  874. if (t < 0) {
  875. ecode = -EIO;
  876. goto init_error;
  877. } else
  878. mac1 |= (t & 0xff);
  879. }
  880. mac2 = 0;
  881. for(i = 4; i < 8; i++) {
  882. int t;
  883. mac2 = mac2 << 8;
  884. t = read_eeprom_byte(dev, 0x8c+i);
  885. if (t < 0) {
  886. ecode = -EIO;
  887. goto init_error;
  888. } else
  889. mac2 |= (t & 0xff);
  890. }
  891. writel(mac1, &regs->MacAddrHi);
  892. writel(mac2, &regs->MacAddrLo);
  893. dev->dev_addr[0] = (mac1 >> 8) & 0xff;
  894. dev->dev_addr[1] = mac1 & 0xff;
  895. dev->dev_addr[2] = (mac2 >> 24) & 0xff;
  896. dev->dev_addr[3] = (mac2 >> 16) & 0xff;
  897. dev->dev_addr[4] = (mac2 >> 8) & 0xff;
  898. dev->dev_addr[5] = mac2 & 0xff;
  899. printk("MAC: %pM\n", dev->dev_addr);
  900. /*
  901. * Looks like this is necessary to deal with on all architectures,
  902. * even this %$#%$# N440BX Intel based thing doesn't get it right.
  903. * Ie. having two NICs in the machine, one will have the cache
  904. * line set at boot time, the other will not.
  905. */
  906. pdev = ap->pdev;
  907. pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &cache_size);
  908. cache_size <<= 2;
  909. if (cache_size != SMP_CACHE_BYTES) {
  910. printk(KERN_INFO " PCI cache line size set incorrectly "
  911. "(%i bytes) by BIOS/FW, ", cache_size);
  912. if (cache_size > SMP_CACHE_BYTES)
  913. printk("expecting %i\n", SMP_CACHE_BYTES);
  914. else {
  915. printk("correcting to %i\n", SMP_CACHE_BYTES);
  916. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
  917. SMP_CACHE_BYTES >> 2);
  918. }
  919. }
  920. pci_state = readl(&regs->PciState);
  921. printk(KERN_INFO " PCI bus width: %i bits, speed: %iMHz, "
  922. "latency: %i clks\n",
  923. (pci_state & PCI_32BIT) ? 32 : 64,
  924. (pci_state & PCI_66MHZ) ? 66 : 33,
  925. ap->pci_latency);
  926. /*
  927. * Set the max DMA transfer size. Seems that for most systems
  928. * the performance is better when no MAX parameter is
  929. * set. However for systems enabling PCI write and invalidate,
  930. * DMA writes must be set to the L1 cache line size to get
  931. * optimal performance.
  932. *
  933. * The default is now to turn the PCI write and invalidate off
  934. * - that is what Alteon does for NT.
  935. */
  936. tmp = READ_CMD_MEM | WRITE_CMD_MEM;
  937. if (ap->version >= 2) {
  938. tmp |= (MEM_READ_MULTIPLE | (pci_state & PCI_66MHZ));
  939. /*
  940. * Tuning parameters only supported for 8 cards
  941. */
  942. if (board_idx == BOARD_IDX_OVERFLOW ||
  943. dis_pci_mem_inval[board_idx]) {
  944. if (ap->pci_command & PCI_COMMAND_INVALIDATE) {
  945. ap->pci_command &= ~PCI_COMMAND_INVALIDATE;
  946. pci_write_config_word(pdev, PCI_COMMAND,
  947. ap->pci_command);
  948. printk(KERN_INFO " Disabling PCI memory "
  949. "write and invalidate\n");
  950. }
  951. } else if (ap->pci_command & PCI_COMMAND_INVALIDATE) {
  952. printk(KERN_INFO " PCI memory write & invalidate "
  953. "enabled by BIOS, enabling counter measures\n");
  954. switch(SMP_CACHE_BYTES) {
  955. case 16:
  956. tmp |= DMA_WRITE_MAX_16;
  957. break;
  958. case 32:
  959. tmp |= DMA_WRITE_MAX_32;
  960. break;
  961. case 64:
  962. tmp |= DMA_WRITE_MAX_64;
  963. break;
  964. case 128:
  965. tmp |= DMA_WRITE_MAX_128;
  966. break;
  967. default:
  968. printk(KERN_INFO " Cache line size %i not "
  969. "supported, PCI write and invalidate "
  970. "disabled\n", SMP_CACHE_BYTES);
  971. ap->pci_command &= ~PCI_COMMAND_INVALIDATE;
  972. pci_write_config_word(pdev, PCI_COMMAND,
  973. ap->pci_command);
  974. }
  975. }
  976. }
  977. #ifdef __sparc__
  978. /*
  979. * On this platform, we know what the best dma settings
  980. * are. We use 64-byte maximum bursts, because if we
  981. * burst larger than the cache line size (or even cross
  982. * a 64byte boundary in a single burst) the UltraSparc
  983. * PCI controller will disconnect at 64-byte multiples.
  984. *
  985. * Read-multiple will be properly enabled above, and when
  986. * set will give the PCI controller proper hints about
  987. * prefetching.
  988. */
  989. tmp &= ~DMA_READ_WRITE_MASK;
  990. tmp |= DMA_READ_MAX_64;
  991. tmp |= DMA_WRITE_MAX_64;
  992. #endif
  993. #ifdef __alpha__
  994. tmp &= ~DMA_READ_WRITE_MASK;
  995. tmp |= DMA_READ_MAX_128;
  996. /*
  997. * All the docs say MUST NOT. Well, I did.
  998. * Nothing terrible happens, if we load wrong size.
  999. * Bit w&i still works better!
  1000. */
  1001. tmp |= DMA_WRITE_MAX_128;
  1002. #endif
  1003. writel(tmp, &regs->PciState);
  1004. #if 0
  1005. /*
  1006. * The Host PCI bus controller driver has to set FBB.
  1007. * If all devices on that PCI bus support FBB, then the controller
  1008. * can enable FBB support in the Host PCI Bus controller (or on
  1009. * the PCI-PCI bridge if that applies).
  1010. * -ggg
  1011. */
  1012. /*
  1013. * I have received reports from people having problems when this
  1014. * bit is enabled.
  1015. */
  1016. if (!(ap->pci_command & PCI_COMMAND_FAST_BACK)) {
  1017. printk(KERN_INFO " Enabling PCI Fast Back to Back\n");
  1018. ap->pci_command |= PCI_COMMAND_FAST_BACK;
  1019. pci_write_config_word(pdev, PCI_COMMAND, ap->pci_command);
  1020. }
  1021. #endif
  1022. /*
  1023. * Configure DMA attributes.
  1024. */
  1025. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  1026. ap->pci_using_dac = 1;
  1027. } else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
  1028. ap->pci_using_dac = 0;
  1029. } else {
  1030. ecode = -ENODEV;
  1031. goto init_error;
  1032. }
  1033. /*
  1034. * Initialize the generic info block and the command+event rings
  1035. * and the control blocks for the transmit and receive rings
  1036. * as they need to be setup once and for all.
  1037. */
  1038. if (!(info = pci_alloc_consistent(ap->pdev, sizeof(struct ace_info),
  1039. &ap->info_dma))) {
  1040. ecode = -EAGAIN;
  1041. goto init_error;
  1042. }
  1043. ap->info = info;
  1044. /*
  1045. * Get the memory for the skb rings.
  1046. */
  1047. if (!(ap->skb = kmalloc(sizeof(struct ace_skb), GFP_KERNEL))) {
  1048. ecode = -EAGAIN;
  1049. goto init_error;
  1050. }
  1051. ecode = request_irq(pdev->irq, ace_interrupt, IRQF_SHARED,
  1052. DRV_NAME, dev);
  1053. if (ecode) {
  1054. printk(KERN_WARNING "%s: Requested IRQ %d is busy\n",
  1055. DRV_NAME, pdev->irq);
  1056. goto init_error;
  1057. } else
  1058. dev->irq = pdev->irq;
  1059. #ifdef INDEX_DEBUG
  1060. spin_lock_init(&ap->debug_lock);
  1061. ap->last_tx = ACE_TX_RING_ENTRIES(ap) - 1;
  1062. ap->last_std_rx = 0;
  1063. ap->last_mini_rx = 0;
  1064. #endif
  1065. memset(ap->info, 0, sizeof(struct ace_info));
  1066. memset(ap->skb, 0, sizeof(struct ace_skb));
  1067. ecode = ace_load_firmware(dev);
  1068. if (ecode)
  1069. goto init_error;
  1070. ap->fw_running = 0;
  1071. tmp_ptr = ap->info_dma;
  1072. writel(tmp_ptr >> 32, &regs->InfoPtrHi);
  1073. writel(tmp_ptr & 0xffffffff, &regs->InfoPtrLo);
  1074. memset(ap->evt_ring, 0, EVT_RING_ENTRIES * sizeof(struct event));
  1075. set_aceaddr(&info->evt_ctrl.rngptr, ap->evt_ring_dma);
  1076. info->evt_ctrl.flags = 0;
  1077. *(ap->evt_prd) = 0;
  1078. wmb();
  1079. set_aceaddr(&info->evt_prd_ptr, ap->evt_prd_dma);
  1080. writel(0, &regs->EvtCsm);
  1081. set_aceaddr(&info->cmd_ctrl.rngptr, 0x100);
  1082. info->cmd_ctrl.flags = 0;
  1083. info->cmd_ctrl.max_len = 0;
  1084. for (i = 0; i < CMD_RING_ENTRIES; i++)
  1085. writel(0, &regs->CmdRng[i]);
  1086. writel(0, &regs->CmdPrd);
  1087. writel(0, &regs->CmdCsm);
  1088. tmp_ptr = ap->info_dma;
  1089. tmp_ptr += (unsigned long) &(((struct ace_info *)0)->s.stats);
  1090. set_aceaddr(&info->stats2_ptr, (dma_addr_t) tmp_ptr);
  1091. set_aceaddr(&info->rx_std_ctrl.rngptr, ap->rx_ring_base_dma);
  1092. info->rx_std_ctrl.max_len = ACE_STD_BUFSIZE;
  1093. info->rx_std_ctrl.flags =
  1094. RCB_FLG_TCP_UDP_SUM | RCB_FLG_NO_PSEUDO_HDR | ACE_RCB_VLAN_FLAG;
  1095. memset(ap->rx_std_ring, 0,
  1096. RX_STD_RING_ENTRIES * sizeof(struct rx_desc));
  1097. for (i = 0; i < RX_STD_RING_ENTRIES; i++)
  1098. ap->rx_std_ring[i].flags = BD_FLG_TCP_UDP_SUM;
  1099. ap->rx_std_skbprd = 0;
  1100. atomic_set(&ap->cur_rx_bufs, 0);
  1101. set_aceaddr(&info->rx_jumbo_ctrl.rngptr,
  1102. (ap->rx_ring_base_dma +
  1103. (sizeof(struct rx_desc) * RX_STD_RING_ENTRIES)));
  1104. info->rx_jumbo_ctrl.max_len = 0;
  1105. info->rx_jumbo_ctrl.flags =
  1106. RCB_FLG_TCP_UDP_SUM | RCB_FLG_NO_PSEUDO_HDR | ACE_RCB_VLAN_FLAG;
  1107. memset(ap->rx_jumbo_ring, 0,
  1108. RX_JUMBO_RING_ENTRIES * sizeof(struct rx_desc));
  1109. for (i = 0; i < RX_JUMBO_RING_ENTRIES; i++)
  1110. ap->rx_jumbo_ring[i].flags = BD_FLG_TCP_UDP_SUM | BD_FLG_JUMBO;
  1111. ap->rx_jumbo_skbprd = 0;
  1112. atomic_set(&ap->cur_jumbo_bufs, 0);
  1113. memset(ap->rx_mini_ring, 0,
  1114. RX_MINI_RING_ENTRIES * sizeof(struct rx_desc));
  1115. if (ap->version >= 2) {
  1116. set_aceaddr(&info->rx_mini_ctrl.rngptr,
  1117. (ap->rx_ring_base_dma +
  1118. (sizeof(struct rx_desc) *
  1119. (RX_STD_RING_ENTRIES +
  1120. RX_JUMBO_RING_ENTRIES))));
  1121. info->rx_mini_ctrl.max_len = ACE_MINI_SIZE;
  1122. info->rx_mini_ctrl.flags =
  1123. RCB_FLG_TCP_UDP_SUM|RCB_FLG_NO_PSEUDO_HDR|ACE_RCB_VLAN_FLAG;
  1124. for (i = 0; i < RX_MINI_RING_ENTRIES; i++)
  1125. ap->rx_mini_ring[i].flags =
  1126. BD_FLG_TCP_UDP_SUM | BD_FLG_MINI;
  1127. } else {
  1128. set_aceaddr(&info->rx_mini_ctrl.rngptr, 0);
  1129. info->rx_mini_ctrl.flags = RCB_FLG_RNG_DISABLE;
  1130. info->rx_mini_ctrl.max_len = 0;
  1131. }
  1132. ap->rx_mini_skbprd = 0;
  1133. atomic_set(&ap->cur_mini_bufs, 0);
  1134. set_aceaddr(&info->rx_return_ctrl.rngptr,
  1135. (ap->rx_ring_base_dma +
  1136. (sizeof(struct rx_desc) *
  1137. (RX_STD_RING_ENTRIES +
  1138. RX_JUMBO_RING_ENTRIES +
  1139. RX_MINI_RING_ENTRIES))));
  1140. info->rx_return_ctrl.flags = 0;
  1141. info->rx_return_ctrl.max_len = RX_RETURN_RING_ENTRIES;
  1142. memset(ap->rx_return_ring, 0,
  1143. RX_RETURN_RING_ENTRIES * sizeof(struct rx_desc));
  1144. set_aceaddr(&info->rx_ret_prd_ptr, ap->rx_ret_prd_dma);
  1145. *(ap->rx_ret_prd) = 0;
  1146. writel(TX_RING_BASE, &regs->WinBase);
  1147. if (ACE_IS_TIGON_I(ap)) {
  1148. ap->tx_ring = (__force struct tx_desc *) regs->Window;
  1149. for (i = 0; i < (TIGON_I_TX_RING_ENTRIES
  1150. * sizeof(struct tx_desc)) / sizeof(u32); i++)
  1151. writel(0, (__force void __iomem *)ap->tx_ring + i * 4);
  1152. set_aceaddr(&info->tx_ctrl.rngptr, TX_RING_BASE);
  1153. } else {
  1154. memset(ap->tx_ring, 0,
  1155. MAX_TX_RING_ENTRIES * sizeof(struct tx_desc));
  1156. set_aceaddr(&info->tx_ctrl.rngptr, ap->tx_ring_dma);
  1157. }
  1158. info->tx_ctrl.max_len = ACE_TX_RING_ENTRIES(ap);
  1159. tmp = RCB_FLG_TCP_UDP_SUM | RCB_FLG_NO_PSEUDO_HDR | ACE_RCB_VLAN_FLAG;
  1160. /*
  1161. * The Tigon I does not like having the TX ring in host memory ;-(
  1162. */
  1163. if (!ACE_IS_TIGON_I(ap))
  1164. tmp |= RCB_FLG_TX_HOST_RING;
  1165. #if TX_COAL_INTS_ONLY
  1166. tmp |= RCB_FLG_COAL_INT_ONLY;
  1167. #endif
  1168. info->tx_ctrl.flags = tmp;
  1169. set_aceaddr(&info->tx_csm_ptr, ap->tx_csm_dma);
  1170. /*
  1171. * Potential item for tuning parameter
  1172. */
  1173. #if 0 /* NO */
  1174. writel(DMA_THRESH_16W, &regs->DmaReadCfg);
  1175. writel(DMA_THRESH_16W, &regs->DmaWriteCfg);
  1176. #else
  1177. writel(DMA_THRESH_8W, &regs->DmaReadCfg);
  1178. writel(DMA_THRESH_8W, &regs->DmaWriteCfg);
  1179. #endif
  1180. writel(0, &regs->MaskInt);
  1181. writel(1, &regs->IfIdx);
  1182. #if 0
  1183. /*
  1184. * McKinley boxes do not like us fiddling with AssistState
  1185. * this early
  1186. */
  1187. writel(1, &regs->AssistState);
  1188. #endif
  1189. writel(DEF_STAT, &regs->TuneStatTicks);
  1190. writel(DEF_TRACE, &regs->TuneTrace);
  1191. ace_set_rxtx_parms(dev, 0);
  1192. if (board_idx == BOARD_IDX_OVERFLOW) {
  1193. printk(KERN_WARNING "%s: more than %i NICs detected, "
  1194. "ignoring module parameters!\n",
  1195. ap->name, ACE_MAX_MOD_PARMS);
  1196. } else if (board_idx >= 0) {
  1197. if (tx_coal_tick[board_idx])
  1198. writel(tx_coal_tick[board_idx],
  1199. &regs->TuneTxCoalTicks);
  1200. if (max_tx_desc[board_idx])
  1201. writel(max_tx_desc[board_idx], &regs->TuneMaxTxDesc);
  1202. if (rx_coal_tick[board_idx])
  1203. writel(rx_coal_tick[board_idx],
  1204. &regs->TuneRxCoalTicks);
  1205. if (max_rx_desc[board_idx])
  1206. writel(max_rx_desc[board_idx], &regs->TuneMaxRxDesc);
  1207. if (trace[board_idx])
  1208. writel(trace[board_idx], &regs->TuneTrace);
  1209. if ((tx_ratio[board_idx] > 0) && (tx_ratio[board_idx] < 64))
  1210. writel(tx_ratio[board_idx], &regs->TxBufRat);
  1211. }
  1212. /*
  1213. * Default link parameters
  1214. */
  1215. tmp = LNK_ENABLE | LNK_FULL_DUPLEX | LNK_1000MB | LNK_100MB |
  1216. LNK_10MB | LNK_RX_FLOW_CTL_Y | LNK_NEG_FCTL | LNK_NEGOTIATE;
  1217. if(ap->version >= 2)
  1218. tmp |= LNK_TX_FLOW_CTL_Y;
  1219. /*
  1220. * Override link default parameters
  1221. */
  1222. if ((board_idx >= 0) && link_state[board_idx]) {
  1223. int option = link_state[board_idx];
  1224. tmp = LNK_ENABLE;
  1225. if (option & 0x01) {
  1226. printk(KERN_INFO "%s: Setting half duplex link\n",
  1227. ap->name);
  1228. tmp &= ~LNK_FULL_DUPLEX;
  1229. }
  1230. if (option & 0x02)
  1231. tmp &= ~LNK_NEGOTIATE;
  1232. if (option & 0x10)
  1233. tmp |= LNK_10MB;
  1234. if (option & 0x20)
  1235. tmp |= LNK_100MB;
  1236. if (option & 0x40)
  1237. tmp |= LNK_1000MB;
  1238. if ((option & 0x70) == 0) {
  1239. printk(KERN_WARNING "%s: No media speed specified, "
  1240. "forcing auto negotiation\n", ap->name);
  1241. tmp |= LNK_NEGOTIATE | LNK_1000MB |
  1242. LNK_100MB | LNK_10MB;
  1243. }
  1244. if ((option & 0x100) == 0)
  1245. tmp |= LNK_NEG_FCTL;
  1246. else
  1247. printk(KERN_INFO "%s: Disabling flow control "
  1248. "negotiation\n", ap->name);
  1249. if (option & 0x200)
  1250. tmp |= LNK_RX_FLOW_CTL_Y;
  1251. if ((option & 0x400) && (ap->version >= 2)) {
  1252. printk(KERN_INFO "%s: Enabling TX flow control\n",
  1253. ap->name);
  1254. tmp |= LNK_TX_FLOW_CTL_Y;
  1255. }
  1256. }
  1257. ap->link = tmp;
  1258. writel(tmp, &regs->TuneLink);
  1259. if (ap->version >= 2)
  1260. writel(tmp, &regs->TuneFastLink);
  1261. writel(ap->firmware_start, &regs->Pc);
  1262. writel(0, &regs->Mb0Lo);
  1263. /*
  1264. * Set tx_csm before we start receiving interrupts, otherwise
  1265. * the interrupt handler might think it is supposed to process
  1266. * tx ints before we are up and running, which may cause a null
  1267. * pointer access in the int handler.
  1268. */
  1269. ap->cur_rx = 0;
  1270. ap->tx_prd = *(ap->tx_csm) = ap->tx_ret_csm = 0;
  1271. wmb();
  1272. ace_set_txprd(regs, ap, 0);
  1273. writel(0, &regs->RxRetCsm);
  1274. /*
  1275. * Enable DMA engine now.
  1276. * If we do this sooner, Mckinley box pukes.
  1277. * I assume it's because Tigon II DMA engine wants to check
  1278. * *something* even before the CPU is started.
  1279. */
  1280. writel(1, &regs->AssistState); /* enable DMA */
  1281. /*
  1282. * Start the NIC CPU
  1283. */
  1284. writel(readl(&regs->CpuCtrl) & ~(CPU_HALT|CPU_TRACE), &regs->CpuCtrl);
  1285. readl(&regs->CpuCtrl);
  1286. /*
  1287. * Wait for the firmware to spin up - max 3 seconds.
  1288. */
  1289. myjif = jiffies + 3 * HZ;
  1290. while (time_before(jiffies, myjif) && !ap->fw_running)
  1291. cpu_relax();
  1292. if (!ap->fw_running) {
  1293. printk(KERN_ERR "%s: Firmware NOT running!\n", ap->name);
  1294. ace_dump_trace(ap);
  1295. writel(readl(&regs->CpuCtrl) | CPU_HALT, &regs->CpuCtrl);
  1296. readl(&regs->CpuCtrl);
  1297. /* aman@sgi.com - account for badly behaving firmware/NIC:
  1298. * - have observed that the NIC may continue to generate
  1299. * interrupts for some reason; attempt to stop it - halt
  1300. * second CPU for Tigon II cards, and also clear Mb0
  1301. * - if we're a module, we'll fail to load if this was
  1302. * the only GbE card in the system => if the kernel does
  1303. * see an interrupt from the NIC, code to handle it is
  1304. * gone and OOps! - so free_irq also
  1305. */
  1306. if (ap->version >= 2)
  1307. writel(readl(&regs->CpuBCtrl) | CPU_HALT,
  1308. &regs->CpuBCtrl);
  1309. writel(0, &regs->Mb0Lo);
  1310. readl(&regs->Mb0Lo);
  1311. ecode = -EBUSY;
  1312. goto init_error;
  1313. }
  1314. /*
  1315. * We load the ring here as there seem to be no way to tell the
  1316. * firmware to wipe the ring without re-initializing it.
  1317. */
  1318. if (!test_and_set_bit(0, &ap->std_refill_busy))
  1319. ace_load_std_rx_ring(ap, RX_RING_SIZE);
  1320. else
  1321. printk(KERN_ERR "%s: Someone is busy refilling the RX ring\n",
  1322. ap->name);
  1323. if (ap->version >= 2) {
  1324. if (!test_and_set_bit(0, &ap->mini_refill_busy))
  1325. ace_load_mini_rx_ring(ap, RX_MINI_SIZE);
  1326. else
  1327. printk(KERN_ERR "%s: Someone is busy refilling "
  1328. "the RX mini ring\n", ap->name);
  1329. }
  1330. return 0;
  1331. init_error:
  1332. ace_init_cleanup(dev);
  1333. return ecode;
  1334. }
  1335. static void ace_set_rxtx_parms(struct net_device *dev, int jumbo)
  1336. {
  1337. struct ace_private *ap = netdev_priv(dev);
  1338. struct ace_regs __iomem *regs = ap->regs;
  1339. int board_idx = ap->board_idx;
  1340. if (board_idx >= 0) {
  1341. if (!jumbo) {
  1342. if (!tx_coal_tick[board_idx])
  1343. writel(DEF_TX_COAL, &regs->TuneTxCoalTicks);
  1344. if (!max_tx_desc[board_idx])
  1345. writel(DEF_TX_MAX_DESC, &regs->TuneMaxTxDesc);
  1346. if (!rx_coal_tick[board_idx])
  1347. writel(DEF_RX_COAL, &regs->TuneRxCoalTicks);
  1348. if (!max_rx_desc[board_idx])
  1349. writel(DEF_RX_MAX_DESC, &regs->TuneMaxRxDesc);
  1350. if (!tx_ratio[board_idx])
  1351. writel(DEF_TX_RATIO, &regs->TxBufRat);
  1352. } else {
  1353. if (!tx_coal_tick[board_idx])
  1354. writel(DEF_JUMBO_TX_COAL,
  1355. &regs->TuneTxCoalTicks);
  1356. if (!max_tx_desc[board_idx])
  1357. writel(DEF_JUMBO_TX_MAX_DESC,
  1358. &regs->TuneMaxTxDesc);
  1359. if (!rx_coal_tick[board_idx])
  1360. writel(DEF_JUMBO_RX_COAL,
  1361. &regs->TuneRxCoalTicks);
  1362. if (!max_rx_desc[board_idx])
  1363. writel(DEF_JUMBO_RX_MAX_DESC,
  1364. &regs->TuneMaxRxDesc);
  1365. if (!tx_ratio[board_idx])
  1366. writel(DEF_JUMBO_TX_RATIO, &regs->TxBufRat);
  1367. }
  1368. }
  1369. }
  1370. static void ace_watchdog(struct net_device *data)
  1371. {
  1372. struct net_device *dev = data;
  1373. struct ace_private *ap = netdev_priv(dev);
  1374. struct ace_regs __iomem *regs = ap->regs;
  1375. /*
  1376. * We haven't received a stats update event for more than 2.5
  1377. * seconds and there is data in the transmit queue, thus we
  1378. * asume the card is stuck.
  1379. */
  1380. if (*ap->tx_csm != ap->tx_ret_csm) {
  1381. printk(KERN_WARNING "%s: Transmitter is stuck, %08x\n",
  1382. dev->name, (unsigned int)readl(&regs->HostCtrl));
  1383. /* This can happen due to ieee flow control. */
  1384. } else {
  1385. printk(KERN_DEBUG "%s: BUG... transmitter died. Kicking it.\n",
  1386. dev->name);
  1387. #if 0
  1388. netif_wake_queue(dev);
  1389. #endif
  1390. }
  1391. }
  1392. static void ace_tasklet(unsigned long dev)
  1393. {
  1394. struct ace_private *ap = netdev_priv((struct net_device *)dev);
  1395. int cur_size;
  1396. cur_size = atomic_read(&ap->cur_rx_bufs);
  1397. if ((cur_size < RX_LOW_STD_THRES) &&
  1398. !test_and_set_bit(0, &ap->std_refill_busy)) {
  1399. #ifdef DEBUG
  1400. printk("refilling buffers (current %i)\n", cur_size);
  1401. #endif
  1402. ace_load_std_rx_ring(ap, RX_RING_SIZE - cur_size);
  1403. }
  1404. if (ap->version >= 2) {
  1405. cur_size = atomic_read(&ap->cur_mini_bufs);
  1406. if ((cur_size < RX_LOW_MINI_THRES) &&
  1407. !test_and_set_bit(0, &ap->mini_refill_busy)) {
  1408. #ifdef DEBUG
  1409. printk("refilling mini buffers (current %i)\n",
  1410. cur_size);
  1411. #endif
  1412. ace_load_mini_rx_ring(ap, RX_MINI_SIZE - cur_size);
  1413. }
  1414. }
  1415. cur_size = atomic_read(&ap->cur_jumbo_bufs);
  1416. if (ap->jumbo && (cur_size < RX_LOW_JUMBO_THRES) &&
  1417. !test_and_set_bit(0, &ap->jumbo_refill_busy)) {
  1418. #ifdef DEBUG
  1419. printk("refilling jumbo buffers (current %i)\n", cur_size);
  1420. #endif
  1421. ace_load_jumbo_rx_ring(ap, RX_JUMBO_SIZE - cur_size);
  1422. }
  1423. ap->tasklet_pending = 0;
  1424. }
  1425. /*
  1426. * Copy the contents of the NIC's trace buffer to kernel memory.
  1427. */
  1428. static void ace_dump_trace(struct ace_private *ap)
  1429. {
  1430. #if 0
  1431. if (!ap->trace_buf)
  1432. if (!(ap->trace_buf = kmalloc(ACE_TRACE_SIZE, GFP_KERNEL)))
  1433. return;
  1434. #endif
  1435. }
  1436. /*
  1437. * Load the standard rx ring.
  1438. *
  1439. * Loading rings is safe without holding the spin lock since this is
  1440. * done only before the device is enabled, thus no interrupts are
  1441. * generated and by the interrupt handler/tasklet handler.
  1442. */
  1443. static void ace_load_std_rx_ring(struct ace_private *ap, int nr_bufs)
  1444. {
  1445. struct ace_regs __iomem *regs = ap->regs;
  1446. short i, idx;
  1447. prefetchw(&ap->cur_rx_bufs);
  1448. idx = ap->rx_std_skbprd;
  1449. for (i = 0; i < nr_bufs; i++) {
  1450. struct sk_buff *skb;
  1451. struct rx_desc *rd;
  1452. dma_addr_t mapping;
  1453. skb = alloc_skb(ACE_STD_BUFSIZE + NET_IP_ALIGN, GFP_ATOMIC);
  1454. if (!skb)
  1455. break;
  1456. skb_reserve(skb, NET_IP_ALIGN);
  1457. mapping = pci_map_page(ap->pdev, virt_to_page(skb->data),
  1458. offset_in_page(skb->data),
  1459. ACE_STD_BUFSIZE,
  1460. PCI_DMA_FROMDEVICE);
  1461. ap->skb->rx_std_skbuff[idx].skb = skb;
  1462. dma_unmap_addr_set(&ap->skb->rx_std_skbuff[idx],
  1463. mapping, mapping);
  1464. rd = &ap->rx_std_ring[idx];
  1465. set_aceaddr(&rd->addr, mapping);
  1466. rd->size = ACE_STD_BUFSIZE;
  1467. rd->idx = idx;
  1468. idx = (idx + 1) % RX_STD_RING_ENTRIES;
  1469. }
  1470. if (!i)
  1471. goto error_out;
  1472. atomic_add(i, &ap->cur_rx_bufs);
  1473. ap->rx_std_skbprd = idx;
  1474. if (ACE_IS_TIGON_I(ap)) {
  1475. struct cmd cmd;
  1476. cmd.evt = C_SET_RX_PRD_IDX;
  1477. cmd.code = 0;
  1478. cmd.idx = ap->rx_std_skbprd;
  1479. ace_issue_cmd(regs, &cmd);
  1480. } else {
  1481. writel(idx, &regs->RxStdPrd);
  1482. wmb();
  1483. }
  1484. out:
  1485. clear_bit(0, &ap->std_refill_busy);
  1486. return;
  1487. error_out:
  1488. printk(KERN_INFO "Out of memory when allocating "
  1489. "standard receive buffers\n");
  1490. goto out;
  1491. }
  1492. static void ace_load_mini_rx_ring(struct ace_private *ap, int nr_bufs)
  1493. {
  1494. struct ace_regs __iomem *regs = ap->regs;
  1495. short i, idx;
  1496. prefetchw(&ap->cur_mini_bufs);
  1497. idx = ap->rx_mini_skbprd;
  1498. for (i = 0; i < nr_bufs; i++) {
  1499. struct sk_buff *skb;
  1500. struct rx_desc *rd;
  1501. dma_addr_t mapping;
  1502. skb = alloc_skb(ACE_MINI_BUFSIZE + NET_IP_ALIGN, GFP_ATOMIC);
  1503. if (!skb)
  1504. break;
  1505. skb_reserve(skb, NET_IP_ALIGN);
  1506. mapping = pci_map_page(ap->pdev, virt_to_page(skb->data),
  1507. offset_in_page(skb->data),
  1508. ACE_MINI_BUFSIZE,
  1509. PCI_DMA_FROMDEVICE);
  1510. ap->skb->rx_mini_skbuff[idx].skb = skb;
  1511. dma_unmap_addr_set(&ap->skb->rx_mini_skbuff[idx],
  1512. mapping, mapping);
  1513. rd = &ap->rx_mini_ring[idx];
  1514. set_aceaddr(&rd->addr, mapping);
  1515. rd->size = ACE_MINI_BUFSIZE;
  1516. rd->idx = idx;
  1517. idx = (idx + 1) % RX_MINI_RING_ENTRIES;
  1518. }
  1519. if (!i)
  1520. goto error_out;
  1521. atomic_add(i, &ap->cur_mini_bufs);
  1522. ap->rx_mini_skbprd = idx;
  1523. writel(idx, &regs->RxMiniPrd);
  1524. wmb();
  1525. out:
  1526. clear_bit(0, &ap->mini_refill_busy);
  1527. return;
  1528. error_out:
  1529. printk(KERN_INFO "Out of memory when allocating "
  1530. "mini receive buffers\n");
  1531. goto out;
  1532. }
  1533. /*
  1534. * Load the jumbo rx ring, this may happen at any time if the MTU
  1535. * is changed to a value > 1500.
  1536. */
  1537. static void ace_load_jumbo_rx_ring(struct ace_private *ap, int nr_bufs)
  1538. {
  1539. struct ace_regs __iomem *regs = ap->regs;
  1540. short i, idx;
  1541. idx = ap->rx_jumbo_skbprd;
  1542. for (i = 0; i < nr_bufs; i++) {
  1543. struct sk_buff *skb;
  1544. struct rx_desc *rd;
  1545. dma_addr_t mapping;
  1546. skb = alloc_skb(ACE_JUMBO_BUFSIZE + NET_IP_ALIGN, GFP_ATOMIC);
  1547. if (!skb)
  1548. break;
  1549. skb_reserve(skb, NET_IP_ALIGN);
  1550. mapping = pci_map_page(ap->pdev, virt_to_page(skb->data),
  1551. offset_in_page(skb->data),
  1552. ACE_JUMBO_BUFSIZE,
  1553. PCI_DMA_FROMDEVICE);
  1554. ap->skb->rx_jumbo_skbuff[idx].skb = skb;
  1555. dma_unmap_addr_set(&ap->skb->rx_jumbo_skbuff[idx],
  1556. mapping, mapping);
  1557. rd = &ap->rx_jumbo_ring[idx];
  1558. set_aceaddr(&rd->addr, mapping);
  1559. rd->size = ACE_JUMBO_BUFSIZE;
  1560. rd->idx = idx;
  1561. idx = (idx + 1) % RX_JUMBO_RING_ENTRIES;
  1562. }
  1563. if (!i)
  1564. goto error_out;
  1565. atomic_add(i, &ap->cur_jumbo_bufs);
  1566. ap->rx_jumbo_skbprd = idx;
  1567. if (ACE_IS_TIGON_I(ap)) {
  1568. struct cmd cmd;
  1569. cmd.evt = C_SET_RX_JUMBO_PRD_IDX;
  1570. cmd.code = 0;
  1571. cmd.idx = ap->rx_jumbo_skbprd;
  1572. ace_issue_cmd(regs, &cmd);
  1573. } else {
  1574. writel(idx, &regs->RxJumboPrd);
  1575. wmb();
  1576. }
  1577. out:
  1578. clear_bit(0, &ap->jumbo_refill_busy);
  1579. return;
  1580. error_out:
  1581. if (net_ratelimit())
  1582. printk(KERN_INFO "Out of memory when allocating "
  1583. "jumbo receive buffers\n");
  1584. goto out;
  1585. }
  1586. /*
  1587. * All events are considered to be slow (RX/TX ints do not generate
  1588. * events) and are handled here, outside the main interrupt handler,
  1589. * to reduce the size of the handler.
  1590. */
  1591. static u32 ace_handle_event(struct net_device *dev, u32 evtcsm, u32 evtprd)
  1592. {
  1593. struct ace_private *ap;
  1594. ap = netdev_priv(dev);
  1595. while (evtcsm != evtprd) {
  1596. switch (ap->evt_ring[evtcsm].evt) {
  1597. case E_FW_RUNNING:
  1598. printk(KERN_INFO "%s: Firmware up and running\n",
  1599. ap->name);
  1600. ap->fw_running = 1;
  1601. wmb();
  1602. break;
  1603. case E_STATS_UPDATED:
  1604. break;
  1605. case E_LNK_STATE:
  1606. {
  1607. u16 code = ap->evt_ring[evtcsm].code;
  1608. switch (code) {
  1609. case E_C_LINK_UP:
  1610. {
  1611. u32 state = readl(&ap->regs->GigLnkState);
  1612. printk(KERN_WARNING "%s: Optical link UP "
  1613. "(%s Duplex, Flow Control: %s%s)\n",
  1614. ap->name,
  1615. state & LNK_FULL_DUPLEX ? "Full":"Half",
  1616. state & LNK_TX_FLOW_CTL_Y ? "TX " : "",
  1617. state & LNK_RX_FLOW_CTL_Y ? "RX" : "");
  1618. break;
  1619. }
  1620. case E_C_LINK_DOWN:
  1621. printk(KERN_WARNING "%s: Optical link DOWN\n",
  1622. ap->name);
  1623. break;
  1624. case E_C_LINK_10_100:
  1625. printk(KERN_WARNING "%s: 10/100BaseT link "
  1626. "UP\n", ap->name);
  1627. break;
  1628. default:
  1629. printk(KERN_ERR "%s: Unknown optical link "
  1630. "state %02x\n", ap->name, code);
  1631. }
  1632. break;
  1633. }
  1634. case E_ERROR:
  1635. switch(ap->evt_ring[evtcsm].code) {
  1636. case E_C_ERR_INVAL_CMD:
  1637. printk(KERN_ERR "%s: invalid command error\n",
  1638. ap->name);
  1639. break;
  1640. case E_C_ERR_UNIMP_CMD:
  1641. printk(KERN_ERR "%s: unimplemented command "
  1642. "error\n", ap->name);
  1643. break;
  1644. case E_C_ERR_BAD_CFG:
  1645. printk(KERN_ERR "%s: bad config error\n",
  1646. ap->name);
  1647. break;
  1648. default:
  1649. printk(KERN_ERR "%s: unknown error %02x\n",
  1650. ap->name, ap->evt_ring[evtcsm].code);
  1651. }
  1652. break;
  1653. case E_RESET_JUMBO_RNG:
  1654. {
  1655. int i;
  1656. for (i = 0; i < RX_JUMBO_RING_ENTRIES; i++) {
  1657. if (ap->skb->rx_jumbo_skbuff[i].skb) {
  1658. ap->rx_jumbo_ring[i].size = 0;
  1659. set_aceaddr(&ap->rx_jumbo_ring[i].addr, 0);
  1660. dev_kfree_skb(ap->skb->rx_jumbo_skbuff[i].skb);
  1661. ap->skb->rx_jumbo_skbuff[i].skb = NULL;
  1662. }
  1663. }
  1664. if (ACE_IS_TIGON_I(ap)) {
  1665. struct cmd cmd;
  1666. cmd.evt = C_SET_RX_JUMBO_PRD_IDX;
  1667. cmd.code = 0;
  1668. cmd.idx = 0;
  1669. ace_issue_cmd(ap->regs, &cmd);
  1670. } else {
  1671. writel(0, &((ap->regs)->RxJumboPrd));
  1672. wmb();
  1673. }
  1674. ap->jumbo = 0;
  1675. ap->rx_jumbo_skbprd = 0;
  1676. printk(KERN_INFO "%s: Jumbo ring flushed\n",
  1677. ap->name);
  1678. clear_bit(0, &ap->jumbo_refill_busy);
  1679. break;
  1680. }
  1681. default:
  1682. printk(KERN_ERR "%s: Unhandled event 0x%02x\n",
  1683. ap->name, ap->evt_ring[evtcsm].evt);
  1684. }
  1685. evtcsm = (evtcsm + 1) % EVT_RING_ENTRIES;
  1686. }
  1687. return evtcsm;
  1688. }
  1689. static void ace_rx_int(struct net_device *dev, u32 rxretprd, u32 rxretcsm)
  1690. {
  1691. struct ace_private *ap = netdev_priv(dev);
  1692. u32 idx;
  1693. int mini_count = 0, std_count = 0;
  1694. idx = rxretcsm;
  1695. prefetchw(&ap->cur_rx_bufs);
  1696. prefetchw(&ap->cur_mini_bufs);
  1697. while (idx != rxretprd) {
  1698. struct ring_info *rip;
  1699. struct sk_buff *skb;
  1700. struct rx_desc *rxdesc, *retdesc;
  1701. u32 skbidx;
  1702. int bd_flags, desc_type, mapsize;
  1703. u16 csum;
  1704. /* make sure the rx descriptor isn't read before rxretprd */
  1705. if (idx == rxretcsm)
  1706. rmb();
  1707. retdesc = &ap->rx_return_ring[idx];
  1708. skbidx = retdesc->idx;
  1709. bd_flags = retdesc->flags;
  1710. desc_type = bd_flags & (BD_FLG_JUMBO | BD_FLG_MINI);
  1711. switch(desc_type) {
  1712. /*
  1713. * Normal frames do not have any flags set
  1714. *
  1715. * Mini and normal frames arrive frequently,
  1716. * so use a local counter to avoid doing
  1717. * atomic operations for each packet arriving.
  1718. */
  1719. case 0:
  1720. rip = &ap->skb->rx_std_skbuff[skbidx];
  1721. mapsize = ACE_STD_BUFSIZE;
  1722. rxdesc = &ap->rx_std_ring[skbidx];
  1723. std_count++;
  1724. break;
  1725. case BD_FLG_JUMBO:
  1726. rip = &ap->skb->rx_jumbo_skbuff[skbidx];
  1727. mapsize = ACE_JUMBO_BUFSIZE;
  1728. rxdesc = &ap->rx_jumbo_ring[skbidx];
  1729. atomic_dec(&ap->cur_jumbo_bufs);
  1730. break;
  1731. case BD_FLG_MINI:
  1732. rip = &ap->skb->rx_mini_skbuff[skbidx];
  1733. mapsize = ACE_MINI_BUFSIZE;
  1734. rxdesc = &ap->rx_mini_ring[skbidx];
  1735. mini_count++;
  1736. break;
  1737. default:
  1738. printk(KERN_INFO "%s: unknown frame type (0x%02x) "
  1739. "returned by NIC\n", dev->name,
  1740. retdesc->flags);
  1741. goto error;
  1742. }
  1743. skb = rip->skb;
  1744. rip->skb = NULL;
  1745. pci_unmap_page(ap->pdev,
  1746. dma_unmap_addr(rip, mapping),
  1747. mapsize,
  1748. PCI_DMA_FROMDEVICE);
  1749. skb_put(skb, retdesc->size);
  1750. /*
  1751. * Fly baby, fly!
  1752. */
  1753. csum = retdesc->tcp_udp_csum;
  1754. skb->protocol = eth_type_trans(skb, dev);
  1755. /*
  1756. * Instead of forcing the poor tigon mips cpu to calculate
  1757. * pseudo hdr checksum, we do this ourselves.
  1758. */
  1759. if (bd_flags & BD_FLG_TCP_UDP_SUM) {
  1760. skb->csum = htons(csum);
  1761. skb->ip_summed = CHECKSUM_COMPLETE;
  1762. } else {
  1763. skb->ip_summed = CHECKSUM_NONE;
  1764. }
  1765. /* send it up */
  1766. #if ACENIC_DO_VLAN
  1767. if (ap->vlgrp && (bd_flags & BD_FLG_VLAN_TAG)) {
  1768. vlan_hwaccel_rx(skb, ap->vlgrp, retdesc->vlan);
  1769. } else
  1770. #endif
  1771. netif_rx(skb);
  1772. dev->stats.rx_packets++;
  1773. dev->stats.rx_bytes += retdesc->size;
  1774. idx = (idx + 1) % RX_RETURN_RING_ENTRIES;
  1775. }
  1776. atomic_sub(std_count, &ap->cur_rx_bufs);
  1777. if (!ACE_IS_TIGON_I(ap))
  1778. atomic_sub(mini_count, &ap->cur_mini_bufs);
  1779. out:
  1780. /*
  1781. * According to the documentation RxRetCsm is obsolete with
  1782. * the 12.3.x Firmware - my Tigon I NICs seem to disagree!
  1783. */
  1784. if (ACE_IS_TIGON_I(ap)) {
  1785. writel(idx, &ap->regs->RxRetCsm);
  1786. }
  1787. ap->cur_rx = idx;
  1788. return;
  1789. error:
  1790. idx = rxretprd;
  1791. goto out;
  1792. }
  1793. static inline void ace_tx_int(struct net_device *dev,
  1794. u32 txcsm, u32 idx)
  1795. {
  1796. struct ace_private *ap = netdev_priv(dev);
  1797. do {
  1798. struct sk_buff *skb;
  1799. struct tx_ring_info *info;
  1800. info = ap->skb->tx_skbuff + idx;
  1801. skb = info->skb;
  1802. if (dma_unmap_len(info, maplen)) {
  1803. pci_unmap_page(ap->pdev, dma_unmap_addr(info, mapping),
  1804. dma_unmap_len(info, maplen),
  1805. PCI_DMA_TODEVICE);
  1806. dma_unmap_len_set(info, maplen, 0);
  1807. }
  1808. if (skb) {
  1809. dev->stats.tx_packets++;
  1810. dev->stats.tx_bytes += skb->len;
  1811. dev_kfree_skb_irq(skb);
  1812. info->skb = NULL;
  1813. }
  1814. idx = (idx + 1) % ACE_TX_RING_ENTRIES(ap);
  1815. } while (idx != txcsm);
  1816. if (netif_queue_stopped(dev))
  1817. netif_wake_queue(dev);
  1818. wmb();
  1819. ap->tx_ret_csm = txcsm;
  1820. /* So... tx_ret_csm is advanced _after_ check for device wakeup.
  1821. *
  1822. * We could try to make it before. In this case we would get
  1823. * the following race condition: hard_start_xmit on other cpu
  1824. * enters after we advanced tx_ret_csm and fills space,
  1825. * which we have just freed, so that we make illegal device wakeup.
  1826. * There is no good way to workaround this (at entry
  1827. * to ace_start_xmit detects this condition and prevents
  1828. * ring corruption, but it is not a good workaround.)
  1829. *
  1830. * When tx_ret_csm is advanced after, we wake up device _only_
  1831. * if we really have some space in ring (though the core doing
  1832. * hard_start_xmit can see full ring for some period and has to
  1833. * synchronize.) Superb.
  1834. * BUT! We get another subtle race condition. hard_start_xmit
  1835. * may think that ring is full between wakeup and advancing
  1836. * tx_ret_csm and will stop device instantly! It is not so bad.
  1837. * We are guaranteed that there is something in ring, so that
  1838. * the next irq will resume transmission. To speedup this we could
  1839. * mark descriptor, which closes ring with BD_FLG_COAL_NOW
  1840. * (see ace_start_xmit).
  1841. *
  1842. * Well, this dilemma exists in all lock-free devices.
  1843. * We, following scheme used in drivers by Donald Becker,
  1844. * select the least dangerous.
  1845. * --ANK
  1846. */
  1847. }
  1848. static irqreturn_t ace_interrupt(int irq, void *dev_id)
  1849. {
  1850. struct net_device *dev = (struct net_device *)dev_id;
  1851. struct ace_private *ap = netdev_priv(dev);
  1852. struct ace_regs __iomem *regs = ap->regs;
  1853. u32 idx;
  1854. u32 txcsm, rxretcsm, rxretprd;
  1855. u32 evtcsm, evtprd;
  1856. /*
  1857. * In case of PCI shared interrupts or spurious interrupts,
  1858. * we want to make sure it is actually our interrupt before
  1859. * spending any time in here.
  1860. */
  1861. if (!(readl(&regs->HostCtrl) & IN_INT))
  1862. return IRQ_NONE;
  1863. /*
  1864. * ACK intr now. Otherwise we will lose updates to rx_ret_prd,
  1865. * which happened _after_ rxretprd = *ap->rx_ret_prd; but before
  1866. * writel(0, &regs->Mb0Lo).
  1867. *
  1868. * "IRQ avoidance" recommended in docs applies to IRQs served
  1869. * threads and it is wrong even for that case.
  1870. */
  1871. writel(0, &regs->Mb0Lo);
  1872. readl(&regs->Mb0Lo);
  1873. /*
  1874. * There is no conflict between transmit handling in
  1875. * start_xmit and receive processing, thus there is no reason
  1876. * to take a spin lock for RX handling. Wait until we start
  1877. * working on the other stuff - hey we don't need a spin lock
  1878. * anymore.
  1879. */
  1880. rxretprd = *ap->rx_ret_prd;
  1881. rxretcsm = ap->cur_rx;
  1882. if (rxretprd != rxretcsm)
  1883. ace_rx_int(dev, rxretprd, rxretcsm);
  1884. txcsm = *ap->tx_csm;
  1885. idx = ap->tx_ret_csm;
  1886. if (txcsm != idx) {
  1887. /*
  1888. * If each skb takes only one descriptor this check degenerates
  1889. * to identity, because new space has just been opened.
  1890. * But if skbs are fragmented we must check that this index
  1891. * update releases enough of space, otherwise we just
  1892. * wait for device to make more work.
  1893. */
  1894. if (!tx_ring_full(ap, txcsm, ap->tx_prd))
  1895. ace_tx_int(dev, txcsm, idx);
  1896. }
  1897. evtcsm = readl(&regs->EvtCsm);
  1898. evtprd = *ap->evt_prd;
  1899. if (evtcsm != evtprd) {
  1900. evtcsm = ace_handle_event(dev, evtcsm, evtprd);
  1901. writel(evtcsm, &regs->EvtCsm);
  1902. }
  1903. /*
  1904. * This has to go last in the interrupt handler and run with
  1905. * the spin lock released ... what lock?
  1906. */
  1907. if (netif_running(dev)) {
  1908. int cur_size;
  1909. int run_tasklet = 0;
  1910. cur_size = atomic_read(&ap->cur_rx_bufs);
  1911. if (cur_size < RX_LOW_STD_THRES) {
  1912. if ((cur_size < RX_PANIC_STD_THRES) &&
  1913. !test_and_set_bit(0, &ap->std_refill_busy)) {
  1914. #ifdef DEBUG
  1915. printk("low on std buffers %i\n", cur_size);
  1916. #endif
  1917. ace_load_std_rx_ring(ap,
  1918. RX_RING_SIZE - cur_size);
  1919. } else
  1920. run_tasklet = 1;
  1921. }
  1922. if (!ACE_IS_TIGON_I(ap)) {
  1923. cur_size = atomic_read(&ap->cur_mini_bufs);
  1924. if (cur_size < RX_LOW_MINI_THRES) {
  1925. if ((cur_size < RX_PANIC_MINI_THRES) &&
  1926. !test_and_set_bit(0,
  1927. &ap->mini_refill_busy)) {
  1928. #ifdef DEBUG
  1929. printk("low on mini buffers %i\n",
  1930. cur_size);
  1931. #endif
  1932. ace_load_mini_rx_ring(ap, RX_MINI_SIZE - cur_size);
  1933. } else
  1934. run_tasklet = 1;
  1935. }
  1936. }
  1937. if (ap->jumbo) {
  1938. cur_size = atomic_read(&ap->cur_jumbo_bufs);
  1939. if (cur_size < RX_LOW_JUMBO_THRES) {
  1940. if ((cur_size < RX_PANIC_JUMBO_THRES) &&
  1941. !test_and_set_bit(0,
  1942. &ap->jumbo_refill_busy)){
  1943. #ifdef DEBUG
  1944. printk("low on jumbo buffers %i\n",
  1945. cur_size);
  1946. #endif
  1947. ace_load_jumbo_rx_ring(ap, RX_JUMBO_SIZE - cur_size);
  1948. } else
  1949. run_tasklet = 1;
  1950. }
  1951. }
  1952. if (run_tasklet && !ap->tasklet_pending) {
  1953. ap->tasklet_pending = 1;
  1954. tasklet_schedule(&ap->ace_tasklet);
  1955. }
  1956. }
  1957. return IRQ_HANDLED;
  1958. }
  1959. #if ACENIC_DO_VLAN
  1960. static void ace_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
  1961. {
  1962. struct ace_private *ap = netdev_priv(dev);
  1963. unsigned long flags;
  1964. local_irq_save(flags);
  1965. ace_mask_irq(dev);
  1966. ap->vlgrp = grp;
  1967. ace_unmask_irq(dev);
  1968. local_irq_restore(flags);
  1969. }
  1970. #endif /* ACENIC_DO_VLAN */
  1971. static int ace_open(struct net_device *dev)
  1972. {
  1973. struct ace_private *ap = netdev_priv(dev);
  1974. struct ace_regs __iomem *regs = ap->regs;
  1975. struct cmd cmd;
  1976. if (!(ap->fw_running)) {
  1977. printk(KERN_WARNING "%s: Firmware not running!\n", dev->name);
  1978. return -EBUSY;
  1979. }
  1980. writel(dev->mtu + ETH_HLEN + 4, &regs->IfMtu);
  1981. cmd.evt = C_CLEAR_STATS;
  1982. cmd.code = 0;
  1983. cmd.idx = 0;
  1984. ace_issue_cmd(regs, &cmd);
  1985. cmd.evt = C_HOST_STATE;
  1986. cmd.code = C_C_STACK_UP;
  1987. cmd.idx = 0;
  1988. ace_issue_cmd(regs, &cmd);
  1989. if (ap->jumbo &&
  1990. !test_and_set_bit(0, &ap->jumbo_refill_busy))
  1991. ace_load_jumbo_rx_ring(ap, RX_JUMBO_SIZE);
  1992. if (dev->flags & IFF_PROMISC) {
  1993. cmd.evt = C_SET_PROMISC_MODE;
  1994. cmd.code = C_C_PROMISC_ENABLE;
  1995. cmd.idx = 0;
  1996. ace_issue_cmd(regs, &cmd);
  1997. ap->promisc = 1;
  1998. }else
  1999. ap->promisc = 0;
  2000. ap->mcast_all = 0;
  2001. #if 0
  2002. cmd.evt = C_LNK_NEGOTIATION;
  2003. cmd.code = 0;
  2004. cmd.idx = 0;
  2005. ace_issue_cmd(regs, &cmd);
  2006. #endif
  2007. netif_start_queue(dev);
  2008. /*
  2009. * Setup the bottom half rx ring refill handler
  2010. */
  2011. tasklet_init(&ap->ace_tasklet, ace_tasklet, (unsigned long)dev);
  2012. return 0;
  2013. }
  2014. static int ace_close(struct net_device *dev)
  2015. {
  2016. struct ace_private *ap = netdev_priv(dev);
  2017. struct ace_regs __iomem *regs = ap->regs;
  2018. struct cmd cmd;
  2019. unsigned long flags;
  2020. short i;
  2021. /*
  2022. * Without (or before) releasing irq and stopping hardware, this
  2023. * is an absolute non-sense, by the way. It will be reset instantly
  2024. * by the first irq.
  2025. */
  2026. netif_stop_queue(dev);
  2027. if (ap->promisc) {
  2028. cmd.evt = C_SET_PROMISC_MODE;
  2029. cmd.code = C_C_PROMISC_DISABLE;
  2030. cmd.idx = 0;
  2031. ace_issue_cmd(regs, &cmd);
  2032. ap->promisc = 0;
  2033. }
  2034. cmd.evt = C_HOST_STATE;
  2035. cmd.code = C_C_STACK_DOWN;
  2036. cmd.idx = 0;
  2037. ace_issue_cmd(regs, &cmd);
  2038. tasklet_kill(&ap->ace_tasklet);
  2039. /*
  2040. * Make sure one CPU is not processing packets while
  2041. * buffers are being released by another.
  2042. */
  2043. local_irq_save(flags);
  2044. ace_mask_irq(dev);
  2045. for (i = 0; i < ACE_TX_RING_ENTRIES(ap); i++) {
  2046. struct sk_buff *skb;
  2047. struct tx_ring_info *info;
  2048. info = ap->skb->tx_skbuff + i;
  2049. skb = info->skb;
  2050. if (dma_unmap_len(info, maplen)) {
  2051. if (ACE_IS_TIGON_I(ap)) {
  2052. /* NB: TIGON_1 is special, tx_ring is in io space */
  2053. struct tx_desc __iomem *tx;
  2054. tx = (__force struct tx_desc __iomem *) &ap->tx_ring[i];
  2055. writel(0, &tx->addr.addrhi);
  2056. writel(0, &tx->addr.addrlo);
  2057. writel(0, &tx->flagsize);
  2058. } else
  2059. memset(ap->tx_ring + i, 0,
  2060. sizeof(struct tx_desc));
  2061. pci_unmap_page(ap->pdev, dma_unmap_addr(info, mapping),
  2062. dma_unmap_len(info, maplen),
  2063. PCI_DMA_TODEVICE);
  2064. dma_unmap_len_set(info, maplen, 0);
  2065. }
  2066. if (skb) {
  2067. dev_kfree_skb(skb);
  2068. info->skb = NULL;
  2069. }
  2070. }
  2071. if (ap->jumbo) {
  2072. cmd.evt = C_RESET_JUMBO_RNG;
  2073. cmd.code = 0;
  2074. cmd.idx = 0;
  2075. ace_issue_cmd(regs, &cmd);
  2076. }
  2077. ace_unmask_irq(dev);
  2078. local_irq_restore(flags);
  2079. return 0;
  2080. }
  2081. static inline dma_addr_t
  2082. ace_map_tx_skb(struct ace_private *ap, struct sk_buff *skb,
  2083. struct sk_buff *tail, u32 idx)
  2084. {
  2085. dma_addr_t mapping;
  2086. struct tx_ring_info *info;
  2087. mapping = pci_map_page(ap->pdev, virt_to_page(skb->data),
  2088. offset_in_page(skb->data),
  2089. skb->len, PCI_DMA_TODEVICE);
  2090. info = ap->skb->tx_skbuff + idx;
  2091. info->skb = tail;
  2092. dma_unmap_addr_set(info, mapping, mapping);
  2093. dma_unmap_len_set(info, maplen, skb->len);
  2094. return mapping;
  2095. }
  2096. static inline void
  2097. ace_load_tx_bd(struct ace_private *ap, struct tx_desc *desc, u64 addr,
  2098. u32 flagsize, u32 vlan_tag)
  2099. {
  2100. #if !USE_TX_COAL_NOW
  2101. flagsize &= ~BD_FLG_COAL_NOW;
  2102. #endif
  2103. if (ACE_IS_TIGON_I(ap)) {
  2104. struct tx_desc __iomem *io = (__force struct tx_desc __iomem *) desc;
  2105. writel(addr >> 32, &io->addr.addrhi);
  2106. writel(addr & 0xffffffff, &io->addr.addrlo);
  2107. writel(flagsize, &io->flagsize);
  2108. #if ACENIC_DO_VLAN
  2109. writel(vlan_tag, &io->vlanres);
  2110. #endif
  2111. } else {
  2112. desc->addr.addrhi = addr >> 32;
  2113. desc->addr.addrlo = addr;
  2114. desc->flagsize = flagsize;
  2115. #if ACENIC_DO_VLAN
  2116. desc->vlanres = vlan_tag;
  2117. #endif
  2118. }
  2119. }
  2120. static netdev_tx_t ace_start_xmit(struct sk_buff *skb,
  2121. struct net_device *dev)
  2122. {
  2123. struct ace_private *ap = netdev_priv(dev);
  2124. struct ace_regs __iomem *regs = ap->regs;
  2125. struct tx_desc *desc;
  2126. u32 idx, flagsize;
  2127. unsigned long maxjiff = jiffies + 3*HZ;
  2128. restart:
  2129. idx = ap->tx_prd;
  2130. if (tx_ring_full(ap, ap->tx_ret_csm, idx))
  2131. goto overflow;
  2132. if (!skb_shinfo(skb)->nr_frags) {
  2133. dma_addr_t mapping;
  2134. u32 vlan_tag = 0;
  2135. mapping = ace_map_tx_skb(ap, skb, skb, idx);
  2136. flagsize = (skb->len << 16) | (BD_FLG_END);
  2137. if (skb->ip_summed == CHECKSUM_PARTIAL)
  2138. flagsize |= BD_FLG_TCP_UDP_SUM;
  2139. #if ACENIC_DO_VLAN
  2140. if (vlan_tx_tag_present(skb)) {
  2141. flagsize |= BD_FLG_VLAN_TAG;
  2142. vlan_tag = vlan_tx_tag_get(skb);
  2143. }
  2144. #endif
  2145. desc = ap->tx_ring + idx;
  2146. idx = (idx + 1) % ACE_TX_RING_ENTRIES(ap);
  2147. /* Look at ace_tx_int for explanations. */
  2148. if (tx_ring_full(ap, ap->tx_ret_csm, idx))
  2149. flagsize |= BD_FLG_COAL_NOW;
  2150. ace_load_tx_bd(ap, desc, mapping, flagsize, vlan_tag);
  2151. } else {
  2152. dma_addr_t mapping;
  2153. u32 vlan_tag = 0;
  2154. int i, len = 0;
  2155. mapping = ace_map_tx_skb(ap, skb, NULL, idx);
  2156. flagsize = (skb_headlen(skb) << 16);
  2157. if (skb->ip_summed == CHECKSUM_PARTIAL)
  2158. flagsize |= BD_FLG_TCP_UDP_SUM;
  2159. #if ACENIC_DO_VLAN
  2160. if (vlan_tx_tag_present(skb)) {
  2161. flagsize |= BD_FLG_VLAN_TAG;
  2162. vlan_tag = vlan_tx_tag_get(skb);
  2163. }
  2164. #endif
  2165. ace_load_tx_bd(ap, ap->tx_ring + idx, mapping, flagsize, vlan_tag);
  2166. idx = (idx + 1) % ACE_TX_RING_ENTRIES(ap);
  2167. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2168. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2169. struct tx_ring_info *info;
  2170. len += frag->size;
  2171. info = ap->skb->tx_skbuff + idx;
  2172. desc = ap->tx_ring + idx;
  2173. mapping = pci_map_page(ap->pdev, frag->page,
  2174. frag->page_offset, frag->size,
  2175. PCI_DMA_TODEVICE);
  2176. flagsize = (frag->size << 16);
  2177. if (skb->ip_summed == CHECKSUM_PARTIAL)
  2178. flagsize |= BD_FLG_TCP_UDP_SUM;
  2179. idx = (idx + 1) % ACE_TX_RING_ENTRIES(ap);
  2180. if (i == skb_shinfo(skb)->nr_frags - 1) {
  2181. flagsize |= BD_FLG_END;
  2182. if (tx_ring_full(ap, ap->tx_ret_csm, idx))
  2183. flagsize |= BD_FLG_COAL_NOW;
  2184. /*
  2185. * Only the last fragment frees
  2186. * the skb!
  2187. */
  2188. info->skb = skb;
  2189. } else {
  2190. info->skb = NULL;
  2191. }
  2192. dma_unmap_addr_set(info, mapping, mapping);
  2193. dma_unmap_len_set(info, maplen, frag->size);
  2194. ace_load_tx_bd(ap, desc, mapping, flagsize, vlan_tag);
  2195. }
  2196. }
  2197. wmb();
  2198. ap->tx_prd = idx;
  2199. ace_set_txprd(regs, ap, idx);
  2200. if (flagsize & BD_FLG_COAL_NOW) {
  2201. netif_stop_queue(dev);
  2202. /*
  2203. * A TX-descriptor producer (an IRQ) might have gotten
  2204. * inbetween, making the ring free again. Since xmit is
  2205. * serialized, this is the only situation we have to
  2206. * re-test.
  2207. */
  2208. if (!tx_ring_full(ap, ap->tx_ret_csm, idx))
  2209. netif_wake_queue(dev);
  2210. }
  2211. return NETDEV_TX_OK;
  2212. overflow:
  2213. /*
  2214. * This race condition is unavoidable with lock-free drivers.
  2215. * We wake up the queue _before_ tx_prd is advanced, so that we can
  2216. * enter hard_start_xmit too early, while tx ring still looks closed.
  2217. * This happens ~1-4 times per 100000 packets, so that we can allow
  2218. * to loop syncing to other CPU. Probably, we need an additional
  2219. * wmb() in ace_tx_intr as well.
  2220. *
  2221. * Note that this race is relieved by reserving one more entry
  2222. * in tx ring than it is necessary (see original non-SG driver).
  2223. * However, with SG we need to reserve 2*MAX_SKB_FRAGS+1, which
  2224. * is already overkill.
  2225. *
  2226. * Alternative is to return with 1 not throttling queue. In this
  2227. * case loop becomes longer, no more useful effects.
  2228. */
  2229. if (time_before(jiffies, maxjiff)) {
  2230. barrier();
  2231. cpu_relax();
  2232. goto restart;
  2233. }
  2234. /* The ring is stuck full. */
  2235. printk(KERN_WARNING "%s: Transmit ring stuck full\n", dev->name);
  2236. return NETDEV_TX_BUSY;
  2237. }
  2238. static int ace_change_mtu(struct net_device *dev, int new_mtu)
  2239. {
  2240. struct ace_private *ap = netdev_priv(dev);
  2241. struct ace_regs __iomem *regs = ap->regs;
  2242. if (new_mtu > ACE_JUMBO_MTU)
  2243. return -EINVAL;
  2244. writel(new_mtu + ETH_HLEN + 4, &regs->IfMtu);
  2245. dev->mtu = new_mtu;
  2246. if (new_mtu > ACE_STD_MTU) {
  2247. if (!(ap->jumbo)) {
  2248. printk(KERN_INFO "%s: Enabling Jumbo frame "
  2249. "support\n", dev->name);
  2250. ap->jumbo = 1;
  2251. if (!test_and_set_bit(0, &ap->jumbo_refill_busy))
  2252. ace_load_jumbo_rx_ring(ap, RX_JUMBO_SIZE);
  2253. ace_set_rxtx_parms(dev, 1);
  2254. }
  2255. } else {
  2256. while (test_and_set_bit(0, &ap->jumbo_refill_busy));
  2257. ace_sync_irq(dev->irq);
  2258. ace_set_rxtx_parms(dev, 0);
  2259. if (ap->jumbo) {
  2260. struct cmd cmd;
  2261. cmd.evt = C_RESET_JUMBO_RNG;
  2262. cmd.code = 0;
  2263. cmd.idx = 0;
  2264. ace_issue_cmd(regs, &cmd);
  2265. }
  2266. }
  2267. return 0;
  2268. }
  2269. static int ace_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  2270. {
  2271. struct ace_private *ap = netdev_priv(dev);
  2272. struct ace_regs __iomem *regs = ap->regs;
  2273. u32 link;
  2274. memset(ecmd, 0, sizeof(struct ethtool_cmd));
  2275. ecmd->supported =
  2276. (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
  2277. SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
  2278. SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full |
  2279. SUPPORTED_Autoneg | SUPPORTED_FIBRE);
  2280. ecmd->port = PORT_FIBRE;
  2281. ecmd->transceiver = XCVR_INTERNAL;
  2282. link = readl(&regs->GigLnkState);
  2283. if (link & LNK_1000MB)
  2284. ecmd->speed = SPEED_1000;
  2285. else {
  2286. link = readl(&regs->FastLnkState);
  2287. if (link & LNK_100MB)
  2288. ecmd->speed = SPEED_100;
  2289. else if (link & LNK_10MB)
  2290. ecmd->speed = SPEED_10;
  2291. else
  2292. ecmd->speed = 0;
  2293. }
  2294. if (link & LNK_FULL_DUPLEX)
  2295. ecmd->duplex = DUPLEX_FULL;
  2296. else
  2297. ecmd->duplex = DUPLEX_HALF;
  2298. if (link & LNK_NEGOTIATE)
  2299. ecmd->autoneg = AUTONEG_ENABLE;
  2300. else
  2301. ecmd->autoneg = AUTONEG_DISABLE;
  2302. #if 0
  2303. /*
  2304. * Current struct ethtool_cmd is insufficient
  2305. */
  2306. ecmd->trace = readl(&regs->TuneTrace);
  2307. ecmd->txcoal = readl(&regs->TuneTxCoalTicks);
  2308. ecmd->rxcoal = readl(&regs->TuneRxCoalTicks);
  2309. #endif
  2310. ecmd->maxtxpkt = readl(&regs->TuneMaxTxDesc);
  2311. ecmd->maxrxpkt = readl(&regs->TuneMaxRxDesc);
  2312. return 0;
  2313. }
  2314. static int ace_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  2315. {
  2316. struct ace_private *ap = netdev_priv(dev);
  2317. struct ace_regs __iomem *regs = ap->regs;
  2318. u32 link, speed;
  2319. link = readl(&regs->GigLnkState);
  2320. if (link & LNK_1000MB)
  2321. speed = SPEED_1000;
  2322. else {
  2323. link = readl(&regs->FastLnkState);
  2324. if (link & LNK_100MB)
  2325. speed = SPEED_100;
  2326. else if (link & LNK_10MB)
  2327. speed = SPEED_10;
  2328. else
  2329. speed = SPEED_100;
  2330. }
  2331. link = LNK_ENABLE | LNK_1000MB | LNK_100MB | LNK_10MB |
  2332. LNK_RX_FLOW_CTL_Y | LNK_NEG_FCTL;
  2333. if (!ACE_IS_TIGON_I(ap))
  2334. link |= LNK_TX_FLOW_CTL_Y;
  2335. if (ecmd->autoneg == AUTONEG_ENABLE)
  2336. link |= LNK_NEGOTIATE;
  2337. if (ecmd->speed != speed) {
  2338. link &= ~(LNK_1000MB | LNK_100MB | LNK_10MB);
  2339. switch (speed) {
  2340. case SPEED_1000:
  2341. link |= LNK_1000MB;
  2342. break;
  2343. case SPEED_100:
  2344. link |= LNK_100MB;
  2345. break;
  2346. case SPEED_10:
  2347. link |= LNK_10MB;
  2348. break;
  2349. }
  2350. }
  2351. if (ecmd->duplex == DUPLEX_FULL)
  2352. link |= LNK_FULL_DUPLEX;
  2353. if (link != ap->link) {
  2354. struct cmd cmd;
  2355. printk(KERN_INFO "%s: Renegotiating link state\n",
  2356. dev->name);
  2357. ap->link = link;
  2358. writel(link, &regs->TuneLink);
  2359. if (!ACE_IS_TIGON_I(ap))
  2360. writel(link, &regs->TuneFastLink);
  2361. wmb();
  2362. cmd.evt = C_LNK_NEGOTIATION;
  2363. cmd.code = 0;
  2364. cmd.idx = 0;
  2365. ace_issue_cmd(regs, &cmd);
  2366. }
  2367. return 0;
  2368. }
  2369. static void ace_get_drvinfo(struct net_device *dev,
  2370. struct ethtool_drvinfo *info)
  2371. {
  2372. struct ace_private *ap = netdev_priv(dev);
  2373. strlcpy(info->driver, "acenic", sizeof(info->driver));
  2374. snprintf(info->version, sizeof(info->version), "%i.%i.%i",
  2375. ap->firmware_major, ap->firmware_minor,
  2376. ap->firmware_fix);
  2377. if (ap->pdev)
  2378. strlcpy(info->bus_info, pci_name(ap->pdev),
  2379. sizeof(info->bus_info));
  2380. }
  2381. /*
  2382. * Set the hardware MAC address.
  2383. */
  2384. static int ace_set_mac_addr(struct net_device *dev, void *p)
  2385. {
  2386. struct ace_private *ap = netdev_priv(dev);
  2387. struct ace_regs __iomem *regs = ap->regs;
  2388. struct sockaddr *addr=p;
  2389. u8 *da;
  2390. struct cmd cmd;
  2391. if(netif_running(dev))
  2392. return -EBUSY;
  2393. memcpy(dev->dev_addr, addr->sa_data,dev->addr_len);
  2394. da = (u8 *)dev->dev_addr;
  2395. writel(da[0] << 8 | da[1], &regs->MacAddrHi);
  2396. writel((da[2] << 24) | (da[3] << 16) | (da[4] << 8) | da[5],
  2397. &regs->MacAddrLo);
  2398. cmd.evt = C_SET_MAC_ADDR;
  2399. cmd.code = 0;
  2400. cmd.idx = 0;
  2401. ace_issue_cmd(regs, &cmd);
  2402. return 0;
  2403. }
  2404. static void ace_set_multicast_list(struct net_device *dev)
  2405. {
  2406. struct ace_private *ap = netdev_priv(dev);
  2407. struct ace_regs __iomem *regs = ap->regs;
  2408. struct cmd cmd;
  2409. if ((dev->flags & IFF_ALLMULTI) && !(ap->mcast_all)) {
  2410. cmd.evt = C_SET_MULTICAST_MODE;
  2411. cmd.code = C_C_MCAST_ENABLE;
  2412. cmd.idx = 0;
  2413. ace_issue_cmd(regs, &cmd);
  2414. ap->mcast_all = 1;
  2415. } else if (ap->mcast_all) {
  2416. cmd.evt = C_SET_MULTICAST_MODE;
  2417. cmd.code = C_C_MCAST_DISABLE;
  2418. cmd.idx = 0;
  2419. ace_issue_cmd(regs, &cmd);
  2420. ap->mcast_all = 0;
  2421. }
  2422. if ((dev->flags & IFF_PROMISC) && !(ap->promisc)) {
  2423. cmd.evt = C_SET_PROMISC_MODE;
  2424. cmd.code = C_C_PROMISC_ENABLE;
  2425. cmd.idx = 0;
  2426. ace_issue_cmd(regs, &cmd);
  2427. ap->promisc = 1;
  2428. }else if (!(dev->flags & IFF_PROMISC) && (ap->promisc)) {
  2429. cmd.evt = C_SET_PROMISC_MODE;
  2430. cmd.code = C_C_PROMISC_DISABLE;
  2431. cmd.idx = 0;
  2432. ace_issue_cmd(regs, &cmd);
  2433. ap->promisc = 0;
  2434. }
  2435. /*
  2436. * For the time being multicast relies on the upper layers
  2437. * filtering it properly. The Firmware does not allow one to
  2438. * set the entire multicast list at a time and keeping track of
  2439. * it here is going to be messy.
  2440. */
  2441. if (!netdev_mc_empty(dev) && !ap->mcast_all) {
  2442. cmd.evt = C_SET_MULTICAST_MODE;
  2443. cmd.code = C_C_MCAST_ENABLE;
  2444. cmd.idx = 0;
  2445. ace_issue_cmd(regs, &cmd);
  2446. }else if (!ap->mcast_all) {
  2447. cmd.evt = C_SET_MULTICAST_MODE;
  2448. cmd.code = C_C_MCAST_DISABLE;
  2449. cmd.idx = 0;
  2450. ace_issue_cmd(regs, &cmd);
  2451. }
  2452. }
  2453. static struct net_device_stats *ace_get_stats(struct net_device *dev)
  2454. {
  2455. struct ace_private *ap = netdev_priv(dev);
  2456. struct ace_mac_stats __iomem *mac_stats =
  2457. (struct ace_mac_stats __iomem *)ap->regs->Stats;
  2458. dev->stats.rx_missed_errors = readl(&mac_stats->drop_space);
  2459. dev->stats.multicast = readl(&mac_stats->kept_mc);
  2460. dev->stats.collisions = readl(&mac_stats->coll);
  2461. return &dev->stats;
  2462. }
  2463. static void __devinit ace_copy(struct ace_regs __iomem *regs, const __be32 *src,
  2464. u32 dest, int size)
  2465. {
  2466. void __iomem *tdest;
  2467. short tsize, i;
  2468. if (size <= 0)
  2469. return;
  2470. while (size > 0) {
  2471. tsize = min_t(u32, ((~dest & (ACE_WINDOW_SIZE - 1)) + 1),
  2472. min_t(u32, size, ACE_WINDOW_SIZE));
  2473. tdest = (void __iomem *) &regs->Window +
  2474. (dest & (ACE_WINDOW_SIZE - 1));
  2475. writel(dest & ~(ACE_WINDOW_SIZE - 1), &regs->WinBase);
  2476. for (i = 0; i < (tsize / 4); i++) {
  2477. /* Firmware is big-endian */
  2478. writel(be32_to_cpup(src), tdest);
  2479. src++;
  2480. tdest += 4;
  2481. dest += 4;
  2482. size -= 4;
  2483. }
  2484. }
  2485. }
  2486. static void __devinit ace_clear(struct ace_regs __iomem *regs, u32 dest, int size)
  2487. {
  2488. void __iomem *tdest;
  2489. short tsize = 0, i;
  2490. if (size <= 0)
  2491. return;
  2492. while (size > 0) {
  2493. tsize = min_t(u32, ((~dest & (ACE_WINDOW_SIZE - 1)) + 1),
  2494. min_t(u32, size, ACE_WINDOW_SIZE));
  2495. tdest = (void __iomem *) &regs->Window +
  2496. (dest & (ACE_WINDOW_SIZE - 1));
  2497. writel(dest & ~(ACE_WINDOW_SIZE - 1), &regs->WinBase);
  2498. for (i = 0; i < (tsize / 4); i++) {
  2499. writel(0, tdest + i*4);
  2500. }
  2501. dest += tsize;
  2502. size -= tsize;
  2503. }
  2504. }
  2505. /*
  2506. * Download the firmware into the SRAM on the NIC
  2507. *
  2508. * This operation requires the NIC to be halted and is performed with
  2509. * interrupts disabled and with the spinlock hold.
  2510. */
  2511. static int __devinit ace_load_firmware(struct net_device *dev)
  2512. {
  2513. const struct firmware *fw;
  2514. const char *fw_name = "acenic/tg2.bin";
  2515. struct ace_private *ap = netdev_priv(dev);
  2516. struct ace_regs __iomem *regs = ap->regs;
  2517. const __be32 *fw_data;
  2518. u32 load_addr;
  2519. int ret;
  2520. if (!(readl(&regs->CpuCtrl) & CPU_HALTED)) {
  2521. printk(KERN_ERR "%s: trying to download firmware while the "
  2522. "CPU is running!\n", ap->name);
  2523. return -EFAULT;
  2524. }
  2525. if (ACE_IS_TIGON_I(ap))
  2526. fw_name = "acenic/tg1.bin";
  2527. ret = request_firmware(&fw, fw_name, &ap->pdev->dev);
  2528. if (ret) {
  2529. printk(KERN_ERR "%s: Failed to load firmware \"%s\"\n",
  2530. ap->name, fw_name);
  2531. return ret;
  2532. }
  2533. fw_data = (void *)fw->data;
  2534. /* Firmware blob starts with version numbers, followed by
  2535. load and start address. Remainder is the blob to be loaded
  2536. contiguously from load address. We don't bother to represent
  2537. the BSS/SBSS sections any more, since we were clearing the
  2538. whole thing anyway. */
  2539. ap->firmware_major = fw->data[0];
  2540. ap->firmware_minor = fw->data[1];
  2541. ap->firmware_fix = fw->data[2];
  2542. ap->firmware_start = be32_to_cpu(fw_data[1]);
  2543. if (ap->firmware_start < 0x4000 || ap->firmware_start >= 0x80000) {
  2544. printk(KERN_ERR "%s: bogus load address %08x in \"%s\"\n",
  2545. ap->name, ap->firmware_start, fw_name);
  2546. ret = -EINVAL;
  2547. goto out;
  2548. }
  2549. load_addr = be32_to_cpu(fw_data[2]);
  2550. if (load_addr < 0x4000 || load_addr >= 0x80000) {
  2551. printk(KERN_ERR "%s: bogus load address %08x in \"%s\"\n",
  2552. ap->name, load_addr, fw_name);
  2553. ret = -EINVAL;
  2554. goto out;
  2555. }
  2556. /*
  2557. * Do not try to clear more than 512KiB or we end up seeing
  2558. * funny things on NICs with only 512KiB SRAM
  2559. */
  2560. ace_clear(regs, 0x2000, 0x80000-0x2000);
  2561. ace_copy(regs, &fw_data[3], load_addr, fw->size-12);
  2562. out:
  2563. release_firmware(fw);
  2564. return ret;
  2565. }
  2566. /*
  2567. * The eeprom on the AceNIC is an Atmel i2c EEPROM.
  2568. *
  2569. * Accessing the EEPROM is `interesting' to say the least - don't read
  2570. * this code right after dinner.
  2571. *
  2572. * This is all about black magic and bit-banging the device .... I
  2573. * wonder in what hospital they have put the guy who designed the i2c
  2574. * specs.
  2575. *
  2576. * Oh yes, this is only the beginning!
  2577. *
  2578. * Thanks to Stevarino Webinski for helping tracking down the bugs in the
  2579. * code i2c readout code by beta testing all my hacks.
  2580. */
  2581. static void __devinit eeprom_start(struct ace_regs __iomem *regs)
  2582. {
  2583. u32 local;
  2584. readl(&regs->LocalCtrl);
  2585. udelay(ACE_SHORT_DELAY);
  2586. local = readl(&regs->LocalCtrl);
  2587. local |= EEPROM_DATA_OUT | EEPROM_WRITE_ENABLE;
  2588. writel(local, &regs->LocalCtrl);
  2589. readl(&regs->LocalCtrl);
  2590. mb();
  2591. udelay(ACE_SHORT_DELAY);
  2592. local |= EEPROM_CLK_OUT;
  2593. writel(local, &regs->LocalCtrl);
  2594. readl(&regs->LocalCtrl);
  2595. mb();
  2596. udelay(ACE_SHORT_DELAY);
  2597. local &= ~EEPROM_DATA_OUT;
  2598. writel(local, &regs->LocalCtrl);
  2599. readl(&regs->LocalCtrl);
  2600. mb();
  2601. udelay(ACE_SHORT_DELAY);
  2602. local &= ~EEPROM_CLK_OUT;
  2603. writel(local, &regs->LocalCtrl);
  2604. readl(&regs->LocalCtrl);
  2605. mb();
  2606. }
  2607. static void __devinit eeprom_prep(struct ace_regs __iomem *regs, u8 magic)
  2608. {
  2609. short i;
  2610. u32 local;
  2611. udelay(ACE_SHORT_DELAY);
  2612. local = readl(&regs->LocalCtrl);
  2613. local &= ~EEPROM_DATA_OUT;
  2614. local |= EEPROM_WRITE_ENABLE;
  2615. writel(local, &regs->LocalCtrl);
  2616. readl(&regs->LocalCtrl);
  2617. mb();
  2618. for (i = 0; i < 8; i++, magic <<= 1) {
  2619. udelay(ACE_SHORT_DELAY);
  2620. if (magic & 0x80)
  2621. local |= EEPROM_DATA_OUT;
  2622. else
  2623. local &= ~EEPROM_DATA_OUT;
  2624. writel(local, &regs->LocalCtrl);
  2625. readl(&regs->LocalCtrl);
  2626. mb();
  2627. udelay(ACE_SHORT_DELAY);
  2628. local |= EEPROM_CLK_OUT;
  2629. writel(local, &regs->LocalCtrl);
  2630. readl(&regs->LocalCtrl);
  2631. mb();
  2632. udelay(ACE_SHORT_DELAY);
  2633. local &= ~(EEPROM_CLK_OUT | EEPROM_DATA_OUT);
  2634. writel(local, &regs->LocalCtrl);
  2635. readl(&regs->LocalCtrl);
  2636. mb();
  2637. }
  2638. }
  2639. static int __devinit eeprom_check_ack(struct ace_regs __iomem *regs)
  2640. {
  2641. int state;
  2642. u32 local;
  2643. local = readl(&regs->LocalCtrl);
  2644. local &= ~EEPROM_WRITE_ENABLE;
  2645. writel(local, &regs->LocalCtrl);
  2646. readl(&regs->LocalCtrl);
  2647. mb();
  2648. udelay(ACE_LONG_DELAY);
  2649. local |= EEPROM_CLK_OUT;
  2650. writel(local, &regs->LocalCtrl);
  2651. readl(&regs->LocalCtrl);
  2652. mb();
  2653. udelay(ACE_SHORT_DELAY);
  2654. /* sample data in middle of high clk */
  2655. state = (readl(&regs->LocalCtrl) & EEPROM_DATA_IN) != 0;
  2656. udelay(ACE_SHORT_DELAY);
  2657. mb();
  2658. writel(readl(&regs->LocalCtrl) & ~EEPROM_CLK_OUT, &regs->LocalCtrl);
  2659. readl(&regs->LocalCtrl);
  2660. mb();
  2661. return state;
  2662. }
  2663. static void __devinit eeprom_stop(struct ace_regs __iomem *regs)
  2664. {
  2665. u32 local;
  2666. udelay(ACE_SHORT_DELAY);
  2667. local = readl(&regs->LocalCtrl);
  2668. local |= EEPROM_WRITE_ENABLE;
  2669. writel(local, &regs->LocalCtrl);
  2670. readl(&regs->LocalCtrl);
  2671. mb();
  2672. udelay(ACE_SHORT_DELAY);
  2673. local &= ~EEPROM_DATA_OUT;
  2674. writel(local, &regs->LocalCtrl);
  2675. readl(&regs->LocalCtrl);
  2676. mb();
  2677. udelay(ACE_SHORT_DELAY);
  2678. local |= EEPROM_CLK_OUT;
  2679. writel(local, &regs->LocalCtrl);
  2680. readl(&regs->LocalCtrl);
  2681. mb();
  2682. udelay(ACE_SHORT_DELAY);
  2683. local |= EEPROM_DATA_OUT;
  2684. writel(local, &regs->LocalCtrl);
  2685. readl(&regs->LocalCtrl);
  2686. mb();
  2687. udelay(ACE_LONG_DELAY);
  2688. local &= ~EEPROM_CLK_OUT;
  2689. writel(local, &regs->LocalCtrl);
  2690. mb();
  2691. }
  2692. /*
  2693. * Read a whole byte from the EEPROM.
  2694. */
  2695. static int __devinit read_eeprom_byte(struct net_device *dev,
  2696. unsigned long offset)
  2697. {
  2698. struct ace_private *ap = netdev_priv(dev);
  2699. struct ace_regs __iomem *regs = ap->regs;
  2700. unsigned long flags;
  2701. u32 local;
  2702. int result = 0;
  2703. short i;
  2704. /*
  2705. * Don't take interrupts on this CPU will bit banging
  2706. * the %#%#@$ I2C device
  2707. */
  2708. local_irq_save(flags);
  2709. eeprom_start(regs);
  2710. eeprom_prep(regs, EEPROM_WRITE_SELECT);
  2711. if (eeprom_check_ack(regs)) {
  2712. local_irq_restore(flags);
  2713. printk(KERN_ERR "%s: Unable to sync eeprom\n", ap->name);
  2714. result = -EIO;
  2715. goto eeprom_read_error;
  2716. }
  2717. eeprom_prep(regs, (offset >> 8) & 0xff);
  2718. if (eeprom_check_ack(regs)) {
  2719. local_irq_restore(flags);
  2720. printk(KERN_ERR "%s: Unable to set address byte 0\n",
  2721. ap->name);
  2722. result = -EIO;
  2723. goto eeprom_read_error;
  2724. }
  2725. eeprom_prep(regs, offset & 0xff);
  2726. if (eeprom_check_ack(regs)) {
  2727. local_irq_restore(flags);
  2728. printk(KERN_ERR "%s: Unable to set address byte 1\n",
  2729. ap->name);
  2730. result = -EIO;
  2731. goto eeprom_read_error;
  2732. }
  2733. eeprom_start(regs);
  2734. eeprom_prep(regs, EEPROM_READ_SELECT);
  2735. if (eeprom_check_ack(regs)) {
  2736. local_irq_restore(flags);
  2737. printk(KERN_ERR "%s: Unable to set READ_SELECT\n",
  2738. ap->name);
  2739. result = -EIO;
  2740. goto eeprom_read_error;
  2741. }
  2742. for (i = 0; i < 8; i++) {
  2743. local = readl(&regs->LocalCtrl);
  2744. local &= ~EEPROM_WRITE_ENABLE;
  2745. writel(local, &regs->LocalCtrl);
  2746. readl(&regs->LocalCtrl);
  2747. udelay(ACE_LONG_DELAY);
  2748. mb();
  2749. local |= EEPROM_CLK_OUT;
  2750. writel(local, &regs->LocalCtrl);
  2751. readl(&regs->LocalCtrl);
  2752. mb();
  2753. udelay(ACE_SHORT_DELAY);
  2754. /* sample data mid high clk */
  2755. result = (result << 1) |
  2756. ((readl(&regs->LocalCtrl) & EEPROM_DATA_IN) != 0);
  2757. udelay(ACE_SHORT_DELAY);
  2758. mb();
  2759. local = readl(&regs->LocalCtrl);
  2760. local &= ~EEPROM_CLK_OUT;
  2761. writel(local, &regs->LocalCtrl);
  2762. readl(&regs->LocalCtrl);
  2763. udelay(ACE_SHORT_DELAY);
  2764. mb();
  2765. if (i == 7) {
  2766. local |= EEPROM_WRITE_ENABLE;
  2767. writel(local, &regs->LocalCtrl);
  2768. readl(&regs->LocalCtrl);
  2769. mb();
  2770. udelay(ACE_SHORT_DELAY);
  2771. }
  2772. }
  2773. local |= EEPROM_DATA_OUT;
  2774. writel(local, &regs->LocalCtrl);
  2775. readl(&regs->LocalCtrl);
  2776. mb();
  2777. udelay(ACE_SHORT_DELAY);
  2778. writel(readl(&regs->LocalCtrl) | EEPROM_CLK_OUT, &regs->LocalCtrl);
  2779. readl(&regs->LocalCtrl);
  2780. udelay(ACE_LONG_DELAY);
  2781. writel(readl(&regs->LocalCtrl) & ~EEPROM_CLK_OUT, &regs->LocalCtrl);
  2782. readl(&regs->LocalCtrl);
  2783. mb();
  2784. udelay(ACE_SHORT_DELAY);
  2785. eeprom_stop(regs);
  2786. local_irq_restore(flags);
  2787. out:
  2788. return result;
  2789. eeprom_read_error:
  2790. printk(KERN_ERR "%s: Unable to read eeprom byte 0x%02lx\n",
  2791. ap->name, offset);
  2792. goto out;
  2793. }