e100.c 68 KB

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