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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* drivers/net/eepro100.c: An Intel i82557-559 Ethernet driver for Linux. */
  2. /*
  3. Written 1996-1999 by Donald Becker.
  4. The driver also contains updates by different kernel developers
  5. (see incomplete list below).
  6. Current maintainer is Andrey V. Savochkin <saw@saw.sw.com.sg>.
  7. Please use this email address and linux-kernel mailing list for bug reports.
  8. This software may be used and distributed according to the terms
  9. of the GNU General Public License, incorporated herein by reference.
  10. This driver is for the Intel EtherExpress Pro100 (Speedo3) design.
  11. It should work with all i82557/558/559 boards.
  12. Version history:
  13. 1998 Apr - 2000 Feb  Andrey V. Savochkin <saw@saw.sw.com.sg>
  14. Serious fixes for multicast filter list setting, TX timeout routine;
  15. RX ring refilling logic;  other stuff
  16. 2000 Feb  Jeff Garzik <jgarzik@mandrakesoft.com>
  17. Convert to new PCI driver interface
  18. 2000 Mar 24  Dragan Stancevic <visitor@valinux.com>
  19. Disabled FC and ER, to avoid lockups when when we get FCP interrupts.
  20. 2000 Jul 17 Goutham Rao <goutham.rao@intel.com>
  21. PCI DMA API fixes, adding pci_dma_sync_single calls where neccesary
  22. 2000 Aug 31 David Mosberger <davidm@hpl.hp.com>
  23. rx_align support: enables rx DMA without causing unaligned accesses.
  24. */
  25. static const char *version =
  26. "eepro100.c:v1.09j-t 9/29/99 Donald Becker http://www.scyld.com/network/eepro100.htmln"
  27. "eepro100.c: $Revision: 1.36 $ 2000/11/17 Modified by Andrey V. Savochkin <saw@saw.sw.com.sg> and othersn";
  28. /* A few user-configurable values that apply to all boards.
  29.    First set is undocumented and spelled per Intel recommendations. */
  30. static int congenb /* = 0 */; /* Enable congestion control in the DP83840. */
  31. static int txfifo = 8; /* Tx FIFO threshold in 4 byte units, 0-15 */
  32. static int rxfifo = 8; /* Rx FIFO threshold, default 32 bytes. */
  33. /* Tx/Rx DMA burst length, 0-127, 0 == no preemption, tx==128 -> disabled. */
  34. static int txdmacount = 128;
  35. static int rxdmacount /* = 0 */;
  36. #if defined(__ia64__) || defined(__alpha__) || defined(__sparc__) || defined(__mips__) || 
  37. defined(__arm__)
  38.   /* align rx buffers to 2 bytes so that IP header is aligned */
  39. # define rx_align(skb) skb_reserve((skb), 2)
  40. # define RxFD_ALIGNMENT __attribute__ ((aligned (2), packed))
  41. #else
  42. # define rx_align(skb)
  43. # define RxFD_ALIGNMENT
  44. #endif
  45. /* Set the copy breakpoint for the copy-only-tiny-buffer Rx method.
  46.    Lower values use more memory, but are faster. */
  47. static int rx_copybreak = 200;
  48. /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
  49. static int max_interrupt_work = 20;
  50. /* Maximum number of multicast addresses to filter (vs. rx-all-multicast) */
  51. static int multicast_filter_limit = 64;
  52. /* 'options' is used to pass a transceiver override or full-duplex flag
  53.    e.g. "options=16" for FD, "options=32" for 100mbps-only. */
  54. static int full_duplex[] = {-1, -1, -1, -1, -1, -1, -1, -1};
  55. static int options[] = {-1, -1, -1, -1, -1, -1, -1, -1};
  56. /* A few values that may be tweaked. */
  57. /* The ring sizes should be a power of two for efficiency. */
  58. #define TX_RING_SIZE 64
  59. #define RX_RING_SIZE 64
  60. /* How much slots multicast filter setup may take.
  61.    Do not descrease without changing set_rx_mode() implementaion. */
  62. #define TX_MULTICAST_SIZE   2
  63. #define TX_MULTICAST_RESERV (TX_MULTICAST_SIZE*2)
  64. /* Actual number of TX packets queued, must be
  65.    <= TX_RING_SIZE-TX_MULTICAST_RESERV. */
  66. #define TX_QUEUE_LIMIT  (TX_RING_SIZE-TX_MULTICAST_RESERV)
  67. /* Hysteresis marking queue as no longer full. */
  68. #define TX_QUEUE_UNFULL (TX_QUEUE_LIMIT-4)
  69. /* Operational parameters that usually are not changed. */
  70. /* Time in jiffies before concluding the transmitter is hung. */
  71. #define TX_TIMEOUT (2*HZ)
  72. /* Size of an pre-allocated Rx buffer: <Ethernet MTU> + slack.*/
  73. #define PKT_BUF_SZ 1536
  74. #if !defined(__OPTIMIZE__)  ||  !defined(__KERNEL__)
  75. #warning  You must compile this file with the correct options!
  76. #warning  See the last lines of the source file.
  77. #error You must compile this driver with "-O".
  78. #endif
  79. #include <linux/config.h>
  80. #include <linux/version.h>
  81. #include <linux/module.h>
  82. #include <linux/kernel.h>
  83. #include <linux/string.h>
  84. #include <linux/errno.h>
  85. #include <linux/ioport.h>
  86. #include <linux/slab.h>
  87. #include <linux/interrupt.h>
  88. #include <linux/timer.h>
  89. #include <linux/pci.h>
  90. #include <linux/spinlock.h>
  91. #include <linux/init.h>
  92. #include <linux/mii.h>
  93. #include <linux/delay.h>
  94. #include <asm/bitops.h>
  95. #include <asm/io.h>
  96. #include <asm/uaccess.h>
  97. #include <linux/netdevice.h>
  98. #include <linux/etherdevice.h>
  99. #include <linux/skbuff.h>
  100. #include <linux/ethtool.h>
  101. #include <linux/mii.h>
  102. static int debug = -1;
  103. #define DEBUG_DEFAULT (NETIF_MSG_DRV | 
  104.  NETIF_MSG_HW | 
  105.  NETIF_MSG_RX_ERR | 
  106.  NETIF_MSG_TX_ERR)
  107. #define DEBUG ((debug >= 0) ? (1<<debug)-1 : DEBUG_DEFAULT)
  108. MODULE_AUTHOR("Maintainer: Andrey V. Savochkin <saw@saw.sw.com.sg>");
  109. MODULE_DESCRIPTION("Intel i82557/i82558/i82559 PCI EtherExpressPro driver");
  110. MODULE_LICENSE("GPL");
  111. MODULE_PARM(debug, "i");
  112. MODULE_PARM(options, "1-" __MODULE_STRING(8) "i");
  113. MODULE_PARM(full_duplex, "1-" __MODULE_STRING(8) "i");
  114. MODULE_PARM(congenb, "i");
  115. MODULE_PARM(txfifo, "i");
  116. MODULE_PARM(rxfifo, "i");
  117. MODULE_PARM(txdmacount, "i");
  118. MODULE_PARM(rxdmacount, "i");
  119. MODULE_PARM(rx_copybreak, "i");
  120. MODULE_PARM(max_interrupt_work, "i");
  121. MODULE_PARM(multicast_filter_limit, "i");
  122. MODULE_PARM_DESC(debug, "debug level (0-6)");
  123. MODULE_PARM_DESC(options, "Bits 0-3: tranceiver type, bit 4: full duplex, bit 5: 100Mbps");
  124. MODULE_PARM_DESC(full_duplex, "full duplex setting(s) (1)");
  125. MODULE_PARM_DESC(congenb, "Enable congestion control (1)");
  126. MODULE_PARM_DESC(txfifo, "Tx FIFO threshold in 4 byte units, (0-15)");
  127. MODULE_PARM_DESC(rxfifo, "Rx FIFO threshold in 4 byte units, (0-15)");
  128. MODULE_PARM_DESC(txdmaccount, "Tx DMA burst length; 128 - disable (0-128)");
  129. MODULE_PARM_DESC(rxdmaccount, "Rx DMA burst length; 128 - disable (0-128)");
  130. MODULE_PARM_DESC(rx_copybreak, "copy breakpoint for copy-only-tiny-frames");
  131. MODULE_PARM_DESC(max_interrupt_work, "maximum events handled per interrupt");
  132. MODULE_PARM_DESC(multicast_filter_limit, "maximum number of filtered multicast addresses");
  133. #define RUN_AT(x) (jiffies + (x))
  134. /* ACPI power states don't universally work (yet) */
  135. #ifndef CONFIG_PM
  136. #undef pci_set_power_state
  137. #define pci_set_power_state null_set_power_state
  138. static inline int null_set_power_state(struct pci_dev *dev, int state)
  139. {
  140. return 0;
  141. }
  142. #endif /* CONFIG_PM */
  143. #define netdevice_start(dev)
  144. #define netdevice_stop(dev)
  145. #define netif_set_tx_timeout(dev, tf, tm) 
  146. do { 
  147. (dev)->tx_timeout = (tf); 
  148. (dev)->watchdog_timeo = (tm); 
  149. } while(0)
  150. /*
  151. Theory of Operation
  152. I. Board Compatibility
  153. This device driver is designed for the Intel i82557 "Speedo3" chip, Intel's
  154. single-chip fast Ethernet controller for PCI, as used on the Intel
  155. EtherExpress Pro 100 adapter.
  156. II. Board-specific settings
  157. PCI bus devices are configured by the system at boot time, so no jumpers
  158. need to be set on the board.  The system BIOS should be set to assign the
  159. PCI INTA signal to an otherwise unused system IRQ line.  While it's
  160. possible to share PCI interrupt lines, it negatively impacts performance and
  161. only recent kernels support it.
  162. III. Driver operation
  163. IIIA. General
  164. The Speedo3 is very similar to other Intel network chips, that is to say
  165. "apparently designed on a different planet".  This chips retains the complex
  166. Rx and Tx descriptors and multiple buffers pointers as previous chips, but
  167. also has simplified Tx and Rx buffer modes.  This driver uses the "flexible"
  168. Tx mode, but in a simplified lower-overhead manner: it associates only a
  169. single buffer descriptor with each frame descriptor.
  170. Despite the extra space overhead in each receive skbuff, the driver must use
  171. the simplified Rx buffer mode to assure that only a single data buffer is
  172. associated with each RxFD. The driver implements this by reserving space
  173. for the Rx descriptor at the head of each Rx skbuff.
  174. The Speedo-3 has receive and command unit base addresses that are added to
  175. almost all descriptor pointers.  The driver sets these to zero, so that all
  176. pointer fields are absolute addresses.
  177. The System Control Block (SCB) of some previous Intel chips exists on the
  178. chip in both PCI I/O and memory space.  This driver uses the I/O space
  179. registers, but might switch to memory mapped mode to better support non-x86
  180. processors.
  181. IIIB. Transmit structure
  182. The driver must use the complex Tx command+descriptor mode in order to
  183. have a indirect pointer to the skbuff data section.  Each Tx command block
  184. (TxCB) is associated with two immediately appended Tx Buffer Descriptor
  185. (TxBD).  A fixed ring of these TxCB+TxBD pairs are kept as part of the
  186. speedo_private data structure for each adapter instance.
  187. The newer i82558 explicitly supports this structure, and can read the two
  188. TxBDs in the same PCI burst as the TxCB.
  189. This ring structure is used for all normal transmit packets, but the
  190. transmit packet descriptors aren't long enough for most non-Tx commands such
  191. as CmdConfigure.  This is complicated by the possibility that the chip has
  192. already loaded the link address in the previous descriptor.  So for these
  193. commands we convert the next free descriptor on the ring to a NoOp, and point
  194. that descriptor's link to the complex command.
  195. An additional complexity of these non-transmit commands are that they may be
  196. added asynchronous to the normal transmit queue, so we disable interrupts
  197. whenever the Tx descriptor ring is manipulated.
  198. A notable aspect of these special configure commands is that they do
  199. work with the normal Tx ring entry scavenge method.  The Tx ring scavenge
  200. is done at interrupt time using the 'dirty_tx' index, and checking for the
  201. command-complete bit.  While the setup frames may have the NoOp command on the
  202. Tx ring marked as complete, but not have completed the setup command, this
  203. is not a problem.  The tx_ring entry can be still safely reused, as the
  204. tx_skbuff[] entry is always empty for config_cmd and mc_setup frames.
  205. Commands may have bits set e.g. CmdSuspend in the command word to either
  206. suspend or stop the transmit/command unit.  This driver always flags the last
  207. command with CmdSuspend, erases the CmdSuspend in the previous command, and
  208. then issues a CU_RESUME.
  209. Note: Watch out for the potential race condition here: imagine
  210. erasing the previous suspend
  211. the chip processes the previous command
  212. the chip processes the final command, and suspends
  213. doing the CU_RESUME
  214. the chip processes the next-yet-valid post-final-command.
  215. So blindly sending a CU_RESUME is only safe if we do it immediately after
  216. after erasing the previous CmdSuspend, without the possibility of an
  217. intervening delay.  Thus the resume command is always within the
  218. interrupts-disabled region.  This is a timing dependence, but handling this
  219. condition in a timing-independent way would considerably complicate the code.
  220. Note: In previous generation Intel chips, restarting the command unit was a
  221. notoriously slow process.  This is presumably no longer true.
  222. IIIC. Receive structure
  223. Because of the bus-master support on the Speedo3 this driver uses the new
  224. SKBUFF_RX_COPYBREAK scheme, rather than a fixed intermediate receive buffer.
  225. This scheme allocates full-sized skbuffs as receive buffers.  The value
  226. SKBUFF_RX_COPYBREAK is used as the copying breakpoint: it is chosen to
  227. trade-off the memory wasted by passing the full-sized skbuff to the queue
  228. layer for all frames vs. the copying cost of copying a frame to a
  229. correctly-sized skbuff.
  230. For small frames the copying cost is negligible (esp. considering that we
  231. are pre-loading the cache with immediately useful header information), so we
  232. allocate a new, minimally-sized skbuff.  For large frames the copying cost
  233. is non-trivial, and the larger copy might flush the cache of useful data, so
  234. we pass up the skbuff the packet was received into.
  235. IV. Notes
  236. Thanks to Steve Williams of Intel for arranging the non-disclosure agreement
  237. that stated that I could disclose the information.  But I still resent
  238. having to sign an Intel NDA when I'm helping Intel sell their own product!
  239. */
  240. static int speedo_found1(struct pci_dev *pdev, long ioaddr, int fnd_cnt, int acpi_idle_state);
  241. enum pci_flags_bit {
  242. PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,
  243. PCI_ADDR0=0x10<<0, PCI_ADDR1=0x10<<1, PCI_ADDR2=0x10<<2, PCI_ADDR3=0x10<<3,
  244. };
  245. static inline unsigned int io_inw(unsigned long port)
  246. {
  247. return inw(port);
  248. }
  249. static inline void io_outw(unsigned int val, unsigned long port)
  250. {
  251. outw(val, port);
  252. }
  253. #ifndef USE_IO
  254. /* Currently alpha headers define in/out macros.
  255.    Undefine them.  2000/03/30  SAW */
  256. #undef inb
  257. #undef inw
  258. #undef inl
  259. #undef outb
  260. #undef outw
  261. #undef outl
  262. #define inb readb
  263. #define inw readw
  264. #define inl readl
  265. #define outb writeb
  266. #define outw writew
  267. #define outl writel
  268. #endif
  269. /* Offsets to the various registers.
  270.    All accesses need not be longword aligned. */
  271. enum speedo_offsets {
  272. SCBStatus = 0, SCBCmd = 2, /* Rx/Command Unit command and status. */
  273. SCBIntmask = 3,
  274. SCBPointer = 4, /* General purpose pointer. */
  275. SCBPort = 8, /* Misc. commands and operands.  */
  276. SCBflash = 12, SCBeeprom = 14, /* EEPROM and flash memory control. */
  277. SCBCtrlMDI = 16, /* MDI interface control. */
  278. SCBEarlyRx = 20, /* Early receive byte count. */
  279. };
  280. /* Commands that can be put in a command list entry. */
  281. enum commands {
  282. CmdNOp = 0, CmdIASetup = 0x10000, CmdConfigure = 0x20000,
  283. CmdMulticastList = 0x30000, CmdTx = 0x40000, CmdTDR = 0x50000,
  284. CmdDump = 0x60000, CmdDiagnose = 0x70000,
  285. CmdSuspend = 0x40000000, /* Suspend after completion. */
  286. CmdIntr = 0x20000000, /* Interrupt after completion. */
  287. CmdTxFlex = 0x00080000, /* Use "Flexible mode" for CmdTx command. */
  288. };
  289. /* Clear CmdSuspend (1<<30) avoiding interference with the card access to the
  290.    status bits.  Previous driver versions used separate 16 bit fields for
  291.    commands and statuses.  --SAW
  292.  */
  293. #if defined(__alpha__)
  294. # define clear_suspend(cmd)  clear_bit(30, &(cmd)->cmd_status);
  295. #else
  296. # if defined(__LITTLE_ENDIAN)
  297. #  define clear_suspend(cmd)  ((__u16 *)&(cmd)->cmd_status)[1] &= ~0x4000
  298. # elif defined(__BIG_ENDIAN)
  299. #  define clear_suspend(cmd)  ((__u16 *)&(cmd)->cmd_status)[1] &= ~0x0040
  300. # else
  301. #  error Unsupported byteorder
  302. # endif
  303. #endif
  304. enum SCBCmdBits {
  305. SCBMaskCmdDone=0x8000, SCBMaskRxDone=0x4000, SCBMaskCmdIdle=0x2000,
  306. SCBMaskRxSuspend=0x1000, SCBMaskEarlyRx=0x0800, SCBMaskFlowCtl=0x0400,
  307. SCBTriggerIntr=0x0200, SCBMaskAll=0x0100,
  308. /* The rest are Rx and Tx commands. */
  309. CUStart=0x0010, CUResume=0x0020, CUStatsAddr=0x0040, CUShowStats=0x0050,
  310. CUCmdBase=0x0060, /* CU Base address (set to zero) . */
  311. CUDumpStats=0x0070, /* Dump then reset stats counters. */
  312. RxStart=0x0001, RxResume=0x0002, RxAbort=0x0004, RxAddrLoad=0x0006,
  313. RxResumeNoResources=0x0007,
  314. };
  315. enum SCBPort_cmds {
  316. PortReset=0, PortSelfTest=1, PortPartialReset=2, PortDump=3,
  317. };
  318. /* The Speedo3 Rx and Tx frame/buffer descriptors. */
  319. struct descriptor {     /* A generic descriptor. */
  320. volatile s32 cmd_status; /* All command and status fields. */
  321. u32 link;     /* struct descriptor *  */
  322. unsigned char params[0];
  323. };
  324. /* The Speedo3 Rx and Tx buffer descriptors. */
  325. struct RxFD { /* Receive frame descriptor. */
  326. volatile s32 status;
  327. u32 link; /* struct RxFD * */
  328. u32 rx_buf_addr; /* void * */
  329. u32 count;
  330. } RxFD_ALIGNMENT;
  331. /* Selected elements of the Tx/RxFD.status word. */
  332. enum RxFD_bits {
  333. RxComplete=0x8000, RxOK=0x2000,
  334. RxErrCRC=0x0800, RxErrAlign=0x0400, RxErrTooBig=0x0200, RxErrSymbol=0x0010,
  335. RxEth2Type=0x0020, RxNoMatch=0x0004, RxNoIAMatch=0x0002,
  336. TxUnderrun=0x1000,  StatusComplete=0x8000,
  337. };
  338. #define CONFIG_DATA_SIZE 22
  339. struct TxFD { /* Transmit frame descriptor set. */
  340. s32 status;
  341. u32 link; /* void * */
  342. u32 tx_desc_addr; /* Always points to the tx_buf_addr element. */
  343. s32 count; /* # of TBD (=1), Tx start thresh., etc. */
  344. /* This constitutes two "TBD" entries -- we only use one. */
  345. #define TX_DESCR_BUF_OFFSET 16
  346. u32 tx_buf_addr0; /* void *, frame to be transmitted.  */
  347. s32 tx_buf_size0; /* Length of Tx frame. */
  348. u32 tx_buf_addr1; /* void *, frame to be transmitted.  */
  349. s32 tx_buf_size1; /* Length of Tx frame. */
  350. /* the structure must have space for at least CONFIG_DATA_SIZE starting
  351.  * from tx_desc_addr field */
  352. };
  353. /* Multicast filter setting block.  --SAW */
  354. struct speedo_mc_block {
  355. struct speedo_mc_block *next;
  356. unsigned int tx;
  357. dma_addr_t frame_dma;
  358. unsigned int len;
  359. struct descriptor frame __attribute__ ((__aligned__(16)));
  360. };
  361. /* Elements of the dump_statistics block. This block must be lword aligned. */
  362. struct speedo_stats {
  363. u32 tx_good_frames;
  364. u32 tx_coll16_errs;
  365. u32 tx_late_colls;
  366. u32 tx_underruns;
  367. u32 tx_lost_carrier;
  368. u32 tx_deferred;
  369. u32 tx_one_colls;
  370. u32 tx_multi_colls;
  371. u32 tx_total_colls;
  372. u32 rx_good_frames;
  373. u32 rx_crc_errs;
  374. u32 rx_align_errs;
  375. u32 rx_resource_errs;
  376. u32 rx_overrun_errs;
  377. u32 rx_colls_errs;
  378. u32 rx_runt_errs;
  379. u32 done_marker;
  380. };
  381. enum Rx_ring_state_bits {
  382. RrNoMem=1, RrPostponed=2, RrNoResources=4, RrOOMReported=8,
  383. };
  384. /* Do not change the position (alignment) of the first few elements!
  385.    The later elements are grouped for cache locality.
  386.    Unfortunately, all the positions have been shifted since there.
  387.    A new re-alignment is required.  2000/03/06  SAW */
  388. struct speedo_private {
  389. struct TxFD *tx_ring; /* Commands (usually CmdTxPacket). */
  390. struct RxFD *rx_ringp[RX_RING_SIZE]; /* Rx descriptor, used as ring. */
  391. /* The addresses of a Tx/Rx-in-place packets/buffers. */
  392. struct sk_buff *tx_skbuff[TX_RING_SIZE];
  393. struct sk_buff *rx_skbuff[RX_RING_SIZE];
  394. /* Mapped addresses of the rings. */
  395. dma_addr_t tx_ring_dma;
  396. #define TX_RING_ELEM_DMA(sp, n) ((sp)->tx_ring_dma + (n)*sizeof(struct TxFD))
  397. dma_addr_t rx_ring_dma[RX_RING_SIZE];
  398. struct descriptor *last_cmd; /* Last command sent. */
  399. unsigned int cur_tx, dirty_tx; /* The ring entries to be free()ed. */
  400. spinlock_t lock; /* Group with Tx control cache line. */
  401. u32 tx_threshold; /* The value for txdesc.count. */
  402. struct RxFD *last_rxf; /* Last filled RX buffer. */
  403. dma_addr_t last_rxf_dma;
  404. unsigned int cur_rx, dirty_rx; /* The next free ring entry */
  405. long last_rx_time; /* Last Rx, in jiffies, to handle Rx hang. */
  406. struct net_device_stats stats;
  407. struct speedo_stats *lstats;
  408. dma_addr_t lstats_dma;
  409. int chip_id;
  410. struct pci_dev *pdev;
  411. struct timer_list timer; /* Media selection timer. */
  412. struct speedo_mc_block *mc_setup_head; /* Multicast setup frame list head. */
  413. struct speedo_mc_block *mc_setup_tail; /* Multicast setup frame list tail. */
  414. long in_interrupt; /* Word-aligned dev->interrupt */
  415. unsigned char acpi_pwr;
  416. signed char rx_mode; /* Current PROMISC/ALLMULTI setting. */
  417. unsigned int tx_full:1; /* The Tx queue is full. */
  418. unsigned int flow_ctrl:1; /* Use 802.3x flow control. */
  419. unsigned int rx_bug:1; /* Work around receiver hang errata. */
  420. unsigned char default_port:8; /* Last dev->if_port value. */
  421. unsigned char rx_ring_state; /* RX ring status flags. */
  422. unsigned short phy[2]; /* PHY media interfaces available. */
  423. unsigned short partner; /* Link partner caps. */
  424. struct mii_if_info mii_if; /* MII API hooks, info */
  425. u32 msg_enable; /* debug message level */
  426. #ifdef CONFIG_PM
  427. u32 pm_state[16];
  428. #endif
  429. };
  430. /* The parameters for a CmdConfigure operation.
  431.    There are so many options that it would be difficult to document each bit.
  432.    We mostly use the default or recommended settings. */
  433. static const char i82557_config_cmd[CONFIG_DATA_SIZE] = {
  434. 22, 0x08, 0, 0,  0, 0, 0x32, 0x03,  1, /* 1=Use MII  0=Use AUI */
  435. 0, 0x2E, 0,  0x60, 0,
  436. 0xf2, 0x48,   0, 0x40, 0xf2, 0x80,  /* 0x40=Force full-duplex */
  437. 0x3f, 0x05, };
  438. static const char i82558_config_cmd[CONFIG_DATA_SIZE] = {
  439. 22, 0x08, 0, 1,  0, 0, 0x22, 0x03,  1, /* 1=Use MII  0=Use AUI */
  440. 0, 0x2E, 0,  0x60, 0x08, 0x88,
  441. 0x68, 0, 0x40, 0xf2, 0x84, /* Disable FC */
  442. 0x31, 0x05, };
  443. /* PHY media interface chips. */
  444. static const char *phys[] = {
  445. "None", "i82553-A/B", "i82553-C", "i82503",
  446. "DP83840", "80c240", "80c24", "i82555",
  447. "unknown-8", "unknown-9", "DP83840A", "unknown-11",
  448. "unknown-12", "unknown-13", "unknown-14", "unknown-15", };
  449. enum phy_chips { NonSuchPhy=0, I82553AB, I82553C, I82503, DP83840, S80C240,
  450.  S80C24, I82555, DP83840A=10, };
  451. static const char is_mii[] = { 0, 1, 1, 0, 1, 1, 0, 1 };
  452. #define EE_READ_CMD (6)
  453. static int eepro100_init_one(struct pci_dev *pdev,
  454. const struct pci_device_id *ent);
  455. static void eepro100_remove_one (struct pci_dev *pdev);
  456. static int do_eeprom_cmd(long ioaddr, int cmd, int cmd_len);
  457. static int mdio_read(struct net_device *dev, int phy_id, int location);
  458. static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
  459. static int speedo_open(struct net_device *dev);
  460. static void speedo_resume(struct net_device *dev);
  461. static void speedo_timer(unsigned long data);
  462. static void speedo_init_rx_ring(struct net_device *dev);
  463. static void speedo_tx_timeout(struct net_device *dev);
  464. static int speedo_start_xmit(struct sk_buff *skb, struct net_device *dev);
  465. static void speedo_refill_rx_buffers(struct net_device *dev, int force);
  466. static int speedo_rx(struct net_device *dev);
  467. static void speedo_tx_buffer_gc(struct net_device *dev);
  468. static void speedo_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
  469. static int speedo_close(struct net_device *dev);
  470. static struct net_device_stats *speedo_get_stats(struct net_device *dev);
  471. static int speedo_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  472. static void set_rx_mode(struct net_device *dev);
  473. static void speedo_show_state(struct net_device *dev);
  474. #ifdef honor_default_port
  475. /* Optional driver feature to allow forcing the transceiver setting.
  476.    Not recommended. */
  477. static int mii_ctrl[8] = { 0x3300, 0x3100, 0x0000, 0x0100,
  478.    0x2000, 0x2100, 0x0400, 0x3100};
  479. #endif
  480. /* How to wait for the command unit to accept a command.
  481.    Typically this takes 0 ticks. */
  482. static inline unsigned char wait_for_cmd_done(struct net_device *dev)
  483. {
  484. int wait = 1000;
  485. long cmd_ioaddr = dev->base_addr + SCBCmd;
  486. unsigned char r;
  487. do  {
  488. udelay(1);
  489. r = inb(cmd_ioaddr);
  490. } while(r && --wait >= 0);
  491. if (wait < 0)
  492. printk(KERN_ALERT "%s: wait_for_cmd_done timeout!n", dev->name);
  493. return r;
  494. }
  495. static int __devinit eepro100_init_one (struct pci_dev *pdev,
  496. const struct pci_device_id *ent)
  497. {
  498. unsigned long ioaddr;
  499. int irq;
  500. int acpi_idle_state = 0, pm;
  501. static int cards_found /* = 0 */;
  502. #ifndef MODULE
  503. /* when built-in, we only print version if device is found */
  504. static int did_version;
  505. if (did_version++ == 0)
  506. printk(version);
  507. #endif
  508. /* save power state before pci_enable_device overwrites it */
  509. pm = pci_find_capability(pdev, PCI_CAP_ID_PM);
  510. if (pm) {
  511. u16 pwr_command;
  512. pci_read_config_word(pdev, pm + PCI_PM_CTRL, &pwr_command);
  513. acpi_idle_state = pwr_command & PCI_PM_CTRL_STATE_MASK;
  514. }
  515. if (pci_enable_device(pdev))
  516. goto err_out_free_mmio_region;
  517. pci_set_master(pdev);
  518. if (!request_region(pci_resource_start(pdev, 1),
  519. pci_resource_len(pdev, 1), "eepro100")) {
  520. printk (KERN_ERR "eepro100: cannot reserve I/O portsn");
  521. goto err_out_none;
  522. }
  523. if (!request_mem_region(pci_resource_start(pdev, 0),
  524. pci_resource_len(pdev, 0), "eepro100")) {
  525. printk (KERN_ERR "eepro100: cannot reserve MMIO regionn");
  526. goto err_out_free_pio_region;
  527. }
  528. irq = pdev->irq;
  529. #ifdef USE_IO
  530. ioaddr = pci_resource_start(pdev, 1);
  531. if (DEBUG & NETIF_MSG_PROBE)
  532. printk("Found Intel i82557 PCI Speedo at I/O %#lx, IRQ %d.n",
  533.    ioaddr, irq);
  534. #else
  535. ioaddr = (unsigned long)ioremap(pci_resource_start(pdev, 0),
  536. pci_resource_len(pdev, 0));
  537. if (!ioaddr) {
  538. printk (KERN_ERR "eepro100: cannot remap MMIO region %lx @ %lxn",
  539. pci_resource_len(pdev, 0), pci_resource_start(pdev, 0));
  540. goto err_out_free_mmio_region;
  541. }
  542. if (DEBUG & NETIF_MSG_PROBE)
  543. printk("Found Intel i82557 PCI Speedo, MMIO at %#lx, IRQ %d.n",
  544.    pci_resource_start(pdev, 0), irq);
  545. #endif
  546. if (speedo_found1(pdev, ioaddr, cards_found, acpi_idle_state) == 0)
  547. cards_found++;
  548. else
  549. goto err_out_iounmap;
  550. return 0;
  551. err_out_iounmap: ;
  552. #ifndef USE_IO
  553. iounmap ((void *)ioaddr);
  554. #endif
  555. err_out_free_mmio_region:
  556. release_mem_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
  557. err_out_free_pio_region:
  558. release_region(pci_resource_start(pdev, 1), pci_resource_len(pdev, 1));
  559. err_out_none:
  560. return -ENODEV;
  561. }
  562. static int __devinit speedo_found1(struct pci_dev *pdev,
  563. long ioaddr, int card_idx, int acpi_idle_state)
  564. {
  565. struct net_device *dev;
  566. struct speedo_private *sp;
  567. const char *product;
  568. int i, option;
  569. u16 eeprom[0x100];
  570. int size;
  571. void *tx_ring_space;
  572. dma_addr_t tx_ring_dma;
  573. size = TX_RING_SIZE * sizeof(struct TxFD) + sizeof(struct speedo_stats);
  574. tx_ring_space = pci_alloc_consistent(pdev, size, &tx_ring_dma);
  575. if (tx_ring_space == NULL)
  576. return -1;
  577. dev = init_etherdev(NULL, sizeof(struct speedo_private));
  578. if (dev == NULL) {
  579. printk(KERN_ERR "eepro100: Could not allocate ethernet device.n");
  580. pci_free_consistent(pdev, size, tx_ring_space, tx_ring_dma);
  581. return -1;
  582. }
  583. SET_MODULE_OWNER(dev);
  584. if (dev->mem_start > 0)
  585. option = dev->mem_start;
  586. else if (card_idx >= 0  &&  options[card_idx] >= 0)
  587. option = options[card_idx];
  588. else
  589. option = 0;
  590. /* Read the station address EEPROM before doing the reset.
  591.    Nominally his should even be done before accepting the device, but
  592.    then we wouldn't have a device name with which to report the error.
  593.    The size test is for 6 bit vs. 8 bit address serial EEPROMs.
  594. */
  595. {
  596. unsigned long iobase;
  597. int read_cmd, ee_size;
  598. u16 sum;
  599. int j;
  600. /* Use IO only to avoid postponed writes and satisfy EEPROM timing
  601.    requirements. */
  602. iobase = pci_resource_start(pdev, 1);
  603. if ((do_eeprom_cmd(iobase, EE_READ_CMD << 24, 27) & 0xffe0000)
  604. == 0xffe0000) {
  605. ee_size = 0x100;
  606. read_cmd = EE_READ_CMD << 24;
  607. } else {
  608. ee_size = 0x40;
  609. read_cmd = EE_READ_CMD << 22;
  610. }
  611. for (j = 0, i = 0, sum = 0; i < ee_size; i++) {
  612. u16 value = do_eeprom_cmd(iobase, read_cmd | (i << 16), 27);
  613. eeprom[i] = value;
  614. sum += value;
  615. if (i < 3) {
  616. dev->dev_addr[j++] = value;
  617. dev->dev_addr[j++] = value >> 8;
  618. }
  619. }
  620. if (sum != 0xBABA)
  621. printk(KERN_WARNING "%s: Invalid EEPROM checksum %#4.4x, "
  622.    "check settings before activating this device!n",
  623.    dev->name, sum);
  624. /* Don't  unregister_netdev(dev);  as the EEPro may actually be
  625.    usable, especially if the MAC address is set later.
  626.    On the other hand, it may be unusable if MDI data is corrupted. */
  627. }
  628. /* Reset the chip: stop Tx and Rx processes and clear counters.
  629.    This takes less than 10usec and will easily finish before the next
  630.    action. */
  631. outl(PortReset, ioaddr + SCBPort);
  632. inl(ioaddr + SCBPort);
  633. udelay(10);
  634. if (eeprom[3] & 0x0100)
  635. product = "OEM i82557/i82558 10/100 Ethernet";
  636. else
  637. product = pdev->name;
  638. printk(KERN_INFO "%s: %s, ", dev->name, product);
  639. for (i = 0; i < 5; i++)
  640. printk("%2.2X:", dev->dev_addr[i]);
  641. printk("%2.2X, ", dev->dev_addr[i]);
  642. #ifdef USE_IO
  643. printk("I/O at %#3lx, ", ioaddr);
  644. #endif
  645. printk("IRQ %d.n", pdev->irq);
  646. /* we must initialize base_addr early, for mdio_{read,write} */
  647. dev->base_addr = ioaddr;
  648. #if 1 || defined(kernel_bloat)
  649. /* OK, this is pure kernel bloat.  I don't like it when other drivers
  650.    waste non-pageable kernel space to emit similar messages, but I need
  651.    them for bug reports. */
  652. {
  653. const char *connectors[] = {" RJ45", " BNC", " AUI", " MII"};
  654. /* The self-test results must be paragraph aligned. */
  655. volatile s32 *self_test_results;
  656. int boguscnt = 16000; /* Timeout for set-test. */
  657. if ((eeprom[3] & 0x03) != 0x03)
  658. printk(KERN_INFO "  Receiver lock-up bug exists -- enabling"
  659.    " work-around.n");
  660. printk(KERN_INFO "  Board assembly %4.4x%2.2x-%3.3d, Physical"
  661.    " connectors present:",
  662.    eeprom[8], eeprom[9]>>8, eeprom[9] & 0xff);
  663. for (i = 0; i < 4; i++)
  664. if (eeprom[5] & (1<<i))
  665. printk(connectors[i]);
  666. printk("n"KERN_INFO"  Primary interface chip %s PHY #%d.n",
  667.    phys[(eeprom[6]>>8)&15], eeprom[6] & 0x1f);
  668. if (eeprom[7] & 0x0700)
  669. printk(KERN_INFO "    Secondary interface chip %s.n",
  670.    phys[(eeprom[7]>>8)&7]);
  671. if (((eeprom[6]>>8) & 0x3f) == DP83840
  672. ||  ((eeprom[6]>>8) & 0x3f) == DP83840A) {
  673. int mdi_reg23 = mdio_read(dev, eeprom[6] & 0x1f, 23) | 0x0422;
  674. if (congenb)
  675.   mdi_reg23 |= 0x0100;
  676. printk(KERN_INFO"  DP83840 specific setup, setting register 23 to %4.4x.n",
  677.    mdi_reg23);
  678. mdio_write(dev, eeprom[6] & 0x1f, 23, mdi_reg23);
  679. }
  680. if ((option >= 0) && (option & 0x70)) {
  681. printk(KERN_INFO "  Forcing %dMbs %s-duplex operation.n",
  682.    (option & 0x20 ? 100 : 10),
  683.    (option & 0x10 ? "full" : "half"));
  684. mdio_write(dev, eeprom[6] & 0x1f, MII_BMCR,
  685.    ((option & 0x20) ? 0x2000 : 0) |  /* 100mbps? */
  686.    ((option & 0x10) ? 0x0100 : 0)); /* Full duplex? */
  687. }
  688. /* Perform a system self-test. */
  689. self_test_results = (s32*) ((((long) tx_ring_space) + 15) & ~0xf);
  690. self_test_results[0] = 0;
  691. self_test_results[1] = -1;
  692. outl(tx_ring_dma | PortSelfTest, ioaddr + SCBPort);
  693. do {
  694. udelay(10);
  695. } while (self_test_results[1] == -1  &&  --boguscnt >= 0);
  696. if (boguscnt < 0) { /* Test optimized out. */
  697. printk(KERN_ERR "Self test failed, status %8.8x:n"
  698.    KERN_ERR " Failure to initialize the i82557.n"
  699.    KERN_ERR " Verify that the card is a bus-master"
  700.    " capable slot.n",
  701.    self_test_results[1]);
  702. } else
  703. printk(KERN_INFO "  General self-test: %s.n"
  704.    KERN_INFO "  Serial sub-system self-test: %s.n"
  705.    KERN_INFO "  Internal registers self-test: %s.n"
  706.    KERN_INFO "  ROM checksum self-test: %s (%#8.8x).n",
  707.    self_test_results[1] & 0x1000 ? "failed" : "passed",
  708.    self_test_results[1] & 0x0020 ? "failed" : "passed",
  709.    self_test_results[1] & 0x0008 ? "failed" : "passed",
  710.    self_test_results[1] & 0x0004 ? "failed" : "passed",
  711.    self_test_results[0]);
  712. }
  713. #endif  /* kernel_bloat */
  714. outl(PortReset, ioaddr + SCBPort);
  715. inl(ioaddr + SCBPort);
  716. udelay(10);
  717. /* Return the chip to its original power state. */
  718. pci_set_power_state(pdev, acpi_idle_state);
  719. pci_set_drvdata (pdev, dev);
  720. dev->irq = pdev->irq;
  721. sp = dev->priv;
  722. sp->pdev = pdev;
  723. sp->msg_enable = DEBUG;
  724. sp->acpi_pwr = acpi_idle_state;
  725. sp->tx_ring = tx_ring_space;
  726. sp->tx_ring_dma = tx_ring_dma;
  727. sp->lstats = (struct speedo_stats *)(sp->tx_ring + TX_RING_SIZE);
  728. sp->lstats_dma = TX_RING_ELEM_DMA(sp, TX_RING_SIZE);
  729. init_timer(&sp->timer); /* used in ioctl() */
  730. sp->mii_if.full_duplex = option >= 0 && (option & 0x10) ? 1 : 0;
  731. if (card_idx >= 0) {
  732. if (full_duplex[card_idx] >= 0)
  733. sp->mii_if.full_duplex = full_duplex[card_idx];
  734. }
  735. sp->default_port = option >= 0 ? (option & 0x0f) : 0;
  736. sp->phy[0] = eeprom[6];
  737. sp->phy[1] = eeprom[7];
  738. sp->mii_if.phy_id = eeprom[6] & 0x1f;
  739. sp->mii_if.phy_id_mask = 0x1f;
  740. sp->mii_if.reg_num_mask = 0x1f;
  741. sp->mii_if.dev = dev;
  742. sp->mii_if.mdio_read = mdio_read;
  743. sp->mii_if.mdio_write = mdio_write;
  744. sp->rx_bug = (eeprom[3] & 0x03) == 3 ? 0 : 1;
  745. if (((pdev->device > 0x1030 && (pdev->device < 0x103F))) 
  746.     || (pdev->device == 0x2449) || (pdev->device == 0x2459) 
  747.             || (pdev->device == 0x245D)) {
  748.      sp->chip_id = 1;
  749. }
  750. if (sp->rx_bug)
  751. printk(KERN_INFO "  Receiver lock-up workaround activated.n");
  752. /* The Speedo-specific entries in the device structure. */
  753. dev->open = &speedo_open;
  754. dev->hard_start_xmit = &speedo_start_xmit;
  755. netif_set_tx_timeout(dev, &speedo_tx_timeout, TX_TIMEOUT);
  756. dev->stop = &speedo_close;
  757. dev->get_stats = &speedo_get_stats;
  758. dev->set_multicast_list = &set_rx_mode;
  759. dev->do_ioctl = &speedo_ioctl;
  760. return 0;
  761. }
  762. static void do_slow_command(struct net_device *dev, int cmd)
  763. {
  764. long cmd_ioaddr = dev->base_addr + SCBCmd;
  765. int wait = 0;
  766. do
  767. if (inb(cmd_ioaddr) == 0) break;
  768. while(++wait <= 200);
  769. if (wait > 100)
  770. printk(KERN_ERR "Command %4.4x never accepted (%d polls)!n",
  771.        inb(cmd_ioaddr), wait);
  772. outb(cmd, cmd_ioaddr);
  773. for (wait = 0; wait <= 100; wait++)
  774. if (inb(cmd_ioaddr) == 0) return;
  775. for (; wait <= 20000; wait++)
  776. if (inb(cmd_ioaddr) == 0) return;
  777. else udelay(1);
  778. printk(KERN_ERR "Command %4.4x was not accepted after %d polls!"
  779.        "  Current status %8.8x.n",
  780.        cmd, wait, inl(dev->base_addr + SCBStatus));
  781. }
  782. /* Serial EEPROM section.
  783.    A "bit" grungy, but we work our way through bit-by-bit :->. */
  784. /*  EEPROM_Ctrl bits. */
  785. #define EE_SHIFT_CLK 0x01 /* EEPROM shift clock. */
  786. #define EE_CS 0x02 /* EEPROM chip select. */
  787. #define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */
  788. #define EE_DATA_READ 0x08 /* EEPROM chip data out. */
  789. #define EE_ENB (0x4800 | EE_CS)
  790. #define EE_WRITE_0 0x4802
  791. #define EE_WRITE_1 0x4806
  792. #define EE_OFFSET SCBeeprom
  793. /* The fixes for the code were kindly provided by Dragan Stancevic
  794.    <visitor@valinux.com> to strictly follow Intel specifications of EEPROM
  795.    access timing.
  796.    The publicly available sheet 64486302 (sec. 3.1) specifies 1us access
  797.    interval for serial EEPROM.  However, it looks like that there is an
  798.    additional requirement dictating larger udelay's in the code below.
  799.    2000/05/24  SAW */
  800. static int __devinit do_eeprom_cmd(long ioaddr, int cmd, int cmd_len)
  801. {
  802. unsigned retval = 0;
  803. long ee_addr = ioaddr + SCBeeprom;
  804. io_outw(EE_ENB, ee_addr); udelay(2);
  805. io_outw(EE_ENB | EE_SHIFT_CLK, ee_addr); udelay(2);
  806. /* Shift the command bits out. */
  807. do {
  808. short dataval = (cmd & (1 << cmd_len)) ? EE_WRITE_1 : EE_WRITE_0;
  809. io_outw(dataval, ee_addr); udelay(2);
  810. io_outw(dataval | EE_SHIFT_CLK, ee_addr); udelay(2);
  811. retval = (retval << 1) | ((io_inw(ee_addr) & EE_DATA_READ) ? 1 : 0);
  812. } while (--cmd_len >= 0);
  813. io_outw(EE_ENB, ee_addr); udelay(2);
  814. /* Terminate the EEPROM access. */
  815. io_outw(EE_ENB & ~EE_CS, ee_addr);
  816. return retval;
  817. }
  818. static int mdio_read(struct net_device *dev, int phy_id, int location)
  819. {
  820. long ioaddr = dev->base_addr;
  821. int val, boguscnt = 64*10; /* <64 usec. to complete, typ 27 ticks */
  822. outl(0x08000000 | (location<<16) | (phy_id<<21), ioaddr + SCBCtrlMDI);
  823. do {
  824. val = inl(ioaddr + SCBCtrlMDI);
  825. if (--boguscnt < 0) {
  826. printk(KERN_ERR " mdio_read() timed out with val = %8.8x.n", val);
  827. break;
  828. }
  829. } while (! (val & 0x10000000));
  830. return val & 0xffff;
  831. }
  832. static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
  833. {
  834. long ioaddr = dev->base_addr;
  835. int val, boguscnt = 64*10; /* <64 usec. to complete, typ 27 ticks */
  836. outl(0x04000000 | (location<<16) | (phy_id<<21) | value,
  837.  ioaddr + SCBCtrlMDI);
  838. do {
  839. val = inl(ioaddr + SCBCtrlMDI);
  840. if (--boguscnt < 0) {
  841. printk(KERN_ERR" mdio_write() timed out with val = %8.8x.n", val);
  842. break;
  843. }
  844. } while (! (val & 0x10000000));
  845. }
  846. static int
  847. speedo_open(struct net_device *dev)
  848. {
  849. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  850. long ioaddr = dev->base_addr;
  851. int retval;
  852. if (netif_msg_ifup(sp))
  853. printk(KERN_DEBUG "%s: speedo_open() irq %d.n", dev->name, dev->irq);
  854. pci_set_power_state(sp->pdev, 0);
  855. /* Set up the Tx queue early.. */
  856. sp->cur_tx = 0;
  857. sp->dirty_tx = 0;
  858. sp->last_cmd = 0;
  859. sp->tx_full = 0;
  860. spin_lock_init(&sp->lock);
  861. sp->in_interrupt = 0;
  862. /* .. we can safely take handler calls during init. */
  863. retval = request_irq(dev->irq, &speedo_interrupt, SA_SHIRQ, dev->name, dev);
  864. if (retval) {
  865. return retval;
  866. }
  867. dev->if_port = sp->default_port;
  868. #ifdef oh_no_you_dont_unless_you_honour_the_options_passed_in_to_us
  869. /* Retrigger negotiation to reset previous errors. */
  870. if ((sp->phy[0] & 0x8000) == 0) {
  871. int phy_addr = sp->phy[0] & 0x1f ;
  872. /* Use 0x3300 for restarting NWay, other values to force xcvr:
  873.    0x0000 10-HD
  874.    0x0100 10-FD
  875.    0x2000 100-HD
  876.    0x2100 100-FD
  877. */
  878. #ifdef honor_default_port
  879. mdio_write(dev, phy_addr, MII_BMCR, mii_ctrl[dev->default_port & 7]);
  880. #else
  881. mdio_write(dev, phy_addr, MII_BMCR, 0x3300);
  882. #endif
  883. }
  884. #endif
  885. speedo_init_rx_ring(dev);
  886. /* Fire up the hardware. */
  887. outw(SCBMaskAll, ioaddr + SCBCmd);
  888. speedo_resume(dev);
  889. netdevice_start(dev);
  890. netif_start_queue(dev);
  891. /* Setup the chip and configure the multicast list. */
  892. sp->mc_setup_head = NULL;
  893. sp->mc_setup_tail = NULL;
  894. sp->flow_ctrl = sp->partner = 0;
  895. sp->rx_mode = -1; /* Invalid -> always reset the mode. */
  896. set_rx_mode(dev);
  897. if ((sp->phy[0] & 0x8000) == 0)
  898. sp->mii_if.advertising = mdio_read(dev, sp->phy[0] & 0x1f, MII_ADVERTISE);
  899. mii_check_link(&sp->mii_if);
  900. if (netif_msg_ifup(sp)) {
  901. printk(KERN_DEBUG "%s: Done speedo_open(), status %8.8x.n",
  902.    dev->name, inw(ioaddr + SCBStatus));
  903. }
  904. /* Set the timer.  The timer serves a dual purpose:
  905.    1) to monitor the media interface (e.g. link beat) and perhaps switch
  906.    to an alternate media type
  907.    2) to monitor Rx activity, and restart the Rx process if the receiver
  908.    hangs. */
  909. sp->timer.expires = RUN_AT((24*HZ)/10);  /* 2.4 sec. */
  910. sp->timer.data = (unsigned long)dev;
  911. sp->timer.function = &speedo_timer; /* timer handler */
  912. add_timer(&sp->timer);
  913. /* No need to wait for the command unit to accept here. */
  914. if ((sp->phy[0] & 0x8000) == 0)
  915. mdio_read(dev, sp->phy[0] & 0x1f, MII_BMCR);
  916. return 0;
  917. }
  918. /* Start the chip hardware after a full reset. */
  919. static void speedo_resume(struct net_device *dev)
  920. {
  921. struct speedo_private *sp = dev->priv;
  922. long ioaddr = dev->base_addr;
  923. /* Start with a Tx threshold of 256 (0x..20.... 8 byte units). */
  924. sp->tx_threshold = 0x01208000;
  925. /* Set the segment registers to '0'. */
  926. if (wait_for_cmd_done(dev) != 0) {
  927. outl(PortPartialReset, ioaddr + SCBPort);
  928. udelay(10);
  929. }
  930.         outl(0, ioaddr + SCBPointer);
  931.         inl(ioaddr + SCBPointer); /* Flush to PCI. */
  932.         udelay(10); /* Bogus, but it avoids the bug. */
  933.         /* Note: these next two operations can take a while. */
  934.         do_slow_command(dev, RxAddrLoad);
  935.         do_slow_command(dev, CUCmdBase);
  936. /* Load the statistics block and rx ring addresses. */
  937. outl(sp->lstats_dma, ioaddr + SCBPointer);
  938. inl(ioaddr + SCBPointer); /* Flush to PCI */
  939. outb(CUStatsAddr, ioaddr + SCBCmd);
  940. sp->lstats->done_marker = 0;
  941. wait_for_cmd_done(dev);
  942. if (sp->rx_ringp[sp->cur_rx % RX_RING_SIZE] == NULL) {
  943. if (netif_msg_rx_err(sp))
  944. printk(KERN_DEBUG "%s: NULL cur_rx in speedo_resume().n",
  945. dev->name);
  946. } else {
  947. outl(sp->rx_ring_dma[sp->cur_rx % RX_RING_SIZE],
  948.  ioaddr + SCBPointer);
  949. inl(ioaddr + SCBPointer); /* Flush to PCI */
  950. }
  951. /* Note: RxStart should complete instantly. */
  952. do_slow_command(dev, RxStart);
  953. do_slow_command(dev, CUDumpStats);
  954. /* Fill the first command with our physical address. */
  955. {
  956. struct descriptor *ias_cmd;
  957. ias_cmd =
  958. (struct descriptor *)&sp->tx_ring[sp->cur_tx++ % TX_RING_SIZE];
  959. /* Avoid a bug(?!) here by marking the command already completed. */
  960. ias_cmd->cmd_status = cpu_to_le32((CmdSuspend | CmdIASetup) | 0xa000);
  961. ias_cmd->link =
  962. cpu_to_le32(TX_RING_ELEM_DMA(sp, sp->cur_tx % TX_RING_SIZE));
  963. memcpy(ias_cmd->params, dev->dev_addr, 6);
  964. if (sp->last_cmd)
  965. clear_suspend(sp->last_cmd);
  966. sp->last_cmd = ias_cmd;
  967. }
  968. /* Start the chip's Tx process and unmask interrupts. */
  969. outl(TX_RING_ELEM_DMA(sp, sp->dirty_tx % TX_RING_SIZE),
  970.  ioaddr + SCBPointer);
  971. /* We are not ACK-ing FCP and ER in the interrupt handler yet so they should
  972.    remain masked --Dragan */
  973. outw(CUStart | SCBMaskEarlyRx | SCBMaskFlowCtl, ioaddr + SCBCmd);
  974. }
  975. /*
  976.  * Sometimes the receiver stops making progress.  This routine knows how to
  977.  * get it going again, without losing packets or being otherwise nasty like
  978.  * a chip reset would be.  Previously the driver had a whole sequence
  979.  * of if RxSuspended, if it's no buffers do one thing, if it's no resources,
  980.  * do another, etc.  But those things don't really matter.  Separate logic
  981.  * in the ISR provides for allocating buffers--the other half of operation
  982.  * is just making sure the receiver is active.  speedo_rx_soft_reset does that.
  983.  * This problem with the old, more involved algorithm is shown up under
  984.  * ping floods on the order of 60K packets/second on a 100Mbps fdx network.
  985.  */
  986. static void
  987. speedo_rx_soft_reset(struct net_device *dev)
  988. {
  989. struct speedo_private *sp = dev->priv;
  990. struct RxFD *rfd;
  991. long ioaddr;
  992. ioaddr = dev->base_addr;
  993. if (wait_for_cmd_done(dev) != 0) {
  994. printk("%s: previous command stalledn", dev->name);
  995. return;
  996. }
  997. /*
  998. * Put the hardware into a known state.
  999. */
  1000. outb(RxAbort, ioaddr + SCBCmd);
  1001. rfd = sp->rx_ringp[sp->cur_rx % RX_RING_SIZE];
  1002. rfd->rx_buf_addr = 0xffffffff;
  1003. if (wait_for_cmd_done(dev) != 0) {
  1004. printk("%s: RxAbort command stalledn", dev->name);
  1005. return;
  1006. }
  1007. outl(sp->rx_ring_dma[sp->cur_rx % RX_RING_SIZE],
  1008. ioaddr + SCBPointer);
  1009. outb(RxStart, ioaddr + SCBCmd);
  1010. }
  1011. /* Media monitoring and control. */
  1012. static void speedo_timer(unsigned long data)
  1013. {
  1014. struct net_device *dev = (struct net_device *)data;
  1015. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  1016. long ioaddr = dev->base_addr;
  1017. int phy_num = sp->phy[0] & 0x1f;
  1018. /* We have MII and lost link beat. */
  1019. if ((sp->phy[0] & 0x8000) == 0) {
  1020. int partner = mdio_read(dev, phy_num, MII_LPA);
  1021. if (partner != sp->partner) {
  1022. int flow_ctrl = sp->mii_if.advertising & partner & 0x0400 ? 1 : 0;
  1023. if (netif_msg_link(sp)) {
  1024. printk(KERN_DEBUG "%s: Link status change.n", dev->name);
  1025. printk(KERN_DEBUG "%s: Old partner %x, new %x, adv %x.n",
  1026.    dev->name, sp->partner, partner, sp->mii_if.advertising);
  1027. }
  1028. sp->partner = partner;
  1029. if (flow_ctrl != sp->flow_ctrl) {
  1030. sp->flow_ctrl = flow_ctrl;
  1031. sp->rx_mode = -1; /* Trigger a reload. */
  1032. }
  1033. }
  1034. }
  1035. mii_check_link(&sp->mii_if);
  1036. if (netif_msg_timer(sp)) {
  1037. printk(KERN_DEBUG "%s: Media control tick, status %4.4x.n",
  1038.    dev->name, inw(ioaddr + SCBStatus));
  1039. }
  1040. if (sp->rx_mode < 0  ||
  1041. (sp->rx_bug  && jiffies - sp->last_rx_time > 2*HZ)) {
  1042. /* We haven't received a packet in a Long Time.  We might have been
  1043.    bitten by the receiver hang bug.  This can be cleared by sending
  1044.    a set multicast list command. */
  1045. if (netif_msg_timer(sp))
  1046. printk(KERN_DEBUG "%s: Sending a multicast list set command"
  1047.    " from a timer routine,"
  1048.    " m=%d, j=%ld, l=%ld.n",
  1049.    dev->name, sp->rx_mode, jiffies, sp->last_rx_time);
  1050. set_rx_mode(dev);
  1051. }
  1052. /* We must continue to monitor the media. */
  1053. sp->timer.expires = RUN_AT(2*HZ);  /* 2.0 sec. */
  1054. add_timer(&sp->timer);
  1055. #if defined(timer_exit)
  1056. timer_exit(&sp->timer);
  1057. #endif
  1058. }
  1059. static void speedo_show_state(struct net_device *dev)
  1060. {
  1061. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  1062. int i;
  1063. if (netif_msg_pktdata(sp)) {
  1064. printk(KERN_DEBUG "%s: Tx ring dump,  Tx queue %u / %u:n", 
  1065.     dev->name, sp->cur_tx, sp->dirty_tx);
  1066. for (i = 0; i < TX_RING_SIZE; i++)
  1067. printk(KERN_DEBUG "%s:  %c%c%2d %8.8x.n", dev->name,
  1068.     i == sp->dirty_tx % TX_RING_SIZE ? '*' : ' ',
  1069.     i == sp->cur_tx % TX_RING_SIZE ? '=' : ' ',
  1070.     i, sp->tx_ring[i].status);
  1071. printk(KERN_DEBUG "%s: Printing Rx ring"
  1072.     " (next to receive into %u, dirty index %u).n",
  1073.     dev->name, sp->cur_rx, sp->dirty_rx);
  1074. for (i = 0; i < RX_RING_SIZE; i++)
  1075. printk(KERN_DEBUG "%s: %c%c%c%2d %8.8x.n", dev->name,
  1076.     sp->rx_ringp[i] == sp->last_rxf ? 'l' : ' ',
  1077.     i == sp->dirty_rx % RX_RING_SIZE ? '*' : ' ',
  1078.     i == sp->cur_rx % RX_RING_SIZE ? '=' : ' ',
  1079.     i, (sp->rx_ringp[i] != NULL) ?
  1080.     (unsigned)sp->rx_ringp[i]->status : 0);
  1081. }
  1082. #if 0
  1083. {
  1084. long ioaddr = dev->base_addr;
  1085. int phy_num = sp->phy[0] & 0x1f;
  1086. for (i = 0; i < 16; i++) {
  1087. /* FIXME: what does it mean?  --SAW */
  1088. if (i == 6) i = 21;
  1089. printk(KERN_DEBUG "%s:  PHY index %d register %d is %4.4x.n",
  1090.    dev->name, phy_num, i, mdio_read(dev, phy_num, i));
  1091. }
  1092. }
  1093. #endif
  1094. }
  1095. /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
  1096. static void
  1097. speedo_init_rx_ring(struct net_device *dev)
  1098. {
  1099. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  1100. struct RxFD *rxf, *last_rxf = NULL;
  1101. dma_addr_t last_rxf_dma = 0 /* to shut up the compiler */;
  1102. int i;
  1103. sp->cur_rx = 0;
  1104. for (i = 0; i < RX_RING_SIZE; i++) {
  1105. struct sk_buff *skb;
  1106. skb = dev_alloc_skb(PKT_BUF_SZ + sizeof(struct RxFD));
  1107. /* XXX: do we really want to call this before the NULL check? --hch */
  1108. rx_align(skb); /* Align IP on 16 byte boundary */
  1109. sp->rx_skbuff[i] = skb;
  1110. if (skb == NULL)
  1111. break; /* OK.  Just initially short of Rx bufs. */
  1112. skb->dev = dev; /* Mark as being used by this device. */
  1113. rxf = (struct RxFD *)skb->tail;
  1114. sp->rx_ringp[i] = rxf;
  1115. sp->rx_ring_dma[i] =
  1116. pci_map_single(sp->pdev, rxf,
  1117. PKT_BUF_SZ + sizeof(struct RxFD), PCI_DMA_BIDIRECTIONAL);
  1118. skb_reserve(skb, sizeof(struct RxFD));
  1119. if (last_rxf) {
  1120. last_rxf->link = cpu_to_le32(sp->rx_ring_dma[i]);
  1121. pci_dma_sync_single(sp->pdev, last_rxf_dma,
  1122. sizeof(struct RxFD), PCI_DMA_TODEVICE);
  1123. }
  1124. last_rxf = rxf;
  1125. last_rxf_dma = sp->rx_ring_dma[i];
  1126. rxf->status = cpu_to_le32(0x00000001); /* '1' is flag value only. */
  1127. rxf->link = 0; /* None yet. */
  1128. /* This field unused by i82557. */
  1129. rxf->rx_buf_addr = 0xffffffff;
  1130. rxf->count = cpu_to_le32(PKT_BUF_SZ << 16);
  1131. pci_dma_sync_single(sp->pdev, sp->rx_ring_dma[i],
  1132. sizeof(struct RxFD), PCI_DMA_TODEVICE);
  1133. }
  1134. sp->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
  1135. /* Mark the last entry as end-of-list. */
  1136. last_rxf->status = cpu_to_le32(0xC0000002); /* '2' is flag value only. */
  1137. pci_dma_sync_single(sp->pdev, sp->rx_ring_dma[RX_RING_SIZE-1],
  1138. sizeof(struct RxFD), PCI_DMA_TODEVICE);
  1139. sp->last_rxf = last_rxf;
  1140. sp->last_rxf_dma = last_rxf_dma;
  1141. }
  1142. static void speedo_purge_tx(struct net_device *dev)
  1143. {
  1144. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  1145. int entry;
  1146. while ((int)(sp->cur_tx - sp->dirty_tx) > 0) {
  1147. entry = sp->dirty_tx % TX_RING_SIZE;
  1148. if (sp->tx_skbuff[entry]) {
  1149. sp->stats.tx_errors++;
  1150. pci_unmap_single(sp->pdev,
  1151. le32_to_cpu(sp->tx_ring[entry].tx_buf_addr0),
  1152. sp->tx_skbuff[entry]->len, PCI_DMA_TODEVICE);
  1153. dev_kfree_skb_irq(sp->tx_skbuff[entry]);
  1154. sp->tx_skbuff[entry] = 0;
  1155. }
  1156. sp->dirty_tx++;
  1157. }
  1158. while (sp->mc_setup_head != NULL) {
  1159. struct speedo_mc_block *t;
  1160. if (netif_msg_tx_err(sp))
  1161. printk(KERN_DEBUG "%s: freeing mc frame.n", dev->name);
  1162. pci_unmap_single(sp->pdev, sp->mc_setup_head->frame_dma,
  1163. sp->mc_setup_head->len, PCI_DMA_TODEVICE);
  1164. t = sp->mc_setup_head->next;
  1165. kfree(sp->mc_setup_head);
  1166. sp->mc_setup_head = t;
  1167. }
  1168. sp->mc_setup_tail = NULL;
  1169. sp->tx_full = 0;
  1170. netif_wake_queue(dev);
  1171. }
  1172. static void reset_mii(struct net_device *dev)
  1173. {
  1174. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  1175. /* Reset the MII transceiver, suggested by Fred Young @ scalable.com. */
  1176. if ((sp->phy[0] & 0x8000) == 0) {
  1177. int phy_addr = sp->phy[0] & 0x1f;
  1178. int advertising = mdio_read(dev, phy_addr, MII_ADVERTISE);
  1179. int mii_bmcr = mdio_read(dev, phy_addr, MII_BMCR);
  1180. mdio_write(dev, phy_addr, MII_BMCR, 0x0400);
  1181. mdio_write(dev, phy_addr, MII_BMSR, 0x0000);
  1182. mdio_write(dev, phy_addr, MII_ADVERTISE, 0x0000);
  1183. mdio_write(dev, phy_addr, MII_BMCR, 0x8000);
  1184. #ifdef honor_default_port
  1185. mdio_write(dev, phy_addr, MII_BMCR, mii_ctrl[dev->default_port & 7]);
  1186. #else
  1187. mdio_read(dev, phy_addr, MII_BMCR);
  1188. mdio_write(dev, phy_addr, MII_BMCR, mii_bmcr);
  1189. mdio_write(dev, phy_addr, MII_ADVERTISE, advertising);
  1190. #endif
  1191. }
  1192. }
  1193. static void speedo_tx_timeout(struct net_device *dev)
  1194. {
  1195. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  1196. long ioaddr = dev->base_addr;
  1197. int status = inw(ioaddr + SCBStatus);
  1198. unsigned long flags;
  1199. if (netif_msg_tx_err(sp)) {
  1200. printk(KERN_WARNING "%s: Transmit timed out: status %4.4x "
  1201.    " %4.4x at %d/%d command %8.8x.n",
  1202.    dev->name, status, inw(ioaddr + SCBCmd),
  1203.    sp->dirty_tx, sp->cur_tx,
  1204.    sp->tx_ring[sp->dirty_tx % TX_RING_SIZE].status);
  1205. }
  1206. speedo_show_state(dev);
  1207. #if 0
  1208. if ((status & 0x00C0) != 0x0080
  1209. &&  (status & 0x003C) == 0x0010) {
  1210. /* Only the command unit has stopped. */
  1211. printk(KERN_WARNING "%s: Trying to restart the transmitter...n",
  1212.    dev->name);
  1213. outl(TX_RING_ELEM_DMA(sp, dirty_tx % TX_RING_SIZE]),
  1214.  ioaddr + SCBPointer);
  1215. outw(CUStart, ioaddr + SCBCmd);
  1216. reset_mii(dev);
  1217. } else {
  1218. #else
  1219. {
  1220. #endif
  1221. del_timer_sync(&sp->timer);
  1222. /* Reset the Tx and Rx units. */
  1223. outl(PortReset, ioaddr + SCBPort);
  1224. /* We may get spurious interrupts here.  But I don't think that they
  1225.    may do much harm.  1999/12/09 SAW */
  1226. udelay(10);
  1227. /* Disable interrupts. */
  1228. outw(SCBMaskAll, ioaddr + SCBCmd);
  1229. synchronize_irq();
  1230. speedo_tx_buffer_gc(dev);
  1231. /* Free as much as possible.
  1232.    It helps to recover from a hang because of out-of-memory.
  1233.    It also simplifies speedo_resume() in case TX ring is full or
  1234.    close-to-be full. */
  1235. speedo_purge_tx(dev);
  1236. speedo_refill_rx_buffers(dev, 1);
  1237. spin_lock_irqsave(&sp->lock, flags);
  1238. speedo_resume(dev);
  1239. sp->rx_mode = -1;
  1240. dev->trans_start = jiffies;
  1241. spin_unlock_irqrestore(&sp->lock, flags);
  1242. set_rx_mode(dev); /* it takes the spinlock itself --SAW */
  1243. /* Reset MII transceiver.  Do it before starting the timer to serialize
  1244.    mdio_xxx operations.  Yes, it's a paranoya :-)  2000/05/09 SAW */
  1245. reset_mii(dev);
  1246. sp->timer.expires = RUN_AT(2*HZ);
  1247. add_timer(&sp->timer);
  1248. }
  1249. return;
  1250. }
  1251. static int
  1252. speedo_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1253. {
  1254. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  1255. long ioaddr = dev->base_addr;
  1256. int entry;
  1257. /* Prevent interrupts from changing the Tx ring from underneath us. */
  1258. unsigned long flags;
  1259. spin_lock_irqsave(&sp->lock, flags);
  1260. /* Check if there are enough space. */
  1261. if ((int)(sp->cur_tx - sp->dirty_tx) >= TX_QUEUE_LIMIT) {
  1262. printk(KERN_ERR "%s: incorrect tbusy state, fixed.n", dev->name);
  1263. netif_stop_queue(dev);
  1264. sp->tx_full = 1;
  1265. spin_unlock_irqrestore(&sp->lock, flags);
  1266. return 1;
  1267. }
  1268. /* Calculate the Tx descriptor entry. */
  1269. entry = sp->cur_tx++ % TX_RING_SIZE;
  1270. sp->tx_skbuff[entry] = skb;
  1271. sp->tx_ring[entry].status =
  1272. cpu_to_le32(CmdSuspend | CmdTx | CmdTxFlex);
  1273. if (!(entry & ((TX_RING_SIZE>>2)-1)))
  1274. sp->tx_ring[entry].status |= cpu_to_le32(CmdIntr);
  1275. sp->tx_ring[entry].link =
  1276. cpu_to_le32(TX_RING_ELEM_DMA(sp, sp->cur_tx % TX_RING_SIZE));
  1277. sp->tx_ring[entry].tx_desc_addr =
  1278. cpu_to_le32(TX_RING_ELEM_DMA(sp, entry) + TX_DESCR_BUF_OFFSET);
  1279. /* The data region is always in one buffer descriptor. */
  1280. sp->tx_ring[entry].count = cpu_to_le32(sp->tx_threshold);
  1281. sp->tx_ring[entry].tx_buf_addr0 =
  1282. cpu_to_le32(pci_map_single(sp->pdev, skb->data,
  1283.    skb->len, PCI_DMA_TODEVICE));
  1284. sp->tx_ring[entry].tx_buf_size0 = cpu_to_le32(skb->len);
  1285. /* workaround for hardware bug on 10 mbit half duplex */
  1286. if ((sp->partner == 0) && (sp->chip_id == 1)) {
  1287. wait_for_cmd_done(dev);
  1288. outb(0 , ioaddr + SCBCmd);
  1289. udelay(1);
  1290. }
  1291. /* Trigger the command unit resume. */
  1292. wait_for_cmd_done(dev);
  1293. clear_suspend(sp->last_cmd);
  1294. /* We want the time window between clearing suspend flag on the previous
  1295.    command and resuming CU to be as small as possible.
  1296.    Interrupts in between are very undesired.  --SAW */
  1297. outb(CUResume, ioaddr + SCBCmd);
  1298. sp->last_cmd = (struct descriptor *)&sp->tx_ring[entry];
  1299. /* Leave room for set_rx_mode(). If there is no more space than reserved
  1300.    for multicast filter mark the ring as full. */
  1301. if ((int)(sp->cur_tx - sp->dirty_tx) >= TX_QUEUE_LIMIT) {
  1302. netif_stop_queue(dev);
  1303. sp->tx_full = 1;
  1304. }
  1305. spin_unlock_irqrestore(&sp->lock, flags);
  1306. dev->trans_start = jiffies;
  1307. return 0;
  1308. }
  1309. static void speedo_tx_buffer_gc(struct net_device *dev)
  1310. {
  1311. unsigned int dirty_tx;
  1312. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  1313. dirty_tx = sp->dirty_tx;
  1314. while ((int)(sp->cur_tx - dirty_tx) > 0) {
  1315. int entry = dirty_tx % TX_RING_SIZE;
  1316. int status = le32_to_cpu(sp->tx_ring[entry].status);
  1317. if (netif_msg_tx_done(sp))
  1318. printk(KERN_DEBUG " scavenge candidate %d status %4.4x.n",
  1319.    entry, status);
  1320. if ((status & StatusComplete) == 0)
  1321. break; /* It still hasn't been processed. */
  1322. if (status & TxUnderrun)
  1323. if (sp->tx_threshold < 0x01e08000) {
  1324. if (netif_msg_tx_err(sp))
  1325. printk(KERN_DEBUG "%s: TX underrun, threshold adjusted.n",
  1326.    dev->name);
  1327. sp->tx_threshold += 0x00040000;
  1328. }
  1329. /* Free the original skb. */
  1330. if (sp->tx_skbuff[entry]) {
  1331. sp->stats.tx_packets++; /* Count only user packets. */
  1332. sp->stats.tx_bytes += sp->tx_skbuff[entry]->len;
  1333. pci_unmap_single(sp->pdev,
  1334. le32_to_cpu(sp->tx_ring[entry].tx_buf_addr0),
  1335. sp->tx_skbuff[entry]->len, PCI_DMA_TODEVICE);
  1336. dev_kfree_skb_irq(sp->tx_skbuff[entry]);
  1337. sp->tx_skbuff[entry] = 0;
  1338. }
  1339. dirty_tx++;
  1340. }
  1341. if (netif_msg_tx_err(sp) && (int)(sp->cur_tx - dirty_tx) > TX_RING_SIZE) {
  1342. printk(KERN_ERR "out-of-sync dirty pointer, %d vs. %d,"
  1343.    " full=%d.n",
  1344.    dirty_tx, sp->cur_tx, sp->tx_full);
  1345. dirty_tx += TX_RING_SIZE;
  1346. }
  1347. while (sp->mc_setup_head != NULL
  1348.    && (int)(dirty_tx - sp->mc_setup_head->tx - 1) > 0) {
  1349. struct speedo_mc_block *t;
  1350. if (netif_msg_tx_err(sp))
  1351. printk(KERN_DEBUG "%s: freeing mc frame.n", dev->name);
  1352. pci_unmap_single(sp->pdev, sp->mc_setup_head->frame_dma,
  1353. sp->mc_setup_head->len, PCI_DMA_TODEVICE);
  1354. t = sp->mc_setup_head->next;
  1355. kfree(sp->mc_setup_head);
  1356. sp->mc_setup_head = t;
  1357. }
  1358. if (sp->mc_setup_head == NULL)
  1359. sp->mc_setup_tail = NULL;
  1360. sp->dirty_tx = dirty_tx;
  1361. }
  1362. /* The interrupt handler does all of the Rx thread work and cleans up
  1363.    after the Tx thread. */
  1364. static void speedo_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
  1365. {
  1366. struct net_device *dev = (struct net_device *)dev_instance;
  1367. struct speedo_private *sp;
  1368. long ioaddr, boguscnt = max_interrupt_work;
  1369. unsigned short status;
  1370. ioaddr = dev->base_addr;
  1371. sp = (struct speedo_private *)dev->priv;
  1372. #ifndef final_version
  1373. /* A lock to prevent simultaneous entry on SMP machines. */
  1374. if (test_and_set_bit(0, (void*)&sp->in_interrupt)) {
  1375. printk(KERN_ERR"%s: SMP simultaneous entry of an interrupt handler.n",
  1376.    dev->name);
  1377. sp->in_interrupt = 0; /* Avoid halting machine. */
  1378. return;
  1379. }
  1380. #endif
  1381. do {
  1382. status = inw(ioaddr + SCBStatus);
  1383. /* Acknowledge all of the current interrupt sources ASAP. */
  1384. /* Will change from 0xfc00 to 0xff00 when we start handling
  1385.    FCP and ER interrupts --Dragan */
  1386. outw(status & 0xfc00, ioaddr + SCBStatus);
  1387. if (netif_msg_intr(sp))
  1388. printk(KERN_DEBUG "%s: interrupt  status=%#4.4x.n",
  1389.    dev->name, status);
  1390. if ((status & 0xfc00) == 0)
  1391. break;
  1392. if ((status & 0x5000) || /* Packet received, or Rx error. */
  1393. (sp->rx_ring_state&(RrNoMem|RrPostponed)) == RrPostponed)
  1394. /* Need to gather the postponed packet. */
  1395. speedo_rx(dev);
  1396. /* Always check if all rx buffers are allocated.  --SAW */
  1397. speedo_refill_rx_buffers(dev, 0);
  1398. spin_lock(&sp->lock);
  1399. /*
  1400.  * The chip may have suspended reception for various reasons.
  1401.  * Check for that, and re-prime it should this be the case.
  1402.  */
  1403. switch ((status >> 2) & 0xf) {
  1404. case 0: /* Idle */
  1405. break;
  1406. case 1: /* Suspended */
  1407. case 2: /* No resources (RxFDs) */
  1408. case 9: /* Suspended with no more RBDs */
  1409. case 10: /* No resources due to no RBDs */
  1410. case 12: /* Ready with no RBDs */
  1411. speedo_rx_soft_reset(dev);
  1412. break;
  1413. case 3:  case 5:  case 6:  case 7:  case 8:
  1414. case 11:  case 13:  case 14:  case 15:
  1415. /* these are all reserved values */
  1416. break;
  1417. }
  1418. /* User interrupt, Command/Tx unit interrupt or CU not active. */
  1419. if (status & 0xA400) {
  1420. speedo_tx_buffer_gc(dev);
  1421. if (sp->tx_full
  1422. && (int)(sp->cur_tx - sp->dirty_tx) < TX_QUEUE_UNFULL) {
  1423. /* The ring is no longer full. */
  1424. sp->tx_full = 0;
  1425. netif_wake_queue(dev); /* Attention: under a spinlock.  --SAW */
  1426. }
  1427. }
  1428. spin_unlock(&sp->lock);
  1429. if (--boguscnt < 0) {
  1430. printk(KERN_ERR "%s: Too much work at interrupt, status=0x%4.4x.n",
  1431.    dev->name, status);
  1432. /* Clear all interrupt sources. */
  1433. /* Will change from 0xfc00 to 0xff00 when we start handling
  1434.    FCP and ER interrupts --Dragan */
  1435. outw(0xfc00, ioaddr + SCBStatus);
  1436. break;
  1437. }
  1438. } while (1);
  1439. if (netif_msg_intr(sp))
  1440. printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.n",
  1441.    dev->name, inw(ioaddr + SCBStatus));
  1442. clear_bit(0, (void*)&sp->in_interrupt);
  1443. return;
  1444. }
  1445. static inline struct RxFD *speedo_rx_alloc(struct net_device *dev, int entry)
  1446. {
  1447. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  1448. struct RxFD *rxf;
  1449. struct sk_buff *skb;
  1450. /* Get a fresh skbuff to replace the consumed one. */
  1451. skb = dev_alloc_skb(PKT_BUF_SZ + sizeof(struct RxFD));
  1452. /* XXX: do we really want to call this before the NULL check? --hch */
  1453. rx_align(skb); /* Align IP on 16 byte boundary */
  1454. sp->rx_skbuff[entry] = skb;
  1455. if (skb == NULL) {
  1456. sp->rx_ringp[entry] = NULL;
  1457. return NULL;
  1458. }
  1459. rxf = sp->rx_ringp[entry] = (struct RxFD *)skb->tail;
  1460. sp->rx_ring_dma[entry] =
  1461. pci_map_single(sp->pdev, rxf,
  1462.    PKT_BUF_SZ + sizeof(struct RxFD), PCI_DMA_FROMDEVICE);
  1463. skb->dev = dev;
  1464. skb_reserve(skb, sizeof(struct RxFD));
  1465. rxf->rx_buf_addr = 0xffffffff;
  1466. pci_dma_sync_single(sp->pdev, sp->rx_ring_dma[entry],
  1467. sizeof(struct RxFD), PCI_DMA_TODEVICE);
  1468. return rxf;
  1469. }
  1470. static inline void speedo_rx_link(struct net_device *dev, int entry,
  1471.   struct RxFD *rxf, dma_addr_t rxf_dma)
  1472. {
  1473. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  1474. rxf->status = cpu_to_le32(0xC0000001);  /* '1' for driver use only. */
  1475. rxf->link = 0; /* None yet. */
  1476. rxf->count = cpu_to_le32(PKT_BUF_SZ << 16);
  1477. sp->last_rxf->link = cpu_to_le32(rxf_dma);
  1478. sp->last_rxf->status &= cpu_to_le32(~0xC0000000);
  1479. pci_dma_sync_single(sp->pdev, sp->last_rxf_dma,
  1480. sizeof(struct RxFD), PCI_DMA_TODEVICE);
  1481. sp->last_rxf = rxf;
  1482. sp->last_rxf_dma = rxf_dma;
  1483. }
  1484. static int speedo_refill_rx_buf(struct net_device *dev, int force)
  1485. {
  1486. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  1487. int entry;
  1488. struct RxFD *rxf;
  1489. entry = sp->dirty_rx % RX_RING_SIZE;
  1490. if (sp->rx_skbuff[entry] == NULL) {
  1491. rxf = speedo_rx_alloc(dev, entry);
  1492. if (rxf == NULL) {
  1493. unsigned int forw;
  1494. int forw_entry;
  1495. if (netif_msg_rx_err(sp) || !(sp->rx_ring_state & RrOOMReported)) {
  1496. printk(KERN_WARNING "%s: can't fill rx buffer (force %d)!n",
  1497. dev->name, force);
  1498. sp->rx_ring_state |= RrOOMReported;
  1499. }
  1500. speedo_show_state(dev);
  1501. if (!force)
  1502. return -1; /* Better luck next time!  */
  1503. /* Borrow an skb from one of next entries. */
  1504. for (forw = sp->dirty_rx + 1; forw != sp->cur_rx; forw++)
  1505. if (sp->rx_skbuff[forw % RX_RING_SIZE] != NULL)
  1506. break;
  1507. if (forw == sp->cur_rx)
  1508. return -1;
  1509. forw_entry = forw % RX_RING_SIZE;
  1510. sp->rx_skbuff[entry] = sp->rx_skbuff[forw_entry];
  1511. sp->rx_skbuff[forw_entry] = NULL;
  1512. rxf = sp->rx_ringp[forw_entry];
  1513. sp->rx_ringp[forw_entry] = NULL;
  1514. sp->rx_ringp[entry] = rxf;
  1515. }
  1516. } else {
  1517. rxf = sp->rx_ringp[entry];
  1518. }
  1519. speedo_rx_link(dev, entry, rxf, sp->rx_ring_dma[entry]);
  1520. sp->dirty_rx++;
  1521. sp->rx_ring_state &= ~(RrNoMem|RrOOMReported); /* Mark the progress. */
  1522. return 0;
  1523. }
  1524. static void speedo_refill_rx_buffers(struct net_device *dev, int force)
  1525. {
  1526. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  1527. /* Refill the RX ring. */
  1528. while ((int)(sp->cur_rx - sp->dirty_rx) > 0 &&
  1529. speedo_refill_rx_buf(dev, force) != -1);
  1530. }
  1531. static int
  1532. speedo_rx(struct net_device *dev)
  1533. {
  1534. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  1535. int entry = sp->cur_rx % RX_RING_SIZE;
  1536. int rx_work_limit = sp->dirty_rx + RX_RING_SIZE - sp->cur_rx;
  1537. int alloc_ok = 1;
  1538. int npkts = 0;
  1539. if (netif_msg_intr(sp))
  1540. printk(KERN_DEBUG " In speedo_rx().n");
  1541. /* If we own the next entry, it's a new packet. Send it up. */
  1542. while (sp->rx_ringp[entry] != NULL) {
  1543. int status;
  1544. int pkt_len;
  1545. pci_dma_sync_single(sp->pdev, sp->rx_ring_dma[entry],
  1546. sizeof(struct RxFD), PCI_DMA_FROMDEVICE);
  1547. status = le32_to_cpu(sp->rx_ringp[entry]->status);
  1548. pkt_len = le32_to_cpu(sp->rx_ringp[entry]->count) & 0x3fff;
  1549. if (!(status & RxComplete))
  1550. break;
  1551. if (--rx_work_limit < 0)
  1552. break;
  1553. /* Check for a rare out-of-memory case: the current buffer is
  1554.    the last buffer allocated in the RX ring.  --SAW */
  1555. if (sp->last_rxf == sp->rx_ringp[entry]) {
  1556. /* Postpone the packet.  It'll be reaped at an interrupt when this
  1557.    packet is no longer the last packet in the ring. */
  1558. if (netif_msg_rx_err(sp))
  1559. printk(KERN_DEBUG "%s: RX packet postponed!n",
  1560.    dev->name);
  1561. sp->rx_ring_state |= RrPostponed;
  1562. break;
  1563. }
  1564. if (netif_msg_rx_status(sp))
  1565. printk(KERN_DEBUG "  speedo_rx() status %8.8x len %d.n", status,
  1566.    pkt_len);
  1567. if ((status & (RxErrTooBig|RxOK|0x0f90)) != RxOK) {
  1568. if (status & RxErrTooBig)
  1569. printk(KERN_ERR "%s: Ethernet frame overran the Rx buffer, "
  1570.    "status %8.8x!n", dev->name, status);
  1571. else if (! (status & RxOK)) {
  1572. /* There was a fatal error.  This *should* be impossible. */
  1573. sp->stats.rx_errors++;
  1574. printk(KERN_ERR "%s: Anomalous event in speedo_rx(), "
  1575.    "status %8.8x.n",
  1576.    dev->name, status);
  1577. }
  1578. } else {
  1579. struct sk_buff *skb;
  1580. /* Check if the packet is long enough to just accept without
  1581.    copying to a properly sized skbuff. */
  1582. if (pkt_len < rx_copybreak
  1583. && (skb = dev_alloc_skb(pkt_len + 2)) != 0) {
  1584. skb->dev = dev;
  1585. skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
  1586. /* 'skb_put()' points to the start of sk_buff data area. */
  1587. pci_dma_sync_single(sp->pdev, sp->rx_ring_dma[entry],
  1588. sizeof(struct RxFD) + pkt_len, PCI_DMA_FROMDEVICE);
  1589. #if 1 || USE_IP_CSUM
  1590. /* Packet is in one chunk -- we can copy + cksum. */
  1591. eth_copy_and_sum(skb, sp->rx_skbuff[entry]->tail, pkt_len, 0);
  1592. skb_put(skb, pkt_len);
  1593. #else
  1594. memcpy(skb_put(skb, pkt_len), sp->rx_skbuff[entry]->tail,
  1595.    pkt_len);
  1596. #endif
  1597. npkts++;
  1598. } else {
  1599. /* Pass up the already-filled skbuff. */
  1600. skb = sp->rx_skbuff[entry];
  1601. if (skb == NULL) {
  1602. printk(KERN_ERR "%s: Inconsistent Rx descriptor chain.n",
  1603.    dev->name);
  1604. break;
  1605. }
  1606. sp->rx_skbuff[entry] = NULL;
  1607. skb_put(skb, pkt_len);
  1608. npkts++;
  1609. sp->rx_ringp[entry] = NULL;
  1610. pci_unmap_single(sp->pdev, sp->rx_ring_dma[entry],
  1611. PKT_BUF_SZ + sizeof(struct RxFD), PCI_DMA_FROMDEVICE);
  1612. }
  1613. skb->protocol = eth_type_trans(skb, dev);
  1614. netif_rx(skb);
  1615. sp->stats.rx_packets++;
  1616. sp->stats.rx_bytes += pkt_len;
  1617. }
  1618. entry = (++sp->cur_rx) % RX_RING_SIZE;
  1619. sp->rx_ring_state &= ~RrPostponed;
  1620. /* Refill the recently taken buffers.
  1621.    Do it one-by-one to handle traffic bursts better. */
  1622. if (alloc_ok && speedo_refill_rx_buf(dev, 0) == -1)
  1623. alloc_ok = 0;
  1624. }
  1625. /* Try hard to refill the recently taken buffers. */
  1626. speedo_refill_rx_buffers(dev, 1);
  1627. if (npkts)
  1628. sp->last_rx_time = jiffies;
  1629. return 0;
  1630. }
  1631. static int
  1632. speedo_close(struct net_device *dev)
  1633. {
  1634. long ioaddr = dev->base_addr;
  1635. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  1636. int i;
  1637. netdevice_stop(dev);
  1638. netif_stop_queue(dev);
  1639. if (netif_msg_ifdown(sp))
  1640. printk(KERN_DEBUG "%s: Shutting down ethercard, status was %4.4x.n",
  1641.    dev->name, inw(ioaddr + SCBStatus));
  1642. /* Shut off the media monitoring timer. */
  1643. del_timer_sync(&sp->timer);
  1644. outw(SCBMaskAll, ioaddr + SCBCmd);
  1645. /* Shutting down the chip nicely fails to disable flow control. So.. */
  1646. outl(PortPartialReset, ioaddr + SCBPort);
  1647. inl(ioaddr + SCBPort); /* flush posted write */
  1648. /*
  1649.  * The chip requires a 10 microsecond quiet period.  Wait here!
  1650.  */
  1651. udelay(10);
  1652. free_irq(dev->irq, dev);
  1653. speedo_show_state(dev);
  1654.     /* Free all the skbuffs in the Rx and Tx queues. */
  1655. for (i = 0; i < RX_RING_SIZE; i++) {
  1656. struct sk_buff *skb = sp->rx_skbuff[i];
  1657. sp->rx_skbuff[i] = 0;
  1658. /* Clear the Rx descriptors. */
  1659. if (skb) {
  1660. pci_unmap_single(sp->pdev,
  1661.  sp->rx_ring_dma[i],
  1662.  PKT_BUF_SZ + sizeof(struct RxFD), PCI_DMA_FROMDEVICE);
  1663. dev_kfree_skb(skb);
  1664. }
  1665. }
  1666. for (i = 0; i < TX_RING_SIZE; i++) {
  1667. struct sk_buff *skb = sp->tx_skbuff[i];
  1668. sp->tx_skbuff[i] = 0;
  1669. /* Clear the Tx descriptors. */
  1670. if (skb) {
  1671. pci_unmap_single(sp->pdev,
  1672.  le32_to_cpu(sp->tx_ring[i].tx_buf_addr0),
  1673.  skb->len, PCI_DMA_TODEVICE);
  1674. dev_kfree_skb(skb);
  1675. }
  1676. }
  1677. /* Free multicast setting blocks. */
  1678. for (i = 0; sp->mc_setup_head != NULL; i++) {
  1679. struct speedo_mc_block *t;
  1680. t = sp->mc_setup_head->next;
  1681. kfree(sp->mc_setup_head);
  1682. sp->mc_setup_head = t;
  1683. }
  1684. sp->mc_setup_tail = NULL;
  1685. if (netif_msg_ifdown(sp))
  1686. printk(KERN_DEBUG "%s: %d multicast blocks dropped.n", dev->name, i);
  1687. pci_set_power_state(sp->pdev, 2);
  1688. return 0;
  1689. }
  1690. /* The Speedo-3 has an especially awkward and unusable method of getting
  1691.    statistics out of the chip.  It takes an unpredictable length of time
  1692.    for the dump-stats command to complete.  To avoid a busy-wait loop we
  1693.    update the stats with the previous dump results, and then trigger a
  1694.    new dump.
  1695.    Oh, and incoming frames are dropped while executing dump-stats!
  1696.    */
  1697. static struct net_device_stats *
  1698. speedo_get_stats(struct net_device *dev)
  1699. {
  1700. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  1701. long ioaddr = dev->base_addr;
  1702. /* Update only if the previous dump finished. */
  1703. if (sp->lstats->done_marker == le32_to_cpu(0xA007)) {
  1704. sp->stats.tx_aborted_errors += le32_to_cpu(sp->lstats->tx_coll16_errs);
  1705. sp->stats.tx_window_errors += le32_to_cpu(sp->lstats->tx_late_colls);
  1706. sp->stats.tx_fifo_errors += le32_to_cpu(sp->lstats->tx_underruns);
  1707. sp->stats.tx_fifo_errors += le32_to_cpu(sp->lstats->tx_lost_carrier);
  1708. /*sp->stats.tx_deferred += le32_to_cpu(sp->lstats->tx_deferred);*/
  1709. sp->stats.collisions += le32_to_cpu(sp->lstats->tx_total_colls);
  1710. sp->stats.rx_crc_errors += le32_to_cpu(sp->lstats->rx_crc_errs);
  1711. sp->stats.rx_frame_errors += le32_to_cpu(sp->lstats->rx_align_errs);
  1712. sp->stats.rx_over_errors += le32_to_cpu(sp->lstats->rx_resource_errs);
  1713. sp->stats.rx_fifo_errors += le32_to_cpu(sp->lstats->rx_overrun_errs);
  1714. sp->stats.rx_length_errors += le32_to_cpu(sp->lstats->rx_runt_errs);
  1715. sp->lstats->done_marker = 0x0000;
  1716. if (netif_running(dev)) {
  1717. unsigned long flags;
  1718. /* Take a spinlock to make wait_for_cmd_done and sending the
  1719.    command atomic.  --SAW */
  1720. spin_lock_irqsave(&sp->lock, flags);
  1721. wait_for_cmd_done(dev);
  1722. outb(CUDumpStats, ioaddr + SCBCmd);
  1723. spin_unlock_irqrestore(&sp->lock, flags);
  1724. }
  1725. }
  1726. return &sp->stats;
  1727. }
  1728. static int netdev_ethtool_ioctl(struct net_device *dev, void *useraddr)
  1729. {
  1730. u32 ethcmd;
  1731. struct speedo_private *sp = dev->priv;
  1732. if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
  1733. return -EFAULT;
  1734.         switch (ethcmd) {
  1735. /* get driver-specific version/etc. info */
  1736. case ETHTOOL_GDRVINFO: {
  1737. struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
  1738. strncpy(info.driver, "eepro100", sizeof(info.driver)-1);
  1739. strncpy(info.version, version, sizeof(info.version)-1);
  1740. if (sp && sp->pdev)
  1741. strcpy(info.bus_info, sp->pdev->slot_name);
  1742. if (copy_to_user(useraddr, &info, sizeof(info)))
  1743. return -EFAULT;
  1744. return 0;
  1745. }
  1746. /* get settings */
  1747. case ETHTOOL_GSET: {
  1748. struct ethtool_cmd ecmd = { ETHTOOL_GSET };
  1749. spin_lock_irq(&sp->lock);
  1750. mii_ethtool_gset(&sp->mii_if, &ecmd);
  1751. spin_unlock_irq(&sp->lock);
  1752. if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
  1753. return -EFAULT;
  1754. return 0;
  1755. }
  1756. /* set settings */
  1757. case ETHTOOL_SSET: {
  1758. int r;
  1759. struct ethtool_cmd ecmd;
  1760. if (copy_from_user(&ecmd, useraddr, sizeof(ecmd)))
  1761. return -EFAULT;
  1762. spin_lock_irq(&sp->lock);
  1763. r = mii_ethtool_sset(&sp->mii_if, &ecmd);
  1764. spin_unlock_irq(&sp->lock);
  1765. return r;
  1766. }
  1767. /* restart autonegotiation */
  1768. case ETHTOOL_NWAY_RST: {
  1769. return mii_nway_restart(&sp->mii_if);
  1770. }
  1771. /* get link status */
  1772. case ETHTOOL_GLINK: {
  1773. struct ethtool_value edata = {ETHTOOL_GLINK};
  1774. edata.data = mii_link_ok(&sp->mii_if);
  1775. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1776. return -EFAULT;
  1777. return 0;
  1778. }
  1779. /* get message-level */
  1780. case ETHTOOL_GMSGLVL: {
  1781. struct ethtool_value edata = {ETHTOOL_GMSGLVL};
  1782. edata.data = sp->msg_enable;
  1783. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1784. return -EFAULT;
  1785. return 0;
  1786. }
  1787. /* set message-level */
  1788. case ETHTOOL_SMSGLVL: {
  1789. struct ethtool_value edata;
  1790. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1791. return -EFAULT;
  1792. sp->msg_enable = edata.data;
  1793. return 0;
  1794. }
  1795.         }
  1796. return -EOPNOTSUPP;
  1797. }
  1798. static int speedo_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1799. {
  1800. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  1801. struct mii_ioctl_data *data = (struct mii_ioctl_data *)&rq->ifr_data;
  1802. int phy = sp->phy[0] & 0x1f;
  1803. int saved_acpi;
  1804. int t;
  1805.     switch(cmd) {
  1806. case SIOCGMIIPHY: /* Get address of MII PHY in use. */
  1807. case SIOCDEVPRIVATE: /* for binary compat, remove in 2.5 */
  1808. data->phy_id = phy;
  1809. case SIOCGMIIREG: /* Read MII PHY register. */
  1810. case SIOCDEVPRIVATE+1: /* for binary compat, remove in 2.5 */
  1811. /* FIXME: these operations need to be serialized with MDIO
  1812.    access from the timeout handler.
  1813.    They are currently serialized only with MDIO access from the
  1814.    timer routine.  2000/05/09 SAW */
  1815. saved_acpi = pci_set_power_state(sp->pdev, 0);
  1816. t = del_timer_sync(&sp->timer);
  1817. data->val_out = mdio_read(dev, data->phy_id & 0x1f, data->reg_num & 0x1f);
  1818. if (t)
  1819. add_timer(&sp->timer); /* may be set to the past  --SAW */
  1820. pci_set_power_state(sp->pdev, saved_acpi);
  1821. return 0;
  1822. case SIOCSMIIREG: /* Write MII PHY register. */
  1823. case SIOCDEVPRIVATE+2: /* for binary compat, remove in 2.5 */
  1824. if (!capable(CAP_NET_ADMIN))
  1825. return -EPERM;
  1826. saved_acpi = pci_set_power_state(sp->pdev, 0);
  1827. t = del_timer_sync(&sp->timer);
  1828. mdio_write(dev, data->phy_id, data->reg_num, data->val_in);
  1829. if (t)
  1830. add_timer(&sp->timer); /* may be set to the past  --SAW */
  1831. pci_set_power_state(sp->pdev, saved_acpi);
  1832. return 0;
  1833. case SIOCETHTOOL:
  1834. return netdev_ethtool_ioctl(dev, (void *) rq->ifr_data);
  1835. default:
  1836. return -EOPNOTSUPP;
  1837. }
  1838. }
  1839. /* Set or clear the multicast filter for this adaptor.
  1840.    This is very ugly with Intel chips -- we usually have to execute an
  1841.    entire configuration command, plus process a multicast command.
  1842.    This is complicated.  We must put a large configuration command and
  1843.    an arbitrarily-sized multicast command in the transmit list.
  1844.    To minimize the disruption -- the previous command might have already
  1845.    loaded the link -- we convert the current command block, normally a Tx
  1846.    command, into a no-op and link it to the new command.
  1847. */
  1848. static void set_rx_mode(struct net_device *dev)
  1849. {
  1850. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  1851. long ioaddr = dev->base_addr;
  1852. struct descriptor *last_cmd;
  1853. char new_rx_mode;
  1854. unsigned long flags;
  1855. int entry, i;
  1856. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  1857. new_rx_mode = 3;
  1858. } else if ((dev->flags & IFF_ALLMULTI)  ||
  1859.    dev->mc_count > multicast_filter_limit) {
  1860. new_rx_mode = 1;
  1861. } else
  1862. new_rx_mode = 0;
  1863. if (netif_msg_rx_status(sp))
  1864. printk(KERN_DEBUG "%s: set_rx_mode %d -> %dn", dev->name,
  1865. sp->rx_mode, new_rx_mode);
  1866. if ((int)(sp->cur_tx - sp->dirty_tx) > TX_RING_SIZE - TX_MULTICAST_SIZE) {
  1867.     /* The Tx ring is full -- don't add anything!  Hope the mode will be
  1868.  * set again later. */
  1869. sp->rx_mode = -1;
  1870. return;
  1871. }
  1872. if (new_rx_mode != sp->rx_mode) {
  1873. u8 *config_cmd_data;
  1874. spin_lock_irqsave(&sp->lock, flags);
  1875. entry = sp->cur_tx++ % TX_RING_SIZE;
  1876. last_cmd = sp->last_cmd;
  1877. sp->last_cmd = (struct descriptor *)&sp->tx_ring[entry];
  1878. sp->tx_skbuff[entry] = 0; /* Redundant. */
  1879. sp->tx_ring[entry].status = cpu_to_le32(CmdSuspend | CmdConfigure);
  1880. sp->tx_ring[entry].link =
  1881. cpu_to_le32(TX_RING_ELEM_DMA(sp, (entry + 1) % TX_RING_SIZE));
  1882. config_cmd_data = (void *)&sp->tx_ring[entry].tx_desc_addr;
  1883. /* Construct a full CmdConfig frame. */
  1884. memcpy(config_cmd_data, i82558_config_cmd, CONFIG_DATA_SIZE);
  1885. config_cmd_data[1] = (txfifo << 4) | rxfifo;
  1886. config_cmd_data[4] = rxdmacount;
  1887. config_cmd_data[5] = txdmacount + 0x80;
  1888. config_cmd_data[15] |= (new_rx_mode & 2) ? 1 : 0;
  1889. /* 0x80 doesn't disable FC 0x84 does.
  1890.    Disable Flow control since we are not ACK-ing any FC interrupts
  1891.    for now. --Dragan */
  1892. config_cmd_data[19] = 0x84;
  1893. config_cmd_data[19] |= sp->mii_if.full_duplex ? 0x40 : 0;
  1894. config_cmd_data[21] = (new_rx_mode & 1) ? 0x0D : 0x05;
  1895. if (sp->phy[0] & 0x8000) { /* Use the AUI port instead. */
  1896. config_cmd_data[15] |= 0x80;
  1897. config_cmd_data[8] = 0;
  1898. }
  1899. /* Trigger the command unit resume. */
  1900. wait_for_cmd_done(dev);
  1901. clear_suspend(last_cmd);
  1902. outb(CUResume, ioaddr + SCBCmd);
  1903. if ((int)(sp->cur_tx - sp->dirty_tx) >= TX_QUEUE_LIMIT) {
  1904. netif_stop_queue(dev);
  1905. sp->tx_full = 1;
  1906. }
  1907. spin_unlock_irqrestore(&sp->lock, flags);
  1908. }
  1909. if (new_rx_mode == 0  &&  dev->mc_count < 4) {
  1910. /* The simple case of 0-3 multicast list entries occurs often, and
  1911.    fits within one tx_ring[] entry. */
  1912. struct dev_mc_list *mclist;
  1913. u16 *setup_params, *eaddrs;
  1914. spin_lock_irqsave(&sp->lock, flags);
  1915. entry = sp->cur_tx++ % TX_RING_SIZE;
  1916. last_cmd = sp->last_cmd;
  1917. sp->last_cmd = (struct descriptor *)&sp->tx_ring[entry];
  1918. sp->tx_skbuff[entry] = 0;
  1919. sp->tx_ring[entry].status = cpu_to_le32(CmdSuspend | CmdMulticastList);
  1920. sp->tx_ring[entry].link =
  1921. cpu_to_le32(TX_RING_ELEM_DMA(sp, (entry + 1) % TX_RING_SIZE));
  1922. sp->tx_ring[entry].tx_desc_addr = 0; /* Really MC list count. */
  1923. setup_params = (u16 *)&sp->tx_ring[entry].tx_desc_addr;
  1924. *setup_params++ = cpu_to_le16(dev->mc_count*6);
  1925. /* Fill in the multicast addresses. */
  1926. for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
  1927.  i++, mclist = mclist->next) {
  1928. eaddrs = (u16 *)mclist->dmi_addr;
  1929. *setup_params++ = *eaddrs++;
  1930. *setup_params++ = *eaddrs++;
  1931. *setup_params++ = *eaddrs++;
  1932. }
  1933. wait_for_cmd_done(dev);
  1934. clear_suspend(last_cmd);
  1935. /* Immediately trigger the command unit resume. */
  1936. outb(CUResume, ioaddr + SCBCmd);
  1937. if ((int)(sp->cur_tx - sp->dirty_tx) >= TX_QUEUE_LIMIT) {
  1938. netif_stop_queue(dev);
  1939. sp->tx_full = 1;
  1940. }
  1941. spin_unlock_irqrestore(&sp->lock, flags);
  1942. } else if (new_rx_mode == 0) {
  1943. struct dev_mc_list *mclist;
  1944. u16 *setup_params, *eaddrs;
  1945. struct speedo_mc_block *mc_blk;
  1946. struct descriptor *mc_setup_frm;
  1947. int i;
  1948. mc_blk = kmalloc(sizeof(*mc_blk) + 2 + multicast_filter_limit*6,
  1949.  GFP_ATOMIC);
  1950. if (mc_blk == NULL) {
  1951. printk(KERN_ERR "%s: Failed to allocate a setup frame.n",
  1952.    dev->name);
  1953. sp->rx_mode = -1; /* We failed, try again. */
  1954. return;
  1955. }
  1956. mc_blk->next = NULL;
  1957. mc_blk->len = 2 + multicast_filter_limit*6;
  1958. mc_blk->frame_dma =
  1959. pci_map_single(sp->pdev, &mc_blk->frame, mc_blk->len,
  1960. PCI_DMA_TODEVICE);
  1961. mc_setup_frm = &mc_blk->frame;
  1962. /* Fill the setup frame. */
  1963. if (netif_msg_ifup(sp))
  1964. printk(KERN_DEBUG "%s: Constructing a setup frame at %p.n",
  1965.    dev->name, mc_setup_frm);
  1966. mc_setup_frm->cmd_status =
  1967. cpu_to_le32(CmdSuspend | CmdIntr | CmdMulticastList);
  1968. /* Link set below. */
  1969. setup_params = (u16 *)&mc_setup_frm->params;
  1970. *setup_params++ = cpu_to_le16(dev->mc_count*6);
  1971. /* Fill in the multicast addresses. */
  1972. for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
  1973.  i++, mclist = mclist->next) {
  1974. eaddrs = (u16 *)mclist->dmi_addr;
  1975. *setup_params++ = *eaddrs++;
  1976. *setup_params++ = *eaddrs++;
  1977. *setup_params++ = *eaddrs++;
  1978. }
  1979. /* Disable interrupts while playing with the Tx Cmd list. */
  1980. spin_lock_irqsave(&sp->lock, flags);
  1981. if (sp->mc_setup_tail)
  1982. sp->mc_setup_tail->next = mc_blk;
  1983. else
  1984. sp->mc_setup_head = mc_blk;
  1985. sp->mc_setup_tail = mc_blk;
  1986. mc_blk->tx = sp->cur_tx;
  1987. entry = sp->cur_tx++ % TX_RING_SIZE;
  1988. last_cmd = sp->last_cmd;
  1989. sp->last_cmd = mc_setup_frm;
  1990. /* Change the command to a NoOp, pointing to the CmdMulti command. */
  1991. sp->tx_skbuff[entry] = 0;
  1992. sp->tx_ring[entry].status = cpu_to_le32(CmdNOp);
  1993. sp->tx_ring[entry].link = cpu_to_le32(mc_blk->frame_dma);
  1994. /* Set the link in the setup frame. */
  1995. mc_setup_frm->link =
  1996. cpu_to_le32(TX_RING_ELEM_DMA(sp, (entry + 1) % TX_RING_SIZE));
  1997. pci_dma_sync_single(sp->pdev, mc_blk->frame_dma,
  1998. mc_blk->len, PCI_DMA_TODEVICE);
  1999. wait_for_cmd_done(dev);
  2000. clear_suspend(last_cmd);
  2001. /* Immediately trigger the command unit resume. */
  2002. outb(CUResume, ioaddr + SCBCmd);
  2003. if ((int)(sp->cur_tx - sp->dirty_tx) >= TX_QUEUE_LIMIT) {
  2004. netif_stop_queue(dev);
  2005. sp->tx_full = 1;
  2006. }
  2007. spin_unlock_irqrestore(&sp->lock, flags);
  2008. if (netif_msg_rx_status(sp))
  2009. printk(" CmdMCSetup frame length %d in entry %d.n",
  2010.    dev->mc_count, entry);
  2011. }
  2012. sp->rx_mode = new_rx_mode;
  2013. }
  2014. #ifdef CONFIG_PM
  2015. static int eepro100_suspend(struct pci_dev *pdev, u32 state)
  2016. {
  2017. struct net_device *dev = pci_get_drvdata (pdev);
  2018. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  2019. long ioaddr = dev->base_addr;
  2020. pci_save_state(pdev, sp->pm_state);
  2021. if (!netif_running(dev))
  2022. return 0;
  2023. del_timer_sync(&sp->timer);
  2024. netif_device_detach(dev);
  2025. outl(PortPartialReset, ioaddr + SCBPort);
  2026. /* XXX call pci_set_power_state ()? */
  2027. return 0;
  2028. }
  2029. static int eepro100_resume(struct pci_dev *pdev)
  2030. {
  2031. struct net_device *dev = pci_get_drvdata (pdev);
  2032. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  2033. long ioaddr = dev->base_addr;
  2034. pci_restore_state(pdev, sp->pm_state);
  2035. if (!netif_running(dev))
  2036. return 0;
  2037. /* I'm absolutely uncertain if this part of code may work.
  2038.    The problems are:
  2039.     - correct hardware reinitialization;
  2040. - correct driver behavior between different steps of the
  2041.   reinitialization;
  2042. - serialization with other driver calls.
  2043.    2000/03/08  SAW */
  2044. outw(SCBMaskAll, ioaddr + SCBCmd);
  2045. speedo_resume(dev);
  2046. netif_device_attach(dev);
  2047. sp->rx_mode = -1;
  2048. sp->flow_ctrl = sp->partner = 0;
  2049. set_rx_mode(dev);
  2050. sp->timer.expires = RUN_AT(2*HZ);
  2051. add_timer(&sp->timer);
  2052. return 0;
  2053. }
  2054. #endif /* CONFIG_PM */
  2055. static void __devexit eepro100_remove_one (struct pci_dev *pdev)
  2056. {
  2057. struct net_device *dev = pci_get_drvdata (pdev);
  2058. struct speedo_private *sp = (struct speedo_private *)dev->priv;
  2059. unregister_netdev(dev);
  2060. release_region(pci_resource_start(pdev, 1), pci_resource_len(pdev, 1));
  2061. release_mem_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
  2062. #ifndef USE_IO
  2063. iounmap((char *)dev->base_addr);
  2064. #endif
  2065. pci_free_consistent(pdev, TX_RING_SIZE * sizeof(struct TxFD)
  2066. + sizeof(struct speedo_stats),
  2067. sp->tx_ring, sp->tx_ring_dma);
  2068. pci_disable_device(pdev);
  2069. kfree(dev);
  2070. }
  2071. static struct pci_device_id eepro100_pci_tbl[] __devinitdata = {
  2072. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82557,
  2073. PCI_ANY_ID, PCI_ANY_ID, },
  2074. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82559ER,
  2075. PCI_ANY_ID, PCI_ANY_ID, },
  2076. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_7,
  2077. PCI_ANY_ID, PCI_ANY_ID, },
  2078. { PCI_VENDOR_ID_INTEL, 0x1029, PCI_ANY_ID, PCI_ANY_ID, },
  2079. { PCI_VENDOR_ID_INTEL, 0x1030, PCI_ANY_ID, PCI_ANY_ID, },
  2080. { PCI_VENDOR_ID_INTEL, 0x1031, PCI_ANY_ID, PCI_ANY_ID, },
  2081. { PCI_VENDOR_ID_INTEL, 0x1032, PCI_ANY_ID, PCI_ANY_ID, },
  2082. { PCI_VENDOR_ID_INTEL, 0x1033, PCI_ANY_ID, PCI_ANY_ID, },
  2083. { PCI_VENDOR_ID_INTEL, 0x1034, PCI_ANY_ID, PCI_ANY_ID, },
  2084. { PCI_VENDOR_ID_INTEL, 0x1035, PCI_ANY_ID, PCI_ANY_ID, },
  2085. { PCI_VENDOR_ID_INTEL, 0x1036, PCI_ANY_ID, PCI_ANY_ID, },
  2086. { PCI_VENDOR_ID_INTEL, 0x1037, PCI_ANY_ID, PCI_ANY_ID, },
  2087. { PCI_VENDOR_ID_INTEL, 0x1038, PCI_ANY_ID, PCI_ANY_ID, },
  2088. { PCI_VENDOR_ID_INTEL, 0x1039, PCI_ANY_ID, PCI_ANY_ID, },
  2089. { PCI_VENDOR_ID_INTEL, 0x103A, PCI_ANY_ID, PCI_ANY_ID, },
  2090. { PCI_VENDOR_ID_INTEL, 0x103B, PCI_ANY_ID, PCI_ANY_ID, },
  2091. { PCI_VENDOR_ID_INTEL, 0x103C, PCI_ANY_ID, PCI_ANY_ID, },
  2092. { PCI_VENDOR_ID_INTEL, 0x103D, PCI_ANY_ID, PCI_ANY_ID, },
  2093. { PCI_VENDOR_ID_INTEL, 0x103E, PCI_ANY_ID, PCI_ANY_ID, },
  2094. { PCI_VENDOR_ID_INTEL, 0x1227, PCI_ANY_ID, PCI_ANY_ID, },
  2095. { PCI_VENDOR_ID_INTEL, 0x1228, PCI_ANY_ID, PCI_ANY_ID, },
  2096. { PCI_VENDOR_ID_INTEL, 0x2449, PCI_ANY_ID, PCI_ANY_ID, },
  2097. { PCI_VENDOR_ID_INTEL, 0x2459, PCI_ANY_ID, PCI_ANY_ID, },
  2098. { PCI_VENDOR_ID_INTEL, 0x245D, PCI_ANY_ID, PCI_ANY_ID, },
  2099. { PCI_VENDOR_ID_INTEL, 0x5200, PCI_ANY_ID, PCI_ANY_ID, },
  2100. { PCI_VENDOR_ID_INTEL, 0x5201, PCI_ANY_ID, PCI_ANY_ID, },
  2101. { 0,}
  2102. };
  2103. MODULE_DEVICE_TABLE(pci, eepro100_pci_tbl);
  2104. static struct pci_driver eepro100_driver = {
  2105. name: "eepro100",
  2106. id_table: eepro100_pci_tbl,
  2107. probe: eepro100_init_one,
  2108. remove: __devexit_p(eepro100_remove_one),
  2109. #ifdef CONFIG_PM
  2110. suspend: eepro100_suspend,
  2111. resume: eepro100_resume,
  2112. #endif /* CONFIG_PM */
  2113. };
  2114. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,48)
  2115. static int pci_module_init(struct pci_driver *pdev)
  2116. {
  2117. int rc;
  2118. rc = pci_register_driver(pdev);
  2119. if (rc <= 0) {
  2120. printk(KERN_INFO "%s: No cards found, driver not installed.n",
  2121.    pdev->name);
  2122. pci_unregister_driver(pdev);
  2123. return -ENODEV;
  2124. }
  2125. return 0;
  2126. }
  2127. #endif
  2128. static int __init eepro100_init_module(void)
  2129. {
  2130. #ifdef MODULE
  2131. printk(version);
  2132. #endif
  2133. return pci_module_init(&eepro100_driver);
  2134. }
  2135. static void __exit eepro100_cleanup_module(void)
  2136. {
  2137. pci_unregister_driver(&eepro100_driver);
  2138. }
  2139. module_init(eepro100_init_module);
  2140. module_exit(eepro100_cleanup_module);
  2141. /*
  2142.  * Local variables:
  2143.  *  compile-command: "gcc -DMODULE -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -c eepro100.c `[ -f /usr/include/linux/modversions.h ] && echo -DMODVERSIONS`"
  2144.  *  c-indent-level: 4
  2145.  *  c-basic-offset: 4
  2146.  *  tab-width: 4
  2147.  * End:
  2148.  */