e100.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885
  1. /*******************************************************************************
  2. Copyright(c) 1999 - 2006 Intel Corporation. All rights reserved.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the Free
  5. Software Foundation; either version 2 of the License, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc., 59
  13. Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. The full GNU General Public License is included in this distribution in the
  15. file called LICENSE.
  16. Contact Information:
  17. Linux NICS <linux.nics@intel.com>
  18. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. *******************************************************************************/
  20. /*
  21. * e100.c: Intel(R) PRO/100 ethernet driver
  22. *
  23. * (Re)written 2003 by scott.feldman@intel.com. Based loosely on
  24. * original e100 driver, but better described as a munging of
  25. * e100, e1000, eepro100, tg3, 8139cp, and other drivers.
  26. *
  27. * References:
  28. * Intel 8255x 10/100 Mbps Ethernet Controller Family,
  29. * Open Source Software Developers Manual,
  30. * http://sourceforge.net/projects/e1000
  31. *
  32. *
  33. * Theory of Operation
  34. *
  35. * I. General
  36. *
  37. * The driver supports Intel(R) 10/100 Mbps PCI Fast Ethernet
  38. * controller family, which includes the 82557, 82558, 82559, 82550,
  39. * 82551, and 82562 devices. 82558 and greater controllers
  40. * integrate the Intel 82555 PHY. The controllers are used in
  41. * server and client network interface cards, as well as in
  42. * LAN-On-Motherboard (LOM), CardBus, MiniPCI, and ICHx
  43. * configurations. 8255x supports a 32-bit linear addressing
  44. * mode and operates at 33Mhz PCI clock rate.
  45. *
  46. * II. Driver Operation
  47. *
  48. * Memory-mapped mode is used exclusively to access the device's
  49. * shared-memory structure, the Control/Status Registers (CSR). All
  50. * setup, configuration, and control of the device, including queuing
  51. * of Tx, Rx, and configuration commands is through the CSR.
  52. * cmd_lock serializes accesses to the CSR command register. cb_lock
  53. * protects the shared Command Block List (CBL).
  54. *
  55. * 8255x is highly MII-compliant and all access to the PHY go
  56. * through the Management Data Interface (MDI). Consequently, the
  57. * driver leverages the mii.c library shared with other MII-compliant
  58. * devices.
  59. *
  60. * Big- and Little-Endian byte order as well as 32- and 64-bit
  61. * archs are supported. Weak-ordered memory and non-cache-coherent
  62. * archs are supported.
  63. *
  64. * III. Transmit
  65. *
  66. * A Tx skb is mapped and hangs off of a TCB. TCBs are linked
  67. * together in a fixed-size ring (CBL) thus forming the flexible mode
  68. * memory structure. A TCB marked with the suspend-bit indicates
  69. * the end of the ring. The last TCB processed suspends the
  70. * controller, and the controller can be restarted by issue a CU
  71. * resume command to continue from the suspend point, or a CU start
  72. * command to start at a given position in the ring.
  73. *
  74. * Non-Tx commands (config, multicast setup, etc) are linked
  75. * into the CBL ring along with Tx commands. The common structure
  76. * used for both Tx and non-Tx commands is the Command Block (CB).
  77. *
  78. * cb_to_use is the next CB to use for queuing a command; cb_to_clean
  79. * is the next CB to check for completion; cb_to_send is the first
  80. * CB to start on in case of a previous failure to resume. CB clean
  81. * up happens in interrupt context in response to a CU interrupt.
  82. * cbs_avail keeps track of number of free CB resources available.
  83. *
  84. * Hardware padding of short packets to minimum packet size is
  85. * enabled. 82557 pads with 7Eh, while the later controllers pad
  86. * with 00h.
  87. *
  88. * IV. Recieve
  89. *
  90. * The Receive Frame Area (RFA) comprises a ring of Receive Frame
  91. * Descriptors (RFD) + data buffer, thus forming the simplified mode
  92. * memory structure. Rx skbs are allocated to contain both the RFD
  93. * and the data buffer, but the RFD is pulled off before the skb is
  94. * indicated. The data buffer is aligned such that encapsulated
  95. * protocol headers are u32-aligned. Since the RFD is part of the
  96. * mapped shared memory, and completion status is contained within
  97. * the RFD, the RFD must be dma_sync'ed to maintain a consistent
  98. * view from software and hardware.
  99. *
  100. * Under typical operation, the receive unit (RU) is start once,
  101. * and the controller happily fills RFDs as frames arrive. If
  102. * replacement RFDs cannot be allocated, or the RU goes non-active,
  103. * the RU must be restarted. Frame arrival generates an interrupt,
  104. * and Rx indication and re-allocation happen in the same context,
  105. * therefore no locking is required. A software-generated interrupt
  106. * is generated from the watchdog to recover from a failed allocation
  107. * senario where all Rx resources have been indicated and none re-
  108. * placed.
  109. *
  110. * V. Miscellaneous
  111. *
  112. * VLAN offloading of tagging, stripping and filtering is not
  113. * supported, but driver will accommodate the extra 4-byte VLAN tag
  114. * for processing by upper layers. Tx/Rx Checksum offloading is not
  115. * supported. Tx Scatter/Gather is not supported. Jumbo Frames is
  116. * not supported (hardware limitation).
  117. *
  118. * MagicPacket(tm) WoL support is enabled/disabled via ethtool.
  119. *
  120. * Thanks to JC (jchapman@katalix.com) for helping with
  121. * testing/troubleshooting the development driver.
  122. *
  123. * TODO:
  124. * o several entry points race with dev->close
  125. * o check for tx-no-resources/stop Q races with tx clean/wake Q
  126. *
  127. * FIXES:
  128. * 2005/12/02 - Michael O'Donnell <Michael.ODonnell at stratus dot com>
  129. * - Stratus87247: protect MDI control register manipulations
  130. */
  131. #include <linux/module.h>
  132. #include <linux/moduleparam.h>
  133. #include <linux/kernel.h>
  134. #include <linux/types.h>
  135. #include <linux/slab.h>
  136. #include <linux/delay.h>
  137. #include <linux/init.h>
  138. #include <linux/pci.h>
  139. #include <linux/dma-mapping.h>
  140. #include <linux/netdevice.h>
  141. #include <linux/etherdevice.h>
  142. #include <linux/mii.h>
  143. #include <linux/if_vlan.h>
  144. #include <linux/skbuff.h>
  145. #include <linux/ethtool.h>
  146. #include <linux/string.h>
  147. #include <asm/unaligned.h>
  148. #define DRV_NAME "e100"
  149. #define DRV_EXT "-NAPI"
  150. #define DRV_VERSION "3.5.16-k2"DRV_EXT
  151. #define DRV_DESCRIPTION "Intel(R) PRO/100 Network Driver"
  152. #define DRV_COPYRIGHT "Copyright(c) 1999-2006 Intel Corporation"
  153. #define PFX DRV_NAME ": "
  154. #define E100_WATCHDOG_PERIOD (2 * HZ)
  155. #define E100_NAPI_WEIGHT 16
  156. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  157. MODULE_AUTHOR(DRV_COPYRIGHT);
  158. MODULE_LICENSE("GPL");
  159. MODULE_VERSION(DRV_VERSION);
  160. static int debug = 3;
  161. static int eeprom_bad_csum_allow = 0;
  162. module_param(debug, int, 0);
  163. module_param(eeprom_bad_csum_allow, int, 0);
  164. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  165. MODULE_PARM_DESC(eeprom_bad_csum_allow, "Allow bad eeprom checksums");
  166. #define DPRINTK(nlevel, klevel, fmt, args...) \
  167. (void)((NETIF_MSG_##nlevel & nic->msg_enable) && \
  168. printk(KERN_##klevel PFX "%s: %s: " fmt, nic->netdev->name, \
  169. __FUNCTION__ , ## args))
  170. #define INTEL_8255X_ETHERNET_DEVICE(device_id, ich) {\
  171. PCI_VENDOR_ID_INTEL, device_id, PCI_ANY_ID, PCI_ANY_ID, \
  172. PCI_CLASS_NETWORK_ETHERNET << 8, 0xFFFF00, ich }
  173. static struct pci_device_id e100_id_table[] = {
  174. INTEL_8255X_ETHERNET_DEVICE(0x1029, 0),
  175. INTEL_8255X_ETHERNET_DEVICE(0x1030, 0),
  176. INTEL_8255X_ETHERNET_DEVICE(0x1031, 3),
  177. INTEL_8255X_ETHERNET_DEVICE(0x1032, 3),
  178. INTEL_8255X_ETHERNET_DEVICE(0x1033, 3),
  179. INTEL_8255X_ETHERNET_DEVICE(0x1034, 3),
  180. INTEL_8255X_ETHERNET_DEVICE(0x1038, 3),
  181. INTEL_8255X_ETHERNET_DEVICE(0x1039, 4),
  182. INTEL_8255X_ETHERNET_DEVICE(0x103A, 4),
  183. INTEL_8255X_ETHERNET_DEVICE(0x103B, 4),
  184. INTEL_8255X_ETHERNET_DEVICE(0x103C, 4),
  185. INTEL_8255X_ETHERNET_DEVICE(0x103D, 4),
  186. INTEL_8255X_ETHERNET_DEVICE(0x103E, 4),
  187. INTEL_8255X_ETHERNET_DEVICE(0x1050, 5),
  188. INTEL_8255X_ETHERNET_DEVICE(0x1051, 5),
  189. INTEL_8255X_ETHERNET_DEVICE(0x1052, 5),
  190. INTEL_8255X_ETHERNET_DEVICE(0x1053, 5),
  191. INTEL_8255X_ETHERNET_DEVICE(0x1054, 5),
  192. INTEL_8255X_ETHERNET_DEVICE(0x1055, 5),
  193. INTEL_8255X_ETHERNET_DEVICE(0x1056, 5),
  194. INTEL_8255X_ETHERNET_DEVICE(0x1057, 5),
  195. INTEL_8255X_ETHERNET_DEVICE(0x1059, 0),
  196. INTEL_8255X_ETHERNET_DEVICE(0x1064, 6),
  197. INTEL_8255X_ETHERNET_DEVICE(0x1065, 6),
  198. INTEL_8255X_ETHERNET_DEVICE(0x1066, 6),
  199. INTEL_8255X_ETHERNET_DEVICE(0x1067, 6),
  200. INTEL_8255X_ETHERNET_DEVICE(0x1068, 6),
  201. INTEL_8255X_ETHERNET_DEVICE(0x1069, 6),
  202. INTEL_8255X_ETHERNET_DEVICE(0x106A, 6),
  203. INTEL_8255X_ETHERNET_DEVICE(0x106B, 6),
  204. INTEL_8255X_ETHERNET_DEVICE(0x1091, 7),
  205. INTEL_8255X_ETHERNET_DEVICE(0x1092, 7),
  206. INTEL_8255X_ETHERNET_DEVICE(0x1093, 7),
  207. INTEL_8255X_ETHERNET_DEVICE(0x1094, 7),
  208. INTEL_8255X_ETHERNET_DEVICE(0x1095, 7),
  209. INTEL_8255X_ETHERNET_DEVICE(0x1209, 0),
  210. INTEL_8255X_ETHERNET_DEVICE(0x1229, 0),
  211. INTEL_8255X_ETHERNET_DEVICE(0x2449, 2),
  212. INTEL_8255X_ETHERNET_DEVICE(0x2459, 2),
  213. INTEL_8255X_ETHERNET_DEVICE(0x245D, 2),
  214. INTEL_8255X_ETHERNET_DEVICE(0x27DC, 7),
  215. { 0, }
  216. };
  217. MODULE_DEVICE_TABLE(pci, e100_id_table);
  218. enum mac {
  219. mac_82557_D100_A = 0,
  220. mac_82557_D100_B = 1,
  221. mac_82557_D100_C = 2,
  222. mac_82558_D101_A4 = 4,
  223. mac_82558_D101_B0 = 5,
  224. mac_82559_D101M = 8,
  225. mac_82559_D101S = 9,
  226. mac_82550_D102 = 12,
  227. mac_82550_D102_C = 13,
  228. mac_82551_E = 14,
  229. mac_82551_F = 15,
  230. mac_82551_10 = 16,
  231. mac_unknown = 0xFF,
  232. };
  233. enum phy {
  234. phy_100a = 0x000003E0,
  235. phy_100c = 0x035002A8,
  236. phy_82555_tx = 0x015002A8,
  237. phy_nsc_tx = 0x5C002000,
  238. phy_82562_et = 0x033002A8,
  239. phy_82562_em = 0x032002A8,
  240. phy_82562_ek = 0x031002A8,
  241. phy_82562_eh = 0x017002A8,
  242. phy_unknown = 0xFFFFFFFF,
  243. };
  244. /* CSR (Control/Status Registers) */
  245. struct csr {
  246. struct {
  247. u8 status;
  248. u8 stat_ack;
  249. u8 cmd_lo;
  250. u8 cmd_hi;
  251. u32 gen_ptr;
  252. } scb;
  253. u32 port;
  254. u16 flash_ctrl;
  255. u8 eeprom_ctrl_lo;
  256. u8 eeprom_ctrl_hi;
  257. u32 mdi_ctrl;
  258. u32 rx_dma_count;
  259. };
  260. enum scb_status {
  261. rus_ready = 0x10,
  262. rus_mask = 0x3C,
  263. };
  264. enum ru_state {
  265. RU_SUSPENDED = 0,
  266. RU_RUNNING = 1,
  267. RU_UNINITIALIZED = -1,
  268. };
  269. enum scb_stat_ack {
  270. stat_ack_not_ours = 0x00,
  271. stat_ack_sw_gen = 0x04,
  272. stat_ack_rnr = 0x10,
  273. stat_ack_cu_idle = 0x20,
  274. stat_ack_frame_rx = 0x40,
  275. stat_ack_cu_cmd_done = 0x80,
  276. stat_ack_not_present = 0xFF,
  277. stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx),
  278. stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done),
  279. };
  280. enum scb_cmd_hi {
  281. irq_mask_none = 0x00,
  282. irq_mask_all = 0x01,
  283. irq_sw_gen = 0x02,
  284. };
  285. enum scb_cmd_lo {
  286. cuc_nop = 0x00,
  287. ruc_start = 0x01,
  288. ruc_load_base = 0x06,
  289. cuc_start = 0x10,
  290. cuc_resume = 0x20,
  291. cuc_dump_addr = 0x40,
  292. cuc_dump_stats = 0x50,
  293. cuc_load_base = 0x60,
  294. cuc_dump_reset = 0x70,
  295. };
  296. enum cuc_dump {
  297. cuc_dump_complete = 0x0000A005,
  298. cuc_dump_reset_complete = 0x0000A007,
  299. };
  300. enum port {
  301. software_reset = 0x0000,
  302. selftest = 0x0001,
  303. selective_reset = 0x0002,
  304. };
  305. enum eeprom_ctrl_lo {
  306. eesk = 0x01,
  307. eecs = 0x02,
  308. eedi = 0x04,
  309. eedo = 0x08,
  310. };
  311. enum mdi_ctrl {
  312. mdi_write = 0x04000000,
  313. mdi_read = 0x08000000,
  314. mdi_ready = 0x10000000,
  315. };
  316. enum eeprom_op {
  317. op_write = 0x05,
  318. op_read = 0x06,
  319. op_ewds = 0x10,
  320. op_ewen = 0x13,
  321. };
  322. enum eeprom_offsets {
  323. eeprom_cnfg_mdix = 0x03,
  324. eeprom_id = 0x0A,
  325. eeprom_config_asf = 0x0D,
  326. eeprom_smbus_addr = 0x90,
  327. };
  328. enum eeprom_cnfg_mdix {
  329. eeprom_mdix_enabled = 0x0080,
  330. };
  331. enum eeprom_id {
  332. eeprom_id_wol = 0x0020,
  333. };
  334. enum eeprom_config_asf {
  335. eeprom_asf = 0x8000,
  336. eeprom_gcl = 0x4000,
  337. };
  338. enum cb_status {
  339. cb_complete = 0x8000,
  340. cb_ok = 0x2000,
  341. };
  342. enum cb_command {
  343. cb_nop = 0x0000,
  344. cb_iaaddr = 0x0001,
  345. cb_config = 0x0002,
  346. cb_multi = 0x0003,
  347. cb_tx = 0x0004,
  348. cb_ucode = 0x0005,
  349. cb_dump = 0x0006,
  350. cb_tx_sf = 0x0008,
  351. cb_cid = 0x1f00,
  352. cb_i = 0x2000,
  353. cb_s = 0x4000,
  354. cb_el = 0x8000,
  355. };
  356. struct rfd {
  357. u16 status;
  358. u16 command;
  359. u32 link;
  360. u32 rbd;
  361. u16 actual_size;
  362. u16 size;
  363. };
  364. struct rx {
  365. struct rx *next, *prev;
  366. struct sk_buff *skb;
  367. dma_addr_t dma_addr;
  368. };
  369. #if defined(__BIG_ENDIAN_BITFIELD)
  370. #define X(a,b) b,a
  371. #else
  372. #define X(a,b) a,b
  373. #endif
  374. struct config {
  375. /*0*/ u8 X(byte_count:6, pad0:2);
  376. /*1*/ u8 X(X(rx_fifo_limit:4, tx_fifo_limit:3), pad1:1);
  377. /*2*/ u8 adaptive_ifs;
  378. /*3*/ u8 X(X(X(X(mwi_enable:1, type_enable:1), read_align_enable:1),
  379. term_write_cache_line:1), pad3:4);
  380. /*4*/ u8 X(rx_dma_max_count:7, pad4:1);
  381. /*5*/ u8 X(tx_dma_max_count:7, dma_max_count_enable:1);
  382. /*6*/ u8 X(X(X(X(X(X(X(late_scb_update:1, direct_rx_dma:1),
  383. tno_intr:1), cna_intr:1), standard_tcb:1), standard_stat_counter:1),
  384. rx_discard_overruns:1), rx_save_bad_frames:1);
  385. /*7*/ u8 X(X(X(X(X(rx_discard_short_frames:1, tx_underrun_retry:2),
  386. pad7:2), rx_extended_rfd:1), tx_two_frames_in_fifo:1),
  387. tx_dynamic_tbd:1);
  388. /*8*/ u8 X(X(mii_mode:1, pad8:6), csma_disabled:1);
  389. /*9*/ u8 X(X(X(X(X(rx_tcpudp_checksum:1, pad9:3), vlan_arp_tco:1),
  390. link_status_wake:1), arp_wake:1), mcmatch_wake:1);
  391. /*10*/ u8 X(X(X(pad10:3, no_source_addr_insertion:1), preamble_length:2),
  392. loopback:2);
  393. /*11*/ u8 X(linear_priority:3, pad11:5);
  394. /*12*/ u8 X(X(linear_priority_mode:1, pad12:3), ifs:4);
  395. /*13*/ u8 ip_addr_lo;
  396. /*14*/ u8 ip_addr_hi;
  397. /*15*/ u8 X(X(X(X(X(X(X(promiscuous_mode:1, broadcast_disabled:1),
  398. wait_after_win:1), pad15_1:1), ignore_ul_bit:1), crc_16_bit:1),
  399. pad15_2:1), crs_or_cdt:1);
  400. /*16*/ u8 fc_delay_lo;
  401. /*17*/ u8 fc_delay_hi;
  402. /*18*/ u8 X(X(X(X(X(rx_stripping:1, tx_padding:1), rx_crc_transfer:1),
  403. rx_long_ok:1), fc_priority_threshold:3), pad18:1);
  404. /*19*/ u8 X(X(X(X(X(X(X(addr_wake:1, magic_packet_disable:1),
  405. fc_disable:1), fc_restop:1), fc_restart:1), fc_reject:1),
  406. full_duplex_force:1), full_duplex_pin:1);
  407. /*20*/ u8 X(X(X(pad20_1:5, fc_priority_location:1), multi_ia:1), pad20_2:1);
  408. /*21*/ u8 X(X(pad21_1:3, multicast_all:1), pad21_2:4);
  409. /*22*/ u8 X(X(rx_d102_mode:1, rx_vlan_drop:1), pad22:6);
  410. u8 pad_d102[9];
  411. };
  412. #define E100_MAX_MULTICAST_ADDRS 64
  413. struct multi {
  414. u16 count;
  415. u8 addr[E100_MAX_MULTICAST_ADDRS * ETH_ALEN + 2/*pad*/];
  416. };
  417. /* Important: keep total struct u32-aligned */
  418. #define UCODE_SIZE 134
  419. struct cb {
  420. u16 status;
  421. u16 command;
  422. u32 link;
  423. union {
  424. u8 iaaddr[ETH_ALEN];
  425. u32 ucode[UCODE_SIZE];
  426. struct config config;
  427. struct multi multi;
  428. struct {
  429. u32 tbd_array;
  430. u16 tcb_byte_count;
  431. u8 threshold;
  432. u8 tbd_count;
  433. struct {
  434. u32 buf_addr;
  435. u16 size;
  436. u16 eol;
  437. } tbd;
  438. } tcb;
  439. u32 dump_buffer_addr;
  440. } u;
  441. struct cb *next, *prev;
  442. dma_addr_t dma_addr;
  443. struct sk_buff *skb;
  444. };
  445. enum loopback {
  446. lb_none = 0, lb_mac = 1, lb_phy = 3,
  447. };
  448. struct stats {
  449. u32 tx_good_frames, tx_max_collisions, tx_late_collisions,
  450. tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,
  451. tx_multiple_collisions, tx_total_collisions;
  452. u32 rx_good_frames, rx_crc_errors, rx_alignment_errors,
  453. rx_resource_errors, rx_overrun_errors, rx_cdt_errors,
  454. rx_short_frame_errors;
  455. u32 fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;
  456. u16 xmt_tco_frames, rcv_tco_frames;
  457. u32 complete;
  458. };
  459. struct mem {
  460. struct {
  461. u32 signature;
  462. u32 result;
  463. } selftest;
  464. struct stats stats;
  465. u8 dump_buf[596];
  466. };
  467. struct param_range {
  468. u32 min;
  469. u32 max;
  470. u32 count;
  471. };
  472. struct params {
  473. struct param_range rfds;
  474. struct param_range cbs;
  475. };
  476. struct nic {
  477. /* Begin: frequently used values: keep adjacent for cache effect */
  478. u32 msg_enable ____cacheline_aligned;
  479. struct net_device *netdev;
  480. struct pci_dev *pdev;
  481. struct rx *rxs ____cacheline_aligned;
  482. struct rx *rx_to_use;
  483. struct rx *rx_to_clean;
  484. struct rfd blank_rfd;
  485. enum ru_state ru_running;
  486. spinlock_t cb_lock ____cacheline_aligned;
  487. spinlock_t cmd_lock;
  488. struct csr __iomem *csr;
  489. enum scb_cmd_lo cuc_cmd;
  490. unsigned int cbs_avail;
  491. struct cb *cbs;
  492. struct cb *cb_to_use;
  493. struct cb *cb_to_send;
  494. struct cb *cb_to_clean;
  495. u16 tx_command;
  496. /* End: frequently used values: keep adjacent for cache effect */
  497. enum {
  498. ich = (1 << 0),
  499. promiscuous = (1 << 1),
  500. multicast_all = (1 << 2),
  501. wol_magic = (1 << 3),
  502. ich_10h_workaround = (1 << 4),
  503. } flags ____cacheline_aligned;
  504. enum mac mac;
  505. enum phy phy;
  506. struct params params;
  507. struct net_device_stats net_stats;
  508. struct timer_list watchdog;
  509. struct timer_list blink_timer;
  510. struct mii_if_info mii;
  511. struct work_struct tx_timeout_task;
  512. enum loopback loopback;
  513. struct mem *mem;
  514. dma_addr_t dma_addr;
  515. dma_addr_t cbs_dma_addr;
  516. u8 adaptive_ifs;
  517. u8 tx_threshold;
  518. u32 tx_frames;
  519. u32 tx_collisions;
  520. u32 tx_deferred;
  521. u32 tx_single_collisions;
  522. u32 tx_multiple_collisions;
  523. u32 tx_fc_pause;
  524. u32 tx_tco_frames;
  525. u32 rx_fc_pause;
  526. u32 rx_fc_unsupported;
  527. u32 rx_tco_frames;
  528. u32 rx_over_length_errors;
  529. u8 rev_id;
  530. u16 leds;
  531. u16 eeprom_wc;
  532. u16 eeprom[256];
  533. spinlock_t mdio_lock;
  534. };
  535. static inline void e100_write_flush(struct nic *nic)
  536. {
  537. /* Flush previous PCI writes through intermediate bridges
  538. * by doing a benign read */
  539. (void)readb(&nic->csr->scb.status);
  540. }
  541. static void e100_enable_irq(struct nic *nic)
  542. {
  543. unsigned long flags;
  544. spin_lock_irqsave(&nic->cmd_lock, flags);
  545. writeb(irq_mask_none, &nic->csr->scb.cmd_hi);
  546. e100_write_flush(nic);
  547. spin_unlock_irqrestore(&nic->cmd_lock, flags);
  548. }
  549. static void e100_disable_irq(struct nic *nic)
  550. {
  551. unsigned long flags;
  552. spin_lock_irqsave(&nic->cmd_lock, flags);
  553. writeb(irq_mask_all, &nic->csr->scb.cmd_hi);
  554. e100_write_flush(nic);
  555. spin_unlock_irqrestore(&nic->cmd_lock, flags);
  556. }
  557. static void e100_hw_reset(struct nic *nic)
  558. {
  559. /* Put CU and RU into idle with a selective reset to get
  560. * device off of PCI bus */
  561. writel(selective_reset, &nic->csr->port);
  562. e100_write_flush(nic); udelay(20);
  563. /* Now fully reset device */
  564. writel(software_reset, &nic->csr->port);
  565. e100_write_flush(nic); udelay(20);
  566. /* Mask off our interrupt line - it's unmasked after reset */
  567. e100_disable_irq(nic);
  568. }
  569. static int e100_self_test(struct nic *nic)
  570. {
  571. u32 dma_addr = nic->dma_addr + offsetof(struct mem, selftest);
  572. /* Passing the self-test is a pretty good indication
  573. * that the device can DMA to/from host memory */
  574. nic->mem->selftest.signature = 0;
  575. nic->mem->selftest.result = 0xFFFFFFFF;
  576. writel(selftest | dma_addr, &nic->csr->port);
  577. e100_write_flush(nic);
  578. /* Wait 10 msec for self-test to complete */
  579. msleep(10);
  580. /* Interrupts are enabled after self-test */
  581. e100_disable_irq(nic);
  582. /* Check results of self-test */
  583. if(nic->mem->selftest.result != 0) {
  584. DPRINTK(HW, ERR, "Self-test failed: result=0x%08X\n",
  585. nic->mem->selftest.result);
  586. return -ETIMEDOUT;
  587. }
  588. if(nic->mem->selftest.signature == 0) {
  589. DPRINTK(HW, ERR, "Self-test failed: timed out\n");
  590. return -ETIMEDOUT;
  591. }
  592. return 0;
  593. }
  594. static void e100_eeprom_write(struct nic *nic, u16 addr_len, u16 addr, u16 data)
  595. {
  596. u32 cmd_addr_data[3];
  597. u8 ctrl;
  598. int i, j;
  599. /* Three cmds: write/erase enable, write data, write/erase disable */
  600. cmd_addr_data[0] = op_ewen << (addr_len - 2);
  601. cmd_addr_data[1] = (((op_write << addr_len) | addr) << 16) |
  602. cpu_to_le16(data);
  603. cmd_addr_data[2] = op_ewds << (addr_len - 2);
  604. /* Bit-bang cmds to write word to eeprom */
  605. for(j = 0; j < 3; j++) {
  606. /* Chip select */
  607. writeb(eecs | eesk, &nic->csr->eeprom_ctrl_lo);
  608. e100_write_flush(nic); udelay(4);
  609. for(i = 31; i >= 0; i--) {
  610. ctrl = (cmd_addr_data[j] & (1 << i)) ?
  611. eecs | eedi : eecs;
  612. writeb(ctrl, &nic->csr->eeprom_ctrl_lo);
  613. e100_write_flush(nic); udelay(4);
  614. writeb(ctrl | eesk, &nic->csr->eeprom_ctrl_lo);
  615. e100_write_flush(nic); udelay(4);
  616. }
  617. /* Wait 10 msec for cmd to complete */
  618. msleep(10);
  619. /* Chip deselect */
  620. writeb(0, &nic->csr->eeprom_ctrl_lo);
  621. e100_write_flush(nic); udelay(4);
  622. }
  623. };
  624. /* General technique stolen from the eepro100 driver - very clever */
  625. static u16 e100_eeprom_read(struct nic *nic, u16 *addr_len, u16 addr)
  626. {
  627. u32 cmd_addr_data;
  628. u16 data = 0;
  629. u8 ctrl;
  630. int i;
  631. cmd_addr_data = ((op_read << *addr_len) | addr) << 16;
  632. /* Chip select */
  633. writeb(eecs | eesk, &nic->csr->eeprom_ctrl_lo);
  634. e100_write_flush(nic); udelay(4);
  635. /* Bit-bang to read word from eeprom */
  636. for(i = 31; i >= 0; i--) {
  637. ctrl = (cmd_addr_data & (1 << i)) ? eecs | eedi : eecs;
  638. writeb(ctrl, &nic->csr->eeprom_ctrl_lo);
  639. e100_write_flush(nic); udelay(4);
  640. writeb(ctrl | eesk, &nic->csr->eeprom_ctrl_lo);
  641. e100_write_flush(nic); udelay(4);
  642. /* Eeprom drives a dummy zero to EEDO after receiving
  643. * complete address. Use this to adjust addr_len. */
  644. ctrl = readb(&nic->csr->eeprom_ctrl_lo);
  645. if(!(ctrl & eedo) && i > 16) {
  646. *addr_len -= (i - 16);
  647. i = 17;
  648. }
  649. data = (data << 1) | (ctrl & eedo ? 1 : 0);
  650. }
  651. /* Chip deselect */
  652. writeb(0, &nic->csr->eeprom_ctrl_lo);
  653. e100_write_flush(nic); udelay(4);
  654. return le16_to_cpu(data);
  655. };
  656. /* Load entire EEPROM image into driver cache and validate checksum */
  657. static int e100_eeprom_load(struct nic *nic)
  658. {
  659. u16 addr, addr_len = 8, checksum = 0;
  660. /* Try reading with an 8-bit addr len to discover actual addr len */
  661. e100_eeprom_read(nic, &addr_len, 0);
  662. nic->eeprom_wc = 1 << addr_len;
  663. for(addr = 0; addr < nic->eeprom_wc; addr++) {
  664. nic->eeprom[addr] = e100_eeprom_read(nic, &addr_len, addr);
  665. if(addr < nic->eeprom_wc - 1)
  666. checksum += cpu_to_le16(nic->eeprom[addr]);
  667. }
  668. /* The checksum, stored in the last word, is calculated such that
  669. * the sum of words should be 0xBABA */
  670. checksum = le16_to_cpu(0xBABA - checksum);
  671. if(checksum != nic->eeprom[nic->eeprom_wc - 1]) {
  672. DPRINTK(PROBE, ERR, "EEPROM corrupted\n");
  673. if (!eeprom_bad_csum_allow)
  674. return -EAGAIN;
  675. }
  676. return 0;
  677. }
  678. /* Save (portion of) driver EEPROM cache to device and update checksum */
  679. static int e100_eeprom_save(struct nic *nic, u16 start, u16 count)
  680. {
  681. u16 addr, addr_len = 8, checksum = 0;
  682. /* Try reading with an 8-bit addr len to discover actual addr len */
  683. e100_eeprom_read(nic, &addr_len, 0);
  684. nic->eeprom_wc = 1 << addr_len;
  685. if(start + count >= nic->eeprom_wc)
  686. return -EINVAL;
  687. for(addr = start; addr < start + count; addr++)
  688. e100_eeprom_write(nic, addr_len, addr, nic->eeprom[addr]);
  689. /* The checksum, stored in the last word, is calculated such that
  690. * the sum of words should be 0xBABA */
  691. for(addr = 0; addr < nic->eeprom_wc - 1; addr++)
  692. checksum += cpu_to_le16(nic->eeprom[addr]);
  693. nic->eeprom[nic->eeprom_wc - 1] = le16_to_cpu(0xBABA - checksum);
  694. e100_eeprom_write(nic, addr_len, nic->eeprom_wc - 1,
  695. nic->eeprom[nic->eeprom_wc - 1]);
  696. return 0;
  697. }
  698. #define E100_WAIT_SCB_TIMEOUT 20000 /* we might have to wait 100ms!!! */
  699. #define E100_WAIT_SCB_FAST 20 /* delay like the old code */
  700. static int e100_exec_cmd(struct nic *nic, u8 cmd, dma_addr_t dma_addr)
  701. {
  702. unsigned long flags;
  703. unsigned int i;
  704. int err = 0;
  705. spin_lock_irqsave(&nic->cmd_lock, flags);
  706. /* Previous command is accepted when SCB clears */
  707. for(i = 0; i < E100_WAIT_SCB_TIMEOUT; i++) {
  708. if(likely(!readb(&nic->csr->scb.cmd_lo)))
  709. break;
  710. cpu_relax();
  711. if(unlikely(i > E100_WAIT_SCB_FAST))
  712. udelay(5);
  713. }
  714. if(unlikely(i == E100_WAIT_SCB_TIMEOUT)) {
  715. err = -EAGAIN;
  716. goto err_unlock;
  717. }
  718. if(unlikely(cmd != cuc_resume))
  719. writel(dma_addr, &nic->csr->scb.gen_ptr);
  720. writeb(cmd, &nic->csr->scb.cmd_lo);
  721. err_unlock:
  722. spin_unlock_irqrestore(&nic->cmd_lock, flags);
  723. return err;
  724. }
  725. static int e100_exec_cb(struct nic *nic, struct sk_buff *skb,
  726. void (*cb_prepare)(struct nic *, struct cb *, struct sk_buff *))
  727. {
  728. struct cb *cb;
  729. unsigned long flags;
  730. int err = 0;
  731. spin_lock_irqsave(&nic->cb_lock, flags);
  732. if(unlikely(!nic->cbs_avail)) {
  733. err = -ENOMEM;
  734. goto err_unlock;
  735. }
  736. cb = nic->cb_to_use;
  737. nic->cb_to_use = cb->next;
  738. nic->cbs_avail--;
  739. cb->skb = skb;
  740. if(unlikely(!nic->cbs_avail))
  741. err = -ENOSPC;
  742. cb_prepare(nic, cb, skb);
  743. /* Order is important otherwise we'll be in a race with h/w:
  744. * set S-bit in current first, then clear S-bit in previous. */
  745. cb->command |= cpu_to_le16(cb_s);
  746. wmb();
  747. cb->prev->command &= cpu_to_le16(~cb_s);
  748. while(nic->cb_to_send != nic->cb_to_use) {
  749. if(unlikely(e100_exec_cmd(nic, nic->cuc_cmd,
  750. nic->cb_to_send->dma_addr))) {
  751. /* Ok, here's where things get sticky. It's
  752. * possible that we can't schedule the command
  753. * because the controller is too busy, so
  754. * let's just queue the command and try again
  755. * when another command is scheduled. */
  756. if(err == -ENOSPC) {
  757. //request a reset
  758. schedule_work(&nic->tx_timeout_task);
  759. }
  760. break;
  761. } else {
  762. nic->cuc_cmd = cuc_resume;
  763. nic->cb_to_send = nic->cb_to_send->next;
  764. }
  765. }
  766. err_unlock:
  767. spin_unlock_irqrestore(&nic->cb_lock, flags);
  768. return err;
  769. }
  770. static u16 mdio_ctrl(struct nic *nic, u32 addr, u32 dir, u32 reg, u16 data)
  771. {
  772. u32 data_out = 0;
  773. unsigned int i;
  774. unsigned long flags;
  775. /*
  776. * Stratus87247: we shouldn't be writing the MDI control
  777. * register until the Ready bit shows True. Also, since
  778. * manipulation of the MDI control registers is a multi-step
  779. * procedure it should be done under lock.
  780. */
  781. spin_lock_irqsave(&nic->mdio_lock, flags);
  782. for (i = 100; i; --i) {
  783. if (readl(&nic->csr->mdi_ctrl) & mdi_ready)
  784. break;
  785. udelay(20);
  786. }
  787. if (unlikely(!i)) {
  788. printk("e100.mdio_ctrl(%s) won't go Ready\n",
  789. nic->netdev->name );
  790. spin_unlock_irqrestore(&nic->mdio_lock, flags);
  791. return 0; /* No way to indicate timeout error */
  792. }
  793. writel((reg << 16) | (addr << 21) | dir | data, &nic->csr->mdi_ctrl);
  794. for (i = 0; i < 100; i++) {
  795. udelay(20);
  796. if ((data_out = readl(&nic->csr->mdi_ctrl)) & mdi_ready)
  797. break;
  798. }
  799. spin_unlock_irqrestore(&nic->mdio_lock, flags);
  800. DPRINTK(HW, DEBUG,
  801. "%s:addr=%d, reg=%d, data_in=0x%04X, data_out=0x%04X\n",
  802. dir == mdi_read ? "READ" : "WRITE", addr, reg, data, data_out);
  803. return (u16)data_out;
  804. }
  805. static int mdio_read(struct net_device *netdev, int addr, int reg)
  806. {
  807. return mdio_ctrl(netdev_priv(netdev), addr, mdi_read, reg, 0);
  808. }
  809. static void mdio_write(struct net_device *netdev, int addr, int reg, int data)
  810. {
  811. mdio_ctrl(netdev_priv(netdev), addr, mdi_write, reg, data);
  812. }
  813. static void e100_get_defaults(struct nic *nic)
  814. {
  815. struct param_range rfds = { .min = 16, .max = 256, .count = 256 };
  816. struct param_range cbs = { .min = 64, .max = 256, .count = 128 };
  817. pci_read_config_byte(nic->pdev, PCI_REVISION_ID, &nic->rev_id);
  818. /* MAC type is encoded as rev ID; exception: ICH is treated as 82559 */
  819. nic->mac = (nic->flags & ich) ? mac_82559_D101M : nic->rev_id;
  820. if(nic->mac == mac_unknown)
  821. nic->mac = mac_82557_D100_A;
  822. nic->params.rfds = rfds;
  823. nic->params.cbs = cbs;
  824. /* Quadwords to DMA into FIFO before starting frame transmit */
  825. nic->tx_threshold = 0xE0;
  826. /* no interrupt for every tx completion, delay = 256us if not 557*/
  827. nic->tx_command = cpu_to_le16(cb_tx | cb_tx_sf |
  828. ((nic->mac >= mac_82558_D101_A4) ? cb_cid : cb_i));
  829. /* Template for a freshly allocated RFD */
  830. nic->blank_rfd.command = cpu_to_le16(cb_el);
  831. nic->blank_rfd.rbd = 0xFFFFFFFF;
  832. nic->blank_rfd.size = cpu_to_le16(VLAN_ETH_FRAME_LEN);
  833. /* MII setup */
  834. nic->mii.phy_id_mask = 0x1F;
  835. nic->mii.reg_num_mask = 0x1F;
  836. nic->mii.dev = nic->netdev;
  837. nic->mii.mdio_read = mdio_read;
  838. nic->mii.mdio_write = mdio_write;
  839. }
  840. static void e100_configure(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  841. {
  842. struct config *config = &cb->u.config;
  843. u8 *c = (u8 *)config;
  844. cb->command = cpu_to_le16(cb_config);
  845. memset(config, 0, sizeof(struct config));
  846. config->byte_count = 0x16; /* bytes in this struct */
  847. config->rx_fifo_limit = 0x8; /* bytes in FIFO before DMA */
  848. config->direct_rx_dma = 0x1; /* reserved */
  849. config->standard_tcb = 0x1; /* 1=standard, 0=extended */
  850. config->standard_stat_counter = 0x1; /* 1=standard, 0=extended */
  851. config->rx_discard_short_frames = 0x1; /* 1=discard, 0=pass */
  852. config->tx_underrun_retry = 0x3; /* # of underrun retries */
  853. config->mii_mode = 0x1; /* 1=MII mode, 0=503 mode */
  854. config->pad10 = 0x6;
  855. config->no_source_addr_insertion = 0x1; /* 1=no, 0=yes */
  856. config->preamble_length = 0x2; /* 0=1, 1=3, 2=7, 3=15 bytes */
  857. config->ifs = 0x6; /* x16 = inter frame spacing */
  858. config->ip_addr_hi = 0xF2; /* ARP IP filter - not used */
  859. config->pad15_1 = 0x1;
  860. config->pad15_2 = 0x1;
  861. config->crs_or_cdt = 0x0; /* 0=CRS only, 1=CRS or CDT */
  862. config->fc_delay_hi = 0x40; /* time delay for fc frame */
  863. config->tx_padding = 0x1; /* 1=pad short frames */
  864. config->fc_priority_threshold = 0x7; /* 7=priority fc disabled */
  865. config->pad18 = 0x1;
  866. config->full_duplex_pin = 0x1; /* 1=examine FDX# pin */
  867. config->pad20_1 = 0x1F;
  868. config->fc_priority_location = 0x1; /* 1=byte#31, 0=byte#19 */
  869. config->pad21_1 = 0x5;
  870. config->adaptive_ifs = nic->adaptive_ifs;
  871. config->loopback = nic->loopback;
  872. if(nic->mii.force_media && nic->mii.full_duplex)
  873. config->full_duplex_force = 0x1; /* 1=force, 0=auto */
  874. if(nic->flags & promiscuous || nic->loopback) {
  875. config->rx_save_bad_frames = 0x1; /* 1=save, 0=discard */
  876. config->rx_discard_short_frames = 0x0; /* 1=discard, 0=save */
  877. config->promiscuous_mode = 0x1; /* 1=on, 0=off */
  878. }
  879. if(nic->flags & multicast_all)
  880. config->multicast_all = 0x1; /* 1=accept, 0=no */
  881. /* disable WoL when up */
  882. if(netif_running(nic->netdev) || !(nic->flags & wol_magic))
  883. config->magic_packet_disable = 0x1; /* 1=off, 0=on */
  884. if(nic->mac >= mac_82558_D101_A4) {
  885. config->fc_disable = 0x1; /* 1=Tx fc off, 0=Tx fc on */
  886. config->mwi_enable = 0x1; /* 1=enable, 0=disable */
  887. config->standard_tcb = 0x0; /* 1=standard, 0=extended */
  888. config->rx_long_ok = 0x1; /* 1=VLANs ok, 0=standard */
  889. if(nic->mac >= mac_82559_D101M)
  890. config->tno_intr = 0x1; /* TCO stats enable */
  891. else
  892. config->standard_stat_counter = 0x0;
  893. }
  894. DPRINTK(HW, DEBUG, "[00-07]=%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
  895. c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7]);
  896. DPRINTK(HW, DEBUG, "[08-15]=%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
  897. c[8], c[9], c[10], c[11], c[12], c[13], c[14], c[15]);
  898. DPRINTK(HW, DEBUG, "[16-23]=%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
  899. c[16], c[17], c[18], c[19], c[20], c[21], c[22], c[23]);
  900. }
  901. /********************************************************/
  902. /* Micro code for 8086:1229 Rev 8 */
  903. /********************************************************/
  904. /* Parameter values for the D101M B-step */
  905. #define D101M_CPUSAVER_TIMER_DWORD 78
  906. #define D101M_CPUSAVER_BUNDLE_DWORD 65
  907. #define D101M_CPUSAVER_MIN_SIZE_DWORD 126
  908. #define D101M_B_RCVBUNDLE_UCODE \
  909. {\
  910. 0x00550215, 0xFFFF0437, 0xFFFFFFFF, 0x06A70789, 0xFFFFFFFF, 0x0558FFFF, \
  911. 0x000C0001, 0x00101312, 0x000C0008, 0x00380216, \
  912. 0x0010009C, 0x00204056, 0x002380CC, 0x00380056, \
  913. 0x0010009C, 0x00244C0B, 0x00000800, 0x00124818, \
  914. 0x00380438, 0x00000000, 0x00140000, 0x00380555, \
  915. 0x00308000, 0x00100662, 0x00100561, 0x000E0408, \
  916. 0x00134861, 0x000C0002, 0x00103093, 0x00308000, \
  917. 0x00100624, 0x00100561, 0x000E0408, 0x00100861, \
  918. 0x000C007E, 0x00222C21, 0x000C0002, 0x00103093, \
  919. 0x00380C7A, 0x00080000, 0x00103090, 0x00380C7A, \
  920. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  921. 0x0010009C, 0x00244C2D, 0x00010004, 0x00041000, \
  922. 0x003A0437, 0x00044010, 0x0038078A, 0x00000000, \
  923. 0x00100099, 0x00206C7A, 0x0010009C, 0x00244C48, \
  924. 0x00130824, 0x000C0001, 0x00101213, 0x00260C75, \
  925. 0x00041000, 0x00010004, 0x00130826, 0x000C0006, \
  926. 0x002206A8, 0x0013C926, 0x00101313, 0x003806A8, \
  927. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  928. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  929. 0x00080600, 0x00101B10, 0x00050004, 0x00100826, \
  930. 0x00101210, 0x00380C34, 0x00000000, 0x00000000, \
  931. 0x0021155B, 0x00100099, 0x00206559, 0x0010009C, \
  932. 0x00244559, 0x00130836, 0x000C0000, 0x00220C62, \
  933. 0x000C0001, 0x00101B13, 0x00229C0E, 0x00210C0E, \
  934. 0x00226C0E, 0x00216C0E, 0x0022FC0E, 0x00215C0E, \
  935. 0x00214C0E, 0x00380555, 0x00010004, 0x00041000, \
  936. 0x00278C67, 0x00040800, 0x00018100, 0x003A0437, \
  937. 0x00130826, 0x000C0001, 0x00220559, 0x00101313, \
  938. 0x00380559, 0x00000000, 0x00000000, 0x00000000, \
  939. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  940. 0x00000000, 0x00130831, 0x0010090B, 0x00124813, \
  941. 0x000CFF80, 0x002606AB, 0x00041000, 0x00010004, \
  942. 0x003806A8, 0x00000000, 0x00000000, 0x00000000, \
  943. }
  944. /********************************************************/
  945. /* Micro code for 8086:1229 Rev 9 */
  946. /********************************************************/
  947. /* Parameter values for the D101S */
  948. #define D101S_CPUSAVER_TIMER_DWORD 78
  949. #define D101S_CPUSAVER_BUNDLE_DWORD 67
  950. #define D101S_CPUSAVER_MIN_SIZE_DWORD 128
  951. #define D101S_RCVBUNDLE_UCODE \
  952. {\
  953. 0x00550242, 0xFFFF047E, 0xFFFFFFFF, 0x06FF0818, 0xFFFFFFFF, 0x05A6FFFF, \
  954. 0x000C0001, 0x00101312, 0x000C0008, 0x00380243, \
  955. 0x0010009C, 0x00204056, 0x002380D0, 0x00380056, \
  956. 0x0010009C, 0x00244F8B, 0x00000800, 0x00124818, \
  957. 0x0038047F, 0x00000000, 0x00140000, 0x003805A3, \
  958. 0x00308000, 0x00100610, 0x00100561, 0x000E0408, \
  959. 0x00134861, 0x000C0002, 0x00103093, 0x00308000, \
  960. 0x00100624, 0x00100561, 0x000E0408, 0x00100861, \
  961. 0x000C007E, 0x00222FA1, 0x000C0002, 0x00103093, \
  962. 0x00380F90, 0x00080000, 0x00103090, 0x00380F90, \
  963. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  964. 0x0010009C, 0x00244FAD, 0x00010004, 0x00041000, \
  965. 0x003A047E, 0x00044010, 0x00380819, 0x00000000, \
  966. 0x00100099, 0x00206FFD, 0x0010009A, 0x0020AFFD, \
  967. 0x0010009C, 0x00244FC8, 0x00130824, 0x000C0001, \
  968. 0x00101213, 0x00260FF7, 0x00041000, 0x00010004, \
  969. 0x00130826, 0x000C0006, 0x00220700, 0x0013C926, \
  970. 0x00101313, 0x00380700, 0x00000000, 0x00000000, \
  971. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  972. 0x00080600, 0x00101B10, 0x00050004, 0x00100826, \
  973. 0x00101210, 0x00380FB6, 0x00000000, 0x00000000, \
  974. 0x002115A9, 0x00100099, 0x002065A7, 0x0010009A, \
  975. 0x0020A5A7, 0x0010009C, 0x002445A7, 0x00130836, \
  976. 0x000C0000, 0x00220FE4, 0x000C0001, 0x00101B13, \
  977. 0x00229F8E, 0x00210F8E, 0x00226F8E, 0x00216F8E, \
  978. 0x0022FF8E, 0x00215F8E, 0x00214F8E, 0x003805A3, \
  979. 0x00010004, 0x00041000, 0x00278FE9, 0x00040800, \
  980. 0x00018100, 0x003A047E, 0x00130826, 0x000C0001, \
  981. 0x002205A7, 0x00101313, 0x003805A7, 0x00000000, \
  982. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  983. 0x00000000, 0x00000000, 0x00000000, 0x00130831, \
  984. 0x0010090B, 0x00124813, 0x000CFF80, 0x00260703, \
  985. 0x00041000, 0x00010004, 0x00380700 \
  986. }
  987. /********************************************************/
  988. /* Micro code for the 8086:1229 Rev F/10 */
  989. /********************************************************/
  990. /* Parameter values for the D102 E-step */
  991. #define D102_E_CPUSAVER_TIMER_DWORD 42
  992. #define D102_E_CPUSAVER_BUNDLE_DWORD 54
  993. #define D102_E_CPUSAVER_MIN_SIZE_DWORD 46
  994. #define D102_E_RCVBUNDLE_UCODE \
  995. {\
  996. 0x007D028F, 0x0E4204F9, 0x14ED0C85, 0x14FA14E9, 0x0EF70E36, 0x1FFF1FFF, \
  997. 0x00E014B9, 0x00000000, 0x00000000, 0x00000000, \
  998. 0x00E014BD, 0x00000000, 0x00000000, 0x00000000, \
  999. 0x00E014D5, 0x00000000, 0x00000000, 0x00000000, \
  1000. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1001. 0x00E014C1, 0x00000000, 0x00000000, 0x00000000, \
  1002. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1003. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1004. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1005. 0x00E014C8, 0x00000000, 0x00000000, 0x00000000, \
  1006. 0x00200600, 0x00E014EE, 0x00000000, 0x00000000, \
  1007. 0x0030FF80, 0x00940E46, 0x00038200, 0x00102000, \
  1008. 0x00E00E43, 0x00000000, 0x00000000, 0x00000000, \
  1009. 0x00300006, 0x00E014FB, 0x00000000, 0x00000000, \
  1010. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1011. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1012. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1013. 0x00906E41, 0x00800E3C, 0x00E00E39, 0x00000000, \
  1014. 0x00906EFD, 0x00900EFD, 0x00E00EF8, 0x00000000, \
  1015. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1016. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1017. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1018. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1019. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1020. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1021. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1022. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1023. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1024. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1025. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1026. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1027. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1028. 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
  1029. }
  1030. static void e100_setup_ucode(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  1031. {
  1032. /* *INDENT-OFF* */
  1033. static struct {
  1034. u32 ucode[UCODE_SIZE + 1];
  1035. u8 mac;
  1036. u8 timer_dword;
  1037. u8 bundle_dword;
  1038. u8 min_size_dword;
  1039. } ucode_opts[] = {
  1040. { D101M_B_RCVBUNDLE_UCODE,
  1041. mac_82559_D101M,
  1042. D101M_CPUSAVER_TIMER_DWORD,
  1043. D101M_CPUSAVER_BUNDLE_DWORD,
  1044. D101M_CPUSAVER_MIN_SIZE_DWORD },
  1045. { D101S_RCVBUNDLE_UCODE,
  1046. mac_82559_D101S,
  1047. D101S_CPUSAVER_TIMER_DWORD,
  1048. D101S_CPUSAVER_BUNDLE_DWORD,
  1049. D101S_CPUSAVER_MIN_SIZE_DWORD },
  1050. { D102_E_RCVBUNDLE_UCODE,
  1051. mac_82551_F,
  1052. D102_E_CPUSAVER_TIMER_DWORD,
  1053. D102_E_CPUSAVER_BUNDLE_DWORD,
  1054. D102_E_CPUSAVER_MIN_SIZE_DWORD },
  1055. { D102_E_RCVBUNDLE_UCODE,
  1056. mac_82551_10,
  1057. D102_E_CPUSAVER_TIMER_DWORD,
  1058. D102_E_CPUSAVER_BUNDLE_DWORD,
  1059. D102_E_CPUSAVER_MIN_SIZE_DWORD },
  1060. { {0}, 0, 0, 0, 0}
  1061. }, *opts;
  1062. /* *INDENT-ON* */
  1063. /*************************************************************************
  1064. * CPUSaver parameters
  1065. *
  1066. * All CPUSaver parameters are 16-bit literals that are part of a
  1067. * "move immediate value" instruction. By changing the value of
  1068. * the literal in the instruction before the code is loaded, the
  1069. * driver can change the algorithm.
  1070. *
  1071. * INTDELAY - This loads the dead-man timer with its inital value.
  1072. * When this timer expires the interrupt is asserted, and the
  1073. * timer is reset each time a new packet is received. (see
  1074. * BUNDLEMAX below to set the limit on number of chained packets)
  1075. * The current default is 0x600 or 1536. Experiments show that
  1076. * the value should probably stay within the 0x200 - 0x1000.
  1077. *
  1078. * BUNDLEMAX -
  1079. * This sets the maximum number of frames that will be bundled. In
  1080. * some situations, such as the TCP windowing algorithm, it may be
  1081. * better to limit the growth of the bundle size than let it go as
  1082. * high as it can, because that could cause too much added latency.
  1083. * The default is six, because this is the number of packets in the
  1084. * default TCP window size. A value of 1 would make CPUSaver indicate
  1085. * an interrupt for every frame received. If you do not want to put
  1086. * a limit on the bundle size, set this value to xFFFF.
  1087. *
  1088. * BUNDLESMALL -
  1089. * This contains a bit-mask describing the minimum size frame that
  1090. * will be bundled. The default masks the lower 7 bits, which means
  1091. * that any frame less than 128 bytes in length will not be bundled,
  1092. * but will instead immediately generate an interrupt. This does
  1093. * not affect the current bundle in any way. Any frame that is 128
  1094. * bytes or large will be bundled normally. This feature is meant
  1095. * to provide immediate indication of ACK frames in a TCP environment.
  1096. * Customers were seeing poor performance when a machine with CPUSaver
  1097. * enabled was sending but not receiving. The delay introduced when
  1098. * the ACKs were received was enough to reduce total throughput, because
  1099. * the sender would sit idle until the ACK was finally seen.
  1100. *
  1101. * The current default is 0xFF80, which masks out the lower 7 bits.
  1102. * This means that any frame which is x7F (127) bytes or smaller
  1103. * will cause an immediate interrupt. Because this value must be a
  1104. * bit mask, there are only a few valid values that can be used. To
  1105. * turn this feature off, the driver can write the value xFFFF to the
  1106. * lower word of this instruction (in the same way that the other
  1107. * parameters are used). Likewise, a value of 0xF800 (2047) would
  1108. * cause an interrupt to be generated for every frame, because all
  1109. * standard Ethernet frames are <= 2047 bytes in length.
  1110. *************************************************************************/
  1111. /* if you wish to disable the ucode functionality, while maintaining the
  1112. * workarounds it provides, set the following defines to:
  1113. * BUNDLESMALL 0
  1114. * BUNDLEMAX 1
  1115. * INTDELAY 1
  1116. */
  1117. #define BUNDLESMALL 1
  1118. #define BUNDLEMAX (u16)6
  1119. #define INTDELAY (u16)1536 /* 0x600 */
  1120. /* do not load u-code for ICH devices */
  1121. if (nic->flags & ich)
  1122. goto noloaducode;
  1123. /* Search for ucode match against h/w rev_id */
  1124. for (opts = ucode_opts; opts->mac; opts++) {
  1125. int i;
  1126. u32 *ucode = opts->ucode;
  1127. if (nic->mac != opts->mac)
  1128. continue;
  1129. /* Insert user-tunable settings */
  1130. ucode[opts->timer_dword] &= 0xFFFF0000;
  1131. ucode[opts->timer_dword] |= INTDELAY;
  1132. ucode[opts->bundle_dword] &= 0xFFFF0000;
  1133. ucode[opts->bundle_dword] |= BUNDLEMAX;
  1134. ucode[opts->min_size_dword] &= 0xFFFF0000;
  1135. ucode[opts->min_size_dword] |= (BUNDLESMALL) ? 0xFFFF : 0xFF80;
  1136. for (i = 0; i < UCODE_SIZE; i++)
  1137. cb->u.ucode[i] = cpu_to_le32(ucode[i]);
  1138. cb->command = cpu_to_le16(cb_ucode | cb_el);
  1139. return;
  1140. }
  1141. noloaducode:
  1142. cb->command = cpu_to_le16(cb_nop | cb_el);
  1143. }
  1144. static inline int e100_exec_cb_wait(struct nic *nic, struct sk_buff *skb,
  1145. void (*cb_prepare)(struct nic *, struct cb *, struct sk_buff *))
  1146. {
  1147. int err = 0, counter = 50;
  1148. struct cb *cb = nic->cb_to_clean;
  1149. if ((err = e100_exec_cb(nic, NULL, e100_setup_ucode)))
  1150. DPRINTK(PROBE,ERR, "ucode cmd failed with error %d\n", err);
  1151. /* must restart cuc */
  1152. nic->cuc_cmd = cuc_start;
  1153. /* wait for completion */
  1154. e100_write_flush(nic);
  1155. udelay(10);
  1156. /* wait for possibly (ouch) 500ms */
  1157. while (!(cb->status & cpu_to_le16(cb_complete))) {
  1158. msleep(10);
  1159. if (!--counter) break;
  1160. }
  1161. /* ack any interupts, something could have been set */
  1162. writeb(~0, &nic->csr->scb.stat_ack);
  1163. /* if the command failed, or is not OK, notify and return */
  1164. if (!counter || !(cb->status & cpu_to_le16(cb_ok))) {
  1165. DPRINTK(PROBE,ERR, "ucode load failed\n");
  1166. err = -EPERM;
  1167. }
  1168. return err;
  1169. }
  1170. static void e100_setup_iaaddr(struct nic *nic, struct cb *cb,
  1171. struct sk_buff *skb)
  1172. {
  1173. cb->command = cpu_to_le16(cb_iaaddr);
  1174. memcpy(cb->u.iaaddr, nic->netdev->dev_addr, ETH_ALEN);
  1175. }
  1176. static void e100_dump(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  1177. {
  1178. cb->command = cpu_to_le16(cb_dump);
  1179. cb->u.dump_buffer_addr = cpu_to_le32(nic->dma_addr +
  1180. offsetof(struct mem, dump_buf));
  1181. }
  1182. #define NCONFIG_AUTO_SWITCH 0x0080
  1183. #define MII_NSC_CONG MII_RESV1
  1184. #define NSC_CONG_ENABLE 0x0100
  1185. #define NSC_CONG_TXREADY 0x0400
  1186. #define ADVERTISE_FC_SUPPORTED 0x0400
  1187. static int e100_phy_init(struct nic *nic)
  1188. {
  1189. struct net_device *netdev = nic->netdev;
  1190. u32 addr;
  1191. u16 bmcr, stat, id_lo, id_hi, cong;
  1192. /* Discover phy addr by searching addrs in order {1,0,2,..., 31} */
  1193. for(addr = 0; addr < 32; addr++) {
  1194. nic->mii.phy_id = (addr == 0) ? 1 : (addr == 1) ? 0 : addr;
  1195. bmcr = mdio_read(netdev, nic->mii.phy_id, MII_BMCR);
  1196. stat = mdio_read(netdev, nic->mii.phy_id, MII_BMSR);
  1197. stat = mdio_read(netdev, nic->mii.phy_id, MII_BMSR);
  1198. if(!((bmcr == 0xFFFF) || ((stat == 0) && (bmcr == 0))))
  1199. break;
  1200. }
  1201. DPRINTK(HW, DEBUG, "phy_addr = %d\n", nic->mii.phy_id);
  1202. if(addr == 32)
  1203. return -EAGAIN;
  1204. /* Selected the phy and isolate the rest */
  1205. for(addr = 0; addr < 32; addr++) {
  1206. if(addr != nic->mii.phy_id) {
  1207. mdio_write(netdev, addr, MII_BMCR, BMCR_ISOLATE);
  1208. } else {
  1209. bmcr = mdio_read(netdev, addr, MII_BMCR);
  1210. mdio_write(netdev, addr, MII_BMCR,
  1211. bmcr & ~BMCR_ISOLATE);
  1212. }
  1213. }
  1214. /* Get phy ID */
  1215. id_lo = mdio_read(netdev, nic->mii.phy_id, MII_PHYSID1);
  1216. id_hi = mdio_read(netdev, nic->mii.phy_id, MII_PHYSID2);
  1217. nic->phy = (u32)id_hi << 16 | (u32)id_lo;
  1218. DPRINTK(HW, DEBUG, "phy ID = 0x%08X\n", nic->phy);
  1219. /* Handle National tx phys */
  1220. #define NCS_PHY_MODEL_MASK 0xFFF0FFFF
  1221. if((nic->phy & NCS_PHY_MODEL_MASK) == phy_nsc_tx) {
  1222. /* Disable congestion control */
  1223. cong = mdio_read(netdev, nic->mii.phy_id, MII_NSC_CONG);
  1224. cong |= NSC_CONG_TXREADY;
  1225. cong &= ~NSC_CONG_ENABLE;
  1226. mdio_write(netdev, nic->mii.phy_id, MII_NSC_CONG, cong);
  1227. }
  1228. if((nic->mac >= mac_82550_D102) || ((nic->flags & ich) &&
  1229. (mdio_read(netdev, nic->mii.phy_id, MII_TPISTATUS) & 0x8000) &&
  1230. !(nic->eeprom[eeprom_cnfg_mdix] & eeprom_mdix_enabled))) {
  1231. /* enable/disable MDI/MDI-X auto-switching. */
  1232. mdio_write(netdev, nic->mii.phy_id, MII_NCONFIG,
  1233. nic->mii.force_media ? 0 : NCONFIG_AUTO_SWITCH);
  1234. }
  1235. return 0;
  1236. }
  1237. static int e100_hw_init(struct nic *nic)
  1238. {
  1239. int err;
  1240. e100_hw_reset(nic);
  1241. DPRINTK(HW, ERR, "e100_hw_init\n");
  1242. if(!in_interrupt() && (err = e100_self_test(nic)))
  1243. return err;
  1244. if((err = e100_phy_init(nic)))
  1245. return err;
  1246. if((err = e100_exec_cmd(nic, cuc_load_base, 0)))
  1247. return err;
  1248. if((err = e100_exec_cmd(nic, ruc_load_base, 0)))
  1249. return err;
  1250. if ((err = e100_exec_cb_wait(nic, NULL, e100_setup_ucode)))
  1251. return err;
  1252. if((err = e100_exec_cb(nic, NULL, e100_configure)))
  1253. return err;
  1254. if((err = e100_exec_cb(nic, NULL, e100_setup_iaaddr)))
  1255. return err;
  1256. if((err = e100_exec_cmd(nic, cuc_dump_addr,
  1257. nic->dma_addr + offsetof(struct mem, stats))))
  1258. return err;
  1259. if((err = e100_exec_cmd(nic, cuc_dump_reset, 0)))
  1260. return err;
  1261. e100_disable_irq(nic);
  1262. return 0;
  1263. }
  1264. static void e100_multi(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  1265. {
  1266. struct net_device *netdev = nic->netdev;
  1267. struct dev_mc_list *list = netdev->mc_list;
  1268. u16 i, count = min(netdev->mc_count, E100_MAX_MULTICAST_ADDRS);
  1269. cb->command = cpu_to_le16(cb_multi);
  1270. cb->u.multi.count = cpu_to_le16(count * ETH_ALEN);
  1271. for(i = 0; list && i < count; i++, list = list->next)
  1272. memcpy(&cb->u.multi.addr[i*ETH_ALEN], &list->dmi_addr,
  1273. ETH_ALEN);
  1274. }
  1275. static void e100_set_multicast_list(struct net_device *netdev)
  1276. {
  1277. struct nic *nic = netdev_priv(netdev);
  1278. DPRINTK(HW, DEBUG, "mc_count=%d, flags=0x%04X\n",
  1279. netdev->mc_count, netdev->flags);
  1280. if(netdev->flags & IFF_PROMISC)
  1281. nic->flags |= promiscuous;
  1282. else
  1283. nic->flags &= ~promiscuous;
  1284. if(netdev->flags & IFF_ALLMULTI ||
  1285. netdev->mc_count > E100_MAX_MULTICAST_ADDRS)
  1286. nic->flags |= multicast_all;
  1287. else
  1288. nic->flags &= ~multicast_all;
  1289. e100_exec_cb(nic, NULL, e100_configure);
  1290. e100_exec_cb(nic, NULL, e100_multi);
  1291. }
  1292. static void e100_update_stats(struct nic *nic)
  1293. {
  1294. struct net_device_stats *ns = &nic->net_stats;
  1295. struct stats *s = &nic->mem->stats;
  1296. u32 *complete = (nic->mac < mac_82558_D101_A4) ? &s->fc_xmt_pause :
  1297. (nic->mac < mac_82559_D101M) ? (u32 *)&s->xmt_tco_frames :
  1298. &s->complete;
  1299. /* Device's stats reporting may take several microseconds to
  1300. * complete, so where always waiting for results of the
  1301. * previous command. */
  1302. if(*complete == le32_to_cpu(cuc_dump_reset_complete)) {
  1303. *complete = 0;
  1304. nic->tx_frames = le32_to_cpu(s->tx_good_frames);
  1305. nic->tx_collisions = le32_to_cpu(s->tx_total_collisions);
  1306. ns->tx_aborted_errors += le32_to_cpu(s->tx_max_collisions);
  1307. ns->tx_window_errors += le32_to_cpu(s->tx_late_collisions);
  1308. ns->tx_carrier_errors += le32_to_cpu(s->tx_lost_crs);
  1309. ns->tx_fifo_errors += le32_to_cpu(s->tx_underruns);
  1310. ns->collisions += nic->tx_collisions;
  1311. ns->tx_errors += le32_to_cpu(s->tx_max_collisions) +
  1312. le32_to_cpu(s->tx_lost_crs);
  1313. ns->rx_length_errors += le32_to_cpu(s->rx_short_frame_errors) +
  1314. nic->rx_over_length_errors;
  1315. ns->rx_crc_errors += le32_to_cpu(s->rx_crc_errors);
  1316. ns->rx_frame_errors += le32_to_cpu(s->rx_alignment_errors);
  1317. ns->rx_over_errors += le32_to_cpu(s->rx_overrun_errors);
  1318. ns->rx_fifo_errors += le32_to_cpu(s->rx_overrun_errors);
  1319. ns->rx_missed_errors += le32_to_cpu(s->rx_resource_errors);
  1320. ns->rx_errors += le32_to_cpu(s->rx_crc_errors) +
  1321. le32_to_cpu(s->rx_alignment_errors) +
  1322. le32_to_cpu(s->rx_short_frame_errors) +
  1323. le32_to_cpu(s->rx_cdt_errors);
  1324. nic->tx_deferred += le32_to_cpu(s->tx_deferred);
  1325. nic->tx_single_collisions +=
  1326. le32_to_cpu(s->tx_single_collisions);
  1327. nic->tx_multiple_collisions +=
  1328. le32_to_cpu(s->tx_multiple_collisions);
  1329. if(nic->mac >= mac_82558_D101_A4) {
  1330. nic->tx_fc_pause += le32_to_cpu(s->fc_xmt_pause);
  1331. nic->rx_fc_pause += le32_to_cpu(s->fc_rcv_pause);
  1332. nic->rx_fc_unsupported +=
  1333. le32_to_cpu(s->fc_rcv_unsupported);
  1334. if(nic->mac >= mac_82559_D101M) {
  1335. nic->tx_tco_frames +=
  1336. le16_to_cpu(s->xmt_tco_frames);
  1337. nic->rx_tco_frames +=
  1338. le16_to_cpu(s->rcv_tco_frames);
  1339. }
  1340. }
  1341. }
  1342. if(e100_exec_cmd(nic, cuc_dump_reset, 0))
  1343. DPRINTK(TX_ERR, DEBUG, "exec cuc_dump_reset failed\n");
  1344. }
  1345. static void e100_adjust_adaptive_ifs(struct nic *nic, int speed, int duplex)
  1346. {
  1347. /* Adjust inter-frame-spacing (IFS) between two transmits if
  1348. * we're getting collisions on a half-duplex connection. */
  1349. if(duplex == DUPLEX_HALF) {
  1350. u32 prev = nic->adaptive_ifs;
  1351. u32 min_frames = (speed == SPEED_100) ? 1000 : 100;
  1352. if((nic->tx_frames / 32 < nic->tx_collisions) &&
  1353. (nic->tx_frames > min_frames)) {
  1354. if(nic->adaptive_ifs < 60)
  1355. nic->adaptive_ifs += 5;
  1356. } else if (nic->tx_frames < min_frames) {
  1357. if(nic->adaptive_ifs >= 5)
  1358. nic->adaptive_ifs -= 5;
  1359. }
  1360. if(nic->adaptive_ifs != prev)
  1361. e100_exec_cb(nic, NULL, e100_configure);
  1362. }
  1363. }
  1364. static void e100_watchdog(unsigned long data)
  1365. {
  1366. struct nic *nic = (struct nic *)data;
  1367. struct ethtool_cmd cmd;
  1368. DPRINTK(TIMER, DEBUG, "right now = %ld\n", jiffies);
  1369. /* mii library handles link maintenance tasks */
  1370. mii_ethtool_gset(&nic->mii, &cmd);
  1371. if(mii_link_ok(&nic->mii) && !netif_carrier_ok(nic->netdev)) {
  1372. DPRINTK(LINK, INFO, "link up, %sMbps, %s-duplex\n",
  1373. cmd.speed == SPEED_100 ? "100" : "10",
  1374. cmd.duplex == DUPLEX_FULL ? "full" : "half");
  1375. } else if(!mii_link_ok(&nic->mii) && netif_carrier_ok(nic->netdev)) {
  1376. DPRINTK(LINK, INFO, "link down\n");
  1377. }
  1378. mii_check_link(&nic->mii);
  1379. /* Software generated interrupt to recover from (rare) Rx
  1380. * allocation failure.
  1381. * Unfortunately have to use a spinlock to not re-enable interrupts
  1382. * accidentally, due to hardware that shares a register between the
  1383. * interrupt mask bit and the SW Interrupt generation bit */
  1384. spin_lock_irq(&nic->cmd_lock);
  1385. writeb(readb(&nic->csr->scb.cmd_hi) | irq_sw_gen,&nic->csr->scb.cmd_hi);
  1386. e100_write_flush(nic);
  1387. spin_unlock_irq(&nic->cmd_lock);
  1388. e100_update_stats(nic);
  1389. e100_adjust_adaptive_ifs(nic, cmd.speed, cmd.duplex);
  1390. if(nic->mac <= mac_82557_D100_C)
  1391. /* Issue a multicast command to workaround a 557 lock up */
  1392. e100_set_multicast_list(nic->netdev);
  1393. if(nic->flags & ich && cmd.speed==SPEED_10 && cmd.duplex==DUPLEX_HALF)
  1394. /* Need SW workaround for ICH[x] 10Mbps/half duplex Tx hang. */
  1395. nic->flags |= ich_10h_workaround;
  1396. else
  1397. nic->flags &= ~ich_10h_workaround;
  1398. mod_timer(&nic->watchdog, jiffies + E100_WATCHDOG_PERIOD);
  1399. }
  1400. static void e100_xmit_prepare(struct nic *nic, struct cb *cb,
  1401. struct sk_buff *skb)
  1402. {
  1403. cb->command = nic->tx_command;
  1404. /* interrupt every 16 packets regardless of delay */
  1405. if((nic->cbs_avail & ~15) == nic->cbs_avail)
  1406. cb->command |= cpu_to_le16(cb_i);
  1407. cb->u.tcb.tbd_array = cb->dma_addr + offsetof(struct cb, u.tcb.tbd);
  1408. cb->u.tcb.tcb_byte_count = 0;
  1409. cb->u.tcb.threshold = nic->tx_threshold;
  1410. cb->u.tcb.tbd_count = 1;
  1411. cb->u.tcb.tbd.buf_addr = cpu_to_le32(pci_map_single(nic->pdev,
  1412. skb->data, skb->len, PCI_DMA_TODEVICE));
  1413. /* check for mapping failure? */
  1414. cb->u.tcb.tbd.size = cpu_to_le16(skb->len);
  1415. }
  1416. static int e100_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  1417. {
  1418. struct nic *nic = netdev_priv(netdev);
  1419. int err;
  1420. if(nic->flags & ich_10h_workaround) {
  1421. /* SW workaround for ICH[x] 10Mbps/half duplex Tx hang.
  1422. Issue a NOP command followed by a 1us delay before
  1423. issuing the Tx command. */
  1424. if(e100_exec_cmd(nic, cuc_nop, 0))
  1425. DPRINTK(TX_ERR, DEBUG, "exec cuc_nop failed\n");
  1426. udelay(1);
  1427. }
  1428. err = e100_exec_cb(nic, skb, e100_xmit_prepare);
  1429. switch(err) {
  1430. case -ENOSPC:
  1431. /* We queued the skb, but now we're out of space. */
  1432. DPRINTK(TX_ERR, DEBUG, "No space for CB\n");
  1433. netif_stop_queue(netdev);
  1434. break;
  1435. case -ENOMEM:
  1436. /* This is a hard error - log it. */
  1437. DPRINTK(TX_ERR, DEBUG, "Out of Tx resources, returning skb\n");
  1438. netif_stop_queue(netdev);
  1439. return 1;
  1440. }
  1441. netdev->trans_start = jiffies;
  1442. return 0;
  1443. }
  1444. static int e100_tx_clean(struct nic *nic)
  1445. {
  1446. struct cb *cb;
  1447. int tx_cleaned = 0;
  1448. spin_lock(&nic->cb_lock);
  1449. DPRINTK(TX_DONE, DEBUG, "cb->status = 0x%04X\n",
  1450. nic->cb_to_clean->status);
  1451. /* Clean CBs marked complete */
  1452. for(cb = nic->cb_to_clean;
  1453. cb->status & cpu_to_le16(cb_complete);
  1454. cb = nic->cb_to_clean = cb->next) {
  1455. if(likely(cb->skb != NULL)) {
  1456. nic->net_stats.tx_packets++;
  1457. nic->net_stats.tx_bytes += cb->skb->len;
  1458. pci_unmap_single(nic->pdev,
  1459. le32_to_cpu(cb->u.tcb.tbd.buf_addr),
  1460. le16_to_cpu(cb->u.tcb.tbd.size),
  1461. PCI_DMA_TODEVICE);
  1462. dev_kfree_skb_any(cb->skb);
  1463. cb->skb = NULL;
  1464. tx_cleaned = 1;
  1465. }
  1466. cb->status = 0;
  1467. nic->cbs_avail++;
  1468. }
  1469. spin_unlock(&nic->cb_lock);
  1470. /* Recover from running out of Tx resources in xmit_frame */
  1471. if(unlikely(tx_cleaned && netif_queue_stopped(nic->netdev)))
  1472. netif_wake_queue(nic->netdev);
  1473. return tx_cleaned;
  1474. }
  1475. static void e100_clean_cbs(struct nic *nic)
  1476. {
  1477. if(nic->cbs) {
  1478. while(nic->cbs_avail != nic->params.cbs.count) {
  1479. struct cb *cb = nic->cb_to_clean;
  1480. if(cb->skb) {
  1481. pci_unmap_single(nic->pdev,
  1482. le32_to_cpu(cb->u.tcb.tbd.buf_addr),
  1483. le16_to_cpu(cb->u.tcb.tbd.size),
  1484. PCI_DMA_TODEVICE);
  1485. dev_kfree_skb(cb->skb);
  1486. }
  1487. nic->cb_to_clean = nic->cb_to_clean->next;
  1488. nic->cbs_avail++;
  1489. }
  1490. pci_free_consistent(nic->pdev,
  1491. sizeof(struct cb) * nic->params.cbs.count,
  1492. nic->cbs, nic->cbs_dma_addr);
  1493. nic->cbs = NULL;
  1494. nic->cbs_avail = 0;
  1495. }
  1496. nic->cuc_cmd = cuc_start;
  1497. nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean =
  1498. nic->cbs;
  1499. }
  1500. static int e100_alloc_cbs(struct nic *nic)
  1501. {
  1502. struct cb *cb;
  1503. unsigned int i, count = nic->params.cbs.count;
  1504. nic->cuc_cmd = cuc_start;
  1505. nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean = NULL;
  1506. nic->cbs_avail = 0;
  1507. nic->cbs = pci_alloc_consistent(nic->pdev,
  1508. sizeof(struct cb) * count, &nic->cbs_dma_addr);
  1509. if(!nic->cbs)
  1510. return -ENOMEM;
  1511. for(cb = nic->cbs, i = 0; i < count; cb++, i++) {
  1512. cb->next = (i + 1 < count) ? cb + 1 : nic->cbs;
  1513. cb->prev = (i == 0) ? nic->cbs + count - 1 : cb - 1;
  1514. cb->dma_addr = nic->cbs_dma_addr + i * sizeof(struct cb);
  1515. cb->link = cpu_to_le32(nic->cbs_dma_addr +
  1516. ((i+1) % count) * sizeof(struct cb));
  1517. cb->skb = NULL;
  1518. }
  1519. nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean = nic->cbs;
  1520. nic->cbs_avail = count;
  1521. return 0;
  1522. }
  1523. static inline void e100_start_receiver(struct nic *nic, struct rx *rx)
  1524. {
  1525. if(!nic->rxs) return;
  1526. if(RU_SUSPENDED != nic->ru_running) return;
  1527. /* handle init time starts */
  1528. if(!rx) rx = nic->rxs;
  1529. /* (Re)start RU if suspended or idle and RFA is non-NULL */
  1530. if(rx->skb) {
  1531. e100_exec_cmd(nic, ruc_start, rx->dma_addr);
  1532. nic->ru_running = RU_RUNNING;
  1533. }
  1534. }
  1535. #define RFD_BUF_LEN (sizeof(struct rfd) + VLAN_ETH_FRAME_LEN)
  1536. static int e100_rx_alloc_skb(struct nic *nic, struct rx *rx)
  1537. {
  1538. if(!(rx->skb = netdev_alloc_skb(nic->netdev, RFD_BUF_LEN + NET_IP_ALIGN)))
  1539. return -ENOMEM;
  1540. /* Align, init, and map the RFD. */
  1541. skb_reserve(rx->skb, NET_IP_ALIGN);
  1542. memcpy(rx->skb->data, &nic->blank_rfd, sizeof(struct rfd));
  1543. rx->dma_addr = pci_map_single(nic->pdev, rx->skb->data,
  1544. RFD_BUF_LEN, PCI_DMA_BIDIRECTIONAL);
  1545. if(pci_dma_mapping_error(rx->dma_addr)) {
  1546. dev_kfree_skb_any(rx->skb);
  1547. rx->skb = NULL;
  1548. rx->dma_addr = 0;
  1549. return -ENOMEM;
  1550. }
  1551. /* Link the RFD to end of RFA by linking previous RFD to
  1552. * this one, and clearing EL bit of previous. */
  1553. if(rx->prev->skb) {
  1554. struct rfd *prev_rfd = (struct rfd *)rx->prev->skb->data;
  1555. put_unaligned(cpu_to_le32(rx->dma_addr),
  1556. (u32 *)&prev_rfd->link);
  1557. wmb();
  1558. prev_rfd->command &= ~cpu_to_le16(cb_el);
  1559. pci_dma_sync_single_for_device(nic->pdev, rx->prev->dma_addr,
  1560. sizeof(struct rfd), PCI_DMA_TODEVICE);
  1561. }
  1562. return 0;
  1563. }
  1564. static int e100_rx_indicate(struct nic *nic, struct rx *rx,
  1565. unsigned int *work_done, unsigned int work_to_do)
  1566. {
  1567. struct sk_buff *skb = rx->skb;
  1568. struct rfd *rfd = (struct rfd *)skb->data;
  1569. u16 rfd_status, actual_size;
  1570. if(unlikely(work_done && *work_done >= work_to_do))
  1571. return -EAGAIN;
  1572. /* Need to sync before taking a peek at cb_complete bit */
  1573. pci_dma_sync_single_for_cpu(nic->pdev, rx->dma_addr,
  1574. sizeof(struct rfd), PCI_DMA_FROMDEVICE);
  1575. rfd_status = le16_to_cpu(rfd->status);
  1576. DPRINTK(RX_STATUS, DEBUG, "status=0x%04X\n", rfd_status);
  1577. /* If data isn't ready, nothing to indicate */
  1578. if(unlikely(!(rfd_status & cb_complete)))
  1579. return -ENODATA;
  1580. /* Get actual data size */
  1581. actual_size = le16_to_cpu(rfd->actual_size) & 0x3FFF;
  1582. if(unlikely(actual_size > RFD_BUF_LEN - sizeof(struct rfd)))
  1583. actual_size = RFD_BUF_LEN - sizeof(struct rfd);
  1584. /* Get data */
  1585. pci_unmap_single(nic->pdev, rx->dma_addr,
  1586. RFD_BUF_LEN, PCI_DMA_FROMDEVICE);
  1587. /* this allows for a fast restart without re-enabling interrupts */
  1588. if(le16_to_cpu(rfd->command) & cb_el)
  1589. nic->ru_running = RU_SUSPENDED;
  1590. /* Pull off the RFD and put the actual data (minus eth hdr) */
  1591. skb_reserve(skb, sizeof(struct rfd));
  1592. skb_put(skb, actual_size);
  1593. skb->protocol = eth_type_trans(skb, nic->netdev);
  1594. if(unlikely(!(rfd_status & cb_ok))) {
  1595. /* Don't indicate if hardware indicates errors */
  1596. dev_kfree_skb_any(skb);
  1597. } else if(actual_size > ETH_DATA_LEN + VLAN_ETH_HLEN) {
  1598. /* Don't indicate oversized frames */
  1599. nic->rx_over_length_errors++;
  1600. dev_kfree_skb_any(skb);
  1601. } else {
  1602. nic->net_stats.rx_packets++;
  1603. nic->net_stats.rx_bytes += actual_size;
  1604. nic->netdev->last_rx = jiffies;
  1605. netif_receive_skb(skb);
  1606. if(work_done)
  1607. (*work_done)++;
  1608. }
  1609. rx->skb = NULL;
  1610. return 0;
  1611. }
  1612. static void e100_rx_clean(struct nic *nic, unsigned int *work_done,
  1613. unsigned int work_to_do)
  1614. {
  1615. struct rx *rx;
  1616. int restart_required = 0;
  1617. struct rx *rx_to_start = NULL;
  1618. /* are we already rnr? then pay attention!!! this ensures that
  1619. * the state machine progression never allows a start with a
  1620. * partially cleaned list, avoiding a race between hardware
  1621. * and rx_to_clean when in NAPI mode */
  1622. if(RU_SUSPENDED == nic->ru_running)
  1623. restart_required = 1;
  1624. /* Indicate newly arrived packets */
  1625. for(rx = nic->rx_to_clean; rx->skb; rx = nic->rx_to_clean = rx->next) {
  1626. int err = e100_rx_indicate(nic, rx, work_done, work_to_do);
  1627. if(-EAGAIN == err) {
  1628. /* hit quota so have more work to do, restart once
  1629. * cleanup is complete */
  1630. restart_required = 0;
  1631. break;
  1632. } else if(-ENODATA == err)
  1633. break; /* No more to clean */
  1634. }
  1635. /* save our starting point as the place we'll restart the receiver */
  1636. if(restart_required)
  1637. rx_to_start = nic->rx_to_clean;
  1638. /* Alloc new skbs to refill list */
  1639. for(rx = nic->rx_to_use; !rx->skb; rx = nic->rx_to_use = rx->next) {
  1640. if(unlikely(e100_rx_alloc_skb(nic, rx)))
  1641. break; /* Better luck next time (see watchdog) */
  1642. }
  1643. if(restart_required) {
  1644. // ack the rnr?
  1645. writeb(stat_ack_rnr, &nic->csr->scb.stat_ack);
  1646. e100_start_receiver(nic, rx_to_start);
  1647. if(work_done)
  1648. (*work_done)++;
  1649. }
  1650. }
  1651. static void e100_rx_clean_list(struct nic *nic)
  1652. {
  1653. struct rx *rx;
  1654. unsigned int i, count = nic->params.rfds.count;
  1655. nic->ru_running = RU_UNINITIALIZED;
  1656. if(nic->rxs) {
  1657. for(rx = nic->rxs, i = 0; i < count; rx++, i++) {
  1658. if(rx->skb) {
  1659. pci_unmap_single(nic->pdev, rx->dma_addr,
  1660. RFD_BUF_LEN, PCI_DMA_FROMDEVICE);
  1661. dev_kfree_skb(rx->skb);
  1662. }
  1663. }
  1664. kfree(nic->rxs);
  1665. nic->rxs = NULL;
  1666. }
  1667. nic->rx_to_use = nic->rx_to_clean = NULL;
  1668. }
  1669. static int e100_rx_alloc_list(struct nic *nic)
  1670. {
  1671. struct rx *rx;
  1672. unsigned int i, count = nic->params.rfds.count;
  1673. nic->rx_to_use = nic->rx_to_clean = NULL;
  1674. nic->ru_running = RU_UNINITIALIZED;
  1675. if(!(nic->rxs = kmalloc(sizeof(struct rx) * count, GFP_ATOMIC)))
  1676. return -ENOMEM;
  1677. memset(nic->rxs, 0, sizeof(struct rx) * count);
  1678. for(rx = nic->rxs, i = 0; i < count; rx++, i++) {
  1679. rx->next = (i + 1 < count) ? rx + 1 : nic->rxs;
  1680. rx->prev = (i == 0) ? nic->rxs + count - 1 : rx - 1;
  1681. if(e100_rx_alloc_skb(nic, rx)) {
  1682. e100_rx_clean_list(nic);
  1683. return -ENOMEM;
  1684. }
  1685. }
  1686. nic->rx_to_use = nic->rx_to_clean = nic->rxs;
  1687. nic->ru_running = RU_SUSPENDED;
  1688. return 0;
  1689. }
  1690. static irqreturn_t e100_intr(int irq, void *dev_id, struct pt_regs *regs)
  1691. {
  1692. struct net_device *netdev = dev_id;
  1693. struct nic *nic = netdev_priv(netdev);
  1694. u8 stat_ack = readb(&nic->csr->scb.stat_ack);
  1695. DPRINTK(INTR, DEBUG, "stat_ack = 0x%02X\n", stat_ack);
  1696. if(stat_ack == stat_ack_not_ours || /* Not our interrupt */
  1697. stat_ack == stat_ack_not_present) /* Hardware is ejected */
  1698. return IRQ_NONE;
  1699. /* Ack interrupt(s) */
  1700. writeb(stat_ack, &nic->csr->scb.stat_ack);
  1701. /* We hit Receive No Resource (RNR); restart RU after cleaning */
  1702. if(stat_ack & stat_ack_rnr)
  1703. nic->ru_running = RU_SUSPENDED;
  1704. if(likely(netif_rx_schedule_prep(netdev))) {
  1705. e100_disable_irq(nic);
  1706. __netif_rx_schedule(netdev);
  1707. }
  1708. return IRQ_HANDLED;
  1709. }
  1710. static int e100_poll(struct net_device *netdev, int *budget)
  1711. {
  1712. struct nic *nic = netdev_priv(netdev);
  1713. unsigned int work_to_do = min(netdev->quota, *budget);
  1714. unsigned int work_done = 0;
  1715. int tx_cleaned;
  1716. e100_rx_clean(nic, &work_done, work_to_do);
  1717. tx_cleaned = e100_tx_clean(nic);
  1718. /* If no Rx and Tx cleanup work was done, exit polling mode. */
  1719. if((!tx_cleaned && (work_done == 0)) || !netif_running(netdev)) {
  1720. netif_rx_complete(netdev);
  1721. e100_enable_irq(nic);
  1722. return 0;
  1723. }
  1724. *budget -= work_done;
  1725. netdev->quota -= work_done;
  1726. return 1;
  1727. }
  1728. #ifdef CONFIG_NET_POLL_CONTROLLER
  1729. static void e100_netpoll(struct net_device *netdev)
  1730. {
  1731. struct nic *nic = netdev_priv(netdev);
  1732. e100_disable_irq(nic);
  1733. e100_intr(nic->pdev->irq, netdev, NULL);
  1734. e100_tx_clean(nic);
  1735. e100_enable_irq(nic);
  1736. }
  1737. #endif
  1738. static struct net_device_stats *e100_get_stats(struct net_device *netdev)
  1739. {
  1740. struct nic *nic = netdev_priv(netdev);
  1741. return &nic->net_stats;
  1742. }
  1743. static int e100_set_mac_address(struct net_device *netdev, void *p)
  1744. {
  1745. struct nic *nic = netdev_priv(netdev);
  1746. struct sockaddr *addr = p;
  1747. if (!is_valid_ether_addr(addr->sa_data))
  1748. return -EADDRNOTAVAIL;
  1749. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  1750. e100_exec_cb(nic, NULL, e100_setup_iaaddr);
  1751. return 0;
  1752. }
  1753. static int e100_change_mtu(struct net_device *netdev, int new_mtu)
  1754. {
  1755. if(new_mtu < ETH_ZLEN || new_mtu > ETH_DATA_LEN)
  1756. return -EINVAL;
  1757. netdev->mtu = new_mtu;
  1758. return 0;
  1759. }
  1760. #ifdef CONFIG_PM
  1761. static int e100_asf(struct nic *nic)
  1762. {
  1763. /* ASF can be enabled from eeprom */
  1764. return((nic->pdev->device >= 0x1050) && (nic->pdev->device <= 0x1057) &&
  1765. (nic->eeprom[eeprom_config_asf] & eeprom_asf) &&
  1766. !(nic->eeprom[eeprom_config_asf] & eeprom_gcl) &&
  1767. ((nic->eeprom[eeprom_smbus_addr] & 0xFF) != 0xFE));
  1768. }
  1769. #endif
  1770. static int e100_up(struct nic *nic)
  1771. {
  1772. int err;
  1773. if((err = e100_rx_alloc_list(nic)))
  1774. return err;
  1775. if((err = e100_alloc_cbs(nic)))
  1776. goto err_rx_clean_list;
  1777. if((err = e100_hw_init(nic)))
  1778. goto err_clean_cbs;
  1779. e100_set_multicast_list(nic->netdev);
  1780. e100_start_receiver(nic, NULL);
  1781. mod_timer(&nic->watchdog, jiffies);
  1782. if((err = request_irq(nic->pdev->irq, e100_intr, IRQF_SHARED,
  1783. nic->netdev->name, nic->netdev)))
  1784. goto err_no_irq;
  1785. netif_wake_queue(nic->netdev);
  1786. netif_poll_enable(nic->netdev);
  1787. /* enable ints _after_ enabling poll, preventing a race between
  1788. * disable ints+schedule */
  1789. e100_enable_irq(nic);
  1790. return 0;
  1791. err_no_irq:
  1792. del_timer_sync(&nic->watchdog);
  1793. err_clean_cbs:
  1794. e100_clean_cbs(nic);
  1795. err_rx_clean_list:
  1796. e100_rx_clean_list(nic);
  1797. return err;
  1798. }
  1799. static void e100_down(struct nic *nic)
  1800. {
  1801. /* wait here for poll to complete */
  1802. netif_poll_disable(nic->netdev);
  1803. netif_stop_queue(nic->netdev);
  1804. e100_hw_reset(nic);
  1805. free_irq(nic->pdev->irq, nic->netdev);
  1806. del_timer_sync(&nic->watchdog);
  1807. netif_carrier_off(nic->netdev);
  1808. e100_clean_cbs(nic);
  1809. e100_rx_clean_list(nic);
  1810. }
  1811. static void e100_tx_timeout(struct net_device *netdev)
  1812. {
  1813. struct nic *nic = netdev_priv(netdev);
  1814. /* Reset outside of interrupt context, to avoid request_irq
  1815. * in interrupt context */
  1816. schedule_work(&nic->tx_timeout_task);
  1817. }
  1818. static void e100_tx_timeout_task(struct net_device *netdev)
  1819. {
  1820. struct nic *nic = netdev_priv(netdev);
  1821. DPRINTK(TX_ERR, DEBUG, "scb.status=0x%02X\n",
  1822. readb(&nic->csr->scb.status));
  1823. e100_down(netdev_priv(netdev));
  1824. e100_up(netdev_priv(netdev));
  1825. }
  1826. static int e100_loopback_test(struct nic *nic, enum loopback loopback_mode)
  1827. {
  1828. int err;
  1829. struct sk_buff *skb;
  1830. /* Use driver resources to perform internal MAC or PHY
  1831. * loopback test. A single packet is prepared and transmitted
  1832. * in loopback mode, and the test passes if the received
  1833. * packet compares byte-for-byte to the transmitted packet. */
  1834. if((err = e100_rx_alloc_list(nic)))
  1835. return err;
  1836. if((err = e100_alloc_cbs(nic)))
  1837. goto err_clean_rx;
  1838. /* ICH PHY loopback is broken so do MAC loopback instead */
  1839. if(nic->flags & ich && loopback_mode == lb_phy)
  1840. loopback_mode = lb_mac;
  1841. nic->loopback = loopback_mode;
  1842. if((err = e100_hw_init(nic)))
  1843. goto err_loopback_none;
  1844. if(loopback_mode == lb_phy)
  1845. mdio_write(nic->netdev, nic->mii.phy_id, MII_BMCR,
  1846. BMCR_LOOPBACK);
  1847. e100_start_receiver(nic, NULL);
  1848. if(!(skb = netdev_alloc_skb(nic->netdev, ETH_DATA_LEN))) {
  1849. err = -ENOMEM;
  1850. goto err_loopback_none;
  1851. }
  1852. skb_put(skb, ETH_DATA_LEN);
  1853. memset(skb->data, 0xFF, ETH_DATA_LEN);
  1854. e100_xmit_frame(skb, nic->netdev);
  1855. msleep(10);
  1856. pci_dma_sync_single_for_cpu(nic->pdev, nic->rx_to_clean->dma_addr,
  1857. RFD_BUF_LEN, PCI_DMA_FROMDEVICE);
  1858. if(memcmp(nic->rx_to_clean->skb->data + sizeof(struct rfd),
  1859. skb->data, ETH_DATA_LEN))
  1860. err = -EAGAIN;
  1861. err_loopback_none:
  1862. mdio_write(nic->netdev, nic->mii.phy_id, MII_BMCR, 0);
  1863. nic->loopback = lb_none;
  1864. e100_clean_cbs(nic);
  1865. e100_hw_reset(nic);
  1866. err_clean_rx:
  1867. e100_rx_clean_list(nic);
  1868. return err;
  1869. }
  1870. #define MII_LED_CONTROL 0x1B
  1871. static void e100_blink_led(unsigned long data)
  1872. {
  1873. struct nic *nic = (struct nic *)data;
  1874. enum led_state {
  1875. led_on = 0x01,
  1876. led_off = 0x04,
  1877. led_on_559 = 0x05,
  1878. led_on_557 = 0x07,
  1879. };
  1880. nic->leds = (nic->leds & led_on) ? led_off :
  1881. (nic->mac < mac_82559_D101M) ? led_on_557 : led_on_559;
  1882. mdio_write(nic->netdev, nic->mii.phy_id, MII_LED_CONTROL, nic->leds);
  1883. mod_timer(&nic->blink_timer, jiffies + HZ / 4);
  1884. }
  1885. static int e100_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
  1886. {
  1887. struct nic *nic = netdev_priv(netdev);
  1888. return mii_ethtool_gset(&nic->mii, cmd);
  1889. }
  1890. static int e100_set_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
  1891. {
  1892. struct nic *nic = netdev_priv(netdev);
  1893. int err;
  1894. mdio_write(netdev, nic->mii.phy_id, MII_BMCR, BMCR_RESET);
  1895. err = mii_ethtool_sset(&nic->mii, cmd);
  1896. e100_exec_cb(nic, NULL, e100_configure);
  1897. return err;
  1898. }
  1899. static void e100_get_drvinfo(struct net_device *netdev,
  1900. struct ethtool_drvinfo *info)
  1901. {
  1902. struct nic *nic = netdev_priv(netdev);
  1903. strcpy(info->driver, DRV_NAME);
  1904. strcpy(info->version, DRV_VERSION);
  1905. strcpy(info->fw_version, "N/A");
  1906. strcpy(info->bus_info, pci_name(nic->pdev));
  1907. }
  1908. static int e100_get_regs_len(struct net_device *netdev)
  1909. {
  1910. struct nic *nic = netdev_priv(netdev);
  1911. #define E100_PHY_REGS 0x1C
  1912. #define E100_REGS_LEN 1 + E100_PHY_REGS + \
  1913. sizeof(nic->mem->dump_buf) / sizeof(u32)
  1914. return E100_REGS_LEN * sizeof(u32);
  1915. }
  1916. static void e100_get_regs(struct net_device *netdev,
  1917. struct ethtool_regs *regs, void *p)
  1918. {
  1919. struct nic *nic = netdev_priv(netdev);
  1920. u32 *buff = p;
  1921. int i;
  1922. regs->version = (1 << 24) | nic->rev_id;
  1923. buff[0] = readb(&nic->csr->scb.cmd_hi) << 24 |
  1924. readb(&nic->csr->scb.cmd_lo) << 16 |
  1925. readw(&nic->csr->scb.status);
  1926. for(i = E100_PHY_REGS; i >= 0; i--)
  1927. buff[1 + E100_PHY_REGS - i] =
  1928. mdio_read(netdev, nic->mii.phy_id, i);
  1929. memset(nic->mem->dump_buf, 0, sizeof(nic->mem->dump_buf));
  1930. e100_exec_cb(nic, NULL, e100_dump);
  1931. msleep(10);
  1932. memcpy(&buff[2 + E100_PHY_REGS], nic->mem->dump_buf,
  1933. sizeof(nic->mem->dump_buf));
  1934. }
  1935. static void e100_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  1936. {
  1937. struct nic *nic = netdev_priv(netdev);
  1938. wol->supported = (nic->mac >= mac_82558_D101_A4) ? WAKE_MAGIC : 0;
  1939. wol->wolopts = (nic->flags & wol_magic) ? WAKE_MAGIC : 0;
  1940. }
  1941. static int e100_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  1942. {
  1943. struct nic *nic = netdev_priv(netdev);
  1944. if(wol->wolopts != WAKE_MAGIC && wol->wolopts != 0)
  1945. return -EOPNOTSUPP;
  1946. if(wol->wolopts)
  1947. nic->flags |= wol_magic;
  1948. else
  1949. nic->flags &= ~wol_magic;
  1950. e100_exec_cb(nic, NULL, e100_configure);
  1951. return 0;
  1952. }
  1953. static u32 e100_get_msglevel(struct net_device *netdev)
  1954. {
  1955. struct nic *nic = netdev_priv(netdev);
  1956. return nic->msg_enable;
  1957. }
  1958. static void e100_set_msglevel(struct net_device *netdev, u32 value)
  1959. {
  1960. struct nic *nic = netdev_priv(netdev);
  1961. nic->msg_enable = value;
  1962. }
  1963. static int e100_nway_reset(struct net_device *netdev)
  1964. {
  1965. struct nic *nic = netdev_priv(netdev);
  1966. return mii_nway_restart(&nic->mii);
  1967. }
  1968. static u32 e100_get_link(struct net_device *netdev)
  1969. {
  1970. struct nic *nic = netdev_priv(netdev);
  1971. return mii_link_ok(&nic->mii);
  1972. }
  1973. static int e100_get_eeprom_len(struct net_device *netdev)
  1974. {
  1975. struct nic *nic = netdev_priv(netdev);
  1976. return nic->eeprom_wc << 1;
  1977. }
  1978. #define E100_EEPROM_MAGIC 0x1234
  1979. static int e100_get_eeprom(struct net_device *netdev,
  1980. struct ethtool_eeprom *eeprom, u8 *bytes)
  1981. {
  1982. struct nic *nic = netdev_priv(netdev);
  1983. eeprom->magic = E100_EEPROM_MAGIC;
  1984. memcpy(bytes, &((u8 *)nic->eeprom)[eeprom->offset], eeprom->len);
  1985. return 0;
  1986. }
  1987. static int e100_set_eeprom(struct net_device *netdev,
  1988. struct ethtool_eeprom *eeprom, u8 *bytes)
  1989. {
  1990. struct nic *nic = netdev_priv(netdev);
  1991. if(eeprom->magic != E100_EEPROM_MAGIC)
  1992. return -EINVAL;
  1993. memcpy(&((u8 *)nic->eeprom)[eeprom->offset], bytes, eeprom->len);
  1994. return e100_eeprom_save(nic, eeprom->offset >> 1,
  1995. (eeprom->len >> 1) + 1);
  1996. }
  1997. static void e100_get_ringparam(struct net_device *netdev,
  1998. struct ethtool_ringparam *ring)
  1999. {
  2000. struct nic *nic = netdev_priv(netdev);
  2001. struct param_range *rfds = &nic->params.rfds;
  2002. struct param_range *cbs = &nic->params.cbs;
  2003. ring->rx_max_pending = rfds->max;
  2004. ring->tx_max_pending = cbs->max;
  2005. ring->rx_mini_max_pending = 0;
  2006. ring->rx_jumbo_max_pending = 0;
  2007. ring->rx_pending = rfds->count;
  2008. ring->tx_pending = cbs->count;
  2009. ring->rx_mini_pending = 0;
  2010. ring->rx_jumbo_pending = 0;
  2011. }
  2012. static int e100_set_ringparam(struct net_device *netdev,
  2013. struct ethtool_ringparam *ring)
  2014. {
  2015. struct nic *nic = netdev_priv(netdev);
  2016. struct param_range *rfds = &nic->params.rfds;
  2017. struct param_range *cbs = &nic->params.cbs;
  2018. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  2019. return -EINVAL;
  2020. if(netif_running(netdev))
  2021. e100_down(nic);
  2022. rfds->count = max(ring->rx_pending, rfds->min);
  2023. rfds->count = min(rfds->count, rfds->max);
  2024. cbs->count = max(ring->tx_pending, cbs->min);
  2025. cbs->count = min(cbs->count, cbs->max);
  2026. DPRINTK(DRV, INFO, "Ring Param settings: rx: %d, tx %d\n",
  2027. rfds->count, cbs->count);
  2028. if(netif_running(netdev))
  2029. e100_up(nic);
  2030. return 0;
  2031. }
  2032. static const char e100_gstrings_test[][ETH_GSTRING_LEN] = {
  2033. "Link test (on/offline)",
  2034. "Eeprom test (on/offline)",
  2035. "Self test (offline)",
  2036. "Mac loopback (offline)",
  2037. "Phy loopback (offline)",
  2038. };
  2039. #define E100_TEST_LEN sizeof(e100_gstrings_test) / ETH_GSTRING_LEN
  2040. static int e100_diag_test_count(struct net_device *netdev)
  2041. {
  2042. return E100_TEST_LEN;
  2043. }
  2044. static void e100_diag_test(struct net_device *netdev,
  2045. struct ethtool_test *test, u64 *data)
  2046. {
  2047. struct ethtool_cmd cmd;
  2048. struct nic *nic = netdev_priv(netdev);
  2049. int i, err;
  2050. memset(data, 0, E100_TEST_LEN * sizeof(u64));
  2051. data[0] = !mii_link_ok(&nic->mii);
  2052. data[1] = e100_eeprom_load(nic);
  2053. if(test->flags & ETH_TEST_FL_OFFLINE) {
  2054. /* save speed, duplex & autoneg settings */
  2055. err = mii_ethtool_gset(&nic->mii, &cmd);
  2056. if(netif_running(netdev))
  2057. e100_down(nic);
  2058. data[2] = e100_self_test(nic);
  2059. data[3] = e100_loopback_test(nic, lb_mac);
  2060. data[4] = e100_loopback_test(nic, lb_phy);
  2061. /* restore speed, duplex & autoneg settings */
  2062. err = mii_ethtool_sset(&nic->mii, &cmd);
  2063. if(netif_running(netdev))
  2064. e100_up(nic);
  2065. }
  2066. for(i = 0; i < E100_TEST_LEN; i++)
  2067. test->flags |= data[i] ? ETH_TEST_FL_FAILED : 0;
  2068. msleep_interruptible(4 * 1000);
  2069. }
  2070. static int e100_phys_id(struct net_device *netdev, u32 data)
  2071. {
  2072. struct nic *nic = netdev_priv(netdev);
  2073. if(!data || data > (u32)(MAX_SCHEDULE_TIMEOUT / HZ))
  2074. data = (u32)(MAX_SCHEDULE_TIMEOUT / HZ);
  2075. mod_timer(&nic->blink_timer, jiffies);
  2076. msleep_interruptible(data * 1000);
  2077. del_timer_sync(&nic->blink_timer);
  2078. mdio_write(netdev, nic->mii.phy_id, MII_LED_CONTROL, 0);
  2079. return 0;
  2080. }
  2081. static const char e100_gstrings_stats[][ETH_GSTRING_LEN] = {
  2082. "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
  2083. "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
  2084. "rx_length_errors", "rx_over_errors", "rx_crc_errors",
  2085. "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
  2086. "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
  2087. "tx_heartbeat_errors", "tx_window_errors",
  2088. /* device-specific stats */
  2089. "tx_deferred", "tx_single_collisions", "tx_multi_collisions",
  2090. "tx_flow_control_pause", "rx_flow_control_pause",
  2091. "rx_flow_control_unsupported", "tx_tco_packets", "rx_tco_packets",
  2092. };
  2093. #define E100_NET_STATS_LEN 21
  2094. #define E100_STATS_LEN sizeof(e100_gstrings_stats) / ETH_GSTRING_LEN
  2095. static int e100_get_stats_count(struct net_device *netdev)
  2096. {
  2097. return E100_STATS_LEN;
  2098. }
  2099. static void e100_get_ethtool_stats(struct net_device *netdev,
  2100. struct ethtool_stats *stats, u64 *data)
  2101. {
  2102. struct nic *nic = netdev_priv(netdev);
  2103. int i;
  2104. for(i = 0; i < E100_NET_STATS_LEN; i++)
  2105. data[i] = ((unsigned long *)&nic->net_stats)[i];
  2106. data[i++] = nic->tx_deferred;
  2107. data[i++] = nic->tx_single_collisions;
  2108. data[i++] = nic->tx_multiple_collisions;
  2109. data[i++] = nic->tx_fc_pause;
  2110. data[i++] = nic->rx_fc_pause;
  2111. data[i++] = nic->rx_fc_unsupported;
  2112. data[i++] = nic->tx_tco_frames;
  2113. data[i++] = nic->rx_tco_frames;
  2114. }
  2115. static void e100_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
  2116. {
  2117. switch(stringset) {
  2118. case ETH_SS_TEST:
  2119. memcpy(data, *e100_gstrings_test, sizeof(e100_gstrings_test));
  2120. break;
  2121. case ETH_SS_STATS:
  2122. memcpy(data, *e100_gstrings_stats, sizeof(e100_gstrings_stats));
  2123. break;
  2124. }
  2125. }
  2126. static const struct ethtool_ops e100_ethtool_ops = {
  2127. .get_settings = e100_get_settings,
  2128. .set_settings = e100_set_settings,
  2129. .get_drvinfo = e100_get_drvinfo,
  2130. .get_regs_len = e100_get_regs_len,
  2131. .get_regs = e100_get_regs,
  2132. .get_wol = e100_get_wol,
  2133. .set_wol = e100_set_wol,
  2134. .get_msglevel = e100_get_msglevel,
  2135. .set_msglevel = e100_set_msglevel,
  2136. .nway_reset = e100_nway_reset,
  2137. .get_link = e100_get_link,
  2138. .get_eeprom_len = e100_get_eeprom_len,
  2139. .get_eeprom = e100_get_eeprom,
  2140. .set_eeprom = e100_set_eeprom,
  2141. .get_ringparam = e100_get_ringparam,
  2142. .set_ringparam = e100_set_ringparam,
  2143. .self_test_count = e100_diag_test_count,
  2144. .self_test = e100_diag_test,
  2145. .get_strings = e100_get_strings,
  2146. .phys_id = e100_phys_id,
  2147. .get_stats_count = e100_get_stats_count,
  2148. .get_ethtool_stats = e100_get_ethtool_stats,
  2149. .get_perm_addr = ethtool_op_get_perm_addr,
  2150. };
  2151. static int e100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2152. {
  2153. struct nic *nic = netdev_priv(netdev);
  2154. return generic_mii_ioctl(&nic->mii, if_mii(ifr), cmd, NULL);
  2155. }
  2156. static int e100_alloc(struct nic *nic)
  2157. {
  2158. nic->mem = pci_alloc_consistent(nic->pdev, sizeof(struct mem),
  2159. &nic->dma_addr);
  2160. return nic->mem ? 0 : -ENOMEM;
  2161. }
  2162. static void e100_free(struct nic *nic)
  2163. {
  2164. if(nic->mem) {
  2165. pci_free_consistent(nic->pdev, sizeof(struct mem),
  2166. nic->mem, nic->dma_addr);
  2167. nic->mem = NULL;
  2168. }
  2169. }
  2170. static int e100_open(struct net_device *netdev)
  2171. {
  2172. struct nic *nic = netdev_priv(netdev);
  2173. int err = 0;
  2174. netif_carrier_off(netdev);
  2175. if((err = e100_up(nic)))
  2176. DPRINTK(IFUP, ERR, "Cannot open interface, aborting.\n");
  2177. return err;
  2178. }
  2179. static int e100_close(struct net_device *netdev)
  2180. {
  2181. e100_down(netdev_priv(netdev));
  2182. return 0;
  2183. }
  2184. static int __devinit e100_probe(struct pci_dev *pdev,
  2185. const struct pci_device_id *ent)
  2186. {
  2187. struct net_device *netdev;
  2188. struct nic *nic;
  2189. int err;
  2190. if(!(netdev = alloc_etherdev(sizeof(struct nic)))) {
  2191. if(((1 << debug) - 1) & NETIF_MSG_PROBE)
  2192. printk(KERN_ERR PFX "Etherdev alloc failed, abort.\n");
  2193. return -ENOMEM;
  2194. }
  2195. netdev->open = e100_open;
  2196. netdev->stop = e100_close;
  2197. netdev->hard_start_xmit = e100_xmit_frame;
  2198. netdev->get_stats = e100_get_stats;
  2199. netdev->set_multicast_list = e100_set_multicast_list;
  2200. netdev->set_mac_address = e100_set_mac_address;
  2201. netdev->change_mtu = e100_change_mtu;
  2202. netdev->do_ioctl = e100_do_ioctl;
  2203. SET_ETHTOOL_OPS(netdev, &e100_ethtool_ops);
  2204. netdev->tx_timeout = e100_tx_timeout;
  2205. netdev->watchdog_timeo = E100_WATCHDOG_PERIOD;
  2206. netdev->poll = e100_poll;
  2207. netdev->weight = E100_NAPI_WEIGHT;
  2208. #ifdef CONFIG_NET_POLL_CONTROLLER
  2209. netdev->poll_controller = e100_netpoll;
  2210. #endif
  2211. strcpy(netdev->name, pci_name(pdev));
  2212. nic = netdev_priv(netdev);
  2213. nic->netdev = netdev;
  2214. nic->pdev = pdev;
  2215. nic->msg_enable = (1 << debug) - 1;
  2216. pci_set_drvdata(pdev, netdev);
  2217. if((err = pci_enable_device(pdev))) {
  2218. DPRINTK(PROBE, ERR, "Cannot enable PCI device, aborting.\n");
  2219. goto err_out_free_dev;
  2220. }
  2221. if(!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  2222. DPRINTK(PROBE, ERR, "Cannot find proper PCI device "
  2223. "base address, aborting.\n");
  2224. err = -ENODEV;
  2225. goto err_out_disable_pdev;
  2226. }
  2227. if((err = pci_request_regions(pdev, DRV_NAME))) {
  2228. DPRINTK(PROBE, ERR, "Cannot obtain PCI resources, aborting.\n");
  2229. goto err_out_disable_pdev;
  2230. }
  2231. if((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK))) {
  2232. DPRINTK(PROBE, ERR, "No usable DMA configuration, aborting.\n");
  2233. goto err_out_free_res;
  2234. }
  2235. SET_MODULE_OWNER(netdev);
  2236. SET_NETDEV_DEV(netdev, &pdev->dev);
  2237. nic->csr = ioremap(pci_resource_start(pdev, 0), sizeof(struct csr));
  2238. if(!nic->csr) {
  2239. DPRINTK(PROBE, ERR, "Cannot map device registers, aborting.\n");
  2240. err = -ENOMEM;
  2241. goto err_out_free_res;
  2242. }
  2243. if(ent->driver_data)
  2244. nic->flags |= ich;
  2245. else
  2246. nic->flags &= ~ich;
  2247. e100_get_defaults(nic);
  2248. /* locks must be initialized before calling hw_reset */
  2249. spin_lock_init(&nic->cb_lock);
  2250. spin_lock_init(&nic->cmd_lock);
  2251. spin_lock_init(&nic->mdio_lock);
  2252. /* Reset the device before pci_set_master() in case device is in some
  2253. * funky state and has an interrupt pending - hint: we don't have the
  2254. * interrupt handler registered yet. */
  2255. e100_hw_reset(nic);
  2256. pci_set_master(pdev);
  2257. init_timer(&nic->watchdog);
  2258. nic->watchdog.function = e100_watchdog;
  2259. nic->watchdog.data = (unsigned long)nic;
  2260. init_timer(&nic->blink_timer);
  2261. nic->blink_timer.function = e100_blink_led;
  2262. nic->blink_timer.data = (unsigned long)nic;
  2263. INIT_WORK(&nic->tx_timeout_task,
  2264. (void (*)(void *))e100_tx_timeout_task, netdev);
  2265. if((err = e100_alloc(nic))) {
  2266. DPRINTK(PROBE, ERR, "Cannot alloc driver memory, aborting.\n");
  2267. goto err_out_iounmap;
  2268. }
  2269. if((err = e100_eeprom_load(nic)))
  2270. goto err_out_free;
  2271. e100_phy_init(nic);
  2272. memcpy(netdev->dev_addr, nic->eeprom, ETH_ALEN);
  2273. memcpy(netdev->perm_addr, nic->eeprom, ETH_ALEN);
  2274. if(!is_valid_ether_addr(netdev->perm_addr)) {
  2275. DPRINTK(PROBE, ERR, "Invalid MAC address from "
  2276. "EEPROM, aborting.\n");
  2277. err = -EAGAIN;
  2278. goto err_out_free;
  2279. }
  2280. /* Wol magic packet can be enabled from eeprom */
  2281. if((nic->mac >= mac_82558_D101_A4) &&
  2282. (nic->eeprom[eeprom_id] & eeprom_id_wol))
  2283. nic->flags |= wol_magic;
  2284. /* ack any pending wake events, disable PME */
  2285. err = pci_enable_wake(pdev, 0, 0);
  2286. if (err)
  2287. DPRINTK(PROBE, ERR, "Error clearing wake event\n");
  2288. strcpy(netdev->name, "eth%d");
  2289. if((err = register_netdev(netdev))) {
  2290. DPRINTK(PROBE, ERR, "Cannot register net device, aborting.\n");
  2291. goto err_out_free;
  2292. }
  2293. DPRINTK(PROBE, INFO, "addr 0x%llx, irq %d, "
  2294. "MAC addr %02X:%02X:%02X:%02X:%02X:%02X\n",
  2295. (unsigned long long)pci_resource_start(pdev, 0), pdev->irq,
  2296. netdev->dev_addr[0], netdev->dev_addr[1], netdev->dev_addr[2],
  2297. netdev->dev_addr[3], netdev->dev_addr[4], netdev->dev_addr[5]);
  2298. return 0;
  2299. err_out_free:
  2300. e100_free(nic);
  2301. err_out_iounmap:
  2302. iounmap(nic->csr);
  2303. err_out_free_res:
  2304. pci_release_regions(pdev);
  2305. err_out_disable_pdev:
  2306. pci_disable_device(pdev);
  2307. err_out_free_dev:
  2308. pci_set_drvdata(pdev, NULL);
  2309. free_netdev(netdev);
  2310. return err;
  2311. }
  2312. static void __devexit e100_remove(struct pci_dev *pdev)
  2313. {
  2314. struct net_device *netdev = pci_get_drvdata(pdev);
  2315. if(netdev) {
  2316. struct nic *nic = netdev_priv(netdev);
  2317. unregister_netdev(netdev);
  2318. e100_free(nic);
  2319. iounmap(nic->csr);
  2320. free_netdev(netdev);
  2321. pci_release_regions(pdev);
  2322. pci_disable_device(pdev);
  2323. pci_set_drvdata(pdev, NULL);
  2324. }
  2325. }
  2326. #ifdef CONFIG_PM
  2327. static int e100_suspend(struct pci_dev *pdev, pm_message_t state)
  2328. {
  2329. struct net_device *netdev = pci_get_drvdata(pdev);
  2330. struct nic *nic = netdev_priv(netdev);
  2331. int retval;
  2332. if(netif_running(netdev))
  2333. e100_down(nic);
  2334. e100_hw_reset(nic);
  2335. netif_device_detach(netdev);
  2336. pci_save_state(pdev);
  2337. retval = pci_enable_wake(pdev, pci_choose_state(pdev, state),
  2338. nic->flags & (wol_magic | e100_asf(nic)));
  2339. if (retval)
  2340. DPRINTK(PROBE,ERR, "Error enabling wake\n");
  2341. pci_disable_device(pdev);
  2342. retval = pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2343. if (retval)
  2344. DPRINTK(PROBE,ERR, "Error %d setting power state\n", retval);
  2345. return 0;
  2346. }
  2347. static int e100_resume(struct pci_dev *pdev)
  2348. {
  2349. struct net_device *netdev = pci_get_drvdata(pdev);
  2350. struct nic *nic = netdev_priv(netdev);
  2351. int retval;
  2352. retval = pci_set_power_state(pdev, PCI_D0);
  2353. if (retval)
  2354. DPRINTK(PROBE,ERR, "Error waking adapter\n");
  2355. pci_restore_state(pdev);
  2356. /* ack any pending wake events, disable PME */
  2357. retval = pci_enable_wake(pdev, 0, 0);
  2358. if (retval)
  2359. DPRINTK(PROBE,ERR, "Error clearing wake events\n");
  2360. netif_device_attach(netdev);
  2361. if(netif_running(netdev))
  2362. e100_up(nic);
  2363. return 0;
  2364. }
  2365. #endif
  2366. static void e100_shutdown(struct pci_dev *pdev)
  2367. {
  2368. struct net_device *netdev = pci_get_drvdata(pdev);
  2369. struct nic *nic = netdev_priv(netdev);
  2370. int retval;
  2371. #ifdef CONFIG_PM
  2372. retval = pci_enable_wake(pdev, 0, nic->flags & (wol_magic | e100_asf(nic)));
  2373. #else
  2374. retval = pci_enable_wake(pdev, 0, nic->flags & (wol_magic));
  2375. #endif
  2376. if (retval)
  2377. DPRINTK(PROBE,ERR, "Error enabling wake\n");
  2378. }
  2379. /* ------------------ PCI Error Recovery infrastructure -------------- */
  2380. /**
  2381. * e100_io_error_detected - called when PCI error is detected.
  2382. * @pdev: Pointer to PCI device
  2383. * @state: The current pci conneection state
  2384. */
  2385. static pci_ers_result_t e100_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  2386. {
  2387. struct net_device *netdev = pci_get_drvdata(pdev);
  2388. /* Similar to calling e100_down(), but avoids adpater I/O. */
  2389. netdev->stop(netdev);
  2390. /* Detach; put netif into state similar to hotplug unplug. */
  2391. netif_poll_enable(netdev);
  2392. netif_device_detach(netdev);
  2393. pci_disable_device(pdev);
  2394. /* Request a slot reset. */
  2395. return PCI_ERS_RESULT_NEED_RESET;
  2396. }
  2397. /**
  2398. * e100_io_slot_reset - called after the pci bus has been reset.
  2399. * @pdev: Pointer to PCI device
  2400. *
  2401. * Restart the card from scratch.
  2402. */
  2403. static pci_ers_result_t e100_io_slot_reset(struct pci_dev *pdev)
  2404. {
  2405. struct net_device *netdev = pci_get_drvdata(pdev);
  2406. struct nic *nic = netdev_priv(netdev);
  2407. if (pci_enable_device(pdev)) {
  2408. printk(KERN_ERR "e100: Cannot re-enable PCI device after reset.\n");
  2409. return PCI_ERS_RESULT_DISCONNECT;
  2410. }
  2411. pci_set_master(pdev);
  2412. /* Only one device per card can do a reset */
  2413. if (0 != PCI_FUNC(pdev->devfn))
  2414. return PCI_ERS_RESULT_RECOVERED;
  2415. e100_hw_reset(nic);
  2416. e100_phy_init(nic);
  2417. return PCI_ERS_RESULT_RECOVERED;
  2418. }
  2419. /**
  2420. * e100_io_resume - resume normal operations
  2421. * @pdev: Pointer to PCI device
  2422. *
  2423. * Resume normal operations after an error recovery
  2424. * sequence has been completed.
  2425. */
  2426. static void e100_io_resume(struct pci_dev *pdev)
  2427. {
  2428. struct net_device *netdev = pci_get_drvdata(pdev);
  2429. struct nic *nic = netdev_priv(netdev);
  2430. /* ack any pending wake events, disable PME */
  2431. pci_enable_wake(pdev, 0, 0);
  2432. netif_device_attach(netdev);
  2433. if (netif_running(netdev)) {
  2434. e100_open(netdev);
  2435. mod_timer(&nic->watchdog, jiffies);
  2436. }
  2437. }
  2438. static struct pci_error_handlers e100_err_handler = {
  2439. .error_detected = e100_io_error_detected,
  2440. .slot_reset = e100_io_slot_reset,
  2441. .resume = e100_io_resume,
  2442. };
  2443. static struct pci_driver e100_driver = {
  2444. .name = DRV_NAME,
  2445. .id_table = e100_id_table,
  2446. .probe = e100_probe,
  2447. .remove = __devexit_p(e100_remove),
  2448. #ifdef CONFIG_PM
  2449. .suspend = e100_suspend,
  2450. .resume = e100_resume,
  2451. #endif
  2452. .shutdown = e100_shutdown,
  2453. .err_handler = &e100_err_handler,
  2454. };
  2455. static int __init e100_init_module(void)
  2456. {
  2457. if(((1 << debug) - 1) & NETIF_MSG_DRV) {
  2458. printk(KERN_INFO PFX "%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  2459. printk(KERN_INFO PFX "%s\n", DRV_COPYRIGHT);
  2460. }
  2461. return pci_register_driver(&e100_driver);
  2462. }
  2463. static void __exit e100_cleanup_module(void)
  2464. {
  2465. pci_unregister_driver(&e100_driver);
  2466. }
  2467. module_init(e100_init_module);
  2468. module_exit(e100_cleanup_module);