e100.c 65 KB

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