e100.c 68 KB

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