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