|
|
1.1 root 1: /* myson803.c: A Linux device driver for the Myson mtd803 Ethernet chip. */
2: /*
3: Written 1998-2003 by Donald Becker.
4:
5: This software may be used and distributed according to the terms of
6: the GNU General Public License (GPL), incorporated herein by reference.
7: Drivers based on or derived from this code fall under the GPL and must
8: retain the authorship, copyright and license notice. This file is not
9: a complete program and may only be used when the entire operating
10: system is licensed under the GPL.
11:
12: The author may be reached as [email protected], or C/O
13: Scyld Computing Corporation
14: 410 Severn Ave., Suite 210
15: Annapolis MD 21403
16:
17: Support information and updates available at
18: http://www.scyld.com/network/myson803.html
19: */
20:
21: /* These identify the driver base version and may not be removed. */
22: static const char version1[] =
23: "myson803.c:v1.05 3/10/2003 Written by Donald Becker <[email protected]>\n";
24: static const char version2[] =
25: " http://www.scyld.com/network/drivers.html\n";
26:
27: /* Automatically extracted configuration info:
28: probe-func: myson803_probe
29: config-in: tristate 'Myson MTD803 series Ethernet support' CONFIG_MYSON_ETHER
30:
31: c-help-name: Myson MTD803 PCI Ethernet support
32: c-help-symbol: CONFIG_MYSON_ETHER
33: c-help: This driver is for the Myson MTD803 Ethernet adapter series.
34: c-help: More specific information and updates are available from
35: c-help: http://www.scyld.com/network/drivers.html
36: */
37:
38: /* The user-configurable values.
39: These may be modified when a driver module is loaded.*/
40:
41: /* Message enable level: 0..31 = no..all messages. See NETIF_MSG docs. */
42: static int debug = 2;
43:
44: /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
45: static int max_interrupt_work = 40;
46:
47: /* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
48: This chip uses a 64 element hash table based on the Ethernet CRC. */
49: static int multicast_filter_limit = 32;
50:
51: /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
52: Setting to > 1518 effectively disables this feature. */
53: static int rx_copybreak = 0;
54:
55: /* Used to pass the media type, etc.
56: Both 'options[]' and 'full_duplex[]' should exist for driver
57: interoperability.
58: The media type is usually passed in 'options[]'.
59: The default is autonegotation for speed and duplex.
60: This should rarely be overridden.
61: Use option values 0x10/0x20 for 10Mbps, 0x100,0x200 for 100Mbps.
62: Use option values 0x10 and 0x100 for forcing half duplex fixed speed.
63: Use option values 0x20 and 0x200 for forcing full duplex operation.
64: */
65: #define MAX_UNITS 8 /* More are supported, limit only on options */
66: static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
67: static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
68:
69: /* Operational parameters that are set at compile time. */
70:
71: /* Keep the ring sizes a power of two for compile efficiency.
72: The compiler will convert <unsigned>'%'<2^N> into a bit mask.
73: Making the Tx ring too large decreases the effectiveness of channel
74: bonding and packet priority.
75: There are no ill effects from too-large receive rings. */
76: #define TX_RING_SIZE 16
77: #define TX_QUEUE_LEN 10 /* Limit Tx ring entries actually used. */
78: #define RX_RING_SIZE 32
79:
80: /* Operational parameters that usually are not changed. */
81: /* Time in jiffies before concluding the transmitter is hung. */
82: #define TX_TIMEOUT (6*HZ)
83:
84: /* Allocation size of Rx buffers with normal sized Ethernet frames.
85: Do not change this value without good reason. This is not a limit,
86: but a way to keep a consistent allocation size among drivers.
87: */
88: #define PKT_BUF_SZ 1536
89:
90: #ifndef __KERNEL__
91: #define __KERNEL__
92: #endif
93: #if !defined(__OPTIMIZE__)
94: #warning You must compile this file with the correct options!
95: #warning See the last lines of the source file.
96: #error You must compile this driver with "-O".
97: #endif
98:
99: /* Include files, designed to support most kernel versions 2.0.0 and later. */
100: #include <linux/config.h>
101: #if defined(CONFIG_SMP) && ! defined(__SMP__)
102: #define __SMP__
103: #endif
104: #if defined(MODULE) && defined(CONFIG_MODVERSIONS) && ! defined(MODVERSIONS)
105: #define MODVERSIONS
106: #endif
107:
108: #include <linux/version.h>
109: #if defined(MODVERSIONS)
110: #include <linux/modversions.h>
111: #endif
112: #include <linux/module.h>
113:
114: #include <linux/kernel.h>
115: #include <linux/string.h>
116: #include <linux/timer.h>
117: #include <linux/errno.h>
118: #include <linux/ioport.h>
119: #if LINUX_VERSION_CODE >= 0x20400
120: #include <linux/slab.h>
121: #else
122: #include <linux/malloc.h>
123: #endif
124: #include <linux/interrupt.h>
125: #include <linux/pci.h>
126: #include <linux/netdevice.h>
127: #include <linux/etherdevice.h>
128: #include <linux/skbuff.h>
129: #include <linux/delay.h>
130: #include <asm/processor.h> /* Processor type for cache alignment. */
131: #include <asm/bitops.h>
132: #include <asm/io.h>
133: #include <asm/unaligned.h>
134:
135: #ifdef INLINE_PCISCAN
136: #include "k_compat.h"
137: #else
138: #include "pci-scan.h"
139: #include "kern_compat.h"
140: #endif
141:
142: /* Condensed operations for readability. */
143: #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
144: #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
145:
146: #if (LINUX_VERSION_CODE >= 0x20100) && defined(MODULE)
147: char kernel_version[] = UTS_RELEASE;
148: #endif
149:
150: /* Kernels before 2.1.0 cannot map the high addrs assigned by some BIOSes. */
151: #if (LINUX_VERSION_CODE < 0x20100) || ! defined(MODULE)
152: #define USE_IO_OPS
153: #endif
154:
155: MODULE_AUTHOR("Donald Becker <[email protected]>");
156: MODULE_DESCRIPTION("Myson mtd803 Ethernet driver");
157: MODULE_LICENSE("GPL");
158: /* List in order of common use. */
159: MODULE_PARM(debug, "i");
160: MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
161: MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
162: MODULE_PARM(max_interrupt_work, "i");
163: MODULE_PARM(rx_copybreak, "i");
164: MODULE_PARM(multicast_filter_limit, "i");
165: MODULE_PARM_DESC(debug, "Driver message level (0-31)");
166: MODULE_PARM_DESC(options, "Force transceiver type or fixed speed+duplex");
167: MODULE_PARM_DESC(full_duplex, "Non-zero to force full duplex, "
168: "non-negotiated link (deprecated).");
169: MODULE_PARM_DESC(max_interrupt_work,
170: "Maximum events handled per interrupt");
171: MODULE_PARM_DESC(rx_copybreak,
172: "Breakpoint in bytes for copy-only-tiny-frames");
173: MODULE_PARM_DESC(multicast_filter_limit,
174: "Multicast addresses before switching to Rx-all-multicast");
175:
176: /*
177: Theory of Operation
178:
179: I. Board Compatibility
180:
181: This driver is for the Myson mtd803 chip.
182: It should work with other Myson 800 series chips.
183:
184: II. Board-specific settings
185:
186: None.
187:
188: III. Driver operation
189:
190: IIIa. Ring buffers
191:
192: This driver uses two statically allocated fixed-size descriptor lists
193: formed into rings by a branch from the final descriptor to the beginning of
194: the list. The ring sizes are set at compile time by RX/TX_RING_SIZE.
195: Some chips explicitly use only 2^N sized rings, while others use a
196: 'next descriptor' pointer that the driver forms into rings.
197:
198: IIIb/c. Transmit/Receive Structure
199:
200: This driver uses a zero-copy receive and transmit scheme.
201: The driver allocates full frame size skbuffs for the Rx ring buffers at
202: open() time and passes the skb->data field to the chip as receive data
203: buffers. When an incoming frame is less than RX_COPYBREAK bytes long,
204: a fresh skbuff is allocated and the frame is copied to the new skbuff.
205: When the incoming frame is larger, the skbuff is passed directly up the
206: protocol stack. Buffers consumed this way are replaced by newly allocated
207: skbuffs in a later phase of receives.
208:
209: The RX_COPYBREAK value is chosen to trade-off the memory wasted by
210: using a full-sized skbuff for small frames vs. the copying costs of larger
211: frames. New boards are typically used in generously configured machines
212: and the underfilled buffers have negligible impact compared to the benefit of
213: a single allocation size, so the default value of zero results in never
214: copying packets. When copying is done, the cost is usually mitigated by using
215: a combined copy/checksum routine. Copying also preloads the cache, which is
216: most useful with small frames.
217:
218: A subtle aspect of the operation is that the IP header at offset 14 in an
219: ethernet frame isn't longword aligned for further processing.
220: When unaligned buffers are permitted by the hardware (and always on copies)
221: frames are put into the skbuff at an offset of "+2", 16-byte aligning
222: the IP header.
223:
224: IIId. Synchronization
225:
226: The driver runs as two independent, single-threaded flows of control. One
227: is the send-packet routine, which enforces single-threaded use by the
228: dev->tbusy flag. The other thread is the interrupt handler, which is single
229: threaded by the hardware and interrupt handling software.
230:
231: The send packet thread has partial control over the Tx ring and 'dev->tbusy'
232: flag. It sets the tbusy flag whenever it's queuing a Tx packet. If the next
233: queue slot is empty, it clears the tbusy flag when finished otherwise it sets
234: the 'lp->tx_full' flag.
235:
236: The interrupt handler has exclusive control over the Rx ring and records stats
237: from the Tx ring. After reaping the stats, it marks the Tx queue entry as
238: empty by incrementing the dirty_tx mark. Iff the 'lp->tx_full' flag is set, it
239: clears both the tx_full and tbusy flags.
240:
241: IIId. SMP semantics
242:
243: The following are serialized with respect to each other via the "xmit_lock".
244: dev->hard_start_xmit() Transmit a packet
245: dev->tx_timeout() Transmit watchdog for stuck Tx
246: dev->set_multicast_list() Set the recieve filter.
247: Note: The Tx timeout watchdog code is implemented by the timer routine in
248: kernels up to 2.2.*. In 2.4.* and later the timeout code is part of the
249: driver interface.
250:
251: The following fall under the global kernel lock. The module will not be
252: unloaded during the call, unless a call with a potential reschedule e.g.
253: kmalloc() is called. No other synchronization assertion is made.
254: dev->open()
255: dev->do_ioctl()
256: dev->get_stats()
257: Caution: The lock for dev->open() is commonly broken with request_irq() or
258: kmalloc(). It is best to avoid any lock-breaking call in do_ioctl() and
259: get_stats(), or additional module locking code must be implemented.
260:
261: The following is self-serialized (no simultaneous entry)
262: An handler registered with request_irq().
263:
264: IV. Notes
265:
266: IVb. References
267:
268: http://www.scyld.com/expert/100mbps.html
269: http://scyld.com/expert/NWay.html
270: http://www.myson.com.hk/mtd/datasheet/mtd803.pdf
271: Myson does not require a NDA to read the datasheet.
272:
273: IVc. Errata
274:
275: No undocumented errata.
276: */
277:
278:
279:
280: /* PCI probe routines. */
281:
282: static void *myson_probe1(struct pci_dev *pdev, void *init_dev,
283: long ioaddr, int irq, int chip_idx, int find_cnt);
284: static int netdev_pwr_event(void *dev_instance, int event);
285:
286: /* Chips prior to the 803 have an external MII transceiver. */
287: enum chip_capability_flags { HasMIIXcvr=1, HasChipXcvr=2 };
288:
289: #ifdef USE_IO_OPS
290: #define PCI_IOTYPE (PCI_USES_MASTER | PCI_USES_IO | PCI_ADDR0)
291: #define PCI_IOSIZE 256
292: #else
293: #define PCI_IOTYPE (PCI_USES_MASTER | PCI_USES_MEM | PCI_ADDR1)
294: #define PCI_IOSIZE 1024
295: #endif
296:
297: static struct pci_id_info pci_id_tbl[] = {
298: {"Myson mtd803 Fast Ethernet", {0x08031516, 0xffffffff, },
299: PCI_IOTYPE, PCI_IOSIZE, HasChipXcvr},
300: {"Myson mtd891 Gigabit Ethernet", {0x08911516, 0xffffffff, },
301: PCI_IOTYPE, PCI_IOSIZE, HasChipXcvr},
302: {0,}, /* 0 terminated list. */
303: };
304:
305: struct drv_id_info myson803_drv_id = {
306: "myson803", 0, PCI_CLASS_NETWORK_ETHERNET<<8, pci_id_tbl, myson_probe1,
307: netdev_pwr_event };
308:
309: /* This driver was written to use PCI memory space, however x86-oriented
310: hardware sometimes works only with I/O space accesses. */
311: #ifdef USE_IO_OPS
312: #undef readb
313: #undef readw
314: #undef readl
315: #undef writeb
316: #undef writew
317: #undef writel
318: #define readb inb
319: #define readw inw
320: #define readl inl
321: #define writeb outb
322: #define writew outw
323: #define writel outl
324: #endif
325:
326: /* Offsets to the various registers.
327: Most accesses must be longword aligned. */
328: enum register_offsets {
329: StationAddr=0x00, MulticastFilter0=0x08, MulticastFilter1=0x0C,
330: FlowCtrlAddr=0x10, RxConfig=0x18, TxConfig=0x1a, PCIBusCfg=0x1c,
331: TxStartDemand=0x20, RxStartDemand=0x24,
332: RxCurrentPtr=0x28, TxRingPtr=0x2c, RxRingPtr=0x30,
333: IntrStatus=0x34, IntrEnable=0x38,
334: FlowCtrlThreshold=0x3c,
335: MIICtrl=0x40, EECtrl=0x40, RxErrCnts=0x44, TxErrCnts=0x48,
336: PHYMgmt=0x4c,
337: };
338:
339: /* Bits in the interrupt status/mask registers. */
340: enum intr_status_bits {
341: IntrRxErr=0x0002, IntrRxDone=0x0004, IntrTxDone=0x0008,
342: IntrTxEmpty=0x0010, IntrRxEmpty=0x0020, StatsMax=0x0040, RxEarly=0x0080,
343: TxEarly=0x0100, RxOverflow=0x0200, TxUnderrun=0x0400,
344: IntrPCIErr=0x2000, NWayDone=0x4000, LinkChange=0x8000,
345: };
346:
347: /* Bits in the RxMode (np->txrx_config) register. */
348: enum rx_mode_bits {
349: RxEnable=0x01, RxFilter=0xfe,
350: AcceptErr=0x02, AcceptRunt=0x08, AcceptBroadcast=0x40,
351: AcceptMulticast=0x20, AcceptAllPhys=0x80, AcceptMyPhys=0x00,
352: RxFlowCtrl=0x2000,
353: TxEnable=0x40000, TxModeFDX=0x00100000, TxThreshold=0x00e00000,
354: };
355:
356: /* Misc. bits. */
357: enum misc_bits {
358: BCR_Reset=1, /* PCIBusCfg */
359: TxThresholdInc=0x200000,
360: };
361:
362: /* The Rx and Tx buffer descriptors. */
363: /* Note that using only 32 bit fields simplifies conversion to big-endian
364: architectures. */
365: struct netdev_desc {
366: u32 status;
367: u32 ctrl_length;
368: u32 buf_addr;
369: u32 next_desc;
370: };
371:
372: /* Bits in network_desc.status */
373: enum desc_status_bits {
374: DescOwn=0x80000000,
375: RxDescStartPacket=0x0800, RxDescEndPacket=0x0400, RxDescWholePkt=0x0c00,
376: RxDescErrSum=0x80, RxErrRunt=0x40, RxErrLong=0x20, RxErrFrame=0x10,
377: RxErrCRC=0x08, RxErrCode=0x04,
378: TxErrAbort=0x2000, TxErrCarrier=0x1000, TxErrLate=0x0800,
379: TxErr16Colls=0x0400, TxErrDefer=0x0200, TxErrHeartbeat=0x0100,
380: TxColls=0x00ff,
381: };
382: /* Bits in network_desc.ctrl_length */
383: enum ctrl_length_bits {
384: TxIntrOnDone=0x80000000, TxIntrOnFIFO=0x40000000,
385: TxDescEndPacket=0x20000000, TxDescStartPacket=0x10000000,
386: TxAppendCRC=0x08000000, TxPadTo64=0x04000000, TxNormalPkt=0x3C000000,
387: };
388:
389: #define PRIV_ALIGN 15 /* Required alignment mask */
390: /* Use __attribute__((aligned (L1_CACHE_BYTES))) to maintain alignment
391: within the structure. */
392: struct netdev_private {
393: /* Descriptor rings first for alignment. */
394: struct netdev_desc rx_ring[RX_RING_SIZE];
395: struct netdev_desc tx_ring[TX_RING_SIZE];
396: struct net_device *next_module; /* Link for devices of this type. */
397: void *priv_addr; /* Unaligned address for kfree */
398: /* The addresses of receive-in-place skbuffs. */
399: struct sk_buff* rx_skbuff[RX_RING_SIZE];
400: /* The saved address of a sent-in-place packet/buffer, for later free(). */
401: struct sk_buff* tx_skbuff[TX_RING_SIZE];
402: struct net_device_stats stats;
403: struct timer_list timer; /* Media monitoring timer. */
404: /* Frequently used values: keep some adjacent for cache effect. */
405: int msg_level;
406: int max_interrupt_work;
407: int intr_enable;
408: int chip_id, drv_flags;
409: struct pci_dev *pci_dev;
410:
411: struct netdev_desc *rx_head_desc;
412: unsigned int cur_rx, dirty_rx; /* Producer/consumer ring indices */
413: unsigned int rx_buf_sz; /* Based on MTU+slack. */
414: int rx_copybreak;
415:
416: unsigned int cur_tx, dirty_tx;
417: unsigned int tx_full:1; /* The Tx queue is full. */
418: unsigned int rx_died:1;
419: unsigned int txrx_config;
420:
421: /* These values keep track of the transceiver/media in use. */
422: unsigned int full_duplex:1; /* Full-duplex operation requested. */
423: unsigned int duplex_lock:1;
424: unsigned int medialock:1; /* Do not sense media. */
425: unsigned int default_port; /* Last dev->if_port value. */
426:
427: unsigned int mcast_filter[2];
428: int multicast_filter_limit;
429:
430: /* MII transceiver section. */
431: int mii_cnt; /* MII device addresses. */
432: u16 advertising; /* NWay media advertisement */
433: unsigned char phys[2]; /* MII device addresses. */
434: };
435:
436: static int eeprom_read(long ioaddr, int location);
437: static int mdio_read(struct net_device *dev, int phy_id,
438: unsigned int location);
439: static void mdio_write(struct net_device *dev, int phy_id,
440: unsigned int location, int value);
441: static int netdev_open(struct net_device *dev);
442: static void check_duplex(struct net_device *dev);
443: static void netdev_timer(unsigned long data);
444: static void tx_timeout(struct net_device *dev);
445: static void init_ring(struct net_device *dev);
446: static int start_tx(struct sk_buff *skb, struct net_device *dev);
447: static void intr_handler(int irq, void *dev_instance, struct pt_regs *regs);
448: static void netdev_error(struct net_device *dev, int intr_status);
449: static int netdev_rx(struct net_device *dev);
450: static void netdev_error(struct net_device *dev, int intr_status);
451: static void set_rx_mode(struct net_device *dev);
452: static struct net_device_stats *get_stats(struct net_device *dev);
453: static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
454: static int netdev_close(struct net_device *dev);
455:
456:
457:
458: /* A list of our installed devices, for removing the driver module. */
459: static struct net_device *root_net_dev = NULL;
460:
461: #ifndef MODULE
462: int myson803_probe(struct net_device *dev)
463: {
464: if (pci_drv_register(&myson803_drv_id, dev) < 0)
465: return -ENODEV;
466: if (debug >= NETIF_MSG_DRV) /* Emit version even if no cards detected. */
467: printk(KERN_INFO "%s" KERN_INFO "%s", version1, version2);
468: return 0;
469: }
470: #endif
471:
472: static void *myson_probe1(struct pci_dev *pdev, void *init_dev,
473: long ioaddr, int irq, int chip_idx, int card_idx)
474: {
475: struct net_device *dev;
476: struct netdev_private *np;
477: void *priv_mem;
478: int i, option = card_idx < MAX_UNITS ? options[card_idx] : 0;
479:
480: dev = init_etherdev(init_dev, 0);
481: if (!dev)
482: return NULL;
483:
484: printk(KERN_INFO "%s: %s at 0x%lx, ",
485: dev->name, pci_id_tbl[chip_idx].name, ioaddr);
486:
487: for (i = 0; i < 3; i++)
488: ((u16 *)dev->dev_addr)[i] = le16_to_cpu(eeprom_read(ioaddr, i + 8));
489: if (memcmp(dev->dev_addr, "\0\0\0\0\0", 6) == 0) {
490: /* Fill a temp addr with the "locally administered" bit set. */
491: memcpy(dev->dev_addr, ">Linux", 6);
492: }
493: for (i = 0; i < 5; i++)
494: printk("%2.2x:", dev->dev_addr[i]);
495: printk("%2.2x, IRQ %d.\n", dev->dev_addr[i], irq);
496:
497: #if ! defined(final_version) /* Dump the EEPROM contents during development. */
498: if (debug > 4)
499: for (i = 0; i < 0x40; i++)
500: printk("%4.4x%s",
501: eeprom_read(ioaddr, i), i % 16 != 15 ? " " : "\n");
502: #endif
503:
504: /* Make certain elements e.g. descriptor lists are aligned. */
505: priv_mem = kmalloc(sizeof(*np) + PRIV_ALIGN, GFP_KERNEL);
506: /* Check for the very unlikely case of no memory. */
507: if (priv_mem == NULL)
508: return NULL;
509:
510: /* Do bogusness checks before this point.
511: We do a request_region() only to register /proc/ioports info. */
512: #ifdef USE_IO_OPS
513: request_region(ioaddr, pci_id_tbl[chip_idx].io_size, dev->name);
514: #endif
515:
516: /* Reset the chip to erase previous misconfiguration. */
517: writel(BCR_Reset, ioaddr + PCIBusCfg);
518:
519: dev->base_addr = ioaddr;
520: dev->irq = irq;
521:
522: dev->priv = np = (void *)(((long)priv_mem + PRIV_ALIGN) & ~PRIV_ALIGN);
523: memset(np, 0, sizeof(*np));
524: np->priv_addr = priv_mem;
525:
526: np->next_module = root_net_dev;
527: root_net_dev = dev;
528:
529: np->pci_dev = pdev;
530: np->chip_id = chip_idx;
531: np->drv_flags = pci_id_tbl[chip_idx].drv_flags;
532: np->msg_level = (1 << debug) - 1;
533: np->rx_copybreak = rx_copybreak;
534: np->max_interrupt_work = max_interrupt_work;
535: np->multicast_filter_limit = multicast_filter_limit;
536:
537: if (dev->mem_start)
538: option = dev->mem_start;
539:
540: /* The lower four bits are the media type. */
541: if (option > 0) {
542: if (option & 0x220)
543: np->full_duplex = 1;
544: np->default_port = option & 0x3ff;
545: if (np->default_port)
546: np->medialock = 1;
547: }
548: if (card_idx < MAX_UNITS && full_duplex[card_idx] > 0)
549: np->full_duplex = 1;
550:
551: if (np->full_duplex) {
552: if (np->msg_level & NETIF_MSG_PROBE)
553: printk(KERN_INFO "%s: Set to forced full duplex, autonegotiation"
554: " disabled.\n", dev->name);
555: np->duplex_lock = 1;
556: }
557:
558: /* The chip-specific entries in the device structure. */
559: dev->open = &netdev_open;
560: dev->hard_start_xmit = &start_tx;
561: dev->stop = &netdev_close;
562: dev->get_stats = &get_stats;
563: dev->set_multicast_list = &set_rx_mode;
564: dev->do_ioctl = &mii_ioctl;
565:
566: if (np->drv_flags & HasMIIXcvr) {
567: int phy, phy_idx = 0;
568: for (phy = 0; phy < 32 && phy_idx < 4; phy++) {
569: int mii_status = mdio_read(dev, phy, 1);
570: if (mii_status != 0xffff && mii_status != 0x0000) {
571: np->phys[phy_idx++] = phy;
572: np->advertising = mdio_read(dev, phy, 4);
573: if (np->msg_level & NETIF_MSG_PROBE)
574: printk(KERN_INFO "%s: MII PHY found at address %d, status "
575: "0x%4.4x advertising %4.4x.\n",
576: dev->name, phy, mii_status, np->advertising);
577: }
578: }
579: np->mii_cnt = phy_idx;
580: }
581: if (np->drv_flags & HasChipXcvr) {
582: np->phys[np->mii_cnt++] = 32;
583: printk(KERN_INFO "%s: Internal PHY status 0x%4.4x"
584: " advertising %4.4x.\n",
585: dev->name, mdio_read(dev, 32, 1), mdio_read(dev, 32, 4));
586: }
587: /* Allow forcing the media type. */
588: if (np->default_port & 0x330) {
589: np->medialock = 1;
590: if (option & 0x220)
591: np->full_duplex = 1;
592: printk(KERN_INFO " Forcing %dMbs %s-duplex operation.\n",
593: (option & 0x300 ? 100 : 10),
594: (np->full_duplex ? "full" : "half"));
595: if (np->mii_cnt)
596: mdio_write(dev, np->phys[0], 0,
597: ((option & 0x300) ? 0x2000 : 0) | /* 100mbps? */
598: (np->full_duplex ? 0x0100 : 0)); /* Full duplex? */
599: }
600:
601: return dev;
602: }
603:
604:
605: /* Read the EEPROM and MII Management Data I/O (MDIO) interfaces. These are
606: often serial bit streams generated by the host processor.
607: The example below is for the common 93c46 EEPROM, 64 16 bit words. */
608:
609: /* This "delay" forces out buffered PCI writes.
610: The udelay() is unreliable for timing, but some Myson NICs shipped with
611: absurdly slow EEPROMs.
612: */
613: #define eeprom_delay(ee_addr) readl(ee_addr); udelay(2); readl(ee_addr)
614:
615: enum EEPROM_Ctrl_Bits {
616: EE_ShiftClk=0x04<<16, EE_ChipSelect=0x88<<16,
617: EE_DataOut=0x02<<16, EE_DataIn=0x01<<16,
618: EE_Write0=0x88<<16, EE_Write1=0x8a<<16,
619: };
620:
621: /* The EEPROM commands always start with 01.. preamble bits.
622: Commands are prepended to the variable-length address. */
623: enum EEPROM_Cmds { EE_WriteCmd=5, EE_ReadCmd=6, EE_EraseCmd=7, };
624:
625: static int eeprom_read(long addr, int location)
626: {
627: int i;
628: int retval = 0;
629: long ee_addr = addr + EECtrl;
630: int read_cmd = location | (EE_ReadCmd<<6);
631:
632: writel(EE_ChipSelect, ee_addr);
633:
634: /* Shift the read command bits out. */
635: for (i = 10; i >= 0; i--) {
636: int dataval = (read_cmd & (1 << i)) ? EE_Write1 : EE_Write0;
637: writel(dataval, ee_addr);
638: eeprom_delay(ee_addr);
639: writel(dataval | EE_ShiftClk, ee_addr);
640: eeprom_delay(ee_addr);
641: }
642: writel(EE_ChipSelect, ee_addr);
643: eeprom_delay(ee_addr);
644:
645: for (i = 16; i > 0; i--) {
646: writel(EE_ChipSelect | EE_ShiftClk, ee_addr);
647: eeprom_delay(ee_addr);
648: retval = (retval << 1) | ((readl(ee_addr) & EE_DataIn) ? 1 : 0);
649: writel(EE_ChipSelect, ee_addr);
650: eeprom_delay(ee_addr);
651: }
652:
653: /* Terminate the EEPROM access. */
654: writel(EE_ChipSelect, ee_addr);
655: writel(0, ee_addr);
656: return retval;
657: }
658:
659: /* MII transceiver control section.
660: Read and write the MII registers using software-generated serial
661: MDIO protocol. See the MII specifications or DP83840A data sheet
662: for details.
663:
664: The maximum data clock rate is 2.5 Mhz.
665: The timing is decoupled from the processor clock by flushing the write
666: from the CPU write buffer with a following read, and using PCI
667: transaction timing. */
668: #define mdio_in(mdio_addr) readl(mdio_addr)
669: #define mdio_out(value, mdio_addr) writel(value, mdio_addr)
670: #define mdio_delay(mdio_addr) readl(mdio_addr)
671:
672: /* Set iff a MII transceiver on any interface requires mdio preamble.
673: This only set with older tranceivers, so the extra
674: code size of a per-interface flag is not worthwhile. */
675: static char mii_preamble_required = 0;
676:
677: enum mii_reg_bits {
678: MDIO_ShiftClk=0x0001, MDIO_Data=0x0002, MDIO_EnbOutput=0x0004,
679: };
680: #define MDIO_EnbIn (0)
681: #define MDIO_WRITE0 (MDIO_EnbOutput)
682: #define MDIO_WRITE1 (MDIO_Data | MDIO_EnbOutput)
683:
684: /* Generate the preamble required for initial synchronization and
685: a few older transceivers. */
686: static void mdio_sync(long mdio_addr)
687: {
688: int bits = 32;
689:
690: /* Establish sync by sending at least 32 logic ones. */
691: while (--bits >= 0) {
692: mdio_out(MDIO_WRITE1, mdio_addr);
693: mdio_delay(mdio_addr);
694: mdio_out(MDIO_WRITE1 | MDIO_ShiftClk, mdio_addr);
695: mdio_delay(mdio_addr);
696: }
697: }
698:
699: static int mdio_read(struct net_device *dev, int phy_id, unsigned int location)
700: {
701: long ioaddr = dev->base_addr;
702: long mdio_addr = ioaddr + MIICtrl;
703: int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;
704: int i, retval = 0;
705:
706: if (location >= 32)
707: return 0xffff;
708: if (phy_id >= 32) {
709: if (location < 6)
710: return readw(ioaddr + PHYMgmt + location*2);
711: else if (location == 16)
712: return readw(ioaddr + PHYMgmt + 6*2);
713: else if (location == 17)
714: return readw(ioaddr + PHYMgmt + 7*2);
715: else if (location == 18)
716: return readw(ioaddr + PHYMgmt + 10*2);
717: else
718: return 0;
719: }
720:
721: if (mii_preamble_required)
722: mdio_sync(mdio_addr);
723:
724: /* Shift the read command bits out. */
725: for (i = 15; i >= 0; i--) {
726: int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
727:
728: mdio_out(dataval, mdio_addr);
729: mdio_delay(mdio_addr);
730: mdio_out(dataval | MDIO_ShiftClk, mdio_addr);
731: mdio_delay(mdio_addr);
732: }
733: /* Read the two transition, 16 data, and wire-idle bits. */
734: for (i = 19; i > 0; i--) {
735: mdio_out(MDIO_EnbIn, mdio_addr);
736: mdio_delay(mdio_addr);
737: retval = (retval << 1) | ((mdio_in(mdio_addr) & MDIO_Data) ? 1 : 0);
738: mdio_out(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
739: mdio_delay(mdio_addr);
740: }
741: return (retval>>1) & 0xffff;
742: }
743:
744: static void mdio_write(struct net_device *dev, int phy_id,
745: unsigned int location, int value)
746: {
747: struct netdev_private *np = (struct netdev_private *)dev->priv;
748: long ioaddr = dev->base_addr;
749: long mdio_addr = ioaddr + MIICtrl;
750: int mii_cmd = (0x5002 << 16) | (phy_id << 23) | (location<<18) | value;
751: int i;
752:
753: if (location == 4 && phy_id == np->phys[0])
754: np->advertising = value;
755: else if (location >= 32)
756: return;
757:
758: if (phy_id == 32) {
759: if (location < 6)
760: writew(value, ioaddr + PHYMgmt + location*2);
761: else if (location == 16)
762: writew(value, ioaddr + PHYMgmt + 6*2);
763: else if (location == 17)
764: writew(value, ioaddr + PHYMgmt + 7*2);
765: return;
766: }
767:
768: if (mii_preamble_required)
769: mdio_sync(mdio_addr);
770:
771: /* Shift the command bits out. */
772: for (i = 31; i >= 0; i--) {
773: int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
774:
775: mdio_out(dataval, mdio_addr);
776: mdio_delay(mdio_addr);
777: mdio_out(dataval | MDIO_ShiftClk, mdio_addr);
778: mdio_delay(mdio_addr);
779: }
780: /* Clear out extra bits. */
781: for (i = 2; i > 0; i--) {
782: mdio_out(MDIO_EnbIn, mdio_addr);
783: mdio_delay(mdio_addr);
784: mdio_out(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
785: mdio_delay(mdio_addr);
786: }
787: return;
788: }
789:
790:
791: static int netdev_open(struct net_device *dev)
792: {
793: struct netdev_private *np = (struct netdev_private *)dev->priv;
794: long ioaddr = dev->base_addr;
795:
796: /* Some chips may need to be reset. */
797:
798: MOD_INC_USE_COUNT;
799:
800: writel(~0, ioaddr + IntrStatus);
801:
802: /* Note that both request_irq() and init_ring() call kmalloc(), which
803: break the global kernel lock protecting this routine. */
804: if (request_irq(dev->irq, &intr_handler, SA_SHIRQ, dev->name, dev)) {
805: MOD_DEC_USE_COUNT;
806: return -EAGAIN;
807: }
808:
809: if (np->msg_level & NETIF_MSG_IFUP)
810: printk(KERN_DEBUG "%s: netdev_open() irq %d.\n",
811: dev->name, dev->irq);
812:
813: init_ring(dev);
814:
815: writel(virt_to_bus(np->rx_ring), ioaddr + RxRingPtr);
816: writel(virt_to_bus(np->tx_ring), ioaddr + TxRingPtr);
817:
818: /* Address register must be written as words. */
819: writel(cpu_to_le32(cpu_to_le32(get_unaligned((u32 *)dev->dev_addr))),
820: ioaddr + StationAddr);
821: writel(cpu_to_le16(cpu_to_le16(get_unaligned((u16 *)(dev->dev_addr+4)))),
822: ioaddr + StationAddr + 4);
823: /* Set the flow control address, 01:80:c2:00:00:01. */
824: writel(0x00c28001, ioaddr + FlowCtrlAddr);
825: writel(0x00000100, ioaddr + FlowCtrlAddr + 4);
826:
827: /* Initialize other registers. */
828: /* Configure the PCI bus bursts and FIFO thresholds. */
829: writel(0x01f8, ioaddr + PCIBusCfg);
830:
831: if (dev->if_port == 0)
832: dev->if_port = np->default_port;
833:
834: np->txrx_config = TxEnable | RxEnable | RxFlowCtrl | 0x00600000;
835: np->mcast_filter[0] = np->mcast_filter[1] = 0;
836: np->rx_died = 0;
837: set_rx_mode(dev);
838: netif_start_tx_queue(dev);
839:
840: /* Enable interrupts by setting the interrupt mask. */
841: np->intr_enable = IntrRxDone | IntrRxErr | IntrRxEmpty | IntrTxDone
842: | IntrTxEmpty | StatsMax | RxOverflow | TxUnderrun | IntrPCIErr
843: | NWayDone | LinkChange;
844: writel(np->intr_enable, ioaddr + IntrEnable);
845:
846: if (np->msg_level & NETIF_MSG_IFUP)
847: printk(KERN_DEBUG "%s: Done netdev_open(), PHY status: %x %x.\n",
848: dev->name, (int)readw(ioaddr + PHYMgmt),
849: (int)readw(ioaddr + PHYMgmt + 2));
850:
851: /* Set the timer to check for link beat. */
852: init_timer(&np->timer);
853: np->timer.expires = jiffies + 3*HZ;
854: np->timer.data = (unsigned long)dev;
855: np->timer.function = &netdev_timer; /* timer handler */
856: add_timer(&np->timer);
857:
858: return 0;
859: }
860:
861: static void check_duplex(struct net_device *dev)
862: {
863: struct netdev_private *np = (struct netdev_private *)dev->priv;
864: long ioaddr = dev->base_addr;
865: int new_tx_mode = np->txrx_config;
866:
867: if (np->medialock) {
868: } else {
869: int mii_reg5 = mdio_read(dev, np->phys[0], 5);
870: int negotiated = mii_reg5 & np->advertising;
871: int duplex = (negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040;
872: if (np->duplex_lock || mii_reg5 == 0xffff)
873: return;
874: if (duplex)
875: new_tx_mode |= TxModeFDX;
876: if (np->full_duplex != duplex) {
877: np->full_duplex = duplex;
878: if (np->msg_level & NETIF_MSG_LINK)
879: printk(KERN_INFO "%s: Setting %s-duplex based on MII #%d"
880: " negotiated capability %4.4x.\n", dev->name,
881: duplex ? "full" : "half", np->phys[0], negotiated);
882: }
883: }
884: if (np->txrx_config != new_tx_mode)
885: writel(new_tx_mode, ioaddr + RxConfig);
886: }
887:
888: static void netdev_timer(unsigned long data)
889: {
890: struct net_device *dev = (struct net_device *)data;
891: struct netdev_private *np = (struct netdev_private *)dev->priv;
892: long ioaddr = dev->base_addr;
893: int next_tick = 10*HZ;
894:
895: if (np->msg_level & NETIF_MSG_TIMER) {
896: printk(KERN_DEBUG "%s: Media selection timer tick, status %8.8x.\n",
897: dev->name, (int)readw(ioaddr + PHYMgmt + 10));
898: }
899: /* This will either have a small false-trigger window or will not catch
900: tbusy incorrectly set when the queue is empty. */
901: if (netif_queue_paused(dev) &&
902: np->cur_tx - np->dirty_tx > 1 &&
903: (jiffies - dev->trans_start) > TX_TIMEOUT) {
904: tx_timeout(dev);
905: }
906: /* It's dead Jim, no race condition. */
907: if (np->rx_died)
908: netdev_rx(dev);
909: check_duplex(dev);
910: np->timer.expires = jiffies + next_tick;
911: add_timer(&np->timer);
912: }
913:
914: static void tx_timeout(struct net_device *dev)
915: {
916: struct netdev_private *np = (struct netdev_private *)dev->priv;
917: long ioaddr = dev->base_addr;
918:
919: printk(KERN_WARNING "%s: Transmit timed out, status %8.8x,"
920: " resetting...\n", dev->name, (int)readl(ioaddr + IntrStatus));
921:
922: if (np->msg_level & NETIF_MSG_TX_ERR) {
923: int i;
924: printk(KERN_DEBUG " Rx ring %p: ", np->rx_ring);
925: for (i = 0; i < RX_RING_SIZE; i++)
926: printk(" %8.8x", (unsigned int)np->rx_ring[i].status);
927: printk("\n"KERN_DEBUG" Tx ring %p: ", np->tx_ring);
928: for (i = 0; i < TX_RING_SIZE; i++)
929: printk(" %8.8x", np->tx_ring[i].status);
930: printk("\n");
931: }
932:
933: /* Stop and restart the chip's Tx processes . */
934: writel(np->txrx_config & ~TxEnable, ioaddr + RxConfig);
935: writel(virt_to_bus(np->tx_ring + (np->dirty_tx%TX_RING_SIZE)),
936: ioaddr + TxRingPtr);
937: writel(np->txrx_config, ioaddr + RxConfig);
938: /* Trigger an immediate transmit demand. */
939: writel(0, dev->base_addr + TxStartDemand);
940:
941: dev->trans_start = jiffies;
942: np->stats.tx_errors++;
943: return;
944: }
945:
946:
947: /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
948: static void init_ring(struct net_device *dev)
949: {
950: struct netdev_private *np = (struct netdev_private *)dev->priv;
951: int i;
952:
953: np->tx_full = 0;
954: np->cur_rx = np->cur_tx = 0;
955: np->dirty_rx = np->dirty_tx = 0;
956:
957: np->rx_buf_sz = (dev->mtu <= 1532 ? PKT_BUF_SZ : dev->mtu + 4);
958: np->rx_head_desc = &np->rx_ring[0];
959:
960: /* Initialize all Rx descriptors. */
961: for (i = 0; i < RX_RING_SIZE; i++) {
962: np->rx_ring[i].ctrl_length = cpu_to_le32(np->rx_buf_sz);
963: np->rx_ring[i].status = 0;
964: np->rx_ring[i].next_desc = virt_to_le32desc(&np->rx_ring[i+1]);
965: np->rx_skbuff[i] = 0;
966: }
967: /* Mark the last entry as wrapping the ring. */
968: np->rx_ring[i-1].next_desc = virt_to_le32desc(&np->rx_ring[0]);
969:
970: /* Fill in the Rx buffers. Handle allocation failure gracefully. */
971: for (i = 0; i < RX_RING_SIZE; i++) {
972: struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz);
973: np->rx_skbuff[i] = skb;
974: if (skb == NULL)
975: break;
976: skb->dev = dev; /* Mark as being used by this device. */
977: np->rx_ring[i].buf_addr = virt_to_le32desc(skb->tail);
978: np->rx_ring[i].status = cpu_to_le32(DescOwn);
979: }
980: np->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
981:
982: for (i = 0; i < TX_RING_SIZE; i++) {
983: np->tx_skbuff[i] = 0;
984: np->tx_ring[i].status = 0;
985: np->tx_ring[i].next_desc = virt_to_le32desc(&np->tx_ring[i+1]);
986: }
987: np->tx_ring[i-1].next_desc = virt_to_le32desc(&np->tx_ring[0]);
988: return;
989: }
990:
991: static int start_tx(struct sk_buff *skb, struct net_device *dev)
992: {
993: struct netdev_private *np = (struct netdev_private *)dev->priv;
994: unsigned entry;
995:
996: /* Block a timer-based transmit from overlapping. This happens when
997: packets are presumed lost, and we use this check the Tx status. */
998: if (netif_pause_tx_queue(dev) != 0) {
999: /* This watchdog code is redundant with the media monitor timer. */
1000: if (jiffies - dev->trans_start > TX_TIMEOUT)
1001: tx_timeout(dev);
1002: return 1;
1003: }
1004:
1005: /* Note: Ordering is important here, set the field with the
1006: "ownership" bit last, and only then increment cur_tx. */
1007:
1008: /* Calculate the next Tx descriptor entry. */
1009: entry = np->cur_tx % TX_RING_SIZE;
1010:
1011: np->tx_skbuff[entry] = skb;
1012:
1013: np->tx_ring[entry].buf_addr = virt_to_le32desc(skb->data);
1014: np->tx_ring[entry].ctrl_length =
1015: cpu_to_le32(TxIntrOnDone | TxNormalPkt | (skb->len << 11) | skb->len);
1016: np->tx_ring[entry].status = cpu_to_le32(DescOwn);
1017: np->cur_tx++;
1018:
1019: /* On some architectures: explicitly flushing cache lines here speeds
1020: operation. */
1021:
1022: if (np->cur_tx - np->dirty_tx >= TX_QUEUE_LEN - 1) {
1023: np->tx_full = 1;
1024: /* Check for a just-cleared queue. */
1025: if (np->cur_tx - (volatile unsigned int)np->dirty_tx
1026: < TX_QUEUE_LEN - 2) {
1027: np->tx_full = 0;
1028: netif_unpause_tx_queue(dev);
1029: } else
1030: netif_stop_tx_queue(dev);
1031: } else
1032: netif_unpause_tx_queue(dev); /* Typical path */
1033: /* Wake the potentially-idle transmit channel. */
1034: writel(0, dev->base_addr + TxStartDemand);
1035:
1036: dev->trans_start = jiffies;
1037:
1038: if (np->msg_level & NETIF_MSG_TX_QUEUED) {
1039: printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n",
1040: dev->name, np->cur_tx, entry);
1041: }
1042: return 0;
1043: }
1044:
1045: /* The interrupt handler does all of the Rx thread work and cleans up
1046: after the Tx thread. */
1047: static void intr_handler(int irq, void *dev_instance, struct pt_regs *rgs)
1048: {
1049: struct net_device *dev = (struct net_device *)dev_instance;
1050: struct netdev_private *np;
1051: long ioaddr;
1052: int boguscnt;
1053:
1054: #ifndef final_version /* Can never occur. */
1055: if (dev == NULL) {
1056: printk (KERN_ERR "Netdev interrupt handler(): IRQ %d for unknown "
1057: "device.\n", irq);
1058: return;
1059: }
1060: #endif
1061:
1062: ioaddr = dev->base_addr;
1063: np = (struct netdev_private *)dev->priv;
1064: boguscnt = np->max_interrupt_work;
1065:
1066: #if defined(__i386__) && LINUX_VERSION_CODE < 0x020300
1067: /* A lock to prevent simultaneous entry bug on Intel SMP machines. */
1068: if (test_and_set_bit(0, (void*)&dev->interrupt)) {
1069: printk(KERN_ERR"%s: SMP simultaneous entry of an interrupt handler.\n",
1070: dev->name);
1071: dev->interrupt = 0; /* Avoid halting machine. */
1072: return;
1073: }
1074: #endif
1075:
1076: do {
1077: u32 intr_status = readl(ioaddr + IntrStatus);
1078:
1079: /* Acknowledge all of the current interrupt sources ASAP. */
1080: writel(intr_status, ioaddr + IntrStatus);
1081:
1082: if (np->msg_level & NETIF_MSG_INTR)
1083: printk(KERN_DEBUG "%s: Interrupt, status %4.4x.\n",
1084: dev->name, intr_status);
1085:
1086: if (intr_status == 0)
1087: break;
1088:
1089: if (intr_status & IntrRxDone)
1090: netdev_rx(dev);
1091:
1092: for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {
1093: int entry = np->dirty_tx % TX_RING_SIZE;
1094: int tx_status = le32_to_cpu(np->tx_ring[entry].status);
1095: if (tx_status & DescOwn)
1096: break;
1097: if (np->msg_level & NETIF_MSG_TX_DONE)
1098: printk(KERN_DEBUG "%s: Transmit done, Tx status %8.8x.\n",
1099: dev->name, tx_status);
1100: if (tx_status & (TxErrAbort | TxErrCarrier | TxErrLate
1101: | TxErr16Colls | TxErrHeartbeat)) {
1102: if (np->msg_level & NETIF_MSG_TX_ERR)
1103: printk(KERN_DEBUG "%s: Transmit error, Tx status %8.8x.\n",
1104: dev->name, tx_status);
1105: np->stats.tx_errors++;
1106: if (tx_status & TxErrCarrier) np->stats.tx_carrier_errors++;
1107: if (tx_status & TxErrLate) np->stats.tx_window_errors++;
1108: if (tx_status & TxErrHeartbeat) np->stats.tx_heartbeat_errors++;
1109: #ifdef ETHER_STATS
1110: if (tx_status & TxErr16Colls) np->stats.collisions16++;
1111: if (tx_status & TxErrAbort) np->stats.tx_aborted_errors++;
1112: #else
1113: if (tx_status & (TxErr16Colls|TxErrAbort))
1114: np->stats.tx_aborted_errors++;
1115: #endif
1116: } else {
1117: np->stats.tx_packets++;
1118: np->stats.collisions += tx_status & TxColls;
1119: #if LINUX_VERSION_CODE > 0x20127
1120: np->stats.tx_bytes += np->tx_skbuff[entry]->len;
1121: #endif
1122: #ifdef ETHER_STATS
1123: if (tx_status & TxErrDefer) np->stats.tx_deferred++;
1124: #endif
1125: }
1126: /* Free the original skb. */
1127: dev_free_skb_irq(np->tx_skbuff[entry]);
1128: np->tx_skbuff[entry] = 0;
1129: }
1130: /* Note the 4 slot hysteresis to mark the queue non-full. */
1131: if (np->tx_full && np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4) {
1132: /* The ring is no longer full, allow new TX entries. */
1133: np->tx_full = 0;
1134: netif_resume_tx_queue(dev);
1135: }
1136:
1137: /* Abnormal error summary/uncommon events handlers. */
1138: if (intr_status & (IntrRxErr | IntrRxEmpty | StatsMax | RxOverflow
1139: | TxUnderrun | IntrPCIErr | NWayDone | LinkChange))
1140: netdev_error(dev, intr_status);
1141:
1142: if (--boguscnt < 0) {
1143: printk(KERN_WARNING "%s: Too much work at interrupt, "
1144: "status=0x%4.4x.\n",
1145: dev->name, intr_status);
1146: break;
1147: }
1148: } while (1);
1149:
1150: if (np->msg_level & NETIF_MSG_INTR)
1151: printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n",
1152: dev->name, (int)readl(ioaddr + IntrStatus));
1153:
1154: #if defined(__i386__) && LINUX_VERSION_CODE < 0x020300
1155: clear_bit(0, (void*)&dev->interrupt);
1156: #endif
1157: return;
1158: }
1159:
1160: /* This routine is logically part of the interrupt handler, but separated
1161: for clarity and better register allocation. */
1162: static int netdev_rx(struct net_device *dev)
1163: {
1164: struct netdev_private *np = (struct netdev_private *)dev->priv;
1165: int entry = np->cur_rx % RX_RING_SIZE;
1166: int boguscnt = np->dirty_rx + RX_RING_SIZE - np->cur_rx;
1167: int refilled = 0;
1168:
1169: if (np->msg_level & NETIF_MSG_RX_STATUS) {
1170: printk(KERN_DEBUG " In netdev_rx(), entry %d status %4.4x.\n",
1171: entry, np->rx_ring[entry].status);
1172: }
1173:
1174: /* If EOP is set on the next entry, it's a new packet. Send it up. */
1175: while ( ! (np->rx_head_desc->status & cpu_to_le32(DescOwn))) {
1176: struct netdev_desc *desc = np->rx_head_desc;
1177: u32 desc_status = le32_to_cpu(desc->status);
1178:
1179: if (np->msg_level & NETIF_MSG_RX_STATUS)
1180: printk(KERN_DEBUG " netdev_rx() status was %8.8x.\n",
1181: desc_status);
1182: if (--boguscnt < 0)
1183: break;
1184: if ((desc_status & RxDescWholePkt) != RxDescWholePkt) {
1185: printk(KERN_WARNING "%s: Oversized Ethernet frame spanned "
1186: "multiple buffers, entry %#x length %d status %4.4x!\n",
1187: dev->name, np->cur_rx, desc_status >> 16, desc_status);
1188: np->stats.rx_length_errors++;
1189: } else if (desc_status & RxDescErrSum) {
1190: /* There was a error. */
1191: if (np->msg_level & NETIF_MSG_RX_ERR)
1192: printk(KERN_DEBUG " netdev_rx() Rx error was %8.8x.\n",
1193: desc_status);
1194: np->stats.rx_errors++;
1195: if (desc_status & (RxErrLong|RxErrRunt))
1196: np->stats.rx_length_errors++;
1197: if (desc_status & (RxErrFrame|RxErrCode))
1198: np->stats.rx_frame_errors++;
1199: if (desc_status & RxErrCRC)
1200: np->stats.rx_crc_errors++;
1201: } else {
1202: struct sk_buff *skb;
1203: /* Reported length should omit the CRC. */
1204: u16 pkt_len = ((desc_status >> 16) & 0xfff) - 4;
1205:
1206: #ifndef final_version
1207: if (np->msg_level & NETIF_MSG_RX_STATUS)
1208: printk(KERN_DEBUG " netdev_rx() normal Rx pkt length %d"
1209: " of %d, bogus_cnt %d.\n",
1210: pkt_len, pkt_len, boguscnt);
1211: #endif
1212: /* Check if the packet is long enough to accept without copying
1213: to a minimally-sized skbuff. */
1214: if (pkt_len < np->rx_copybreak
1215: && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
1216: skb->dev = dev;
1217: skb_reserve(skb, 2); /* 16 byte align the IP header */
1218: eth_copy_and_sum(skb, np->rx_skbuff[entry]->tail, pkt_len, 0);
1219: skb_put(skb, pkt_len);
1220: } else {
1221: skb_put(skb = np->rx_skbuff[entry], pkt_len);
1222: np->rx_skbuff[entry] = NULL;
1223: }
1224: #ifndef final_version /* Remove after testing. */
1225: /* You will want this info for the initial debug. */
1226: if (np->msg_level & NETIF_MSG_PKTDATA)
1227: printk(KERN_DEBUG " Rx data %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:"
1228: "%2.2x %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x %2.2x%2.2x "
1229: "%d.%d.%d.%d.\n",
1230: skb->data[0], skb->data[1], skb->data[2], skb->data[3],
1231: skb->data[4], skb->data[5], skb->data[6], skb->data[7],
1232: skb->data[8], skb->data[9], skb->data[10],
1233: skb->data[11], skb->data[12], skb->data[13],
1234: skb->data[14], skb->data[15], skb->data[16],
1235: skb->data[17]);
1236: #endif
1237: skb->mac.raw = skb->data;
1238: /* Protocol lookup disabled until verified with all kernels. */
1239: if (0 && ntohs(skb->mac.ethernet->h_proto) >= 0x0800) {
1240: struct ethhdr *eth = skb->mac.ethernet;
1241: skb->protocol = eth->h_proto;
1242: if (desc_status & 0x1000) {
1243: if ((dev->flags & IFF_PROMISC) &&
1244: memcmp(eth->h_dest, dev->dev_addr, ETH_ALEN))
1245: skb->pkt_type = PACKET_OTHERHOST;
1246: } else if (desc_status & 0x2000)
1247: skb->pkt_type = PACKET_BROADCAST;
1248: else if (desc_status & 0x4000)
1249: skb->pkt_type = PACKET_MULTICAST;
1250: } else
1251: skb->protocol = eth_type_trans(skb, dev);
1252: netif_rx(skb);
1253: dev->last_rx = jiffies;
1254: np->stats.rx_packets++;
1255: #if LINUX_VERSION_CODE > 0x20127
1256: np->stats.rx_bytes += pkt_len;
1257: #endif
1258: }
1259: entry = (++np->cur_rx) % RX_RING_SIZE;
1260: np->rx_head_desc = &np->rx_ring[entry];
1261: }
1262:
1263: /* Refill the Rx ring buffers. */
1264: for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {
1265: struct sk_buff *skb;
1266: entry = np->dirty_rx % RX_RING_SIZE;
1267: if (np->rx_skbuff[entry] == NULL) {
1268: skb = dev_alloc_skb(np->rx_buf_sz);
1269: np->rx_skbuff[entry] = skb;
1270: if (skb == NULL)
1271: break; /* Better luck next round. */
1272: skb->dev = dev; /* Mark as being used by this device. */
1273: np->rx_ring[entry].buf_addr = virt_to_le32desc(skb->tail);
1274: }
1275: np->rx_ring[entry].ctrl_length = cpu_to_le32(np->rx_buf_sz);
1276: np->rx_ring[entry].status = cpu_to_le32(DescOwn);
1277: refilled++;
1278: }
1279:
1280: /* Restart Rx engine if stopped. */
1281: if (refilled) { /* Perhaps "&& np->rx_died" */
1282: writel(0, dev->base_addr + RxStartDemand);
1283: np->rx_died = 0;
1284: }
1285: return refilled;
1286: }
1287:
1288: static void netdev_error(struct net_device *dev, int intr_status)
1289: {
1290: struct netdev_private *np = (struct netdev_private *)dev->priv;
1291: long ioaddr = dev->base_addr;
1292:
1293: if (intr_status & (LinkChange | NWayDone)) {
1294: if (np->msg_level & NETIF_MSG_LINK)
1295: printk(KERN_NOTICE "%s: Link changed: Autonegotiation advertising"
1296: " %4.4x partner %4.4x.\n", dev->name,
1297: mdio_read(dev, np->phys[0], 4),
1298: mdio_read(dev, np->phys[0], 5));
1299: /* Clear sticky bit first. */
1300: readw(ioaddr + PHYMgmt + 2);
1301: if (readw(ioaddr + PHYMgmt + 2) & 0x0004)
1302: netif_link_up(dev);
1303: else
1304: netif_link_down(dev);
1305: check_duplex(dev);
1306: }
1307: if ((intr_status & TxUnderrun)
1308: && (np->txrx_config & TxThreshold) != TxThreshold) {
1309: np->txrx_config += TxThresholdInc;
1310: writel(np->txrx_config, ioaddr + RxConfig);
1311: np->stats.tx_fifo_errors++;
1312: }
1313: if (intr_status & IntrRxEmpty) {
1314: printk(KERN_WARNING "%s: Out of receive buffers: no free memory.\n",
1315: dev->name);
1316: /* Refill Rx descriptors */
1317: np->rx_died = 1;
1318: netdev_rx(dev);
1319: }
1320: if (intr_status & RxOverflow) {
1321: printk(KERN_WARNING "%s: Receiver overflow.\n", dev->name);
1322: np->stats.rx_over_errors++;
1323: netdev_rx(dev); /* Refill Rx descriptors */
1324: get_stats(dev); /* Empty dropped counter. */
1325: }
1326: if (intr_status & StatsMax) {
1327: get_stats(dev);
1328: }
1329: if ((intr_status & ~(LinkChange|NWayDone|StatsMax|TxUnderrun|RxOverflow
1330: |TxEarly|RxEarly|0x001e))
1331: && (np->msg_level & NETIF_MSG_DRV))
1332: printk(KERN_ERR "%s: Something Wicked happened! %4.4x.\n",
1333: dev->name, intr_status);
1334: /* Hmmmmm, it's not clear how to recover from PCI faults. */
1335: if (intr_status & IntrPCIErr) {
1336: const char *const pcierr[4] =
1337: { "Parity Error", "Master Abort", "Target Abort", "Unknown Error" };
1338: if (np->msg_level & NETIF_MSG_DRV)
1339: printk(KERN_WARNING "%s: PCI Bus %s, %x.\n",
1340: dev->name, pcierr[(intr_status>>11) & 3], intr_status);
1341: }
1342: }
1343:
1344: /* We do not bother to spinlock statistics.
1345: A window only exists if we have non-atomic adds, the error counts are
1346: typically zero, and statistics are non-critical. */
1347: static struct net_device_stats *get_stats(struct net_device *dev)
1348: {
1349: long ioaddr = dev->base_addr;
1350: struct netdev_private *np = (struct netdev_private *)dev->priv;
1351: unsigned int rxerrs = readl(ioaddr + RxErrCnts);
1352: unsigned int txerrs = readl(ioaddr + TxErrCnts);
1353:
1354: /* The chip only need report frames silently dropped. */
1355: np->stats.rx_crc_errors += rxerrs >> 16;
1356: np->stats.rx_missed_errors += rxerrs & 0xffff;
1357:
1358: /* These stats are required when the descriptor is closed before Tx. */
1359: np->stats.tx_aborted_errors += txerrs >> 24;
1360: np->stats.tx_window_errors += (txerrs >> 16) & 0xff;
1361: np->stats.collisions += txerrs & 0xffff;
1362:
1363: return &np->stats;
1364: }
1365:
1366: /* Big-endian AUTODIN II ethernet CRC calculations.
1367: This is slow but compact code. Do not use this routine for bulk data,
1368: use a table-based routine instead.
1369: This is common code and may be in the kernel with Linux 2.5+.
1370: */
1371: static unsigned const ethernet_polynomial = 0x04c11db7U;
1372: static inline u32 ether_crc(int length, unsigned char *data)
1373: {
1374: u32 crc = ~0;
1375:
1376: while(--length >= 0) {
1377: unsigned char current_octet = *data++;
1378: int bit;
1379: for (bit = 0; bit < 8; bit++, current_octet >>= 1)
1380: crc = (crc << 1) ^
1381: ((crc < 0) ^ (current_octet & 1) ? ethernet_polynomial : 0);
1382: }
1383: return crc;
1384: }
1385:
1386: static void set_rx_mode(struct net_device *dev)
1387: {
1388: struct netdev_private *np = (struct netdev_private *)dev->priv;
1389: long ioaddr = dev->base_addr;
1390: u32 mc_filter[2]; /* Multicast hash filter */
1391: u32 rx_mode;
1392:
1393: if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
1394: /* Unconditionally log net taps. */
1395: printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);
1396: mc_filter[1] = mc_filter[0] = ~0;
1397: rx_mode = AcceptBroadcast | AcceptMulticast | AcceptAllPhys
1398: | AcceptMyPhys;
1399: } else if ((dev->mc_count > np->multicast_filter_limit)
1400: || (dev->flags & IFF_ALLMULTI)) {
1401: /* Too many to match, or accept all multicasts. */
1402: mc_filter[1] = mc_filter[0] = ~0;
1403: rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
1404: } else {
1405: struct dev_mc_list *mclist;
1406: int i;
1407: mc_filter[1] = mc_filter[0] = 0;
1408: for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
1409: i++, mclist = mclist->next) {
1410: set_bit((ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26) & 0x3f,
1411: mc_filter);
1412: }
1413: rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
1414: }
1415: if (mc_filter[0] != np->mcast_filter[0] ||
1416: mc_filter[1] != np->mcast_filter[1]) {
1417: writel(mc_filter[0], ioaddr + MulticastFilter0);
1418: writel(mc_filter[1], ioaddr + MulticastFilter1);
1419: np->mcast_filter[0] = mc_filter[0];
1420: np->mcast_filter[1] = mc_filter[1];
1421: }
1422: if ((np->txrx_config & RxFilter) != rx_mode) {
1423: np->txrx_config &= ~RxFilter;
1424: np->txrx_config |= rx_mode;
1425: writel(np->txrx_config, ioaddr + RxConfig);
1426: }
1427: }
1428:
1429: /*
1430: Handle user-level ioctl() calls.
1431: We must use two numeric constants as the key because some clueless person
1432: changed the value for the symbolic name.
1433: */
1434: static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1435: {
1436: struct netdev_private *np = (struct netdev_private *)dev->priv;
1437: u16 *data = (u16 *)&rq->ifr_data;
1438: u32 *data32 = (void *)&rq->ifr_data;
1439:
1440: switch(cmd) {
1441: case 0x8947: case 0x89F0:
1442: /* SIOCGMIIPHY: Get the address of the PHY in use. */
1443: data[0] = np->phys[0];
1444: /* Fall Through */
1445: case 0x8948: case 0x89F1:
1446: /* SIOCGMIIREG: Read the specified MII register. */
1447: data[3] = mdio_read(dev, data[0], data[1]);
1448: return 0;
1449: case 0x8949: case 0x89F2:
1450: /* SIOCSMIIREG: Write the specified MII register */
1451: if (!capable(CAP_NET_ADMIN))
1452: return -EPERM;
1453: if (data[0] == np->phys[0]) {
1454: u16 value = data[2];
1455: switch (data[1]) {
1456: case 0:
1457: /* Check for autonegotiation on or reset. */
1458: np->medialock = (value & 0x9000) ? 0 : 1;
1459: if (np->medialock)
1460: np->full_duplex = (value & 0x0100) ? 1 : 0;
1461: break;
1462: case 4: np->advertising = value; break;
1463: }
1464: /* Perhaps check_duplex(dev), depending on chip semantics. */
1465: }
1466: mdio_write(dev, data[0], data[1], data[2]);
1467: return 0;
1468: case SIOCGPARAMS:
1469: data32[0] = np->msg_level;
1470: data32[1] = np->multicast_filter_limit;
1471: data32[2] = np->max_interrupt_work;
1472: data32[3] = np->rx_copybreak;
1473: return 0;
1474: case SIOCSPARAMS:
1475: if (!capable(CAP_NET_ADMIN))
1476: return -EPERM;
1477: np->msg_level = data32[0];
1478: np->multicast_filter_limit = data32[1];
1479: np->max_interrupt_work = data32[2];
1480: np->rx_copybreak = data32[3];
1481: return 0;
1482: default:
1483: return -EOPNOTSUPP;
1484: }
1485: }
1486:
1487: static int netdev_close(struct net_device *dev)
1488: {
1489: long ioaddr = dev->base_addr;
1490: struct netdev_private *np = (struct netdev_private *)dev->priv;
1491: int i;
1492:
1493: netif_stop_tx_queue(dev);
1494:
1495: if (np->msg_level & NETIF_MSG_IFDOWN) {
1496: printk(KERN_DEBUG "%s: Shutting down ethercard, status was %8.8x.\n",
1497: dev->name, (int)readl(ioaddr + RxConfig));
1498: printk(KERN_DEBUG "%s: Queue pointers were Tx %d / %d, Rx %d / %d.\n",
1499: dev->name, np->cur_tx, np->dirty_tx, np->cur_rx, np->dirty_rx);
1500: }
1501:
1502: /* Disable interrupts by clearing the interrupt mask. */
1503: writel(0x0000, ioaddr + IntrEnable);
1504:
1505: /* Stop the chip's Tx and Rx processes. */
1506: np->txrx_config = 0;
1507: writel(0, ioaddr + RxConfig);
1508:
1509: del_timer(&np->timer);
1510:
1511: #ifdef __i386__
1512: if (np->msg_level & NETIF_MSG_IFDOWN) {
1513: printk("\n"KERN_DEBUG" Tx ring at %8.8x:\n",
1514: (int)virt_to_bus(np->tx_ring));
1515: for (i = 0; i < TX_RING_SIZE; i++)
1516: printk(" #%d desc. %x %x %8.8x.\n",
1517: i, np->tx_ring[i].status, np->tx_ring[i].ctrl_length,
1518: np->tx_ring[i].buf_addr);
1519: printk("\n"KERN_DEBUG " Rx ring %8.8x:\n",
1520: (int)virt_to_bus(np->rx_ring));
1521: for (i = 0; i < RX_RING_SIZE; i++) {
1522: printk(KERN_DEBUG " #%d desc. %4.4x %4.4x %8.8x\n",
1523: i, np->rx_ring[i].status, np->rx_ring[i].ctrl_length,
1524: np->rx_ring[i].buf_addr);
1525: }
1526: }
1527: #endif /* __i386__ debugging only */
1528:
1529: free_irq(dev->irq, dev);
1530:
1531: /* Free all the skbuffs in the Rx queue. */
1532: for (i = 0; i < RX_RING_SIZE; i++) {
1533: np->rx_ring[i].status = 0;
1534: np->rx_ring[i].buf_addr = 0xBADF00D0; /* An invalid address. */
1535: if (np->rx_skbuff[i]) {
1536: #if LINUX_VERSION_CODE < 0x20100
1537: np->rx_skbuff[i]->free = 1;
1538: #endif
1539: dev_free_skb(np->rx_skbuff[i]);
1540: }
1541: np->rx_skbuff[i] = 0;
1542: }
1543: for (i = 0; i < TX_RING_SIZE; i++) {
1544: if (np->tx_skbuff[i])
1545: dev_free_skb(np->tx_skbuff[i]);
1546: np->tx_skbuff[i] = 0;
1547: }
1548:
1549: MOD_DEC_USE_COUNT;
1550:
1551: return 0;
1552: }
1553:
1554: static int netdev_pwr_event(void *dev_instance, int event)
1555: {
1556: struct net_device *dev = dev_instance;
1557: struct netdev_private *np = (struct netdev_private *)dev->priv;
1558: long ioaddr = dev->base_addr;
1559:
1560: if (np->msg_level & NETIF_MSG_LINK)
1561: printk(KERN_DEBUG "%s: Handling power event %d.\n", dev->name, event);
1562: switch(event) {
1563: case DRV_ATTACH:
1564: MOD_INC_USE_COUNT;
1565: break;
1566: case DRV_SUSPEND:
1567: /* Disable interrupts, stop Tx and Rx. */
1568: writel(0, ioaddr + IntrEnable);
1569: writel(0, ioaddr + RxConfig);
1570: break;
1571: case DRV_RESUME:
1572: /* This is incomplete: the actions are very chip specific. */
1573: set_rx_mode(dev);
1574: writel(np->intr_enable, ioaddr + IntrEnable);
1575: break;
1576: case DRV_DETACH: {
1577: struct net_device **devp, **next;
1578: if (dev->flags & IFF_UP) {
1579: /* Some, but not all, kernel versions close automatically. */
1580: dev_close(dev);
1581: dev->flags &= ~(IFF_UP|IFF_RUNNING);
1582: }
1583: unregister_netdev(dev);
1584: release_region(dev->base_addr, pci_id_tbl[np->chip_id].io_size);
1585: #ifndef USE_IO_OPS
1586: iounmap((char *)dev->base_addr);
1587: #endif
1588: for (devp = &root_net_dev; *devp; devp = next) {
1589: next = &((struct netdev_private *)(*devp)->priv)->next_module;
1590: if (*devp == dev) {
1591: *devp = *next;
1592: break;
1593: }
1594: }
1595: if (np->priv_addr)
1596: kfree(np->priv_addr);
1597: kfree(dev);
1598: MOD_DEC_USE_COUNT;
1599: break;
1600: }
1601: }
1602:
1603: return 0;
1604: }
1605:
1606:
1607: #ifdef MODULE
1608: int init_module(void)
1609: {
1610: if (debug >= NETIF_MSG_DRV) /* Emit version even if no cards detected. */
1611: printk(KERN_INFO "%s" KERN_INFO "%s", version1, version2);
1612: return pci_drv_register(&myson803_drv_id, NULL);
1613: }
1614:
1615: void cleanup_module(void)
1616: {
1617: struct net_device *next_dev;
1618:
1619: pci_drv_unregister(&myson803_drv_id);
1620:
1621: /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1622: while (root_net_dev) {
1623: struct netdev_private *np = (void *)(root_net_dev->priv);
1624: unregister_netdev(root_net_dev);
1625: #ifdef USE_IO_OPS
1626: release_region(root_net_dev->base_addr,
1627: pci_id_tbl[np->chip_id].io_size);
1628: #else
1629: iounmap((char *)(root_net_dev->base_addr));
1630: #endif
1631: next_dev = np->next_module;
1632: if (np->priv_addr)
1633: kfree(np->priv_addr);
1634: kfree(root_net_dev);
1635: root_net_dev = next_dev;
1636: }
1637: }
1638:
1639: #endif /* MODULE */
1640:
1641: /*
1642: * Local variables:
1643: * compile-command: "make KERNVER=`uname -r` myson803.o"
1644: * compile-cmd: "gcc -DMODULE -Wall -Wstrict-prototypes -O6 -c myson803.c"
1645: * simple-compile-command: "gcc -DMODULE -O6 -c myson803.c"
1646: * c-indent-level: 4
1647: * c-basic-offset: 4
1648: * tab-width: 4
1649: * End:
1650: */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.