starfire.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194
  1. /* starfire.c: Linux device driver for the Adaptec Starfire network adapter. */
  2. /*
  3. Written 1998-2000 by Donald Becker.
  4. Current maintainer is Ion Badulescu <ionut ta badula tod org>. Please
  5. send all bug reports to me, and not to Donald Becker, as this code
  6. has been heavily modified from Donald's original version.
  7. This software may be used and distributed according to the terms of
  8. the GNU General Public License (GPL), incorporated herein by reference.
  9. Drivers based on or derived from this code fall under the GPL and must
  10. retain the authorship, copyright and license notice. This file is not
  11. a complete program and may only be used when the entire operating
  12. system is licensed under the GPL.
  13. The information below comes from Donald Becker's original driver:
  14. The author may be reached as becker@scyld.com, or C/O
  15. Scyld Computing Corporation
  16. 410 Severn Ave., Suite 210
  17. Annapolis MD 21403
  18. Support and updates available at
  19. http://www.scyld.com/network/starfire.html
  20. -----------------------------------------------------------
  21. Linux kernel-specific changes:
  22. LK1.1.1 (jgarzik):
  23. - Use PCI driver interface
  24. - Fix MOD_xxx races
  25. - softnet fixups
  26. LK1.1.2 (jgarzik):
  27. - Merge Becker version 0.15
  28. LK1.1.3 (Andrew Morton)
  29. - Timer cleanups
  30. LK1.1.4 (jgarzik):
  31. - Merge Becker version 1.03
  32. LK1.2.1 (Ion Badulescu <ionut@cs.columbia.edu>)
  33. - Support hardware Rx/Tx checksumming
  34. - Use the GFP firmware taken from Adaptec's Netware driver
  35. LK1.2.2 (Ion Badulescu)
  36. - Backported to 2.2.x
  37. LK1.2.3 (Ion Badulescu)
  38. - Fix the flaky mdio interface
  39. - More compat clean-ups
  40. LK1.2.4 (Ion Badulescu)
  41. - More 2.2.x initialization fixes
  42. LK1.2.5 (Ion Badulescu)
  43. - Several fixes from Manfred Spraul
  44. LK1.2.6 (Ion Badulescu)
  45. - Fixed ifup/ifdown/ifup problem in 2.4.x
  46. LK1.2.7 (Ion Badulescu)
  47. - Removed unused code
  48. - Made more functions static and __init
  49. LK1.2.8 (Ion Badulescu)
  50. - Quell bogus error messages, inform about the Tx threshold
  51. - Removed #ifdef CONFIG_PCI, this driver is PCI only
  52. LK1.2.9 (Ion Badulescu)
  53. - Merged Jeff Garzik's changes from 2.4.4-pre5
  54. - Added 2.2.x compatibility stuff required by the above changes
  55. LK1.2.9a (Ion Badulescu)
  56. - More updates from Jeff Garzik
  57. LK1.3.0 (Ion Badulescu)
  58. - Merged zerocopy support
  59. LK1.3.1 (Ion Badulescu)
  60. - Added ethtool support
  61. - Added GPIO (media change) interrupt support
  62. LK1.3.2 (Ion Badulescu)
  63. - Fixed 2.2.x compatibility issues introduced in 1.3.1
  64. - Fixed ethtool ioctl returning uninitialized memory
  65. LK1.3.3 (Ion Badulescu)
  66. - Initialize the TxMode register properly
  67. - Don't dereference dev->priv after freeing it
  68. LK1.3.4 (Ion Badulescu)
  69. - Fixed initialization timing problems
  70. - Fixed interrupt mask definitions
  71. LK1.3.5 (jgarzik)
  72. - ethtool NWAY_RST, GLINK, [GS]MSGLVL support
  73. LK1.3.6:
  74. - Sparc64 support and fixes (Ion Badulescu)
  75. - Better stats and error handling (Ion Badulescu)
  76. - Use new pci_set_mwi() PCI API function (jgarzik)
  77. LK1.3.7 (Ion Badulescu)
  78. - minimal implementation of tx_timeout()
  79. - correctly shutdown the Rx/Tx engines in netdev_close()
  80. - added calls to netif_carrier_on/off
  81. (patch from Stefan Rompf <srompf@isg.de>)
  82. - VLAN support
  83. LK1.3.8 (Ion Badulescu)
  84. - adjust DMA burst size on sparc64
  85. - 64-bit support
  86. - reworked zerocopy support for 64-bit buffers
  87. - working and usable interrupt mitigation/latency
  88. - reduced Tx interrupt frequency for lower interrupt overhead
  89. LK1.3.9 (Ion Badulescu)
  90. - bugfix for mcast filter
  91. - enable the right kind of Tx interrupts (TxDMADone, not TxDone)
  92. LK1.4.0 (Ion Badulescu)
  93. - NAPI support
  94. LK1.4.1 (Ion Badulescu)
  95. - flush PCI posting buffers after disabling Rx interrupts
  96. - put the chip to a D3 slumber on driver unload
  97. - added config option to enable/disable NAPI
  98. LK1.4.2 (Ion Badulescu)
  99. - finally added firmware (GPL'ed by Adaptec)
  100. - removed compatibility code for 2.2.x
  101. LK1.4.2.1 (Ion Badulescu)
  102. - fixed 32/64 bit issues on i386 + CONFIG_HIGHMEM
  103. - added 32-bit padding to outgoing skb's, removed previous workaround
  104. TODO: - fix forced speed/duplexing code (broken a long time ago, when
  105. somebody converted the driver to use the generic MII code)
  106. - fix VLAN support
  107. */
  108. #define DRV_NAME "starfire"
  109. #define DRV_VERSION "1.03+LK1.4.2.1"
  110. #define DRV_RELDATE "October 3, 2005"
  111. #include <linux/config.h>
  112. #include <linux/module.h>
  113. #include <linux/kernel.h>
  114. #include <linux/pci.h>
  115. #include <linux/netdevice.h>
  116. #include <linux/etherdevice.h>
  117. #include <linux/init.h>
  118. #include <linux/delay.h>
  119. #include <linux/crc32.h>
  120. #include <linux/ethtool.h>
  121. #include <linux/mii.h>
  122. #include <linux/if_vlan.h>
  123. #include <asm/processor.h> /* Processor type for cache alignment. */
  124. #include <asm/uaccess.h>
  125. #include <asm/io.h>
  126. #include "starfire_firmware.h"
  127. /*
  128. * The current frame processor firmware fails to checksum a fragment
  129. * of length 1. If and when this is fixed, the #define below can be removed.
  130. */
  131. #define HAS_BROKEN_FIRMWARE
  132. /*
  133. * If using the broken firmware, data must be padded to the next 32-bit boundary.
  134. */
  135. #ifdef HAS_BROKEN_FIRMWARE
  136. #define PADDING_MASK 3
  137. #endif
  138. /*
  139. * Define this if using the driver with the zero-copy patch
  140. */
  141. #define ZEROCOPY
  142. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  143. #define VLAN_SUPPORT
  144. #endif
  145. #ifndef CONFIG_ADAPTEC_STARFIRE_NAPI
  146. #undef HAVE_NETDEV_POLL
  147. #endif
  148. /* The user-configurable values.
  149. These may be modified when a driver module is loaded.*/
  150. /* Used for tuning interrupt latency vs. overhead. */
  151. static int intr_latency;
  152. static int small_frames;
  153. static int debug = 1; /* 1 normal messages, 0 quiet .. 7 verbose. */
  154. static int max_interrupt_work = 20;
  155. static int mtu;
  156. /* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
  157. The Starfire has a 512 element hash table based on the Ethernet CRC. */
  158. static const int multicast_filter_limit = 512;
  159. /* Whether to do TCP/UDP checksums in hardware */
  160. static int enable_hw_cksum = 1;
  161. #define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer.*/
  162. /*
  163. * Set the copy breakpoint for the copy-only-tiny-frames scheme.
  164. * Setting to > 1518 effectively disables this feature.
  165. *
  166. * NOTE:
  167. * The ia64 doesn't allow for unaligned loads even of integers being
  168. * misaligned on a 2 byte boundary. Thus always force copying of
  169. * packets as the starfire doesn't allow for misaligned DMAs ;-(
  170. * 23/10/2000 - Jes
  171. *
  172. * The Alpha and the Sparc don't like unaligned loads, either. On Sparc64,
  173. * at least, having unaligned frames leads to a rather serious performance
  174. * penalty. -Ion
  175. */
  176. #if defined(__ia64__) || defined(__alpha__) || defined(__sparc__)
  177. static int rx_copybreak = PKT_BUF_SZ;
  178. #else
  179. static int rx_copybreak /* = 0 */;
  180. #endif
  181. /* PCI DMA burst size -- on sparc64 we want to force it to 64 bytes, on the others the default of 128 is fine. */
  182. #ifdef __sparc__
  183. #define DMA_BURST_SIZE 64
  184. #else
  185. #define DMA_BURST_SIZE 128
  186. #endif
  187. /* Used to pass the media type, etc.
  188. Both 'options[]' and 'full_duplex[]' exist for driver interoperability.
  189. The media type is usually passed in 'options[]'.
  190. These variables are deprecated, use ethtool instead. -Ion
  191. */
  192. #define MAX_UNITS 8 /* More are supported, limit only on options */
  193. static int options[MAX_UNITS] = {0, };
  194. static int full_duplex[MAX_UNITS] = {0, };
  195. /* Operational parameters that are set at compile time. */
  196. /* The "native" ring sizes are either 256 or 2048.
  197. However in some modes a descriptor may be marked to wrap the ring earlier.
  198. */
  199. #define RX_RING_SIZE 256
  200. #define TX_RING_SIZE 32
  201. /* The completion queues are fixed at 1024 entries i.e. 4K or 8KB. */
  202. #define DONE_Q_SIZE 1024
  203. /* All queues must be aligned on a 256-byte boundary */
  204. #define QUEUE_ALIGN 256
  205. #if RX_RING_SIZE > 256
  206. #define RX_Q_ENTRIES Rx2048QEntries
  207. #else
  208. #define RX_Q_ENTRIES Rx256QEntries
  209. #endif
  210. /* Operational parameters that usually are not changed. */
  211. /* Time in jiffies before concluding the transmitter is hung. */
  212. #define TX_TIMEOUT (2 * HZ)
  213. /*
  214. * This SUCKS.
  215. * We need a much better method to determine if dma_addr_t is 64-bit.
  216. */
  217. #if (defined(__i386__) && defined(CONFIG_HIGHMEM64G)) || defined(__x86_64__) || defined (__ia64__) || defined(__mips64__) || (defined(__mips__) && defined(CONFIG_HIGHMEM) && defined(CONFIG_64BIT_PHYS_ADDR))
  218. /* 64-bit dma_addr_t */
  219. #define ADDR_64BITS /* This chip uses 64 bit addresses. */
  220. #define netdrv_addr_t u64
  221. #define cpu_to_dma(x) cpu_to_le64(x)
  222. #define dma_to_cpu(x) le64_to_cpu(x)
  223. #define RX_DESC_Q_ADDR_SIZE RxDescQAddr64bit
  224. #define TX_DESC_Q_ADDR_SIZE TxDescQAddr64bit
  225. #define RX_COMPL_Q_ADDR_SIZE RxComplQAddr64bit
  226. #define TX_COMPL_Q_ADDR_SIZE TxComplQAddr64bit
  227. #define RX_DESC_ADDR_SIZE RxDescAddr64bit
  228. #else /* 32-bit dma_addr_t */
  229. #define netdrv_addr_t u32
  230. #define cpu_to_dma(x) cpu_to_le32(x)
  231. #define dma_to_cpu(x) le32_to_cpu(x)
  232. #define RX_DESC_Q_ADDR_SIZE RxDescQAddr32bit
  233. #define TX_DESC_Q_ADDR_SIZE TxDescQAddr32bit
  234. #define RX_COMPL_Q_ADDR_SIZE RxComplQAddr32bit
  235. #define TX_COMPL_Q_ADDR_SIZE TxComplQAddr32bit
  236. #define RX_DESC_ADDR_SIZE RxDescAddr32bit
  237. #endif
  238. #define skb_first_frag_len(skb) skb_headlen(skb)
  239. #define skb_num_frags(skb) (skb_shinfo(skb)->nr_frags + 1)
  240. #ifdef HAVE_NETDEV_POLL
  241. #define init_poll(dev) \
  242. do { \
  243. dev->poll = &netdev_poll; \
  244. dev->weight = max_interrupt_work; \
  245. } while (0)
  246. #define netdev_rx(dev, ioaddr) \
  247. do { \
  248. u32 intr_enable; \
  249. if (netif_rx_schedule_prep(dev)) { \
  250. __netif_rx_schedule(dev); \
  251. intr_enable = readl(ioaddr + IntrEnable); \
  252. intr_enable &= ~(IntrRxDone | IntrRxEmpty); \
  253. writel(intr_enable, ioaddr + IntrEnable); \
  254. readl(ioaddr + IntrEnable); /* flush PCI posting buffers */ \
  255. } else { \
  256. /* Paranoia check */ \
  257. intr_enable = readl(ioaddr + IntrEnable); \
  258. if (intr_enable & (IntrRxDone | IntrRxEmpty)) { \
  259. printk(KERN_INFO "%s: interrupt while in polling mode!\n", dev->name); \
  260. intr_enable &= ~(IntrRxDone | IntrRxEmpty); \
  261. writel(intr_enable, ioaddr + IntrEnable); \
  262. } \
  263. } \
  264. } while (0)
  265. #define netdev_receive_skb(skb) netif_receive_skb(skb)
  266. #define vlan_netdev_receive_skb(skb, vlgrp, vlid) vlan_hwaccel_receive_skb(skb, vlgrp, vlid)
  267. static int netdev_poll(struct net_device *dev, int *budget);
  268. #else /* not HAVE_NETDEV_POLL */
  269. #define init_poll(dev)
  270. #define netdev_receive_skb(skb) netif_rx(skb)
  271. #define vlan_netdev_receive_skb(skb, vlgrp, vlid) vlan_hwaccel_rx(skb, vlgrp, vlid)
  272. #define netdev_rx(dev, ioaddr) \
  273. do { \
  274. int quota = np->dirty_rx + RX_RING_SIZE - np->cur_rx; \
  275. __netdev_rx(dev, &quota);\
  276. } while (0)
  277. #endif /* not HAVE_NETDEV_POLL */
  278. /* end of compatibility code */
  279. /* These identify the driver base version and may not be removed. */
  280. static const char version[] __devinitdata =
  281. KERN_INFO "starfire.c:v1.03 7/26/2000 Written by Donald Becker <becker@scyld.com>\n"
  282. KERN_INFO " (unofficial 2.2/2.4 kernel port, version " DRV_VERSION ", " DRV_RELDATE ")\n";
  283. MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
  284. MODULE_DESCRIPTION("Adaptec Starfire Ethernet driver");
  285. MODULE_LICENSE("GPL");
  286. MODULE_VERSION(DRV_VERSION);
  287. module_param(max_interrupt_work, int, 0);
  288. module_param(mtu, int, 0);
  289. module_param(debug, int, 0);
  290. module_param(rx_copybreak, int, 0);
  291. module_param(intr_latency, int, 0);
  292. module_param(small_frames, int, 0);
  293. module_param_array(options, int, NULL, 0);
  294. module_param_array(full_duplex, int, NULL, 0);
  295. module_param(enable_hw_cksum, int, 0);
  296. MODULE_PARM_DESC(max_interrupt_work, "Maximum events handled per interrupt");
  297. MODULE_PARM_DESC(mtu, "MTU (all boards)");
  298. MODULE_PARM_DESC(debug, "Debug level (0-6)");
  299. MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
  300. MODULE_PARM_DESC(intr_latency, "Maximum interrupt latency, in microseconds");
  301. MODULE_PARM_DESC(small_frames, "Maximum size of receive frames that bypass interrupt latency (0,64,128,256,512)");
  302. MODULE_PARM_DESC(options, "Deprecated: Bits 0-3: media type, bit 17: full duplex");
  303. MODULE_PARM_DESC(full_duplex, "Deprecated: Forced full-duplex setting (0/1)");
  304. MODULE_PARM_DESC(enable_hw_cksum, "Enable/disable hardware cksum support (0/1)");
  305. /*
  306. Theory of Operation
  307. I. Board Compatibility
  308. This driver is for the Adaptec 6915 "Starfire" 64 bit PCI Ethernet adapter.
  309. II. Board-specific settings
  310. III. Driver operation
  311. IIIa. Ring buffers
  312. The Starfire hardware uses multiple fixed-size descriptor queues/rings. The
  313. ring sizes are set fixed by the hardware, but may optionally be wrapped
  314. earlier by the END bit in the descriptor.
  315. This driver uses that hardware queue size for the Rx ring, where a large
  316. number of entries has no ill effect beyond increases the potential backlog.
  317. The Tx ring is wrapped with the END bit, since a large hardware Tx queue
  318. disables the queue layer priority ordering and we have no mechanism to
  319. utilize the hardware two-level priority queue. When modifying the
  320. RX/TX_RING_SIZE pay close attention to page sizes and the ring-empty warning
  321. levels.
  322. IIIb/c. Transmit/Receive Structure
  323. See the Adaptec manual for the many possible structures, and options for
  324. each structure. There are far too many to document all of them here.
  325. For transmit this driver uses type 0/1 transmit descriptors (depending
  326. on the 32/64 bitness of the architecture), and relies on automatic
  327. minimum-length padding. It does not use the completion queue
  328. consumer index, but instead checks for non-zero status entries.
  329. For receive this driver uses type 2/3 receive descriptors. The driver
  330. allocates full frame size skbuffs for the Rx ring buffers, so all frames
  331. should fit in a single descriptor. The driver does not use the completion
  332. queue consumer index, but instead checks for non-zero status entries.
  333. When an incoming frame is less than RX_COPYBREAK bytes long, a fresh skbuff
  334. is allocated and the frame is copied to the new skbuff. When the incoming
  335. frame is larger, the skbuff is passed directly up the protocol stack.
  336. Buffers consumed this way are replaced by newly allocated skbuffs in a later
  337. phase of receive.
  338. A notable aspect of operation is that unaligned buffers are not permitted by
  339. the Starfire hardware. Thus the IP header at offset 14 in an ethernet frame
  340. isn't longword aligned, which may cause problems on some machine
  341. e.g. Alphas and IA64. For these architectures, the driver is forced to copy
  342. the frame into a new skbuff unconditionally. Copied frames are put into the
  343. skbuff at an offset of "+2", thus 16-byte aligning the IP header.
  344. IIId. Synchronization
  345. The driver runs as two independent, single-threaded flows of control. One
  346. is the send-packet routine, which enforces single-threaded use by the
  347. dev->tbusy flag. The other thread is the interrupt handler, which is single
  348. threaded by the hardware and interrupt handling software.
  349. The send packet thread has partial control over the Tx ring and the netif_queue
  350. status. If the number of free Tx slots in the ring falls below a certain number
  351. (currently hardcoded to 4), it signals the upper layer to stop the queue.
  352. The interrupt handler has exclusive control over the Rx ring and records stats
  353. from the Tx ring. After reaping the stats, it marks the Tx queue entry as
  354. empty by incrementing the dirty_tx mark. Iff the netif_queue is stopped and the
  355. number of free Tx slow is above the threshold, it signals the upper layer to
  356. restart the queue.
  357. IV. Notes
  358. IVb. References
  359. The Adaptec Starfire manuals, available only from Adaptec.
  360. http://www.scyld.com/expert/100mbps.html
  361. http://www.scyld.com/expert/NWay.html
  362. IVc. Errata
  363. - StopOnPerr is broken, don't enable
  364. - Hardware ethernet padding exposes random data, perform software padding
  365. instead (unverified -- works correctly for all the hardware I have)
  366. */
  367. enum chip_capability_flags {CanHaveMII=1, };
  368. enum chipset {
  369. CH_6915 = 0,
  370. };
  371. static struct pci_device_id starfire_pci_tbl[] = {
  372. { 0x9004, 0x6915, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_6915 },
  373. { 0, }
  374. };
  375. MODULE_DEVICE_TABLE(pci, starfire_pci_tbl);
  376. /* A chip capabilities table, matching the CH_xxx entries in xxx_pci_tbl[] above. */
  377. static const struct chip_info {
  378. const char *name;
  379. int drv_flags;
  380. } netdrv_tbl[] __devinitdata = {
  381. { "Adaptec Starfire 6915", CanHaveMII },
  382. };
  383. /* Offsets to the device registers.
  384. Unlike software-only systems, device drivers interact with complex hardware.
  385. It's not useful to define symbolic names for every register bit in the
  386. device. The name can only partially document the semantics and make
  387. the driver longer and more difficult to read.
  388. In general, only the important configuration values or bits changed
  389. multiple times should be defined symbolically.
  390. */
  391. enum register_offsets {
  392. PCIDeviceConfig=0x50040, GenCtrl=0x50070, IntrTimerCtrl=0x50074,
  393. IntrClear=0x50080, IntrStatus=0x50084, IntrEnable=0x50088,
  394. MIICtrl=0x52000, TxStationAddr=0x50120, EEPROMCtrl=0x51000,
  395. GPIOCtrl=0x5008C, TxDescCtrl=0x50090,
  396. TxRingPtr=0x50098, HiPriTxRingPtr=0x50094, /* Low and High priority. */
  397. TxRingHiAddr=0x5009C, /* 64 bit address extension. */
  398. TxProducerIdx=0x500A0, TxConsumerIdx=0x500A4,
  399. TxThreshold=0x500B0,
  400. CompletionHiAddr=0x500B4, TxCompletionAddr=0x500B8,
  401. RxCompletionAddr=0x500BC, RxCompletionQ2Addr=0x500C0,
  402. CompletionQConsumerIdx=0x500C4, RxDMACtrl=0x500D0,
  403. RxDescQCtrl=0x500D4, RxDescQHiAddr=0x500DC, RxDescQAddr=0x500E0,
  404. RxDescQIdx=0x500E8, RxDMAStatus=0x500F0, RxFilterMode=0x500F4,
  405. TxMode=0x55000, VlanType=0x55064,
  406. PerfFilterTable=0x56000, HashTable=0x56100,
  407. TxGfpMem=0x58000, RxGfpMem=0x5a000,
  408. };
  409. /*
  410. * Bits in the interrupt status/mask registers.
  411. * Warning: setting Intr[Ab]NormalSummary in the IntrEnable register
  412. * enables all the interrupt sources that are or'ed into those status bits.
  413. */
  414. enum intr_status_bits {
  415. IntrLinkChange=0xf0000000, IntrStatsMax=0x08000000,
  416. IntrAbnormalSummary=0x02000000, IntrGeneralTimer=0x01000000,
  417. IntrSoftware=0x800000, IntrRxComplQ1Low=0x400000,
  418. IntrTxComplQLow=0x200000, IntrPCI=0x100000,
  419. IntrDMAErr=0x080000, IntrTxDataLow=0x040000,
  420. IntrRxComplQ2Low=0x020000, IntrRxDescQ1Low=0x010000,
  421. IntrNormalSummary=0x8000, IntrTxDone=0x4000,
  422. IntrTxDMADone=0x2000, IntrTxEmpty=0x1000,
  423. IntrEarlyRxQ2=0x0800, IntrEarlyRxQ1=0x0400,
  424. IntrRxQ2Done=0x0200, IntrRxQ1Done=0x0100,
  425. IntrRxGFPDead=0x80, IntrRxDescQ2Low=0x40,
  426. IntrNoTxCsum=0x20, IntrTxBadID=0x10,
  427. IntrHiPriTxBadID=0x08, IntrRxGfp=0x04,
  428. IntrTxGfp=0x02, IntrPCIPad=0x01,
  429. /* not quite bits */
  430. IntrRxDone=IntrRxQ2Done | IntrRxQ1Done,
  431. IntrRxEmpty=IntrRxDescQ1Low | IntrRxDescQ2Low,
  432. IntrNormalMask=0xff00, IntrAbnormalMask=0x3ff00fe,
  433. };
  434. /* Bits in the RxFilterMode register. */
  435. enum rx_mode_bits {
  436. AcceptBroadcast=0x04, AcceptAllMulticast=0x02, AcceptAll=0x01,
  437. AcceptMulticast=0x10, PerfectFilter=0x40, HashFilter=0x30,
  438. PerfectFilterVlan=0x80, MinVLANPrio=0xE000, VlanMode=0x0200,
  439. WakeupOnGFP=0x0800,
  440. };
  441. /* Bits in the TxMode register */
  442. enum tx_mode_bits {
  443. MiiSoftReset=0x8000, MIILoopback=0x4000,
  444. TxFlowEnable=0x0800, RxFlowEnable=0x0400,
  445. PadEnable=0x04, FullDuplex=0x02, HugeFrame=0x01,
  446. };
  447. /* Bits in the TxDescCtrl register. */
  448. enum tx_ctrl_bits {
  449. TxDescSpaceUnlim=0x00, TxDescSpace32=0x10, TxDescSpace64=0x20,
  450. TxDescSpace128=0x30, TxDescSpace256=0x40,
  451. TxDescType0=0x00, TxDescType1=0x01, TxDescType2=0x02,
  452. TxDescType3=0x03, TxDescType4=0x04,
  453. TxNoDMACompletion=0x08,
  454. TxDescQAddr64bit=0x80, TxDescQAddr32bit=0,
  455. TxHiPriFIFOThreshShift=24, TxPadLenShift=16,
  456. TxDMABurstSizeShift=8,
  457. };
  458. /* Bits in the RxDescQCtrl register. */
  459. enum rx_ctrl_bits {
  460. RxBufferLenShift=16, RxMinDescrThreshShift=0,
  461. RxPrefetchMode=0x8000, RxVariableQ=0x2000,
  462. Rx2048QEntries=0x4000, Rx256QEntries=0,
  463. RxDescAddr64bit=0x1000, RxDescAddr32bit=0,
  464. RxDescQAddr64bit=0x0100, RxDescQAddr32bit=0,
  465. RxDescSpace4=0x000, RxDescSpace8=0x100,
  466. RxDescSpace16=0x200, RxDescSpace32=0x300,
  467. RxDescSpace64=0x400, RxDescSpace128=0x500,
  468. RxConsumerWrEn=0x80,
  469. };
  470. /* Bits in the RxDMACtrl register. */
  471. enum rx_dmactrl_bits {
  472. RxReportBadFrames=0x80000000, RxDMAShortFrames=0x40000000,
  473. RxDMABadFrames=0x20000000, RxDMACrcErrorFrames=0x10000000,
  474. RxDMAControlFrame=0x08000000, RxDMAPauseFrame=0x04000000,
  475. RxChecksumIgnore=0, RxChecksumRejectTCPUDP=0x02000000,
  476. RxChecksumRejectTCPOnly=0x01000000,
  477. RxCompletionQ2Enable=0x800000,
  478. RxDMAQ2Disable=0, RxDMAQ2FPOnly=0x100000,
  479. RxDMAQ2SmallPkt=0x200000, RxDMAQ2HighPrio=0x300000,
  480. RxDMAQ2NonIP=0x400000,
  481. RxUseBackupQueue=0x080000, RxDMACRC=0x040000,
  482. RxEarlyIntThreshShift=12, RxHighPrioThreshShift=8,
  483. RxBurstSizeShift=0,
  484. };
  485. /* Bits in the RxCompletionAddr register */
  486. enum rx_compl_bits {
  487. RxComplQAddr64bit=0x80, RxComplQAddr32bit=0,
  488. RxComplProducerWrEn=0x40,
  489. RxComplType0=0x00, RxComplType1=0x10,
  490. RxComplType2=0x20, RxComplType3=0x30,
  491. RxComplThreshShift=0,
  492. };
  493. /* Bits in the TxCompletionAddr register */
  494. enum tx_compl_bits {
  495. TxComplQAddr64bit=0x80, TxComplQAddr32bit=0,
  496. TxComplProducerWrEn=0x40,
  497. TxComplIntrStatus=0x20,
  498. CommonQueueMode=0x10,
  499. TxComplThreshShift=0,
  500. };
  501. /* Bits in the GenCtrl register */
  502. enum gen_ctrl_bits {
  503. RxEnable=0x05, TxEnable=0x0a,
  504. RxGFPEnable=0x10, TxGFPEnable=0x20,
  505. };
  506. /* Bits in the IntrTimerCtrl register */
  507. enum intr_ctrl_bits {
  508. Timer10X=0x800, EnableIntrMasking=0x60, SmallFrameBypass=0x100,
  509. SmallFrame64=0, SmallFrame128=0x200, SmallFrame256=0x400, SmallFrame512=0x600,
  510. IntrLatencyMask=0x1f,
  511. };
  512. /* The Rx and Tx buffer descriptors. */
  513. struct starfire_rx_desc {
  514. dma_addr_t rxaddr;
  515. };
  516. enum rx_desc_bits {
  517. RxDescValid=1, RxDescEndRing=2,
  518. };
  519. /* Completion queue entry. */
  520. struct short_rx_done_desc {
  521. u32 status; /* Low 16 bits is length. */
  522. };
  523. struct basic_rx_done_desc {
  524. u32 status; /* Low 16 bits is length. */
  525. u16 vlanid;
  526. u16 status2;
  527. };
  528. struct csum_rx_done_desc {
  529. u32 status; /* Low 16 bits is length. */
  530. u16 csum; /* Partial checksum */
  531. u16 status2;
  532. };
  533. struct full_rx_done_desc {
  534. u32 status; /* Low 16 bits is length. */
  535. u16 status3;
  536. u16 status2;
  537. u16 vlanid;
  538. u16 csum; /* partial checksum */
  539. u32 timestamp;
  540. };
  541. /* XXX: this is ugly and I'm not sure it's worth the trouble -Ion */
  542. #ifdef VLAN_SUPPORT
  543. typedef struct full_rx_done_desc rx_done_desc;
  544. #define RxComplType RxComplType3
  545. #else /* not VLAN_SUPPORT */
  546. typedef struct csum_rx_done_desc rx_done_desc;
  547. #define RxComplType RxComplType2
  548. #endif /* not VLAN_SUPPORT */
  549. enum rx_done_bits {
  550. RxOK=0x20000000, RxFIFOErr=0x10000000, RxBufQ2=0x08000000,
  551. };
  552. /* Type 1 Tx descriptor. */
  553. struct starfire_tx_desc_1 {
  554. u32 status; /* Upper bits are status, lower 16 length. */
  555. u32 addr;
  556. };
  557. /* Type 2 Tx descriptor. */
  558. struct starfire_tx_desc_2 {
  559. u32 status; /* Upper bits are status, lower 16 length. */
  560. u32 reserved;
  561. u64 addr;
  562. };
  563. #ifdef ADDR_64BITS
  564. typedef struct starfire_tx_desc_2 starfire_tx_desc;
  565. #define TX_DESC_TYPE TxDescType2
  566. #else /* not ADDR_64BITS */
  567. typedef struct starfire_tx_desc_1 starfire_tx_desc;
  568. #define TX_DESC_TYPE TxDescType1
  569. #endif /* not ADDR_64BITS */
  570. #define TX_DESC_SPACING TxDescSpaceUnlim
  571. enum tx_desc_bits {
  572. TxDescID=0xB0000000,
  573. TxCRCEn=0x01000000, TxDescIntr=0x08000000,
  574. TxRingWrap=0x04000000, TxCalTCP=0x02000000,
  575. };
  576. struct tx_done_desc {
  577. u32 status; /* timestamp, index. */
  578. #if 0
  579. u32 intrstatus; /* interrupt status */
  580. #endif
  581. };
  582. struct rx_ring_info {
  583. struct sk_buff *skb;
  584. dma_addr_t mapping;
  585. };
  586. struct tx_ring_info {
  587. struct sk_buff *skb;
  588. dma_addr_t mapping;
  589. unsigned int used_slots;
  590. };
  591. #define PHY_CNT 2
  592. struct netdev_private {
  593. /* Descriptor rings first for alignment. */
  594. struct starfire_rx_desc *rx_ring;
  595. starfire_tx_desc *tx_ring;
  596. dma_addr_t rx_ring_dma;
  597. dma_addr_t tx_ring_dma;
  598. /* The addresses of rx/tx-in-place skbuffs. */
  599. struct rx_ring_info rx_info[RX_RING_SIZE];
  600. struct tx_ring_info tx_info[TX_RING_SIZE];
  601. /* Pointers to completion queues (full pages). */
  602. rx_done_desc *rx_done_q;
  603. dma_addr_t rx_done_q_dma;
  604. unsigned int rx_done;
  605. struct tx_done_desc *tx_done_q;
  606. dma_addr_t tx_done_q_dma;
  607. unsigned int tx_done;
  608. struct net_device_stats stats;
  609. struct pci_dev *pci_dev;
  610. #ifdef VLAN_SUPPORT
  611. struct vlan_group *vlgrp;
  612. #endif
  613. void *queue_mem;
  614. dma_addr_t queue_mem_dma;
  615. size_t queue_mem_size;
  616. /* Frequently used values: keep some adjacent for cache effect. */
  617. spinlock_t lock;
  618. unsigned int cur_rx, dirty_rx; /* Producer/consumer ring indices */
  619. unsigned int cur_tx, dirty_tx, reap_tx;
  620. unsigned int rx_buf_sz; /* Based on MTU+slack. */
  621. /* These values keep track of the transceiver/media in use. */
  622. int speed100; /* Set if speed == 100MBit. */
  623. u32 tx_mode;
  624. u32 intr_timer_ctrl;
  625. u8 tx_threshold;
  626. /* MII transceiver section. */
  627. struct mii_if_info mii_if; /* MII lib hooks/info */
  628. int phy_cnt; /* MII device addresses. */
  629. unsigned char phys[PHY_CNT]; /* MII device addresses. */
  630. void __iomem *base;
  631. };
  632. static int mdio_read(struct net_device *dev, int phy_id, int location);
  633. static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
  634. static int netdev_open(struct net_device *dev);
  635. static void check_duplex(struct net_device *dev);
  636. static void tx_timeout(struct net_device *dev);
  637. static void init_ring(struct net_device *dev);
  638. static int start_tx(struct sk_buff *skb, struct net_device *dev);
  639. static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *regs);
  640. static void netdev_error(struct net_device *dev, int intr_status);
  641. static int __netdev_rx(struct net_device *dev, int *quota);
  642. static void refill_rx_ring(struct net_device *dev);
  643. static void netdev_error(struct net_device *dev, int intr_status);
  644. static void set_rx_mode(struct net_device *dev);
  645. static struct net_device_stats *get_stats(struct net_device *dev);
  646. static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  647. static int netdev_close(struct net_device *dev);
  648. static void netdev_media_change(struct net_device *dev);
  649. static struct ethtool_ops ethtool_ops;
  650. #ifdef VLAN_SUPPORT
  651. static void netdev_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
  652. {
  653. struct netdev_private *np = netdev_priv(dev);
  654. spin_lock(&np->lock);
  655. if (debug > 2)
  656. printk("%s: Setting vlgrp to %p\n", dev->name, grp);
  657. np->vlgrp = grp;
  658. set_rx_mode(dev);
  659. spin_unlock(&np->lock);
  660. }
  661. static void netdev_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
  662. {
  663. struct netdev_private *np = netdev_priv(dev);
  664. spin_lock(&np->lock);
  665. if (debug > 1)
  666. printk("%s: Adding vlanid %d to vlan filter\n", dev->name, vid);
  667. set_rx_mode(dev);
  668. spin_unlock(&np->lock);
  669. }
  670. static void netdev_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
  671. {
  672. struct netdev_private *np = netdev_priv(dev);
  673. spin_lock(&np->lock);
  674. if (debug > 1)
  675. printk("%s: removing vlanid %d from vlan filter\n", dev->name, vid);
  676. if (np->vlgrp)
  677. np->vlgrp->vlan_devices[vid] = NULL;
  678. set_rx_mode(dev);
  679. spin_unlock(&np->lock);
  680. }
  681. #endif /* VLAN_SUPPORT */
  682. static int __devinit starfire_init_one(struct pci_dev *pdev,
  683. const struct pci_device_id *ent)
  684. {
  685. struct netdev_private *np;
  686. int i, irq, option, chip_idx = ent->driver_data;
  687. struct net_device *dev;
  688. static int card_idx = -1;
  689. long ioaddr;
  690. void __iomem *base;
  691. int drv_flags, io_size;
  692. int boguscnt;
  693. /* when built into the kernel, we only print version if device is found */
  694. #ifndef MODULE
  695. static int printed_version;
  696. if (!printed_version++)
  697. printk(version);
  698. #endif
  699. card_idx++;
  700. if (pci_enable_device (pdev))
  701. return -EIO;
  702. ioaddr = pci_resource_start(pdev, 0);
  703. io_size = pci_resource_len(pdev, 0);
  704. if (!ioaddr || ((pci_resource_flags(pdev, 0) & IORESOURCE_MEM) == 0)) {
  705. printk(KERN_ERR DRV_NAME " %d: no PCI MEM resources, aborting\n", card_idx);
  706. return -ENODEV;
  707. }
  708. dev = alloc_etherdev(sizeof(*np));
  709. if (!dev) {
  710. printk(KERN_ERR DRV_NAME " %d: cannot alloc etherdev, aborting\n", card_idx);
  711. return -ENOMEM;
  712. }
  713. SET_MODULE_OWNER(dev);
  714. SET_NETDEV_DEV(dev, &pdev->dev);
  715. irq = pdev->irq;
  716. if (pci_request_regions (pdev, DRV_NAME)) {
  717. printk(KERN_ERR DRV_NAME " %d: cannot reserve PCI resources, aborting\n", card_idx);
  718. goto err_out_free_netdev;
  719. }
  720. /* ioremap is borken in Linux-2.2.x/sparc64 */
  721. base = ioremap(ioaddr, io_size);
  722. if (!base) {
  723. printk(KERN_ERR DRV_NAME " %d: cannot remap %#x @ %#lx, aborting\n",
  724. card_idx, io_size, ioaddr);
  725. goto err_out_free_res;
  726. }
  727. pci_set_master(pdev);
  728. /* enable MWI -- it vastly improves Rx performance on sparc64 */
  729. pci_set_mwi(pdev);
  730. #ifdef ZEROCOPY
  731. /* Starfire can do TCP/UDP checksumming */
  732. if (enable_hw_cksum)
  733. dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
  734. #endif /* ZEROCOPY */
  735. #ifdef VLAN_SUPPORT
  736. dev->features |= NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
  737. dev->vlan_rx_register = netdev_vlan_rx_register;
  738. dev->vlan_rx_add_vid = netdev_vlan_rx_add_vid;
  739. dev->vlan_rx_kill_vid = netdev_vlan_rx_kill_vid;
  740. #endif /* VLAN_RX_KILL_VID */
  741. #ifdef ADDR_64BITS
  742. dev->features |= NETIF_F_HIGHDMA;
  743. #endif /* ADDR_64BITS */
  744. /* Serial EEPROM reads are hidden by the hardware. */
  745. for (i = 0; i < 6; i++)
  746. dev->dev_addr[i] = readb(base + EEPROMCtrl + 20 - i);
  747. #if ! defined(final_version) /* Dump the EEPROM contents during development. */
  748. if (debug > 4)
  749. for (i = 0; i < 0x20; i++)
  750. printk("%2.2x%s",
  751. (unsigned int)readb(base + EEPROMCtrl + i),
  752. i % 16 != 15 ? " " : "\n");
  753. #endif
  754. /* Issue soft reset */
  755. writel(MiiSoftReset, base + TxMode);
  756. udelay(1000);
  757. writel(0, base + TxMode);
  758. /* Reset the chip to erase previous misconfiguration. */
  759. writel(1, base + PCIDeviceConfig);
  760. boguscnt = 1000;
  761. while (--boguscnt > 0) {
  762. udelay(10);
  763. if ((readl(base + PCIDeviceConfig) & 1) == 0)
  764. break;
  765. }
  766. if (boguscnt == 0)
  767. printk("%s: chipset reset never completed!\n", dev->name);
  768. /* wait a little longer */
  769. udelay(1000);
  770. dev->base_addr = (unsigned long)base;
  771. dev->irq = irq;
  772. np = netdev_priv(dev);
  773. np->base = base;
  774. spin_lock_init(&np->lock);
  775. pci_set_drvdata(pdev, dev);
  776. np->pci_dev = pdev;
  777. np->mii_if.dev = dev;
  778. np->mii_if.mdio_read = mdio_read;
  779. np->mii_if.mdio_write = mdio_write;
  780. np->mii_if.phy_id_mask = 0x1f;
  781. np->mii_if.reg_num_mask = 0x1f;
  782. drv_flags = netdrv_tbl[chip_idx].drv_flags;
  783. option = card_idx < MAX_UNITS ? options[card_idx] : 0;
  784. if (dev->mem_start)
  785. option = dev->mem_start;
  786. /* The lower four bits are the media type. */
  787. if (option & 0x200)
  788. np->mii_if.full_duplex = 1;
  789. if (card_idx < MAX_UNITS && full_duplex[card_idx] > 0)
  790. np->mii_if.full_duplex = 1;
  791. if (np->mii_if.full_duplex)
  792. np->mii_if.force_media = 1;
  793. else
  794. np->mii_if.force_media = 0;
  795. np->speed100 = 1;
  796. /* timer resolution is 128 * 0.8us */
  797. np->intr_timer_ctrl = (((intr_latency * 10) / 1024) & IntrLatencyMask) |
  798. Timer10X | EnableIntrMasking;
  799. if (small_frames > 0) {
  800. np->intr_timer_ctrl |= SmallFrameBypass;
  801. switch (small_frames) {
  802. case 1 ... 64:
  803. np->intr_timer_ctrl |= SmallFrame64;
  804. break;
  805. case 65 ... 128:
  806. np->intr_timer_ctrl |= SmallFrame128;
  807. break;
  808. case 129 ... 256:
  809. np->intr_timer_ctrl |= SmallFrame256;
  810. break;
  811. default:
  812. np->intr_timer_ctrl |= SmallFrame512;
  813. if (small_frames > 512)
  814. printk("Adjusting small_frames down to 512\n");
  815. break;
  816. }
  817. }
  818. /* The chip-specific entries in the device structure. */
  819. dev->open = &netdev_open;
  820. dev->hard_start_xmit = &start_tx;
  821. dev->tx_timeout = tx_timeout;
  822. dev->watchdog_timeo = TX_TIMEOUT;
  823. init_poll(dev);
  824. dev->stop = &netdev_close;
  825. dev->get_stats = &get_stats;
  826. dev->set_multicast_list = &set_rx_mode;
  827. dev->do_ioctl = &netdev_ioctl;
  828. SET_ETHTOOL_OPS(dev, &ethtool_ops);
  829. if (mtu)
  830. dev->mtu = mtu;
  831. if (register_netdev(dev))
  832. goto err_out_cleardev;
  833. printk(KERN_INFO "%s: %s at %p, ",
  834. dev->name, netdrv_tbl[chip_idx].name, base);
  835. for (i = 0; i < 5; i++)
  836. printk("%2.2x:", dev->dev_addr[i]);
  837. printk("%2.2x, IRQ %d.\n", dev->dev_addr[i], irq);
  838. if (drv_flags & CanHaveMII) {
  839. int phy, phy_idx = 0;
  840. int mii_status;
  841. for (phy = 0; phy < 32 && phy_idx < PHY_CNT; phy++) {
  842. mdio_write(dev, phy, MII_BMCR, BMCR_RESET);
  843. mdelay(100);
  844. boguscnt = 1000;
  845. while (--boguscnt > 0)
  846. if ((mdio_read(dev, phy, MII_BMCR) & BMCR_RESET) == 0)
  847. break;
  848. if (boguscnt == 0) {
  849. printk("%s: PHY#%d reset never completed!\n", dev->name, phy);
  850. continue;
  851. }
  852. mii_status = mdio_read(dev, phy, MII_BMSR);
  853. if (mii_status != 0) {
  854. np->phys[phy_idx++] = phy;
  855. np->mii_if.advertising = mdio_read(dev, phy, MII_ADVERTISE);
  856. printk(KERN_INFO "%s: MII PHY found at address %d, status "
  857. "%#4.4x advertising %#4.4x.\n",
  858. dev->name, phy, mii_status, np->mii_if.advertising);
  859. /* there can be only one PHY on-board */
  860. break;
  861. }
  862. }
  863. np->phy_cnt = phy_idx;
  864. if (np->phy_cnt > 0)
  865. np->mii_if.phy_id = np->phys[0];
  866. else
  867. memset(&np->mii_if, 0, sizeof(np->mii_if));
  868. }
  869. printk(KERN_INFO "%s: scatter-gather and hardware TCP cksumming %s.\n",
  870. dev->name, enable_hw_cksum ? "enabled" : "disabled");
  871. return 0;
  872. err_out_cleardev:
  873. pci_set_drvdata(pdev, NULL);
  874. iounmap(base);
  875. err_out_free_res:
  876. pci_release_regions (pdev);
  877. err_out_free_netdev:
  878. free_netdev(dev);
  879. return -ENODEV;
  880. }
  881. /* Read the MII Management Data I/O (MDIO) interfaces. */
  882. static int mdio_read(struct net_device *dev, int phy_id, int location)
  883. {
  884. struct netdev_private *np = netdev_priv(dev);
  885. void __iomem *mdio_addr = np->base + MIICtrl + (phy_id<<7) + (location<<2);
  886. int result, boguscnt=1000;
  887. /* ??? Should we add a busy-wait here? */
  888. do
  889. result = readl(mdio_addr);
  890. while ((result & 0xC0000000) != 0x80000000 && --boguscnt > 0);
  891. if (boguscnt == 0)
  892. return 0;
  893. if ((result & 0xffff) == 0xffff)
  894. return 0;
  895. return result & 0xffff;
  896. }
  897. static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
  898. {
  899. struct netdev_private *np = netdev_priv(dev);
  900. void __iomem *mdio_addr = np->base + MIICtrl + (phy_id<<7) + (location<<2);
  901. writel(value, mdio_addr);
  902. /* The busy-wait will occur before a read. */
  903. }
  904. static int netdev_open(struct net_device *dev)
  905. {
  906. struct netdev_private *np = netdev_priv(dev);
  907. void __iomem *ioaddr = np->base;
  908. int i, retval;
  909. size_t tx_done_q_size, rx_done_q_size, tx_ring_size, rx_ring_size;
  910. /* Do we ever need to reset the chip??? */
  911. retval = request_irq(dev->irq, &intr_handler, SA_SHIRQ, dev->name, dev);
  912. if (retval)
  913. return retval;
  914. /* Disable the Rx and Tx, and reset the chip. */
  915. writel(0, ioaddr + GenCtrl);
  916. writel(1, ioaddr + PCIDeviceConfig);
  917. if (debug > 1)
  918. printk(KERN_DEBUG "%s: netdev_open() irq %d.\n",
  919. dev->name, dev->irq);
  920. /* Allocate the various queues. */
  921. if (np->queue_mem == 0) {
  922. tx_done_q_size = ((sizeof(struct tx_done_desc) * DONE_Q_SIZE + QUEUE_ALIGN - 1) / QUEUE_ALIGN) * QUEUE_ALIGN;
  923. rx_done_q_size = ((sizeof(rx_done_desc) * DONE_Q_SIZE + QUEUE_ALIGN - 1) / QUEUE_ALIGN) * QUEUE_ALIGN;
  924. tx_ring_size = ((sizeof(starfire_tx_desc) * TX_RING_SIZE + QUEUE_ALIGN - 1) / QUEUE_ALIGN) * QUEUE_ALIGN;
  925. rx_ring_size = sizeof(struct starfire_rx_desc) * RX_RING_SIZE;
  926. np->queue_mem_size = tx_done_q_size + rx_done_q_size + tx_ring_size + rx_ring_size;
  927. np->queue_mem = pci_alloc_consistent(np->pci_dev, np->queue_mem_size, &np->queue_mem_dma);
  928. if (np->queue_mem == NULL) {
  929. free_irq(dev->irq, dev);
  930. return -ENOMEM;
  931. }
  932. np->tx_done_q = np->queue_mem;
  933. np->tx_done_q_dma = np->queue_mem_dma;
  934. np->rx_done_q = (void *) np->tx_done_q + tx_done_q_size;
  935. np->rx_done_q_dma = np->tx_done_q_dma + tx_done_q_size;
  936. np->tx_ring = (void *) np->rx_done_q + rx_done_q_size;
  937. np->tx_ring_dma = np->rx_done_q_dma + rx_done_q_size;
  938. np->rx_ring = (void *) np->tx_ring + tx_ring_size;
  939. np->rx_ring_dma = np->tx_ring_dma + tx_ring_size;
  940. }
  941. /* Start with no carrier, it gets adjusted later */
  942. netif_carrier_off(dev);
  943. init_ring(dev);
  944. /* Set the size of the Rx buffers. */
  945. writel((np->rx_buf_sz << RxBufferLenShift) |
  946. (0 << RxMinDescrThreshShift) |
  947. RxPrefetchMode | RxVariableQ |
  948. RX_Q_ENTRIES |
  949. RX_DESC_Q_ADDR_SIZE | RX_DESC_ADDR_SIZE |
  950. RxDescSpace4,
  951. ioaddr + RxDescQCtrl);
  952. /* Set up the Rx DMA controller. */
  953. writel(RxChecksumIgnore |
  954. (0 << RxEarlyIntThreshShift) |
  955. (6 << RxHighPrioThreshShift) |
  956. ((DMA_BURST_SIZE / 32) << RxBurstSizeShift),
  957. ioaddr + RxDMACtrl);
  958. /* Set Tx descriptor */
  959. writel((2 << TxHiPriFIFOThreshShift) |
  960. (0 << TxPadLenShift) |
  961. ((DMA_BURST_SIZE / 32) << TxDMABurstSizeShift) |
  962. TX_DESC_Q_ADDR_SIZE |
  963. TX_DESC_SPACING | TX_DESC_TYPE,
  964. ioaddr + TxDescCtrl);
  965. writel( (np->queue_mem_dma >> 16) >> 16, ioaddr + RxDescQHiAddr);
  966. writel( (np->queue_mem_dma >> 16) >> 16, ioaddr + TxRingHiAddr);
  967. writel( (np->queue_mem_dma >> 16) >> 16, ioaddr + CompletionHiAddr);
  968. writel(np->rx_ring_dma, ioaddr + RxDescQAddr);
  969. writel(np->tx_ring_dma, ioaddr + TxRingPtr);
  970. writel(np->tx_done_q_dma, ioaddr + TxCompletionAddr);
  971. writel(np->rx_done_q_dma |
  972. RxComplType |
  973. (0 << RxComplThreshShift),
  974. ioaddr + RxCompletionAddr);
  975. if (debug > 1)
  976. printk(KERN_DEBUG "%s: Filling in the station address.\n", dev->name);
  977. /* Fill both the Tx SA register and the Rx perfect filter. */
  978. for (i = 0; i < 6; i++)
  979. writeb(dev->dev_addr[i], ioaddr + TxStationAddr + 5 - i);
  980. /* The first entry is special because it bypasses the VLAN filter.
  981. Don't use it. */
  982. writew(0, ioaddr + PerfFilterTable);
  983. writew(0, ioaddr + PerfFilterTable + 4);
  984. writew(0, ioaddr + PerfFilterTable + 8);
  985. for (i = 1; i < 16; i++) {
  986. u16 *eaddrs = (u16 *)dev->dev_addr;
  987. void __iomem *setup_frm = ioaddr + PerfFilterTable + i * 16;
  988. writew(cpu_to_be16(eaddrs[2]), setup_frm); setup_frm += 4;
  989. writew(cpu_to_be16(eaddrs[1]), setup_frm); setup_frm += 4;
  990. writew(cpu_to_be16(eaddrs[0]), setup_frm); setup_frm += 8;
  991. }
  992. /* Initialize other registers. */
  993. /* Configure the PCI bus bursts and FIFO thresholds. */
  994. np->tx_mode = TxFlowEnable|RxFlowEnable|PadEnable; /* modified when link is up. */
  995. writel(MiiSoftReset | np->tx_mode, ioaddr + TxMode);
  996. udelay(1000);
  997. writel(np->tx_mode, ioaddr + TxMode);
  998. np->tx_threshold = 4;
  999. writel(np->tx_threshold, ioaddr + TxThreshold);
  1000. writel(np->intr_timer_ctrl, ioaddr + IntrTimerCtrl);
  1001. netif_start_queue(dev);
  1002. if (debug > 1)
  1003. printk(KERN_DEBUG "%s: Setting the Rx and Tx modes.\n", dev->name);
  1004. set_rx_mode(dev);
  1005. np->mii_if.advertising = mdio_read(dev, np->phys[0], MII_ADVERTISE);
  1006. check_duplex(dev);
  1007. /* Enable GPIO interrupts on link change */
  1008. writel(0x0f00ff00, ioaddr + GPIOCtrl);
  1009. /* Set the interrupt mask */
  1010. writel(IntrRxDone | IntrRxEmpty | IntrDMAErr |
  1011. IntrTxDMADone | IntrStatsMax | IntrLinkChange |
  1012. IntrRxGFPDead | IntrNoTxCsum | IntrTxBadID,
  1013. ioaddr + IntrEnable);
  1014. /* Enable PCI interrupts. */
  1015. writel(0x00800000 | readl(ioaddr + PCIDeviceConfig),
  1016. ioaddr + PCIDeviceConfig);
  1017. #ifdef VLAN_SUPPORT
  1018. /* Set VLAN type to 802.1q */
  1019. writel(ETH_P_8021Q, ioaddr + VlanType);
  1020. #endif /* VLAN_SUPPORT */
  1021. /* Load Rx/Tx firmware into the frame processors */
  1022. for (i = 0; i < FIRMWARE_RX_SIZE * 2; i++)
  1023. writel(firmware_rx[i], ioaddr + RxGfpMem + i * 4);
  1024. for (i = 0; i < FIRMWARE_TX_SIZE * 2; i++)
  1025. writel(firmware_tx[i], ioaddr + TxGfpMem + i * 4);
  1026. if (enable_hw_cksum)
  1027. /* Enable the Rx and Tx units, and the Rx/Tx frame processors. */
  1028. writel(TxEnable|TxGFPEnable|RxEnable|RxGFPEnable, ioaddr + GenCtrl);
  1029. else
  1030. /* Enable the Rx and Tx units only. */
  1031. writel(TxEnable|RxEnable, ioaddr + GenCtrl);
  1032. if (debug > 1)
  1033. printk(KERN_DEBUG "%s: Done netdev_open().\n",
  1034. dev->name);
  1035. return 0;
  1036. }
  1037. static void check_duplex(struct net_device *dev)
  1038. {
  1039. struct netdev_private *np = netdev_priv(dev);
  1040. u16 reg0;
  1041. int silly_count = 1000;
  1042. mdio_write(dev, np->phys[0], MII_ADVERTISE, np->mii_if.advertising);
  1043. mdio_write(dev, np->phys[0], MII_BMCR, BMCR_RESET);
  1044. udelay(500);
  1045. while (--silly_count && mdio_read(dev, np->phys[0], MII_BMCR) & BMCR_RESET)
  1046. /* do nothing */;
  1047. if (!silly_count) {
  1048. printk("%s: MII reset failed!\n", dev->name);
  1049. return;
  1050. }
  1051. reg0 = mdio_read(dev, np->phys[0], MII_BMCR);
  1052. if (!np->mii_if.force_media) {
  1053. reg0 |= BMCR_ANENABLE | BMCR_ANRESTART;
  1054. } else {
  1055. reg0 &= ~(BMCR_ANENABLE | BMCR_ANRESTART);
  1056. if (np->speed100)
  1057. reg0 |= BMCR_SPEED100;
  1058. if (np->mii_if.full_duplex)
  1059. reg0 |= BMCR_FULLDPLX;
  1060. printk(KERN_DEBUG "%s: Link forced to %sMbit %s-duplex\n",
  1061. dev->name,
  1062. np->speed100 ? "100" : "10",
  1063. np->mii_if.full_duplex ? "full" : "half");
  1064. }
  1065. mdio_write(dev, np->phys[0], MII_BMCR, reg0);
  1066. }
  1067. static void tx_timeout(struct net_device *dev)
  1068. {
  1069. struct netdev_private *np = netdev_priv(dev);
  1070. void __iomem *ioaddr = np->base;
  1071. int old_debug;
  1072. printk(KERN_WARNING "%s: Transmit timed out, status %#8.8x, "
  1073. "resetting...\n", dev->name, (int) readl(ioaddr + IntrStatus));
  1074. /* Perhaps we should reinitialize the hardware here. */
  1075. /*
  1076. * Stop and restart the interface.
  1077. * Cheat and increase the debug level temporarily.
  1078. */
  1079. old_debug = debug;
  1080. debug = 2;
  1081. netdev_close(dev);
  1082. netdev_open(dev);
  1083. debug = old_debug;
  1084. /* Trigger an immediate transmit demand. */
  1085. dev->trans_start = jiffies;
  1086. np->stats.tx_errors++;
  1087. netif_wake_queue(dev);
  1088. }
  1089. /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
  1090. static void init_ring(struct net_device *dev)
  1091. {
  1092. struct netdev_private *np = netdev_priv(dev);
  1093. int i;
  1094. np->cur_rx = np->cur_tx = np->reap_tx = 0;
  1095. np->dirty_rx = np->dirty_tx = np->rx_done = np->tx_done = 0;
  1096. np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
  1097. /* Fill in the Rx buffers. Handle allocation failure gracefully. */
  1098. for (i = 0; i < RX_RING_SIZE; i++) {
  1099. struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz);
  1100. np->rx_info[i].skb = skb;
  1101. if (skb == NULL)
  1102. break;
  1103. np->rx_info[i].mapping = pci_map_single(np->pci_dev, skb->data, np->rx_buf_sz, PCI_DMA_FROMDEVICE);
  1104. skb->dev = dev; /* Mark as being used by this device. */
  1105. /* Grrr, we cannot offset to correctly align the IP header. */
  1106. np->rx_ring[i].rxaddr = cpu_to_dma(np->rx_info[i].mapping | RxDescValid);
  1107. }
  1108. writew(i - 1, np->base + RxDescQIdx);
  1109. np->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
  1110. /* Clear the remainder of the Rx buffer ring. */
  1111. for ( ; i < RX_RING_SIZE; i++) {
  1112. np->rx_ring[i].rxaddr = 0;
  1113. np->rx_info[i].skb = NULL;
  1114. np->rx_info[i].mapping = 0;
  1115. }
  1116. /* Mark the last entry as wrapping the ring. */
  1117. np->rx_ring[RX_RING_SIZE - 1].rxaddr |= cpu_to_dma(RxDescEndRing);
  1118. /* Clear the completion rings. */
  1119. for (i = 0; i < DONE_Q_SIZE; i++) {
  1120. np->rx_done_q[i].status = 0;
  1121. np->tx_done_q[i].status = 0;
  1122. }
  1123. for (i = 0; i < TX_RING_SIZE; i++)
  1124. memset(&np->tx_info[i], 0, sizeof(np->tx_info[i]));
  1125. return;
  1126. }
  1127. static int start_tx(struct sk_buff *skb, struct net_device *dev)
  1128. {
  1129. struct netdev_private *np = netdev_priv(dev);
  1130. unsigned int entry;
  1131. u32 status;
  1132. int i;
  1133. /*
  1134. * be cautious here, wrapping the queue has weird semantics
  1135. * and we may not have enough slots even when it seems we do.
  1136. */
  1137. if ((np->cur_tx - np->dirty_tx) + skb_num_frags(skb) * 2 > TX_RING_SIZE) {
  1138. netif_stop_queue(dev);
  1139. return 1;
  1140. }
  1141. #if defined(ZEROCOPY) && defined(HAS_BROKEN_FIRMWARE)
  1142. if (skb->ip_summed == CHECKSUM_HW) {
  1143. skb = skb_padto(skb, (skb->len + PADDING_MASK) & ~PADDING_MASK);
  1144. if (skb == NULL)
  1145. return NETDEV_TX_OK;
  1146. }
  1147. #endif /* ZEROCOPY && HAS_BROKEN_FIRMWARE */
  1148. entry = np->cur_tx % TX_RING_SIZE;
  1149. for (i = 0; i < skb_num_frags(skb); i++) {
  1150. int wrap_ring = 0;
  1151. status = TxDescID;
  1152. if (i == 0) {
  1153. np->tx_info[entry].skb = skb;
  1154. status |= TxCRCEn;
  1155. if (entry >= TX_RING_SIZE - skb_num_frags(skb)) {
  1156. status |= TxRingWrap;
  1157. wrap_ring = 1;
  1158. }
  1159. if (np->reap_tx) {
  1160. status |= TxDescIntr;
  1161. np->reap_tx = 0;
  1162. }
  1163. if (skb->ip_summed == CHECKSUM_HW) {
  1164. status |= TxCalTCP;
  1165. np->stats.tx_compressed++;
  1166. }
  1167. status |= skb_first_frag_len(skb) | (skb_num_frags(skb) << 16);
  1168. np->tx_info[entry].mapping =
  1169. pci_map_single(np->pci_dev, skb->data, skb_first_frag_len(skb), PCI_DMA_TODEVICE);
  1170. } else {
  1171. skb_frag_t *this_frag = &skb_shinfo(skb)->frags[i - 1];
  1172. status |= this_frag->size;
  1173. np->tx_info[entry].mapping =
  1174. pci_map_single(np->pci_dev, page_address(this_frag->page) + this_frag->page_offset, this_frag->size, PCI_DMA_TODEVICE);
  1175. }
  1176. np->tx_ring[entry].addr = cpu_to_dma(np->tx_info[entry].mapping);
  1177. np->tx_ring[entry].status = cpu_to_le32(status);
  1178. if (debug > 3)
  1179. printk(KERN_DEBUG "%s: Tx #%d/#%d slot %d status %#8.8x.\n",
  1180. dev->name, np->cur_tx, np->dirty_tx,
  1181. entry, status);
  1182. if (wrap_ring) {
  1183. np->tx_info[entry].used_slots = TX_RING_SIZE - entry;
  1184. np->cur_tx += np->tx_info[entry].used_slots;
  1185. entry = 0;
  1186. } else {
  1187. np->tx_info[entry].used_slots = 1;
  1188. np->cur_tx += np->tx_info[entry].used_slots;
  1189. entry++;
  1190. }
  1191. /* scavenge the tx descriptors twice per TX_RING_SIZE */
  1192. if (np->cur_tx % (TX_RING_SIZE / 2) == 0)
  1193. np->reap_tx = 1;
  1194. }
  1195. /* Non-x86: explicitly flush descriptor cache lines here. */
  1196. /* Ensure all descriptors are written back before the transmit is
  1197. initiated. - Jes */
  1198. wmb();
  1199. /* Update the producer index. */
  1200. writel(entry * (sizeof(starfire_tx_desc) / 8), np->base + TxProducerIdx);
  1201. /* 4 is arbitrary, but should be ok */
  1202. if ((np->cur_tx - np->dirty_tx) + 4 > TX_RING_SIZE)
  1203. netif_stop_queue(dev);
  1204. dev->trans_start = jiffies;
  1205. return 0;
  1206. }
  1207. /* The interrupt handler does all of the Rx thread work and cleans up
  1208. after the Tx thread. */
  1209. static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *rgs)
  1210. {
  1211. struct net_device *dev = dev_instance;
  1212. struct netdev_private *np = netdev_priv(dev);
  1213. void __iomem *ioaddr = np->base;
  1214. int boguscnt = max_interrupt_work;
  1215. int consumer;
  1216. int tx_status;
  1217. int handled = 0;
  1218. do {
  1219. u32 intr_status = readl(ioaddr + IntrClear);
  1220. if (debug > 4)
  1221. printk(KERN_DEBUG "%s: Interrupt status %#8.8x.\n",
  1222. dev->name, intr_status);
  1223. if (intr_status == 0 || intr_status == (u32) -1)
  1224. break;
  1225. handled = 1;
  1226. if (intr_status & (IntrRxDone | IntrRxEmpty))
  1227. netdev_rx(dev, ioaddr);
  1228. /* Scavenge the skbuff list based on the Tx-done queue.
  1229. There are redundant checks here that may be cleaned up
  1230. after the driver has proven to be reliable. */
  1231. consumer = readl(ioaddr + TxConsumerIdx);
  1232. if (debug > 3)
  1233. printk(KERN_DEBUG "%s: Tx Consumer index is %d.\n",
  1234. dev->name, consumer);
  1235. while ((tx_status = le32_to_cpu(np->tx_done_q[np->tx_done].status)) != 0) {
  1236. if (debug > 3)
  1237. printk(KERN_DEBUG "%s: Tx completion #%d entry %d is %#8.8x.\n",
  1238. dev->name, np->dirty_tx, np->tx_done, tx_status);
  1239. if ((tx_status & 0xe0000000) == 0xa0000000) {
  1240. np->stats.tx_packets++;
  1241. } else if ((tx_status & 0xe0000000) == 0x80000000) {
  1242. u16 entry = (tx_status & 0x7fff) / sizeof(starfire_tx_desc);
  1243. struct sk_buff *skb = np->tx_info[entry].skb;
  1244. np->tx_info[entry].skb = NULL;
  1245. pci_unmap_single(np->pci_dev,
  1246. np->tx_info[entry].mapping,
  1247. skb_first_frag_len(skb),
  1248. PCI_DMA_TODEVICE);
  1249. np->tx_info[entry].mapping = 0;
  1250. np->dirty_tx += np->tx_info[entry].used_slots;
  1251. entry = (entry + np->tx_info[entry].used_slots) % TX_RING_SIZE;
  1252. {
  1253. int i;
  1254. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1255. pci_unmap_single(np->pci_dev,
  1256. np->tx_info[entry].mapping,
  1257. skb_shinfo(skb)->frags[i].size,
  1258. PCI_DMA_TODEVICE);
  1259. np->dirty_tx++;
  1260. entry++;
  1261. }
  1262. }
  1263. dev_kfree_skb_irq(skb);
  1264. }
  1265. np->tx_done_q[np->tx_done].status = 0;
  1266. np->tx_done = (np->tx_done + 1) % DONE_Q_SIZE;
  1267. }
  1268. writew(np->tx_done, ioaddr + CompletionQConsumerIdx + 2);
  1269. if (netif_queue_stopped(dev) &&
  1270. (np->cur_tx - np->dirty_tx + 4 < TX_RING_SIZE)) {
  1271. /* The ring is no longer full, wake the queue. */
  1272. netif_wake_queue(dev);
  1273. }
  1274. /* Stats overflow */
  1275. if (intr_status & IntrStatsMax)
  1276. get_stats(dev);
  1277. /* Media change interrupt. */
  1278. if (intr_status & IntrLinkChange)
  1279. netdev_media_change(dev);
  1280. /* Abnormal error summary/uncommon events handlers. */
  1281. if (intr_status & IntrAbnormalSummary)
  1282. netdev_error(dev, intr_status);
  1283. if (--boguscnt < 0) {
  1284. if (debug > 1)
  1285. printk(KERN_WARNING "%s: Too much work at interrupt, "
  1286. "status=%#8.8x.\n",
  1287. dev->name, intr_status);
  1288. break;
  1289. }
  1290. } while (1);
  1291. if (debug > 4)
  1292. printk(KERN_DEBUG "%s: exiting interrupt, status=%#8.8x.\n",
  1293. dev->name, (int) readl(ioaddr + IntrStatus));
  1294. return IRQ_RETVAL(handled);
  1295. }
  1296. /* This routine is logically part of the interrupt/poll handler, but separated
  1297. for clarity, code sharing between NAPI/non-NAPI, and better register allocation. */
  1298. static int __netdev_rx(struct net_device *dev, int *quota)
  1299. {
  1300. struct netdev_private *np = netdev_priv(dev);
  1301. u32 desc_status;
  1302. int retcode = 0;
  1303. /* If EOP is set on the next entry, it's a new packet. Send it up. */
  1304. while ((desc_status = le32_to_cpu(np->rx_done_q[np->rx_done].status)) != 0) {
  1305. struct sk_buff *skb;
  1306. u16 pkt_len;
  1307. int entry;
  1308. rx_done_desc *desc = &np->rx_done_q[np->rx_done];
  1309. if (debug > 4)
  1310. printk(KERN_DEBUG " netdev_rx() status of %d was %#8.8x.\n", np->rx_done, desc_status);
  1311. if (!(desc_status & RxOK)) {
  1312. /* There was an error. */
  1313. if (debug > 2)
  1314. printk(KERN_DEBUG " netdev_rx() Rx error was %#8.8x.\n", desc_status);
  1315. np->stats.rx_errors++;
  1316. if (desc_status & RxFIFOErr)
  1317. np->stats.rx_fifo_errors++;
  1318. goto next_rx;
  1319. }
  1320. if (*quota <= 0) { /* out of rx quota */
  1321. retcode = 1;
  1322. goto out;
  1323. }
  1324. (*quota)--;
  1325. pkt_len = desc_status; /* Implicitly Truncate */
  1326. entry = (desc_status >> 16) & 0x7ff;
  1327. if (debug > 4)
  1328. printk(KERN_DEBUG " netdev_rx() normal Rx pkt length %d, quota %d.\n", pkt_len, *quota);
  1329. /* Check if the packet is long enough to accept without copying
  1330. to a minimally-sized skbuff. */
  1331. if (pkt_len < rx_copybreak
  1332. && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
  1333. skb->dev = dev;
  1334. skb_reserve(skb, 2); /* 16 byte align the IP header */
  1335. pci_dma_sync_single_for_cpu(np->pci_dev,
  1336. np->rx_info[entry].mapping,
  1337. pkt_len, PCI_DMA_FROMDEVICE);
  1338. eth_copy_and_sum(skb, np->rx_info[entry].skb->data, pkt_len, 0);
  1339. pci_dma_sync_single_for_device(np->pci_dev,
  1340. np->rx_info[entry].mapping,
  1341. pkt_len, PCI_DMA_FROMDEVICE);
  1342. skb_put(skb, pkt_len);
  1343. } else {
  1344. pci_unmap_single(np->pci_dev, np->rx_info[entry].mapping, np->rx_buf_sz, PCI_DMA_FROMDEVICE);
  1345. skb = np->rx_info[entry].skb;
  1346. skb_put(skb, pkt_len);
  1347. np->rx_info[entry].skb = NULL;
  1348. np->rx_info[entry].mapping = 0;
  1349. }
  1350. #ifndef final_version /* Remove after testing. */
  1351. /* You will want this info for the initial debug. */
  1352. if (debug > 5)
  1353. printk(KERN_DEBUG " Rx data %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:"
  1354. "%2.2x %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x %2.2x%2.2x.\n",
  1355. skb->data[0], skb->data[1], skb->data[2], skb->data[3],
  1356. skb->data[4], skb->data[5], skb->data[6], skb->data[7],
  1357. skb->data[8], skb->data[9], skb->data[10],
  1358. skb->data[11], skb->data[12], skb->data[13]);
  1359. #endif
  1360. skb->protocol = eth_type_trans(skb, dev);
  1361. #ifdef VLAN_SUPPORT
  1362. if (debug > 4)
  1363. printk(KERN_DEBUG " netdev_rx() status2 of %d was %#4.4x.\n", np->rx_done, le16_to_cpu(desc->status2));
  1364. #endif
  1365. if (le16_to_cpu(desc->status2) & 0x0100) {
  1366. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1367. np->stats.rx_compressed++;
  1368. }
  1369. /*
  1370. * This feature doesn't seem to be working, at least
  1371. * with the two firmware versions I have. If the GFP sees
  1372. * an IP fragment, it either ignores it completely, or reports
  1373. * "bad checksum" on it.
  1374. *
  1375. * Maybe I missed something -- corrections are welcome.
  1376. * Until then, the printk stays. :-) -Ion
  1377. */
  1378. else if (le16_to_cpu(desc->status2) & 0x0040) {
  1379. skb->ip_summed = CHECKSUM_HW;
  1380. skb->csum = le16_to_cpu(desc->csum);
  1381. printk(KERN_DEBUG "%s: checksum_hw, status2 = %#x\n", dev->name, le16_to_cpu(desc->status2));
  1382. }
  1383. #ifdef VLAN_SUPPORT
  1384. if (np->vlgrp && le16_to_cpu(desc->status2) & 0x0200) {
  1385. if (debug > 4)
  1386. printk(KERN_DEBUG " netdev_rx() vlanid = %d\n", le16_to_cpu(desc->vlanid));
  1387. /* vlan_netdev_receive_skb() expects a packet with the VLAN tag stripped out */
  1388. vlan_netdev_receive_skb(skb, np->vlgrp, le16_to_cpu(desc->vlanid) & VLAN_VID_MASK);
  1389. } else
  1390. #endif /* VLAN_SUPPORT */
  1391. netdev_receive_skb(skb);
  1392. dev->last_rx = jiffies;
  1393. np->stats.rx_packets++;
  1394. next_rx:
  1395. np->cur_rx++;
  1396. desc->status = 0;
  1397. np->rx_done = (np->rx_done + 1) % DONE_Q_SIZE;
  1398. }
  1399. writew(np->rx_done, np->base + CompletionQConsumerIdx);
  1400. out:
  1401. refill_rx_ring(dev);
  1402. if (debug > 5)
  1403. printk(KERN_DEBUG " exiting netdev_rx(): %d, status of %d was %#8.8x.\n",
  1404. retcode, np->rx_done, desc_status);
  1405. return retcode;
  1406. }
  1407. #ifdef HAVE_NETDEV_POLL
  1408. static int netdev_poll(struct net_device *dev, int *budget)
  1409. {
  1410. u32 intr_status;
  1411. struct netdev_private *np = netdev_priv(dev);
  1412. void __iomem *ioaddr = np->base;
  1413. int retcode = 0, quota = dev->quota;
  1414. do {
  1415. writel(IntrRxDone | IntrRxEmpty, ioaddr + IntrClear);
  1416. retcode = __netdev_rx(dev, &quota);
  1417. *budget -= (dev->quota - quota);
  1418. dev->quota = quota;
  1419. if (retcode)
  1420. goto out;
  1421. intr_status = readl(ioaddr + IntrStatus);
  1422. } while (intr_status & (IntrRxDone | IntrRxEmpty));
  1423. netif_rx_complete(dev);
  1424. intr_status = readl(ioaddr + IntrEnable);
  1425. intr_status |= IntrRxDone | IntrRxEmpty;
  1426. writel(intr_status, ioaddr + IntrEnable);
  1427. out:
  1428. if (debug > 5)
  1429. printk(KERN_DEBUG " exiting netdev_poll(): %d.\n", retcode);
  1430. /* Restart Rx engine if stopped. */
  1431. return retcode;
  1432. }
  1433. #endif /* HAVE_NETDEV_POLL */
  1434. static void refill_rx_ring(struct net_device *dev)
  1435. {
  1436. struct netdev_private *np = netdev_priv(dev);
  1437. struct sk_buff *skb;
  1438. int entry = -1;
  1439. /* Refill the Rx ring buffers. */
  1440. for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {
  1441. entry = np->dirty_rx % RX_RING_SIZE;
  1442. if (np->rx_info[entry].skb == NULL) {
  1443. skb = dev_alloc_skb(np->rx_buf_sz);
  1444. np->rx_info[entry].skb = skb;
  1445. if (skb == NULL)
  1446. break; /* Better luck next round. */
  1447. np->rx_info[entry].mapping =
  1448. pci_map_single(np->pci_dev, skb->data, np->rx_buf_sz, PCI_DMA_FROMDEVICE);
  1449. skb->dev = dev; /* Mark as being used by this device. */
  1450. np->rx_ring[entry].rxaddr =
  1451. cpu_to_dma(np->rx_info[entry].mapping | RxDescValid);
  1452. }
  1453. if (entry == RX_RING_SIZE - 1)
  1454. np->rx_ring[entry].rxaddr |= cpu_to_dma(RxDescEndRing);
  1455. }
  1456. if (entry >= 0)
  1457. writew(entry, np->base + RxDescQIdx);
  1458. }
  1459. static void netdev_media_change(struct net_device *dev)
  1460. {
  1461. struct netdev_private *np = netdev_priv(dev);
  1462. void __iomem *ioaddr = np->base;
  1463. u16 reg0, reg1, reg4, reg5;
  1464. u32 new_tx_mode;
  1465. u32 new_intr_timer_ctrl;
  1466. /* reset status first */
  1467. mdio_read(dev, np->phys[0], MII_BMCR);
  1468. mdio_read(dev, np->phys[0], MII_BMSR);
  1469. reg0 = mdio_read(dev, np->phys[0], MII_BMCR);
  1470. reg1 = mdio_read(dev, np->phys[0], MII_BMSR);
  1471. if (reg1 & BMSR_LSTATUS) {
  1472. /* link is up */
  1473. if (reg0 & BMCR_ANENABLE) {
  1474. /* autonegotiation is enabled */
  1475. reg4 = mdio_read(dev, np->phys[0], MII_ADVERTISE);
  1476. reg5 = mdio_read(dev, np->phys[0], MII_LPA);
  1477. if (reg4 & ADVERTISE_100FULL && reg5 & LPA_100FULL) {
  1478. np->speed100 = 1;
  1479. np->mii_if.full_duplex = 1;
  1480. } else if (reg4 & ADVERTISE_100HALF && reg5 & LPA_100HALF) {
  1481. np->speed100 = 1;
  1482. np->mii_if.full_duplex = 0;
  1483. } else if (reg4 & ADVERTISE_10FULL && reg5 & LPA_10FULL) {
  1484. np->speed100 = 0;
  1485. np->mii_if.full_duplex = 1;
  1486. } else {
  1487. np->speed100 = 0;
  1488. np->mii_if.full_duplex = 0;
  1489. }
  1490. } else {
  1491. /* autonegotiation is disabled */
  1492. if (reg0 & BMCR_SPEED100)
  1493. np->speed100 = 1;
  1494. else
  1495. np->speed100 = 0;
  1496. if (reg0 & BMCR_FULLDPLX)
  1497. np->mii_if.full_duplex = 1;
  1498. else
  1499. np->mii_if.full_duplex = 0;
  1500. }
  1501. netif_carrier_on(dev);
  1502. printk(KERN_DEBUG "%s: Link is up, running at %sMbit %s-duplex\n",
  1503. dev->name,
  1504. np->speed100 ? "100" : "10",
  1505. np->mii_if.full_duplex ? "full" : "half");
  1506. new_tx_mode = np->tx_mode & ~FullDuplex; /* duplex setting */
  1507. if (np->mii_if.full_duplex)
  1508. new_tx_mode |= FullDuplex;
  1509. if (np->tx_mode != new_tx_mode) {
  1510. np->tx_mode = new_tx_mode;
  1511. writel(np->tx_mode | MiiSoftReset, ioaddr + TxMode);
  1512. udelay(1000);
  1513. writel(np->tx_mode, ioaddr + TxMode);
  1514. }
  1515. new_intr_timer_ctrl = np->intr_timer_ctrl & ~Timer10X;
  1516. if (np->speed100)
  1517. new_intr_timer_ctrl |= Timer10X;
  1518. if (np->intr_timer_ctrl != new_intr_timer_ctrl) {
  1519. np->intr_timer_ctrl = new_intr_timer_ctrl;
  1520. writel(new_intr_timer_ctrl, ioaddr + IntrTimerCtrl);
  1521. }
  1522. } else {
  1523. netif_carrier_off(dev);
  1524. printk(KERN_DEBUG "%s: Link is down\n", dev->name);
  1525. }
  1526. }
  1527. static void netdev_error(struct net_device *dev, int intr_status)
  1528. {
  1529. struct netdev_private *np = netdev_priv(dev);
  1530. /* Came close to underrunning the Tx FIFO, increase threshold. */
  1531. if (intr_status & IntrTxDataLow) {
  1532. if (np->tx_threshold <= PKT_BUF_SZ / 16) {
  1533. writel(++np->tx_threshold, np->base + TxThreshold);
  1534. printk(KERN_NOTICE "%s: PCI bus congestion, increasing Tx FIFO threshold to %d bytes\n",
  1535. dev->name, np->tx_threshold * 16);
  1536. } else
  1537. printk(KERN_WARNING "%s: PCI Tx underflow -- adapter is probably malfunctioning\n", dev->name);
  1538. }
  1539. if (intr_status & IntrRxGFPDead) {
  1540. np->stats.rx_fifo_errors++;
  1541. np->stats.rx_errors++;
  1542. }
  1543. if (intr_status & (IntrNoTxCsum | IntrDMAErr)) {
  1544. np->stats.tx_fifo_errors++;
  1545. np->stats.tx_errors++;
  1546. }
  1547. if ((intr_status & ~(IntrNormalMask | IntrAbnormalSummary | IntrLinkChange | IntrStatsMax | IntrTxDataLow | IntrRxGFPDead | IntrNoTxCsum | IntrPCIPad)) && debug)
  1548. printk(KERN_ERR "%s: Something Wicked happened! %#8.8x.\n",
  1549. dev->name, intr_status);
  1550. }
  1551. static struct net_device_stats *get_stats(struct net_device *dev)
  1552. {
  1553. struct netdev_private *np = netdev_priv(dev);
  1554. void __iomem *ioaddr = np->base;
  1555. /* This adapter architecture needs no SMP locks. */
  1556. np->stats.tx_bytes = readl(ioaddr + 0x57010);
  1557. np->stats.rx_bytes = readl(ioaddr + 0x57044);
  1558. np->stats.tx_packets = readl(ioaddr + 0x57000);
  1559. np->stats.tx_aborted_errors =
  1560. readl(ioaddr + 0x57024) + readl(ioaddr + 0x57028);
  1561. np->stats.tx_window_errors = readl(ioaddr + 0x57018);
  1562. np->stats.collisions =
  1563. readl(ioaddr + 0x57004) + readl(ioaddr + 0x57008);
  1564. /* The chip only need report frame silently dropped. */
  1565. np->stats.rx_dropped += readw(ioaddr + RxDMAStatus);
  1566. writew(0, ioaddr + RxDMAStatus);
  1567. np->stats.rx_crc_errors = readl(ioaddr + 0x5703C);
  1568. np->stats.rx_frame_errors = readl(ioaddr + 0x57040);
  1569. np->stats.rx_length_errors = readl(ioaddr + 0x57058);
  1570. np->stats.rx_missed_errors = readl(ioaddr + 0x5707C);
  1571. return &np->stats;
  1572. }
  1573. static void set_rx_mode(struct net_device *dev)
  1574. {
  1575. struct netdev_private *np = netdev_priv(dev);
  1576. void __iomem *ioaddr = np->base;
  1577. u32 rx_mode = MinVLANPrio;
  1578. struct dev_mc_list *mclist;
  1579. int i;
  1580. #ifdef VLAN_SUPPORT
  1581. rx_mode |= VlanMode;
  1582. if (np->vlgrp) {
  1583. int vlan_count = 0;
  1584. void __iomem *filter_addr = ioaddr + HashTable + 8;
  1585. for (i = 0; i < VLAN_VID_MASK; i++) {
  1586. if (np->vlgrp->vlan_devices[i]) {
  1587. if (vlan_count >= 32)
  1588. break;
  1589. writew(cpu_to_be16(i), filter_addr);
  1590. filter_addr += 16;
  1591. vlan_count++;
  1592. }
  1593. }
  1594. if (i == VLAN_VID_MASK) {
  1595. rx_mode |= PerfectFilterVlan;
  1596. while (vlan_count < 32) {
  1597. writew(0, filter_addr);
  1598. filter_addr += 16;
  1599. vlan_count++;
  1600. }
  1601. }
  1602. }
  1603. #endif /* VLAN_SUPPORT */
  1604. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  1605. rx_mode |= AcceptAll;
  1606. } else if ((dev->mc_count > multicast_filter_limit)
  1607. || (dev->flags & IFF_ALLMULTI)) {
  1608. /* Too many to match, or accept all multicasts. */
  1609. rx_mode |= AcceptBroadcast|AcceptAllMulticast|PerfectFilter;
  1610. } else if (dev->mc_count <= 14) {
  1611. /* Use the 16 element perfect filter, skip first two entries. */
  1612. void __iomem *filter_addr = ioaddr + PerfFilterTable + 2 * 16;
  1613. u16 *eaddrs;
  1614. for (i = 2, mclist = dev->mc_list; mclist && i < dev->mc_count + 2;
  1615. i++, mclist = mclist->next) {
  1616. eaddrs = (u16 *)mclist->dmi_addr;
  1617. writew(cpu_to_be16(eaddrs[2]), filter_addr); filter_addr += 4;
  1618. writew(cpu_to_be16(eaddrs[1]), filter_addr); filter_addr += 4;
  1619. writew(cpu_to_be16(eaddrs[0]), filter_addr); filter_addr += 8;
  1620. }
  1621. eaddrs = (u16 *)dev->dev_addr;
  1622. while (i++ < 16) {
  1623. writew(cpu_to_be16(eaddrs[0]), filter_addr); filter_addr += 4;
  1624. writew(cpu_to_be16(eaddrs[1]), filter_addr); filter_addr += 4;
  1625. writew(cpu_to_be16(eaddrs[2]), filter_addr); filter_addr += 8;
  1626. }
  1627. rx_mode |= AcceptBroadcast|PerfectFilter;
  1628. } else {
  1629. /* Must use a multicast hash table. */
  1630. void __iomem *filter_addr;
  1631. u16 *eaddrs;
  1632. u16 mc_filter[32] __attribute__ ((aligned(sizeof(long)))); /* Multicast hash filter */
  1633. memset(mc_filter, 0, sizeof(mc_filter));
  1634. for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
  1635. i++, mclist = mclist->next) {
  1636. /* The chip uses the upper 9 CRC bits
  1637. as index into the hash table */
  1638. int bit_nr = ether_crc_le(ETH_ALEN, mclist->dmi_addr) >> 23;
  1639. __u32 *fptr = (__u32 *) &mc_filter[(bit_nr >> 4) & ~1];
  1640. *fptr |= cpu_to_le32(1 << (bit_nr & 31));
  1641. }
  1642. /* Clear the perfect filter list, skip first two entries. */
  1643. filter_addr = ioaddr + PerfFilterTable + 2 * 16;
  1644. eaddrs = (u16 *)dev->dev_addr;
  1645. for (i = 2; i < 16; i++) {
  1646. writew(cpu_to_be16(eaddrs[0]), filter_addr); filter_addr += 4;
  1647. writew(cpu_to_be16(eaddrs[1]), filter_addr); filter_addr += 4;
  1648. writew(cpu_to_be16(eaddrs[2]), filter_addr); filter_addr += 8;
  1649. }
  1650. for (filter_addr = ioaddr + HashTable, i = 0; i < 32; filter_addr+= 16, i++)
  1651. writew(mc_filter[i], filter_addr);
  1652. rx_mode |= AcceptBroadcast|PerfectFilter|HashFilter;
  1653. }
  1654. writel(rx_mode, ioaddr + RxFilterMode);
  1655. }
  1656. static int check_if_running(struct net_device *dev)
  1657. {
  1658. if (!netif_running(dev))
  1659. return -EINVAL;
  1660. return 0;
  1661. }
  1662. static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1663. {
  1664. struct netdev_private *np = netdev_priv(dev);
  1665. strcpy(info->driver, DRV_NAME);
  1666. strcpy(info->version, DRV_VERSION);
  1667. strcpy(info->bus_info, pci_name(np->pci_dev));
  1668. }
  1669. static int get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  1670. {
  1671. struct netdev_private *np = netdev_priv(dev);
  1672. spin_lock_irq(&np->lock);
  1673. mii_ethtool_gset(&np->mii_if, ecmd);
  1674. spin_unlock_irq(&np->lock);
  1675. return 0;
  1676. }
  1677. static int set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  1678. {
  1679. struct netdev_private *np = netdev_priv(dev);
  1680. int res;
  1681. spin_lock_irq(&np->lock);
  1682. res = mii_ethtool_sset(&np->mii_if, ecmd);
  1683. spin_unlock_irq(&np->lock);
  1684. check_duplex(dev);
  1685. return res;
  1686. }
  1687. static int nway_reset(struct net_device *dev)
  1688. {
  1689. struct netdev_private *np = netdev_priv(dev);
  1690. return mii_nway_restart(&np->mii_if);
  1691. }
  1692. static u32 get_link(struct net_device *dev)
  1693. {
  1694. struct netdev_private *np = netdev_priv(dev);
  1695. return mii_link_ok(&np->mii_if);
  1696. }
  1697. static u32 get_msglevel(struct net_device *dev)
  1698. {
  1699. return debug;
  1700. }
  1701. static void set_msglevel(struct net_device *dev, u32 val)
  1702. {
  1703. debug = val;
  1704. }
  1705. static struct ethtool_ops ethtool_ops = {
  1706. .begin = check_if_running,
  1707. .get_drvinfo = get_drvinfo,
  1708. .get_settings = get_settings,
  1709. .set_settings = set_settings,
  1710. .nway_reset = nway_reset,
  1711. .get_link = get_link,
  1712. .get_msglevel = get_msglevel,
  1713. .set_msglevel = set_msglevel,
  1714. };
  1715. static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1716. {
  1717. struct netdev_private *np = netdev_priv(dev);
  1718. struct mii_ioctl_data *data = if_mii(rq);
  1719. int rc;
  1720. if (!netif_running(dev))
  1721. return -EINVAL;
  1722. spin_lock_irq(&np->lock);
  1723. rc = generic_mii_ioctl(&np->mii_if, data, cmd, NULL);
  1724. spin_unlock_irq(&np->lock);
  1725. if ((cmd == SIOCSMIIREG) && (data->phy_id == np->phys[0]))
  1726. check_duplex(dev);
  1727. return rc;
  1728. }
  1729. static int netdev_close(struct net_device *dev)
  1730. {
  1731. struct netdev_private *np = netdev_priv(dev);
  1732. void __iomem *ioaddr = np->base;
  1733. int i;
  1734. netif_stop_queue(dev);
  1735. if (debug > 1) {
  1736. printk(KERN_DEBUG "%s: Shutting down ethercard, Intr status %#8.8x.\n",
  1737. dev->name, (int) readl(ioaddr + IntrStatus));
  1738. printk(KERN_DEBUG "%s: Queue pointers were Tx %d / %d, Rx %d / %d.\n",
  1739. dev->name, np->cur_tx, np->dirty_tx,
  1740. np->cur_rx, np->dirty_rx);
  1741. }
  1742. /* Disable interrupts by clearing the interrupt mask. */
  1743. writel(0, ioaddr + IntrEnable);
  1744. /* Stop the chip's Tx and Rx processes. */
  1745. writel(0, ioaddr + GenCtrl);
  1746. readl(ioaddr + GenCtrl);
  1747. if (debug > 5) {
  1748. printk(KERN_DEBUG" Tx ring at %#llx:\n",
  1749. (long long) np->tx_ring_dma);
  1750. for (i = 0; i < 8 /* TX_RING_SIZE is huge! */; i++)
  1751. printk(KERN_DEBUG " #%d desc. %#8.8x %#llx -> %#8.8x.\n",
  1752. i, le32_to_cpu(np->tx_ring[i].status),
  1753. (long long) dma_to_cpu(np->tx_ring[i].addr),
  1754. le32_to_cpu(np->tx_done_q[i].status));
  1755. printk(KERN_DEBUG " Rx ring at %#llx -> %p:\n",
  1756. (long long) np->rx_ring_dma, np->rx_done_q);
  1757. if (np->rx_done_q)
  1758. for (i = 0; i < 8 /* RX_RING_SIZE */; i++) {
  1759. printk(KERN_DEBUG " #%d desc. %#llx -> %#8.8x\n",
  1760. i, (long long) dma_to_cpu(np->rx_ring[i].rxaddr), le32_to_cpu(np->rx_done_q[i].status));
  1761. }
  1762. }
  1763. free_irq(dev->irq, dev);
  1764. /* Free all the skbuffs in the Rx queue. */
  1765. for (i = 0; i < RX_RING_SIZE; i++) {
  1766. np->rx_ring[i].rxaddr = cpu_to_dma(0xBADF00D0); /* An invalid address. */
  1767. if (np->rx_info[i].skb != NULL) {
  1768. pci_unmap_single(np->pci_dev, np->rx_info[i].mapping, np->rx_buf_sz, PCI_DMA_FROMDEVICE);
  1769. dev_kfree_skb(np->rx_info[i].skb);
  1770. }
  1771. np->rx_info[i].skb = NULL;
  1772. np->rx_info[i].mapping = 0;
  1773. }
  1774. for (i = 0; i < TX_RING_SIZE; i++) {
  1775. struct sk_buff *skb = np->tx_info[i].skb;
  1776. if (skb == NULL)
  1777. continue;
  1778. pci_unmap_single(np->pci_dev,
  1779. np->tx_info[i].mapping,
  1780. skb_first_frag_len(skb), PCI_DMA_TODEVICE);
  1781. np->tx_info[i].mapping = 0;
  1782. dev_kfree_skb(skb);
  1783. np->tx_info[i].skb = NULL;
  1784. }
  1785. return 0;
  1786. }
  1787. #ifdef CONFIG_PM
  1788. static int starfire_suspend(struct pci_dev *pdev, pm_message_t state)
  1789. {
  1790. struct net_device *dev = pci_get_drvdata(pdev);
  1791. if (netif_running(dev)) {
  1792. netif_device_detach(dev);
  1793. netdev_close(dev);
  1794. }
  1795. pci_save_state(pdev);
  1796. pci_set_power_state(pdev, pci_choose_state(pdev,state));
  1797. return 0;
  1798. }
  1799. static int starfire_resume(struct pci_dev *pdev)
  1800. {
  1801. struct net_device *dev = pci_get_drvdata(pdev);
  1802. pci_set_power_state(pdev, PCI_D0);
  1803. pci_restore_state(pdev);
  1804. if (netif_running(dev)) {
  1805. netdev_open(dev);
  1806. netif_device_attach(dev);
  1807. }
  1808. return 0;
  1809. }
  1810. #endif /* CONFIG_PM */
  1811. static void __devexit starfire_remove_one (struct pci_dev *pdev)
  1812. {
  1813. struct net_device *dev = pci_get_drvdata(pdev);
  1814. struct netdev_private *np = netdev_priv(dev);
  1815. BUG_ON(!dev);
  1816. unregister_netdev(dev);
  1817. if (np->queue_mem)
  1818. pci_free_consistent(pdev, np->queue_mem_size, np->queue_mem, np->queue_mem_dma);
  1819. /* XXX: add wakeup code -- requires firmware for MagicPacket */
  1820. pci_set_power_state(pdev, PCI_D3hot); /* go to sleep in D3 mode */
  1821. pci_disable_device(pdev);
  1822. iounmap(np->base);
  1823. pci_release_regions(pdev);
  1824. pci_set_drvdata(pdev, NULL);
  1825. free_netdev(dev); /* Will also free np!! */
  1826. }
  1827. static struct pci_driver starfire_driver = {
  1828. .name = DRV_NAME,
  1829. .probe = starfire_init_one,
  1830. .remove = __devexit_p(starfire_remove_one),
  1831. #ifdef CONFIG_PM
  1832. .suspend = starfire_suspend,
  1833. .resume = starfire_resume,
  1834. #endif /* CONFIG_PM */
  1835. .id_table = starfire_pci_tbl,
  1836. };
  1837. static int __init starfire_init (void)
  1838. {
  1839. /* when a module, this is printed whether or not devices are found in probe */
  1840. #ifdef MODULE
  1841. printk(version);
  1842. #ifdef HAVE_NETDEV_POLL
  1843. printk(KERN_INFO DRV_NAME ": polling (NAPI) enabled\n");
  1844. #else
  1845. printk(KERN_INFO DRV_NAME ": polling (NAPI) disabled\n");
  1846. #endif
  1847. #endif
  1848. /* we can do this test only at run-time... sigh */
  1849. if (sizeof(dma_addr_t) != sizeof(netdrv_addr_t)) {
  1850. printk("This driver has dma_addr_t issues, please send email to maintainer\n");
  1851. return -ENODEV;
  1852. }
  1853. return pci_module_init (&starfire_driver);
  1854. }
  1855. static void __exit starfire_cleanup (void)
  1856. {
  1857. pci_unregister_driver (&starfire_driver);
  1858. }
  1859. module_init(starfire_init);
  1860. module_exit(starfire_cleanup);
  1861. /*
  1862. * Local variables:
  1863. * c-basic-offset: 8
  1864. * tab-width: 8
  1865. * End:
  1866. */