|
|
1.1 root 1: /* sundance.c: A Linux device driver for the Sundance ST201 "Alta". */
2: /*
3: Written 1999-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/sundance.html
19: */
20:
21: /* These identify the driver base version and may not be removed. */
22: static const char version1[] =
23: "sundance.c:v1.11 2/4/2003 Written by Donald Becker <[email protected]>\n";
24: static const char version2[] =
25: " http://www.scyld.com/network/sundance.html\n";
26: /* Updated to recommendations in pci-skeleton v2.12. */
27:
28: /* Automatically extracted configuration info:
29: probe-func: sundance_probe
30: config-in: tristate 'Sundance ST201 "Alta" PCI Ethernet support' CONFIG_SUNDANCE
31: c-help-name: Sundance ST201 "Alta" PCI Ethernet support
32: c-help-symbol: CONFIG_SUNDANCE
33: c-help: This driver is for the Sundance ST201 "Alta" and Kendin KS8723, as
34: c-help: used on the D-Link DFE-550 and DFE-580.
35: c-help: Design information, usage details and updates are available from
36: c-help: http://www.scyld.com/network/sundance.html
37: */
38:
39: /* The user-configurable values.
40: These may be modified when a driver module is loaded.*/
41:
42: /* Message enable level: 0..31 = no..all messages. See NETIF_MSG docs. */
43: static int debug = 2;
44:
45: /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
46: static int max_interrupt_work = 20;
47:
48: /* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
49: The sundance uses a 64 element hash table based on the Ethernet CRC. */
50: static int multicast_filter_limit = 32;
51:
52: /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
53: Setting to > 1518 effectively disables this feature.
54: This chip can receive into any byte alignment buffers, so word-oriented
55: archs do not need a copy-align of the IP header. */
56: static int rx_copybreak = 0;
57:
58: /* Used to pass the media type, etc.
59: Both 'options[]' and 'full_duplex[]' should exist for driver
60: interoperability.
61: The media type is usually passed in 'options[]'.
62: The default is autonegotation for speed and duplex.
63: This should rarely be overridden.
64: Use option values 0x10/0x20 for 10Mbps, 0x100,0x200 for 100Mbps.
65: Use option values 0x10 and 0x100 for forcing half duplex fixed speed.
66: Use option values 0x20 and 0x200 for forcing full duplex operation.
67: */
68: #define MAX_UNITS 8 /* More are supported, limit only on options */
69: static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
70: static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
71:
72: /* Operational parameters that are set at compile time. */
73:
74: /* Ring sizes are a power of two only for compile efficiency.
75: The compiler will convert <unsigned>'%'<2^N> into a bit mask.
76: There must be at least five Tx entries for the tx_full hysteresis, and
77: more than 31 requires modifying the Tx status handling error recovery.
78: Leave a inactive gap in the Tx ring for better cache behavior.
79: Making the Tx ring too large decreases the effectiveness of channel
80: bonding and packet priority.
81: Large receive rings waste memory and impact buffer accounting.
82: The driver need to protect against interrupt latency and the kernel
83: not reserving enough available memory.
84: */
85: #define TX_RING_SIZE 16
86: #define TX_QUEUE_LEN 10 /* Limit ring entries actually used. */
87: #define RX_RING_SIZE 32
88:
89: /* Operational parameters that usually are not changed. */
90: /* Time in jiffies before concluding the transmitter is hung. */
91: #define TX_TIMEOUT (6*HZ)
92:
93: /* Allocation size of Rx buffers with normal sized Ethernet frames.
94: Do not change this value without good reason. This is not a limit,
95: but a way to keep a consistent allocation size among drivers.
96: */
97: #define PKT_BUF_SZ 1536
98:
99: /* Set iff a MII transceiver on any interface requires mdio preamble.
100: This only set with older tranceivers, so the extra
101: code size of a per-interface flag is not worthwhile. */
102: static char mii_preamble_required = 0;
103:
104: #ifndef __KERNEL__
105: #define __KERNEL__
106: #endif
107: #if !defined(__OPTIMIZE__)
108: #warning You must compile this file with the correct options!
109: #warning See the last lines of the source file.
110: #error You must compile this driver with "-O".
111: #endif
112:
113: /* Include files, designed to support most kernel versions 2.0.0 and later. */
114: #include <linux/config.h>
115: #if defined(CONFIG_SMP) && ! defined(__SMP__)
116: #define __SMP__
117: #endif
118: #if defined(MODULE) && defined(CONFIG_MODVERSIONS) && ! defined(MODVERSIONS)
119: #define MODVERSIONS
120: #endif
121:
122: #include <linux/version.h>
123: #if defined(MODVERSIONS)
124: #include <linux/modversions.h>
125: #endif
126: #include <linux/module.h>
127:
128: #include <linux/kernel.h>
129: #include <linux/string.h>
130: #include <linux/timer.h>
131: #include <linux/errno.h>
132: #include <linux/ioport.h>
133: #if LINUX_VERSION_CODE >= 0x20400
134: #include <linux/slab.h>
135: #else
136: #include <linux/malloc.h>
137: #endif
138: #include <linux/interrupt.h>
139: #include <linux/pci.h>
140: #include <linux/netdevice.h>
141: #include <linux/etherdevice.h>
142: #include <linux/skbuff.h>
143: #include <asm/bitops.h>
144: #include <asm/io.h>
145:
146: #if LINUX_VERSION_CODE >= 0x20300
147: #include <linux/spinlock.h>
148: #elif LINUX_VERSION_CODE >= 0x20200
149: #include <asm/spinlock.h>
150: #endif
151:
152: #ifdef INLINE_PCISCAN
153: #include "k_compat.h"
154: #else
155: #include "pci-scan.h"
156: #include "kern_compat.h"
157: #endif
158:
159: /* Condensed operations for readability. */
160: #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
161: #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
162:
163: #if (LINUX_VERSION_CODE >= 0x20100) && defined(MODULE)
164: char kernel_version[] = UTS_RELEASE;
165: #endif
166:
167: MODULE_AUTHOR("Donald Becker <[email protected]>");
168: MODULE_DESCRIPTION("Sundance Alta Ethernet driver");
169: MODULE_LICENSE("GPL");
170: MODULE_PARM(max_interrupt_work, "i");
171: MODULE_PARM(debug, "i");
172: MODULE_PARM(rx_copybreak, "i");
173: MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
174: MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
175: MODULE_PARM(multicast_filter_limit, "i");
176:
177: MODULE_PARM_DESC(debug, "Driver message level (0-31)");
178: MODULE_PARM_DESC(options, "Force transceiver type or fixed speed+duplex");
179: MODULE_PARM_DESC(max_interrupt_work,
180: "Driver maximum events handled per interrupt");
181: MODULE_PARM_DESC(full_duplex,
182: "Non-zero to set forced full duplex (deprecated).");
183: MODULE_PARM_DESC(rx_copybreak,
184: "Breakpoint in bytes for copy-only-tiny-frames");
185: MODULE_PARM_DESC(multicast_filter_limit,
186: "Multicast addresses before switching to Rx-all-multicast");
187:
188: /*
189: Theory of Operation
190:
191: I. Board Compatibility
192:
193: This driver is designed for the Sundance Technologies "Alta" ST201 chip.
194: The Kendin KS8723 is the same design with an integrated transceiver and
195: new quirks.
196:
197: II. Board-specific settings
198:
199: This is an all-in-one chip, so there are no board-specific settings.
200:
201: III. Driver operation
202:
203: IIIa. Ring buffers
204:
205: This driver uses two statically allocated fixed-size descriptor lists
206: formed into rings by a branch from the final descriptor to the beginning of
207: the list. The ring sizes are set at compile time by RX/TX_RING_SIZE.
208: Some chips explicitly use only 2^N sized rings, while others use a
209: 'next descriptor' pointer that the driver forms into rings.
210:
211: IIIb/c. Transmit/Receive Structure
212:
213: This driver uses a zero-copy receive and transmit scheme.
214: The driver allocates full frame size skbuffs for the Rx ring buffers at
215: open() time and passes the skb->data field to the chip as receive data
216: buffers. When an incoming frame is less than RX_COPYBREAK bytes long,
217: a fresh skbuff is allocated and the frame is copied to the new skbuff.
218: When the incoming frame is larger, the skbuff is passed directly up the
219: protocol stack. Buffers consumed this way are replaced by newly allocated
220: skbuffs in a later phase of receives.
221:
222: The RX_COPYBREAK value is chosen to trade-off the memory wasted by
223: using a full-sized skbuff for small frames vs. the copying costs of larger
224: frames. New boards are typically used in generously configured machines
225: and the underfilled buffers have negligible impact compared to the benefit of
226: a single allocation size, so the default value of zero results in never
227: copying packets. When copying is done, the cost is usually mitigated by using
228: a combined copy/checksum routine. Copying also preloads the cache, which is
229: most useful with small frames.
230:
231: A subtle aspect of the operation is that the IP header at offset 14 in an
232: ethernet frame isn't longword aligned for further processing.
233: Unaligned buffers are permitted by the Sundance hardware, so
234: frames are received into the skbuff at an offset of "+2", 16-byte aligning
235: the IP header.
236:
237: IIId. Synchronization
238:
239: The driver runs as two independent, single-threaded flows of control. One
240: is the send-packet routine, which enforces single-threaded use by the
241: dev->tbusy flag. The other thread is the interrupt handler, which is single
242: threaded by the hardware and interrupt handling software.
243:
244: The send packet thread has partial control over the Tx ring and 'dev->tbusy'
245: flag. It sets the tbusy flag whenever it's queuing a Tx packet. If the next
246: queue slot is empty, it clears the tbusy flag when finished otherwise it sets
247: the 'lp->tx_full' flag.
248:
249: The interrupt handler has exclusive control over the Rx ring and records stats
250: from the Tx ring. After reaping the stats, it marks the Tx queue entry as
251: empty by incrementing the dirty_tx mark. Iff the 'lp->tx_full' flag is set, it
252: clears both the tx_full and tbusy flags.
253:
254: IV. Notes
255:
256: IVb. References
257:
258: The Sundance ST201 datasheet, preliminary version.
259: The Kendin KS8723 datasheet, preliminary version.
260: http://www.scyld.com/expert/100mbps.html
261: http://www.scyld.com/expert/NWay.html
262:
263: IVc. Errata
264:
265: */
266:
267:
268:
269: /* Work-around for Kendin chip bugs. This will be reversed after tracking
270: down all of the chip access quirks in memory mode. */
271: #ifndef USE_MEM_OPS
272: #define USE_IO_OPS 1
273: #endif
274:
275: static void *sundance_probe1(struct pci_dev *pdev, void *init_dev,
276: long ioaddr, int irq, int chip_idx, int find_cnt);
277: static int sundance_pwr_event(void *dev_instance, int event);
278:
279: enum chip_capability_flags {CanHaveMII=1, KendinPktDropBug=2, };
280: #ifdef USE_IO_OPS
281: #define PCI_IOTYPE (PCI_USES_MASTER | PCI_USES_IO | PCI_ADDR0)
282: #else
283: #define PCI_IOTYPE (PCI_USES_MASTER | PCI_USES_MEM | PCI_ADDR1)
284: #endif
285:
286: static struct pci_id_info pci_id_tbl[] = {
287: {"D-Link DFE-580TX (Kendin/Sundance ST201 Alta)",
288: {0x10021186, 0xffffffff, 0x10121186, 0xffffffff, 0x14, 0xff},
289: PCI_IOTYPE, 128, CanHaveMII|KendinPktDropBug},
290: {"D-Link DFE-580TX (Sundance ST201)",
291: {0x10021186, 0xffffffff, 0x10121186, 0xffffffff, },
292: PCI_IOTYPE, 128, CanHaveMII|KendinPktDropBug},
293: {"D-Link DFE-550FX 100baseFx (Sundance ST201)",
294: {0x10031186, 0xffffffff, },
295: PCI_IOTYPE, 128, CanHaveMII|KendinPktDropBug},
296: {"OEM Sundance Technology ST201", {0x10021186, 0xffffffff, },
297: PCI_IOTYPE, 128, CanHaveMII},
298: {"Sundance Technology Alta", {0x020113F0, 0xffffffff, },
299: PCI_IOTYPE, 128, CanHaveMII},
300: {0,}, /* 0 terminated list. */
301: };
302:
303: struct drv_id_info sundance_drv_id = {
304: "sundance", PCI_HOTSWAP, PCI_CLASS_NETWORK_ETHERNET<<8, pci_id_tbl,
305: sundance_probe1, sundance_pwr_event };
306:
307: /* This driver was written to use PCI memory space, however x86-oriented
308: hardware often uses I/O space accesses. */
309: #ifdef USE_IO_OPS
310: #undef readb
311: #undef readw
312: #undef readl
313: #undef writeb
314: #undef writew
315: #undef writel
316: #define readb inb
317: #define readw inw
318: #define readl inl
319: #define writeb outb
320: #define writew outw
321: #define writel outl
322: #endif
323:
324: /* Offsets to the device registers.
325: Unlike software-only systems, device drivers interact with complex hardware.
326: It's not useful to define symbolic names for every register bit in the
327: device. The name can only partially document the semantics and make
328: the driver longer and more difficult to read.
329: In general, only the important configuration values or bits changed
330: multiple times should be defined symbolically.
331: */
332: enum alta_offsets {
333: DMACtrl=0x00, TxListPtr=0x04, TxDMACtrl=0x08, TxDescPoll=0x0a,
334: RxDMAStatus=0x0c, RxListPtr=0x10, RxDMACtrl=0x14, RxDescPoll=0x16,
335: LEDCtrl=0x1a, ASICCtrl=0x30,
336: EEData=0x34, EECtrl=0x36, TxThreshold=0x3c,
337: FlashAddr=0x40, FlashData=0x44, WakeEvent=0x45, TxStatus=0x46,
338: DownCounter=0x48, IntrClear=0x4a, IntrEnable=0x4c, IntrStatus=0x4e,
339: MACCtrl0=0x50, MACCtrl1=0x52, StationAddr=0x54,
340: MaxFrameSize=0x5A, RxMode=0x5c, MIICtrl=0x5e,
341: MulticastFilter0=0x60, MulticastFilter1=0x64,
342: RxOctetsLow=0x68, RxOctetsHigh=0x6a, TxOctetsLow=0x6c, TxOctetsHigh=0x6e,
343: TxFramesOK=0x70, RxFramesOK=0x72, StatsCarrierError=0x74,
344: StatsLateColl=0x75, StatsMultiColl=0x76, StatsOneColl=0x77,
345: StatsTxDefer=0x78, RxMissed=0x79, StatsTxXSDefer=0x7a, StatsTxAbort=0x7b,
346: StatsBcastTx=0x7c, StatsBcastRx=0x7d, StatsMcastTx=0x7e, StatsMcastRx=0x7f,
347: /* Aliased and bogus values! */
348: RxStatus=0x0c,
349: };
350:
351: /* Bits in the interrupt status/mask registers. */
352: enum intr_status_bits {
353: IntrSummary=0x0001, IntrPCIErr=0x0002, IntrMACCtrl=0x0008,
354: IntrTxDone=0x0004, IntrRxDone=0x0010, IntrRxStart=0x0020,
355: IntrDrvRqst=0x0040,
356: StatsMax=0x0080, LinkChange=0x0100,
357: IntrTxDMADone=0x0200, IntrRxDMADone=0x0400,
358: };
359:
360: /* Bits in the RxMode register. */
361: enum rx_mode_bits {
362: AcceptAllIPMulti=0x20, AcceptMultiHash=0x10, AcceptAll=0x08,
363: AcceptBroadcast=0x04, AcceptMulticast=0x02, AcceptMyPhys=0x01,
364: };
365: /* Bits in MACCtrl. */
366: enum mac_ctrl0_bits {
367: EnbFullDuplex=0x20, EnbRcvLargeFrame=0x40,
368: EnbFlowCtrl=0x100, EnbPassRxCRC=0x200,
369: };
370: enum mac_ctrl1_bits {
371: StatsEnable=0x0020, StatsDisable=0x0040, StatsEnabled=0x0080,
372: TxEnable=0x0100, TxDisable=0x0200, TxEnabled=0x0400,
373: RxEnable=0x0800, RxDisable=0x1000, RxEnabled=0x2000,
374: };
375:
376: /* The Rx and Tx buffer descriptors.
377: Using only 32 bit fields simplifies software endian correction.
378: This structure must be aligned, and should avoid spanning cache lines.
379: */
380: struct netdev_desc {
381: u32 next_desc;
382: u32 status;
383: struct desc_frag { u32 addr, length; } frag[1];
384: };
385:
386: /* Bits in netdev_desc.status */
387: enum desc_status_bits {
388: DescOwn=0x8000, DescEndPacket=0x4000, DescEndRing=0x2000,
389: DescTxDMADone=0x10000,
390: LastFrag=0x80000000, DescIntrOnTx=0x8000, DescIntrOnDMADone=0x80000000,
391: };
392:
393: #define PRIV_ALIGN 15 /* Required alignment mask */
394: /* Use __attribute__((aligned (L1_CACHE_BYTES))) to maintain alignment
395: within the structure. */
396: struct netdev_private {
397: /* Descriptor rings first for alignment. */
398: struct netdev_desc rx_ring[RX_RING_SIZE];
399: struct netdev_desc tx_ring[TX_RING_SIZE];
400: struct net_device *next_module; /* Link for devices of this type. */
401: void *priv_addr; /* Unaligned address for kfree */
402: const char *product_name;
403: /* The addresses of receive-in-place skbuffs. */
404: struct sk_buff* rx_skbuff[RX_RING_SIZE];
405: /* The saved address of a sent-in-place packet/buffer, for later free(). */
406: struct sk_buff* tx_skbuff[TX_RING_SIZE];
407: struct net_device_stats stats;
408: struct timer_list timer; /* Media monitoring timer. */
409: /* Frequently used values: keep some adjacent for cache effect. */
410: int msg_level;
411: int chip_id, drv_flags;
412: struct pci_dev *pci_dev;
413: int max_interrupt_work;
414:
415: /* Note: Group variables for cache line effect. */
416: struct netdev_desc *rx_head_desc;
417: unsigned int cur_rx, dirty_rx; /* Producer/consumer ring indices */
418: unsigned int rx_buf_sz; /* Based on MTU+slack. */
419: int rx_copybreak;
420:
421: spinlock_t txlock; /* Group with Tx control cache line. */
422: struct netdev_desc *last_tx; /* Last Tx descriptor used. */
423: unsigned int cur_tx, dirty_tx;
424: unsigned int tx_full:1; /* The Tx queue is full. */
425:
426: /* These values keep track of the transceiver/media in use. */
427: unsigned int full_duplex:1; /* Full-duplex operation requested. */
428: unsigned int duplex_lock:1;
429: unsigned int medialock:1; /* Do not sense media. */
430: unsigned int default_port; /* Last dev->if_port value. */
431: /* Multicast and receive mode. */
432: spinlock_t mcastlock; /* SMP lock multicast updates. */
433: u16 mcast_filter[4];
434: int multicast_filter_limit;
435:
436: /* MII transceiver section. */
437: int mii_cnt; /* MII device addresses. */
438: int link_status;
439: u16 advertising; /* NWay media advertisement */
440: unsigned char phys[2]; /* MII device addresses. */
441: };
442:
443: /* The station address location in the EEPROM. */
444: #define EEPROM_SA_OFFSET 0x10
445:
446: static int eeprom_read(long ioaddr, int location);
447: static int mdio_read(struct net_device *dev, int phy_id,
448: unsigned int location);
449: static void mdio_write(struct net_device *dev, int phy_id,
450: unsigned int location, int value);
451: static int netdev_open(struct net_device *dev);
452: static void sundance_start(struct net_device *dev);
453: static int change_mtu(struct net_device *dev, int new_mtu);
454: static void check_duplex(struct net_device *dev);
455: static void netdev_timer(unsigned long data);
456: static void tx_timeout(struct net_device *dev);
457: static void init_ring(struct net_device *dev);
458: static int start_tx(struct sk_buff *skb, struct net_device *dev);
459: static void intr_handler(int irq, void *dev_instance, struct pt_regs *regs);
460: static void netdev_error(struct net_device *dev, int intr_status);
461: static int netdev_rx(struct net_device *dev);
462: static void netdev_error(struct net_device *dev, int intr_status);
463: static void set_rx_mode(struct net_device *dev);
464: static struct net_device_stats *get_stats(struct net_device *dev);
465: static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
466: static int netdev_close(struct net_device *dev);
467:
468:
469:
470: /* A list of our installed devices, for removing the driver module. */
471: static struct net_device *root_net_dev = NULL;
472:
473: #ifndef MODULE
474: int sundance_probe(struct net_device *dev)
475: {
476: if (pci_drv_register(&sundance_drv_id, dev) < 0)
477: return -ENODEV;
478: if (debug >= NETIF_MSG_DRV) /* Emit version even if no cards detected. */
479: printk(KERN_INFO "%s" KERN_INFO "%s", version1, version2);
480: return 0;
481: }
482: #endif
483:
484: static void *sundance_probe1(struct pci_dev *pdev, void *init_dev,
485: long ioaddr, int irq, int chip_idx, int card_idx)
486: {
487: struct net_device *dev;
488: struct netdev_private *np;
489: void *priv_mem;
490: int i, option = card_idx < MAX_UNITS ? options[card_idx] : 0;
491:
492: dev = init_etherdev(init_dev, 0);
493: if (!dev)
494: return NULL;
495:
496: /* Perhaps NETIF_MSG_PROBE */
497: printk(KERN_INFO "%s: %s at 0x%lx, ",
498: dev->name, pci_id_tbl[chip_idx].name, ioaddr);
499:
500: for (i = 0; i < 3; i++)
501: ((u16 *)dev->dev_addr)[i] =
502: le16_to_cpu(eeprom_read(ioaddr, i + EEPROM_SA_OFFSET));
503: for (i = 0; i < 5; i++)
504: printk("%2.2x:", dev->dev_addr[i]);
505: printk("%2.2x, IRQ %d.\n", dev->dev_addr[i], irq);
506:
507: /* Make certain elements e.g. descriptor lists are aligned. */
508: priv_mem = kmalloc(sizeof(*np) + PRIV_ALIGN, GFP_KERNEL);
509: /* Check for the very unlikely case of no memory. */
510: if (priv_mem == NULL)
511: return NULL;
512:
513: /* All failure checks before this point.
514: We do a request_region() only to register /proc/ioports info. */
515: #ifdef USE_IO_OPS
516: request_region(ioaddr, pci_id_tbl[chip_idx].io_size, dev->name);
517: #endif
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: if (card_idx < MAX_UNITS && full_duplex[card_idx] > 0)
541: np->full_duplex = 1;
542:
543: if (np->full_duplex)
544: np->medialock = 1;
545:
546: /* The chip-specific entries in the device structure. */
547: dev->open = &netdev_open;
548: dev->hard_start_xmit = &start_tx;
549: dev->stop = &netdev_close;
550: dev->get_stats = &get_stats;
551: dev->set_multicast_list = &set_rx_mode;
552: dev->do_ioctl = &mii_ioctl;
553: dev->change_mtu = &change_mtu;
554:
555: if (1) {
556: int phy, phy_idx = 0;
557: np->phys[0] = 1; /* Default setting */
558: mii_preamble_required++;
559: for (phy = 1; phy < 32 && phy_idx < 4; phy++) {
560: int mii_status = mdio_read(dev, phy, 1);
561: if (mii_status != 0xffff && mii_status != 0x0000) {
562: np->phys[phy_idx++] = phy;
563: np->advertising = mdio_read(dev, phy, 4);
564: if ((mii_status & 0x0040) == 0)
565: mii_preamble_required++;
566: if (np->msg_level & NETIF_MSG_PROBE)
567: printk(KERN_INFO "%s: MII PHY found at address %d, status "
568: "0x%4.4x advertising %4.4x.\n",
569: dev->name, phy, mii_status, np->advertising);
570: }
571: }
572: mii_preamble_required--;
573: np->mii_cnt = phy_idx;
574: if (phy_idx == 0)
575: printk(KERN_INFO "%s: No MII transceiver found!, ASIC status %x\n",
576: dev->name, (int)readl(ioaddr + ASICCtrl));
577: }
578:
579: /* Allow forcing the media type. */
580: if (option > 0) {
581: if (option & 0x220)
582: np->full_duplex = 1;
583: np->default_port = option & 0x3ff;
584: if (np->default_port & 0x330) {
585: np->medialock = 1;
586: if (np->msg_level & NETIF_MSG_PROBE)
587: printk(KERN_INFO " Forcing %dMbs %s-duplex operation.\n",
588: (option & 0x300 ? 100 : 10),
589: (np->full_duplex ? "full" : "half"));
590: if (np->mii_cnt)
591: mdio_write(dev, np->phys[0], 0,
592: ((option & 0x300) ? 0x2000 : 0) | /* 100mbps? */
593: (np->full_duplex ? 0x0100 : 0)); /* Full duplex? */
594: }
595: }
596:
597: /* Reset the chip to erase previous misconfiguration. */
598: if (np->msg_level & NETIF_MSG_MISC)
599: printk("ASIC Control is %x.\n", (int)readl(ioaddr + ASICCtrl));
600: writel(0x007f0000 | readl(ioaddr + ASICCtrl), ioaddr + ASICCtrl);
601: if (np->msg_level & NETIF_MSG_MISC)
602: printk("ASIC Control is now %x.\n", (int)readl(ioaddr + ASICCtrl));
603:
604: return dev;
605: }
606:
607:
608:
609: static int change_mtu(struct net_device *dev, int new_mtu)
610: {
611: if ((new_mtu < 68) || (new_mtu > 8191)) /* Limited by RxDMAFrameLen */
612: return -EINVAL;
613: if (netif_running(dev))
614: return -EBUSY;
615: dev->mtu = new_mtu;
616: return 0;
617: }
618:
619: /* Read the EEPROM and MII Management Data I/O (MDIO) interfaces. */
620: static int eeprom_read(long ioaddr, int location)
621: {
622: int boguscnt = 2000; /* Typical 190 ticks. */
623: writew(0x0200 | (location & 0xff), ioaddr + EECtrl);
624: do {
625: if (! (readw(ioaddr + EECtrl) & 0x8000)) {
626: return readw(ioaddr + EEData);
627: }
628: } while (--boguscnt > 0);
629: return 0;
630: }
631:
632: /* MII transceiver control section.
633: Read and write the MII registers using software-generated serial
634: MDIO protocol. See the MII specifications or DP83840A data sheet
635: for details.
636:
637: The maximum data clock rate is 2.5 Mhz.
638: The timing is decoupled from the processor clock by flushing the write
639: from the CPU write buffer with a following read, and using PCI
640: transaction time. */
641: #define mdio_in(mdio_addr) readb(mdio_addr)
642: #define mdio_out(value, mdio_addr) writeb(value, mdio_addr)
643: #define mdio_delay(mdio_addr) readb(mdio_addr)
644:
645: enum mii_reg_bits {
646: MDIO_ShiftClk=0x0001, MDIO_Data=0x0002, MDIO_EnbOutput=0x0004,
647: };
648: #define MDIO_EnbIn (0)
649: #define MDIO_WRITE0 (MDIO_EnbOutput)
650: #define MDIO_WRITE1 (MDIO_Data | MDIO_EnbOutput)
651:
652: /* Generate the preamble required for initial synchronization and
653: a few older transceivers. */
654: static void mdio_sync(long mdio_addr)
655: {
656: int bits = 32;
657:
658: /* Establish sync by sending at least 32 logic ones. */
659: while (--bits >= 0) {
660: mdio_out(MDIO_WRITE1, mdio_addr);
661: mdio_delay(mdio_addr);
662: mdio_out(MDIO_WRITE1 | MDIO_ShiftClk, mdio_addr);
663: mdio_delay(mdio_addr);
664: }
665: }
666:
667: static int mdio_read(struct net_device *dev, int phy_id, unsigned int location)
668: {
669: long mdio_addr = dev->base_addr + MIICtrl;
670: int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;
671: int i, retval = 0;
672:
673: if (mii_preamble_required)
674: mdio_sync(mdio_addr);
675:
676: /* Shift the read command bits out. */
677: for (i = 15; i >= 0; i--) {
678: int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
679:
680: mdio_out(dataval, mdio_addr);
681: mdio_delay(mdio_addr);
682: mdio_out(dataval | MDIO_ShiftClk, mdio_addr);
683: mdio_delay(mdio_addr);
684: }
685: /* Read the two transition, 16 data, and wire-idle bits. */
686: for (i = 19; i > 0; i--) {
687: mdio_out(MDIO_EnbIn, mdio_addr);
688: mdio_delay(mdio_addr);
689: retval = (retval << 1) | ((mdio_in(mdio_addr) & MDIO_Data) ? 1 : 0);
690: mdio_out(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
691: mdio_delay(mdio_addr);
692: }
693: return (retval>>1) & 0xffff;
694: }
695:
696: static void mdio_write(struct net_device *dev, int phy_id,
697: unsigned int location, int value)
698: {
699: long mdio_addr = dev->base_addr + MIICtrl;
700: int mii_cmd = (0x5002 << 16) | (phy_id << 23) | (location<<18) | value;
701: int i;
702:
703: if (mii_preamble_required)
704: mdio_sync(mdio_addr);
705:
706: /* Shift the command bits out. */
707: for (i = 31; i >= 0; i--) {
708: int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
709:
710: mdio_out(dataval, mdio_addr);
711: mdio_delay(mdio_addr);
712: mdio_out(dataval | MDIO_ShiftClk, mdio_addr);
713: mdio_delay(mdio_addr);
714: }
715: /* Clear out extra bits. */
716: for (i = 2; i > 0; i--) {
717: mdio_out(MDIO_EnbIn, mdio_addr);
718: mdio_delay(mdio_addr);
719: mdio_out(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
720: mdio_delay(mdio_addr);
721: }
722: return;
723: }
724:
725:
726: static int netdev_open(struct net_device *dev)
727: {
728: struct netdev_private *np = (struct netdev_private *)dev->priv;
729: long ioaddr = dev->base_addr;
730:
731: MOD_INC_USE_COUNT;
732:
733: if (request_irq(dev->irq, &intr_handler, SA_SHIRQ, dev->name, dev)) {
734: MOD_DEC_USE_COUNT;
735: return -EAGAIN;
736: }
737:
738: if (np->msg_level & NETIF_MSG_IFUP)
739: printk(KERN_DEBUG "%s: netdev_open() irq %d.\n",
740: dev->name, dev->irq);
741:
742: init_ring(dev);
743:
744: if (dev->if_port == 0)
745: dev->if_port = np->default_port;
746:
747: np->full_duplex = np->duplex_lock;
748: np->mcastlock = (spinlock_t) SPIN_LOCK_UNLOCKED;
749:
750: sundance_start(dev);
751: netif_start_tx_queue(dev);
752:
753: if (np->msg_level & NETIF_MSG_IFUP)
754: printk(KERN_DEBUG "%s: Done netdev_open(), status: Rx %x Tx %x "
755: "MAC Control %x, %4.4x %4.4x.\n",
756: dev->name, (int)readl(ioaddr + RxStatus),
757: (int)readw(ioaddr + TxStatus), (int)readl(ioaddr + MACCtrl0),
758: (int)readw(ioaddr + MACCtrl1), (int)readw(ioaddr + MACCtrl0));
759:
760: /* Set the timer to check for link beat. */
761: init_timer(&np->timer);
762: np->timer.expires = jiffies + 3*HZ;
763: np->timer.data = (unsigned long)dev;
764: np->timer.function = &netdev_timer; /* timer handler */
765: add_timer(&np->timer);
766:
767: return 0;
768: }
769:
770: static void sundance_start(struct net_device *dev)
771: {
772: struct netdev_private *np = (struct netdev_private *)dev->priv;
773: long ioaddr = dev->base_addr;
774: int i;
775:
776: /* No reports have indicated that we need to reset the chip. */
777:
778: writel(virt_to_bus(&np->rx_ring[np->cur_rx % RX_RING_SIZE]),
779: ioaddr + RxListPtr);
780: /* The Tx list pointer is written as packets are queued. */
781:
782: /* Station address must be written as 16 bit words with the Kendin chip. */
783: for (i = 0; i < 6; i += 2)
784: writew((dev->dev_addr[i + 1] << 8) + dev->dev_addr[i],
785: ioaddr + StationAddr + i);
786:
787: np->link_status = readb(ioaddr + MIICtrl) & 0xE0;
788: writew((np->full_duplex || (np->link_status & 0x20)) ? 0x120 : 0,
789: ioaddr + MACCtrl0);
790: writew(dev->mtu + 14, ioaddr + MaxFrameSize);
791: if (dev->mtu > 2047)
792: writel(readl(ioaddr + ASICCtrl) | 0x0C, ioaddr + ASICCtrl);
793:
794: set_rx_mode(dev);
795: writew(0, ioaddr + DownCounter);
796: /* Set the chip to poll every N*320nsec. */
797: writeb(100, ioaddr + RxDescPoll);
798: writeb(127, ioaddr + TxDescPoll);
799: #if 0
800: if (np->drv_flags & KendinPktDropBug)
801: writeb(0x01, ioaddr + DebugCtrl1);
802: #endif
803:
804: /* Enable interrupts by setting the interrupt mask. */
805: writew(IntrRxDMADone | IntrPCIErr | IntrDrvRqst | IntrTxDone
806: | StatsMax | LinkChange, ioaddr + IntrEnable);
807: writew(StatsEnable | RxEnable | TxEnable, ioaddr + MACCtrl1);
808: }
809:
810: static void check_duplex(struct net_device *dev)
811: {
812: struct netdev_private *np = (struct netdev_private *)dev->priv;
813: long ioaddr = dev->base_addr;
814: int mii_reg5 = mdio_read(dev, np->phys[0], 5);
815: int negotiated = mii_reg5 & np->advertising;
816: int duplex;
817:
818: if (np->duplex_lock || mii_reg5 == 0xffff)
819: return;
820: duplex = (negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040;
821: if (np->full_duplex != duplex) {
822: np->full_duplex = duplex;
823: if (np->msg_level & NETIF_MSG_LINK)
824: printk(KERN_INFO "%s: Setting %s-duplex based on MII #%d "
825: "negotiated capability %4.4x.\n", dev->name,
826: duplex ? "full" : "half", np->phys[0], negotiated);
827: writew(duplex ? 0x20 : 0, ioaddr + MACCtrl0);
828: }
829: }
830:
831: static void netdev_timer(unsigned long data)
832: {
833: struct net_device *dev = (struct net_device *)data;
834: struct netdev_private *np = (struct netdev_private *)dev->priv;
835: long ioaddr = dev->base_addr;
836: int next_tick = 10*HZ;
837:
838: if (np->msg_level & NETIF_MSG_TIMER) {
839: printk(KERN_DEBUG "%s: Media selection timer tick, intr status %4.4x, "
840: "Tx %x Rx %x.\n",
841: dev->name, (int)readw(ioaddr + IntrEnable),
842: (int)readw(ioaddr + TxStatus), (int)readl(ioaddr + RxStatus));
843: }
844: /* Note: This does not catch a 0 or 1 element stuck queue. */
845: if (netif_queue_paused(dev) &&
846: np->cur_tx - np->dirty_tx > 1 &&
847: (jiffies - dev->trans_start) > TX_TIMEOUT) {
848: tx_timeout(dev);
849: }
850: check_duplex(dev);
851: np->timer.expires = jiffies + next_tick;
852: add_timer(&np->timer);
853: }
854:
855: static void tx_timeout(struct net_device *dev)
856: {
857: struct netdev_private *np = (struct netdev_private *)dev->priv;
858: long ioaddr = dev->base_addr;
859:
860: printk(KERN_WARNING "%s: Transmit timed out, status %4.4x,"
861: " resetting...\n", dev->name, (int)readw(ioaddr + TxStatus));
862:
863: #ifdef __i386__
864: if (np->msg_level & NETIF_MSG_TX_ERR) {
865: int i;
866: printk(KERN_DEBUG " Rx ring %8.8x: ", (int)np->rx_ring);
867: for (i = 0; i < RX_RING_SIZE; i++)
868: printk(" %8.8x", (unsigned int)np->rx_ring[i].status);
869: printk("\n"KERN_DEBUG" Tx ring %8.8x: ", (int)np->tx_ring);
870: for (i = 0; i < TX_RING_SIZE; i++)
871: printk(" %8.8x", np->tx_ring[i].status);
872: printk("\n");
873: }
874: #endif
875:
876: /* Perhaps we should reinitialize the hardware here. */
877: dev->if_port = 0;
878: /* Stop and restart the chip's Tx processes . */
879:
880: /* Trigger an immediate transmit demand. */
881: writew(IntrRxDMADone | IntrPCIErr | IntrDrvRqst | IntrTxDone
882: | StatsMax | LinkChange, ioaddr + IntrEnable);
883:
884: dev->trans_start = jiffies;
885: np->stats.tx_errors++;
886: return;
887: }
888:
889:
890: /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
891: static void init_ring(struct net_device *dev)
892: {
893: struct netdev_private *np = (struct netdev_private *)dev->priv;
894: int i;
895:
896: np->tx_full = 0;
897: np->cur_rx = np->cur_tx = 0;
898: np->dirty_rx = np->dirty_tx = 0;
899:
900: np->rx_buf_sz = dev->mtu + 20;
901: if (np->rx_buf_sz < PKT_BUF_SZ)
902: np->rx_buf_sz = PKT_BUF_SZ;
903: np->rx_head_desc = &np->rx_ring[0];
904:
905: /* Initialize all Rx descriptors. */
906: for (i = 0; i < RX_RING_SIZE; i++) {
907: np->rx_ring[i].next_desc = virt_to_le32desc(&np->rx_ring[i+1]);
908: np->rx_ring[i].status = 0;
909: np->rx_ring[i].frag[0].length = 0;
910: np->rx_skbuff[i] = 0;
911: }
912: /* Wrap the ring. */
913: np->rx_ring[i-1].next_desc = virt_to_le32desc(&np->rx_ring[0]);
914:
915: /* Fill in the Rx buffers. Handle allocation failure gracefully. */
916: for (i = 0; i < RX_RING_SIZE; i++) {
917: struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz);
918: np->rx_skbuff[i] = skb;
919: if (skb == NULL)
920: break;
921: skb->dev = dev; /* Mark as being used by this device. */
922: skb_reserve(skb, 2); /* 16 byte align the IP header. */
923: np->rx_ring[i].frag[0].addr = virt_to_le32desc(skb->tail);
924: np->rx_ring[i].frag[0].length = cpu_to_le32(np->rx_buf_sz | LastFrag);
925: }
926: np->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
927:
928: for (i = 0; i < TX_RING_SIZE; i++) {
929: np->tx_skbuff[i] = 0;
930: np->tx_ring[i].status = 0;
931: }
932: return;
933: }
934:
935: static int start_tx(struct sk_buff *skb, struct net_device *dev)
936: {
937: struct netdev_private *np = (struct netdev_private *)dev->priv;
938: struct netdev_desc *txdesc;
939: unsigned entry;
940:
941: /* Block a timer-based transmit from overlapping. */
942: if (netif_pause_tx_queue(dev) != 0) {
943: /* This watchdog code is redundant with the media monitor timer. */
944: if (jiffies - dev->trans_start > TX_TIMEOUT)
945: tx_timeout(dev);
946: return 1;
947: }
948:
949: /* Note: Ordering is important here, set the field with the
950: "ownership" bit last, and only then increment cur_tx. */
951:
952: /* Calculate the next Tx descriptor entry. */
953: entry = np->cur_tx % TX_RING_SIZE;
954: np->tx_skbuff[entry] = skb;
955: txdesc = &np->tx_ring[entry];
956:
957: txdesc->next_desc = 0;
958: /* Note: disable the interrupt generation here before releasing. */
959: txdesc->status =
960: cpu_to_le32((entry<<2) | DescIntrOnDMADone | DescIntrOnTx | 1);
961: txdesc->frag[0].addr = virt_to_le32desc(skb->data);
962: txdesc->frag[0].length = cpu_to_le32(skb->len | LastFrag);
963: if (np->last_tx)
964: np->last_tx->next_desc = virt_to_le32desc(txdesc);
965: np->last_tx = txdesc;
966: np->cur_tx++;
967:
968: /* On some architectures: explicitly flush cache lines here. */
969:
970: if (np->cur_tx - np->dirty_tx >= TX_QUEUE_LEN - 1) {
971: np->tx_full = 1;
972: /* Check for a just-cleared queue. */
973: if (np->cur_tx - (volatile unsigned int)np->dirty_tx
974: < TX_QUEUE_LEN - 2) {
975: np->tx_full = 0;
976: netif_unpause_tx_queue(dev);
977: } else
978: netif_stop_tx_queue(dev);
979: } else
980: netif_unpause_tx_queue(dev); /* Typical path */
981:
982: /* Side effect: The read wakes the potentially-idle transmit channel. */
983: if (readl(dev->base_addr + TxListPtr) == 0)
984: writel(virt_to_bus(&np->tx_ring[entry]), dev->base_addr + TxListPtr);
985:
986: dev->trans_start = jiffies;
987:
988: if (np->msg_level & NETIF_MSG_TX_QUEUED) {
989: printk(KERN_DEBUG "%s: Transmit frame #%d len %ld queued in slot %ld.\n",
990: dev->name, np->cur_tx, skb->len, entry);
991: }
992: return 0;
993: }
994:
995: /* The interrupt handler does all of the Rx thread work and cleans up
996: after the Tx thread. */
997: static void intr_handler(int irq, void *dev_instance, struct pt_regs *rgs)
998: {
999: struct net_device *dev = (struct net_device *)dev_instance;
1000: struct netdev_private *np;
1001: long ioaddr;
1002: int boguscnt;
1003:
1004: ioaddr = dev->base_addr;
1005: np = (struct netdev_private *)dev->priv;
1006: boguscnt = np->max_interrupt_work;
1007:
1008: do {
1009: int intr_status = readw(ioaddr + IntrStatus);
1010: if ((intr_status & ~IntrRxDone) == 0 || intr_status == 0xffff)
1011: break;
1012:
1013: writew(intr_status & (IntrRxDMADone | IntrPCIErr |
1014: IntrDrvRqst |IntrTxDone|IntrTxDMADone |
1015: StatsMax | LinkChange),
1016: ioaddr + IntrStatus);
1017:
1018: if (np->msg_level & NETIF_MSG_INTR)
1019: printk(KERN_DEBUG "%s: Interrupt, status %4.4x.\n",
1020: dev->name, intr_status);
1021:
1022: if (intr_status & IntrRxDMADone)
1023: netdev_rx(dev);
1024:
1025: if (intr_status & IntrTxDone) {
1026: int txboguscnt = 32;
1027: int tx_status = readw(ioaddr + TxStatus);
1028: while (tx_status & 0x80) {
1029: if (np->msg_level & NETIF_MSG_TX_DONE)
1030: printk("%s: Transmit status is %4.4x.\n",
1031: dev->name, tx_status);
1032: if (tx_status & 0x1e) {
1033: if (np->msg_level & NETIF_MSG_TX_ERR)
1034: printk("%s: Transmit error status %4.4x.\n",
1035: dev->name, tx_status);
1036: np->stats.tx_errors++;
1037: if (tx_status & 0x10) np->stats.tx_fifo_errors++;
1038: #ifdef ETHER_STATS
1039: if (tx_status & 0x08) np->stats.collisions16++;
1040: #else
1041: if (tx_status & 0x08) np->stats.collisions++;
1042: #endif
1043: if (tx_status & 0x04) np->stats.tx_fifo_errors++;
1044: if (tx_status & 0x02) np->stats.tx_window_errors++;
1045: /* This reset has not been verified!. */
1046: if (tx_status & 0x10) { /* Reset the Tx. */
1047: writel(0x001c0000 | readl(ioaddr + ASICCtrl),
1048: ioaddr + ASICCtrl);
1049: #if 0 /* Do we need to reset the Tx pointer here? */
1050: writel(virt_to_bus(&np->tx_ring[np->dirty_tx]),
1051: dev->base_addr + TxListPtr);
1052: #endif
1053: }
1054: if (tx_status & 0x1e) /* Restart the Tx. */
1055: writew(TxEnable, ioaddr + MACCtrl1);
1056: }
1057: /* Yup, this is a documentation bug. It cost me *hours*. */
1058: writew(0, ioaddr + TxStatus);
1059: if (--txboguscnt < 0)
1060: break;
1061: tx_status = readw(ioaddr + TxStatus);
1062: }
1063: }
1064: for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {
1065: int entry = np->dirty_tx % TX_RING_SIZE;
1066: if ( ! (np->tx_ring[entry].status & cpu_to_le32(DescTxDMADone)))
1067: break;
1068: /* Free the original skb. */
1069: dev_free_skb_irq(np->tx_skbuff[entry]);
1070: np->tx_skbuff[entry] = 0;
1071: }
1072: if (np->tx_full && np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4) {
1073: /* The ring is no longer full, allow new TX entries. */
1074: np->tx_full = 0;
1075: netif_resume_tx_queue(dev);
1076: }
1077:
1078: /* Abnormal error summary/uncommon events handlers. */
1079: if (intr_status & (IntrDrvRqst | IntrPCIErr | LinkChange | StatsMax))
1080: netdev_error(dev, intr_status);
1081:
1082: if (--boguscnt < 0) {
1083: int intr_clear = readw(ioaddr + IntrClear);
1084: get_stats(dev);
1085: printk(KERN_WARNING "%s: Too much work at interrupt, "
1086: "status=0x%4.4x / 0x%4.4x .. 0x%4.4x.\n",
1087: dev->name, intr_status, intr_clear,
1088: (int)readw(ioaddr + IntrClear));
1089: /* Re-enable us in 3.2msec. */
1090: writew(1000, ioaddr + DownCounter);
1091: writew(IntrDrvRqst, ioaddr + IntrEnable);
1092: break;
1093: }
1094: } while (1);
1095:
1096: if (np->msg_level & NETIF_MSG_INTR)
1097: printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n",
1098: dev->name, (int)readw(ioaddr + IntrStatus));
1099:
1100: return;
1101: }
1102:
1103: /* This routine is logically part of the interrupt handler, but separated
1104: for clarity and better register allocation. */
1105: static int netdev_rx(struct net_device *dev)
1106: {
1107: struct netdev_private *np = (struct netdev_private *)dev->priv;
1108: int entry = np->cur_rx % RX_RING_SIZE;
1109: int boguscnt = np->dirty_rx + RX_RING_SIZE - np->cur_rx;
1110:
1111: if (np->msg_level & NETIF_MSG_RX_STATUS) {
1112: printk(KERN_DEBUG " In netdev_rx(), entry %d status %4.4x.\n",
1113: entry, np->rx_ring[entry].status);
1114: }
1115:
1116: /* If EOP is set on the next entry, it's a new packet. Send it up. */
1117: while (np->rx_head_desc->status & cpu_to_le32(DescOwn)) {
1118: struct netdev_desc *desc = np->rx_head_desc;
1119: u32 frame_status = le32_to_cpu(desc->status);
1120: int pkt_len = frame_status & 0x1fff; /* Chip omits the CRC. */
1121:
1122: if (np->msg_level & NETIF_MSG_RX_STATUS)
1123: printk(KERN_DEBUG " netdev_rx() status was %8.8x.\n",
1124: frame_status);
1125: if (--boguscnt < 0)
1126: break;
1127: if (frame_status & 0x001f4000) {
1128: /* There was a error. */
1129: if (np->msg_level & NETIF_MSG_RX_ERR)
1130: printk(KERN_DEBUG " netdev_rx() Rx error was %8.8x.\n",
1131: frame_status);
1132: np->stats.rx_errors++;
1133: if (frame_status & 0x00100000) np->stats.rx_length_errors++;
1134: if (frame_status & 0x00010000) np->stats.rx_fifo_errors++;
1135: if (frame_status & 0x00060000) np->stats.rx_frame_errors++;
1136: if (frame_status & 0x00080000) np->stats.rx_crc_errors++;
1137: if (frame_status & 0x00100000) {
1138: printk(KERN_WARNING "%s: Oversized Ethernet frame,"
1139: " status %8.8x.\n",
1140: dev->name, frame_status);
1141: }
1142: } else {
1143: struct sk_buff *skb;
1144:
1145: #ifndef final_version
1146: if (np->msg_level & NETIF_MSG_RX_STATUS)
1147: printk(KERN_DEBUG " netdev_rx() normal Rx pkt length %d"
1148: ", bogus_cnt %d.\n",
1149: pkt_len, boguscnt);
1150: #endif
1151: /* Check if the packet is long enough to accept without copying
1152: to a minimally-sized skbuff. */
1153: if (pkt_len < np->rx_copybreak
1154: && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
1155: skb->dev = dev;
1156: skb_reserve(skb, 2); /* 16 byte align the IP header */
1157: eth_copy_and_sum(skb, np->rx_skbuff[entry]->tail, pkt_len, 0);
1158: skb_put(skb, pkt_len);
1159: } else {
1160: skb_put(skb = np->rx_skbuff[entry], pkt_len);
1161: np->rx_skbuff[entry] = NULL;
1162: }
1163: skb->protocol = eth_type_trans(skb, dev);
1164: /* Note: checksum -> skb->ip_summed = CHECKSUM_UNNECESSARY; */
1165: netif_rx(skb);
1166: dev->last_rx = jiffies;
1167: }
1168: entry = (++np->cur_rx) % RX_RING_SIZE;
1169: np->rx_head_desc = &np->rx_ring[entry];
1170: }
1171:
1172: /* Refill the Rx ring buffers. */
1173: for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {
1174: struct sk_buff *skb;
1175: entry = np->dirty_rx % RX_RING_SIZE;
1176: if (np->rx_skbuff[entry] == NULL) {
1177: skb = dev_alloc_skb(np->rx_buf_sz);
1178: np->rx_skbuff[entry] = skb;
1179: if (skb == NULL)
1180: break; /* Better luck next round. */
1181: skb->dev = dev; /* Mark as being used by this device. */
1182: skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
1183: np->rx_ring[entry].frag[0].addr = virt_to_le32desc(skb->tail);
1184: }
1185: /* Perhaps we need not reset this field. */
1186: np->rx_ring[entry].frag[0].length =
1187: cpu_to_le32(np->rx_buf_sz | LastFrag);
1188: np->rx_ring[entry].status = 0;
1189: }
1190:
1191: /* No need to restart Rx engine, it will poll. */
1192: return 0;
1193: }
1194:
1195: static void netdev_error(struct net_device *dev, int intr_status)
1196: {
1197: long ioaddr = dev->base_addr;
1198: struct netdev_private *np = (struct netdev_private *)dev->priv;
1199:
1200: if (intr_status & IntrDrvRqst) {
1201: /* Stop the down counter and turn interrupts back on. */
1202: printk(KERN_WARNING "%s: Turning interrupts back on.\n", dev->name);
1203: writew(0, ioaddr + DownCounter);
1204: writew(IntrRxDMADone | IntrPCIErr | IntrDrvRqst |
1205: IntrTxDone | StatsMax | LinkChange, ioaddr + IntrEnable);
1206: }
1207: if (intr_status & LinkChange) {
1208: int new_status = readb(ioaddr + MIICtrl) & 0xE0;
1209: if (np->msg_level & NETIF_MSG_LINK)
1210: printk(KERN_NOTICE "%s: Link changed: Autonegotiation advertising"
1211: " %4.4x partner %4.4x.\n", dev->name,
1212: mdio_read(dev, np->phys[0], 4),
1213: mdio_read(dev, np->phys[0], 5));
1214: if ((np->link_status ^ new_status) & 0x80) {
1215: if (new_status & 0x80)
1216: netif_link_up(dev);
1217: else
1218: netif_link_down(dev);
1219: }
1220: np->link_status = new_status;
1221: check_duplex(dev);
1222: }
1223: if (intr_status & StatsMax) {
1224: get_stats(dev);
1225: }
1226: if (intr_status & IntrPCIErr) {
1227: printk(KERN_ERR "%s: Something Wicked happened! %4.4x.\n",
1228: dev->name, intr_status);
1229: /* We must do a global reset of DMA to continue. */
1230: }
1231: }
1232:
1233: static struct net_device_stats *get_stats(struct net_device *dev)
1234: {
1235: long ioaddr = dev->base_addr;
1236: struct netdev_private *np = (struct netdev_private *)dev->priv;
1237: int i;
1238:
1239: if (readw(ioaddr + StationAddr) == 0xffff)
1240: return &np->stats;
1241:
1242: /* We do not spinlock statistics.
1243: A window only exists if we have non-atomic adds, the error counts
1244: are typically zero, and statistics are non-critical. */
1245: np->stats.rx_missed_errors += readb(ioaddr + RxMissed);
1246: np->stats.tx_packets += readw(ioaddr + TxFramesOK);
1247: np->stats.rx_packets += readw(ioaddr + RxFramesOK);
1248: np->stats.collisions += readb(ioaddr + StatsLateColl);
1249: np->stats.collisions += readb(ioaddr + StatsMultiColl);
1250: np->stats.collisions += readb(ioaddr + StatsOneColl);
1251: readb(ioaddr + StatsCarrierError);
1252: readb(ioaddr + StatsTxDefer);
1253: for (i = StatsTxXSDefer; i <= StatsMcastRx; i++)
1254: readb(ioaddr + i);
1255: #if LINUX_VERSION_CODE > 0x20127
1256: np->stats.tx_bytes += readw(ioaddr + TxOctetsLow);
1257: np->stats.tx_bytes += readw(ioaddr + TxOctetsHigh) << 16;
1258: np->stats.rx_bytes += readw(ioaddr + RxOctetsLow);
1259: np->stats.rx_bytes += readw(ioaddr + RxOctetsHigh) << 16;
1260: #else
1261: readw(ioaddr + TxOctetsLow);
1262: readw(ioaddr + TxOctetsHigh);
1263: readw(ioaddr + RxOctetsLow);
1264: readw(ioaddr + RxOctetsHigh);
1265: #endif
1266:
1267: return &np->stats;
1268: }
1269:
1270: /* The little-endian AUTODIN II ethernet CRC calculations.
1271: A big-endian version is also available.
1272: This is slow but compact code. Do not use this routine for bulk data,
1273: use a table-based routine instead.
1274: This is common code and should be moved to net/core/crc.c.
1275: Chips may use the upper or lower CRC bits, and may reverse and/or invert
1276: them. Select the endian-ness that results in minimal calculations.
1277: */
1278: static unsigned const ethernet_polynomial_le = 0xedb88320U;
1279: static inline unsigned ether_crc_le(int length, unsigned char *data)
1280: {
1281: unsigned int crc = ~0; /* Initial value. */
1282: while(--length >= 0) {
1283: unsigned char current_octet = *data++;
1284: int bit;
1285: for (bit = 8; --bit >= 0; current_octet >>= 1) {
1286: if ((crc ^ current_octet) & 1) {
1287: crc >>= 1;
1288: crc ^= ethernet_polynomial_le;
1289: } else
1290: crc >>= 1;
1291: }
1292: }
1293: return crc;
1294: }
1295:
1296: static void set_rx_mode(struct net_device *dev)
1297: {
1298: struct netdev_private *np = (struct netdev_private *)dev->priv;
1299: long ioaddr = dev->base_addr;
1300: u16 mc_filter[4]; /* Multicast hash filter */
1301: u32 rx_mode;
1302: int i;
1303:
1304: if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
1305: /* Unconditionally log net taps. */
1306: printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);
1307: memset(mc_filter, ~0, sizeof(mc_filter));
1308: rx_mode = AcceptBroadcast | AcceptMulticast | AcceptAll | AcceptMyPhys;
1309: } else if ((dev->mc_count > np->multicast_filter_limit)
1310: || (dev->flags & IFF_ALLMULTI)) {
1311: /* Too many to match, or accept all multicasts. */
1312: memset(mc_filter, 0xff, sizeof(mc_filter));
1313: rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
1314: } else if (dev->mc_count) {
1315: struct dev_mc_list *mclist;
1316: memset(mc_filter, 0, sizeof(mc_filter));
1317: for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
1318: i++, mclist = mclist->next) {
1319: set_bit(ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x3f,
1320: mc_filter);
1321: }
1322: rx_mode = AcceptBroadcast | AcceptMultiHash | AcceptMyPhys;
1323: } else {
1324: writeb(AcceptBroadcast | AcceptMyPhys, ioaddr + RxMode);
1325: return;
1326: }
1327: for (i = 0; i < 4; i++)
1328: writew(mc_filter[i], ioaddr + MulticastFilter0 + i*2);
1329: writeb(rx_mode, ioaddr + RxMode);
1330: }
1331:
1332: static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1333: {
1334: struct netdev_private *np = (struct netdev_private *)dev->priv;
1335: u16 *data = (u16 *)&rq->ifr_data;
1336: u32 *data32 = (void *)&rq->ifr_data;
1337:
1338: switch(cmd) {
1339: case 0x8947: case 0x89F0:
1340: /* SIOCGMIIPHY: Get the address of the PHY in use. */
1341: data[0] = np->phys[0] & 0x1f;
1342: /* Fall Through */
1343: case 0x8948: case 0x89F1:
1344: /* SIOCGMIIREG: Read the specified MII register. */
1345: data[3] = mdio_read(dev, data[0] & 0x1f, data[1] & 0x1f);
1346: return 0;
1347: case 0x8949: case 0x89F2:
1348: /* SIOCSMIIREG: Write the specified MII register */
1349: if (!capable(CAP_NET_ADMIN))
1350: return -EPERM;
1351: if (data[0] == np->phys[0]) {
1352: u16 value = data[2];
1353: switch (data[1]) {
1354: case 0:
1355: /* Check for autonegotiation on or reset. */
1356: np->medialock = (value & 0x9000) ? 0 : 1;
1357: if (np->medialock)
1358: np->full_duplex = (value & 0x0100) ? 1 : 0;
1359: break;
1360: case 4: np->advertising = value; break;
1361: }
1362: /* Perhaps check_duplex(dev), depending on chip semantics. */
1363: }
1364: mdio_write(dev, data[0] & 0x1f, data[1] & 0x1f, data[2]);
1365: return 0;
1366: case SIOCGPARAMS:
1367: data32[0] = np->msg_level;
1368: data32[1] = np->multicast_filter_limit;
1369: data32[2] = np->max_interrupt_work;
1370: data32[3] = np->rx_copybreak;
1371: return 0;
1372: case SIOCSPARAMS:
1373: if (!capable(CAP_NET_ADMIN))
1374: return -EPERM;
1375: np->msg_level = data32[0];
1376: np->multicast_filter_limit = data32[1];
1377: np->max_interrupt_work = data32[2];
1378: np->rx_copybreak = data32[3];
1379: return 0;
1380: default:
1381: return -EOPNOTSUPP;
1382: }
1383: }
1384:
1385: static int sundance_pwr_event(void *dev_instance, int event)
1386: {
1387: struct net_device *dev = dev_instance;
1388: struct netdev_private *np = (struct netdev_private *)dev->priv;
1389: long ioaddr = dev->base_addr;
1390:
1391: if (np->msg_level & NETIF_MSG_LINK)
1392: printk(KERN_DEBUG "%s: Handling power event %d.\n", dev->name, event);
1393: switch(event) {
1394: case DRV_ATTACH:
1395: MOD_INC_USE_COUNT;
1396: break;
1397: case DRV_SUSPEND:
1398: /* Disable interrupts, stop Tx and Rx. */
1399: writew(0x0000, ioaddr + IntrEnable);
1400: writew(TxDisable | RxDisable | StatsDisable, ioaddr + MACCtrl1);
1401: break;
1402: case DRV_RESUME:
1403: sundance_start(dev);
1404: break;
1405: case DRV_DETACH: {
1406: struct net_device **devp, **next;
1407: if (dev->flags & IFF_UP) {
1408: /* Some, but not all, kernel versions close automatically. */
1409: dev_close(dev);
1410: dev->flags &= ~(IFF_UP|IFF_RUNNING);
1411: }
1412: unregister_netdev(dev);
1413: release_region(dev->base_addr, pci_id_tbl[np->chip_id].io_size);
1414: #ifndef USE_IO_OPS
1415: iounmap((char *)dev->base_addr);
1416: #endif
1417: for (devp = &root_net_dev; *devp; devp = next) {
1418: next = &((struct netdev_private *)(*devp)->priv)->next_module;
1419: if (*devp == dev) {
1420: *devp = *next;
1421: break;
1422: }
1423: }
1424: if (np->priv_addr)
1425: kfree(np->priv_addr);
1426: kfree(dev);
1427: MOD_DEC_USE_COUNT;
1428: break;
1429: }
1430: case DRV_PWR_WakeOn:
1431: writeb(readb(ioaddr + WakeEvent) | 2, ioaddr + WakeEvent);
1432: /* Fall through. */
1433: case DRV_PWR_DOWN:
1434: case DRV_PWR_UP:
1435: acpi_set_pwr_state(np->pci_dev, event==DRV_PWR_UP ? ACPI_D0:ACPI_D3);
1436: break;
1437: default:
1438: return -1;
1439: }
1440:
1441: return 0;
1442: }
1443:
1444: static int netdev_close(struct net_device *dev)
1445: {
1446: long ioaddr = dev->base_addr;
1447: struct netdev_private *np = (struct netdev_private *)dev->priv;
1448: int i;
1449:
1450: netif_stop_tx_queue(dev);
1451:
1452: if (np->msg_level & NETIF_MSG_IFDOWN) {
1453: printk(KERN_DEBUG "%s: Shutting down ethercard, status was Tx %2.2x "
1454: "Rx %4.4x Int %2.2x.\n",
1455: dev->name, (int)readw(ioaddr + TxStatus),
1456: (int)readl(ioaddr + RxStatus), (int)readw(ioaddr + IntrStatus));
1457: printk(KERN_DEBUG "%s: Queue pointers were Tx %d / %d, Rx %d / %d.\n",
1458: dev->name, np->cur_tx, np->dirty_tx, np->cur_rx, np->dirty_rx);
1459: }
1460:
1461: /* Disable interrupts by clearing the interrupt mask. */
1462: writew(0x0000, ioaddr + IntrEnable);
1463:
1464: /* Stop the chip's Tx and Rx processes. */
1465: writew(TxDisable | RxDisable | StatsDisable, ioaddr + MACCtrl1);
1466:
1467: del_timer(&np->timer);
1468:
1469: #ifdef __i386__
1470: if (np->msg_level & NETIF_MSG_IFDOWN) {
1471: printk("\n"KERN_DEBUG" Tx ring at %8.8x:\n",
1472: (int)virt_to_bus(np->tx_ring));
1473: for (i = 0; i < TX_RING_SIZE; i++)
1474: printk(" #%d desc. %4.4x %8.8x %8.8x.\n",
1475: i, np->tx_ring[i].status, np->tx_ring[i].frag[0].addr,
1476: np->tx_ring[i].frag[0].length);
1477: printk("\n"KERN_DEBUG " Rx ring %8.8x:\n",
1478: (int)virt_to_bus(np->rx_ring));
1479: for (i = 0; i < /*RX_RING_SIZE*/4 ; i++) {
1480: printk(KERN_DEBUG " #%d desc. %4.4x %4.4x %8.8x\n",
1481: i, np->rx_ring[i].status, np->rx_ring[i].frag[0].addr,
1482: np->rx_ring[i].frag[0].length);
1483: }
1484: }
1485: #endif /* __i386__ debugging only */
1486:
1487: free_irq(dev->irq, dev);
1488:
1489: /* Free all the skbuffs in the Rx queue. */
1490: for (i = 0; i < RX_RING_SIZE; i++) {
1491: np->rx_ring[i].status = 0;
1492: np->rx_ring[i].frag[0].addr = 0xBADF00D0; /* An invalid address. */
1493: if (np->rx_skbuff[i]) {
1494: #if LINUX_VERSION_CODE < 0x20100
1495: np->rx_skbuff[i]->free = 1;
1496: #endif
1497: dev_free_skb(np->rx_skbuff[i]);
1498: }
1499: np->rx_skbuff[i] = 0;
1500: }
1501: for (i = 0; i < TX_RING_SIZE; i++) {
1502: if (np->tx_skbuff[i])
1503: dev_free_skb(np->tx_skbuff[i]);
1504: np->tx_skbuff[i] = 0;
1505: }
1506:
1507: MOD_DEC_USE_COUNT;
1508:
1509: return 0;
1510: }
1511:
1512:
1513: #ifdef MODULE
1514: int init_module(void)
1515: {
1516: if (debug >= NETIF_MSG_DRV) /* Emit version even if no cards detected. */
1517: printk(KERN_INFO "%s" KERN_INFO "%s", version1, version2);
1518: return pci_drv_register(&sundance_drv_id, NULL);
1519: }
1520:
1521: void cleanup_module(void)
1522: {
1523: struct net_device *next_dev;
1524:
1525: pci_drv_unregister(&sundance_drv_id);
1526:
1527: /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1528: while (root_net_dev) {
1529: struct netdev_private *np = (void *)(root_net_dev->priv);
1530: unregister_netdev(root_net_dev);
1531: #ifdef USE_IO_OPS
1532: release_region(root_net_dev->base_addr,
1533: pci_id_tbl[np->chip_id].io_size);
1534: #else
1535: iounmap((char *)root_net_dev->base_addr);
1536: #endif
1537: next_dev = np->next_module;
1538: if (np->priv_addr)
1539: kfree(np->priv_addr);
1540: kfree(root_net_dev);
1541: root_net_dev = next_dev;
1542: }
1543: }
1544:
1545: #endif /* MODULE */
1546:
1547: /*
1548: * Local variables:
1549: * compile-command: "make KERNVER=`uname -r` sundance.o"
1550: * compile-cmd1: "gcc -DMODULE -Wall -Wstrict-prototypes -O6 -c sundance.c"
1551: * simple-compile-command: "gcc -DMODULE -O6 -c sundance.c"
1552: * c-indent-level: 4
1553: * c-basic-offset: 4
1554: * tab-width: 4
1555: * End:
1556: */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.