e100.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490
  1. /*******************************************************************************
  2. Copyright(c) 1999 - 2004 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.8-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. static inline int e100_exec_cmd(struct nic *nic, u8 cmd, dma_addr_t dma_addr)
  692. {
  693. unsigned long flags;
  694. unsigned int i;
  695. int err = 0;
  696. spin_lock_irqsave(&nic->cmd_lock, flags);
  697. /* Previous command is accepted when SCB clears */
  698. for(i = 0; i < E100_WAIT_SCB_TIMEOUT; i++) {
  699. if(likely(!readb(&nic->csr->scb.cmd_lo)))
  700. break;
  701. cpu_relax();
  702. if(unlikely(i > (E100_WAIT_SCB_TIMEOUT >> 1)))
  703. udelay(5);
  704. }
  705. if(unlikely(i == E100_WAIT_SCB_TIMEOUT)) {
  706. err = -EAGAIN;
  707. goto err_unlock;
  708. }
  709. if(unlikely(cmd != cuc_resume))
  710. writel(dma_addr, &nic->csr->scb.gen_ptr);
  711. writeb(cmd, &nic->csr->scb.cmd_lo);
  712. err_unlock:
  713. spin_unlock_irqrestore(&nic->cmd_lock, flags);
  714. return err;
  715. }
  716. static inline int e100_exec_cb(struct nic *nic, struct sk_buff *skb,
  717. void (*cb_prepare)(struct nic *, struct cb *, struct sk_buff *))
  718. {
  719. struct cb *cb;
  720. unsigned long flags;
  721. int err = 0;
  722. spin_lock_irqsave(&nic->cb_lock, flags);
  723. if(unlikely(!nic->cbs_avail)) {
  724. err = -ENOMEM;
  725. goto err_unlock;
  726. }
  727. cb = nic->cb_to_use;
  728. nic->cb_to_use = cb->next;
  729. nic->cbs_avail--;
  730. cb->skb = skb;
  731. if(unlikely(!nic->cbs_avail))
  732. err = -ENOSPC;
  733. cb_prepare(nic, cb, skb);
  734. /* Order is important otherwise we'll be in a race with h/w:
  735. * set S-bit in current first, then clear S-bit in previous. */
  736. cb->command |= cpu_to_le16(cb_s);
  737. wmb();
  738. cb->prev->command &= cpu_to_le16(~cb_s);
  739. while(nic->cb_to_send != nic->cb_to_use) {
  740. if(unlikely(e100_exec_cmd(nic, nic->cuc_cmd,
  741. nic->cb_to_send->dma_addr))) {
  742. /* Ok, here's where things get sticky. It's
  743. * possible that we can't schedule the command
  744. * because the controller is too busy, so
  745. * let's just queue the command and try again
  746. * when another command is scheduled. */
  747. if(err == -ENOSPC) {
  748. //request a reset
  749. schedule_work(&nic->tx_timeout_task);
  750. }
  751. break;
  752. } else {
  753. nic->cuc_cmd = cuc_resume;
  754. nic->cb_to_send = nic->cb_to_send->next;
  755. }
  756. }
  757. err_unlock:
  758. spin_unlock_irqrestore(&nic->cb_lock, flags);
  759. return err;
  760. }
  761. static u16 mdio_ctrl(struct nic *nic, u32 addr, u32 dir, u32 reg, u16 data)
  762. {
  763. u32 data_out = 0;
  764. unsigned int i;
  765. writel((reg << 16) | (addr << 21) | dir | data, &nic->csr->mdi_ctrl);
  766. for(i = 0; i < 100; i++) {
  767. udelay(20);
  768. if((data_out = readl(&nic->csr->mdi_ctrl)) & mdi_ready)
  769. break;
  770. }
  771. DPRINTK(HW, DEBUG,
  772. "%s:addr=%d, reg=%d, data_in=0x%04X, data_out=0x%04X\n",
  773. dir == mdi_read ? "READ" : "WRITE", addr, reg, data, data_out);
  774. return (u16)data_out;
  775. }
  776. static int mdio_read(struct net_device *netdev, int addr, int reg)
  777. {
  778. return mdio_ctrl(netdev_priv(netdev), addr, mdi_read, reg, 0);
  779. }
  780. static void mdio_write(struct net_device *netdev, int addr, int reg, int data)
  781. {
  782. mdio_ctrl(netdev_priv(netdev), addr, mdi_write, reg, data);
  783. }
  784. static void e100_get_defaults(struct nic *nic)
  785. {
  786. struct param_range rfds = { .min = 16, .max = 256, .count = 64 };
  787. struct param_range cbs = { .min = 64, .max = 256, .count = 64 };
  788. pci_read_config_byte(nic->pdev, PCI_REVISION_ID, &nic->rev_id);
  789. /* MAC type is encoded as rev ID; exception: ICH is treated as 82559 */
  790. nic->mac = (nic->flags & ich) ? mac_82559_D101M : nic->rev_id;
  791. if(nic->mac == mac_unknown)
  792. nic->mac = mac_82557_D100_A;
  793. nic->params.rfds = rfds;
  794. nic->params.cbs = cbs;
  795. /* Quadwords to DMA into FIFO before starting frame transmit */
  796. nic->tx_threshold = 0xE0;
  797. /* no interrupt for every tx completion, delay = 256us if not 557*/
  798. nic->tx_command = cpu_to_le16(cb_tx | cb_tx_sf |
  799. ((nic->mac >= mac_82558_D101_A4) ? cb_cid : cb_i));
  800. /* Template for a freshly allocated RFD */
  801. nic->blank_rfd.command = cpu_to_le16(cb_el);
  802. nic->blank_rfd.rbd = 0xFFFFFFFF;
  803. nic->blank_rfd.size = cpu_to_le16(VLAN_ETH_FRAME_LEN);
  804. /* MII setup */
  805. nic->mii.phy_id_mask = 0x1F;
  806. nic->mii.reg_num_mask = 0x1F;
  807. nic->mii.dev = nic->netdev;
  808. nic->mii.mdio_read = mdio_read;
  809. nic->mii.mdio_write = mdio_write;
  810. }
  811. static void e100_configure(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  812. {
  813. struct config *config = &cb->u.config;
  814. u8 *c = (u8 *)config;
  815. cb->command = cpu_to_le16(cb_config);
  816. memset(config, 0, sizeof(struct config));
  817. config->byte_count = 0x16; /* bytes in this struct */
  818. config->rx_fifo_limit = 0x8; /* bytes in FIFO before DMA */
  819. config->direct_rx_dma = 0x1; /* reserved */
  820. config->standard_tcb = 0x1; /* 1=standard, 0=extended */
  821. config->standard_stat_counter = 0x1; /* 1=standard, 0=extended */
  822. config->rx_discard_short_frames = 0x1; /* 1=discard, 0=pass */
  823. config->tx_underrun_retry = 0x3; /* # of underrun retries */
  824. config->mii_mode = 0x1; /* 1=MII mode, 0=503 mode */
  825. config->pad10 = 0x6;
  826. config->no_source_addr_insertion = 0x1; /* 1=no, 0=yes */
  827. config->preamble_length = 0x2; /* 0=1, 1=3, 2=7, 3=15 bytes */
  828. config->ifs = 0x6; /* x16 = inter frame spacing */
  829. config->ip_addr_hi = 0xF2; /* ARP IP filter - not used */
  830. config->pad15_1 = 0x1;
  831. config->pad15_2 = 0x1;
  832. config->crs_or_cdt = 0x0; /* 0=CRS only, 1=CRS or CDT */
  833. config->fc_delay_hi = 0x40; /* time delay for fc frame */
  834. config->tx_padding = 0x1; /* 1=pad short frames */
  835. config->fc_priority_threshold = 0x7; /* 7=priority fc disabled */
  836. config->pad18 = 0x1;
  837. config->full_duplex_pin = 0x1; /* 1=examine FDX# pin */
  838. config->pad20_1 = 0x1F;
  839. config->fc_priority_location = 0x1; /* 1=byte#31, 0=byte#19 */
  840. config->pad21_1 = 0x5;
  841. config->adaptive_ifs = nic->adaptive_ifs;
  842. config->loopback = nic->loopback;
  843. if(nic->mii.force_media && nic->mii.full_duplex)
  844. config->full_duplex_force = 0x1; /* 1=force, 0=auto */
  845. if(nic->flags & promiscuous || nic->loopback) {
  846. config->rx_save_bad_frames = 0x1; /* 1=save, 0=discard */
  847. config->rx_discard_short_frames = 0x0; /* 1=discard, 0=save */
  848. config->promiscuous_mode = 0x1; /* 1=on, 0=off */
  849. }
  850. if(nic->flags & multicast_all)
  851. config->multicast_all = 0x1; /* 1=accept, 0=no */
  852. /* disable WoL when up */
  853. if(netif_running(nic->netdev) || !(nic->flags & wol_magic))
  854. config->magic_packet_disable = 0x1; /* 1=off, 0=on */
  855. if(nic->mac >= mac_82558_D101_A4) {
  856. config->fc_disable = 0x1; /* 1=Tx fc off, 0=Tx fc on */
  857. config->mwi_enable = 0x1; /* 1=enable, 0=disable */
  858. config->standard_tcb = 0x0; /* 1=standard, 0=extended */
  859. config->rx_long_ok = 0x1; /* 1=VLANs ok, 0=standard */
  860. if(nic->mac >= mac_82559_D101M)
  861. config->tno_intr = 0x1; /* TCO stats enable */
  862. else
  863. config->standard_stat_counter = 0x0;
  864. }
  865. DPRINTK(HW, DEBUG, "[00-07]=%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
  866. c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7]);
  867. DPRINTK(HW, DEBUG, "[08-15]=%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
  868. c[8], c[9], c[10], c[11], c[12], c[13], c[14], c[15]);
  869. DPRINTK(HW, DEBUG, "[16-23]=%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
  870. c[16], c[17], c[18], c[19], c[20], c[21], c[22], c[23]);
  871. }
  872. static void e100_load_ucode(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  873. {
  874. int i;
  875. static const u32 ucode[UCODE_SIZE] = {
  876. /* NFS packets are misinterpreted as TCO packets and
  877. * incorrectly routed to the BMC over SMBus. This
  878. * microcode patch checks the fragmented IP bit in the
  879. * NFS/UDP header to distinguish between NFS and TCO. */
  880. 0x0EF70E36, 0x1FFF1FFF, 0x1FFF1FFF, 0x1FFF1FFF, 0x1FFF1FFF,
  881. 0x1FFF1FFF, 0x00906E41, 0x00800E3C, 0x00E00E39, 0x00000000,
  882. 0x00906EFD, 0x00900EFD, 0x00E00EF8,
  883. };
  884. if(nic->mac == mac_82551_F || nic->mac == mac_82551_10) {
  885. for(i = 0; i < UCODE_SIZE; i++)
  886. cb->u.ucode[i] = cpu_to_le32(ucode[i]);
  887. cb->command = cpu_to_le16(cb_ucode);
  888. } else
  889. cb->command = cpu_to_le16(cb_nop);
  890. }
  891. static void e100_setup_iaaddr(struct nic *nic, struct cb *cb,
  892. struct sk_buff *skb)
  893. {
  894. cb->command = cpu_to_le16(cb_iaaddr);
  895. memcpy(cb->u.iaaddr, nic->netdev->dev_addr, ETH_ALEN);
  896. }
  897. static void e100_dump(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  898. {
  899. cb->command = cpu_to_le16(cb_dump);
  900. cb->u.dump_buffer_addr = cpu_to_le32(nic->dma_addr +
  901. offsetof(struct mem, dump_buf));
  902. }
  903. #define NCONFIG_AUTO_SWITCH 0x0080
  904. #define MII_NSC_CONG MII_RESV1
  905. #define NSC_CONG_ENABLE 0x0100
  906. #define NSC_CONG_TXREADY 0x0400
  907. #define ADVERTISE_FC_SUPPORTED 0x0400
  908. static int e100_phy_init(struct nic *nic)
  909. {
  910. struct net_device *netdev = nic->netdev;
  911. u32 addr;
  912. u16 bmcr, stat, id_lo, id_hi, cong;
  913. /* Discover phy addr by searching addrs in order {1,0,2,..., 31} */
  914. for(addr = 0; addr < 32; addr++) {
  915. nic->mii.phy_id = (addr == 0) ? 1 : (addr == 1) ? 0 : addr;
  916. bmcr = mdio_read(netdev, nic->mii.phy_id, MII_BMCR);
  917. stat = mdio_read(netdev, nic->mii.phy_id, MII_BMSR);
  918. stat = mdio_read(netdev, nic->mii.phy_id, MII_BMSR);
  919. if(!((bmcr == 0xFFFF) || ((stat == 0) && (bmcr == 0))))
  920. break;
  921. }
  922. DPRINTK(HW, DEBUG, "phy_addr = %d\n", nic->mii.phy_id);
  923. if(addr == 32)
  924. return -EAGAIN;
  925. /* Selected the phy and isolate the rest */
  926. for(addr = 0; addr < 32; addr++) {
  927. if(addr != nic->mii.phy_id) {
  928. mdio_write(netdev, addr, MII_BMCR, BMCR_ISOLATE);
  929. } else {
  930. bmcr = mdio_read(netdev, addr, MII_BMCR);
  931. mdio_write(netdev, addr, MII_BMCR,
  932. bmcr & ~BMCR_ISOLATE);
  933. }
  934. }
  935. /* Get phy ID */
  936. id_lo = mdio_read(netdev, nic->mii.phy_id, MII_PHYSID1);
  937. id_hi = mdio_read(netdev, nic->mii.phy_id, MII_PHYSID2);
  938. nic->phy = (u32)id_hi << 16 | (u32)id_lo;
  939. DPRINTK(HW, DEBUG, "phy ID = 0x%08X\n", nic->phy);
  940. /* Handle National tx phys */
  941. #define NCS_PHY_MODEL_MASK 0xFFF0FFFF
  942. if((nic->phy & NCS_PHY_MODEL_MASK) == phy_nsc_tx) {
  943. /* Disable congestion control */
  944. cong = mdio_read(netdev, nic->mii.phy_id, MII_NSC_CONG);
  945. cong |= NSC_CONG_TXREADY;
  946. cong &= ~NSC_CONG_ENABLE;
  947. mdio_write(netdev, nic->mii.phy_id, MII_NSC_CONG, cong);
  948. }
  949. if((nic->mac >= mac_82550_D102) || ((nic->flags & ich) &&
  950. (mdio_read(netdev, nic->mii.phy_id, MII_TPISTATUS) & 0x8000))) {
  951. /* enable/disable MDI/MDI-X auto-switching.
  952. MDI/MDI-X auto-switching is disabled for 82551ER/QM chips */
  953. if((nic->mac == mac_82551_E) || (nic->mac == mac_82551_F) ||
  954. (nic->mac == mac_82551_10) || (nic->mii.force_media) ||
  955. !(nic->eeprom[eeprom_cnfg_mdix] & eeprom_mdix_enabled))
  956. mdio_write(netdev, nic->mii.phy_id, MII_NCONFIG, 0);
  957. else
  958. mdio_write(netdev, nic->mii.phy_id, MII_NCONFIG, NCONFIG_AUTO_SWITCH);
  959. }
  960. return 0;
  961. }
  962. static int e100_hw_init(struct nic *nic)
  963. {
  964. int err;
  965. e100_hw_reset(nic);
  966. DPRINTK(HW, ERR, "e100_hw_init\n");
  967. if(!in_interrupt() && (err = e100_self_test(nic)))
  968. return err;
  969. if((err = e100_phy_init(nic)))
  970. return err;
  971. if((err = e100_exec_cmd(nic, cuc_load_base, 0)))
  972. return err;
  973. if((err = e100_exec_cmd(nic, ruc_load_base, 0)))
  974. return err;
  975. if((err = e100_exec_cb(nic, NULL, e100_load_ucode)))
  976. return err;
  977. if((err = e100_exec_cb(nic, NULL, e100_configure)))
  978. return err;
  979. if((err = e100_exec_cb(nic, NULL, e100_setup_iaaddr)))
  980. return err;
  981. if((err = e100_exec_cmd(nic, cuc_dump_addr,
  982. nic->dma_addr + offsetof(struct mem, stats))))
  983. return err;
  984. if((err = e100_exec_cmd(nic, cuc_dump_reset, 0)))
  985. return err;
  986. e100_disable_irq(nic);
  987. return 0;
  988. }
  989. static void e100_multi(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  990. {
  991. struct net_device *netdev = nic->netdev;
  992. struct dev_mc_list *list = netdev->mc_list;
  993. u16 i, count = min(netdev->mc_count, E100_MAX_MULTICAST_ADDRS);
  994. cb->command = cpu_to_le16(cb_multi);
  995. cb->u.multi.count = cpu_to_le16(count * ETH_ALEN);
  996. for(i = 0; list && i < count; i++, list = list->next)
  997. memcpy(&cb->u.multi.addr[i*ETH_ALEN], &list->dmi_addr,
  998. ETH_ALEN);
  999. }
  1000. static void e100_set_multicast_list(struct net_device *netdev)
  1001. {
  1002. struct nic *nic = netdev_priv(netdev);
  1003. DPRINTK(HW, DEBUG, "mc_count=%d, flags=0x%04X\n",
  1004. netdev->mc_count, netdev->flags);
  1005. if(netdev->flags & IFF_PROMISC)
  1006. nic->flags |= promiscuous;
  1007. else
  1008. nic->flags &= ~promiscuous;
  1009. if(netdev->flags & IFF_ALLMULTI ||
  1010. netdev->mc_count > E100_MAX_MULTICAST_ADDRS)
  1011. nic->flags |= multicast_all;
  1012. else
  1013. nic->flags &= ~multicast_all;
  1014. e100_exec_cb(nic, NULL, e100_configure);
  1015. e100_exec_cb(nic, NULL, e100_multi);
  1016. }
  1017. static void e100_update_stats(struct nic *nic)
  1018. {
  1019. struct net_device_stats *ns = &nic->net_stats;
  1020. struct stats *s = &nic->mem->stats;
  1021. u32 *complete = (nic->mac < mac_82558_D101_A4) ? &s->fc_xmt_pause :
  1022. (nic->mac < mac_82559_D101M) ? (u32 *)&s->xmt_tco_frames :
  1023. &s->complete;
  1024. /* Device's stats reporting may take several microseconds to
  1025. * complete, so where always waiting for results of the
  1026. * previous command. */
  1027. if(*complete == le32_to_cpu(cuc_dump_reset_complete)) {
  1028. *complete = 0;
  1029. nic->tx_frames = le32_to_cpu(s->tx_good_frames);
  1030. nic->tx_collisions = le32_to_cpu(s->tx_total_collisions);
  1031. ns->tx_aborted_errors += le32_to_cpu(s->tx_max_collisions);
  1032. ns->tx_window_errors += le32_to_cpu(s->tx_late_collisions);
  1033. ns->tx_carrier_errors += le32_to_cpu(s->tx_lost_crs);
  1034. ns->tx_fifo_errors += le32_to_cpu(s->tx_underruns);
  1035. ns->collisions += nic->tx_collisions;
  1036. ns->tx_errors += le32_to_cpu(s->tx_max_collisions) +
  1037. le32_to_cpu(s->tx_lost_crs);
  1038. ns->rx_dropped += le32_to_cpu(s->rx_resource_errors);
  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_errors += le32_to_cpu(s->rx_crc_errors) +
  1046. le32_to_cpu(s->rx_alignment_errors) +
  1047. le32_to_cpu(s->rx_short_frame_errors) +
  1048. le32_to_cpu(s->rx_cdt_errors);
  1049. nic->tx_deferred += le32_to_cpu(s->tx_deferred);
  1050. nic->tx_single_collisions +=
  1051. le32_to_cpu(s->tx_single_collisions);
  1052. nic->tx_multiple_collisions +=
  1053. le32_to_cpu(s->tx_multiple_collisions);
  1054. if(nic->mac >= mac_82558_D101_A4) {
  1055. nic->tx_fc_pause += le32_to_cpu(s->fc_xmt_pause);
  1056. nic->rx_fc_pause += le32_to_cpu(s->fc_rcv_pause);
  1057. nic->rx_fc_unsupported +=
  1058. le32_to_cpu(s->fc_rcv_unsupported);
  1059. if(nic->mac >= mac_82559_D101M) {
  1060. nic->tx_tco_frames +=
  1061. le16_to_cpu(s->xmt_tco_frames);
  1062. nic->rx_tco_frames +=
  1063. le16_to_cpu(s->rcv_tco_frames);
  1064. }
  1065. }
  1066. }
  1067. if(e100_exec_cmd(nic, cuc_dump_reset, 0))
  1068. DPRINTK(TX_ERR, DEBUG, "exec cuc_dump_reset failed\n");
  1069. }
  1070. static void e100_adjust_adaptive_ifs(struct nic *nic, int speed, int duplex)
  1071. {
  1072. /* Adjust inter-frame-spacing (IFS) between two transmits if
  1073. * we're getting collisions on a half-duplex connection. */
  1074. if(duplex == DUPLEX_HALF) {
  1075. u32 prev = nic->adaptive_ifs;
  1076. u32 min_frames = (speed == SPEED_100) ? 1000 : 100;
  1077. if((nic->tx_frames / 32 < nic->tx_collisions) &&
  1078. (nic->tx_frames > min_frames)) {
  1079. if(nic->adaptive_ifs < 60)
  1080. nic->adaptive_ifs += 5;
  1081. } else if (nic->tx_frames < min_frames) {
  1082. if(nic->adaptive_ifs >= 5)
  1083. nic->adaptive_ifs -= 5;
  1084. }
  1085. if(nic->adaptive_ifs != prev)
  1086. e100_exec_cb(nic, NULL, e100_configure);
  1087. }
  1088. }
  1089. static void e100_watchdog(unsigned long data)
  1090. {
  1091. struct nic *nic = (struct nic *)data;
  1092. struct ethtool_cmd cmd;
  1093. DPRINTK(TIMER, DEBUG, "right now = %ld\n", jiffies);
  1094. /* mii library handles link maintenance tasks */
  1095. mii_ethtool_gset(&nic->mii, &cmd);
  1096. if(mii_link_ok(&nic->mii) && !netif_carrier_ok(nic->netdev)) {
  1097. DPRINTK(LINK, INFO, "link up, %sMbps, %s-duplex\n",
  1098. cmd.speed == SPEED_100 ? "100" : "10",
  1099. cmd.duplex == DUPLEX_FULL ? "full" : "half");
  1100. } else if(!mii_link_ok(&nic->mii) && netif_carrier_ok(nic->netdev)) {
  1101. DPRINTK(LINK, INFO, "link down\n");
  1102. }
  1103. mii_check_link(&nic->mii);
  1104. /* Software generated interrupt to recover from (rare) Rx
  1105. * allocation failure.
  1106. * Unfortunately have to use a spinlock to not re-enable interrupts
  1107. * accidentally, due to hardware that shares a register between the
  1108. * interrupt mask bit and the SW Interrupt generation bit */
  1109. spin_lock_irq(&nic->cmd_lock);
  1110. writeb(readb(&nic->csr->scb.cmd_hi) | irq_sw_gen,&nic->csr->scb.cmd_hi);
  1111. spin_unlock_irq(&nic->cmd_lock);
  1112. e100_write_flush(nic);
  1113. e100_update_stats(nic);
  1114. e100_adjust_adaptive_ifs(nic, cmd.speed, cmd.duplex);
  1115. if(nic->mac <= mac_82557_D100_C)
  1116. /* Issue a multicast command to workaround a 557 lock up */
  1117. e100_set_multicast_list(nic->netdev);
  1118. if(nic->flags & ich && cmd.speed==SPEED_10 && cmd.duplex==DUPLEX_HALF)
  1119. /* Need SW workaround for ICH[x] 10Mbps/half duplex Tx hang. */
  1120. nic->flags |= ich_10h_workaround;
  1121. else
  1122. nic->flags &= ~ich_10h_workaround;
  1123. mod_timer(&nic->watchdog, jiffies + E100_WATCHDOG_PERIOD);
  1124. }
  1125. static inline void e100_xmit_prepare(struct nic *nic, struct cb *cb,
  1126. struct sk_buff *skb)
  1127. {
  1128. cb->command = nic->tx_command;
  1129. /* interrupt every 16 packets regardless of delay */
  1130. if((nic->cbs_avail & ~15) == nic->cbs_avail) cb->command |= cb_i;
  1131. cb->u.tcb.tbd_array = cb->dma_addr + offsetof(struct cb, u.tcb.tbd);
  1132. cb->u.tcb.tcb_byte_count = 0;
  1133. cb->u.tcb.threshold = nic->tx_threshold;
  1134. cb->u.tcb.tbd_count = 1;
  1135. cb->u.tcb.tbd.buf_addr = cpu_to_le32(pci_map_single(nic->pdev,
  1136. skb->data, skb->len, PCI_DMA_TODEVICE));
  1137. // check for mapping failure?
  1138. cb->u.tcb.tbd.size = cpu_to_le16(skb->len);
  1139. }
  1140. static int e100_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  1141. {
  1142. struct nic *nic = netdev_priv(netdev);
  1143. int err;
  1144. if(nic->flags & ich_10h_workaround) {
  1145. /* SW workaround for ICH[x] 10Mbps/half duplex Tx hang.
  1146. Issue a NOP command followed by a 1us delay before
  1147. issuing the Tx command. */
  1148. if(e100_exec_cmd(nic, cuc_nop, 0))
  1149. DPRINTK(TX_ERR, DEBUG, "exec cuc_nop failed\n");
  1150. udelay(1);
  1151. }
  1152. err = e100_exec_cb(nic, skb, e100_xmit_prepare);
  1153. switch(err) {
  1154. case -ENOSPC:
  1155. /* We queued the skb, but now we're out of space. */
  1156. DPRINTK(TX_ERR, DEBUG, "No space for CB\n");
  1157. netif_stop_queue(netdev);
  1158. break;
  1159. case -ENOMEM:
  1160. /* This is a hard error - log it. */
  1161. DPRINTK(TX_ERR, DEBUG, "Out of Tx resources, returning skb\n");
  1162. netif_stop_queue(netdev);
  1163. return 1;
  1164. }
  1165. netdev->trans_start = jiffies;
  1166. return 0;
  1167. }
  1168. static inline int e100_tx_clean(struct nic *nic)
  1169. {
  1170. struct cb *cb;
  1171. int tx_cleaned = 0;
  1172. spin_lock(&nic->cb_lock);
  1173. DPRINTK(TX_DONE, DEBUG, "cb->status = 0x%04X\n",
  1174. nic->cb_to_clean->status);
  1175. /* Clean CBs marked complete */
  1176. for(cb = nic->cb_to_clean;
  1177. cb->status & cpu_to_le16(cb_complete);
  1178. cb = nic->cb_to_clean = cb->next) {
  1179. if(likely(cb->skb != NULL)) {
  1180. nic->net_stats.tx_packets++;
  1181. nic->net_stats.tx_bytes += cb->skb->len;
  1182. pci_unmap_single(nic->pdev,
  1183. le32_to_cpu(cb->u.tcb.tbd.buf_addr),
  1184. le16_to_cpu(cb->u.tcb.tbd.size),
  1185. PCI_DMA_TODEVICE);
  1186. dev_kfree_skb_any(cb->skb);
  1187. cb->skb = NULL;
  1188. tx_cleaned = 1;
  1189. }
  1190. cb->status = 0;
  1191. nic->cbs_avail++;
  1192. }
  1193. spin_unlock(&nic->cb_lock);
  1194. /* Recover from running out of Tx resources in xmit_frame */
  1195. if(unlikely(tx_cleaned && netif_queue_stopped(nic->netdev)))
  1196. netif_wake_queue(nic->netdev);
  1197. return tx_cleaned;
  1198. }
  1199. static void e100_clean_cbs(struct nic *nic)
  1200. {
  1201. if(nic->cbs) {
  1202. while(nic->cbs_avail != nic->params.cbs.count) {
  1203. struct cb *cb = nic->cb_to_clean;
  1204. if(cb->skb) {
  1205. pci_unmap_single(nic->pdev,
  1206. le32_to_cpu(cb->u.tcb.tbd.buf_addr),
  1207. le16_to_cpu(cb->u.tcb.tbd.size),
  1208. PCI_DMA_TODEVICE);
  1209. dev_kfree_skb(cb->skb);
  1210. }
  1211. nic->cb_to_clean = nic->cb_to_clean->next;
  1212. nic->cbs_avail++;
  1213. }
  1214. pci_free_consistent(nic->pdev,
  1215. sizeof(struct cb) * nic->params.cbs.count,
  1216. nic->cbs, nic->cbs_dma_addr);
  1217. nic->cbs = NULL;
  1218. nic->cbs_avail = 0;
  1219. }
  1220. nic->cuc_cmd = cuc_start;
  1221. nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean =
  1222. nic->cbs;
  1223. }
  1224. static int e100_alloc_cbs(struct nic *nic)
  1225. {
  1226. struct cb *cb;
  1227. unsigned int i, count = nic->params.cbs.count;
  1228. nic->cuc_cmd = cuc_start;
  1229. nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean = NULL;
  1230. nic->cbs_avail = 0;
  1231. nic->cbs = pci_alloc_consistent(nic->pdev,
  1232. sizeof(struct cb) * count, &nic->cbs_dma_addr);
  1233. if(!nic->cbs)
  1234. return -ENOMEM;
  1235. for(cb = nic->cbs, i = 0; i < count; cb++, i++) {
  1236. cb->next = (i + 1 < count) ? cb + 1 : nic->cbs;
  1237. cb->prev = (i == 0) ? nic->cbs + count - 1 : cb - 1;
  1238. cb->dma_addr = nic->cbs_dma_addr + i * sizeof(struct cb);
  1239. cb->link = cpu_to_le32(nic->cbs_dma_addr +
  1240. ((i+1) % count) * sizeof(struct cb));
  1241. cb->skb = NULL;
  1242. }
  1243. nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean = nic->cbs;
  1244. nic->cbs_avail = count;
  1245. return 0;
  1246. }
  1247. static inline void e100_start_receiver(struct nic *nic, struct rx *rx)
  1248. {
  1249. if(!nic->rxs) return;
  1250. if(RU_SUSPENDED != nic->ru_running) return;
  1251. /* handle init time starts */
  1252. if(!rx) rx = nic->rxs;
  1253. /* (Re)start RU if suspended or idle and RFA is non-NULL */
  1254. if(rx->skb) {
  1255. e100_exec_cmd(nic, ruc_start, rx->dma_addr);
  1256. nic->ru_running = RU_RUNNING;
  1257. }
  1258. }
  1259. #define RFD_BUF_LEN (sizeof(struct rfd) + VLAN_ETH_FRAME_LEN)
  1260. static inline int e100_rx_alloc_skb(struct nic *nic, struct rx *rx)
  1261. {
  1262. if(!(rx->skb = dev_alloc_skb(RFD_BUF_LEN + NET_IP_ALIGN)))
  1263. return -ENOMEM;
  1264. /* Align, init, and map the RFD. */
  1265. rx->skb->dev = nic->netdev;
  1266. skb_reserve(rx->skb, NET_IP_ALIGN);
  1267. memcpy(rx->skb->data, &nic->blank_rfd, sizeof(struct rfd));
  1268. rx->dma_addr = pci_map_single(nic->pdev, rx->skb->data,
  1269. RFD_BUF_LEN, PCI_DMA_BIDIRECTIONAL);
  1270. if(pci_dma_mapping_error(rx->dma_addr)) {
  1271. dev_kfree_skb_any(rx->skb);
  1272. rx->skb = 0;
  1273. rx->dma_addr = 0;
  1274. return -ENOMEM;
  1275. }
  1276. /* Link the RFD to end of RFA by linking previous RFD to
  1277. * this one, and clearing EL bit of previous. */
  1278. if(rx->prev->skb) {
  1279. struct rfd *prev_rfd = (struct rfd *)rx->prev->skb->data;
  1280. put_unaligned(cpu_to_le32(rx->dma_addr),
  1281. (u32 *)&prev_rfd->link);
  1282. wmb();
  1283. prev_rfd->command &= ~cpu_to_le16(cb_el);
  1284. pci_dma_sync_single_for_device(nic->pdev, rx->prev->dma_addr,
  1285. sizeof(struct rfd), PCI_DMA_TODEVICE);
  1286. }
  1287. return 0;
  1288. }
  1289. static inline int e100_rx_indicate(struct nic *nic, struct rx *rx,
  1290. unsigned int *work_done, unsigned int work_to_do)
  1291. {
  1292. struct sk_buff *skb = rx->skb;
  1293. struct rfd *rfd = (struct rfd *)skb->data;
  1294. u16 rfd_status, actual_size;
  1295. if(unlikely(work_done && *work_done >= work_to_do))
  1296. return -EAGAIN;
  1297. /* Need to sync before taking a peek at cb_complete bit */
  1298. pci_dma_sync_single_for_cpu(nic->pdev, rx->dma_addr,
  1299. sizeof(struct rfd), PCI_DMA_FROMDEVICE);
  1300. rfd_status = le16_to_cpu(rfd->status);
  1301. DPRINTK(RX_STATUS, DEBUG, "status=0x%04X\n", rfd_status);
  1302. /* If data isn't ready, nothing to indicate */
  1303. if(unlikely(!(rfd_status & cb_complete)))
  1304. return -ENODATA;
  1305. /* Get actual data size */
  1306. actual_size = le16_to_cpu(rfd->actual_size) & 0x3FFF;
  1307. if(unlikely(actual_size > RFD_BUF_LEN - sizeof(struct rfd)))
  1308. actual_size = RFD_BUF_LEN - sizeof(struct rfd);
  1309. /* Get data */
  1310. pci_unmap_single(nic->pdev, rx->dma_addr,
  1311. RFD_BUF_LEN, PCI_DMA_FROMDEVICE);
  1312. /* this allows for a fast restart without re-enabling interrupts */
  1313. if(le16_to_cpu(rfd->command) & cb_el)
  1314. nic->ru_running = RU_SUSPENDED;
  1315. /* Pull off the RFD and put the actual data (minus eth hdr) */
  1316. skb_reserve(skb, sizeof(struct rfd));
  1317. skb_put(skb, actual_size);
  1318. skb->protocol = eth_type_trans(skb, nic->netdev);
  1319. if(unlikely(!(rfd_status & cb_ok))) {
  1320. /* Don't indicate if hardware indicates errors */
  1321. nic->net_stats.rx_dropped++;
  1322. dev_kfree_skb_any(skb);
  1323. } else if(actual_size > nic->netdev->mtu + VLAN_ETH_HLEN) {
  1324. /* Don't indicate oversized frames */
  1325. nic->rx_over_length_errors++;
  1326. nic->net_stats.rx_dropped++;
  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. }
  1794. static int e100_phys_id(struct net_device *netdev, u32 data)
  1795. {
  1796. struct nic *nic = netdev_priv(netdev);
  1797. if(!data || data > (u32)(MAX_SCHEDULE_TIMEOUT / HZ))
  1798. data = (u32)(MAX_SCHEDULE_TIMEOUT / HZ);
  1799. mod_timer(&nic->blink_timer, jiffies);
  1800. msleep_interruptible(data * 1000);
  1801. del_timer_sync(&nic->blink_timer);
  1802. mdio_write(netdev, nic->mii.phy_id, MII_LED_CONTROL, 0);
  1803. return 0;
  1804. }
  1805. static const char e100_gstrings_stats[][ETH_GSTRING_LEN] = {
  1806. "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
  1807. "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
  1808. "rx_length_errors", "rx_over_errors", "rx_crc_errors",
  1809. "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
  1810. "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
  1811. "tx_heartbeat_errors", "tx_window_errors",
  1812. /* device-specific stats */
  1813. "tx_deferred", "tx_single_collisions", "tx_multi_collisions",
  1814. "tx_flow_control_pause", "rx_flow_control_pause",
  1815. "rx_flow_control_unsupported", "tx_tco_packets", "rx_tco_packets",
  1816. };
  1817. #define E100_NET_STATS_LEN 21
  1818. #define E100_STATS_LEN sizeof(e100_gstrings_stats) / ETH_GSTRING_LEN
  1819. static int e100_get_stats_count(struct net_device *netdev)
  1820. {
  1821. return E100_STATS_LEN;
  1822. }
  1823. static void e100_get_ethtool_stats(struct net_device *netdev,
  1824. struct ethtool_stats *stats, u64 *data)
  1825. {
  1826. struct nic *nic = netdev_priv(netdev);
  1827. int i;
  1828. for(i = 0; i < E100_NET_STATS_LEN; i++)
  1829. data[i] = ((unsigned long *)&nic->net_stats)[i];
  1830. data[i++] = nic->tx_deferred;
  1831. data[i++] = nic->tx_single_collisions;
  1832. data[i++] = nic->tx_multiple_collisions;
  1833. data[i++] = nic->tx_fc_pause;
  1834. data[i++] = nic->rx_fc_pause;
  1835. data[i++] = nic->rx_fc_unsupported;
  1836. data[i++] = nic->tx_tco_frames;
  1837. data[i++] = nic->rx_tco_frames;
  1838. }
  1839. static void e100_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
  1840. {
  1841. switch(stringset) {
  1842. case ETH_SS_TEST:
  1843. memcpy(data, *e100_gstrings_test, sizeof(e100_gstrings_test));
  1844. break;
  1845. case ETH_SS_STATS:
  1846. memcpy(data, *e100_gstrings_stats, sizeof(e100_gstrings_stats));
  1847. break;
  1848. }
  1849. }
  1850. static struct ethtool_ops e100_ethtool_ops = {
  1851. .get_settings = e100_get_settings,
  1852. .set_settings = e100_set_settings,
  1853. .get_drvinfo = e100_get_drvinfo,
  1854. .get_regs_len = e100_get_regs_len,
  1855. .get_regs = e100_get_regs,
  1856. .get_wol = e100_get_wol,
  1857. .set_wol = e100_set_wol,
  1858. .get_msglevel = e100_get_msglevel,
  1859. .set_msglevel = e100_set_msglevel,
  1860. .nway_reset = e100_nway_reset,
  1861. .get_link = e100_get_link,
  1862. .get_eeprom_len = e100_get_eeprom_len,
  1863. .get_eeprom = e100_get_eeprom,
  1864. .set_eeprom = e100_set_eeprom,
  1865. .get_ringparam = e100_get_ringparam,
  1866. .set_ringparam = e100_set_ringparam,
  1867. .self_test_count = e100_diag_test_count,
  1868. .self_test = e100_diag_test,
  1869. .get_strings = e100_get_strings,
  1870. .phys_id = e100_phys_id,
  1871. .get_stats_count = e100_get_stats_count,
  1872. .get_ethtool_stats = e100_get_ethtool_stats,
  1873. };
  1874. static int e100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  1875. {
  1876. struct nic *nic = netdev_priv(netdev);
  1877. return generic_mii_ioctl(&nic->mii, if_mii(ifr), cmd, NULL);
  1878. }
  1879. static int e100_alloc(struct nic *nic)
  1880. {
  1881. nic->mem = pci_alloc_consistent(nic->pdev, sizeof(struct mem),
  1882. &nic->dma_addr);
  1883. return nic->mem ? 0 : -ENOMEM;
  1884. }
  1885. static void e100_free(struct nic *nic)
  1886. {
  1887. if(nic->mem) {
  1888. pci_free_consistent(nic->pdev, sizeof(struct mem),
  1889. nic->mem, nic->dma_addr);
  1890. nic->mem = NULL;
  1891. }
  1892. }
  1893. static int e100_open(struct net_device *netdev)
  1894. {
  1895. struct nic *nic = netdev_priv(netdev);
  1896. int err = 0;
  1897. netif_carrier_off(netdev);
  1898. if((err = e100_up(nic)))
  1899. DPRINTK(IFUP, ERR, "Cannot open interface, aborting.\n");
  1900. return err;
  1901. }
  1902. static int e100_close(struct net_device *netdev)
  1903. {
  1904. e100_down(netdev_priv(netdev));
  1905. return 0;
  1906. }
  1907. static int __devinit e100_probe(struct pci_dev *pdev,
  1908. const struct pci_device_id *ent)
  1909. {
  1910. struct net_device *netdev;
  1911. struct nic *nic;
  1912. int err;
  1913. if(!(netdev = alloc_etherdev(sizeof(struct nic)))) {
  1914. if(((1 << debug) - 1) & NETIF_MSG_PROBE)
  1915. printk(KERN_ERR PFX "Etherdev alloc failed, abort.\n");
  1916. return -ENOMEM;
  1917. }
  1918. netdev->open = e100_open;
  1919. netdev->stop = e100_close;
  1920. netdev->hard_start_xmit = e100_xmit_frame;
  1921. netdev->get_stats = e100_get_stats;
  1922. netdev->set_multicast_list = e100_set_multicast_list;
  1923. netdev->set_mac_address = e100_set_mac_address;
  1924. netdev->change_mtu = e100_change_mtu;
  1925. netdev->do_ioctl = e100_do_ioctl;
  1926. SET_ETHTOOL_OPS(netdev, &e100_ethtool_ops);
  1927. netdev->tx_timeout = e100_tx_timeout;
  1928. netdev->watchdog_timeo = E100_WATCHDOG_PERIOD;
  1929. netdev->poll = e100_poll;
  1930. netdev->weight = E100_NAPI_WEIGHT;
  1931. #ifdef CONFIG_NET_POLL_CONTROLLER
  1932. netdev->poll_controller = e100_netpoll;
  1933. #endif
  1934. strcpy(netdev->name, pci_name(pdev));
  1935. nic = netdev_priv(netdev);
  1936. nic->netdev = netdev;
  1937. nic->pdev = pdev;
  1938. nic->msg_enable = (1 << debug) - 1;
  1939. pci_set_drvdata(pdev, netdev);
  1940. if((err = pci_enable_device(pdev))) {
  1941. DPRINTK(PROBE, ERR, "Cannot enable PCI device, aborting.\n");
  1942. goto err_out_free_dev;
  1943. }
  1944. if(!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  1945. DPRINTK(PROBE, ERR, "Cannot find proper PCI device "
  1946. "base address, aborting.\n");
  1947. err = -ENODEV;
  1948. goto err_out_disable_pdev;
  1949. }
  1950. if((err = pci_request_regions(pdev, DRV_NAME))) {
  1951. DPRINTK(PROBE, ERR, "Cannot obtain PCI resources, aborting.\n");
  1952. goto err_out_disable_pdev;
  1953. }
  1954. if((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK))) {
  1955. DPRINTK(PROBE, ERR, "No usable DMA configuration, aborting.\n");
  1956. goto err_out_free_res;
  1957. }
  1958. SET_MODULE_OWNER(netdev);
  1959. SET_NETDEV_DEV(netdev, &pdev->dev);
  1960. nic->csr = ioremap(pci_resource_start(pdev, 0), sizeof(struct csr));
  1961. if(!nic->csr) {
  1962. DPRINTK(PROBE, ERR, "Cannot map device registers, aborting.\n");
  1963. err = -ENOMEM;
  1964. goto err_out_free_res;
  1965. }
  1966. if(ent->driver_data)
  1967. nic->flags |= ich;
  1968. else
  1969. nic->flags &= ~ich;
  1970. e100_get_defaults(nic);
  1971. /* locks must be initialized before calling hw_reset */
  1972. spin_lock_init(&nic->cb_lock);
  1973. spin_lock_init(&nic->cmd_lock);
  1974. /* Reset the device before pci_set_master() in case device is in some
  1975. * funky state and has an interrupt pending - hint: we don't have the
  1976. * interrupt handler registered yet. */
  1977. e100_hw_reset(nic);
  1978. pci_set_master(pdev);
  1979. init_timer(&nic->watchdog);
  1980. nic->watchdog.function = e100_watchdog;
  1981. nic->watchdog.data = (unsigned long)nic;
  1982. init_timer(&nic->blink_timer);
  1983. nic->blink_timer.function = e100_blink_led;
  1984. nic->blink_timer.data = (unsigned long)nic;
  1985. INIT_WORK(&nic->tx_timeout_task,
  1986. (void (*)(void *))e100_tx_timeout_task, netdev);
  1987. if((err = e100_alloc(nic))) {
  1988. DPRINTK(PROBE, ERR, "Cannot alloc driver memory, aborting.\n");
  1989. goto err_out_iounmap;
  1990. }
  1991. if((err = e100_eeprom_load(nic)))
  1992. goto err_out_free;
  1993. e100_phy_init(nic);
  1994. memcpy(netdev->dev_addr, nic->eeprom, ETH_ALEN);
  1995. if(!is_valid_ether_addr(netdev->dev_addr)) {
  1996. DPRINTK(PROBE, ERR, "Invalid MAC address from "
  1997. "EEPROM, aborting.\n");
  1998. err = -EAGAIN;
  1999. goto err_out_free;
  2000. }
  2001. /* Wol magic packet can be enabled from eeprom */
  2002. if((nic->mac >= mac_82558_D101_A4) &&
  2003. (nic->eeprom[eeprom_id] & eeprom_id_wol))
  2004. nic->flags |= wol_magic;
  2005. /* ack any pending wake events, disable PME */
  2006. pci_enable_wake(pdev, 0, 0);
  2007. strcpy(netdev->name, "eth%d");
  2008. if((err = register_netdev(netdev))) {
  2009. DPRINTK(PROBE, ERR, "Cannot register net device, aborting.\n");
  2010. goto err_out_free;
  2011. }
  2012. DPRINTK(PROBE, INFO, "addr 0x%lx, irq %d, "
  2013. "MAC addr %02X:%02X:%02X:%02X:%02X:%02X\n",
  2014. pci_resource_start(pdev, 0), pdev->irq,
  2015. netdev->dev_addr[0], netdev->dev_addr[1], netdev->dev_addr[2],
  2016. netdev->dev_addr[3], netdev->dev_addr[4], netdev->dev_addr[5]);
  2017. return 0;
  2018. err_out_free:
  2019. e100_free(nic);
  2020. err_out_iounmap:
  2021. iounmap(nic->csr);
  2022. err_out_free_res:
  2023. pci_release_regions(pdev);
  2024. err_out_disable_pdev:
  2025. pci_disable_device(pdev);
  2026. err_out_free_dev:
  2027. pci_set_drvdata(pdev, NULL);
  2028. free_netdev(netdev);
  2029. return err;
  2030. }
  2031. static void __devexit e100_remove(struct pci_dev *pdev)
  2032. {
  2033. struct net_device *netdev = pci_get_drvdata(pdev);
  2034. if(netdev) {
  2035. struct nic *nic = netdev_priv(netdev);
  2036. unregister_netdev(netdev);
  2037. e100_free(nic);
  2038. iounmap(nic->csr);
  2039. free_netdev(netdev);
  2040. pci_release_regions(pdev);
  2041. pci_disable_device(pdev);
  2042. pci_set_drvdata(pdev, NULL);
  2043. }
  2044. }
  2045. #ifdef CONFIG_PM
  2046. static int e100_suspend(struct pci_dev *pdev, pm_message_t state)
  2047. {
  2048. struct net_device *netdev = pci_get_drvdata(pdev);
  2049. struct nic *nic = netdev_priv(netdev);
  2050. if(netif_running(netdev))
  2051. e100_down(nic);
  2052. e100_hw_reset(nic);
  2053. netif_device_detach(netdev);
  2054. pci_save_state(pdev);
  2055. pci_enable_wake(pdev, pci_choose_state(pdev, state), nic->flags & (wol_magic | e100_asf(nic)));
  2056. pci_disable_device(pdev);
  2057. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2058. return 0;
  2059. }
  2060. static int e100_resume(struct pci_dev *pdev)
  2061. {
  2062. struct net_device *netdev = pci_get_drvdata(pdev);
  2063. struct nic *nic = netdev_priv(netdev);
  2064. pci_set_power_state(pdev, PCI_D0);
  2065. pci_restore_state(pdev);
  2066. /* ack any pending wake events, disable PME */
  2067. pci_enable_wake(pdev, 0, 0);
  2068. if(e100_hw_init(nic))
  2069. DPRINTK(HW, ERR, "e100_hw_init failed\n");
  2070. netif_device_attach(netdev);
  2071. if(netif_running(netdev))
  2072. e100_up(nic);
  2073. return 0;
  2074. }
  2075. #endif
  2076. static void e100_shutdown(struct pci_dev *pdev)
  2077. {
  2078. struct net_device *netdev = pci_get_drvdata(pdev);
  2079. struct nic *nic = netdev_priv(netdev);
  2080. #ifdef CONFIG_PM
  2081. pci_enable_wake(pdev, 0, nic->flags & (wol_magic | e100_asf(nic)));
  2082. #else
  2083. pci_enable_wake(pdev, 0, nic->flags & (wol_magic));
  2084. #endif
  2085. }
  2086. static struct pci_driver e100_driver = {
  2087. .name = DRV_NAME,
  2088. .id_table = e100_id_table,
  2089. .probe = e100_probe,
  2090. .remove = __devexit_p(e100_remove),
  2091. #ifdef CONFIG_PM
  2092. .suspend = e100_suspend,
  2093. .resume = e100_resume,
  2094. #endif
  2095. .shutdown = e100_shutdown,
  2096. };
  2097. static int __init e100_init_module(void)
  2098. {
  2099. if(((1 << debug) - 1) & NETIF_MSG_DRV) {
  2100. printk(KERN_INFO PFX "%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  2101. printk(KERN_INFO PFX "%s\n", DRV_COPYRIGHT);
  2102. }
  2103. return pci_module_init(&e100_driver);
  2104. }
  2105. static void __exit e100_cleanup_module(void)
  2106. {
  2107. pci_unregister_driver(&e100_driver);
  2108. }
  2109. module_init(e100_init_module);
  2110. module_exit(e100_cleanup_module);