natsemi.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:74k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* natsemi.c: A Linux PCI Ethernet driver for the NatSemi DP8381x series. */
  2. /*
  3. Written/copyright 1999-2001 by Donald Becker.
  4. Portions copyright (c) 2001,2002 Sun Microsystems (thockin@sun.com)
  5. Portions copyright 2001,2002 Manfred Spraul (manfred@colorfullife.com)
  6. This software may be used and distributed according to the terms of
  7. the GNU General Public License (GPL), incorporated herein by reference.
  8. Drivers based on or derived from this code fall under the GPL and must
  9. retain the authorship, copyright and license notice.  This file is not
  10. a complete program and may only be used when the entire operating
  11. system is licensed under the GPL.  License for under other terms may be
  12. available.  Contact the original author for details.
  13. The original author may be reached as becker@scyld.com, or at
  14. Scyld Computing Corporation
  15. 410 Severn Ave., Suite 210
  16. Annapolis MD 21403
  17. Support information and updates available at
  18. http://www.scyld.com/network/netsemi.html
  19. Linux kernel modifications:
  20. Version 1.0.1:
  21. - Spinlock fixes
  22. - Bug fixes and better intr performance (Tjeerd)
  23. Version 1.0.2:
  24. - Now reads correct MAC address from eeprom
  25. Version 1.0.3:
  26. - Eliminate redundant priv->tx_full flag
  27. - Call netif_start_queue from dev->tx_timeout
  28. - wmb() in start_tx() to flush data
  29. - Update Tx locking
  30. - Clean up PCI enable (davej)
  31. Version 1.0.4:
  32. - Merge Donald Becker's natsemi.c version 1.07
  33. Version 1.0.5:
  34. - { fill me in }
  35. Version 1.0.6:
  36. * ethtool support (jgarzik)
  37. * Proper initialization of the card (which sometimes
  38. fails to occur and leaves the card in a non-functional
  39. state). (uzi)
  40. * Some documented register settings to optimize some
  41. of the 100Mbit autodetection circuitry in rev C cards. (uzi)
  42. * Polling of the PHY intr for stuff like link state
  43. change and auto- negotiation to finally work properly. (uzi)
  44. * One-liner removal of a duplicate declaration of
  45. netdev_error(). (uzi)
  46. Version 1.0.7: (Manfred Spraul)
  47. * pci dma
  48. * SMP locking update
  49. * full reset added into tx_timeout
  50. * correct multicast hash generation (both big and little endian)
  51. [copied from a natsemi driver version
  52.  from Myrio Corporation, Greg Smith]
  53. * suspend/resume
  54. version 1.0.8 (Tim Hockin <thockin@sun.com>)
  55. * ETHTOOL_* support
  56. * Wake on lan support (Erik Gilling)
  57. * MXDMA fixes for serverworks
  58. * EEPROM reload
  59. version 1.0.9 (Manfred Spraul)
  60. * Main change: fix lack of synchronize
  61. netif_close/netif_suspend against a last interrupt
  62. or packet.
  63. * do not enable superflous interrupts (e.g. the
  64. drivers relies on TxDone - TxIntr not needed)
  65. * wait that the hardware has really stopped in close
  66. and suspend.
  67. * workaround for the (at least) gcc-2.95.1 compiler
  68. problem. Also simplifies the code a bit.
  69. * disable_irq() in tx_timeout - needed to protect
  70. against rx interrupts.
  71. * stop the nic before switching into silent rx mode
  72. for wol (required according to docu).
  73. version 1.0.10:
  74. * use long for ee_addr (various)
  75. * print pointers properly (DaveM)
  76. * include asm/irq.h (?)
  77. version 1.0.11:
  78. * check and reset if PHY errors appear (Adrian Sun)
  79. * WoL cleanup (Tim Hockin)
  80. * Magic number cleanup (Tim Hockin)
  81. * Don't reload EEPROM on every reset (Tim Hockin)
  82. * Save and restore EEPROM state across reset (Tim Hockin)
  83. * MDIO Cleanup (Tim Hockin)
  84. * Reformat register offsets/bits (jgarzik)
  85. version 1.0.12:
  86. * ETHTOOL_* further support (Tim Hockin)
  87. version 1.0.13:
  88. * ETHTOOL_[G]EEPROM support (Tim Hockin)
  89. version 1.0.13:
  90. * crc cleanup (Matt Domsch <Matt_Domsch@dell.com>)
  91. version 1.0.14:
  92. * Cleanup some messages and autoneg in ethtool (Tim Hockin)
  93. version 1.0.15:
  94. * Get rid of cable_magic flag
  95. * use new (National provided) solution for cable magic issue
  96. version 1.0.16:
  97. * call netdev_rx() for RxErrors (Manfred Spraul)
  98. * formatting and cleanups
  99. * change options and full_duplex arrays to be zero
  100.   initialized
  101. * enable only the WoL and PHY interrupts in wol mode
  102. version 1.0.17:
  103. * only do cable_magic on 83815 and early 83816 (Tim Hockin)
  104. * create a function for rx refill (Manfred Spraul)
  105. * combine drain_ring and init_ring (Manfred Spraul)
  106. * oom handling (Manfred Spraul)
  107. * hands_off instead of playing with netif_device_{de,a}ttach
  108.   (Manfred Spraul)
  109. * be sure to write the MAC back to the chip (Manfred Spraul)
  110. * lengthen EEPROM timeout, and always warn about timeouts
  111.   (Manfred Spraul)
  112. * comments update (Manfred)
  113. * do the right thing on a phy-reset (Manfred and Tim)
  114. TODO:
  115. * big endian support with CFG:BEM instead of cpu_to_le32
  116. * support for an external PHY
  117. * NAPI
  118. */
  119. #if !defined(__OPTIMIZE__)
  120. #warning  You must compile this file with the correct options!
  121. #warning  See the last lines of the source file.
  122. #error You must compile this driver with "-O".
  123. #endif
  124. #include <linux/config.h>
  125. #include <linux/module.h>
  126. #include <linux/kernel.h>
  127. #include <linux/string.h>
  128. #include <linux/timer.h>
  129. #include <linux/errno.h>
  130. #include <linux/ioport.h>
  131. #include <linux/slab.h>
  132. #include <linux/interrupt.h>
  133. #include <linux/pci.h>
  134. #include <linux/netdevice.h>
  135. #include <linux/etherdevice.h>
  136. #include <linux/skbuff.h>
  137. #include <linux/init.h>
  138. #include <linux/spinlock.h>
  139. #include <linux/ethtool.h>
  140. #include <linux/delay.h>
  141. #include <linux/rtnetlink.h>
  142. #include <linux/mii.h>
  143. #include <asm/processor.h> /* Processor type for cache alignment. */
  144. #include <asm/bitops.h>
  145. #include <asm/io.h>
  146. #include <asm/irq.h>
  147. #include <asm/uaccess.h>
  148. #define DRV_NAME "natsemi"
  149. #define DRV_VERSION "1.07+LK1.0.17"
  150. #define DRV_RELDATE "Sep 27, 2002"
  151. /* Updated to recommendations in pci-skeleton v2.03. */
  152. /* The user-configurable values.
  153.    These may be modified when a driver module is loaded.*/
  154. #define NATSEMI_DEF_MSG (NETIF_MSG_DRV | 
  155.  NETIF_MSG_LINK | 
  156.  NETIF_MSG_WOL | 
  157.  NETIF_MSG_RX_ERR | 
  158.  NETIF_MSG_TX_ERR)
  159. static int debug = -1;
  160. /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
  161. static int max_interrupt_work = 20;
  162. static int mtu;
  163. /* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
  164.    This chip uses a 512 element hash table based on the Ethernet CRC.  */
  165. static int multicast_filter_limit = 100;
  166. /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
  167.    Setting to > 1518 effectively disables this feature. */
  168. static int rx_copybreak;
  169. /* Used to pass the media type, etc.
  170.    Both 'options[]' and 'full_duplex[]' should exist for driver
  171.    interoperability.
  172.    The media type is usually passed in 'options[]'.
  173. */
  174. #define MAX_UNITS 8 /* More are supported, limit only on options */
  175. static int options[MAX_UNITS];
  176. static int full_duplex[MAX_UNITS];
  177. /* Operational parameters that are set at compile time. */
  178. /* Keep the ring sizes a power of two for compile efficiency.
  179.    The compiler will convert <unsigned>'%'<2^N> into a bit mask.
  180.    Making the Tx ring too large decreases the effectiveness of channel
  181.    bonding and packet priority.
  182.    There are no ill effects from too-large receive rings. */
  183. #define TX_RING_SIZE 16
  184. #define TX_QUEUE_LEN 10 /* Limit ring entries actually used, min 4. */
  185. #define RX_RING_SIZE 32
  186. /* Operational parameters that usually are not changed. */
  187. /* Time in jiffies before concluding the transmitter is hung. */
  188. #define TX_TIMEOUT  (2*HZ)
  189. #define NATSEMI_HW_TIMEOUT 400
  190. #define NATSEMI_TIMER_FREQ 3*HZ
  191. #define NATSEMI_PG0_NREGS 64
  192. #define NATSEMI_RFDR_NREGS 8
  193. #define NATSEMI_PG1_NREGS 4
  194. #define NATSEMI_NREGS (NATSEMI_PG0_NREGS + NATSEMI_RFDR_NREGS + 
  195.  NATSEMI_PG1_NREGS)
  196. #define NATSEMI_REGS_VER 1 /* v1 added RFDR registers */
  197. #define NATSEMI_REGS_SIZE (NATSEMI_NREGS * sizeof(u32))
  198. #define NATSEMI_EEPROM_SIZE 24 /* 12 16-bit values */
  199. #define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer. */
  200. /* These identify the driver base version and may not be removed. */
  201. static char version[] __devinitdata =
  202.   KERN_INFO DRV_NAME " dp8381x driver, version "
  203.       DRV_VERSION ", " DRV_RELDATE "n"
  204.   KERN_INFO "  originally by Donald Becker <becker@scyld.com>n"
  205.   KERN_INFO "  http://www.scyld.com/network/natsemi.htmln"
  206.   KERN_INFO "  2.4.x kernel port by Jeff Garzik, Tjeerd Muldern";
  207. MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
  208. MODULE_DESCRIPTION("National Semiconductor DP8381x series PCI Ethernet driver");
  209. MODULE_LICENSE("GPL");
  210. MODULE_PARM(max_interrupt_work, "i");
  211. MODULE_PARM(mtu, "i");
  212. MODULE_PARM(debug, "i");
  213. MODULE_PARM(rx_copybreak, "i");
  214. MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
  215. MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
  216. MODULE_PARM_DESC(max_interrupt_work, 
  217. "DP8381x maximum events handled per interrupt");
  218. MODULE_PARM_DESC(mtu, "DP8381x MTU (all boards)");
  219. MODULE_PARM_DESC(debug, "DP8381x default debug level");
  220. MODULE_PARM_DESC(rx_copybreak, 
  221. "DP8381x copy breakpoint for copy-only-tiny-frames");
  222. MODULE_PARM_DESC(options, 
  223. "DP8381x: Bits 0-3: media type, bit 17: full duplex");
  224. MODULE_PARM_DESC(full_duplex, "DP8381x full duplex setting(s) (1)");
  225. /*
  226. Theory of Operation
  227. I. Board Compatibility
  228. This driver is designed for National Semiconductor DP83815 PCI Ethernet NIC.
  229. It also works with other chips in in the DP83810 series.
  230. II. Board-specific settings
  231. This driver requires the PCI interrupt line to be valid.
  232. It honors the EEPROM-set values.
  233. III. Driver operation
  234. IIIa. Ring buffers
  235. This driver uses two statically allocated fixed-size descriptor lists
  236. formed into rings by a branch from the final descriptor to the beginning of
  237. the list.  The ring sizes are set at compile time by RX/TX_RING_SIZE.
  238. The NatSemi design uses a 'next descriptor' pointer that the driver forms
  239. into a list.
  240. IIIb/c. Transmit/Receive Structure
  241. This driver uses a zero-copy receive and transmit scheme.
  242. The driver allocates full frame size skbuffs for the Rx ring buffers at
  243. open() time and passes the skb->data field to the chip as receive data
  244. buffers.  When an incoming frame is less than RX_COPYBREAK bytes long,
  245. a fresh skbuff is allocated and the frame is copied to the new skbuff.
  246. When the incoming frame is larger, the skbuff is passed directly up the
  247. protocol stack.  Buffers consumed this way are replaced by newly allocated
  248. skbuffs in a later phase of receives.
  249. The RX_COPYBREAK value is chosen to trade-off the memory wasted by
  250. using a full-sized skbuff for small frames vs. the copying costs of larger
  251. frames.  New boards are typically used in generously configured machines
  252. and the underfilled buffers have negligible impact compared to the benefit of
  253. a single allocation size, so the default value of zero results in never
  254. copying packets.  When copying is done, the cost is usually mitigated by using
  255. a combined copy/checksum routine.  Copying also preloads the cache, which is
  256. most useful with small frames.
  257. A subtle aspect of the operation is that unaligned buffers are not permitted
  258. by the hardware.  Thus the IP header at offset 14 in an ethernet frame isn't
  259. longword aligned for further processing.  On copies frames are put into the
  260. skbuff at an offset of "+2", 16-byte aligning the IP header.
  261. IIId. Synchronization
  262. Most operations are synchronized on the np->lock irq spinlock, except the
  263. performance critical codepaths:
  264. The rx process only runs in the interrupt handler. Access from outside
  265. the interrupt handler is only permitted after disable_irq().
  266. The rx process usually runs under the dev->xmit_lock. If np->intr_tx_reap
  267. is set, then access is permitted under spin_lock_irq(&np->lock).
  268. Thus configuration functions that want to access everything must call
  269. disable_irq(dev->irq);
  270. spin_lock_bh(dev->xmit_lock);
  271. spin_lock_irq(&np->lock);
  272. IV. Notes
  273. NatSemi PCI network controllers are very uncommon.
  274. IVb. References
  275. http://www.scyld.com/expert/100mbps.html
  276. http://www.scyld.com/expert/NWay.html
  277. Datasheet is available from:
  278. http://www.national.com/pf/DP/DP83815.html
  279. IVc. Errata
  280. None characterised.
  281. */
  282. enum pcistuff {
  283. PCI_USES_IO = 0x01,
  284. PCI_USES_MEM = 0x02,
  285. PCI_USES_MASTER = 0x04,
  286. PCI_ADDR0 = 0x08,
  287. PCI_ADDR1 = 0x10,
  288. };
  289. /* MMIO operations required */
  290. #define PCI_IOTYPE (PCI_USES_MASTER | PCI_USES_MEM | PCI_ADDR1)
  291. /* array of board data directly indexed by pci_tbl[x].driver_data */
  292. static struct {
  293. const char *name;
  294. unsigned long flags;
  295. } natsemi_pci_info[] __devinitdata = {
  296. { "NatSemi DP8381[56]", PCI_IOTYPE },
  297. };
  298. static struct pci_device_id natsemi_pci_tbl[] __devinitdata = {
  299. { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_83815, PCI_ANY_ID, PCI_ANY_ID, },
  300. { 0, },
  301. };
  302. MODULE_DEVICE_TABLE(pci, natsemi_pci_tbl);
  303. /* Offsets to the device registers.
  304.    Unlike software-only systems, device drivers interact with complex hardware.
  305.    It's not useful to define symbolic names for every register bit in the
  306.    device.
  307. */
  308. enum register_offsets {
  309. ChipCmd = 0x00,
  310. ChipConfig = 0x04,
  311. EECtrl = 0x08,
  312. PCIBusCfg = 0x0C,
  313. IntrStatus = 0x10,
  314. IntrMask = 0x14,
  315. IntrEnable = 0x18,
  316. IntrHoldoff = 0x16, /* DP83816 only */
  317. TxRingPtr = 0x20,
  318. TxConfig = 0x24,
  319. RxRingPtr = 0x30,
  320. RxConfig = 0x34,
  321. ClkRun = 0x3C,
  322. WOLCmd = 0x40,
  323. PauseCmd = 0x44,
  324. RxFilterAddr = 0x48,
  325. RxFilterData = 0x4C,
  326. BootRomAddr = 0x50,
  327. BootRomData = 0x54,
  328. SiliconRev = 0x58,
  329. StatsCtrl = 0x5C,
  330. StatsData = 0x60,
  331. RxPktErrs = 0x60,
  332. RxMissed = 0x68,
  333. RxCRCErrs = 0x64,
  334. BasicControl = 0x80,
  335. BasicStatus = 0x84,
  336. AnegAdv = 0x90,
  337. AnegPeer = 0x94,
  338. PhyStatus = 0xC0,
  339. MIntrCtrl = 0xC4,
  340. MIntrStatus = 0xC8,
  341. PhyCtrl = 0xE4,
  342. /* These are from the spec, around page 78... on a separate table.
  343.  * The meaning of these registers depend on the value of PGSEL. */
  344. PGSEL = 0xCC,
  345. PMDCSR = 0xE4,
  346. TSTDAT = 0xFC,
  347. DSPCFG = 0xF4,
  348. SDCFG = 0xF8
  349. };
  350. /* the values for the 'magic' registers above (PGSEL=1) */
  351. #define PMDCSR_VAL 0x189c /* enable preferred adaptation circuitry */
  352. #define TSTDAT_VAL 0x0
  353. #define DSPCFG_VAL 0x5040
  354. #define SDCFG_VAL 0x008c /* set voltage thresholds for Signal Detect */
  355. #define DSPCFG_LOCK 0x20 /* coefficient lock bit in DSPCFG */
  356. #define TSTDAT_FIXED 0xe8 /* magic number for bad coefficients */
  357. /* misc PCI space registers */
  358. enum pci_register_offsets {
  359. PCIPM = 0x44,
  360. };
  361. enum ChipCmd_bits {
  362. ChipReset = 0x100,
  363. RxReset = 0x20,
  364. TxReset = 0x10,
  365. RxOff = 0x08,
  366. RxOn = 0x04,
  367. TxOff = 0x02,
  368. TxOn = 0x01,
  369. };
  370. enum ChipConfig_bits {
  371. CfgPhyDis = 0x200,
  372. CfgPhyRst = 0x400,
  373. CfgExtPhy = 0x1000,
  374. CfgAnegEnable = 0x2000,
  375. CfgAneg100 = 0x4000,
  376. CfgAnegFull = 0x8000,
  377. CfgAnegDone = 0x8000000,
  378. CfgFullDuplex = 0x20000000,
  379. CfgSpeed100 = 0x40000000,
  380. CfgLink = 0x80000000,
  381. };
  382. enum EECtrl_bits {
  383. EE_ShiftClk = 0x04,
  384. EE_DataIn = 0x01,
  385. EE_ChipSelect = 0x08,
  386. EE_DataOut = 0x02,
  387. };
  388. enum PCIBusCfg_bits {
  389. EepromReload = 0x4,
  390. };
  391. /* Bits in the interrupt status/mask registers. */
  392. enum IntrStatus_bits {
  393. IntrRxDone = 0x0001,
  394. IntrRxIntr = 0x0002,
  395. IntrRxErr = 0x0004,
  396. IntrRxEarly = 0x0008,
  397. IntrRxIdle = 0x0010,
  398. IntrRxOverrun = 0x0020,
  399. IntrTxDone = 0x0040,
  400. IntrTxIntr = 0x0080,
  401. IntrTxErr = 0x0100,
  402. IntrTxIdle = 0x0200,
  403. IntrTxUnderrun = 0x0400,
  404. StatsMax = 0x0800,
  405. SWInt = 0x1000,
  406. WOLPkt = 0x2000,
  407. LinkChange = 0x4000,
  408. IntrHighBits = 0x8000,
  409. RxStatusFIFOOver = 0x10000,
  410. IntrPCIErr = 0xf00000,
  411. RxResetDone = 0x1000000,
  412. TxResetDone = 0x2000000,
  413. IntrAbnormalSummary = 0xCD20,
  414. };
  415. /*
  416.  * Default Interrupts:
  417.  * Rx OK, Rx Packet Error, Rx Overrun,
  418.  * Tx OK, Tx Packet Error, Tx Underrun,
  419.  * MIB Service, Phy Interrupt, High Bits,
  420.  * Rx Status FIFO overrun,
  421.  * Received Target Abort, Received Master Abort,
  422.  * Signalled System Error, Received Parity Error
  423.  */
  424. #define DEFAULT_INTR 0x00f1cd65
  425. enum TxConfig_bits {
  426. TxDrthMask = 0x3f,
  427. TxFlthMask = 0x3f00,
  428. TxMxdmaMask = 0x700000,
  429. TxMxdma_512 = 0x0,
  430. TxMxdma_4 = 0x100000,
  431. TxMxdma_8 = 0x200000,
  432. TxMxdma_16 = 0x300000,
  433. TxMxdma_32 = 0x400000,
  434. TxMxdma_64 = 0x500000,
  435. TxMxdma_128 = 0x600000,
  436. TxMxdma_256 = 0x700000,
  437. TxCollRetry = 0x800000,
  438. TxAutoPad = 0x10000000,
  439. TxMacLoop = 0x20000000,
  440. TxHeartIgn = 0x40000000,
  441. TxCarrierIgn = 0x80000000
  442. };
  443. enum RxConfig_bits {
  444. RxDrthMask = 0x3e,
  445. RxMxdmaMask = 0x700000,
  446. RxMxdma_512 = 0x0,
  447. RxMxdma_4 = 0x100000,
  448. RxMxdma_8 = 0x200000,
  449. RxMxdma_16 = 0x300000,
  450. RxMxdma_32 = 0x400000,
  451. RxMxdma_64 = 0x500000,
  452. RxMxdma_128 = 0x600000,
  453. RxMxdma_256 = 0x700000,
  454. RxAcceptLong = 0x8000000,
  455. RxAcceptTx = 0x10000000,
  456. RxAcceptRunt = 0x40000000,
  457. RxAcceptErr = 0x80000000
  458. };
  459. enum ClkRun_bits {
  460. PMEEnable = 0x100,
  461. PMEStatus = 0x8000,
  462. };
  463. enum WolCmd_bits {
  464. WakePhy = 0x1,
  465. WakeUnicast = 0x2,
  466. WakeMulticast = 0x4,
  467. WakeBroadcast = 0x8,
  468. WakeArp = 0x10,
  469. WakePMatch0 = 0x20,
  470. WakePMatch1 = 0x40,
  471. WakePMatch2 = 0x80,
  472. WakePMatch3 = 0x100,
  473. WakeMagic = 0x200,
  474. WakeMagicSecure = 0x400,
  475. SecureHack = 0x100000,
  476. WokePhy = 0x400000,
  477. WokeUnicast = 0x800000,
  478. WokeMulticast = 0x1000000,
  479. WokeBroadcast = 0x2000000,
  480. WokeArp = 0x4000000,
  481. WokePMatch0 = 0x8000000,
  482. WokePMatch1 = 0x10000000,
  483. WokePMatch2 = 0x20000000,
  484. WokePMatch3 = 0x40000000,
  485. WokeMagic = 0x80000000,
  486. WakeOptsSummary = 0x7ff
  487. };
  488. enum RxFilterAddr_bits {
  489. RFCRAddressMask = 0x3ff,
  490. AcceptMulticast = 0x00200000,
  491. AcceptMyPhys = 0x08000000,
  492. AcceptAllPhys = 0x10000000,
  493. AcceptAllMulticast = 0x20000000,
  494. AcceptBroadcast = 0x40000000,
  495. RxFilterEnable = 0x80000000
  496. };
  497. enum StatsCtrl_bits {
  498. StatsWarn = 0x1,
  499. StatsFreeze = 0x2,
  500. StatsClear = 0x4,
  501. StatsStrobe = 0x8,
  502. };
  503. enum MIntrCtrl_bits {
  504. MICRIntEn = 0x2,
  505. };
  506. enum PhyCtrl_bits {
  507. PhyAddrMask = 0xf,
  508. };
  509. /* values we might find in the silicon revision register */
  510. #define SRR_DP83815_C 0x0302
  511. #define SRR_DP83815_D 0x0403
  512. #define SRR_DP83816_A4 0x0504
  513. #define SRR_DP83816_A5 0x0505
  514. /* The Rx and Tx buffer descriptors. */
  515. /* Note that using only 32 bit fields simplifies conversion to big-endian
  516.    architectures. */
  517. struct netdev_desc {
  518. u32 next_desc;
  519. s32 cmd_status;
  520. u32 addr;
  521. u32 software_use;
  522. };
  523. /* Bits in network_desc.status */
  524. enum desc_status_bits {
  525. DescOwn=0x80000000, DescMore=0x40000000, DescIntr=0x20000000,
  526. DescNoCRC=0x10000000, DescPktOK=0x08000000,
  527. DescSizeMask=0xfff,
  528. DescTxAbort=0x04000000, DescTxFIFO=0x02000000,
  529. DescTxCarrier=0x01000000, DescTxDefer=0x00800000,
  530. DescTxExcDefer=0x00400000, DescTxOOWCol=0x00200000,
  531. DescTxExcColl=0x00100000, DescTxCollCount=0x000f0000,
  532. DescRxAbort=0x04000000, DescRxOver=0x02000000,
  533. DescRxDest=0x01800000, DescRxLong=0x00400000,
  534. DescRxRunt=0x00200000, DescRxInvalid=0x00100000,
  535. DescRxCRC=0x00080000, DescRxAlign=0x00040000,
  536. DescRxLoop=0x00020000, DesRxColl=0x00010000,
  537. };
  538. struct netdev_private {
  539. /* Descriptor rings first for alignment */
  540. dma_addr_t ring_dma;
  541. struct netdev_desc *rx_ring;
  542. struct netdev_desc *tx_ring;
  543. /* The addresses of receive-in-place skbuffs */
  544. struct sk_buff *rx_skbuff[RX_RING_SIZE];
  545. dma_addr_t rx_dma[RX_RING_SIZE];
  546. /* address of a sent-in-place packet/buffer, for later free() */
  547. struct sk_buff *tx_skbuff[TX_RING_SIZE];
  548. dma_addr_t tx_dma[TX_RING_SIZE];
  549. struct net_device_stats stats;
  550. /* Media monitoring timer */
  551. struct timer_list timer;
  552. /* Frequently used values: keep some adjacent for cache effect */
  553. struct pci_dev *pci_dev;
  554. struct netdev_desc *rx_head_desc;
  555. /* Producer/consumer ring indices */
  556. unsigned int cur_rx, dirty_rx;
  557. unsigned int cur_tx, dirty_tx;
  558. /* Based on MTU+slack. */
  559. unsigned int rx_buf_sz;
  560. int oom;
  561. /* Do not touch the nic registers */
  562. int hands_off;
  563. /* These values are keep track of the transceiver/media in use */
  564. unsigned int full_duplex;
  565. /* Rx filter */
  566. u32 cur_rx_mode;
  567. u32 rx_filter[16];
  568. /* FIFO and PCI burst thresholds */
  569. u32 tx_config, rx_config;
  570. /* original contents of ClkRun register */
  571. u32 SavedClkRun;
  572. /* silicon revision */
  573. u32 srr;
  574. /* expected DSPCFG value */
  575. u16 dspcfg;
  576. /* MII transceiver section */
  577. u16 advertising;
  578. unsigned int iosize;
  579. spinlock_t lock;
  580. u32 msg_enable;
  581. };
  582. static int eeprom_read(long ioaddr, int location);
  583. static int mdio_read(struct net_device *dev, int phy_id, int reg);
  584. static void mdio_write(struct net_device *dev, int phy_id, int reg, u16 data);
  585. static void natsemi_reset(struct net_device *dev);
  586. static void natsemi_reload_eeprom(struct net_device *dev);
  587. static void natsemi_stop_rxtx(struct net_device *dev);
  588. static int netdev_open(struct net_device *dev);
  589. static void do_cable_magic(struct net_device *dev);
  590. static void undo_cable_magic(struct net_device *dev);
  591. static void check_link(struct net_device *dev);
  592. static void netdev_timer(unsigned long data);
  593. static void dump_ring(struct net_device *dev);
  594. static void tx_timeout(struct net_device *dev);
  595. static int alloc_ring(struct net_device *dev);
  596. static void refill_rx(struct net_device *dev);
  597. static void init_ring(struct net_device *dev);
  598. static void drain_tx(struct net_device *dev);
  599. static void drain_ring(struct net_device *dev);
  600. static void free_ring(struct net_device *dev);
  601. static void reinit_ring(struct net_device *dev);
  602. static void init_registers(struct net_device *dev);
  603. static int start_tx(struct sk_buff *skb, struct net_device *dev);
  604. static void intr_handler(int irq, void *dev_instance, struct pt_regs *regs);
  605. static void netdev_error(struct net_device *dev, int intr_status);
  606. static void netdev_rx(struct net_device *dev);
  607. static void netdev_tx_done(struct net_device *dev);
  608. static void __set_rx_mode(struct net_device *dev);
  609. static void set_rx_mode(struct net_device *dev);
  610. static void __get_stats(struct net_device *dev);
  611. static struct net_device_stats *get_stats(struct net_device *dev);
  612. static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  613. static int netdev_set_wol(struct net_device *dev, u32 newval);
  614. static int netdev_get_wol(struct net_device *dev, u32 *supported, u32 *cur);
  615. static int netdev_set_sopass(struct net_device *dev, u8 *newval);
  616. static int netdev_get_sopass(struct net_device *dev, u8 *data);
  617. static int netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd);
  618. static int netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd);
  619. static void enable_wol_mode(struct net_device *dev, int enable_intr);
  620. static int netdev_close(struct net_device *dev);
  621. static int netdev_get_regs(struct net_device *dev, u8 *buf);
  622. static int netdev_get_eeprom(struct net_device *dev, u8 *buf);
  623. static int __devinit natsemi_probe1 (struct pci_dev *pdev,
  624. const struct pci_device_id *ent)
  625. {
  626. struct net_device *dev;
  627. struct netdev_private *np;
  628. int i, option, irq, chip_idx = ent->driver_data;
  629. static int find_cnt = -1;
  630. unsigned long ioaddr, iosize;
  631. const int pcibar = 1; /* PCI base address register */
  632. int prev_eedata;
  633. u32 tmp;
  634. /* when built into the kernel, we only print version if device is found */
  635. #ifndef MODULE
  636. static int printed_version;
  637. if (!printed_version++)
  638. printk(version);
  639. #endif
  640. i = pci_enable_device(pdev);
  641. if (i) return i;
  642. /* natsemi has a non-standard PM control register
  643.  * in PCI config space.  Some boards apparently need
  644.  * to be brought to D0 in this manner.
  645.  */
  646. pci_read_config_dword(pdev, PCIPM, &tmp);
  647. if (tmp & PCI_PM_CTRL_STATE_MASK) {
  648. /* D0 state, disable PME assertion */
  649. u32 newtmp = tmp & ~PCI_PM_CTRL_STATE_MASK;
  650. pci_write_config_dword(pdev, PCIPM, newtmp);
  651. }
  652. find_cnt++;
  653. ioaddr = pci_resource_start(pdev, pcibar);
  654. iosize = pci_resource_len(pdev, pcibar);
  655. irq = pdev->irq;
  656. if (natsemi_pci_info[chip_idx].flags & PCI_USES_MASTER)
  657. pci_set_master(pdev);
  658. dev = alloc_etherdev(sizeof (struct netdev_private));
  659. if (!dev)
  660. return -ENOMEM;
  661. SET_MODULE_OWNER(dev);
  662. i = pci_request_regions(pdev, dev->name);
  663. if (i) {
  664. kfree(dev);
  665. return i;
  666. }
  667. {
  668. void *mmio = ioremap (ioaddr, iosize);
  669. if (!mmio) {
  670. pci_release_regions(pdev);
  671. kfree(dev);
  672. return -ENOMEM;
  673. }
  674. ioaddr = (unsigned long) mmio;
  675. }
  676. /* Work around the dropped serial bit. */
  677. prev_eedata = eeprom_read(ioaddr, 6);
  678. for (i = 0; i < 3; i++) {
  679. int eedata = eeprom_read(ioaddr, i + 7);
  680. dev->dev_addr[i*2] = (eedata << 1) + (prev_eedata >> 15);
  681. dev->dev_addr[i*2+1] = eedata >> 7;
  682. prev_eedata = eedata;
  683. }
  684. dev->base_addr = ioaddr;
  685. dev->irq = irq;
  686. np = dev->priv;
  687. np->pci_dev = pdev;
  688. pci_set_drvdata(pdev, dev);
  689. np->iosize = iosize;
  690. spin_lock_init(&np->lock);
  691. np->msg_enable = (debug >= 0) ? (1<<debug)-1 : NATSEMI_DEF_MSG;
  692. np->hands_off = 0;
  693. /* Reset the chip to erase previous misconfiguration. */
  694. natsemi_reload_eeprom(dev);
  695. natsemi_reset(dev);
  696. option = find_cnt < MAX_UNITS ? options[find_cnt] : 0;
  697. if (dev->mem_start)
  698. option = dev->mem_start;
  699. /* The lower four bits are the media type. */
  700. if (option) {
  701. if (option & 0x200)
  702. np->full_duplex = 1;
  703. if (option & 15)
  704. printk(KERN_INFO
  705. "%s: ignoring user supplied media type %d",
  706. dev->name, option & 15);
  707. }
  708. if (find_cnt < MAX_UNITS  &&  full_duplex[find_cnt])
  709. np->full_duplex = 1;
  710. /* The chip-specific entries in the device structure. */
  711. dev->open = &netdev_open;
  712. dev->hard_start_xmit = &start_tx;
  713. dev->stop = &netdev_close;
  714. dev->get_stats = &get_stats;
  715. dev->set_multicast_list = &set_rx_mode;
  716. dev->do_ioctl = &netdev_ioctl;
  717. dev->tx_timeout = &tx_timeout;
  718. dev->watchdog_timeo = TX_TIMEOUT;
  719. if (mtu)
  720. dev->mtu = mtu;
  721. i = register_netdev(dev);
  722. if (i) {
  723. pci_release_regions(pdev);
  724. unregister_netdev(dev);
  725. kfree(dev);
  726. pci_set_drvdata(pdev, NULL);
  727. return i;
  728. }
  729. netif_carrier_off(dev);
  730. if (netif_msg_drv(np)) {
  731. printk(KERN_INFO "%s: %s at %#08lx, ",
  732. dev->name, natsemi_pci_info[chip_idx].name, ioaddr);
  733. for (i = 0; i < ETH_ALEN-1; i++)
  734. printk("%02x:", dev->dev_addr[i]);
  735. printk("%02x, IRQ %d.n", dev->dev_addr[i], irq);
  736. }
  737. np->advertising = mdio_read(dev, 1, MII_ADVERTISE);
  738. if ((readl(ioaddr + ChipConfig) & 0xe000) != 0xe000
  739.  && netif_msg_probe(np)) {
  740. u32 chip_config = readl(ioaddr + ChipConfig);
  741. printk(KERN_INFO "%s: Transceiver default autonegotiation %s "
  742. "10%s %s duplex.n",
  743. dev->name,
  744. chip_config & CfgAnegEnable ?
  745.   "enabled, advertise" : "disabled, force",
  746. chip_config & CfgAneg100 ? "0" : "",
  747. chip_config & CfgAnegFull ? "full" : "half");
  748. }
  749. if (netif_msg_probe(np))
  750. printk(KERN_INFO
  751. "%s: Transceiver status %#04x advertising %#04x.n",
  752. dev->name, mdio_read(dev, 1, MII_BMSR),
  753. np->advertising);
  754. /* save the silicon revision for later querying */
  755. np->srr = readl(ioaddr + SiliconRev);
  756. if (netif_msg_hw(np))
  757. printk(KERN_INFO "%s: silicon revision %#04x.n",
  758. dev->name, np->srr);
  759. return 0;
  760. }
  761. /* Read the EEPROM and MII Management Data I/O (MDIO) interfaces.
  762.    The EEPROM code is for the common 93c06/46 EEPROMs with 6 bit addresses. */
  763. /* Delay between EEPROM clock transitions.
  764.    No extra delay is needed with 33Mhz PCI, but future 66Mhz access may need
  765.    a delay.  Note that pre-2.0.34 kernels had a cache-alignment bug that
  766.    made udelay() unreliable.
  767.    The old method of using an ISA access as a delay, __SLOW_DOWN_IO__, is
  768.    depricated.
  769. */
  770. #define eeprom_delay(ee_addr) readl(ee_addr)
  771. #define EE_Write0 (EE_ChipSelect)
  772. #define EE_Write1 (EE_ChipSelect | EE_DataIn)
  773. /* The EEPROM commands include the alway-set leading bit. */
  774. enum EEPROM_Cmds {
  775. EE_WriteCmd=(5 << 6), EE_ReadCmd=(6 << 6), EE_EraseCmd=(7 << 6),
  776. };
  777. static int eeprom_read(long addr, int location)
  778. {
  779. int i;
  780. int retval = 0;
  781. long ee_addr = addr + EECtrl;
  782. int read_cmd = location | EE_ReadCmd;
  783. writel(EE_Write0, ee_addr);
  784. /* Shift the read command bits out. */
  785. for (i = 10; i >= 0; i--) {
  786. short dataval = (read_cmd & (1 << i)) ? EE_Write1 : EE_Write0;
  787. writel(dataval, ee_addr);
  788. eeprom_delay(ee_addr);
  789. writel(dataval | EE_ShiftClk, ee_addr);
  790. eeprom_delay(ee_addr);
  791. }
  792. writel(EE_ChipSelect, ee_addr);
  793. eeprom_delay(ee_addr);
  794. for (i = 0; i < 16; i++) {
  795. writel(EE_ChipSelect | EE_ShiftClk, ee_addr);
  796. eeprom_delay(ee_addr);
  797. retval |= (readl(ee_addr) & EE_DataOut) ? 1 << i : 0;
  798. writel(EE_ChipSelect, ee_addr);
  799. eeprom_delay(ee_addr);
  800. }
  801. /* Terminate the EEPROM access. */
  802. writel(EE_Write0, ee_addr);
  803. writel(0, ee_addr);
  804. return retval;
  805. }
  806. /* MII transceiver control section.
  807.  * The 83815 series has an internal transceiver, and we present the
  808.  * management registers as if they were MII connected. */
  809. static int mdio_read(struct net_device *dev, int phy_id, int reg)
  810. {
  811. if (phy_id == 1 && reg < 32)
  812. return readl(dev->base_addr+BasicControl+(reg<<2))&0xffff;
  813. else
  814. return 0xffff;
  815. }
  816. static void mdio_write(struct net_device *dev, int phy_id, int reg, u16 data)
  817. {
  818. struct netdev_private *np = dev->priv;
  819. if (phy_id == 1 && reg < 32) {
  820. writew(data, dev->base_addr+BasicControl+(reg<<2));
  821. switch (reg) {
  822. case MII_ADVERTISE: np->advertising = data; break;
  823. }
  824. }
  825. }
  826. /* CFG bits [13:16] [18:23] */
  827. #define CFG_RESET_SAVE 0xfde000
  828. /* WCSR bits [0:4] [9:10] */
  829. #define WCSR_RESET_SAVE 0x61f
  830. /* RFCR bits [20] [22] [27:31] */
  831. #define RFCR_RESET_SAVE 0xf8500000;
  832. static void natsemi_reset(struct net_device *dev)
  833. {
  834. int i;
  835. u32 cfg;
  836. u32 wcsr;
  837. u32 rfcr;
  838. u16 pmatch[3];
  839. u16 sopass[3];
  840. struct netdev_private *np = dev->priv;
  841. /*
  842.  * Resetting the chip causes some registers to be lost.
  843.  * Natsemi suggests NOT reloading the EEPROM while live, so instead
  844.  * we save the state that would have been loaded from EEPROM
  845.  * on a normal power-up (see the spec EEPROM map).  This assumes
  846.  * whoever calls this will follow up with init_registers() eventually.
  847.  */
  848. /* CFG */
  849. cfg = readl(dev->base_addr + ChipConfig) & CFG_RESET_SAVE;
  850. /* WCSR */
  851. wcsr = readl(dev->base_addr + WOLCmd) & WCSR_RESET_SAVE;
  852. /* RFCR */
  853. rfcr = readl(dev->base_addr + RxFilterAddr) & RFCR_RESET_SAVE;
  854. /* PMATCH */
  855. for (i = 0; i < 3; i++) {
  856. writel(i*2, dev->base_addr + RxFilterAddr);
  857. pmatch[i] = readw(dev->base_addr + RxFilterData);
  858. }
  859. /* SOPAS */
  860. for (i = 0; i < 3; i++) {
  861. writel(0xa+(i*2), dev->base_addr + RxFilterAddr);
  862. sopass[i] = readw(dev->base_addr + RxFilterData);
  863. }
  864. /* now whack the chip */
  865. writel(ChipReset, dev->base_addr + ChipCmd);
  866. for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
  867. if (!(readl(dev->base_addr + ChipCmd) & ChipReset))
  868. break;
  869. udelay(5);
  870. }
  871. if (i==NATSEMI_HW_TIMEOUT) {
  872. printk(KERN_WARNING "%s: reset did not complete in %d usec.n",
  873. dev->name, i*5);
  874. } else if (netif_msg_hw(np)) {
  875. printk(KERN_DEBUG "%s: reset completed in %d usec.n",
  876. dev->name, i*5);
  877. }
  878. /* restore CFG */
  879. cfg |= readl(dev->base_addr + ChipConfig) & ~CFG_RESET_SAVE;
  880. writel(cfg, dev->base_addr + ChipConfig);
  881. /* restore WCSR */
  882. wcsr |= readl(dev->base_addr + WOLCmd) & ~WCSR_RESET_SAVE;
  883. writel(wcsr, dev->base_addr + WOLCmd);
  884. /* read RFCR */
  885. rfcr |= readl(dev->base_addr + RxFilterAddr) & ~RFCR_RESET_SAVE;
  886. /* restore PMATCH */
  887. for (i = 0; i < 3; i++) {
  888. writel(i*2, dev->base_addr + RxFilterAddr);
  889. writew(pmatch[i], dev->base_addr + RxFilterData);
  890. }
  891. for (i = 0; i < 3; i++) {
  892. writel(0xa+(i*2), dev->base_addr + RxFilterAddr);
  893. writew(sopass[i], dev->base_addr + RxFilterData);
  894. }
  895. /* restore RFCR */
  896. writel(rfcr, dev->base_addr + RxFilterAddr);
  897. }
  898. static void natsemi_reload_eeprom(struct net_device *dev)
  899. {
  900. struct netdev_private *np = dev->priv;
  901. int i;
  902. writel(EepromReload, dev->base_addr + PCIBusCfg);
  903. for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
  904. udelay(50);
  905. if (!(readl(dev->base_addr + PCIBusCfg) & EepromReload))
  906. break;
  907. }
  908. if (i==NATSEMI_HW_TIMEOUT) {
  909. printk(KERN_WARNING "%s: EEPROM did not reload in %d usec.n",
  910. dev->name, i*50);
  911. } else if (netif_msg_hw(np)) {
  912. printk(KERN_DEBUG "%s: EEPROM reloaded in %d usec.n",
  913. dev->name, i*50);
  914. }
  915. }
  916. static void natsemi_stop_rxtx(struct net_device *dev)
  917. {
  918. long ioaddr = dev->base_addr;
  919. struct netdev_private *np = dev->priv;
  920. int i;
  921. writel(RxOff | TxOff, ioaddr + ChipCmd);
  922. for(i=0;i< NATSEMI_HW_TIMEOUT;i++) {
  923. if ((readl(ioaddr + ChipCmd) & (TxOn|RxOn)) == 0)
  924. break;
  925. udelay(5);
  926. }
  927. if (i==NATSEMI_HW_TIMEOUT) {
  928. printk(KERN_WARNING "%s: Tx/Rx process did not stop in %d usec.n",
  929. dev->name, i*5);
  930. } else if (netif_msg_hw(np)) {
  931. printk(KERN_DEBUG "%s: Tx/Rx process stopped in %d usec.n",
  932. dev->name, i*5);
  933. }
  934. }
  935. static int netdev_open(struct net_device *dev)
  936. {
  937. struct netdev_private *np = dev->priv;
  938. long ioaddr = dev->base_addr;
  939. int i;
  940. /* Reset the chip, just in case. */
  941. natsemi_reset(dev);
  942. i = request_irq(dev->irq, &intr_handler, SA_SHIRQ, dev->name, dev);
  943. if (i) return i;
  944. if (netif_msg_ifup(np))
  945. printk(KERN_DEBUG "%s: netdev_open() irq %d.n",
  946. dev->name, dev->irq);
  947. i = alloc_ring(dev);
  948. if (i < 0) {
  949. free_irq(dev->irq, dev);
  950. return i;
  951. }
  952. init_ring(dev);
  953. spin_lock_irq(&np->lock);
  954. init_registers(dev);
  955. /* now set the MAC address according to dev->dev_addr */
  956. for (i = 0; i < 3; i++) {
  957. u16 mac = (dev->dev_addr[2*i+1]<<8) + dev->dev_addr[2*i];
  958. writel(i*2, ioaddr + RxFilterAddr);
  959. writew(mac, ioaddr + RxFilterData);
  960. }
  961. writel(np->cur_rx_mode, ioaddr + RxFilterAddr);
  962. spin_unlock_irq(&np->lock);
  963. netif_start_queue(dev);
  964. if (netif_msg_ifup(np))
  965. printk(KERN_DEBUG "%s: Done netdev_open(), status: %#08x.n",
  966. dev->name, (int)readl(ioaddr + ChipCmd));
  967. /* Set the timer to check for link beat. */
  968. init_timer(&np->timer);
  969. np->timer.expires = jiffies + NATSEMI_TIMER_FREQ;
  970. np->timer.data = (unsigned long)dev;
  971. np->timer.function = &netdev_timer; /* timer handler */
  972. add_timer(&np->timer);
  973. return 0;
  974. }
  975. static void do_cable_magic(struct net_device *dev)
  976. {
  977. struct netdev_private *np = dev->priv;
  978. if (np->srr >= SRR_DP83816_A5)
  979. return;
  980. /*
  981.  * 100 MBit links with short cables can trip an issue with the chip.
  982.  * The problem manifests as lots of CRC errors and/or flickering
  983.  * activity LED while idle.  This process is based on instructions
  984.  * from engineers at National.
  985.  */
  986. if (readl(dev->base_addr + ChipConfig) & CfgSpeed100) {
  987. u16 data;
  988. writew(1, dev->base_addr + PGSEL);
  989. /*
  990.  * coefficient visibility should already be enabled via
  991.  * DSPCFG | 0x1000
  992.  */
  993. data = readw(dev->base_addr + TSTDAT) & 0xff;
  994. /*
  995.  * the value must be negative, and within certain values
  996.  * (these values all come from National)
  997.  */
  998. if (!(data & 0x80) || ((data >= 0xd8) && (data <= 0xff))) {
  999. struct netdev_private *np = dev->priv;
  1000. /* the bug has been triggered - fix the coefficient */
  1001. writew(TSTDAT_FIXED, dev->base_addr + TSTDAT);
  1002. /* lock the value */
  1003. data = readw(dev->base_addr + DSPCFG);
  1004. np->dspcfg = data | DSPCFG_LOCK;
  1005. writew(np->dspcfg, dev->base_addr + DSPCFG);
  1006. }
  1007. writew(0, dev->base_addr + PGSEL);
  1008. }
  1009. }
  1010. static void undo_cable_magic(struct net_device *dev)
  1011. {
  1012. u16 data;
  1013. struct netdev_private *np = dev->priv;
  1014. if (np->srr >= SRR_DP83816_A5)
  1015. return;
  1016. writew(1, dev->base_addr + PGSEL);
  1017. /* make sure the lock bit is clear */
  1018. data = readw(dev->base_addr + DSPCFG);
  1019. np->dspcfg = data & ~DSPCFG_LOCK;
  1020. writew(np->dspcfg, dev->base_addr + DSPCFG);
  1021. writew(0, dev->base_addr + PGSEL);
  1022. }
  1023. static void check_link(struct net_device *dev)
  1024. {
  1025. struct netdev_private *np = dev->priv;
  1026. long ioaddr = dev->base_addr;
  1027. int duplex;
  1028. int chipcfg = readl(ioaddr + ChipConfig);
  1029. if (!(chipcfg & CfgLink)) {
  1030. if (netif_carrier_ok(dev)) {
  1031. if (netif_msg_link(np))
  1032. printk(KERN_NOTICE "%s: link down.n",
  1033. dev->name);
  1034. netif_carrier_off(dev);
  1035. undo_cable_magic(dev);
  1036. }
  1037. return;
  1038. }
  1039. if (!netif_carrier_ok(dev)) {
  1040. if (netif_msg_link(np))
  1041. printk(KERN_NOTICE "%s: link up.n", dev->name);
  1042. netif_carrier_on(dev);
  1043. do_cable_magic(dev);
  1044. }
  1045. duplex = np->full_duplex || (chipcfg & CfgFullDuplex ? 1 : 0);
  1046. /* if duplex is set then bit 28 must be set, too */
  1047. if (duplex ^ !!(np->rx_config & RxAcceptTx)) {
  1048. if (netif_msg_link(np))
  1049. printk(KERN_INFO
  1050. "%s: Setting %s-duplex based on negotiated "
  1051. "link capability.n", dev->name,
  1052. duplex ? "full" : "half");
  1053. if (duplex) {
  1054. np->rx_config |= RxAcceptTx;
  1055. np->tx_config |= TxCarrierIgn | TxHeartIgn;
  1056. } else {
  1057. np->rx_config &= ~RxAcceptTx;
  1058. np->tx_config &= ~(TxCarrierIgn | TxHeartIgn);
  1059. }
  1060. writel(np->tx_config, ioaddr + TxConfig);
  1061. writel(np->rx_config, ioaddr + RxConfig);
  1062. }
  1063. }
  1064. static void init_registers(struct net_device *dev)
  1065. {
  1066. struct netdev_private *np = dev->priv;
  1067. long ioaddr = dev->base_addr;
  1068. int i;
  1069. for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
  1070. if (readl(dev->base_addr + ChipConfig) & CfgAnegDone)
  1071. break;
  1072. udelay(10);
  1073. }
  1074. if (i==NATSEMI_HW_TIMEOUT && netif_msg_link(np)) {
  1075. printk(KERN_INFO
  1076. "%s: autonegotiation did not complete in %d usec.n",
  1077. dev->name, i*10);
  1078. }
  1079. /* On page 78 of the spec, they recommend some settings for "optimum
  1080.    performance" to be done in sequence.  These settings optimize some
  1081.    of the 100Mbit autodetection circuitry.  They say we only want to
  1082.    do this for rev C of the chip, but engineers at NSC (Bradley
  1083.    Kennedy) recommends always setting them.  If you don't, you get
  1084.    errors on some autonegotiations that make the device unusable.
  1085. */
  1086. writew(1, ioaddr + PGSEL);
  1087. writew(PMDCSR_VAL, ioaddr + PMDCSR);
  1088. writew(TSTDAT_VAL, ioaddr + TSTDAT);
  1089. writew(DSPCFG_VAL, ioaddr + DSPCFG);
  1090. writew(SDCFG_VAL, ioaddr + SDCFG);
  1091. writew(0, ioaddr + PGSEL);
  1092. np->dspcfg = DSPCFG_VAL;
  1093. /* Enable PHY Specific event based interrupts.  Link state change
  1094.    and Auto-Negotiation Completion are among the affected.
  1095.    Read the intr status to clear it (needed for wake events).
  1096. */
  1097. readw(ioaddr + MIntrStatus);
  1098. writew(MICRIntEn, ioaddr + MIntrCtrl);
  1099. /* clear any interrupts that are pending, such as wake events */
  1100. readl(ioaddr + IntrStatus);
  1101. writel(np->ring_dma, ioaddr + RxRingPtr);
  1102. writel(np->ring_dma + RX_RING_SIZE * sizeof(struct netdev_desc),
  1103. ioaddr + TxRingPtr);
  1104. /* Initialize other registers.
  1105.  * Configure the PCI bus bursts and FIFO thresholds.
  1106.  * Configure for standard, in-spec Ethernet.
  1107.  * Start with half-duplex. check_link will update
  1108.  * to the correct settings.
  1109.  */
  1110. /* DRTH: 2: start tx if 64 bytes are in the fifo
  1111.  * FLTH: 0x10: refill with next packet if 512 bytes are free
  1112.  * MXDMA: 0: up to 256 byte bursts.
  1113.  *  MXDMA must be <= FLTH
  1114.  * ECRETRY=1
  1115.  * ATP=1
  1116.  */
  1117. np->tx_config = TxAutoPad | TxCollRetry | TxMxdma_256 | (0x1002);
  1118. writel(np->tx_config, ioaddr + TxConfig);
  1119. /* DRTH 0x10: start copying to memory if 128 bytes are in the fifo
  1120.  * MXDMA 0: up to 256 byte bursts
  1121.  */
  1122. np->rx_config = RxMxdma_256 | 0x20;
  1123. writel(np->rx_config, ioaddr + RxConfig);
  1124. /* Disable PME:
  1125.  * The PME bit is initialized from the EEPROM contents.
  1126.  * PCI cards probably have PME disabled, but motherboard
  1127.  * implementations may have PME set to enable WakeOnLan.
  1128.  * With PME set the chip will scan incoming packets but
  1129.  * nothing will be written to memory. */
  1130. np->SavedClkRun = readl(ioaddr + ClkRun);
  1131. writel(np->SavedClkRun & ~PMEEnable, ioaddr + ClkRun);
  1132. if (np->SavedClkRun & PMEStatus && netif_msg_wol(np)) {
  1133. printk(KERN_NOTICE "%s: Wake-up event %#08xn",
  1134. dev->name, readl(ioaddr + WOLCmd));
  1135. }
  1136. check_link(dev);
  1137. __set_rx_mode(dev);
  1138. /* Enable interrupts by setting the interrupt mask. */
  1139. writel(DEFAULT_INTR, ioaddr + IntrMask);
  1140. writel(1, ioaddr + IntrEnable);
  1141. writel(RxOn | TxOn, ioaddr + ChipCmd);
  1142. writel(StatsClear, ioaddr + StatsCtrl); /* Clear Stats */
  1143. }
  1144. /*
  1145.  * netdev_timer:
  1146.  * Purpose:
  1147.  * 1) check for link changes. Usually they are handled by the MII interrupt
  1148.  *    but it doesn't hurt to check twice.
  1149.  * 2) check for sudden death of the NIC:
  1150.  *    It seems that a reference set for this chip went out with incorrect info,
  1151.  *    and there exist boards that aren't quite right.  An unexpected voltage
  1152.  *    drop can cause the PHY to get itself in a weird state (basically reset).
  1153.  *    NOTE: this only seems to affect revC chips.
  1154.  * 3) check of death of the RX path due to OOM
  1155.  */
  1156. static void netdev_timer(unsigned long data)
  1157. {
  1158. struct net_device *dev = (struct net_device *)data;
  1159. struct netdev_private *np = dev->priv;
  1160. int next_tick = 5*HZ;
  1161. long ioaddr = dev->base_addr;
  1162. u16 dspcfg;
  1163. if (netif_msg_timer(np)) {
  1164. /* DO NOT read the IntrStatus register,
  1165.  * a read clears any pending interrupts.
  1166.  */
  1167. printk(KERN_DEBUG "%s: Media selection timer tick.n",
  1168. dev->name);
  1169. }
  1170. spin_lock_irq(&np->lock);
  1171. /* check for a nasty random phy-reset - use dspcfg as a flag */
  1172. writew(1, ioaddr+PGSEL);
  1173. dspcfg = readw(ioaddr+DSPCFG);
  1174. writew(0, ioaddr+PGSEL);
  1175. if (dspcfg != np->dspcfg) {
  1176. if (!netif_queue_stopped(dev)) {
  1177. spin_unlock_irq(&np->lock);
  1178. if (netif_msg_hw(np))
  1179. printk(KERN_NOTICE "%s: possible phy reset: "
  1180. "re-initializingn", dev->name);
  1181. disable_irq(dev->irq);
  1182. spin_lock_irq(&np->lock);
  1183. natsemi_stop_rxtx(dev);
  1184. dump_ring(dev);
  1185. reinit_ring(dev);
  1186. init_registers(dev);
  1187. spin_unlock_irq(&np->lock);
  1188. enable_irq(dev->irq);
  1189. } else {
  1190. /* hurry back */
  1191. next_tick = HZ;
  1192. spin_unlock_irq(&np->lock);
  1193. }
  1194. } else {
  1195. /* init_registers() calls check_link() for the above case */
  1196. check_link(dev);
  1197. spin_unlock_irq(&np->lock);
  1198. }
  1199. if (np->oom) {
  1200. disable_irq(dev->irq);
  1201. np->oom = 0;
  1202. refill_rx(dev);
  1203. enable_irq(dev->irq);
  1204. if (!np->oom) {
  1205. writel(RxOn, dev->base_addr + ChipCmd);
  1206. } else {
  1207. next_tick = 1;
  1208. }
  1209. }
  1210. mod_timer(&np->timer, jiffies + next_tick);
  1211. }
  1212. static void dump_ring(struct net_device *dev)
  1213. {
  1214. struct netdev_private *np = dev->priv;
  1215. if (netif_msg_pktdata(np)) {
  1216. int i;
  1217. printk(KERN_DEBUG "  Tx ring at %p:n", np->tx_ring);
  1218. for (i = 0; i < TX_RING_SIZE; i++) {
  1219. printk(KERN_DEBUG " #%d desc. %#08x %#08x %#08x.n",
  1220. i, np->tx_ring[i].next_desc,
  1221. np->tx_ring[i].cmd_status,
  1222. np->tx_ring[i].addr);
  1223. }
  1224. printk(KERN_DEBUG "  Rx ring %p:n", np->rx_ring);
  1225. for (i = 0; i < RX_RING_SIZE; i++) {
  1226. printk(KERN_DEBUG " #%d desc. %#08x %#08x %#08x.n",
  1227. i, np->rx_ring[i].next_desc,
  1228. np->rx_ring[i].cmd_status,
  1229. np->rx_ring[i].addr);
  1230. }
  1231. }
  1232. }
  1233. static void tx_timeout(struct net_device *dev)
  1234. {
  1235. struct netdev_private *np = dev->priv;
  1236. long ioaddr = dev->base_addr;
  1237. disable_irq(dev->irq);
  1238. spin_lock_irq(&np->lock);
  1239. if (!np->hands_off) {
  1240. if (netif_msg_tx_err(np))
  1241. printk(KERN_WARNING
  1242. "%s: Transmit timed out, status %#08x,"
  1243. " resetting...n",
  1244. dev->name, readl(ioaddr + IntrStatus));
  1245. dump_ring(dev);
  1246. natsemi_reset(dev);
  1247. reinit_ring(dev);
  1248. init_registers(dev);
  1249. } else {
  1250. printk(KERN_WARNING
  1251. "%s: tx_timeout while in hands_off state?n",
  1252. dev->name);
  1253. }
  1254. spin_unlock_irq(&np->lock);
  1255. enable_irq(dev->irq);
  1256. dev->trans_start = jiffies;
  1257. np->stats.tx_errors++;
  1258. netif_wake_queue(dev);
  1259. }
  1260. static int alloc_ring(struct net_device *dev)
  1261. {
  1262. struct netdev_private *np = dev->priv;
  1263. np->rx_ring = pci_alloc_consistent(np->pci_dev,
  1264. sizeof(struct netdev_desc) * (RX_RING_SIZE+TX_RING_SIZE),
  1265. &np->ring_dma);
  1266. if (!np->rx_ring)
  1267. return -ENOMEM;
  1268. np->tx_ring = &np->rx_ring[RX_RING_SIZE];
  1269. return 0;
  1270. }
  1271. static void refill_rx(struct net_device *dev)
  1272. {
  1273. struct netdev_private *np = dev->priv;
  1274. /* Refill the Rx ring buffers. */
  1275. for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {
  1276. struct sk_buff *skb;
  1277. int entry = np->dirty_rx % RX_RING_SIZE;
  1278. if (np->rx_skbuff[entry] == NULL) {
  1279. skb = dev_alloc_skb(np->rx_buf_sz);
  1280. np->rx_skbuff[entry] = skb;
  1281. if (skb == NULL)
  1282. break; /* Better luck next round. */
  1283. skb->dev = dev; /* Mark as being used by this device. */
  1284. np->rx_dma[entry] = pci_map_single(np->pci_dev,
  1285. skb->data, skb->len, PCI_DMA_FROMDEVICE);
  1286. np->rx_ring[entry].addr = cpu_to_le32(np->rx_dma[entry]);
  1287. }
  1288. np->rx_ring[entry].cmd_status = cpu_to_le32(np->rx_buf_sz);
  1289. }
  1290. if (np->cur_rx - np->dirty_rx == RX_RING_SIZE) {
  1291. if (netif_msg_rx_err(np))
  1292. printk(KERN_WARNING "%s: going OOM.n", dev->name);
  1293. np->oom = 1;
  1294. }
  1295. }
  1296. /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
  1297. static void init_ring(struct net_device *dev)
  1298. {
  1299. struct netdev_private *np = dev->priv;
  1300. int i;
  1301. /* 1) TX ring */
  1302. np->dirty_tx = np->cur_tx = 0;
  1303. for (i = 0; i < TX_RING_SIZE; i++) {
  1304. np->tx_skbuff[i] = NULL;
  1305. np->tx_ring[i].next_desc = cpu_to_le32(np->ring_dma
  1306. +sizeof(struct netdev_desc)
  1307. *((i+1)%TX_RING_SIZE+RX_RING_SIZE));
  1308. np->tx_ring[i].cmd_status = 0;
  1309. }
  1310. /* 2) RX ring */
  1311. np->dirty_rx = 0;
  1312. np->cur_rx = RX_RING_SIZE;
  1313. np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
  1314. np->oom = 0;
  1315. np->rx_head_desc = &np->rx_ring[0];
  1316. /* Please be carefull before changing this loop - at least gcc-2.95.1
  1317.  * miscompiles it otherwise.
  1318.  */
  1319. /* Initialize all Rx descriptors. */
  1320. for (i = 0; i < RX_RING_SIZE; i++) {
  1321. np->rx_ring[i].next_desc = cpu_to_le32(np->ring_dma
  1322. +sizeof(struct netdev_desc)
  1323. *((i+1)%RX_RING_SIZE));
  1324. np->rx_ring[i].cmd_status = cpu_to_le32(DescOwn);
  1325. np->rx_skbuff[i] = NULL;
  1326. }
  1327. refill_rx(dev);
  1328. dump_ring(dev);
  1329. }
  1330. static void drain_tx(struct net_device *dev)
  1331. {
  1332. struct netdev_private *np = dev->priv;
  1333. int i;
  1334. for (i = 0; i < TX_RING_SIZE; i++) {
  1335. if (np->tx_skbuff[i]) {
  1336. pci_unmap_single(np->pci_dev,
  1337. np->rx_dma[i], np->rx_skbuff[i]->len,
  1338. PCI_DMA_TODEVICE);
  1339. dev_kfree_skb(np->tx_skbuff[i]);
  1340. np->stats.tx_dropped++;
  1341. }
  1342. np->tx_skbuff[i] = NULL;
  1343. }
  1344. }
  1345. static void drain_ring(struct net_device *dev)
  1346. {
  1347. struct netdev_private *np = dev->priv;
  1348. int i;
  1349. /* Free all the skbuffs in the Rx queue. */
  1350. for (i = 0; i < RX_RING_SIZE; i++) {
  1351. np->rx_ring[i].cmd_status = 0;
  1352. np->rx_ring[i].addr = 0xBADF00D0; /* An invalid address. */
  1353. if (np->rx_skbuff[i]) {
  1354. pci_unmap_single(np->pci_dev,
  1355. np->rx_dma[i], np->rx_skbuff[i]->len,
  1356. PCI_DMA_FROMDEVICE);
  1357. dev_kfree_skb(np->rx_skbuff[i]);
  1358. }
  1359. np->rx_skbuff[i] = NULL;
  1360. }
  1361. drain_tx(dev);
  1362. }
  1363. static void free_ring(struct net_device *dev)
  1364. {
  1365. struct netdev_private *np = dev->priv;
  1366. pci_free_consistent(np->pci_dev,
  1367. sizeof(struct netdev_desc) * (RX_RING_SIZE+TX_RING_SIZE),
  1368. np->rx_ring, np->ring_dma);
  1369. }
  1370. static void reinit_ring(struct net_device *dev)
  1371. {
  1372. struct netdev_private *np = dev->priv;
  1373. int i;
  1374. /* drain TX ring */
  1375. drain_tx(dev);
  1376. np->dirty_tx = np->cur_tx = 0;
  1377. for (i=0;i<TX_RING_SIZE;i++)
  1378. np->tx_ring[i].cmd_status = 0;
  1379. /* RX Ring */
  1380. np->dirty_rx = 0;
  1381. np->cur_rx = RX_RING_SIZE;
  1382. np->rx_head_desc = &np->rx_ring[0];
  1383. /* Initialize all Rx descriptors. */
  1384. for (i = 0; i < RX_RING_SIZE; i++)
  1385. np->rx_ring[i].cmd_status = cpu_to_le32(DescOwn);
  1386. refill_rx(dev);
  1387. }
  1388. static int start_tx(struct sk_buff *skb, struct net_device *dev)
  1389. {
  1390. struct netdev_private *np = dev->priv;
  1391. unsigned entry;
  1392. /* Note: Ordering is important here, set the field with the
  1393.    "ownership" bit last, and only then increment cur_tx. */
  1394. /* Calculate the next Tx descriptor entry. */
  1395. entry = np->cur_tx % TX_RING_SIZE;
  1396. np->tx_skbuff[entry] = skb;
  1397. np->tx_dma[entry] = pci_map_single(np->pci_dev,
  1398. skb->data,skb->len, PCI_DMA_TODEVICE);
  1399. np->tx_ring[entry].addr = cpu_to_le32(np->tx_dma[entry]);
  1400. spin_lock_irq(&np->lock);
  1401. if (!np->hands_off) {
  1402. np->tx_ring[entry].cmd_status = cpu_to_le32(DescOwn | skb->len);
  1403. /* StrongARM: Explicitly cache flush np->tx_ring and
  1404.  * skb->data,skb->len. */
  1405. wmb();
  1406. np->cur_tx++;
  1407. if (np->cur_tx - np->dirty_tx >= TX_QUEUE_LEN - 1) {
  1408. netdev_tx_done(dev);
  1409. if (np->cur_tx - np->dirty_tx >= TX_QUEUE_LEN - 1)
  1410. netif_stop_queue(dev);
  1411. }
  1412. /* Wake the potentially-idle transmit channel. */
  1413. writel(TxOn, dev->base_addr + ChipCmd);
  1414. } else {
  1415. dev_kfree_skb_irq(skb);
  1416. np->stats.tx_dropped++;
  1417. }
  1418. spin_unlock_irq(&np->lock);
  1419. dev->trans_start = jiffies;
  1420. if (netif_msg_tx_queued(np)) {
  1421. printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.n",
  1422. dev->name, np->cur_tx, entry);
  1423. }
  1424. return 0;
  1425. }
  1426. static void netdev_tx_done(struct net_device *dev)
  1427. {
  1428. struct netdev_private *np = dev->priv;
  1429. for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {
  1430. int entry = np->dirty_tx % TX_RING_SIZE;
  1431. if (np->tx_ring[entry].cmd_status & cpu_to_le32(DescOwn))
  1432. break;
  1433. if (netif_msg_tx_done(np))
  1434. printk(KERN_DEBUG
  1435. "%s: tx frame #%d finished, status %#08x.n",
  1436. dev->name, np->dirty_tx,
  1437. le32_to_cpu(np->tx_ring[entry].cmd_status));
  1438. if (np->tx_ring[entry].cmd_status & cpu_to_le32(DescPktOK)) {
  1439. np->stats.tx_packets++;
  1440. np->stats.tx_bytes += np->tx_skbuff[entry]->len;
  1441. } else { /* Various Tx errors */
  1442. int tx_status =
  1443. le32_to_cpu(np->tx_ring[entry].cmd_status);
  1444. if (tx_status & (DescTxAbort|DescTxExcColl))
  1445. np->stats.tx_aborted_errors++;
  1446. if (tx_status & DescTxFIFO)
  1447. np->stats.tx_fifo_errors++;
  1448. if (tx_status & DescTxCarrier)
  1449. np->stats.tx_carrier_errors++;
  1450. if (tx_status & DescTxOOWCol)
  1451. np->stats.tx_window_errors++;
  1452. np->stats.tx_errors++;
  1453. }
  1454. pci_unmap_single(np->pci_dev,np->tx_dma[entry],
  1455. np->tx_skbuff[entry]->len,
  1456. PCI_DMA_TODEVICE);
  1457. /* Free the original skb. */
  1458. dev_kfree_skb_irq(np->tx_skbuff[entry]);
  1459. np->tx_skbuff[entry] = NULL;
  1460. }
  1461. if (netif_queue_stopped(dev)
  1462. && np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4) {
  1463. /* The ring is no longer full, wake queue. */
  1464. netif_wake_queue(dev);
  1465. }
  1466. }
  1467. /* The interrupt handler does all of the Rx thread work and cleans up
  1468.    after the Tx thread. */
  1469. static void intr_handler(int irq, void *dev_instance, struct pt_regs *rgs)
  1470. {
  1471. struct net_device *dev = dev_instance;
  1472. struct netdev_private *np = dev->priv;
  1473. long ioaddr = dev->base_addr;
  1474. int boguscnt = max_interrupt_work;
  1475. if (np->hands_off)
  1476. return;
  1477. do {
  1478. /* Reading automatically acknowledges all int sources. */
  1479. u32 intr_status = readl(ioaddr + IntrStatus);
  1480. if (netif_msg_intr(np))
  1481. printk(KERN_DEBUG
  1482. "%s: Interrupt, status %#08x, mask %#08x.n",
  1483. dev->name, intr_status,
  1484. readl(ioaddr + IntrMask));
  1485. if (intr_status == 0)
  1486. break;
  1487. if (intr_status &
  1488.    (IntrRxDone | IntrRxIntr | RxStatusFIFOOver |
  1489.     IntrRxErr | IntrRxOverrun)) {
  1490. netdev_rx(dev);
  1491. }
  1492. if (intr_status &
  1493.    (IntrTxDone | IntrTxIntr | IntrTxIdle | IntrTxErr)) {
  1494. spin_lock(&np->lock);
  1495. netdev_tx_done(dev);
  1496. spin_unlock(&np->lock);
  1497. }
  1498. /* Abnormal error summary/uncommon events handlers. */
  1499. if (intr_status & IntrAbnormalSummary)
  1500. netdev_error(dev, intr_status);
  1501. if (--boguscnt < 0) {
  1502. if (netif_msg_intr(np))
  1503. printk(KERN_WARNING
  1504. "%s: Too much work at interrupt, "
  1505. "status=%#08x.n",
  1506. dev->name, intr_status);
  1507. break;
  1508. }
  1509. } while (1);
  1510. if (netif_msg_intr(np))
  1511. printk(KERN_DEBUG "%s: exiting interrupt.n", dev->name);
  1512. }
  1513. /* This routine is logically part of the interrupt handler, but separated
  1514.    for clarity and better register allocation. */
  1515. static void netdev_rx(struct net_device *dev)
  1516. {
  1517. struct netdev_private *np = dev->priv;
  1518. int entry = np->cur_rx % RX_RING_SIZE;
  1519. int boguscnt = np->dirty_rx + RX_RING_SIZE - np->cur_rx;
  1520. s32 desc_status = le32_to_cpu(np->rx_head_desc->cmd_status);
  1521. /* If the driver owns the next entry it's a new packet. Send it up. */
  1522. while (desc_status < 0) { /* e.g. & DescOwn */
  1523. if (netif_msg_rx_status(np))
  1524. printk(KERN_DEBUG
  1525. "  netdev_rx() entry %d status was %#08x.n",
  1526. entry, desc_status);
  1527. if (--boguscnt < 0)
  1528. break;
  1529. if ((desc_status&(DescMore|DescPktOK|DescRxLong)) != DescPktOK){
  1530. if (desc_status & DescMore) {
  1531. if (netif_msg_rx_err(np))
  1532. printk(KERN_WARNING
  1533. "%s: Oversized(?) Ethernet "
  1534. "frame spanned multiple "
  1535. "buffers, entry %#08x "
  1536. "status %#08x.n", dev->name,
  1537. np->cur_rx, desc_status);
  1538. np->stats.rx_length_errors++;
  1539. } else {
  1540. /* There was an error. */
  1541. np->stats.rx_errors++;
  1542. if (desc_status & (DescRxAbort|DescRxOver))
  1543. np->stats.rx_over_errors++;
  1544. if (desc_status & (DescRxLong|DescRxRunt))
  1545. np->stats.rx_length_errors++;
  1546. if (desc_status & (DescRxInvalid|DescRxAlign))
  1547. np->stats.rx_frame_errors++;
  1548. if (desc_status & DescRxCRC)
  1549. np->stats.rx_crc_errors++;
  1550. }
  1551. } else {
  1552. struct sk_buff *skb;
  1553. /* Omit CRC size. */
  1554. int pkt_len = (desc_status & DescSizeMask) - 4;
  1555. /* Check if the packet is long enough to accept
  1556.  * without copying to a minimally-sized skbuff. */
  1557. if (pkt_len < rx_copybreak
  1558.     && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
  1559. skb->dev = dev;
  1560. /* 16 byte align the IP header */
  1561. skb_reserve(skb, 2);
  1562. pci_dma_sync_single(np->pci_dev,
  1563. np->rx_dma[entry],
  1564. np->rx_skbuff[entry]->len,
  1565. PCI_DMA_FROMDEVICE);
  1566. #if HAS_IP_COPYSUM
  1567. eth_copy_and_sum(skb,
  1568. np->rx_skbuff[entry]->tail, pkt_len, 0);
  1569. skb_put(skb, pkt_len);
  1570. #else
  1571. memcpy(skb_put(skb, pkt_len),
  1572. np->rx_skbuff[entry]->tail, pkt_len);
  1573. #endif
  1574. } else {
  1575. pci_unmap_single(np->pci_dev, np->rx_dma[entry],
  1576. np->rx_skbuff[entry]->len,
  1577. PCI_DMA_FROMDEVICE);
  1578. skb_put(skb = np->rx_skbuff[entry], pkt_len);
  1579. np->rx_skbuff[entry] = NULL;
  1580. }
  1581. skb->protocol = eth_type_trans(skb, dev);
  1582. netif_rx(skb);
  1583. dev->last_rx = jiffies;
  1584. np->stats.rx_packets++;
  1585. np->stats.rx_bytes += pkt_len;
  1586. }
  1587. entry = (++np->cur_rx) % RX_RING_SIZE;
  1588. np->rx_head_desc = &np->rx_ring[entry];
  1589. desc_status = le32_to_cpu(np->rx_head_desc->cmd_status);
  1590. }
  1591. refill_rx(dev);
  1592. /* Restart Rx engine if stopped. */
  1593. if (np->oom)
  1594. mod_timer(&np->timer, jiffies + 1);
  1595. else
  1596. writel(RxOn, dev->base_addr + ChipCmd);
  1597. }
  1598. static void netdev_error(struct net_device *dev, int intr_status)
  1599. {
  1600. struct netdev_private *np = dev->priv;
  1601. long ioaddr = dev->base_addr;
  1602. spin_lock(&np->lock);
  1603. if (intr_status & LinkChange) {
  1604. u16 adv = mdio_read(dev, 1, MII_ADVERTISE);
  1605. u16 lpa = mdio_read(dev, 1, MII_LPA);
  1606. if (mdio_read(dev, 1, MII_BMCR) & BMCR_ANENABLE
  1607.  && netif_msg_link(np)) {
  1608. printk(KERN_INFO
  1609. "%s: Autonegotiation advertising"
  1610. " %#04x  partner %#04x.n", dev->name,
  1611. adv, lpa);
  1612. }
  1613. /* read MII int status to clear the flag */
  1614. readw(ioaddr + MIntrStatus);
  1615. check_link(dev);
  1616. }
  1617. if (intr_status & StatsMax) {
  1618. __get_stats(dev);
  1619. }
  1620. if (intr_status & IntrTxUnderrun) {
  1621. if ((np->tx_config & TxDrthMask) < 62)
  1622. np->tx_config += 2;
  1623. if (netif_msg_tx_err(np))
  1624. printk(KERN_NOTICE
  1625. "%s: increased Tx threshold, txcfg %#08x.n",
  1626. dev->name, np->tx_config);
  1627. writel(np->tx_config, ioaddr + TxConfig);
  1628. }
  1629. if (intr_status & WOLPkt && netif_msg_wol(np)) {
  1630. int wol_status = readl(ioaddr + WOLCmd);
  1631. printk(KERN_NOTICE "%s: Link wake-up event %#08xn",
  1632. dev->name, wol_status);
  1633. }
  1634. if (intr_status & RxStatusFIFOOver) {
  1635. if (netif_msg_rx_err(np) && netif_msg_intr(np)) {
  1636. printk(KERN_NOTICE "%s: Rx status FIFO overrunn",
  1637. dev->name);
  1638. }
  1639. np->stats.rx_fifo_errors++;
  1640. }
  1641. /* Hmmmmm, it's not clear how to recover from PCI faults. */
  1642. if (intr_status & IntrPCIErr) {
  1643. printk(KERN_NOTICE "%s: PCI error %#08xn", dev->name,
  1644. intr_status & IntrPCIErr);
  1645. np->stats.tx_fifo_errors++;
  1646. np->stats.rx_fifo_errors++;
  1647. }
  1648. spin_unlock(&np->lock);
  1649. }
  1650. static void __get_stats(struct net_device *dev)
  1651. {
  1652. long ioaddr = dev->base_addr;
  1653. struct netdev_private *np = dev->priv;
  1654. /* The chip only need report frame silently dropped. */
  1655. np->stats.rx_crc_errors += readl(ioaddr + RxCRCErrs);
  1656. np->stats.rx_missed_errors += readl(ioaddr + RxMissed);
  1657. }
  1658. static struct net_device_stats *get_stats(struct net_device *dev)
  1659. {
  1660. struct netdev_private *np = dev->priv;
  1661. /* The chip only need report frame silently dropped. */
  1662. spin_lock_irq(&np->lock);
  1663. if (netif_running(dev) && !np->hands_off)
  1664. __get_stats(dev);
  1665. spin_unlock_irq(&np->lock);
  1666. return &np->stats;
  1667. }
  1668. /**
  1669.  * dp83815_crc - computer CRC for hash table entries
  1670.  *
  1671.  * Note - this is, for some reason, *not* the same function
  1672.  * as ether_crc_le() or ether_crc(), though it uses the
  1673.  * same big-endian polynomial.
  1674.  */
  1675. #define DP_POLYNOMIAL 0x04C11DB7
  1676. static unsigned dp83815_crc(int length, unsigned char *data)
  1677. {
  1678. u32 crc;
  1679. u8 cur_byte;
  1680. u8 msb;
  1681. u8 byte, bit;
  1682. crc = ~0;
  1683. for (byte=0; byte<length; byte++) {
  1684. cur_byte = *data++;
  1685. for (bit=0; bit<8; bit++) {
  1686. msb = crc >> 31;
  1687. crc <<= 1;
  1688. if (msb ^ (cur_byte & 1)) {
  1689. crc ^= DP_POLYNOMIAL;
  1690. crc |= 1;
  1691. }
  1692. cur_byte >>= 1;
  1693. }
  1694. }
  1695. crc >>= 23;
  1696. return (crc);
  1697. }
  1698. void set_bit_le(int offset, unsigned char * data)
  1699. {
  1700. data[offset >> 3] |= (1 << (offset & 0x07));
  1701. }
  1702. #define HASH_TABLE 0x200
  1703. static void __set_rx_mode(struct net_device *dev)
  1704. {
  1705. long ioaddr = dev->base_addr;
  1706. struct netdev_private *np = dev->priv;
  1707. u8 mc_filter[64]; /* Multicast hash filter */
  1708. u32 rx_mode;
  1709. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  1710. /* Unconditionally log net taps. */
  1711. printk(KERN_NOTICE "%s: Promiscuous mode enabled.n",
  1712. dev->name);
  1713. rx_mode = RxFilterEnable | AcceptBroadcast
  1714. | AcceptAllMulticast | AcceptAllPhys | AcceptMyPhys;
  1715. } else if ((dev->mc_count > multicast_filter_limit)
  1716.   || (dev->flags & IFF_ALLMULTI)) {
  1717. rx_mode = RxFilterEnable | AcceptBroadcast
  1718. | AcceptAllMulticast | AcceptMyPhys;
  1719. } else {
  1720. struct dev_mc_list *mclist;
  1721. int i;
  1722. memset(mc_filter, 0, sizeof(mc_filter));
  1723. for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
  1724.  i++, mclist = mclist->next) {
  1725. set_bit_le(
  1726. dp83815_crc(ETH_ALEN, mclist->dmi_addr) & 0x1ff,
  1727. mc_filter);
  1728. }
  1729. rx_mode = RxFilterEnable | AcceptBroadcast
  1730. | AcceptMulticast | AcceptMyPhys;
  1731. for (i = 0; i < 64; i += 2) {
  1732. writew(HASH_TABLE + i, ioaddr + RxFilterAddr);
  1733. writew((mc_filter[i+1]<<8) + mc_filter[i],
  1734. ioaddr + RxFilterData);
  1735. }
  1736. }
  1737. writel(rx_mode, ioaddr + RxFilterAddr);
  1738. np->cur_rx_mode = rx_mode;
  1739. }
  1740. static void set_rx_mode(struct net_device *dev)
  1741. {
  1742. struct netdev_private *np = dev->priv;
  1743. spin_lock_irq(&np->lock);
  1744. if (!np->hands_off)
  1745. __set_rx_mode(dev);
  1746. spin_unlock_irq(&np->lock);
  1747. }
  1748. static int netdev_ethtool_ioctl(struct net_device *dev, void *useraddr)
  1749. {
  1750. struct netdev_private *np = dev->priv;
  1751. u32 cmd;
  1752. if (get_user(cmd, (u32 *)useraddr))
  1753. return -EFAULT;
  1754. switch (cmd) {
  1755. /* get driver info */
  1756. case ETHTOOL_GDRVINFO: {
  1757. struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
  1758. strncpy(info.driver, DRV_NAME, ETHTOOL_BUSINFO_LEN);
  1759. strncpy(info.version, DRV_VERSION, ETHTOOL_BUSINFO_LEN);
  1760. info.fw_version[0] = '';
  1761. strncpy(info.bus_info, np->pci_dev->slot_name,
  1762. ETHTOOL_BUSINFO_LEN);
  1763. info.eedump_len = NATSEMI_EEPROM_SIZE;
  1764. info.regdump_len = NATSEMI_REGS_SIZE;
  1765. if (copy_to_user(useraddr, &info, sizeof(info)))
  1766. return -EFAULT;
  1767. return 0;
  1768. }
  1769. /* get settings */
  1770. case ETHTOOL_GSET: {
  1771. struct ethtool_cmd ecmd = { ETHTOOL_GSET };
  1772. spin_lock_irq(&np->lock);
  1773. netdev_get_ecmd(dev, &ecmd);
  1774. spin_unlock_irq(&np->lock);
  1775. if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
  1776. return -EFAULT;
  1777. return 0;
  1778. }
  1779. /* set settings */
  1780. case ETHTOOL_SSET: {
  1781. struct ethtool_cmd ecmd;
  1782. int r;
  1783. if (copy_from_user(&ecmd, useraddr, sizeof(ecmd)))
  1784. return -EFAULT;
  1785. spin_lock_irq(&np->lock);
  1786. r = netdev_set_ecmd(dev, &ecmd);
  1787. spin_unlock_irq(&np->lock);
  1788. return r;
  1789. }
  1790. /* get wake-on-lan */
  1791. case ETHTOOL_GWOL: {
  1792. struct ethtool_wolinfo wol = {ETHTOOL_GWOL};
  1793. spin_lock_irq(&np->lock);
  1794. netdev_get_wol(dev, &wol.supported, &wol.wolopts);
  1795. netdev_get_sopass(dev, wol.sopass);
  1796. spin_unlock_irq(&np->lock);
  1797. if (copy_to_user(useraddr, &wol, sizeof(wol)))
  1798. return -EFAULT;
  1799. return 0;
  1800. }
  1801. /* set wake-on-lan */
  1802. case ETHTOOL_SWOL: {
  1803. struct ethtool_wolinfo wol;
  1804. int r;
  1805. if (copy_from_user(&wol, useraddr, sizeof(wol)))
  1806. return -EFAULT;
  1807. spin_lock_irq(&np->lock);
  1808. netdev_set_wol(dev, wol.wolopts);
  1809. r = netdev_set_sopass(dev, wol.sopass);
  1810. spin_unlock_irq(&np->lock);
  1811. return r;
  1812. }
  1813. /* get registers */
  1814. case ETHTOOL_GREGS: {
  1815. struct ethtool_regs regs;
  1816. u8 regbuf[NATSEMI_REGS_SIZE];
  1817. int r;
  1818. if (copy_from_user(&regs, useraddr, sizeof(regs)))
  1819. return -EFAULT;
  1820. if (regs.len > NATSEMI_REGS_SIZE) {
  1821. regs.len = NATSEMI_REGS_SIZE;
  1822. }
  1823. regs.version = NATSEMI_REGS_VER;
  1824. if (copy_to_user(useraddr, &regs, sizeof(regs)))
  1825. return -EFAULT;
  1826. useraddr += offsetof(struct ethtool_regs, data);
  1827. spin_lock_irq(&np->lock);
  1828. r = netdev_get_regs(dev, regbuf);
  1829. spin_unlock_irq(&np->lock);
  1830. if (r)
  1831. return r;
  1832. if (copy_to_user(useraddr, regbuf, regs.len))
  1833. return -EFAULT;
  1834. return 0;
  1835. }
  1836. /* get message-level */
  1837. case ETHTOOL_GMSGLVL: {
  1838. struct ethtool_value edata = {ETHTOOL_GMSGLVL};
  1839. edata.data = np->msg_enable;
  1840. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1841. return -EFAULT;
  1842. return 0;
  1843. }
  1844. /* set message-level */
  1845. case ETHTOOL_SMSGLVL: {
  1846. struct ethtool_value edata;
  1847. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1848. return -EFAULT;
  1849. np->msg_enable = edata.data;
  1850. return 0;
  1851. }
  1852. /* restart autonegotiation */
  1853. case ETHTOOL_NWAY_RST: {
  1854. int tmp;
  1855. int r = -EINVAL;
  1856. /* if autoneg is off, it's an error */
  1857. tmp = mdio_read(dev, 1, MII_BMCR);
  1858. if (tmp & BMCR_ANENABLE) {
  1859. tmp |= (BMCR_ANRESTART);
  1860. mdio_write(dev, 1, MII_BMCR, tmp);
  1861. r = 0;
  1862. }
  1863. return r;
  1864. }
  1865. /* get link status */
  1866. case ETHTOOL_GLINK: {
  1867. struct ethtool_value edata = {ETHTOOL_GLINK};
  1868. /* LSTATUS is latched low until a read - so read twice */
  1869. mdio_read(dev, 1, MII_BMSR);
  1870. edata.data = (mdio_read(dev, 1, MII_BMSR)&BMSR_LSTATUS) ? 1:0;
  1871. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1872. return -EFAULT;
  1873. return 0;
  1874. }
  1875. /* get EEPROM */
  1876. case ETHTOOL_GEEPROM: {
  1877. struct ethtool_eeprom eeprom;
  1878. u8 eebuf[NATSEMI_EEPROM_SIZE];
  1879. int r;
  1880. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  1881. return -EFAULT;
  1882. if (eeprom.offset > eeprom.offset+eeprom.len)
  1883. return -EINVAL;
  1884. if ((eeprom.offset+eeprom.len) > NATSEMI_EEPROM_SIZE) {
  1885. eeprom.len = NATSEMI_EEPROM_SIZE-eeprom.offset;
  1886. }
  1887. eeprom.magic = PCI_VENDOR_ID_NS | (PCI_DEVICE_ID_NS_83815<<16);
  1888. if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
  1889. return -EFAULT;
  1890. useraddr += offsetof(struct ethtool_eeprom, data);
  1891. spin_lock_irq(&np->lock);
  1892. r = netdev_get_eeprom(dev, eebuf);
  1893. spin_unlock_irq(&np->lock);
  1894. if (r)
  1895. return r;
  1896. if (copy_to_user(useraddr, eebuf+eeprom.offset, eeprom.len))
  1897. return -EFAULT;
  1898. return 0;
  1899. }
  1900. }
  1901. return -EOPNOTSUPP;
  1902. }
  1903. static int netdev_set_wol(struct net_device *dev, u32 newval)
  1904. {
  1905. struct netdev_private *np = dev->priv;
  1906. u32 data = readl(dev->base_addr + WOLCmd) & ~WakeOptsSummary;
  1907. /* translate to bitmasks this chip understands */
  1908. if (newval & WAKE_PHY)
  1909. data |= WakePhy;
  1910. if (newval & WAKE_UCAST)
  1911. data |= WakeUnicast;
  1912. if (newval & WAKE_MCAST)
  1913. data |= WakeMulticast;
  1914. if (newval & WAKE_BCAST)
  1915. data |= WakeBroadcast;
  1916. if (newval & WAKE_ARP)
  1917. data |= WakeArp;
  1918. if (newval & WAKE_MAGIC)
  1919. data |= WakeMagic;
  1920. if (np->srr >= SRR_DP83815_D) {
  1921. if (newval & WAKE_MAGICSECURE) {
  1922. data |= WakeMagicSecure;
  1923. }
  1924. }
  1925. writel(data, dev->base_addr + WOLCmd);
  1926. return 0;
  1927. }
  1928. static int netdev_get_wol(struct net_device *dev, u32 *supported, u32 *cur)
  1929. {
  1930. struct netdev_private *np = dev->priv;
  1931. u32 regval = readl(dev->base_addr + WOLCmd);
  1932. *supported = (WAKE_PHY | WAKE_UCAST | WAKE_MCAST | WAKE_BCAST
  1933. | WAKE_ARP | WAKE_MAGIC);
  1934. if (np->srr >= SRR_DP83815_D) {
  1935. /* SOPASS works on revD and higher */
  1936. *supported |= WAKE_MAGICSECURE;
  1937. }
  1938. *cur = 0;
  1939. /* translate from chip bitmasks */
  1940. if (regval & WakePhy)
  1941. *cur |= WAKE_PHY;
  1942. if (regval & WakeUnicast)
  1943. *cur |= WAKE_UCAST;
  1944. if (regval & WakeMulticast)
  1945. *cur |= WAKE_MCAST;
  1946. if (regval & WakeBroadcast)
  1947. *cur |= WAKE_BCAST;
  1948. if (regval & WakeArp)
  1949. *cur |= WAKE_ARP;
  1950. if (regval & WakeMagic)
  1951. *cur |= WAKE_MAGIC;
  1952. if (regval & WakeMagicSecure) {
  1953. /* this can be on in revC, but it's broken */
  1954. *cur |= WAKE_MAGICSECURE;
  1955. }
  1956. return 0;
  1957. }
  1958. static int netdev_set_sopass(struct net_device *dev, u8 *newval)
  1959. {
  1960. struct netdev_private *np = dev->priv;
  1961. u16 *sval = (u16 *)newval;
  1962. u32 addr;
  1963. if (np->srr < SRR_DP83815_D) {
  1964. return 0;
  1965. }
  1966. /* enable writing to these registers by disabling the RX filter */
  1967. addr = readl(dev->base_addr + RxFilterAddr) & ~RFCRAddressMask;
  1968. addr &= ~RxFilterEnable;
  1969. writel(addr, dev->base_addr + RxFilterAddr);
  1970. /* write the three words to (undocumented) RFCR vals 0xa, 0xc, 0xe */
  1971. writel(addr | 0xa, dev->base_addr + RxFilterAddr);
  1972. writew(sval[0], dev->base_addr + RxFilterData);
  1973. writel(addr | 0xc, dev->base_addr + RxFilterAddr);
  1974. writew(sval[1], dev->base_addr + RxFilterData);
  1975. writel(addr | 0xe, dev->base_addr + RxFilterAddr);
  1976. writew(sval[2], dev->base_addr + RxFilterData);
  1977. /* re-enable the RX filter */
  1978. writel(addr | RxFilterEnable, dev->base_addr + RxFilterAddr);
  1979. return 0;
  1980. }
  1981. static int netdev_get_sopass(struct net_device *dev, u8 *data)
  1982. {
  1983. struct netdev_private *np = dev->priv;
  1984. u16 *sval = (u16 *)data;
  1985. u32 addr;
  1986. if (np->srr < SRR_DP83815_D) {
  1987. sval[0] = sval[1] = sval[2] = 0;
  1988. return 0;
  1989. }
  1990. /* read the three words from (undocumented) RFCR vals 0xa, 0xc, 0xe */
  1991. addr = readl(dev->base_addr + RxFilterAddr) & ~RFCRAddressMask;
  1992. writel(addr | 0xa, dev->base_addr + RxFilterAddr);
  1993. sval[0] = readw(dev->base_addr + RxFilterData);
  1994. writel(addr | 0xc, dev->base_addr + RxFilterAddr);
  1995. sval[1] = readw(dev->base_addr + RxFilterData);
  1996. writel(addr | 0xe, dev->base_addr + RxFilterAddr);
  1997. sval[2] = readw(dev->base_addr + RxFilterData);
  1998. writel(addr, dev->base_addr + RxFilterAddr);
  1999. return 0;
  2000. }
  2001. static int netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
  2002. {
  2003. u32 tmp;
  2004. ecmd->supported =
  2005. (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
  2006. SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
  2007. SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);
  2008. /* only supports twisted-pair or MII */
  2009. tmp = readl(dev->base_addr + ChipConfig);
  2010. if (tmp & CfgExtPhy)
  2011. ecmd->port = PORT_MII;
  2012. else
  2013. ecmd->port = PORT_TP;
  2014. /* only supports internal transceiver */
  2015. ecmd->transceiver = XCVR_INTERNAL;
  2016. /* not sure what this is for */
  2017. ecmd->phy_address = readw(dev->base_addr + PhyCtrl) & PhyAddrMask;
  2018. ecmd->advertising = ADVERTISED_TP | ADVERTISED_MII;
  2019. tmp = mdio_read(dev, 1, MII_ADVERTISE);
  2020. if (tmp & ADVERTISE_10HALF)
  2021. ecmd->advertising |= ADVERTISED_10baseT_Half;
  2022. if (tmp & ADVERTISE_10FULL)
  2023. ecmd->advertising |= ADVERTISED_10baseT_Full;
  2024. if (tmp & ADVERTISE_100HALF)
  2025. ecmd->advertising |= ADVERTISED_100baseT_Half;
  2026. if (tmp & ADVERTISE_100FULL)
  2027. ecmd->advertising |= ADVERTISED_100baseT_Full;
  2028. tmp = mdio_read(dev, 1, MII_BMCR);
  2029. if (tmp & BMCR_ANENABLE) {
  2030. ecmd->advertising |= ADVERTISED_Autoneg;
  2031. ecmd->autoneg = AUTONEG_ENABLE;
  2032. } else {
  2033. ecmd->autoneg = AUTONEG_DISABLE;
  2034. }
  2035. tmp = readl(dev->base_addr + ChipConfig);
  2036. if (tmp & CfgSpeed100) {
  2037. ecmd->speed = SPEED_100;
  2038. } else {
  2039. ecmd->speed = SPEED_10;
  2040. }
  2041. if (tmp & CfgFullDuplex) {
  2042. ecmd->duplex = DUPLEX_FULL;
  2043. } else {
  2044. ecmd->duplex = DUPLEX_HALF;
  2045. }
  2046. /* ignore maxtxpkt, maxrxpkt for now */
  2047. return 0;
  2048. }
  2049. static int netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
  2050. {
  2051. struct netdev_private *np = dev->priv;
  2052. u32 tmp;
  2053. if (ecmd->speed != SPEED_10 && ecmd->speed != SPEED_100)
  2054. return -EINVAL;
  2055. if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
  2056. return -EINVAL;
  2057. if (ecmd->port != PORT_TP && ecmd->port != PORT_MII)
  2058. return -EINVAL;
  2059. if (ecmd->transceiver != XCVR_INTERNAL)
  2060. return -EINVAL;
  2061. if (ecmd->autoneg != AUTONEG_DISABLE && ecmd->autoneg != AUTONEG_ENABLE)
  2062. return -EINVAL;
  2063. /* ignore phy_address, maxtxpkt, maxrxpkt for now */
  2064. /* WHEW! now lets bang some bits */
  2065. tmp = mdio_read(dev, 1, MII_BMCR);
  2066. if (ecmd->autoneg == AUTONEG_ENABLE) {
  2067. /* turn on autonegotiation */
  2068. tmp |= BMCR_ANENABLE;
  2069. np->advertising = mdio_read(dev, 1, MII_ADVERTISE);
  2070. } else {
  2071. /* turn off auto negotiation, set speed and duplexity */
  2072. tmp &= ~(BMCR_ANENABLE | BMCR_SPEED100 | BMCR_FULLDPLX);
  2073. if (ecmd->speed == SPEED_100)
  2074. tmp |= BMCR_SPEED100;
  2075. if (ecmd->duplex == DUPLEX_FULL)
  2076. tmp |= BMCR_FULLDPLX;
  2077. else
  2078. np->full_duplex = 0;
  2079. }
  2080. mdio_write(dev, 1, MII_BMCR, tmp);
  2081. return 0;
  2082. }
  2083. static int netdev_get_regs(struct net_device *dev, u8 *buf)
  2084. {
  2085. int i;
  2086. int j;
  2087. u32 rfcr;
  2088. u32 *rbuf = (u32 *)buf;
  2089. /* read all of page 0 of registers */
  2090. for (i = 0; i < NATSEMI_PG0_NREGS; i++) {
  2091. rbuf[i] = readl(dev->base_addr + i*4);
  2092. }
  2093. /* read only the 'magic' registers from page 1 */
  2094. writew(1, dev->base_addr + PGSEL);
  2095. rbuf[i++] = readw(dev->base_addr + PMDCSR);
  2096. rbuf[i++] = readw(dev->base_addr + TSTDAT);
  2097. rbuf[i++] = readw(dev->base_addr + DSPCFG);
  2098. rbuf[i++] = readw(dev->base_addr + SDCFG);
  2099. writew(0, dev->base_addr + PGSEL);
  2100. /* read RFCR indexed registers */
  2101. rfcr = readl(dev->base_addr + RxFilterAddr);
  2102. for (j = 0; j < NATSEMI_RFDR_NREGS; j++) {
  2103. writel(j*2, dev->base_addr + RxFilterAddr);
  2104. rbuf[i++] = readw(dev->base_addr + RxFilterData);
  2105. }
  2106. writel(rfcr, dev->base_addr + RxFilterAddr);
  2107. /* the interrupt status is clear-on-read - see if we missed any */
  2108. if (rbuf[4] & rbuf[5]) {
  2109. printk(KERN_WARNING
  2110. "%s: shoot, we dropped an interrupt (%#08x)n",
  2111. dev->name, rbuf[4] & rbuf[5]);
  2112. }
  2113. return 0;
  2114. }
  2115. #define SWAP_BITS(x) ( (((x) & 0x0001) << 15) | (((x) & 0x0002) << 13) 
  2116. | (((x) & 0x0004) << 11) | (((x) & 0x0008) << 9)  
  2117. | (((x) & 0x0010) << 7)  | (((x) & 0x0020) << 5)  
  2118. | (((x) & 0x0040) << 3)  | (((x) & 0x0080) << 1)  
  2119. | (((x) & 0x0100) >> 1)  | (((x) & 0x0200) >> 3)  
  2120. | (((x) & 0x0400) >> 5)  | (((x) & 0x0800) >> 7)  
  2121. | (((x) & 0x1000) >> 9)  | (((x) & 0x2000) >> 11) 
  2122. | (((x) & 0x4000) >> 13) | (((x) & 0x8000) >> 15) )
  2123. static int netdev_get_eeprom(struct net_device *dev, u8 *buf)
  2124. {
  2125. int i;
  2126. u16 *ebuf = (u16 *)buf;
  2127. /* eeprom_read reads 16 bits, and indexes by 16 bits */
  2128. for (i = 0; i < NATSEMI_EEPROM_SIZE/2; i++) {
  2129. ebuf[i] = eeprom_read(dev->base_addr, i);
  2130. /* The EEPROM itself stores data bit-swapped, but eeprom_read
  2131.  * reads it back "sanely". So we swap it back here in order to
  2132.  * present it to userland as it is stored. */
  2133. ebuf[i] = SWAP_BITS(ebuf[i]);
  2134. }
  2135. return 0;
  2136. }
  2137. static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2138. {
  2139. struct mii_ioctl_data *data = (struct mii_ioctl_data *)&rq->ifr_data;
  2140. switch(cmd) {
  2141. case SIOCETHTOOL:
  2142. return netdev_ethtool_ioctl(dev, (void *) rq->ifr_data);
  2143. case SIOCGMIIPHY: /* Get address of MII PHY in use. */
  2144. case SIOCDEVPRIVATE: /* for binary compat, remove in 2.5 */
  2145. data->phy_id = 1;
  2146. /* Fall Through */
  2147. case SIOCGMIIREG: /* Read MII PHY register. */
  2148. case SIOCDEVPRIVATE+1: /* for binary compat, remove in 2.5 */
  2149. data->val_out = mdio_read(dev, data->phy_id & 0x1f,
  2150. data->reg_num & 0x1f);
  2151. return 0;
  2152. case SIOCSMIIREG: /* Write MII PHY register. */
  2153. case SIOCDEVPRIVATE+2: /* for binary compat, remove in 2.5 */
  2154. if (!capable(CAP_NET_ADMIN))
  2155. return -EPERM;
  2156. mdio_write(dev, data->phy_id & 0x1f, data->reg_num & 0x1f,
  2157. data->val_in);
  2158. return 0;
  2159. default:
  2160. return -EOPNOTSUPP;
  2161. }
  2162. }
  2163. static void enable_wol_mode(struct net_device *dev, int enable_intr)
  2164. {
  2165. long ioaddr = dev->base_addr;
  2166. struct netdev_private *np = dev->priv;
  2167. if (netif_msg_wol(np))
  2168. printk(KERN_INFO "%s: remaining active for wake-on-lann",
  2169. dev->name);
  2170. /* For WOL we must restart the rx process in silent mode.
  2171.  * Write NULL to the RxRingPtr. Only possible if
  2172.  * rx process is stopped
  2173.  */
  2174. writel(0, ioaddr + RxRingPtr);
  2175. /* read WoL status to clear */
  2176. readl(ioaddr + WOLCmd);
  2177. /* PME on, clear status */
  2178. writel(np->SavedClkRun | PMEEnable | PMEStatus, ioaddr + ClkRun);
  2179. /* and restart the rx process */
  2180. writel(RxOn, ioaddr + ChipCmd);
  2181. if (enable_intr) {
  2182. /* enable the WOL interrupt.
  2183.  * Could be used to send a netlink message.
  2184.  */
  2185. writel(WOLPkt | LinkChange, ioaddr + IntrMask);
  2186. writel(1, ioaddr + IntrEnable);
  2187. }
  2188. }
  2189. static int netdev_close(struct net_device *dev)
  2190. {
  2191. long ioaddr = dev->base_addr;
  2192. struct netdev_private *np = dev->priv;
  2193. if (netif_msg_ifdown(np))
  2194. printk(KERN_DEBUG
  2195. "%s: Shutting down ethercard, status was %#04x.n",
  2196. dev->name, (int)readl(ioaddr + ChipCmd));
  2197. if (netif_msg_pktdata(np))
  2198. printk(KERN_DEBUG
  2199. "%s: Queue pointers were Tx %d / %d,  Rx %d / %d.n",
  2200. dev->name, np->cur_tx, np->dirty_tx,
  2201. np->cur_rx, np->dirty_rx);
  2202. /*
  2203.  * FIXME: what if someone tries to close a device
  2204.  * that is suspended?
  2205.  * Should we reenable the nic to switch to
  2206.  * the final WOL settings?
  2207.  */
  2208. del_timer_sync(&np->timer);
  2209. disable_irq(dev->irq);
  2210. spin_lock_irq(&np->lock);
  2211. /* Disable interrupts, and flush posted writes */
  2212. writel(0, ioaddr + IntrEnable);
  2213. readl(ioaddr + IntrEnable);
  2214. np->hands_off = 1;
  2215. spin_unlock_irq(&np->lock);
  2216. enable_irq(dev->irq);
  2217. free_irq(dev->irq, dev);
  2218. /* Interrupt disabled, interrupt handler released,
  2219.  * queue stopped, timer deleted, rtnl_lock held
  2220.  * All async codepaths that access the driver are disabled.
  2221.  */
  2222. spin_lock_irq(&np->lock);
  2223. np->hands_off = 0;
  2224. readl(ioaddr + IntrMask);
  2225. readw(ioaddr + MIntrStatus);
  2226. /* Freeze Stats */
  2227. writel(StatsFreeze, ioaddr + StatsCtrl);
  2228. /* Stop the chip's Tx and Rx processes. */
  2229. natsemi_stop_rxtx(dev);
  2230. __get_stats(dev);
  2231. spin_unlock_irq(&np->lock);
  2232. /* clear the carrier last - an interrupt could reenable it otherwise */
  2233. netif_carrier_off(dev);
  2234. netif_stop_queue(dev);
  2235. dump_ring(dev);
  2236. drain_ring(dev);
  2237. free_ring(dev);
  2238. {
  2239. u32 wol = readl(ioaddr + WOLCmd) & WakeOptsSummary;
  2240. if (wol) {
  2241. /* restart the NIC in WOL mode.
  2242.  * The nic must be stopped for this.
  2243.  */
  2244. enable_wol_mode(dev, 0);
  2245. } else {
  2246. /* Restore PME enable bit unmolested */
  2247. writel(np->SavedClkRun, ioaddr + ClkRun);
  2248. }
  2249. }
  2250. return 0;
  2251. }
  2252. static void __devexit natsemi_remove1 (struct pci_dev *pdev)
  2253. {
  2254. struct net_device *dev = pci_get_drvdata(pdev);
  2255. unregister_netdev (dev);
  2256. pci_release_regions (pdev);
  2257. iounmap ((char *) dev->base_addr);
  2258. kfree (dev);
  2259. pci_set_drvdata(pdev, NULL);
  2260. }
  2261. #ifdef CONFIG_PM
  2262. /*
  2263.  * The ns83815 chip doesn't have explicit RxStop bits.
  2264.  * Kicking the Rx or Tx process for a new packet reenables the Rx process
  2265.  * of the nic, thus this function must be very careful:
  2266.  *
  2267.  * suspend/resume synchronization:
  2268.  * entry points:
  2269.  *   netdev_open, netdev_close, netdev_ioctl, set_rx_mode, intr_handler,
  2270.  *   start_tx, tx_timeout
  2271.  *
  2272.  * No function accesses the hardware without checking np->hands_off.
  2273.  * the check occurs under spin_lock_irq(&np->lock);
  2274.  * exceptions:
  2275.  * * netdev_ioctl: noncritical access.
  2276.  * * netdev_open: cannot happen due to the device_detach
  2277.  * * netdev_close: doesn't hurt.
  2278.  * * netdev_timer: timer stopped by natsemi_suspend.
  2279.  * * intr_handler: doesn't acquire the spinlock. suspend calls
  2280.  * disable_irq() to enforce synchronization.
  2281.  *
  2282.  * Interrupts must be disabled, otherwise hands_off can cause irq storms.
  2283.  */
  2284. static int natsemi_suspend (struct pci_dev *pdev, u32 state)
  2285. {
  2286. struct net_device *dev = pci_get_drvdata (pdev);
  2287. struct netdev_private *np = dev->priv;
  2288. long ioaddr = dev->base_addr;
  2289. rtnl_lock();
  2290. if (netif_running (dev)) {
  2291. del_timer_sync(&np->timer);
  2292. disable_irq(dev->irq);
  2293. spin_lock_irq(&np->lock);
  2294. writel(0, ioaddr + IntrEnable);
  2295. np->hands_off = 1;
  2296. natsemi_stop_rxtx(dev);
  2297. netif_stop_queue(dev);
  2298. spin_unlock_irq(&np->lock);
  2299. enable_irq(dev->irq);
  2300. /* Update the error counts. */
  2301. __get_stats(dev);
  2302. /* pci_power_off(pdev, -1); */
  2303. drain_ring(dev);
  2304. {
  2305. u32 wol = readl(ioaddr + WOLCmd) & WakeOptsSummary;
  2306. /* Restore PME enable bit */
  2307. if (wol) {
  2308. /* restart the NIC in WOL mode.
  2309.  * The nic must be stopped for this.
  2310.  * FIXME: use the WOL interupt
  2311.  */
  2312. enable_wol_mode(dev, 0);
  2313. } else {
  2314. /* Restore PME enable bit unmolested */
  2315. writel(np->SavedClkRun, ioaddr + ClkRun);
  2316. }
  2317. }
  2318. }
  2319. netif_device_detach(dev);
  2320. rtnl_unlock();
  2321. return 0;
  2322. }
  2323. static int natsemi_resume (struct pci_dev *pdev)
  2324. {
  2325. struct net_device *dev = pci_get_drvdata (pdev);
  2326. struct netdev_private *np = dev->priv;
  2327. rtnl_lock();
  2328. if (netif_device_present(dev))
  2329. goto out;
  2330. if (netif_running(dev)) {
  2331. BUG_ON(!np->hands_off);
  2332. pci_enable_device(pdev);
  2333. /* pci_power_on(pdev); */
  2334. natsemi_reset(dev);
  2335. init_ring(dev);
  2336. disable_irq(dev->irq);
  2337. spin_lock_irq(&np->lock);
  2338. np->hands_off = 0;
  2339. init_registers(dev);
  2340. netif_device_attach(dev);
  2341. spin_unlock_irq(&np->lock);
  2342. enable_irq(dev->irq);
  2343. mod_timer(&np->timer, jiffies + 1*HZ);
  2344. }
  2345. netif_device_attach(dev);
  2346. out:
  2347. rtnl_unlock();
  2348. return 0;
  2349. }
  2350. #endif /* CONFIG_PM */
  2351. static struct pci_driver natsemi_driver = {
  2352. .name = DRV_NAME,
  2353. .id_table = natsemi_pci_tbl,
  2354. .probe = natsemi_probe1,
  2355. .remove = __devexit_p(natsemi_remove1),
  2356. #ifdef CONFIG_PM
  2357. .suspend = natsemi_suspend,
  2358. .resume = natsemi_resume,
  2359. #endif
  2360. };
  2361. static int __init natsemi_init_mod (void)
  2362. {
  2363. /* when a module, this is printed whether or not devices are found in probe */
  2364. #ifdef MODULE
  2365. printk(version);
  2366. #endif
  2367. return pci_module_init (&natsemi_driver);
  2368. }
  2369. static void __exit natsemi_exit_mod (void)
  2370. {
  2371. pci_unregister_driver (&natsemi_driver);
  2372. }
  2373. module_init(natsemi_init_mod);
  2374. module_exit(natsemi_exit_mod);