e100.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495
  1. /*******************************************************************************
  2. Copyright(c) 1999 - 2005 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. #include <linux/config.h>
  128. #include <linux/module.h>
  129. #include <linux/moduleparam.h>
  130. #include <linux/kernel.h>
  131. #include <linux/types.h>
  132. #include <linux/slab.h>
  133. #include <linux/delay.h>
  134. #include <linux/init.h>
  135. #include <linux/pci.h>
  136. #include <linux/dma-mapping.h>
  137. #include <linux/netdevice.h>
  138. #include <linux/etherdevice.h>
  139. #include <linux/mii.h>
  140. #include <linux/if_vlan.h>
  141. #include <linux/skbuff.h>
  142. #include <linux/ethtool.h>
  143. #include <linux/string.h>
  144. #include <asm/unaligned.h>
  145. #define DRV_NAME "e100"
  146. #define DRV_EXT "-NAPI"
  147. #define DRV_VERSION "3.4.14-k2"DRV_EXT
  148. #define DRV_DESCRIPTION "Intel(R) PRO/100 Network Driver"
  149. #define DRV_COPYRIGHT "Copyright(c) 1999-2005 Intel Corporation"
  150. #define PFX DRV_NAME ": "
  151. #define E100_WATCHDOG_PERIOD (2 * HZ)
  152. #define E100_NAPI_WEIGHT 16
  153. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  154. MODULE_AUTHOR(DRV_COPYRIGHT);
  155. MODULE_LICENSE("GPL");
  156. MODULE_VERSION(DRV_VERSION);
  157. static int debug = 3;
  158. module_param(debug, int, 0);
  159. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  160. #define DPRINTK(nlevel, klevel, fmt, args...) \
  161. (void)((NETIF_MSG_##nlevel & nic->msg_enable) && \
  162. printk(KERN_##klevel PFX "%s: %s: " fmt, nic->netdev->name, \
  163. __FUNCTION__ , ## args))
  164. #define INTEL_8255X_ETHERNET_DEVICE(device_id, ich) {\
  165. PCI_VENDOR_ID_INTEL, device_id, PCI_ANY_ID, PCI_ANY_ID, \
  166. PCI_CLASS_NETWORK_ETHERNET << 8, 0xFFFF00, ich }
  167. static struct pci_device_id e100_id_table[] = {
  168. INTEL_8255X_ETHERNET_DEVICE(0x1029, 0),
  169. INTEL_8255X_ETHERNET_DEVICE(0x1030, 0),
  170. INTEL_8255X_ETHERNET_DEVICE(0x1031, 3),
  171. INTEL_8255X_ETHERNET_DEVICE(0x1032, 3),
  172. INTEL_8255X_ETHERNET_DEVICE(0x1033, 3),
  173. INTEL_8255X_ETHERNET_DEVICE(0x1034, 3),
  174. INTEL_8255X_ETHERNET_DEVICE(0x1038, 3),
  175. INTEL_8255X_ETHERNET_DEVICE(0x1039, 4),
  176. INTEL_8255X_ETHERNET_DEVICE(0x103A, 4),
  177. INTEL_8255X_ETHERNET_DEVICE(0x103B, 4),
  178. INTEL_8255X_ETHERNET_DEVICE(0x103C, 4),
  179. INTEL_8255X_ETHERNET_DEVICE(0x103D, 4),
  180. INTEL_8255X_ETHERNET_DEVICE(0x103E, 4),
  181. INTEL_8255X_ETHERNET_DEVICE(0x1050, 5),
  182. INTEL_8255X_ETHERNET_DEVICE(0x1051, 5),
  183. INTEL_8255X_ETHERNET_DEVICE(0x1052, 5),
  184. INTEL_8255X_ETHERNET_DEVICE(0x1053, 5),
  185. INTEL_8255X_ETHERNET_DEVICE(0x1054, 5),
  186. INTEL_8255X_ETHERNET_DEVICE(0x1055, 5),
  187. INTEL_8255X_ETHERNET_DEVICE(0x1056, 5),
  188. INTEL_8255X_ETHERNET_DEVICE(0x1057, 5),
  189. INTEL_8255X_ETHERNET_DEVICE(0x1059, 0),
  190. INTEL_8255X_ETHERNET_DEVICE(0x1064, 6),
  191. INTEL_8255X_ETHERNET_DEVICE(0x1065, 6),
  192. INTEL_8255X_ETHERNET_DEVICE(0x1066, 6),
  193. INTEL_8255X_ETHERNET_DEVICE(0x1067, 6),
  194. INTEL_8255X_ETHERNET_DEVICE(0x1068, 6),
  195. INTEL_8255X_ETHERNET_DEVICE(0x1069, 6),
  196. INTEL_8255X_ETHERNET_DEVICE(0x106A, 6),
  197. INTEL_8255X_ETHERNET_DEVICE(0x106B, 6),
  198. INTEL_8255X_ETHERNET_DEVICE(0x1091, 7),
  199. INTEL_8255X_ETHERNET_DEVICE(0x1092, 7),
  200. INTEL_8255X_ETHERNET_DEVICE(0x1093, 7),
  201. INTEL_8255X_ETHERNET_DEVICE(0x1094, 7),
  202. INTEL_8255X_ETHERNET_DEVICE(0x1095, 7),
  203. INTEL_8255X_ETHERNET_DEVICE(0x1209, 0),
  204. INTEL_8255X_ETHERNET_DEVICE(0x1229, 0),
  205. INTEL_8255X_ETHERNET_DEVICE(0x2449, 2),
  206. INTEL_8255X_ETHERNET_DEVICE(0x2459, 2),
  207. INTEL_8255X_ETHERNET_DEVICE(0x245D, 2),
  208. INTEL_8255X_ETHERNET_DEVICE(0x27DC, 7),
  209. { 0, }
  210. };
  211. MODULE_DEVICE_TABLE(pci, e100_id_table);
  212. enum mac {
  213. mac_82557_D100_A = 0,
  214. mac_82557_D100_B = 1,
  215. mac_82557_D100_C = 2,
  216. mac_82558_D101_A4 = 4,
  217. mac_82558_D101_B0 = 5,
  218. mac_82559_D101M = 8,
  219. mac_82559_D101S = 9,
  220. mac_82550_D102 = 12,
  221. mac_82550_D102_C = 13,
  222. mac_82551_E = 14,
  223. mac_82551_F = 15,
  224. mac_82551_10 = 16,
  225. mac_unknown = 0xFF,
  226. };
  227. enum phy {
  228. phy_100a = 0x000003E0,
  229. phy_100c = 0x035002A8,
  230. phy_82555_tx = 0x015002A8,
  231. phy_nsc_tx = 0x5C002000,
  232. phy_82562_et = 0x033002A8,
  233. phy_82562_em = 0x032002A8,
  234. phy_82562_ek = 0x031002A8,
  235. phy_82562_eh = 0x017002A8,
  236. phy_unknown = 0xFFFFFFFF,
  237. };
  238. /* CSR (Control/Status Registers) */
  239. struct csr {
  240. struct {
  241. u8 status;
  242. u8 stat_ack;
  243. u8 cmd_lo;
  244. u8 cmd_hi;
  245. u32 gen_ptr;
  246. } scb;
  247. u32 port;
  248. u16 flash_ctrl;
  249. u8 eeprom_ctrl_lo;
  250. u8 eeprom_ctrl_hi;
  251. u32 mdi_ctrl;
  252. u32 rx_dma_count;
  253. };
  254. enum scb_status {
  255. rus_ready = 0x10,
  256. rus_mask = 0x3C,
  257. };
  258. enum ru_state {
  259. RU_SUSPENDED = 0,
  260. RU_RUNNING = 1,
  261. RU_UNINITIALIZED = -1,
  262. };
  263. enum scb_stat_ack {
  264. stat_ack_not_ours = 0x00,
  265. stat_ack_sw_gen = 0x04,
  266. stat_ack_rnr = 0x10,
  267. stat_ack_cu_idle = 0x20,
  268. stat_ack_frame_rx = 0x40,
  269. stat_ack_cu_cmd_done = 0x80,
  270. stat_ack_not_present = 0xFF,
  271. stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx),
  272. stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done),
  273. };
  274. enum scb_cmd_hi {
  275. irq_mask_none = 0x00,
  276. irq_mask_all = 0x01,
  277. irq_sw_gen = 0x02,
  278. };
  279. enum scb_cmd_lo {
  280. cuc_nop = 0x00,
  281. ruc_start = 0x01,
  282. ruc_load_base = 0x06,
  283. cuc_start = 0x10,
  284. cuc_resume = 0x20,
  285. cuc_dump_addr = 0x40,
  286. cuc_dump_stats = 0x50,
  287. cuc_load_base = 0x60,
  288. cuc_dump_reset = 0x70,
  289. };
  290. enum cuc_dump {
  291. cuc_dump_complete = 0x0000A005,
  292. cuc_dump_reset_complete = 0x0000A007,
  293. };
  294. enum port {
  295. software_reset = 0x0000,
  296. selftest = 0x0001,
  297. selective_reset = 0x0002,
  298. };
  299. enum eeprom_ctrl_lo {
  300. eesk = 0x01,
  301. eecs = 0x02,
  302. eedi = 0x04,
  303. eedo = 0x08,
  304. };
  305. enum mdi_ctrl {
  306. mdi_write = 0x04000000,
  307. mdi_read = 0x08000000,
  308. mdi_ready = 0x10000000,
  309. };
  310. enum eeprom_op {
  311. op_write = 0x05,
  312. op_read = 0x06,
  313. op_ewds = 0x10,
  314. op_ewen = 0x13,
  315. };
  316. enum eeprom_offsets {
  317. eeprom_cnfg_mdix = 0x03,
  318. eeprom_id = 0x0A,
  319. eeprom_config_asf = 0x0D,
  320. eeprom_smbus_addr = 0x90,
  321. };
  322. enum eeprom_cnfg_mdix {
  323. eeprom_mdix_enabled = 0x0080,
  324. };
  325. enum eeprom_id {
  326. eeprom_id_wol = 0x0020,
  327. };
  328. enum eeprom_config_asf {
  329. eeprom_asf = 0x8000,
  330. eeprom_gcl = 0x4000,
  331. };
  332. enum cb_status {
  333. cb_complete = 0x8000,
  334. cb_ok = 0x2000,
  335. };
  336. enum cb_command {
  337. cb_nop = 0x0000,
  338. cb_iaaddr = 0x0001,
  339. cb_config = 0x0002,
  340. cb_multi = 0x0003,
  341. cb_tx = 0x0004,
  342. cb_ucode = 0x0005,
  343. cb_dump = 0x0006,
  344. cb_tx_sf = 0x0008,
  345. cb_cid = 0x1f00,
  346. cb_i = 0x2000,
  347. cb_s = 0x4000,
  348. cb_el = 0x8000,
  349. };
  350. struct rfd {
  351. u16 status;
  352. u16 command;
  353. u32 link;
  354. u32 rbd;
  355. u16 actual_size;
  356. u16 size;
  357. };
  358. struct rx {
  359. struct rx *next, *prev;
  360. struct sk_buff *skb;
  361. dma_addr_t dma_addr;
  362. };
  363. #if defined(__BIG_ENDIAN_BITFIELD)
  364. #define X(a,b) b,a
  365. #else
  366. #define X(a,b) a,b
  367. #endif
  368. struct config {
  369. /*0*/ u8 X(byte_count:6, pad0:2);
  370. /*1*/ u8 X(X(rx_fifo_limit:4, tx_fifo_limit:3), pad1:1);
  371. /*2*/ u8 adaptive_ifs;
  372. /*3*/ u8 X(X(X(X(mwi_enable:1, type_enable:1), read_align_enable:1),
  373. term_write_cache_line:1), pad3:4);
  374. /*4*/ u8 X(rx_dma_max_count:7, pad4:1);
  375. /*5*/ u8 X(tx_dma_max_count:7, dma_max_count_enable:1);
  376. /*6*/ u8 X(X(X(X(X(X(X(late_scb_update:1, direct_rx_dma:1),
  377. tno_intr:1), cna_intr:1), standard_tcb:1), standard_stat_counter:1),
  378. rx_discard_overruns:1), rx_save_bad_frames:1);
  379. /*7*/ u8 X(X(X(X(X(rx_discard_short_frames:1, tx_underrun_retry:2),
  380. pad7:2), rx_extended_rfd:1), tx_two_frames_in_fifo:1),
  381. tx_dynamic_tbd:1);
  382. /*8*/ u8 X(X(mii_mode:1, pad8:6), csma_disabled:1);
  383. /*9*/ u8 X(X(X(X(X(rx_tcpudp_checksum:1, pad9:3), vlan_arp_tco:1),
  384. link_status_wake:1), arp_wake:1), mcmatch_wake:1);
  385. /*10*/ u8 X(X(X(pad10:3, no_source_addr_insertion:1), preamble_length:2),
  386. loopback:2);
  387. /*11*/ u8 X(linear_priority:3, pad11:5);
  388. /*12*/ u8 X(X(linear_priority_mode:1, pad12:3), ifs:4);
  389. /*13*/ u8 ip_addr_lo;
  390. /*14*/ u8 ip_addr_hi;
  391. /*15*/ u8 X(X(X(X(X(X(X(promiscuous_mode:1, broadcast_disabled:1),
  392. wait_after_win:1), pad15_1:1), ignore_ul_bit:1), crc_16_bit:1),
  393. pad15_2:1), crs_or_cdt:1);
  394. /*16*/ u8 fc_delay_lo;
  395. /*17*/ u8 fc_delay_hi;
  396. /*18*/ u8 X(X(X(X(X(rx_stripping:1, tx_padding:1), rx_crc_transfer:1),
  397. rx_long_ok:1), fc_priority_threshold:3), pad18:1);
  398. /*19*/ u8 X(X(X(X(X(X(X(addr_wake:1, magic_packet_disable:1),
  399. fc_disable:1), fc_restop:1), fc_restart:1), fc_reject:1),
  400. full_duplex_force:1), full_duplex_pin:1);
  401. /*20*/ u8 X(X(X(pad20_1:5, fc_priority_location:1), multi_ia:1), pad20_2:1);
  402. /*21*/ u8 X(X(pad21_1:3, multicast_all:1), pad21_2:4);
  403. /*22*/ u8 X(X(rx_d102_mode:1, rx_vlan_drop:1), pad22:6);
  404. u8 pad_d102[9];
  405. };
  406. #define E100_MAX_MULTICAST_ADDRS 64
  407. struct multi {
  408. u16 count;
  409. u8 addr[E100_MAX_MULTICAST_ADDRS * ETH_ALEN + 2/*pad*/];
  410. };
  411. /* Important: keep total struct u32-aligned */
  412. #define UCODE_SIZE 134
  413. struct cb {
  414. u16 status;
  415. u16 command;
  416. u32 link;
  417. union {
  418. u8 iaaddr[ETH_ALEN];
  419. u32 ucode[UCODE_SIZE];
  420. struct config config;
  421. struct multi multi;
  422. struct {
  423. u32 tbd_array;
  424. u16 tcb_byte_count;
  425. u8 threshold;
  426. u8 tbd_count;
  427. struct {
  428. u32 buf_addr;
  429. u16 size;
  430. u16 eol;
  431. } tbd;
  432. } tcb;
  433. u32 dump_buffer_addr;
  434. } u;
  435. struct cb *next, *prev;
  436. dma_addr_t dma_addr;
  437. struct sk_buff *skb;
  438. };
  439. enum loopback {
  440. lb_none = 0, lb_mac = 1, lb_phy = 3,
  441. };
  442. struct stats {
  443. u32 tx_good_frames, tx_max_collisions, tx_late_collisions,
  444. tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,
  445. tx_multiple_collisions, tx_total_collisions;
  446. u32 rx_good_frames, rx_crc_errors, rx_alignment_errors,
  447. rx_resource_errors, rx_overrun_errors, rx_cdt_errors,
  448. rx_short_frame_errors;
  449. u32 fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;
  450. u16 xmt_tco_frames, rcv_tco_frames;
  451. u32 complete;
  452. };
  453. struct mem {
  454. struct {
  455. u32 signature;
  456. u32 result;
  457. } selftest;
  458. struct stats stats;
  459. u8 dump_buf[596];
  460. };
  461. struct param_range {
  462. u32 min;
  463. u32 max;
  464. u32 count;
  465. };
  466. struct params {
  467. struct param_range rfds;
  468. struct param_range cbs;
  469. };
  470. struct nic {
  471. /* Begin: frequently used values: keep adjacent for cache effect */
  472. u32 msg_enable ____cacheline_aligned;
  473. struct net_device *netdev;
  474. struct pci_dev *pdev;
  475. struct rx *rxs ____cacheline_aligned;
  476. struct rx *rx_to_use;
  477. struct rx *rx_to_clean;
  478. struct rfd blank_rfd;
  479. enum ru_state ru_running;
  480. spinlock_t cb_lock ____cacheline_aligned;
  481. spinlock_t cmd_lock;
  482. struct csr __iomem *csr;
  483. enum scb_cmd_lo cuc_cmd;
  484. unsigned int cbs_avail;
  485. struct cb *cbs;
  486. struct cb *cb_to_use;
  487. struct cb *cb_to_send;
  488. struct cb *cb_to_clean;
  489. u16 tx_command;
  490. /* End: frequently used values: keep adjacent for cache effect */
  491. enum {
  492. ich = (1 << 0),
  493. promiscuous = (1 << 1),
  494. multicast_all = (1 << 2),
  495. wol_magic = (1 << 3),
  496. ich_10h_workaround = (1 << 4),
  497. } flags ____cacheline_aligned;
  498. enum mac mac;
  499. enum phy phy;
  500. struct params params;
  501. struct net_device_stats net_stats;
  502. struct timer_list watchdog;
  503. struct timer_list blink_timer;
  504. struct mii_if_info mii;
  505. struct work_struct tx_timeout_task;
  506. enum loopback loopback;
  507. struct mem *mem;
  508. dma_addr_t dma_addr;
  509. dma_addr_t cbs_dma_addr;
  510. u8 adaptive_ifs;
  511. u8 tx_threshold;
  512. u32 tx_frames;
  513. u32 tx_collisions;
  514. u32 tx_deferred;
  515. u32 tx_single_collisions;
  516. u32 tx_multiple_collisions;
  517. u32 tx_fc_pause;
  518. u32 tx_tco_frames;
  519. u32 rx_fc_pause;
  520. u32 rx_fc_unsupported;
  521. u32 rx_tco_frames;
  522. u32 rx_over_length_errors;
  523. u8 rev_id;
  524. u16 leds;
  525. u16 eeprom_wc;
  526. u16 eeprom[256];
  527. };
  528. static inline void e100_write_flush(struct nic *nic)
  529. {
  530. /* Flush previous PCI writes through intermediate bridges
  531. * by doing a benign read */
  532. (void)readb(&nic->csr->scb.status);
  533. }
  534. static inline void e100_enable_irq(struct nic *nic)
  535. {
  536. unsigned long flags;
  537. spin_lock_irqsave(&nic->cmd_lock, flags);
  538. writeb(irq_mask_none, &nic->csr->scb.cmd_hi);
  539. spin_unlock_irqrestore(&nic->cmd_lock, flags);
  540. e100_write_flush(nic);
  541. }
  542. static inline void e100_disable_irq(struct nic *nic)
  543. {
  544. unsigned long flags;
  545. spin_lock_irqsave(&nic->cmd_lock, flags);
  546. writeb(irq_mask_all, &nic->csr->scb.cmd_hi);
  547. spin_unlock_irqrestore(&nic->cmd_lock, flags);
  548. e100_write_flush(nic);
  549. }
  550. static void e100_hw_reset(struct nic *nic)
  551. {
  552. /* Put CU and RU into idle with a selective reset to get
  553. * device off of PCI bus */
  554. writel(selective_reset, &nic->csr->port);
  555. e100_write_flush(nic); udelay(20);
  556. /* Now fully reset device */
  557. writel(software_reset, &nic->csr->port);
  558. e100_write_flush(nic); udelay(20);
  559. /* Mask off our interrupt line - it's unmasked after reset */
  560. e100_disable_irq(nic);
  561. }
  562. static int e100_self_test(struct nic *nic)
  563. {
  564. u32 dma_addr = nic->dma_addr + offsetof(struct mem, selftest);
  565. /* Passing the self-test is a pretty good indication
  566. * that the device can DMA to/from host memory */
  567. nic->mem->selftest.signature = 0;
  568. nic->mem->selftest.result = 0xFFFFFFFF;
  569. writel(selftest | dma_addr, &nic->csr->port);
  570. e100_write_flush(nic);
  571. /* Wait 10 msec for self-test to complete */
  572. msleep(10);
  573. /* Interrupts are enabled after self-test */
  574. e100_disable_irq(nic);
  575. /* Check results of self-test */
  576. if(nic->mem->selftest.result != 0) {
  577. DPRINTK(HW, ERR, "Self-test failed: result=0x%08X\n",
  578. nic->mem->selftest.result);
  579. return -ETIMEDOUT;
  580. }
  581. if(nic->mem->selftest.signature == 0) {
  582. DPRINTK(HW, ERR, "Self-test failed: timed out\n");
  583. return -ETIMEDOUT;
  584. }
  585. return 0;
  586. }
  587. static void e100_eeprom_write(struct nic *nic, u16 addr_len, u16 addr, u16 data)
  588. {
  589. u32 cmd_addr_data[3];
  590. u8 ctrl;
  591. int i, j;
  592. /* Three cmds: write/erase enable, write data, write/erase disable */
  593. cmd_addr_data[0] = op_ewen << (addr_len - 2);
  594. cmd_addr_data[1] = (((op_write << addr_len) | addr) << 16) |
  595. cpu_to_le16(data);
  596. cmd_addr_data[2] = op_ewds << (addr_len - 2);
  597. /* Bit-bang cmds to write word to eeprom */
  598. for(j = 0; j < 3; j++) {
  599. /* Chip select */
  600. writeb(eecs | eesk, &nic->csr->eeprom_ctrl_lo);
  601. e100_write_flush(nic); udelay(4);
  602. for(i = 31; i >= 0; i--) {
  603. ctrl = (cmd_addr_data[j] & (1 << i)) ?
  604. eecs | eedi : eecs;
  605. writeb(ctrl, &nic->csr->eeprom_ctrl_lo);
  606. e100_write_flush(nic); udelay(4);
  607. writeb(ctrl | eesk, &nic->csr->eeprom_ctrl_lo);
  608. e100_write_flush(nic); udelay(4);
  609. }
  610. /* Wait 10 msec for cmd to complete */
  611. msleep(10);
  612. /* Chip deselect */
  613. writeb(0, &nic->csr->eeprom_ctrl_lo);
  614. e100_write_flush(nic); udelay(4);
  615. }
  616. };
  617. /* General technique stolen from the eepro100 driver - very clever */
  618. static u16 e100_eeprom_read(struct nic *nic, u16 *addr_len, u16 addr)
  619. {
  620. u32 cmd_addr_data;
  621. u16 data = 0;
  622. u8 ctrl;
  623. int i;
  624. cmd_addr_data = ((op_read << *addr_len) | addr) << 16;
  625. /* Chip select */
  626. writeb(eecs | eesk, &nic->csr->eeprom_ctrl_lo);
  627. e100_write_flush(nic); udelay(4);
  628. /* Bit-bang to read word from eeprom */
  629. for(i = 31; i >= 0; i--) {
  630. ctrl = (cmd_addr_data & (1 << i)) ? eecs | eedi : eecs;
  631. writeb(ctrl, &nic->csr->eeprom_ctrl_lo);
  632. e100_write_flush(nic); udelay(4);
  633. writeb(ctrl | eesk, &nic->csr->eeprom_ctrl_lo);
  634. e100_write_flush(nic); udelay(4);
  635. /* Eeprom drives a dummy zero to EEDO after receiving
  636. * complete address. Use this to adjust addr_len. */
  637. ctrl = readb(&nic->csr->eeprom_ctrl_lo);
  638. if(!(ctrl & eedo) && i > 16) {
  639. *addr_len -= (i - 16);
  640. i = 17;
  641. }
  642. data = (data << 1) | (ctrl & eedo ? 1 : 0);
  643. }
  644. /* Chip deselect */
  645. writeb(0, &nic->csr->eeprom_ctrl_lo);
  646. e100_write_flush(nic); udelay(4);
  647. return le16_to_cpu(data);
  648. };
  649. /* Load entire EEPROM image into driver cache and validate checksum */
  650. static int e100_eeprom_load(struct nic *nic)
  651. {
  652. u16 addr, addr_len = 8, checksum = 0;
  653. /* Try reading with an 8-bit addr len to discover actual addr len */
  654. e100_eeprom_read(nic, &addr_len, 0);
  655. nic->eeprom_wc = 1 << addr_len;
  656. for(addr = 0; addr < nic->eeprom_wc; addr++) {
  657. nic->eeprom[addr] = e100_eeprom_read(nic, &addr_len, addr);
  658. if(addr < nic->eeprom_wc - 1)
  659. checksum += cpu_to_le16(nic->eeprom[addr]);
  660. }
  661. /* The checksum, stored in the last word, is calculated such that
  662. * the sum of words should be 0xBABA */
  663. checksum = le16_to_cpu(0xBABA - checksum);
  664. if(checksum != nic->eeprom[nic->eeprom_wc - 1]) {
  665. DPRINTK(PROBE, ERR, "EEPROM corrupted\n");
  666. return -EAGAIN;
  667. }
  668. return 0;
  669. }
  670. /* Save (portion of) driver EEPROM cache to device and update checksum */
  671. static int e100_eeprom_save(struct nic *nic, u16 start, u16 count)
  672. {
  673. u16 addr, addr_len = 8, checksum = 0;
  674. /* Try reading with an 8-bit addr len to discover actual addr len */
  675. e100_eeprom_read(nic, &addr_len, 0);
  676. nic->eeprom_wc = 1 << addr_len;
  677. if(start + count >= nic->eeprom_wc)
  678. return -EINVAL;
  679. for(addr = start; addr < start + count; addr++)
  680. e100_eeprom_write(nic, addr_len, addr, nic->eeprom[addr]);
  681. /* The checksum, stored in the last word, is calculated such that
  682. * the sum of words should be 0xBABA */
  683. for(addr = 0; addr < nic->eeprom_wc - 1; addr++)
  684. checksum += cpu_to_le16(nic->eeprom[addr]);
  685. nic->eeprom[nic->eeprom_wc - 1] = le16_to_cpu(0xBABA - checksum);
  686. e100_eeprom_write(nic, addr_len, nic->eeprom_wc - 1,
  687. nic->eeprom[nic->eeprom_wc - 1]);
  688. return 0;
  689. }
  690. #define E100_WAIT_SCB_TIMEOUT 20000 /* we might have to wait 100ms!!! */
  691. #define E100_WAIT_SCB_FAST 20 /* delay like the old code */
  692. static inline int e100_exec_cmd(struct nic *nic, u8 cmd, dma_addr_t dma_addr)
  693. {
  694. unsigned long flags;
  695. unsigned int i;
  696. int err = 0;
  697. spin_lock_irqsave(&nic->cmd_lock, flags);
  698. /* Previous command is accepted when SCB clears */
  699. for(i = 0; i < E100_WAIT_SCB_TIMEOUT; i++) {
  700. if(likely(!readb(&nic->csr->scb.cmd_lo)))
  701. break;
  702. cpu_relax();
  703. if(unlikely(i > E100_WAIT_SCB_FAST))
  704. udelay(5);
  705. }
  706. if(unlikely(i == E100_WAIT_SCB_TIMEOUT)) {
  707. err = -EAGAIN;
  708. goto err_unlock;
  709. }
  710. if(unlikely(cmd != cuc_resume))
  711. writel(dma_addr, &nic->csr->scb.gen_ptr);
  712. writeb(cmd, &nic->csr->scb.cmd_lo);
  713. err_unlock:
  714. spin_unlock_irqrestore(&nic->cmd_lock, flags);
  715. return err;
  716. }
  717. static inline int e100_exec_cb(struct nic *nic, struct sk_buff *skb,
  718. void (*cb_prepare)(struct nic *, struct cb *, struct sk_buff *))
  719. {
  720. struct cb *cb;
  721. unsigned long flags;
  722. int err = 0;
  723. spin_lock_irqsave(&nic->cb_lock, flags);
  724. if(unlikely(!nic->cbs_avail)) {
  725. err = -ENOMEM;
  726. goto err_unlock;
  727. }
  728. cb = nic->cb_to_use;
  729. nic->cb_to_use = cb->next;
  730. nic->cbs_avail--;
  731. cb->skb = skb;
  732. if(unlikely(!nic->cbs_avail))
  733. err = -ENOSPC;
  734. cb_prepare(nic, cb, skb);
  735. /* Order is important otherwise we'll be in a race with h/w:
  736. * set S-bit in current first, then clear S-bit in previous. */
  737. cb->command |= cpu_to_le16(cb_s);
  738. wmb();
  739. cb->prev->command &= cpu_to_le16(~cb_s);
  740. while(nic->cb_to_send != nic->cb_to_use) {
  741. if(unlikely(e100_exec_cmd(nic, nic->cuc_cmd,
  742. nic->cb_to_send->dma_addr))) {
  743. /* Ok, here's where things get sticky. It's
  744. * possible that we can't schedule the command
  745. * because the controller is too busy, so
  746. * let's just queue the command and try again
  747. * when another command is scheduled. */
  748. if(err == -ENOSPC) {
  749. //request a reset
  750. schedule_work(&nic->tx_timeout_task);
  751. }
  752. break;
  753. } else {
  754. nic->cuc_cmd = cuc_resume;
  755. nic->cb_to_send = nic->cb_to_send->next;
  756. }
  757. }
  758. err_unlock:
  759. spin_unlock_irqrestore(&nic->cb_lock, flags);
  760. return err;
  761. }
  762. static u16 mdio_ctrl(struct nic *nic, u32 addr, u32 dir, u32 reg, u16 data)
  763. {
  764. u32 data_out = 0;
  765. unsigned int i;
  766. writel((reg << 16) | (addr << 21) | dir | data, &nic->csr->mdi_ctrl);
  767. for(i = 0; i < 100; i++) {
  768. udelay(20);
  769. if((data_out = readl(&nic->csr->mdi_ctrl)) & mdi_ready)
  770. break;
  771. }
  772. DPRINTK(HW, DEBUG,
  773. "%s:addr=%d, reg=%d, data_in=0x%04X, data_out=0x%04X\n",
  774. dir == mdi_read ? "READ" : "WRITE", addr, reg, data, data_out);
  775. return (u16)data_out;
  776. }
  777. static int mdio_read(struct net_device *netdev, int addr, int reg)
  778. {
  779. return mdio_ctrl(netdev_priv(netdev), addr, mdi_read, reg, 0);
  780. }
  781. static void mdio_write(struct net_device *netdev, int addr, int reg, int data)
  782. {
  783. mdio_ctrl(netdev_priv(netdev), addr, mdi_write, reg, data);
  784. }
  785. static void e100_get_defaults(struct nic *nic)
  786. {
  787. struct param_range rfds = { .min = 16, .max = 256, .count = 64 };
  788. struct param_range cbs = { .min = 64, .max = 256, .count = 64 };
  789. pci_read_config_byte(nic->pdev, PCI_REVISION_ID, &nic->rev_id);
  790. /* MAC type is encoded as rev ID; exception: ICH is treated as 82559 */
  791. nic->mac = (nic->flags & ich) ? mac_82559_D101M : nic->rev_id;
  792. if(nic->mac == mac_unknown)
  793. nic->mac = mac_82557_D100_A;
  794. nic->params.rfds = rfds;
  795. nic->params.cbs = cbs;
  796. /* Quadwords to DMA into FIFO before starting frame transmit */
  797. nic->tx_threshold = 0xE0;
  798. /* no interrupt for every tx completion, delay = 256us if not 557*/
  799. nic->tx_command = cpu_to_le16(cb_tx | cb_tx_sf |
  800. ((nic->mac >= mac_82558_D101_A4) ? cb_cid : cb_i));
  801. /* Template for a freshly allocated RFD */
  802. nic->blank_rfd.command = cpu_to_le16(cb_el);
  803. nic->blank_rfd.rbd = 0xFFFFFFFF;
  804. nic->blank_rfd.size = cpu_to_le16(VLAN_ETH_FRAME_LEN);
  805. /* MII setup */
  806. nic->mii.phy_id_mask = 0x1F;
  807. nic->mii.reg_num_mask = 0x1F;
  808. nic->mii.dev = nic->netdev;
  809. nic->mii.mdio_read = mdio_read;
  810. nic->mii.mdio_write = mdio_write;
  811. }
  812. static void e100_configure(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  813. {
  814. struct config *config = &cb->u.config;
  815. u8 *c = (u8 *)config;
  816. cb->command = cpu_to_le16(cb_config);
  817. memset(config, 0, sizeof(struct config));
  818. config->byte_count = 0x16; /* bytes in this struct */
  819. config->rx_fifo_limit = 0x8; /* bytes in FIFO before DMA */
  820. config->direct_rx_dma = 0x1; /* reserved */
  821. config->standard_tcb = 0x1; /* 1=standard, 0=extended */
  822. config->standard_stat_counter = 0x1; /* 1=standard, 0=extended */
  823. config->rx_discard_short_frames = 0x1; /* 1=discard, 0=pass */
  824. config->tx_underrun_retry = 0x3; /* # of underrun retries */
  825. config->mii_mode = 0x1; /* 1=MII mode, 0=503 mode */
  826. config->pad10 = 0x6;
  827. config->no_source_addr_insertion = 0x1; /* 1=no, 0=yes */
  828. config->preamble_length = 0x2; /* 0=1, 1=3, 2=7, 3=15 bytes */
  829. config->ifs = 0x6; /* x16 = inter frame spacing */
  830. config->ip_addr_hi = 0xF2; /* ARP IP filter - not used */
  831. config->pad15_1 = 0x1;
  832. config->pad15_2 = 0x1;
  833. config->crs_or_cdt = 0x0; /* 0=CRS only, 1=CRS or CDT */
  834. config->fc_delay_hi = 0x40; /* time delay for fc frame */
  835. config->tx_padding = 0x1; /* 1=pad short frames */
  836. config->fc_priority_threshold = 0x7; /* 7=priority fc disabled */
  837. config->pad18 = 0x1;
  838. config->full_duplex_pin = 0x1; /* 1=examine FDX# pin */
  839. config->pad20_1 = 0x1F;
  840. config->fc_priority_location = 0x1; /* 1=byte#31, 0=byte#19 */
  841. config->pad21_1 = 0x5;
  842. config->adaptive_ifs = nic->adaptive_ifs;
  843. config->loopback = nic->loopback;
  844. if(nic->mii.force_media && nic->mii.full_duplex)
  845. config->full_duplex_force = 0x1; /* 1=force, 0=auto */
  846. if(nic->flags & promiscuous || nic->loopback) {
  847. config->rx_save_bad_frames = 0x1; /* 1=save, 0=discard */
  848. config->rx_discard_short_frames = 0x0; /* 1=discard, 0=save */
  849. config->promiscuous_mode = 0x1; /* 1=on, 0=off */
  850. }
  851. if(nic->flags & multicast_all)
  852. config->multicast_all = 0x1; /* 1=accept, 0=no */
  853. /* disable WoL when up */
  854. if(netif_running(nic->netdev) || !(nic->flags & wol_magic))
  855. config->magic_packet_disable = 0x1; /* 1=off, 0=on */
  856. if(nic->mac >= mac_82558_D101_A4) {
  857. config->fc_disable = 0x1; /* 1=Tx fc off, 0=Tx fc on */
  858. config->mwi_enable = 0x1; /* 1=enable, 0=disable */
  859. config->standard_tcb = 0x0; /* 1=standard, 0=extended */
  860. config->rx_long_ok = 0x1; /* 1=VLANs ok, 0=standard */
  861. if(nic->mac >= mac_82559_D101M)
  862. config->tno_intr = 0x1; /* TCO stats enable */
  863. else
  864. config->standard_stat_counter = 0x0;
  865. }
  866. DPRINTK(HW, DEBUG, "[00-07]=%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
  867. c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7]);
  868. DPRINTK(HW, DEBUG, "[08-15]=%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
  869. c[8], c[9], c[10], c[11], c[12], c[13], c[14], c[15]);
  870. DPRINTK(HW, DEBUG, "[16-23]=%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
  871. c[16], c[17], c[18], c[19], c[20], c[21], c[22], c[23]);
  872. }
  873. static void e100_load_ucode(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  874. {
  875. int i;
  876. static const u32 ucode[UCODE_SIZE] = {
  877. /* NFS packets are misinterpreted as TCO packets and
  878. * incorrectly routed to the BMC over SMBus. This
  879. * microcode patch checks the fragmented IP bit in the
  880. * NFS/UDP header to distinguish between NFS and TCO. */
  881. 0x0EF70E36, 0x1FFF1FFF, 0x1FFF1FFF, 0x1FFF1FFF, 0x1FFF1FFF,
  882. 0x1FFF1FFF, 0x00906E41, 0x00800E3C, 0x00E00E39, 0x00000000,
  883. 0x00906EFD, 0x00900EFD, 0x00E00EF8,
  884. };
  885. if(nic->mac == mac_82551_F || nic->mac == mac_82551_10) {
  886. for(i = 0; i < UCODE_SIZE; i++)
  887. cb->u.ucode[i] = cpu_to_le32(ucode[i]);
  888. cb->command = cpu_to_le16(cb_ucode);
  889. } else
  890. cb->command = cpu_to_le16(cb_nop);
  891. }
  892. static void e100_setup_iaaddr(struct nic *nic, struct cb *cb,
  893. struct sk_buff *skb)
  894. {
  895. cb->command = cpu_to_le16(cb_iaaddr);
  896. memcpy(cb->u.iaaddr, nic->netdev->dev_addr, ETH_ALEN);
  897. }
  898. static void e100_dump(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  899. {
  900. cb->command = cpu_to_le16(cb_dump);
  901. cb->u.dump_buffer_addr = cpu_to_le32(nic->dma_addr +
  902. offsetof(struct mem, dump_buf));
  903. }
  904. #define NCONFIG_AUTO_SWITCH 0x0080
  905. #define MII_NSC_CONG MII_RESV1
  906. #define NSC_CONG_ENABLE 0x0100
  907. #define NSC_CONG_TXREADY 0x0400
  908. #define ADVERTISE_FC_SUPPORTED 0x0400
  909. static int e100_phy_init(struct nic *nic)
  910. {
  911. struct net_device *netdev = nic->netdev;
  912. u32 addr;
  913. u16 bmcr, stat, id_lo, id_hi, cong;
  914. /* Discover phy addr by searching addrs in order {1,0,2,..., 31} */
  915. for(addr = 0; addr < 32; addr++) {
  916. nic->mii.phy_id = (addr == 0) ? 1 : (addr == 1) ? 0 : addr;
  917. bmcr = mdio_read(netdev, nic->mii.phy_id, MII_BMCR);
  918. stat = mdio_read(netdev, nic->mii.phy_id, MII_BMSR);
  919. stat = mdio_read(netdev, nic->mii.phy_id, MII_BMSR);
  920. if(!((bmcr == 0xFFFF) || ((stat == 0) && (bmcr == 0))))
  921. break;
  922. }
  923. DPRINTK(HW, DEBUG, "phy_addr = %d\n", nic->mii.phy_id);
  924. if(addr == 32)
  925. return -EAGAIN;
  926. /* Selected the phy and isolate the rest */
  927. for(addr = 0; addr < 32; addr++) {
  928. if(addr != nic->mii.phy_id) {
  929. mdio_write(netdev, addr, MII_BMCR, BMCR_ISOLATE);
  930. } else {
  931. bmcr = mdio_read(netdev, addr, MII_BMCR);
  932. mdio_write(netdev, addr, MII_BMCR,
  933. bmcr & ~BMCR_ISOLATE);
  934. }
  935. }
  936. /* Get phy ID */
  937. id_lo = mdio_read(netdev, nic->mii.phy_id, MII_PHYSID1);
  938. id_hi = mdio_read(netdev, nic->mii.phy_id, MII_PHYSID2);
  939. nic->phy = (u32)id_hi << 16 | (u32)id_lo;
  940. DPRINTK(HW, DEBUG, "phy ID = 0x%08X\n", nic->phy);
  941. /* Handle National tx phys */
  942. #define NCS_PHY_MODEL_MASK 0xFFF0FFFF
  943. if((nic->phy & NCS_PHY_MODEL_MASK) == phy_nsc_tx) {
  944. /* Disable congestion control */
  945. cong = mdio_read(netdev, nic->mii.phy_id, MII_NSC_CONG);
  946. cong |= NSC_CONG_TXREADY;
  947. cong &= ~NSC_CONG_ENABLE;
  948. mdio_write(netdev, nic->mii.phy_id, MII_NSC_CONG, cong);
  949. }
  950. if((nic->mac >= mac_82550_D102) || ((nic->flags & ich) &&
  951. (mdio_read(netdev, nic->mii.phy_id, MII_TPISTATUS) & 0x8000))) {
  952. /* enable/disable MDI/MDI-X auto-switching.
  953. MDI/MDI-X auto-switching is disabled for 82551ER/QM chips */
  954. if((nic->mac == mac_82551_E) || (nic->mac == mac_82551_F) ||
  955. (nic->mac == mac_82551_10) || (nic->mii.force_media) ||
  956. !(nic->eeprom[eeprom_cnfg_mdix] & eeprom_mdix_enabled))
  957. mdio_write(netdev, nic->mii.phy_id, MII_NCONFIG, 0);
  958. else
  959. mdio_write(netdev, nic->mii.phy_id, MII_NCONFIG, NCONFIG_AUTO_SWITCH);
  960. }
  961. return 0;
  962. }
  963. static int e100_hw_init(struct nic *nic)
  964. {
  965. int err;
  966. e100_hw_reset(nic);
  967. DPRINTK(HW, ERR, "e100_hw_init\n");
  968. if(!in_interrupt() && (err = e100_self_test(nic)))
  969. return err;
  970. if((err = e100_phy_init(nic)))
  971. return err;
  972. if((err = e100_exec_cmd(nic, cuc_load_base, 0)))
  973. return err;
  974. if((err = e100_exec_cmd(nic, ruc_load_base, 0)))
  975. return err;
  976. if((err = e100_exec_cb(nic, NULL, e100_load_ucode)))
  977. return err;
  978. if((err = e100_exec_cb(nic, NULL, e100_configure)))
  979. return err;
  980. if((err = e100_exec_cb(nic, NULL, e100_setup_iaaddr)))
  981. return err;
  982. if((err = e100_exec_cmd(nic, cuc_dump_addr,
  983. nic->dma_addr + offsetof(struct mem, stats))))
  984. return err;
  985. if((err = e100_exec_cmd(nic, cuc_dump_reset, 0)))
  986. return err;
  987. e100_disable_irq(nic);
  988. return 0;
  989. }
  990. static void e100_multi(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  991. {
  992. struct net_device *netdev = nic->netdev;
  993. struct dev_mc_list *list = netdev->mc_list;
  994. u16 i, count = min(netdev->mc_count, E100_MAX_MULTICAST_ADDRS);
  995. cb->command = cpu_to_le16(cb_multi);
  996. cb->u.multi.count = cpu_to_le16(count * ETH_ALEN);
  997. for(i = 0; list && i < count; i++, list = list->next)
  998. memcpy(&cb->u.multi.addr[i*ETH_ALEN], &list->dmi_addr,
  999. ETH_ALEN);
  1000. }
  1001. static void e100_set_multicast_list(struct net_device *netdev)
  1002. {
  1003. struct nic *nic = netdev_priv(netdev);
  1004. DPRINTK(HW, DEBUG, "mc_count=%d, flags=0x%04X\n",
  1005. netdev->mc_count, netdev->flags);
  1006. if(netdev->flags & IFF_PROMISC)
  1007. nic->flags |= promiscuous;
  1008. else
  1009. nic->flags &= ~promiscuous;
  1010. if(netdev->flags & IFF_ALLMULTI ||
  1011. netdev->mc_count > E100_MAX_MULTICAST_ADDRS)
  1012. nic->flags |= multicast_all;
  1013. else
  1014. nic->flags &= ~multicast_all;
  1015. e100_exec_cb(nic, NULL, e100_configure);
  1016. e100_exec_cb(nic, NULL, e100_multi);
  1017. }
  1018. static void e100_update_stats(struct nic *nic)
  1019. {
  1020. struct net_device_stats *ns = &nic->net_stats;
  1021. struct stats *s = &nic->mem->stats;
  1022. u32 *complete = (nic->mac < mac_82558_D101_A4) ? &s->fc_xmt_pause :
  1023. (nic->mac < mac_82559_D101M) ? (u32 *)&s->xmt_tco_frames :
  1024. &s->complete;
  1025. /* Device's stats reporting may take several microseconds to
  1026. * complete, so where always waiting for results of the
  1027. * previous command. */
  1028. if(*complete == le32_to_cpu(cuc_dump_reset_complete)) {
  1029. *complete = 0;
  1030. nic->tx_frames = le32_to_cpu(s->tx_good_frames);
  1031. nic->tx_collisions = le32_to_cpu(s->tx_total_collisions);
  1032. ns->tx_aborted_errors += le32_to_cpu(s->tx_max_collisions);
  1033. ns->tx_window_errors += le32_to_cpu(s->tx_late_collisions);
  1034. ns->tx_carrier_errors += le32_to_cpu(s->tx_lost_crs);
  1035. ns->tx_fifo_errors += le32_to_cpu(s->tx_underruns);
  1036. ns->collisions += nic->tx_collisions;
  1037. ns->tx_errors += le32_to_cpu(s->tx_max_collisions) +
  1038. le32_to_cpu(s->tx_lost_crs);
  1039. ns->rx_length_errors += le32_to_cpu(s->rx_short_frame_errors) +
  1040. nic->rx_over_length_errors;
  1041. ns->rx_crc_errors += le32_to_cpu(s->rx_crc_errors);
  1042. ns->rx_frame_errors += le32_to_cpu(s->rx_alignment_errors);
  1043. ns->rx_over_errors += le32_to_cpu(s->rx_overrun_errors);
  1044. ns->rx_fifo_errors += le32_to_cpu(s->rx_overrun_errors);
  1045. ns->rx_missed_errors += le32_to_cpu(s->rx_resource_errors);
  1046. ns->rx_errors += le32_to_cpu(s->rx_crc_errors) +
  1047. le32_to_cpu(s->rx_alignment_errors) +
  1048. le32_to_cpu(s->rx_short_frame_errors) +
  1049. le32_to_cpu(s->rx_cdt_errors);
  1050. nic->tx_deferred += le32_to_cpu(s->tx_deferred);
  1051. nic->tx_single_collisions +=
  1052. le32_to_cpu(s->tx_single_collisions);
  1053. nic->tx_multiple_collisions +=
  1054. le32_to_cpu(s->tx_multiple_collisions);
  1055. if(nic->mac >= mac_82558_D101_A4) {
  1056. nic->tx_fc_pause += le32_to_cpu(s->fc_xmt_pause);
  1057. nic->rx_fc_pause += le32_to_cpu(s->fc_rcv_pause);
  1058. nic->rx_fc_unsupported +=
  1059. le32_to_cpu(s->fc_rcv_unsupported);
  1060. if(nic->mac >= mac_82559_D101M) {
  1061. nic->tx_tco_frames +=
  1062. le16_to_cpu(s->xmt_tco_frames);
  1063. nic->rx_tco_frames +=
  1064. le16_to_cpu(s->rcv_tco_frames);
  1065. }
  1066. }
  1067. }
  1068. if(e100_exec_cmd(nic, cuc_dump_reset, 0))
  1069. DPRINTK(TX_ERR, DEBUG, "exec cuc_dump_reset failed\n");
  1070. }
  1071. static void e100_adjust_adaptive_ifs(struct nic *nic, int speed, int duplex)
  1072. {
  1073. /* Adjust inter-frame-spacing (IFS) between two transmits if
  1074. * we're getting collisions on a half-duplex connection. */
  1075. if(duplex == DUPLEX_HALF) {
  1076. u32 prev = nic->adaptive_ifs;
  1077. u32 min_frames = (speed == SPEED_100) ? 1000 : 100;
  1078. if((nic->tx_frames / 32 < nic->tx_collisions) &&
  1079. (nic->tx_frames > min_frames)) {
  1080. if(nic->adaptive_ifs < 60)
  1081. nic->adaptive_ifs += 5;
  1082. } else if (nic->tx_frames < min_frames) {
  1083. if(nic->adaptive_ifs >= 5)
  1084. nic->adaptive_ifs -= 5;
  1085. }
  1086. if(nic->adaptive_ifs != prev)
  1087. e100_exec_cb(nic, NULL, e100_configure);
  1088. }
  1089. }
  1090. static void e100_watchdog(unsigned long data)
  1091. {
  1092. struct nic *nic = (struct nic *)data;
  1093. struct ethtool_cmd cmd;
  1094. DPRINTK(TIMER, DEBUG, "right now = %ld\n", jiffies);
  1095. /* mii library handles link maintenance tasks */
  1096. mii_ethtool_gset(&nic->mii, &cmd);
  1097. if(mii_link_ok(&nic->mii) && !netif_carrier_ok(nic->netdev)) {
  1098. DPRINTK(LINK, INFO, "link up, %sMbps, %s-duplex\n",
  1099. cmd.speed == SPEED_100 ? "100" : "10",
  1100. cmd.duplex == DUPLEX_FULL ? "full" : "half");
  1101. } else if(!mii_link_ok(&nic->mii) && netif_carrier_ok(nic->netdev)) {
  1102. DPRINTK(LINK, INFO, "link down\n");
  1103. }
  1104. mii_check_link(&nic->mii);
  1105. /* Software generated interrupt to recover from (rare) Rx
  1106. * allocation failure.
  1107. * Unfortunately have to use a spinlock to not re-enable interrupts
  1108. * accidentally, due to hardware that shares a register between the
  1109. * interrupt mask bit and the SW Interrupt generation bit */
  1110. spin_lock_irq(&nic->cmd_lock);
  1111. writeb(readb(&nic->csr->scb.cmd_hi) | irq_sw_gen,&nic->csr->scb.cmd_hi);
  1112. spin_unlock_irq(&nic->cmd_lock);
  1113. e100_write_flush(nic);
  1114. e100_update_stats(nic);
  1115. e100_adjust_adaptive_ifs(nic, cmd.speed, cmd.duplex);
  1116. if(nic->mac <= mac_82557_D100_C)
  1117. /* Issue a multicast command to workaround a 557 lock up */
  1118. e100_set_multicast_list(nic->netdev);
  1119. if(nic->flags & ich && cmd.speed==SPEED_10 && cmd.duplex==DUPLEX_HALF)
  1120. /* Need SW workaround for ICH[x] 10Mbps/half duplex Tx hang. */
  1121. nic->flags |= ich_10h_workaround;
  1122. else
  1123. nic->flags &= ~ich_10h_workaround;
  1124. mod_timer(&nic->watchdog, jiffies + E100_WATCHDOG_PERIOD);
  1125. }
  1126. static inline void e100_xmit_prepare(struct nic *nic, struct cb *cb,
  1127. struct sk_buff *skb)
  1128. {
  1129. cb->command = nic->tx_command;
  1130. /* interrupt every 16 packets regardless of delay */
  1131. if((nic->cbs_avail & ~15) == nic->cbs_avail)
  1132. cb->command |= cpu_to_le16(cb_i);
  1133. cb->u.tcb.tbd_array = cb->dma_addr + offsetof(struct cb, u.tcb.tbd);
  1134. cb->u.tcb.tcb_byte_count = 0;
  1135. cb->u.tcb.threshold = nic->tx_threshold;
  1136. cb->u.tcb.tbd_count = 1;
  1137. cb->u.tcb.tbd.buf_addr = cpu_to_le32(pci_map_single(nic->pdev,
  1138. skb->data, skb->len, PCI_DMA_TODEVICE));
  1139. /* check for mapping failure? */
  1140. cb->u.tcb.tbd.size = cpu_to_le16(skb->len);
  1141. }
  1142. static int e100_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  1143. {
  1144. struct nic *nic = netdev_priv(netdev);
  1145. int err;
  1146. if(nic->flags & ich_10h_workaround) {
  1147. /* SW workaround for ICH[x] 10Mbps/half duplex Tx hang.
  1148. Issue a NOP command followed by a 1us delay before
  1149. issuing the Tx command. */
  1150. if(e100_exec_cmd(nic, cuc_nop, 0))
  1151. DPRINTK(TX_ERR, DEBUG, "exec cuc_nop failed\n");
  1152. udelay(1);
  1153. }
  1154. err = e100_exec_cb(nic, skb, e100_xmit_prepare);
  1155. switch(err) {
  1156. case -ENOSPC:
  1157. /* We queued the skb, but now we're out of space. */
  1158. DPRINTK(TX_ERR, DEBUG, "No space for CB\n");
  1159. netif_stop_queue(netdev);
  1160. break;
  1161. case -ENOMEM:
  1162. /* This is a hard error - log it. */
  1163. DPRINTK(TX_ERR, DEBUG, "Out of Tx resources, returning skb\n");
  1164. netif_stop_queue(netdev);
  1165. return 1;
  1166. }
  1167. netdev->trans_start = jiffies;
  1168. return 0;
  1169. }
  1170. static inline int e100_tx_clean(struct nic *nic)
  1171. {
  1172. struct cb *cb;
  1173. int tx_cleaned = 0;
  1174. spin_lock(&nic->cb_lock);
  1175. DPRINTK(TX_DONE, DEBUG, "cb->status = 0x%04X\n",
  1176. nic->cb_to_clean->status);
  1177. /* Clean CBs marked complete */
  1178. for(cb = nic->cb_to_clean;
  1179. cb->status & cpu_to_le16(cb_complete);
  1180. cb = nic->cb_to_clean = cb->next) {
  1181. if(likely(cb->skb != NULL)) {
  1182. nic->net_stats.tx_packets++;
  1183. nic->net_stats.tx_bytes += cb->skb->len;
  1184. pci_unmap_single(nic->pdev,
  1185. le32_to_cpu(cb->u.tcb.tbd.buf_addr),
  1186. le16_to_cpu(cb->u.tcb.tbd.size),
  1187. PCI_DMA_TODEVICE);
  1188. dev_kfree_skb_any(cb->skb);
  1189. cb->skb = NULL;
  1190. tx_cleaned = 1;
  1191. }
  1192. cb->status = 0;
  1193. nic->cbs_avail++;
  1194. }
  1195. spin_unlock(&nic->cb_lock);
  1196. /* Recover from running out of Tx resources in xmit_frame */
  1197. if(unlikely(tx_cleaned && netif_queue_stopped(nic->netdev)))
  1198. netif_wake_queue(nic->netdev);
  1199. return tx_cleaned;
  1200. }
  1201. static void e100_clean_cbs(struct nic *nic)
  1202. {
  1203. if(nic->cbs) {
  1204. while(nic->cbs_avail != nic->params.cbs.count) {
  1205. struct cb *cb = nic->cb_to_clean;
  1206. if(cb->skb) {
  1207. pci_unmap_single(nic->pdev,
  1208. le32_to_cpu(cb->u.tcb.tbd.buf_addr),
  1209. le16_to_cpu(cb->u.tcb.tbd.size),
  1210. PCI_DMA_TODEVICE);
  1211. dev_kfree_skb(cb->skb);
  1212. }
  1213. nic->cb_to_clean = nic->cb_to_clean->next;
  1214. nic->cbs_avail++;
  1215. }
  1216. pci_free_consistent(nic->pdev,
  1217. sizeof(struct cb) * nic->params.cbs.count,
  1218. nic->cbs, nic->cbs_dma_addr);
  1219. nic->cbs = NULL;
  1220. nic->cbs_avail = 0;
  1221. }
  1222. nic->cuc_cmd = cuc_start;
  1223. nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean =
  1224. nic->cbs;
  1225. }
  1226. static int e100_alloc_cbs(struct nic *nic)
  1227. {
  1228. struct cb *cb;
  1229. unsigned int i, count = nic->params.cbs.count;
  1230. nic->cuc_cmd = cuc_start;
  1231. nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean = NULL;
  1232. nic->cbs_avail = 0;
  1233. nic->cbs = pci_alloc_consistent(nic->pdev,
  1234. sizeof(struct cb) * count, &nic->cbs_dma_addr);
  1235. if(!nic->cbs)
  1236. return -ENOMEM;
  1237. for(cb = nic->cbs, i = 0; i < count; cb++, i++) {
  1238. cb->next = (i + 1 < count) ? cb + 1 : nic->cbs;
  1239. cb->prev = (i == 0) ? nic->cbs + count - 1 : cb - 1;
  1240. cb->dma_addr = nic->cbs_dma_addr + i * sizeof(struct cb);
  1241. cb->link = cpu_to_le32(nic->cbs_dma_addr +
  1242. ((i+1) % count) * sizeof(struct cb));
  1243. cb->skb = NULL;
  1244. }
  1245. nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean = nic->cbs;
  1246. nic->cbs_avail = count;
  1247. return 0;
  1248. }
  1249. static inline void e100_start_receiver(struct nic *nic, struct rx *rx)
  1250. {
  1251. if(!nic->rxs) return;
  1252. if(RU_SUSPENDED != nic->ru_running) return;
  1253. /* handle init time starts */
  1254. if(!rx) rx = nic->rxs;
  1255. /* (Re)start RU if suspended or idle and RFA is non-NULL */
  1256. if(rx->skb) {
  1257. e100_exec_cmd(nic, ruc_start, rx->dma_addr);
  1258. nic->ru_running = RU_RUNNING;
  1259. }
  1260. }
  1261. #define RFD_BUF_LEN (sizeof(struct rfd) + VLAN_ETH_FRAME_LEN)
  1262. static inline int e100_rx_alloc_skb(struct nic *nic, struct rx *rx)
  1263. {
  1264. if(!(rx->skb = dev_alloc_skb(RFD_BUF_LEN + NET_IP_ALIGN)))
  1265. return -ENOMEM;
  1266. /* Align, init, and map the RFD. */
  1267. rx->skb->dev = nic->netdev;
  1268. skb_reserve(rx->skb, NET_IP_ALIGN);
  1269. memcpy(rx->skb->data, &nic->blank_rfd, sizeof(struct rfd));
  1270. rx->dma_addr = pci_map_single(nic->pdev, rx->skb->data,
  1271. RFD_BUF_LEN, PCI_DMA_BIDIRECTIONAL);
  1272. if(pci_dma_mapping_error(rx->dma_addr)) {
  1273. dev_kfree_skb_any(rx->skb);
  1274. rx->skb = 0;
  1275. rx->dma_addr = 0;
  1276. return -ENOMEM;
  1277. }
  1278. /* Link the RFD to end of RFA by linking previous RFD to
  1279. * this one, and clearing EL bit of previous. */
  1280. if(rx->prev->skb) {
  1281. struct rfd *prev_rfd = (struct rfd *)rx->prev->skb->data;
  1282. put_unaligned(cpu_to_le32(rx->dma_addr),
  1283. (u32 *)&prev_rfd->link);
  1284. wmb();
  1285. prev_rfd->command &= ~cpu_to_le16(cb_el);
  1286. pci_dma_sync_single_for_device(nic->pdev, rx->prev->dma_addr,
  1287. sizeof(struct rfd), PCI_DMA_TODEVICE);
  1288. }
  1289. return 0;
  1290. }
  1291. static inline int e100_rx_indicate(struct nic *nic, struct rx *rx,
  1292. unsigned int *work_done, unsigned int work_to_do)
  1293. {
  1294. struct sk_buff *skb = rx->skb;
  1295. struct rfd *rfd = (struct rfd *)skb->data;
  1296. u16 rfd_status, actual_size;
  1297. if(unlikely(work_done && *work_done >= work_to_do))
  1298. return -EAGAIN;
  1299. /* Need to sync before taking a peek at cb_complete bit */
  1300. pci_dma_sync_single_for_cpu(nic->pdev, rx->dma_addr,
  1301. sizeof(struct rfd), PCI_DMA_FROMDEVICE);
  1302. rfd_status = le16_to_cpu(rfd->status);
  1303. DPRINTK(RX_STATUS, DEBUG, "status=0x%04X\n", rfd_status);
  1304. /* If data isn't ready, nothing to indicate */
  1305. if(unlikely(!(rfd_status & cb_complete)))
  1306. return -ENODATA;
  1307. /* Get actual data size */
  1308. actual_size = le16_to_cpu(rfd->actual_size) & 0x3FFF;
  1309. if(unlikely(actual_size > RFD_BUF_LEN - sizeof(struct rfd)))
  1310. actual_size = RFD_BUF_LEN - sizeof(struct rfd);
  1311. /* Get data */
  1312. pci_unmap_single(nic->pdev, rx->dma_addr,
  1313. RFD_BUF_LEN, PCI_DMA_FROMDEVICE);
  1314. /* this allows for a fast restart without re-enabling interrupts */
  1315. if(le16_to_cpu(rfd->command) & cb_el)
  1316. nic->ru_running = RU_SUSPENDED;
  1317. /* Pull off the RFD and put the actual data (minus eth hdr) */
  1318. skb_reserve(skb, sizeof(struct rfd));
  1319. skb_put(skb, actual_size);
  1320. skb->protocol = eth_type_trans(skb, nic->netdev);
  1321. if(unlikely(!(rfd_status & cb_ok))) {
  1322. /* Don't indicate if hardware indicates errors */
  1323. dev_kfree_skb_any(skb);
  1324. } else if(actual_size > ETH_DATA_LEN + VLAN_ETH_HLEN) {
  1325. /* Don't indicate oversized frames */
  1326. nic->rx_over_length_errors++;
  1327. dev_kfree_skb_any(skb);
  1328. } else {
  1329. nic->net_stats.rx_packets++;
  1330. nic->net_stats.rx_bytes += actual_size;
  1331. nic->netdev->last_rx = jiffies;
  1332. netif_receive_skb(skb);
  1333. if(work_done)
  1334. (*work_done)++;
  1335. }
  1336. rx->skb = NULL;
  1337. return 0;
  1338. }
  1339. static inline void e100_rx_clean(struct nic *nic, unsigned int *work_done,
  1340. unsigned int work_to_do)
  1341. {
  1342. struct rx *rx;
  1343. int restart_required = 0;
  1344. struct rx *rx_to_start = NULL;
  1345. /* are we already rnr? then pay attention!!! this ensures that
  1346. * the state machine progression never allows a start with a
  1347. * partially cleaned list, avoiding a race between hardware
  1348. * and rx_to_clean when in NAPI mode */
  1349. if(RU_SUSPENDED == nic->ru_running)
  1350. restart_required = 1;
  1351. /* Indicate newly arrived packets */
  1352. for(rx = nic->rx_to_clean; rx->skb; rx = nic->rx_to_clean = rx->next) {
  1353. int err = e100_rx_indicate(nic, rx, work_done, work_to_do);
  1354. if(-EAGAIN == err) {
  1355. /* hit quota so have more work to do, restart once
  1356. * cleanup is complete */
  1357. restart_required = 0;
  1358. break;
  1359. } else if(-ENODATA == err)
  1360. break; /* No more to clean */
  1361. }
  1362. /* save our starting point as the place we'll restart the receiver */
  1363. if(restart_required)
  1364. rx_to_start = nic->rx_to_clean;
  1365. /* Alloc new skbs to refill list */
  1366. for(rx = nic->rx_to_use; !rx->skb; rx = nic->rx_to_use = rx->next) {
  1367. if(unlikely(e100_rx_alloc_skb(nic, rx)))
  1368. break; /* Better luck next time (see watchdog) */
  1369. }
  1370. if(restart_required) {
  1371. // ack the rnr?
  1372. writeb(stat_ack_rnr, &nic->csr->scb.stat_ack);
  1373. e100_start_receiver(nic, rx_to_start);
  1374. if(work_done)
  1375. (*work_done)++;
  1376. }
  1377. }
  1378. static void e100_rx_clean_list(struct nic *nic)
  1379. {
  1380. struct rx *rx;
  1381. unsigned int i, count = nic->params.rfds.count;
  1382. nic->ru_running = RU_UNINITIALIZED;
  1383. if(nic->rxs) {
  1384. for(rx = nic->rxs, i = 0; i < count; rx++, i++) {
  1385. if(rx->skb) {
  1386. pci_unmap_single(nic->pdev, rx->dma_addr,
  1387. RFD_BUF_LEN, PCI_DMA_FROMDEVICE);
  1388. dev_kfree_skb(rx->skb);
  1389. }
  1390. }
  1391. kfree(nic->rxs);
  1392. nic->rxs = NULL;
  1393. }
  1394. nic->rx_to_use = nic->rx_to_clean = NULL;
  1395. }
  1396. static int e100_rx_alloc_list(struct nic *nic)
  1397. {
  1398. struct rx *rx;
  1399. unsigned int i, count = nic->params.rfds.count;
  1400. nic->rx_to_use = nic->rx_to_clean = NULL;
  1401. nic->ru_running = RU_UNINITIALIZED;
  1402. if(!(nic->rxs = kmalloc(sizeof(struct rx) * count, GFP_ATOMIC)))
  1403. return -ENOMEM;
  1404. memset(nic->rxs, 0, sizeof(struct rx) * count);
  1405. for(rx = nic->rxs, i = 0; i < count; rx++, i++) {
  1406. rx->next = (i + 1 < count) ? rx + 1 : nic->rxs;
  1407. rx->prev = (i == 0) ? nic->rxs + count - 1 : rx - 1;
  1408. if(e100_rx_alloc_skb(nic, rx)) {
  1409. e100_rx_clean_list(nic);
  1410. return -ENOMEM;
  1411. }
  1412. }
  1413. nic->rx_to_use = nic->rx_to_clean = nic->rxs;
  1414. nic->ru_running = RU_SUSPENDED;
  1415. return 0;
  1416. }
  1417. static irqreturn_t e100_intr(int irq, void *dev_id, struct pt_regs *regs)
  1418. {
  1419. struct net_device *netdev = dev_id;
  1420. struct nic *nic = netdev_priv(netdev);
  1421. u8 stat_ack = readb(&nic->csr->scb.stat_ack);
  1422. DPRINTK(INTR, DEBUG, "stat_ack = 0x%02X\n", stat_ack);
  1423. if(stat_ack == stat_ack_not_ours || /* Not our interrupt */
  1424. stat_ack == stat_ack_not_present) /* Hardware is ejected */
  1425. return IRQ_NONE;
  1426. /* Ack interrupt(s) */
  1427. writeb(stat_ack, &nic->csr->scb.stat_ack);
  1428. /* We hit Receive No Resource (RNR); restart RU after cleaning */
  1429. if(stat_ack & stat_ack_rnr)
  1430. nic->ru_running = RU_SUSPENDED;
  1431. if(likely(netif_rx_schedule_prep(netdev))) {
  1432. e100_disable_irq(nic);
  1433. __netif_rx_schedule(netdev);
  1434. }
  1435. return IRQ_HANDLED;
  1436. }
  1437. static int e100_poll(struct net_device *netdev, int *budget)
  1438. {
  1439. struct nic *nic = netdev_priv(netdev);
  1440. unsigned int work_to_do = min(netdev->quota, *budget);
  1441. unsigned int work_done = 0;
  1442. int tx_cleaned;
  1443. e100_rx_clean(nic, &work_done, work_to_do);
  1444. tx_cleaned = e100_tx_clean(nic);
  1445. /* If no Rx and Tx cleanup work was done, exit polling mode. */
  1446. if((!tx_cleaned && (work_done == 0)) || !netif_running(netdev)) {
  1447. netif_rx_complete(netdev);
  1448. e100_enable_irq(nic);
  1449. return 0;
  1450. }
  1451. *budget -= work_done;
  1452. netdev->quota -= work_done;
  1453. return 1;
  1454. }
  1455. #ifdef CONFIG_NET_POLL_CONTROLLER
  1456. static void e100_netpoll(struct net_device *netdev)
  1457. {
  1458. struct nic *nic = netdev_priv(netdev);
  1459. e100_disable_irq(nic);
  1460. e100_intr(nic->pdev->irq, netdev, NULL);
  1461. e100_tx_clean(nic);
  1462. e100_enable_irq(nic);
  1463. }
  1464. #endif
  1465. static struct net_device_stats *e100_get_stats(struct net_device *netdev)
  1466. {
  1467. struct nic *nic = netdev_priv(netdev);
  1468. return &nic->net_stats;
  1469. }
  1470. static int e100_set_mac_address(struct net_device *netdev, void *p)
  1471. {
  1472. struct nic *nic = netdev_priv(netdev);
  1473. struct sockaddr *addr = p;
  1474. if (!is_valid_ether_addr(addr->sa_data))
  1475. return -EADDRNOTAVAIL;
  1476. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  1477. e100_exec_cb(nic, NULL, e100_setup_iaaddr);
  1478. return 0;
  1479. }
  1480. static int e100_change_mtu(struct net_device *netdev, int new_mtu)
  1481. {
  1482. if(new_mtu < ETH_ZLEN || new_mtu > ETH_DATA_LEN)
  1483. return -EINVAL;
  1484. netdev->mtu = new_mtu;
  1485. return 0;
  1486. }
  1487. #ifdef CONFIG_PM
  1488. static int e100_asf(struct nic *nic)
  1489. {
  1490. /* ASF can be enabled from eeprom */
  1491. return((nic->pdev->device >= 0x1050) && (nic->pdev->device <= 0x1057) &&
  1492. (nic->eeprom[eeprom_config_asf] & eeprom_asf) &&
  1493. !(nic->eeprom[eeprom_config_asf] & eeprom_gcl) &&
  1494. ((nic->eeprom[eeprom_smbus_addr] & 0xFF) != 0xFE));
  1495. }
  1496. #endif
  1497. static int e100_up(struct nic *nic)
  1498. {
  1499. int err;
  1500. if((err = e100_rx_alloc_list(nic)))
  1501. return err;
  1502. if((err = e100_alloc_cbs(nic)))
  1503. goto err_rx_clean_list;
  1504. if((err = e100_hw_init(nic)))
  1505. goto err_clean_cbs;
  1506. e100_set_multicast_list(nic->netdev);
  1507. e100_start_receiver(nic, 0);
  1508. mod_timer(&nic->watchdog, jiffies);
  1509. if((err = request_irq(nic->pdev->irq, e100_intr, SA_SHIRQ,
  1510. nic->netdev->name, nic->netdev)))
  1511. goto err_no_irq;
  1512. netif_wake_queue(nic->netdev);
  1513. netif_poll_enable(nic->netdev);
  1514. /* enable ints _after_ enabling poll, preventing a race between
  1515. * disable ints+schedule */
  1516. e100_enable_irq(nic);
  1517. return 0;
  1518. err_no_irq:
  1519. del_timer_sync(&nic->watchdog);
  1520. err_clean_cbs:
  1521. e100_clean_cbs(nic);
  1522. err_rx_clean_list:
  1523. e100_rx_clean_list(nic);
  1524. return err;
  1525. }
  1526. static void e100_down(struct nic *nic)
  1527. {
  1528. /* wait here for poll to complete */
  1529. netif_poll_disable(nic->netdev);
  1530. netif_stop_queue(nic->netdev);
  1531. e100_hw_reset(nic);
  1532. free_irq(nic->pdev->irq, nic->netdev);
  1533. del_timer_sync(&nic->watchdog);
  1534. netif_carrier_off(nic->netdev);
  1535. e100_clean_cbs(nic);
  1536. e100_rx_clean_list(nic);
  1537. }
  1538. static void e100_tx_timeout(struct net_device *netdev)
  1539. {
  1540. struct nic *nic = netdev_priv(netdev);
  1541. /* Reset outside of interrupt context, to avoid request_irq
  1542. * in interrupt context */
  1543. schedule_work(&nic->tx_timeout_task);
  1544. }
  1545. static void e100_tx_timeout_task(struct net_device *netdev)
  1546. {
  1547. struct nic *nic = netdev_priv(netdev);
  1548. DPRINTK(TX_ERR, DEBUG, "scb.status=0x%02X\n",
  1549. readb(&nic->csr->scb.status));
  1550. e100_down(netdev_priv(netdev));
  1551. e100_up(netdev_priv(netdev));
  1552. }
  1553. static int e100_loopback_test(struct nic *nic, enum loopback loopback_mode)
  1554. {
  1555. int err;
  1556. struct sk_buff *skb;
  1557. /* Use driver resources to perform internal MAC or PHY
  1558. * loopback test. A single packet is prepared and transmitted
  1559. * in loopback mode, and the test passes if the received
  1560. * packet compares byte-for-byte to the transmitted packet. */
  1561. if((err = e100_rx_alloc_list(nic)))
  1562. return err;
  1563. if((err = e100_alloc_cbs(nic)))
  1564. goto err_clean_rx;
  1565. /* ICH PHY loopback is broken so do MAC loopback instead */
  1566. if(nic->flags & ich && loopback_mode == lb_phy)
  1567. loopback_mode = lb_mac;
  1568. nic->loopback = loopback_mode;
  1569. if((err = e100_hw_init(nic)))
  1570. goto err_loopback_none;
  1571. if(loopback_mode == lb_phy)
  1572. mdio_write(nic->netdev, nic->mii.phy_id, MII_BMCR,
  1573. BMCR_LOOPBACK);
  1574. e100_start_receiver(nic, 0);
  1575. if(!(skb = dev_alloc_skb(ETH_DATA_LEN))) {
  1576. err = -ENOMEM;
  1577. goto err_loopback_none;
  1578. }
  1579. skb_put(skb, ETH_DATA_LEN);
  1580. memset(skb->data, 0xFF, ETH_DATA_LEN);
  1581. e100_xmit_frame(skb, nic->netdev);
  1582. msleep(10);
  1583. if(memcmp(nic->rx_to_clean->skb->data + sizeof(struct rfd),
  1584. skb->data, ETH_DATA_LEN))
  1585. err = -EAGAIN;
  1586. err_loopback_none:
  1587. mdio_write(nic->netdev, nic->mii.phy_id, MII_BMCR, 0);
  1588. nic->loopback = lb_none;
  1589. e100_hw_init(nic);
  1590. e100_clean_cbs(nic);
  1591. err_clean_rx:
  1592. e100_rx_clean_list(nic);
  1593. return err;
  1594. }
  1595. #define MII_LED_CONTROL 0x1B
  1596. static void e100_blink_led(unsigned long data)
  1597. {
  1598. struct nic *nic = (struct nic *)data;
  1599. enum led_state {
  1600. led_on = 0x01,
  1601. led_off = 0x04,
  1602. led_on_559 = 0x05,
  1603. led_on_557 = 0x07,
  1604. };
  1605. nic->leds = (nic->leds & led_on) ? led_off :
  1606. (nic->mac < mac_82559_D101M) ? led_on_557 : led_on_559;
  1607. mdio_write(nic->netdev, nic->mii.phy_id, MII_LED_CONTROL, nic->leds);
  1608. mod_timer(&nic->blink_timer, jiffies + HZ / 4);
  1609. }
  1610. static int e100_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
  1611. {
  1612. struct nic *nic = netdev_priv(netdev);
  1613. return mii_ethtool_gset(&nic->mii, cmd);
  1614. }
  1615. static int e100_set_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
  1616. {
  1617. struct nic *nic = netdev_priv(netdev);
  1618. int err;
  1619. mdio_write(netdev, nic->mii.phy_id, MII_BMCR, BMCR_RESET);
  1620. err = mii_ethtool_sset(&nic->mii, cmd);
  1621. e100_exec_cb(nic, NULL, e100_configure);
  1622. return err;
  1623. }
  1624. static void e100_get_drvinfo(struct net_device *netdev,
  1625. struct ethtool_drvinfo *info)
  1626. {
  1627. struct nic *nic = netdev_priv(netdev);
  1628. strcpy(info->driver, DRV_NAME);
  1629. strcpy(info->version, DRV_VERSION);
  1630. strcpy(info->fw_version, "N/A");
  1631. strcpy(info->bus_info, pci_name(nic->pdev));
  1632. }
  1633. static int e100_get_regs_len(struct net_device *netdev)
  1634. {
  1635. struct nic *nic = netdev_priv(netdev);
  1636. #define E100_PHY_REGS 0x1C
  1637. #define E100_REGS_LEN 1 + E100_PHY_REGS + \
  1638. sizeof(nic->mem->dump_buf) / sizeof(u32)
  1639. return E100_REGS_LEN * sizeof(u32);
  1640. }
  1641. static void e100_get_regs(struct net_device *netdev,
  1642. struct ethtool_regs *regs, void *p)
  1643. {
  1644. struct nic *nic = netdev_priv(netdev);
  1645. u32 *buff = p;
  1646. int i;
  1647. regs->version = (1 << 24) | nic->rev_id;
  1648. buff[0] = readb(&nic->csr->scb.cmd_hi) << 24 |
  1649. readb(&nic->csr->scb.cmd_lo) << 16 |
  1650. readw(&nic->csr->scb.status);
  1651. for(i = E100_PHY_REGS; i >= 0; i--)
  1652. buff[1 + E100_PHY_REGS - i] =
  1653. mdio_read(netdev, nic->mii.phy_id, i);
  1654. memset(nic->mem->dump_buf, 0, sizeof(nic->mem->dump_buf));
  1655. e100_exec_cb(nic, NULL, e100_dump);
  1656. msleep(10);
  1657. memcpy(&buff[2 + E100_PHY_REGS], nic->mem->dump_buf,
  1658. sizeof(nic->mem->dump_buf));
  1659. }
  1660. static void e100_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  1661. {
  1662. struct nic *nic = netdev_priv(netdev);
  1663. wol->supported = (nic->mac >= mac_82558_D101_A4) ? WAKE_MAGIC : 0;
  1664. wol->wolopts = (nic->flags & wol_magic) ? WAKE_MAGIC : 0;
  1665. }
  1666. static int e100_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  1667. {
  1668. struct nic *nic = netdev_priv(netdev);
  1669. if(wol->wolopts != WAKE_MAGIC && wol->wolopts != 0)
  1670. return -EOPNOTSUPP;
  1671. if(wol->wolopts)
  1672. nic->flags |= wol_magic;
  1673. else
  1674. nic->flags &= ~wol_magic;
  1675. e100_exec_cb(nic, NULL, e100_configure);
  1676. return 0;
  1677. }
  1678. static u32 e100_get_msglevel(struct net_device *netdev)
  1679. {
  1680. struct nic *nic = netdev_priv(netdev);
  1681. return nic->msg_enable;
  1682. }
  1683. static void e100_set_msglevel(struct net_device *netdev, u32 value)
  1684. {
  1685. struct nic *nic = netdev_priv(netdev);
  1686. nic->msg_enable = value;
  1687. }
  1688. static int e100_nway_reset(struct net_device *netdev)
  1689. {
  1690. struct nic *nic = netdev_priv(netdev);
  1691. return mii_nway_restart(&nic->mii);
  1692. }
  1693. static u32 e100_get_link(struct net_device *netdev)
  1694. {
  1695. struct nic *nic = netdev_priv(netdev);
  1696. return mii_link_ok(&nic->mii);
  1697. }
  1698. static int e100_get_eeprom_len(struct net_device *netdev)
  1699. {
  1700. struct nic *nic = netdev_priv(netdev);
  1701. return nic->eeprom_wc << 1;
  1702. }
  1703. #define E100_EEPROM_MAGIC 0x1234
  1704. static int e100_get_eeprom(struct net_device *netdev,
  1705. struct ethtool_eeprom *eeprom, u8 *bytes)
  1706. {
  1707. struct nic *nic = netdev_priv(netdev);
  1708. eeprom->magic = E100_EEPROM_MAGIC;
  1709. memcpy(bytes, &((u8 *)nic->eeprom)[eeprom->offset], eeprom->len);
  1710. return 0;
  1711. }
  1712. static int e100_set_eeprom(struct net_device *netdev,
  1713. struct ethtool_eeprom *eeprom, u8 *bytes)
  1714. {
  1715. struct nic *nic = netdev_priv(netdev);
  1716. if(eeprom->magic != E100_EEPROM_MAGIC)
  1717. return -EINVAL;
  1718. memcpy(&((u8 *)nic->eeprom)[eeprom->offset], bytes, eeprom->len);
  1719. return e100_eeprom_save(nic, eeprom->offset >> 1,
  1720. (eeprom->len >> 1) + 1);
  1721. }
  1722. static void e100_get_ringparam(struct net_device *netdev,
  1723. struct ethtool_ringparam *ring)
  1724. {
  1725. struct nic *nic = netdev_priv(netdev);
  1726. struct param_range *rfds = &nic->params.rfds;
  1727. struct param_range *cbs = &nic->params.cbs;
  1728. ring->rx_max_pending = rfds->max;
  1729. ring->tx_max_pending = cbs->max;
  1730. ring->rx_mini_max_pending = 0;
  1731. ring->rx_jumbo_max_pending = 0;
  1732. ring->rx_pending = rfds->count;
  1733. ring->tx_pending = cbs->count;
  1734. ring->rx_mini_pending = 0;
  1735. ring->rx_jumbo_pending = 0;
  1736. }
  1737. static int e100_set_ringparam(struct net_device *netdev,
  1738. struct ethtool_ringparam *ring)
  1739. {
  1740. struct nic *nic = netdev_priv(netdev);
  1741. struct param_range *rfds = &nic->params.rfds;
  1742. struct param_range *cbs = &nic->params.cbs;
  1743. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  1744. return -EINVAL;
  1745. if(netif_running(netdev))
  1746. e100_down(nic);
  1747. rfds->count = max(ring->rx_pending, rfds->min);
  1748. rfds->count = min(rfds->count, rfds->max);
  1749. cbs->count = max(ring->tx_pending, cbs->min);
  1750. cbs->count = min(cbs->count, cbs->max);
  1751. DPRINTK(DRV, INFO, "Ring Param settings: rx: %d, tx %d\n",
  1752. rfds->count, cbs->count);
  1753. if(netif_running(netdev))
  1754. e100_up(nic);
  1755. return 0;
  1756. }
  1757. static const char e100_gstrings_test[][ETH_GSTRING_LEN] = {
  1758. "Link test (on/offline)",
  1759. "Eeprom test (on/offline)",
  1760. "Self test (offline)",
  1761. "Mac loopback (offline)",
  1762. "Phy loopback (offline)",
  1763. };
  1764. #define E100_TEST_LEN sizeof(e100_gstrings_test) / ETH_GSTRING_LEN
  1765. static int e100_diag_test_count(struct net_device *netdev)
  1766. {
  1767. return E100_TEST_LEN;
  1768. }
  1769. static void e100_diag_test(struct net_device *netdev,
  1770. struct ethtool_test *test, u64 *data)
  1771. {
  1772. struct ethtool_cmd cmd;
  1773. struct nic *nic = netdev_priv(netdev);
  1774. int i, err;
  1775. memset(data, 0, E100_TEST_LEN * sizeof(u64));
  1776. data[0] = !mii_link_ok(&nic->mii);
  1777. data[1] = e100_eeprom_load(nic);
  1778. if(test->flags & ETH_TEST_FL_OFFLINE) {
  1779. /* save speed, duplex & autoneg settings */
  1780. err = mii_ethtool_gset(&nic->mii, &cmd);
  1781. if(netif_running(netdev))
  1782. e100_down(nic);
  1783. data[2] = e100_self_test(nic);
  1784. data[3] = e100_loopback_test(nic, lb_mac);
  1785. data[4] = e100_loopback_test(nic, lb_phy);
  1786. /* restore speed, duplex & autoneg settings */
  1787. err = mii_ethtool_sset(&nic->mii, &cmd);
  1788. if(netif_running(netdev))
  1789. e100_up(nic);
  1790. }
  1791. for(i = 0; i < E100_TEST_LEN; i++)
  1792. test->flags |= data[i] ? ETH_TEST_FL_FAILED : 0;
  1793. msleep_interruptible(4 * 1000);
  1794. }
  1795. static int e100_phys_id(struct net_device *netdev, u32 data)
  1796. {
  1797. struct nic *nic = netdev_priv(netdev);
  1798. if(!data || data > (u32)(MAX_SCHEDULE_TIMEOUT / HZ))
  1799. data = (u32)(MAX_SCHEDULE_TIMEOUT / HZ);
  1800. mod_timer(&nic->blink_timer, jiffies);
  1801. msleep_interruptible(data * 1000);
  1802. del_timer_sync(&nic->blink_timer);
  1803. mdio_write(netdev, nic->mii.phy_id, MII_LED_CONTROL, 0);
  1804. return 0;
  1805. }
  1806. static const char e100_gstrings_stats[][ETH_GSTRING_LEN] = {
  1807. "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
  1808. "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
  1809. "rx_length_errors", "rx_over_errors", "rx_crc_errors",
  1810. "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
  1811. "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
  1812. "tx_heartbeat_errors", "tx_window_errors",
  1813. /* device-specific stats */
  1814. "tx_deferred", "tx_single_collisions", "tx_multi_collisions",
  1815. "tx_flow_control_pause", "rx_flow_control_pause",
  1816. "rx_flow_control_unsupported", "tx_tco_packets", "rx_tco_packets",
  1817. };
  1818. #define E100_NET_STATS_LEN 21
  1819. #define E100_STATS_LEN sizeof(e100_gstrings_stats) / ETH_GSTRING_LEN
  1820. static int e100_get_stats_count(struct net_device *netdev)
  1821. {
  1822. return E100_STATS_LEN;
  1823. }
  1824. static void e100_get_ethtool_stats(struct net_device *netdev,
  1825. struct ethtool_stats *stats, u64 *data)
  1826. {
  1827. struct nic *nic = netdev_priv(netdev);
  1828. int i;
  1829. for(i = 0; i < E100_NET_STATS_LEN; i++)
  1830. data[i] = ((unsigned long *)&nic->net_stats)[i];
  1831. data[i++] = nic->tx_deferred;
  1832. data[i++] = nic->tx_single_collisions;
  1833. data[i++] = nic->tx_multiple_collisions;
  1834. data[i++] = nic->tx_fc_pause;
  1835. data[i++] = nic->rx_fc_pause;
  1836. data[i++] = nic->rx_fc_unsupported;
  1837. data[i++] = nic->tx_tco_frames;
  1838. data[i++] = nic->rx_tco_frames;
  1839. }
  1840. static void e100_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
  1841. {
  1842. switch(stringset) {
  1843. case ETH_SS_TEST:
  1844. memcpy(data, *e100_gstrings_test, sizeof(e100_gstrings_test));
  1845. break;
  1846. case ETH_SS_STATS:
  1847. memcpy(data, *e100_gstrings_stats, sizeof(e100_gstrings_stats));
  1848. break;
  1849. }
  1850. }
  1851. static struct ethtool_ops e100_ethtool_ops = {
  1852. .get_settings = e100_get_settings,
  1853. .set_settings = e100_set_settings,
  1854. .get_drvinfo = e100_get_drvinfo,
  1855. .get_regs_len = e100_get_regs_len,
  1856. .get_regs = e100_get_regs,
  1857. .get_wol = e100_get_wol,
  1858. .set_wol = e100_set_wol,
  1859. .get_msglevel = e100_get_msglevel,
  1860. .set_msglevel = e100_set_msglevel,
  1861. .nway_reset = e100_nway_reset,
  1862. .get_link = e100_get_link,
  1863. .get_eeprom_len = e100_get_eeprom_len,
  1864. .get_eeprom = e100_get_eeprom,
  1865. .set_eeprom = e100_set_eeprom,
  1866. .get_ringparam = e100_get_ringparam,
  1867. .set_ringparam = e100_set_ringparam,
  1868. .self_test_count = e100_diag_test_count,
  1869. .self_test = e100_diag_test,
  1870. .get_strings = e100_get_strings,
  1871. .phys_id = e100_phys_id,
  1872. .get_stats_count = e100_get_stats_count,
  1873. .get_ethtool_stats = e100_get_ethtool_stats,
  1874. .get_perm_addr = ethtool_op_get_perm_addr,
  1875. };
  1876. static int e100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  1877. {
  1878. struct nic *nic = netdev_priv(netdev);
  1879. return generic_mii_ioctl(&nic->mii, if_mii(ifr), cmd, NULL);
  1880. }
  1881. static int e100_alloc(struct nic *nic)
  1882. {
  1883. nic->mem = pci_alloc_consistent(nic->pdev, sizeof(struct mem),
  1884. &nic->dma_addr);
  1885. return nic->mem ? 0 : -ENOMEM;
  1886. }
  1887. static void e100_free(struct nic *nic)
  1888. {
  1889. if(nic->mem) {
  1890. pci_free_consistent(nic->pdev, sizeof(struct mem),
  1891. nic->mem, nic->dma_addr);
  1892. nic->mem = NULL;
  1893. }
  1894. }
  1895. static int e100_open(struct net_device *netdev)
  1896. {
  1897. struct nic *nic = netdev_priv(netdev);
  1898. int err = 0;
  1899. netif_carrier_off(netdev);
  1900. if((err = e100_up(nic)))
  1901. DPRINTK(IFUP, ERR, "Cannot open interface, aborting.\n");
  1902. return err;
  1903. }
  1904. static int e100_close(struct net_device *netdev)
  1905. {
  1906. e100_down(netdev_priv(netdev));
  1907. return 0;
  1908. }
  1909. static int __devinit e100_probe(struct pci_dev *pdev,
  1910. const struct pci_device_id *ent)
  1911. {
  1912. struct net_device *netdev;
  1913. struct nic *nic;
  1914. int err;
  1915. if(!(netdev = alloc_etherdev(sizeof(struct nic)))) {
  1916. if(((1 << debug) - 1) & NETIF_MSG_PROBE)
  1917. printk(KERN_ERR PFX "Etherdev alloc failed, abort.\n");
  1918. return -ENOMEM;
  1919. }
  1920. netdev->open = e100_open;
  1921. netdev->stop = e100_close;
  1922. netdev->hard_start_xmit = e100_xmit_frame;
  1923. netdev->get_stats = e100_get_stats;
  1924. netdev->set_multicast_list = e100_set_multicast_list;
  1925. netdev->set_mac_address = e100_set_mac_address;
  1926. netdev->change_mtu = e100_change_mtu;
  1927. netdev->do_ioctl = e100_do_ioctl;
  1928. SET_ETHTOOL_OPS(netdev, &e100_ethtool_ops);
  1929. netdev->tx_timeout = e100_tx_timeout;
  1930. netdev->watchdog_timeo = E100_WATCHDOG_PERIOD;
  1931. netdev->poll = e100_poll;
  1932. netdev->weight = E100_NAPI_WEIGHT;
  1933. #ifdef CONFIG_NET_POLL_CONTROLLER
  1934. netdev->poll_controller = e100_netpoll;
  1935. #endif
  1936. strcpy(netdev->name, pci_name(pdev));
  1937. nic = netdev_priv(netdev);
  1938. nic->netdev = netdev;
  1939. nic->pdev = pdev;
  1940. nic->msg_enable = (1 << debug) - 1;
  1941. pci_set_drvdata(pdev, netdev);
  1942. if((err = pci_enable_device(pdev))) {
  1943. DPRINTK(PROBE, ERR, "Cannot enable PCI device, aborting.\n");
  1944. goto err_out_free_dev;
  1945. }
  1946. if(!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  1947. DPRINTK(PROBE, ERR, "Cannot find proper PCI device "
  1948. "base address, aborting.\n");
  1949. err = -ENODEV;
  1950. goto err_out_disable_pdev;
  1951. }
  1952. if((err = pci_request_regions(pdev, DRV_NAME))) {
  1953. DPRINTK(PROBE, ERR, "Cannot obtain PCI resources, aborting.\n");
  1954. goto err_out_disable_pdev;
  1955. }
  1956. if((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK))) {
  1957. DPRINTK(PROBE, ERR, "No usable DMA configuration, aborting.\n");
  1958. goto err_out_free_res;
  1959. }
  1960. SET_MODULE_OWNER(netdev);
  1961. SET_NETDEV_DEV(netdev, &pdev->dev);
  1962. nic->csr = ioremap(pci_resource_start(pdev, 0), sizeof(struct csr));
  1963. if(!nic->csr) {
  1964. DPRINTK(PROBE, ERR, "Cannot map device registers, aborting.\n");
  1965. err = -ENOMEM;
  1966. goto err_out_free_res;
  1967. }
  1968. if(ent->driver_data)
  1969. nic->flags |= ich;
  1970. else
  1971. nic->flags &= ~ich;
  1972. e100_get_defaults(nic);
  1973. /* locks must be initialized before calling hw_reset */
  1974. spin_lock_init(&nic->cb_lock);
  1975. spin_lock_init(&nic->cmd_lock);
  1976. /* Reset the device before pci_set_master() in case device is in some
  1977. * funky state and has an interrupt pending - hint: we don't have the
  1978. * interrupt handler registered yet. */
  1979. e100_hw_reset(nic);
  1980. pci_set_master(pdev);
  1981. init_timer(&nic->watchdog);
  1982. nic->watchdog.function = e100_watchdog;
  1983. nic->watchdog.data = (unsigned long)nic;
  1984. init_timer(&nic->blink_timer);
  1985. nic->blink_timer.function = e100_blink_led;
  1986. nic->blink_timer.data = (unsigned long)nic;
  1987. INIT_WORK(&nic->tx_timeout_task,
  1988. (void (*)(void *))e100_tx_timeout_task, netdev);
  1989. if((err = e100_alloc(nic))) {
  1990. DPRINTK(PROBE, ERR, "Cannot alloc driver memory, aborting.\n");
  1991. goto err_out_iounmap;
  1992. }
  1993. if((err = e100_eeprom_load(nic)))
  1994. goto err_out_free;
  1995. e100_phy_init(nic);
  1996. memcpy(netdev->dev_addr, nic->eeprom, ETH_ALEN);
  1997. memcpy(netdev->perm_addr, nic->eeprom, ETH_ALEN);
  1998. if(!is_valid_ether_addr(netdev->perm_addr)) {
  1999. DPRINTK(PROBE, ERR, "Invalid MAC address from "
  2000. "EEPROM, aborting.\n");
  2001. err = -EAGAIN;
  2002. goto err_out_free;
  2003. }
  2004. /* Wol magic packet can be enabled from eeprom */
  2005. if((nic->mac >= mac_82558_D101_A4) &&
  2006. (nic->eeprom[eeprom_id] & eeprom_id_wol))
  2007. nic->flags |= wol_magic;
  2008. /* ack any pending wake events, disable PME */
  2009. pci_enable_wake(pdev, 0, 0);
  2010. strcpy(netdev->name, "eth%d");
  2011. if((err = register_netdev(netdev))) {
  2012. DPRINTK(PROBE, ERR, "Cannot register net device, aborting.\n");
  2013. goto err_out_free;
  2014. }
  2015. DPRINTK(PROBE, INFO, "addr 0x%lx, irq %d, "
  2016. "MAC addr %02X:%02X:%02X:%02X:%02X:%02X\n",
  2017. pci_resource_start(pdev, 0), pdev->irq,
  2018. netdev->dev_addr[0], netdev->dev_addr[1], netdev->dev_addr[2],
  2019. netdev->dev_addr[3], netdev->dev_addr[4], netdev->dev_addr[5]);
  2020. return 0;
  2021. err_out_free:
  2022. e100_free(nic);
  2023. err_out_iounmap:
  2024. iounmap(nic->csr);
  2025. err_out_free_res:
  2026. pci_release_regions(pdev);
  2027. err_out_disable_pdev:
  2028. pci_disable_device(pdev);
  2029. err_out_free_dev:
  2030. pci_set_drvdata(pdev, NULL);
  2031. free_netdev(netdev);
  2032. return err;
  2033. }
  2034. static void __devexit e100_remove(struct pci_dev *pdev)
  2035. {
  2036. struct net_device *netdev = pci_get_drvdata(pdev);
  2037. if(netdev) {
  2038. struct nic *nic = netdev_priv(netdev);
  2039. unregister_netdev(netdev);
  2040. e100_free(nic);
  2041. iounmap(nic->csr);
  2042. free_netdev(netdev);
  2043. pci_release_regions(pdev);
  2044. pci_disable_device(pdev);
  2045. pci_set_drvdata(pdev, NULL);
  2046. }
  2047. }
  2048. #ifdef CONFIG_PM
  2049. static int e100_suspend(struct pci_dev *pdev, pm_message_t state)
  2050. {
  2051. struct net_device *netdev = pci_get_drvdata(pdev);
  2052. struct nic *nic = netdev_priv(netdev);
  2053. if(netif_running(netdev))
  2054. e100_down(nic);
  2055. e100_hw_reset(nic);
  2056. netif_device_detach(netdev);
  2057. pci_save_state(pdev);
  2058. pci_enable_wake(pdev, pci_choose_state(pdev, state), nic->flags & (wol_magic | e100_asf(nic)));
  2059. pci_disable_device(pdev);
  2060. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2061. return 0;
  2062. }
  2063. static int e100_resume(struct pci_dev *pdev)
  2064. {
  2065. struct net_device *netdev = pci_get_drvdata(pdev);
  2066. struct nic *nic = netdev_priv(netdev);
  2067. pci_set_power_state(pdev, PCI_D0);
  2068. pci_restore_state(pdev);
  2069. /* ack any pending wake events, disable PME */
  2070. pci_enable_wake(pdev, 0, 0);
  2071. if(e100_hw_init(nic))
  2072. DPRINTK(HW, ERR, "e100_hw_init failed\n");
  2073. netif_device_attach(netdev);
  2074. if(netif_running(netdev))
  2075. e100_up(nic);
  2076. return 0;
  2077. }
  2078. #endif
  2079. static void e100_shutdown(struct pci_dev *pdev)
  2080. {
  2081. struct net_device *netdev = pci_get_drvdata(pdev);
  2082. struct nic *nic = netdev_priv(netdev);
  2083. #ifdef CONFIG_PM
  2084. pci_enable_wake(pdev, 0, nic->flags & (wol_magic | e100_asf(nic)));
  2085. #else
  2086. pci_enable_wake(pdev, 0, nic->flags & (wol_magic));
  2087. #endif
  2088. }
  2089. static struct pci_driver e100_driver = {
  2090. .name = DRV_NAME,
  2091. .id_table = e100_id_table,
  2092. .probe = e100_probe,
  2093. .remove = __devexit_p(e100_remove),
  2094. #ifdef CONFIG_PM
  2095. .suspend = e100_suspend,
  2096. .resume = e100_resume,
  2097. #endif
  2098. .shutdown = e100_shutdown,
  2099. };
  2100. static int __init e100_init_module(void)
  2101. {
  2102. if(((1 << debug) - 1) & NETIF_MSG_DRV) {
  2103. printk(KERN_INFO PFX "%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  2104. printk(KERN_INFO PFX "%s\n", DRV_COPYRIGHT);
  2105. }
  2106. return pci_module_init(&e100_driver);
  2107. }
  2108. static void __exit e100_cleanup_module(void)
  2109. {
  2110. pci_unregister_driver(&e100_driver);
  2111. }
  2112. module_init(e100_init_module);
  2113. module_exit(e100_cleanup_module);