make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / net / ioc3-eth.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Driver for SGI's IOC3 based Ethernet cards as found in the PCI card.
7  *
8  * Copyright (C) 1999, 2000, 2001 Ralf Baechle
9  * Copyright (C) 1995, 1999, 2000, 2001 by Silicon Graphics, Inc.
10  *
11  * References:
12  *  o IOC3 ASIC specification 4.51, 1996-04-18
13  *  o IEEE 802.3 specification, 2000 edition
14  *  o DP38840A Specification, National Semiconductor, March 1997
15  *
16  * To do:
17  *
18  *  o Handle allocation failures in ioc3_alloc_skb() more gracefully.
19  *  o Handle allocation failures in ioc3_init_rings().
20  *  o Use prefetching for large packets.  What is a good lower limit for
21  *    prefetching?
22  *  o We're probably allocating a bit too much memory.
23  *  o Use hardware checksums.
24  *  o Convert to using a IOC3 meta driver.
25  *  o Which PHYs might possibly be attached to the IOC3 in real live,
26  *    which workarounds are required for them?  Do we ever have Lucent's?
27  *  o For the 2.5 branch kill the mii-tool ioctls.
28  */
29 #include <linux/config.h>
30 #include <linux/init.h>
31 #include <linux/delay.h>
32 #include <linux/kernel.h>
33 #include <linux/mm.h>
34 #include <linux/errno.h>
35 #include <linux/module.h>
36 #include <linux/pci.h>
37 #include <linux/crc32.h>
38
39 #ifdef CONFIG_SERIAL
40 #include <linux/serial.h>
41 #include <asm/serial.h>
42 #define IOC3_BAUD (22000000 / (3*16))
43 #define IOC3_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
44 #endif
45
46 #include <linux/netdevice.h>
47 #include <linux/etherdevice.h>
48 #include <linux/ethtool.h>
49 #include <linux/skbuff.h>
50 #include <linux/dp83840.h>
51
52 #include <asm/byteorder.h>
53 #include <asm/io.h>
54 #include <asm/pgtable.h>
55 #include <asm/uaccess.h>
56 #include <asm/sn/types.h>
57 #include <asm/sn/sn0/addrs.h>
58 #include <asm/sn/sn0/hubni.h>
59 #include <asm/sn/sn0/hubio.h>
60 #include <asm/sn/klconfig.h>
61 #include <asm/sn/ioc3.h>
62 #include <asm/sn/sn0/ip27.h>
63 #include <asm/pci/bridge.h>
64
65 /*
66  * 64 RX buffers.  This is tunable in the range of 16 <= x < 512.  The
67  * value must be a power of two.
68  */
69 #define RX_BUFFS 64
70
71 /* Timer state engine. */
72 enum ioc3_timer_state {
73         arbwait  = 0,   /* Waiting for auto negotiation to complete.          */
74         lupwait  = 1,   /* Auto-neg complete, awaiting link-up status.        */
75         ltrywait = 2,   /* Forcing try of all modes, from fastest to slowest. */
76         asleep   = 3,   /* Time inactive.                                     */
77 };
78
79 /* Private per NIC data of the driver.  */
80 struct ioc3_private {
81         struct ioc3 *regs;
82         int phy;
83         unsigned long *rxr;             /* pointer to receiver ring */
84         struct ioc3_etxd *txr;
85         struct sk_buff *rx_skbs[512];
86         struct sk_buff *tx_skbs[128];
87         struct net_device_stats stats;
88         int rx_ci;                      /* RX consumer index */
89         int rx_pi;                      /* RX producer index */
90         int tx_ci;                      /* TX consumer index */
91         int tx_pi;                      /* TX producer index */
92         int txqlen;
93         u32 emcr, ehar_h, ehar_l;
94         spinlock_t ioc3_lock;
95         struct net_device *dev;
96
97         /* Members used by autonegotiation  */
98         struct timer_list ioc3_timer;
99         enum ioc3_timer_state timer_state; /* State of auto-neg timer.     */
100         unsigned int timer_ticks;       /* Number of clicks at each state  */
101         unsigned short sw_bmcr;         /* sw copy of MII config register  */
102         unsigned short sw_bmsr;         /* sw copy of MII status register  */
103         unsigned short sw_physid1;      /* sw copy of PHYSID1              */
104         unsigned short sw_physid2;      /* sw copy of PHYSID2              */
105         unsigned short sw_advertise;    /* sw copy of ADVERTISE            */
106         unsigned short sw_lpa;          /* sw copy of LPA                  */
107         unsigned short sw_csconfig;     /* sw copy of CSCONFIG             */
108 };
109
110 static int ioc3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
111 static void ioc3_set_multicast_list(struct net_device *dev);
112 static int ioc3_start_xmit(struct sk_buff *skb, struct net_device *dev);
113 static void ioc3_timeout(struct net_device *dev);
114 static inline unsigned int ioc3_hash(const unsigned char *addr);
115 static inline void ioc3_stop(struct ioc3_private *ip);
116 static void ioc3_init(struct ioc3_private *ip);
117
118 static const char ioc3_str[] = "IOC3 Ethernet";
119
120 /* We use this to acquire receive skb's that we can DMA directly into. */
121 #define ALIGNED_RX_SKB_ADDR(addr) \
122         ((((unsigned long)(addr) + (128 - 1)) & ~(128 - 1)) - (unsigned long)(addr))
123
124 #define ioc3_alloc_skb(__length, __gfp_flags) \
125 ({      struct sk_buff *__skb; \
126         __skb = alloc_skb((__length) + 128, (__gfp_flags)); \
127         if (__skb) { \
128                 int __offset = ALIGNED_RX_SKB_ADDR(__skb->data); \
129                 if(__offset) \
130                         skb_reserve(__skb, __offset); \
131         } \
132         __skb; \
133 })
134
135 /* BEWARE: The IOC3 documentation documents the size of rx buffers as
136    1644 while it's actually 1664.  This one was nasty to track down ...  */
137 #define RX_OFFSET               10
138 #define RX_BUF_ALLOC_SIZE       (1664 + RX_OFFSET + 128)
139
140 /* DMA barrier to separate cached and uncached accesses.  */
141 #define BARRIER()                                                       \
142         __asm__("sync" ::: "memory")
143
144
145 #define IOC3_SIZE 0x100000
146
147 #define ioc3_r(reg)                                                     \
148 ({                                                                      \
149         u32 __res;                                                      \
150         __res = ioc3->reg;                                              \
151         __res;                                                          \
152 })
153
154 #define ioc3_w(reg,val)                                                 \
155 do {                                                                    \
156         (ioc3->reg = (val));                                            \
157 } while(0)
158
159 static inline u32
160 mcr_pack(u32 pulse, u32 sample)
161 {
162         return (pulse << 10) | (sample << 2);
163 }
164
165 static int
166 nic_wait(struct ioc3 *ioc3)
167 {
168         u32 mcr;
169
170         do {
171                 mcr = ioc3_r(mcr);
172         } while (!(mcr & 2));
173
174         return mcr & 1;
175 }
176
177 static int
178 nic_reset(struct ioc3 *ioc3)
179 {
180         int presence;
181
182         ioc3_w(mcr, mcr_pack(500, 65));
183         presence = nic_wait(ioc3);
184
185         ioc3_w(mcr, mcr_pack(0, 500));
186         nic_wait(ioc3);
187
188         return presence;
189 }
190
191 static inline int
192 nic_read_bit(struct ioc3 *ioc3)
193 {
194         int result;
195
196         ioc3_w(mcr, mcr_pack(6, 13));
197         result = nic_wait(ioc3);
198         ioc3_w(mcr, mcr_pack(0, 100));
199         nic_wait(ioc3);
200
201         return result;
202 }
203
204 static inline void
205 nic_write_bit(struct ioc3 *ioc3, int bit)
206 {
207         if (bit)
208                 ioc3_w(mcr, mcr_pack(6, 110));
209         else
210                 ioc3_w(mcr, mcr_pack(80, 30));
211
212         nic_wait(ioc3);
213 }
214
215 /*
216  * Read a byte from an iButton device
217  */
218 static u32
219 nic_read_byte(struct ioc3 *ioc3)
220 {
221         u32 result = 0;
222         int i;
223
224         for (i = 0; i < 8; i++)
225                 result = (result >> 1) | (nic_read_bit(ioc3) << 7);
226
227         return result;
228 }
229
230 /*
231  * Write a byte to an iButton device
232  */
233 static void
234 nic_write_byte(struct ioc3 *ioc3, int byte)
235 {
236         int i, bit;
237
238         for (i = 8; i; i--) {
239                 bit = byte & 1;
240                 byte >>= 1;
241
242                 nic_write_bit(ioc3, bit);
243         }
244 }
245
246 static u64
247 nic_find(struct ioc3 *ioc3, int *last)
248 {
249         int a, b, index, disc;
250         u64 address = 0;
251
252         nic_reset(ioc3);
253         /* Search ROM.  */
254         nic_write_byte(ioc3, 0xf0);
255
256         /* Algorithm from ``Book of iButton Standards''.  */
257         for (index = 0, disc = 0; index < 64; index++) {
258                 a = nic_read_bit(ioc3);
259                 b = nic_read_bit(ioc3);
260
261                 if (a && b) {
262                         printk("NIC search failed (not fatal).\n");
263                         *last = 0;
264                         return 0;
265                 }
266
267                 if (!a && !b) {
268                         if (index == *last) {
269                                 address |= 1UL << index;
270                         } else if (index > *last) {
271                                 address &= ~(1UL << index);
272                                 disc = index;
273                         } else if ((address & (1UL << index)) == 0)
274                                 disc = index;
275                         nic_write_bit(ioc3, address & (1UL << index));
276                         continue;
277                 } else {
278                         if (a)
279                                 address |= 1UL << index;
280                         else
281                                 address &= ~(1UL << index);
282                         nic_write_bit(ioc3, a);
283                         continue;
284                 }
285         }
286
287         *last = disc;
288
289         return address;
290 }
291
292 static int nic_init(struct ioc3 *ioc3)
293 {
294         const char *type;
295         u8 crc;
296         u8 serial[6];
297         int save = 0, i;
298
299         type = "unknown";
300
301         while (1) {
302                 u64 reg;
303                 reg = nic_find(ioc3, &save);
304
305                 switch (reg & 0xff) {
306                 case 0x91:
307                         type = "DS1981U";
308                         break;
309                 default:
310                         if (save == 0) {
311                                 /* Let the caller try again.  */
312                                 return -1;
313                         }
314                         continue;
315                 }
316
317                 nic_reset(ioc3);
318
319                 /* Match ROM.  */
320                 nic_write_byte(ioc3, 0x55);
321                 for (i = 0; i < 8; i++)
322                         nic_write_byte(ioc3, (reg >> (i << 3)) & 0xff);
323
324                 reg >>= 8; /* Shift out type.  */
325                 for (i = 0; i < 6; i++) {
326                         serial[i] = reg & 0xff;
327                         reg >>= 8;
328                 }
329                 crc = reg & 0xff;
330                 break;
331         }
332
333         printk("Found %s NIC", type);
334         if (type != "unknown") {
335                 printk (" registration number %02x:%02x:%02x:%02x:%02x:%02x,"
336                         " CRC %02x", serial[0], serial[1], serial[2],
337                         serial[3], serial[4], serial[5], crc);
338         }
339         printk(".\n");
340
341         return 0;
342 }
343
344 /*
345  * Read the NIC (Number-In-a-Can) device used to store the MAC address on
346  * SN0 / SN00 nodeboards and PCI cards.
347  */
348 static void ioc3_get_eaddr_nic(struct ioc3_private *ip)
349 {
350         struct ioc3 *ioc3 = ip->regs;
351         u8 nic[14];
352         int tries = 2; /* There may be some problem with the battery?  */
353         int i;
354
355         ioc3_w(gpcr_s, (1 << 21));
356
357         while (tries--) {
358                 if (!nic_init(ioc3))
359                         break;
360                 udelay(500);
361         }
362
363         if (tries < 0) {
364                 printk("Failed to read MAC address\n");
365                 return;
366         }
367
368         /* Read Memory.  */
369         nic_write_byte(ioc3, 0xf0);
370         nic_write_byte(ioc3, 0x00);
371         nic_write_byte(ioc3, 0x00);
372
373         for (i = 13; i >= 0; i--)
374                 nic[i] = nic_read_byte(ioc3);
375
376         for (i = 2; i < 8; i++)
377                 ip->dev->dev_addr[i - 2] = nic[i];
378 }
379
380 #if defined(CONFIG_IA64_SGI_SN1) || defined(CONFIG_IA64_SGI_SN2)
381 /*
382  * Get the ether-address on SN1 nodes
383  */
384 static void ioc3_get_eaddr_sn(struct ioc3_private *ip)
385 {
386         int ibrick_mac_addr_get(nasid_t, char *);
387         struct ioc3 *ioc3 = ip->regs;
388         nasid_t nasid_of_ioc3;
389         char io7eaddr[20];
390         long mac;
391         int err_val;
392
393         /*
394          * err_val = ibrick_mac_addr_get(get_nasid(), io7eaddr );
395          * 
396          * BAD!!  The above call uses get_nasid() and assumes that
397          * the ioc3 pointed to by struct ioc3 is hooked up to the
398          * cbrick that we're running on.  The proper way to make this call
399          * is to figure out which nasid the ioc3 is connected to
400          * and use that to call ibrick_mac_addr_get.  Below is
401          * a hack to do just that.
402          */
403
404         /*
405          * Get the nasid of the ioc3 from the ioc3's base addr.
406          * FIXME: the 8 at the end assumes we're in memory mode, 
407          * not node mode (for that, we'd change it to a 9).
408          * Is there a call to extract this info from a physical
409          * addr somewhere in an sn header file already?  If so,
410          * we should probably use that, or restructure this routine
411          * to use pci_dev and generic numa nodeid getting stuff.
412          */
413         nasid_of_ioc3 = (((unsigned long)ioc3 >> 33) & ~(-1 << 8));
414         err_val = ibrick_mac_addr_get(nasid_of_ioc3, io7eaddr );
415
416
417         if (err_val) {
418                 /* Couldn't read the eeprom; try OSLoadOptions. */
419                 printk("WARNING: ibrick_mac_addr_get failed: %d\n", err_val);
420
421                 /* this is where we hardwire the mac address
422                  * 1st ibrick had 08:00:69:11:34:75
423                  * 2nd ibrick had 08:00:69:11:35:35
424                  *
425                  * Eagan Machines:
426                  *      mankato1 08:00:69:11:BE:95
427                  *      warroad  08:00:69:11:bd:60
428                  *      duron    08:00:69:11:34:60
429                  *
430                  * an easy way to get the mac address is to hook
431                  * up an ip35, then from L1 do 'cti serial'
432                  * and then look for MAC line XXX THIS DOESN"T QUITE WORK!!
433                  */
434                 printk("ioc3_get_eaddr: setting ethernet address to:\n -----> ");
435                 ip->dev->dev_addr[0] = 0x8;
436                 ip->dev->dev_addr[1] = 0x0;
437                 ip->dev->dev_addr[2] = 0x69;
438                 ip->dev->dev_addr[3] = 0x11;
439                 ip->dev->dev_addr[4] = 0x34;
440                 ip->dev->dev_addr[5] = 0x60;
441         }
442         else {
443                 long simple_strtol(const char *,char **,unsigned int);
444
445                 mac = simple_strtol(io7eaddr, (char **)0, 16);
446                 ip->dev->dev_addr[0] = (mac >> 40) & 0xff;
447                 ip->dev->dev_addr[1] = (mac >> 32) & 0xff;
448                 ip->dev->dev_addr[2] = (mac >> 24) & 0xff;
449                 ip->dev->dev_addr[3] = (mac >> 16) & 0xff;
450                 ip->dev->dev_addr[4] = (mac >> 8) & 0xff;
451                 ip->dev->dev_addr[5] = mac & 0xff;
452         }
453 }
454 #endif
455
456 /*
457  * Ok, this is hosed by design.  It's necessary to know what machine the
458  * NIC is in in order to know how to read the NIC address.  We also have
459  * to know if it's a PCI card or a NIC in on the node board ...
460  */
461 static void ioc3_get_eaddr(struct ioc3_private *ip)
462 {
463         void (*do_get_eaddr)(struct ioc3_private *ip) = NULL;
464         int i;
465
466         /*
467          * We should also use this code for PCI cards, no matter what host
468          * machine but how to know that we're a PCI card?
469          */
470 #ifdef CONFIG_SGI_IP27
471         do_get_eaddr = ioc3_get_eaddr_nic;
472 #endif
473 #if defined(CONFIG_IA64_SGI_SN1) || defined(CONFIG_IA64_SGI_SN2)
474         do_get_eaddr = ioc3_get_eaddr_sn;
475 #endif
476
477         if (!do_get_eaddr) {
478                 printk(KERN_ERR "Don't know how to read MAC address of this "
479                        "IOC3 NIC\n");
480                 return;
481         }
482
483         printk("Ethernet address is ");
484         for (i = 0; i < 6; i++) {
485                 printk("%02x", ip->dev->dev_addr[i]);
486                 if (i < 7)
487                         printk(":");
488         }
489         printk(".\n");
490 }
491
492
493 /*
494  * Caller must hold the ioc3_lock ever for MII readers.  This is also
495  * used to protect the transmitter side but it's low contention.
496  */
497 static u16 mii_read(struct ioc3_private *ip, int reg)
498 {
499         struct ioc3 *ioc3 = ip->regs;
500         int phy = ip->phy;
501
502         while (ioc3->micr & MICR_BUSY);
503         ioc3->micr = (phy << MICR_PHYADDR_SHIFT) | reg | MICR_READTRIG;
504         while (ioc3->micr & MICR_BUSY);
505
506         return ioc3->midr_r & MIDR_DATA_MASK;
507 }
508
509 static void mii_write(struct ioc3_private *ip, int reg, u16 data)
510 {
511         struct ioc3 *ioc3 = ip->regs;
512         int phy = ip->phy;
513
514         while (ioc3->micr & MICR_BUSY);
515         ioc3->midr_w = data;
516         ioc3->micr = (phy << MICR_PHYADDR_SHIFT) | reg;
517         while (ioc3->micr & MICR_BUSY);
518 }
519
520 static int ioc3_mii_init(struct ioc3_private *ip);
521
522 static struct net_device_stats *ioc3_get_stats(struct net_device *dev)
523 {
524         struct ioc3_private *ip = dev->priv;
525         struct ioc3 *ioc3 = ip->regs;
526
527         ip->stats.collisions += (ioc3->etcdc & ETCDC_COLLCNT_MASK);
528         return &ip->stats;
529 }
530
531 static inline void
532 ioc3_rx(struct ioc3_private *ip)
533 {
534         struct sk_buff *skb, *new_skb;
535         struct ioc3 *ioc3 = ip->regs;
536         int rx_entry, n_entry, len;
537         struct ioc3_erxbuf *rxb;
538         unsigned long *rxr;
539         u32 w0, err;
540
541         rxr = (unsigned long *) ip->rxr;                /* Ring base */
542         rx_entry = ip->rx_ci;                           /* RX consume index */
543         n_entry = ip->rx_pi;
544
545         skb = ip->rx_skbs[rx_entry];
546         rxb = (struct ioc3_erxbuf *) (skb->data - RX_OFFSET);
547         w0 = be32_to_cpu(rxb->w0);
548
549         while (w0 & ERXBUF_V) {
550                 err = be32_to_cpu(rxb->err);            /* It's valid ...  */
551                 if (err & ERXBUF_GOODPKT) {
552                         len = ((w0 >> ERXBUF_BYTECNT_SHIFT) & 0x7ff) - 4;
553                         skb_trim(skb, len);
554                         skb->protocol = eth_type_trans(skb, ip->dev);
555
556                         new_skb = ioc3_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
557                         if (!new_skb) {
558                                 /* Ouch, drop packet and just recycle packet
559                                    to keep the ring filled.  */
560                                 ip->stats.rx_dropped++;
561                                 new_skb = skb;
562                                 goto next;
563                         }
564                         netif_rx(skb);
565
566                         ip->rx_skbs[rx_entry] = NULL;   /* Poison  */
567
568                         new_skb->dev = ip->dev;
569
570                         /* Because we reserve afterwards. */
571                         skb_put(new_skb, (1664 + RX_OFFSET));
572                         rxb = (struct ioc3_erxbuf *) new_skb->data;
573                         skb_reserve(new_skb, RX_OFFSET);
574
575                         ip->dev->last_rx = jiffies;
576                         ip->stats.rx_packets++;         /* Statistics */
577                         ip->stats.rx_bytes += len;
578                 } else {
579                         /* The frame is invalid and the skb never
580                            reached the network layer so we can just
581                            recycle it.  */
582                         new_skb = skb;
583                         ip->stats.rx_errors++;
584                 }
585                 if (err & ERXBUF_CRCERR)        /* Statistics */
586                         ip->stats.rx_crc_errors++;
587                 if (err & ERXBUF_FRAMERR)
588                         ip->stats.rx_frame_errors++;
589 next:
590                 ip->rx_skbs[n_entry] = new_skb;
591                 rxr[n_entry] = cpu_to_be32((0xa5UL << 56) |
592                                          ((unsigned long) rxb & TO_PHYS_MASK));
593                 rxb->w0 = 0;                            /* Clear valid flag */
594                 n_entry = (n_entry + 1) & 511;          /* Update erpir */
595
596                 /* Now go on to the next ring entry.  */
597                 rx_entry = (rx_entry + 1) & 511;
598                 skb = ip->rx_skbs[rx_entry];
599                 rxb = (struct ioc3_erxbuf *) (skb->data - RX_OFFSET);
600                 w0 = be32_to_cpu(rxb->w0);
601         }
602         ioc3->erpir = (n_entry << 3) | ERPIR_ARM;
603         ip->rx_pi = n_entry;
604         ip->rx_ci = rx_entry;
605 }
606
607 static inline void
608 ioc3_tx(struct ioc3_private *ip)
609 {
610         unsigned long packets, bytes;
611         struct ioc3 *ioc3 = ip->regs;
612         int tx_entry, o_entry;
613         struct sk_buff *skb;
614         u32 etcir;
615
616         spin_lock(&ip->ioc3_lock);
617         etcir = ioc3->etcir;
618
619         tx_entry = (etcir >> 7) & 127;
620         o_entry = ip->tx_ci;
621         packets = 0;
622         bytes = 0;
623
624         while (o_entry != tx_entry) {
625                 packets++;
626                 skb = ip->tx_skbs[o_entry];
627                 bytes += skb->len;
628                 dev_kfree_skb_irq(skb);
629                 ip->tx_skbs[o_entry] = NULL;
630
631                 o_entry = (o_entry + 1) & 127;          /* Next */
632
633                 etcir = ioc3->etcir;                    /* More pkts sent?  */
634                 tx_entry = (etcir >> 7) & 127;
635         }
636
637         ip->stats.tx_packets += packets;
638         ip->stats.tx_bytes += bytes;
639         ip->txqlen -= packets;
640
641         if (ip->txqlen < 128)
642                 netif_wake_queue(ip->dev);
643
644         ip->tx_ci = o_entry;
645         spin_unlock(&ip->ioc3_lock);
646 }
647
648 /*
649  * Deal with fatal IOC3 errors.  This condition might be caused by a hard or
650  * software problems, so we should try to recover
651  * more gracefully if this ever happens.  In theory we might be flooded
652  * with such error interrupts if something really goes wrong, so we might
653  * also consider to take the interface down.
654  */
655 static void
656 ioc3_error(struct ioc3_private *ip, u32 eisr)
657 {
658         struct net_device *dev = ip->dev;
659         unsigned char *iface = dev->name;
660
661         if (eisr & EISR_RXOFLO)
662                 printk(KERN_ERR "%s: RX overflow.\n", iface);
663         if (eisr & EISR_RXBUFOFLO)
664                 printk(KERN_ERR "%s: RX buffer overflow.\n", iface);
665         if (eisr & EISR_RXMEMERR)
666                 printk(KERN_ERR "%s: RX PCI error.\n", iface);
667         if (eisr & EISR_RXPARERR)
668                 printk(KERN_ERR "%s: RX SSRAM parity error.\n", iface);
669         if (eisr & EISR_TXBUFUFLO)
670                 printk(KERN_ERR "%s: TX buffer underflow.\n", iface);
671         if (eisr & EISR_TXMEMERR)
672                 printk(KERN_ERR "%s: TX PCI error.\n", iface);
673
674         ioc3_stop(ip);
675         ioc3_init(ip);
676         ioc3_mii_init(ip);
677
678         dev->trans_start = jiffies;
679         netif_wake_queue(dev);
680 }
681
682 /* The interrupt handler does all of the Rx thread work and cleans up
683    after the Tx thread.  */
684 static void ioc3_interrupt(int irq, void *_dev, struct pt_regs *regs)
685 {
686         struct net_device *dev = (struct net_device *)_dev;
687         struct ioc3_private *ip = dev->priv;
688         struct ioc3 *ioc3 = ip->regs;
689         const u32 enabled = EISR_RXTIMERINT | EISR_RXOFLO | EISR_RXBUFOFLO |
690                             EISR_RXMEMERR | EISR_RXPARERR | EISR_TXBUFUFLO |
691                             EISR_TXEXPLICIT | EISR_TXMEMERR;
692         u32 eisr;
693
694         eisr = ioc3->eisr & enabled;
695
696         while (eisr) {
697                 ioc3->eisr = eisr;
698                 ioc3->eisr;                             /* Flush */
699
700                 if (eisr & (EISR_RXOFLO | EISR_RXBUFOFLO | EISR_RXMEMERR |
701                             EISR_RXPARERR | EISR_TXBUFUFLO | EISR_TXMEMERR))
702                         ioc3_error(ip, eisr);
703                 if (eisr & EISR_RXTIMERINT)
704                         ioc3_rx(ip);
705                 if (eisr & EISR_TXEXPLICIT)
706                         ioc3_tx(ip);
707
708                 eisr = ioc3->eisr & enabled;
709         }
710 }
711
712 /*
713  * Auto negotiation.  The scheme is very simple.  We have a timer routine that
714  * keeps watching the auto negotiation process as it progresses.  The DP83840
715  * is first told to start doing it's thing, we set up the time and place the
716  * timer state machine in it's initial state.
717  *
718  * Here the timer peeks at the DP83840 status registers at each click to see
719  * if the auto negotiation has completed, we assume here that the DP83840 PHY
720  * will time out at some point and just tell us what (didn't) happen.  For
721  * complete coverage we only allow so many of the ticks at this level to run,
722  * when this has expired we print a warning message and try another strategy.
723  * This "other" strategy is to force the interface into various speed/duplex
724  * configurations and we stop when we see a link-up condition before the
725  * maximum number of "peek" ticks have occurred.
726  *
727  * Once a valid link status has been detected we configure the IOC3 to speak
728  * the most efficient protocol we could get a clean link for.  The priority
729  * for link configurations, highest first is:
730  *
731  *     100 Base-T Full Duplex
732  *     100 Base-T Half Duplex
733  *     10 Base-T Full Duplex
734  *     10 Base-T Half Duplex
735  *
736  * We start a new timer now, after a successful auto negotiation status has
737  * been detected.  This timer just waits for the link-up bit to get set in
738  * the BMCR of the DP83840.  When this occurs we print a kernel log message
739  * describing the link type in use and the fact that it is up.
740  *
741  * If a fatal error of some sort is signalled and detected in the interrupt
742  * service routine, and the chip is reset, or the link is ifconfig'd down
743  * and then back up, this entire process repeats itself all over again.
744  */
745 static int ioc3_try_next_permutation(struct ioc3_private *ip)
746 {
747         ip->sw_bmcr = mii_read(ip, MII_BMCR);
748
749         /* Downgrade from full to half duplex.  Only possible via ethtool.  */
750         if (ip->sw_bmcr & BMCR_FULLDPLX) {
751                 ip->sw_bmcr &= ~BMCR_FULLDPLX;
752                 mii_write(ip, MII_BMCR, ip->sw_bmcr);
753
754                 return 0;
755         }
756
757         /* Downgrade from 100 to 10. */
758         if (ip->sw_bmcr & BMCR_SPEED100) {
759                 ip->sw_bmcr &= ~BMCR_SPEED100;
760                 mii_write(ip, MII_BMCR, ip->sw_bmcr);
761
762                 return 0;
763         }
764
765         /* We've tried everything. */
766         return -1;
767 }
768
769 static void
770 ioc3_display_link_mode(struct ioc3_private *ip)
771 {
772         char *tmode = "";
773
774         ip->sw_lpa = mii_read(ip, MII_LPA);
775
776         if (ip->sw_lpa & (LPA_100HALF | LPA_100FULL)) {
777                 if (ip->sw_lpa & LPA_100FULL)
778                         tmode = "100Mb/s, Full Duplex";
779                 else
780                         tmode = "100Mb/s, Half Duplex";
781         } else {
782                 if (ip->sw_lpa & LPA_10FULL)
783                         tmode = "10Mb/s, Full Duplex";
784                 else
785                         tmode = "10Mb/s, Half Duplex";
786         }
787
788         printk(KERN_INFO "%s: Link is up at %s.\n", ip->dev->name, tmode);
789 }
790
791 static void
792 ioc3_display_forced_link_mode(struct ioc3_private *ip)
793 {
794         char *speed = "", *duplex = "";
795
796         ip->sw_bmcr = mii_read(ip, MII_BMCR);
797         if (ip->sw_bmcr & BMCR_SPEED100)
798                 speed = "100Mb/s, ";
799         else
800                 speed = "10Mb/s, ";
801         if (ip->sw_bmcr & BMCR_FULLDPLX)
802                 duplex = "Full Duplex.\n";
803         else
804                 duplex = "Half Duplex.\n";
805
806         printk(KERN_INFO "%s: Link has been forced up at %s%s", ip->dev->name,
807                speed, duplex);
808 }
809
810 static int ioc3_set_link_modes(struct ioc3_private *ip)
811 {
812         struct ioc3 *ioc3 = ip->regs;
813         int full;
814
815         /*
816          * All we care about is making sure the bigmac tx_cfg has a
817          * proper duplex setting.
818          */
819         if (ip->timer_state == arbwait) {
820                 ip->sw_lpa = mii_read(ip, MII_LPA);
821                 if (!(ip->sw_lpa & (LPA_10HALF | LPA_10FULL |
822                                     LPA_100HALF | LPA_100FULL)))
823                         goto no_response;
824                 if (ip->sw_lpa & LPA_100FULL)
825                         full = 1;
826                 else if (ip->sw_lpa & LPA_100HALF)
827                         full = 0;
828                 else if (ip->sw_lpa & LPA_10FULL)
829                         full = 1;
830                 else
831                         full = 0;
832         } else {
833                 /* Forcing a link mode. */
834                 ip->sw_bmcr = mii_read(ip, MII_BMCR);
835                 if (ip->sw_bmcr & BMCR_FULLDPLX)
836                         full = 1;
837                 else
838                         full = 0;
839         }
840
841         if (full)
842                 ip->emcr |= EMCR_DUPLEX;
843         else
844                 ip->emcr &= ~EMCR_DUPLEX;
845
846         ioc3->emcr = ip->emcr;
847         ioc3->emcr;
848
849         return 0;
850
851 no_response:
852
853         return 1;
854 }
855
856 static int is_lucent_phy(struct ioc3_private *ip)
857 {
858         unsigned short mr2, mr3;
859         int ret = 0;
860
861         mr2 = mii_read(ip, MII_PHYSID1);
862         mr3 = mii_read(ip, MII_PHYSID2);
863         if ((mr2 & 0xffff) == 0x0180 && ((mr3 & 0xffff) >> 10) == 0x1d) {
864                 ret = 1;
865         }
866
867         return ret;
868 }
869
870 static void ioc3_timer(unsigned long data)
871 {
872         struct ioc3_private *ip = (struct ioc3_private *) data;
873         int restart_timer = 0;
874
875         ip->timer_ticks++;
876         switch (ip->timer_state) {
877         case arbwait:
878                 /*
879                  * Only allow for 5 ticks, thats 10 seconds and much too
880                  * long to wait for arbitration to complete.
881                  */
882                 if (ip->timer_ticks >= 10) {
883                         /* Enter force mode. */
884         do_force_mode:
885                         ip->sw_bmcr = mii_read(ip, MII_BMCR);
886                         printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful,"
887                                " trying force link mode\n", ip->dev->name);
888                         ip->sw_bmcr = BMCR_SPEED100;
889                         mii_write(ip, MII_BMCR, ip->sw_bmcr);
890
891                         if (!is_lucent_phy(ip)) {
892                                 /*
893                                  * OK, seems we need do disable the transceiver
894                                  * for the first tick to make sure we get an
895                                  * accurate link state at the second tick.
896                                  */
897                                 ip->sw_csconfig = mii_read(ip, MII_CSCONFIG);
898                                 ip->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
899                                 mii_write(ip, MII_CSCONFIG, ip->sw_csconfig);
900                         }
901                         ip->timer_state = ltrywait;
902                         ip->timer_ticks = 0;
903                         restart_timer = 1;
904                 } else {
905                         /* Anything interesting happen? */
906                         ip->sw_bmsr = mii_read(ip, MII_BMSR);
907                         if (ip->sw_bmsr & BMSR_ANEGCOMPLETE) {
908                                 int ret;
909
910                                 /* Just what we've been waiting for... */
911                                 ret = ioc3_set_link_modes(ip);
912                                 if (ret) {
913                                         /* Ooops, something bad happened, go to
914                                          * force mode.
915                                          *
916                                          * XXX Broken hubs which don't support
917                                          * XXX 802.3u auto-negotiation make this
918                                          * XXX happen as well.
919                                          */
920                                         goto do_force_mode;
921                                 }
922
923                                 /*
924                                  * Success, at least so far, advance our state
925                                  * engine.
926                                  */
927                                 ip->timer_state = lupwait;
928                                 restart_timer = 1;
929                         } else {
930                                 restart_timer = 1;
931                         }
932                 }
933                 break;
934
935         case lupwait:
936                 /*
937                  * Auto negotiation was successful and we are awaiting a
938                  * link up status.  I have decided to let this timer run
939                  * forever until some sort of error is signalled, reporting
940                  * a message to the user at 10 second intervals.
941                  */
942                 ip->sw_bmsr = mii_read(ip, MII_BMSR);
943                 if (ip->sw_bmsr & BMSR_LSTATUS) {
944                         /*
945                          * Wheee, it's up, display the link mode in use and put
946                          * the timer to sleep.
947                          */
948                         ioc3_display_link_mode(ip);
949                         ip->timer_state = asleep;
950                         restart_timer = 0;
951                 } else {
952                         if (ip->timer_ticks >= 10) {
953                                 printk(KERN_NOTICE "%s: Auto negotiation successful, link still "
954                                        "not completely up.\n", ip->dev->name);
955                                 ip->timer_ticks = 0;
956                                 restart_timer = 1;
957                         } else {
958                                 restart_timer = 1;
959                         }
960                 }
961                 break;
962
963         case ltrywait:
964                 /*
965                  * Making the timeout here too long can make it take
966                  * annoyingly long to attempt all of the link mode
967                  * permutations, but then again this is essentially
968                  * error recovery code for the most part.
969                  */
970                 ip->sw_bmsr = mii_read(ip, MII_BMSR);
971                 ip->sw_csconfig = mii_read(ip, MII_CSCONFIG);
972                 if (ip->timer_ticks == 1) {
973                         if (!is_lucent_phy(ip)) {
974                                 /*
975                                  * Re-enable transceiver, we'll re-enable the
976                                  * transceiver next tick, then check link state
977                                  * on the following tick.
978                                  */
979                                 ip->sw_csconfig |= CSCONFIG_TCVDISAB;
980                                 mii_write(ip, MII_CSCONFIG, ip->sw_csconfig);
981                         }
982                         restart_timer = 1;
983                         break;
984                 }
985                 if (ip->timer_ticks == 2) {
986                         if (!is_lucent_phy(ip)) {
987                                 ip->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
988                                 mii_write(ip, MII_CSCONFIG, ip->sw_csconfig);
989                         }
990                         restart_timer = 1;
991                         break;
992                 }
993                 if (ip->sw_bmsr & BMSR_LSTATUS) {
994                         /* Force mode selection success. */
995                         ioc3_display_forced_link_mode(ip);
996                         ioc3_set_link_modes(ip);  /* XXX error? then what? */
997                         ip->timer_state = asleep;
998                         restart_timer = 0;
999                 } else {
1000                         if (ip->timer_ticks >= 4) { /* 6 seconds or so... */
1001                                 int ret;
1002
1003                                 ret = ioc3_try_next_permutation(ip);
1004                                 if (ret == -1) {
1005                                         /*
1006                                          * Aieee, tried them all, reset the
1007                                          * chip and try all over again.
1008                                          */
1009                                         printk(KERN_NOTICE "%s: Link down, "
1010                                                "cable problem?\n",
1011                                                ip->dev->name);
1012
1013                                         ioc3_init(ip);
1014                                         return;
1015                                 }
1016                                 if (!is_lucent_phy(ip)) {
1017                                         ip->sw_csconfig = mii_read(ip,
1018                                                             MII_CSCONFIG);
1019                                         ip->sw_csconfig |= CSCONFIG_TCVDISAB;
1020                                         mii_write(ip, MII_CSCONFIG,
1021                                                   ip->sw_csconfig);
1022                                 }
1023                                 ip->timer_ticks = 0;
1024                                 restart_timer = 1;
1025                         } else {
1026                                 restart_timer = 1;
1027                         }
1028                 }
1029                 break;
1030
1031         case asleep:
1032         default:
1033                 /* Can't happens.... */
1034                 printk(KERN_ERR "%s: Aieee, link timer is asleep but we got "
1035                        "one anyways!\n", ip->dev->name);
1036                 restart_timer = 0;
1037                 ip->timer_ticks = 0;
1038                 ip->timer_state = asleep; /* foo on you */
1039                 break;
1040         };
1041
1042         if (restart_timer) {
1043                 ip->ioc3_timer.expires = jiffies + ((12 * HZ)/10); /* 1.2s */
1044                 add_timer(&ip->ioc3_timer);
1045         }
1046 }
1047
1048 static void
1049 ioc3_start_auto_negotiation(struct ioc3_private *ip, struct ethtool_cmd *ep)
1050 {
1051         int timeout;
1052
1053         /* Read all of the registers we are interested in now. */
1054         ip->sw_bmsr      = mii_read(ip, MII_BMSR);
1055         ip->sw_bmcr      = mii_read(ip, MII_BMCR);
1056         ip->sw_physid1   = mii_read(ip, MII_PHYSID1);
1057         ip->sw_physid2   = mii_read(ip, MII_PHYSID2);
1058
1059         /* XXX Check BMSR_ANEGCAPABLE, should not be necessary though. */
1060
1061         ip->sw_advertise = mii_read(ip, MII_ADVERTISE);
1062         if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
1063                 /* Advertise everything we can support. */
1064                 if (ip->sw_bmsr & BMSR_10HALF)
1065                         ip->sw_advertise |= ADVERTISE_10HALF;
1066                 else
1067                         ip->sw_advertise &= ~ADVERTISE_10HALF;
1068
1069                 if (ip->sw_bmsr & BMSR_10FULL)
1070                         ip->sw_advertise |= ADVERTISE_10FULL;
1071                 else
1072                         ip->sw_advertise &= ~ADVERTISE_10FULL;
1073                 if (ip->sw_bmsr & BMSR_100HALF)
1074                         ip->sw_advertise |= ADVERTISE_100HALF;
1075                 else
1076                         ip->sw_advertise &= ~ADVERTISE_100HALF;
1077                 if (ip->sw_bmsr & BMSR_100FULL)
1078                         ip->sw_advertise |= ADVERTISE_100FULL;
1079                 else
1080                         ip->sw_advertise &= ~ADVERTISE_100FULL;
1081                 mii_write(ip, MII_ADVERTISE, ip->sw_advertise);
1082
1083                 /*
1084                  * XXX Currently no IOC3 card I know off supports 100BaseT4,
1085                  * XXX and this is because the DP83840 does not support it,
1086                  * XXX changes XXX would need to be made to the tx/rx logic in
1087                  * XXX the driver as well so I completely skip checking for it
1088                  * XXX in the BMSR for now.
1089                  */
1090
1091 #ifdef AUTO_SWITCH_DEBUG
1092                 ASD(("%s: Advertising [ ", ip->dev->name));
1093                 if (ip->sw_advertise & ADVERTISE_10HALF)
1094                         ASD(("10H "));
1095                 if (ip->sw_advertise & ADVERTISE_10FULL)
1096                         ASD(("10F "));
1097                 if (ip->sw_advertise & ADVERTISE_100HALF)
1098                         ASD(("100H "));
1099                 if (ip->sw_advertise & ADVERTISE_100FULL)
1100                         ASD(("100F "));
1101 #endif
1102
1103                 /* Enable Auto-Negotiation, this is usually on already... */
1104                 ip->sw_bmcr |= BMCR_ANENABLE;
1105                 mii_write(ip, MII_BMCR, ip->sw_bmcr);
1106
1107                 /* Restart it to make sure it is going. */
1108                 ip->sw_bmcr |= BMCR_ANRESTART;
1109                 mii_write(ip, MII_BMCR, ip->sw_bmcr);
1110
1111                 /* BMCR_ANRESTART self clears when the process has begun. */
1112
1113                 timeout = 64;  /* More than enough. */
1114                 while (--timeout) {
1115                         ip->sw_bmcr = mii_read(ip, MII_BMCR);
1116                         if (!(ip->sw_bmcr & BMCR_ANRESTART))
1117                                 break; /* got it. */
1118                         udelay(10);
1119                 }
1120                 if (!timeout) {
1121                         printk(KERN_ERR "%s: IOC3 would not start auto "
1122                                "negotiation BMCR=0x%04x\n",
1123                                ip->dev->name, ip->sw_bmcr);
1124                         printk(KERN_NOTICE "%s: Performing force link "
1125                                "detection.\n", ip->dev->name);
1126                         goto force_link;
1127                 } else {
1128                         ip->timer_state = arbwait;
1129                 }
1130         } else {
1131 force_link:
1132                 /*
1133                  * Force the link up, trying first a particular mode.  Either
1134                  * we are here at the request of ethtool or because the IOC3
1135                  * would not start to autoneg.
1136                  */
1137
1138                 /*
1139                  * Disable auto-negotiation in BMCR, enable the duplex and
1140                  * speed setting, init the timer state machine, and fire it off.
1141                  */
1142                 if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
1143                         ip->sw_bmcr = BMCR_SPEED100;
1144                 } else {
1145                         if (ep->speed == SPEED_100)
1146                                 ip->sw_bmcr = BMCR_SPEED100;
1147                         else
1148                                 ip->sw_bmcr = 0;
1149                         if (ep->duplex == DUPLEX_FULL)
1150                                 ip->sw_bmcr |= BMCR_FULLDPLX;
1151                 }
1152                 mii_write(ip, MII_BMCR, ip->sw_bmcr);
1153
1154                 if (!is_lucent_phy(ip)) {
1155                         /*
1156                          * OK, seems we need do disable the transceiver for the
1157                          * first tick to make sure we get an accurate link
1158                          * state at the second tick.
1159                          */
1160                         ip->sw_csconfig = mii_read(ip, MII_CSCONFIG);
1161                         ip->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
1162                         mii_write(ip, MII_CSCONFIG, ip->sw_csconfig);
1163                 }
1164                 ip->timer_state = ltrywait;
1165         }
1166
1167         del_timer(&ip->ioc3_timer);
1168         ip->timer_ticks = 0;
1169         ip->ioc3_timer.expires = jiffies + (12 * HZ)/10;  /* 1.2 sec. */
1170         ip->ioc3_timer.data = (unsigned long) ip;
1171         ip->ioc3_timer.function = &ioc3_timer;
1172         add_timer(&ip->ioc3_timer);
1173 }
1174
1175 static int ioc3_mii_init(struct ioc3_private *ip)
1176 {
1177         int i, found;
1178         u16 word;
1179
1180         found = 0;
1181         spin_lock_irq(&ip->ioc3_lock);
1182         for (i = 0; i < 32; i++) {
1183                 ip->phy = i;
1184                 word = mii_read(ip, 2);
1185                 if ((word != 0xffff) && (word != 0x0000)) {
1186                         found = 1;
1187                         break;                  /* Found a PHY          */
1188                 }
1189         }
1190         if (!found) {
1191                 spin_unlock_irq(&ip->ioc3_lock);
1192                 return -ENODEV;
1193         }
1194
1195         ioc3_start_auto_negotiation(ip, NULL);          // XXX ethtool
1196
1197         spin_unlock_irq(&ip->ioc3_lock);
1198
1199         return 0;
1200 }
1201
1202 static inline void
1203 ioc3_clean_rx_ring(struct ioc3_private *ip)
1204 {
1205         struct sk_buff *skb;
1206         int i;
1207
1208         for (i = ip->rx_ci; i & 15; i++) {
1209                 ip->rx_skbs[ip->rx_pi] = ip->rx_skbs[ip->rx_ci];
1210                 ip->rxr[ip->rx_pi++] = ip->rxr[ip->rx_ci++];
1211         }
1212         ip->rx_pi &= 511;
1213         ip->rx_ci &= 511;
1214
1215         for (i = ip->rx_ci; i != ip->rx_pi; i = (i+1) & 511) {
1216                 struct ioc3_erxbuf *rxb;
1217                 skb = ip->rx_skbs[i];
1218                 rxb = (struct ioc3_erxbuf *) (skb->data - RX_OFFSET);
1219                 rxb->w0 = 0;
1220         }
1221 }
1222
1223 static inline void
1224 ioc3_clean_tx_ring(struct ioc3_private *ip)
1225 {
1226         struct sk_buff *skb;
1227         int i;
1228
1229         for (i=0; i < 128; i++) {
1230                 skb = ip->tx_skbs[i];
1231                 if (skb) {
1232                         ip->tx_skbs[i] = NULL;
1233                         dev_kfree_skb_any(skb);
1234                 }
1235                 ip->txr[i].cmd = 0;
1236         }
1237         ip->tx_pi = 0;
1238         ip->tx_ci = 0;
1239 }
1240
1241 static void
1242 ioc3_free_rings(struct ioc3_private *ip)
1243 {
1244         struct sk_buff *skb;
1245         int rx_entry, n_entry;
1246
1247         if (ip->txr) {
1248                 ioc3_clean_tx_ring(ip);
1249                 free_pages((unsigned long)ip->txr, 2);
1250                 ip->txr = NULL;
1251         }
1252
1253         if (ip->rxr) {
1254                 n_entry = ip->rx_ci;
1255                 rx_entry = ip->rx_pi;
1256
1257                 while (n_entry != rx_entry) {
1258                         skb = ip->rx_skbs[n_entry];
1259                         if (skb)
1260                                 dev_kfree_skb_any(skb);
1261
1262                         n_entry = (n_entry + 1) & 511;
1263                 }
1264                 free_page((unsigned long)ip->rxr);
1265                 ip->rxr = NULL;
1266         }
1267 }
1268
1269 static void
1270 ioc3_alloc_rings(struct net_device *dev, struct ioc3_private *ip,
1271                  struct ioc3 *ioc3)
1272 {
1273         struct ioc3_erxbuf *rxb;
1274         unsigned long *rxr;
1275         int i;
1276
1277         if (ip->rxr == NULL) {
1278                 /* Allocate and initialize rx ring.  4kb = 512 entries  */
1279                 ip->rxr = (unsigned long *) get_free_page(GFP_ATOMIC);
1280                 rxr = (unsigned long *) ip->rxr;
1281                 if (!rxr)
1282                         printk("ioc3_alloc_rings(): get_free_page() failed!\n");
1283
1284                 /* Now the rx buffers.  The RX ring may be larger but
1285                    we only allocate 16 buffers for now.  Need to tune
1286                    this for performance and memory later.  */
1287                 for (i = 0; i < RX_BUFFS; i++) {
1288                         struct sk_buff *skb;
1289
1290                         skb = ioc3_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
1291                         if (!skb) {
1292                                 show_free_areas();
1293                                 continue;
1294                         }
1295
1296                         ip->rx_skbs[i] = skb;
1297                         skb->dev = dev;
1298
1299                         /* Because we reserve afterwards. */
1300                         skb_put(skb, (1664 + RX_OFFSET));
1301                         rxb = (struct ioc3_erxbuf *) skb->data;
1302                         rxr[i] = cpu_to_be64((0xa5UL << 56) |
1303                                         ((unsigned long) rxb & TO_PHYS_MASK));
1304                         skb_reserve(skb, RX_OFFSET);
1305                 }
1306                 ip->rx_ci = 0;
1307                 ip->rx_pi = RX_BUFFS;
1308         }
1309
1310         if (ip->txr == NULL) {
1311                 /* Allocate and initialize tx rings.  16kb = 128 bufs.  */
1312                 ip->txr = (struct ioc3_etxd *)__get_free_pages(GFP_KERNEL, 2);
1313                 if (!ip->txr)
1314                         printk("ioc3_alloc_rings(): get_free_page() failed!\n");
1315                 ip->tx_pi = 0;
1316                 ip->tx_ci = 0;
1317         }
1318 }
1319
1320 static void
1321 ioc3_init_rings(struct net_device *dev, struct ioc3_private *ip,
1322                 struct ioc3 *ioc3)
1323 {
1324         unsigned long ring;
1325
1326         ioc3_free_rings(ip);
1327         ioc3_alloc_rings(dev, ip, ioc3);
1328
1329         ioc3_clean_rx_ring(ip);
1330         ioc3_clean_tx_ring(ip);
1331
1332         /* Now the rx ring base, consume & produce registers.  */
1333         ring = (0xa5UL << 56) | ((unsigned long)ip->rxr & TO_PHYS_MASK);
1334         ioc3->erbr_h = ring >> 32;
1335         ioc3->erbr_l = ring & 0xffffffff;
1336         ioc3->ercir  = (ip->rx_ci << 3);
1337         ioc3->erpir  = (ip->rx_pi << 3) | ERPIR_ARM;
1338
1339         ring = (0xa5UL << 56) | ((unsigned long)ip->txr & TO_PHYS_MASK);
1340
1341         ip->txqlen = 0;                                 /* nothing queued  */
1342
1343         /* Now the tx ring base, consume & produce registers.  */
1344         ioc3->etbr_h = ring >> 32;
1345         ioc3->etbr_l = ring & 0xffffffff;
1346         ioc3->etpir  = (ip->tx_pi << 7);
1347         ioc3->etcir  = (ip->tx_ci << 7);
1348         ioc3->etcir;                                    /* Flush */
1349 }
1350
1351 static inline void
1352 ioc3_ssram_disc(struct ioc3_private *ip)
1353 {
1354         struct ioc3 *ioc3 = ip->regs;
1355         volatile u32 *ssram0 = &ioc3->ssram[0x0000];
1356         volatile u32 *ssram1 = &ioc3->ssram[0x4000];
1357         unsigned int pattern = 0x5555;
1358
1359         /* Assume the larger size SSRAM and enable parity checking */
1360         ioc3->emcr |= (EMCR_BUFSIZ | EMCR_RAMPAR);
1361
1362         *ssram0 = pattern;
1363         *ssram1 = ~pattern & IOC3_SSRAM_DM;
1364
1365         if ((*ssram0 & IOC3_SSRAM_DM) != pattern ||
1366             (*ssram1 & IOC3_SSRAM_DM) != (~pattern & IOC3_SSRAM_DM)) {
1367                 /* set ssram size to 64 KB */
1368                 ip->emcr = EMCR_RAMPAR;
1369                 ioc3->emcr &= ~EMCR_BUFSIZ;
1370         } else {
1371                 ip->emcr = EMCR_BUFSIZ | EMCR_RAMPAR;
1372         }
1373 }
1374
1375 static void ioc3_init(struct ioc3_private *ip)
1376 {
1377         struct net_device *dev = ip->dev;
1378         struct ioc3 *ioc3 = ip->regs;
1379
1380         del_timer(&ip->ioc3_timer);             /* Kill if running      */
1381
1382         ioc3->emcr = EMCR_RST;                  /* Reset                */
1383         ioc3->emcr;                             /* Flush WB             */
1384         udelay(4);                              /* Give it time ...     */
1385         ioc3->emcr = 0;
1386         ioc3->emcr;
1387
1388         /* Misc registers  */
1389         ioc3->erbar = 0;
1390         ioc3->etcsr = (17<<ETCSR_IPGR2_SHIFT) | (11<<ETCSR_IPGR1_SHIFT) | 21;
1391         ioc3->etcdc;                            /* Clear on read */
1392         ioc3->ercsr = 15;                       /* RX low watermark  */
1393         ioc3->ertr = 0;                         /* Interrupt immediately */
1394         ioc3->emar_h = (dev->dev_addr[5] << 8) | dev->dev_addr[4];
1395         ioc3->emar_l = (dev->dev_addr[3] << 24) | (dev->dev_addr[2] << 16) |
1396                        (dev->dev_addr[1] <<  8) | dev->dev_addr[0];
1397         ioc3->ehar_h = ip->ehar_h;
1398         ioc3->ehar_l = ip->ehar_l;
1399         ioc3->ersr = 42;                        /* XXX should be random */
1400
1401         ioc3_init_rings(ip->dev, ip, ioc3);
1402
1403         ip->emcr |= ((RX_OFFSET / 2) << EMCR_RXOFF_SHIFT) | EMCR_TXDMAEN |
1404                      EMCR_TXEN | EMCR_RXDMAEN | EMCR_RXEN;
1405         ioc3->emcr = ip->emcr;
1406         ioc3->eier = EISR_RXTIMERINT | EISR_RXOFLO | EISR_RXBUFOFLO |
1407                      EISR_RXMEMERR | EISR_RXPARERR | EISR_TXBUFUFLO |
1408                      EISR_TXEXPLICIT | EISR_TXMEMERR;
1409         ioc3->eier;
1410 }
1411
1412 static inline void ioc3_stop(struct ioc3_private *ip)
1413 {
1414         struct ioc3 *ioc3 = ip->regs;
1415
1416         ioc3->emcr = 0;                         /* Shutup */
1417         ioc3->eier = 0;                         /* Disable interrupts */
1418         ioc3->eier;                             /* Flush */
1419 }
1420
1421 static int
1422 ioc3_open(struct net_device *dev)
1423 {
1424         struct ioc3_private *ip = dev->priv;
1425
1426         if (request_irq(dev->irq, ioc3_interrupt, SA_SHIRQ, ioc3_str, dev)) {
1427                 printk(KERN_ERR "%s: Can't get irq %d\n", dev->name, dev->irq);
1428
1429                 return -EAGAIN;
1430         }
1431
1432         ip->ehar_h = 0;
1433         ip->ehar_l = 0;
1434         ioc3_init(ip);
1435
1436         netif_start_queue(dev);
1437         return 0;
1438 }
1439
1440 static int
1441 ioc3_close(struct net_device *dev)
1442 {
1443         struct ioc3_private *ip = dev->priv;
1444
1445         del_timer(&ip->ioc3_timer);
1446
1447         netif_stop_queue(dev);
1448
1449         ioc3_stop(ip);
1450         free_irq(dev->irq, dev);
1451
1452         ioc3_free_rings(ip);
1453         return 0;
1454 }
1455
1456 /*
1457  * MENET cards have four IOC3 chips, which are attached to two sets of
1458  * PCI slot resources each: the primary connections are on slots
1459  * 0..3 and the secondaries are on 4..7
1460  *
1461  * All four ethernets are brought out to connectors; six serial ports
1462  * (a pair from each of the first three IOC3s) are brought out to
1463  * MiniDINs; all other subdevices are left swinging in the wind, leave
1464  * them disabled.
1465  */
1466 static inline int ioc3_is_menet(struct pci_dev *pdev)
1467 {
1468         struct pci_dev *dev;
1469
1470         return pdev->bus->parent == NULL
1471                && (dev = pci_find_slot(pdev->bus->number, PCI_DEVFN(0, 0)))
1472                && dev->vendor == PCI_VENDOR_ID_SGI
1473                && dev->device == PCI_DEVICE_ID_SGI_IOC3
1474                && (dev = pci_find_slot(pdev->bus->number, PCI_DEVFN(1, 0)))
1475                && dev->vendor == PCI_VENDOR_ID_SGI
1476                && dev->device == PCI_DEVICE_ID_SGI_IOC3
1477                && (dev = pci_find_slot(pdev->bus->number, PCI_DEVFN(2, 0)))
1478                && dev->vendor == PCI_VENDOR_ID_SGI
1479                && dev->device == PCI_DEVICE_ID_SGI_IOC3;
1480 }
1481
1482 static void inline ioc3_serial_probe(struct pci_dev *pdev,
1483                                 struct ioc3 *ioc3)
1484 {
1485         struct serial_struct req;
1486
1487         /*
1488          * We need to recognice and treat the fourth MENET serial as it
1489          * does not have an SuperIO chip attached to it, therefore attempting
1490          * to access it will result in bus errors.  We call something an
1491          * MENET if PCI slot 0, 1, 2 and 3 of a master PCI bus all have an IOC3
1492          * in it.  This is paranoid but we want to avoid blowing up on a
1493          * showhorn PCI box that happens to have 4 IOC3 cards in it so it's
1494          * not paranoid enough ...
1495          */
1496         if (ioc3_is_menet(pdev) && PCI_SLOT(pdev->devfn) == 3)
1497                 return;
1498
1499         /* Register to interrupt zero because we share the interrupt with
1500            the serial driver which we don't properly support yet.  */
1501         memset(&req, 0, sizeof(req));
1502         req.irq             = 0;
1503         req.flags           = IOC3_COM_FLAGS;
1504         req.io_type         = SERIAL_IO_MEM;
1505         req.iomem_reg_shift = 0;
1506         req.baud_base       = IOC3_BAUD;
1507
1508         req.iomem_base      = (unsigned char *) &ioc3->sregs.uarta;
1509         register_serial(&req);
1510
1511         req.iomem_base      = (unsigned char *) &ioc3->sregs.uartb;
1512         register_serial(&req);
1513 }
1514
1515 static int __devinit ioc3_probe(struct pci_dev *pdev,
1516                                 const struct pci_device_id *ent)
1517 {
1518         struct net_device *dev = NULL;
1519         struct ioc3_private *ip;
1520         struct ioc3 *ioc3;
1521         unsigned long ioc3_base, ioc3_size;
1522         u32 vendor, model, rev;
1523         int err;
1524
1525         dev = alloc_etherdev(sizeof(struct ioc3_private));
1526         if (!dev)
1527                 return -ENOMEM;
1528
1529         err = pci_request_regions(pdev, "ioc3");
1530         if (err)
1531                 goto out_free;
1532
1533         SET_MODULE_OWNER(dev);
1534         ip = dev->priv;
1535         ip->dev = dev;
1536
1537         dev->irq = pdev->irq;
1538
1539         ioc3_base = pci_resource_start(pdev, 0);
1540         ioc3_size = pci_resource_len(pdev, 0);
1541         ioc3 = (struct ioc3 *) ioremap(ioc3_base, ioc3_size);
1542         if (!ioc3) {
1543                 printk(KERN_CRIT "ioc3eth(%s): ioremap failed, goodbye.\n",
1544                        pdev->slot_name);
1545                 err = -ENOMEM;
1546                 goto out_res;
1547         }
1548         ip->regs = ioc3;
1549
1550 #ifdef CONFIG_SERIAL
1551         ioc3_serial_probe(pdev, ioc3);
1552 #endif
1553
1554         spin_lock_init(&ip->ioc3_lock);
1555
1556         ioc3_stop(ip);
1557         ioc3_init(ip);
1558
1559         init_timer(&ip->ioc3_timer);
1560         ioc3_mii_init(ip);
1561
1562         if (ip->phy == -1) {
1563                 printk(KERN_CRIT "ioc3-eth(%s): Didn't find a PHY, goodbye.\n",
1564                        pdev->slot_name);
1565                 err = -ENODEV;
1566                 goto out_stop;
1567         }
1568
1569         ioc3_ssram_disc(ip);
1570         ioc3_get_eaddr(ip);
1571
1572         /* The IOC3-specific entries in the device structure. */
1573         dev->open               = ioc3_open;
1574         dev->hard_start_xmit    = ioc3_start_xmit;
1575         dev->tx_timeout         = ioc3_timeout;
1576         dev->watchdog_timeo     = 5 * HZ;
1577         dev->stop               = ioc3_close;
1578         dev->get_stats          = ioc3_get_stats;
1579         dev->do_ioctl           = ioc3_ioctl;
1580         dev->set_multicast_list = ioc3_set_multicast_list;
1581
1582         err = register_netdev(dev);
1583         if (err)
1584                 goto out_stop;
1585
1586         vendor = (ip->sw_physid1 << 12) | (ip->sw_physid2 >> 4);
1587         model  = (ip->sw_physid2 >> 4) & 0x3f;
1588         rev    = ip->sw_physid2 & 0xf;
1589         printk(KERN_INFO "%s: Using PHY %d, vendor 0x%x, model %d, "
1590                "rev %d.\n", dev->name, ip->phy, vendor, model, rev);
1591         printk(KERN_INFO "%s: IOC3 SSRAM has %d kbyte.\n", dev->name,
1592                ip->emcr & EMCR_BUFSIZ ? 128 : 64);
1593
1594         return 0;
1595
1596 out_stop:
1597         ioc3_stop(ip);
1598         free_irq(dev->irq, dev);
1599         ioc3_free_rings(ip);
1600 out_res:
1601         pci_release_regions(pdev);
1602 out_free:
1603         kfree(dev);
1604         return err;
1605 }
1606
1607 static void __devexit ioc3_remove_one (struct pci_dev *pdev)
1608 {
1609         struct net_device *dev = pci_get_drvdata(pdev);
1610         struct ioc3_private *ip = dev->priv;
1611         struct ioc3 *ioc3 = ip->regs;
1612
1613         iounmap(ioc3);
1614         pci_release_regions(pdev);
1615         kfree(dev);
1616 }
1617
1618 static struct pci_device_id ioc3_pci_tbl[] __devinitdata = {
1619         { PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC3, PCI_ANY_ID, PCI_ANY_ID },
1620         { 0 }
1621 };
1622 MODULE_DEVICE_TABLE(pci, ioc3_pci_tbl);
1623
1624 static struct pci_driver ioc3_driver = {
1625         name:           "ioc3-eth",
1626         id_table:       ioc3_pci_tbl,
1627         probe:          ioc3_probe,
1628         remove:         __devexit_p(ioc3_remove_one),
1629 };
1630
1631 static int __init ioc3_init_module(void)
1632 {
1633         return pci_module_init(&ioc3_driver);
1634 }
1635
1636 static void __exit ioc3_cleanup_module(void)
1637 {
1638         pci_unregister_driver(&ioc3_driver);
1639 }
1640
1641 static int
1642 ioc3_start_xmit(struct sk_buff *skb, struct net_device *dev)
1643 {
1644         unsigned long data;
1645         struct ioc3_private *ip = dev->priv;
1646         struct ioc3 *ioc3 = ip->regs;
1647         unsigned int len;
1648         struct ioc3_etxd *desc;
1649         int produce;
1650
1651         spin_lock_irq(&ip->ioc3_lock);
1652
1653         data = (unsigned long) skb->data;
1654         len = skb->len;
1655
1656         produce = ip->tx_pi;
1657         desc = &ip->txr[produce];
1658
1659         if (len <= 104) {
1660                 /* Short packet, let's copy it directly into the ring.  */
1661                 memcpy(desc->data, skb->data, skb->len);
1662                 if (len < ETH_ZLEN) {
1663                         /* Very short packet, pad with zeros at the end. */
1664                         memset(desc->data + len, 0, ETH_ZLEN - len);
1665                         len = ETH_ZLEN;
1666                 }
1667                 desc->cmd    = cpu_to_be32(len | ETXD_INTWHENDONE | ETXD_D0V);
1668                 desc->bufcnt = cpu_to_be32(len);
1669         } else if ((data ^ (data + len)) & 0x4000) {
1670                 unsigned long b2, s1, s2;
1671
1672                 b2 = (data | 0x3fffUL) + 1UL;
1673                 s1 = b2 - data;
1674                 s2 = data + len - b2;
1675
1676                 desc->cmd    = cpu_to_be32(len | ETXD_INTWHENDONE |
1677                                            ETXD_B1V | ETXD_B2V);
1678                 desc->bufcnt = cpu_to_be32((s1 << ETXD_B1CNT_SHIFT)
1679                                            | (s2 << ETXD_B2CNT_SHIFT));
1680                 desc->p1     = cpu_to_be64((0xa5UL << 56) |
1681                                            (data & TO_PHYS_MASK));
1682                 desc->p2     = cpu_to_be64((0xa5UL << 56) |
1683                                            (data & TO_PHYS_MASK));
1684         } else {
1685                 /* Normal sized packet that doesn't cross a page boundary. */
1686                 desc->cmd    = cpu_to_be32(len | ETXD_INTWHENDONE | ETXD_B1V);
1687                 desc->bufcnt = cpu_to_be32(len << ETXD_B1CNT_SHIFT);
1688                 desc->p1     = cpu_to_be64((0xa5UL << 56) |
1689                                            (data & TO_PHYS_MASK));
1690         }
1691
1692         BARRIER();
1693
1694         dev->trans_start = jiffies;
1695         ip->tx_skbs[produce] = skb;                     /* Remember skb */
1696         produce = (produce + 1) & 127;
1697         ip->tx_pi = produce;
1698         ioc3->etpir = produce << 7;                     /* Fire ... */
1699
1700         ip->txqlen++;
1701
1702         if (ip->txqlen > 127)
1703                 netif_stop_queue(dev);
1704
1705         spin_unlock_irq(&ip->ioc3_lock);
1706
1707         return 0;
1708 }
1709
1710 static void ioc3_timeout(struct net_device *dev)
1711 {
1712         struct ioc3_private *ip = dev->priv;
1713
1714         printk(KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
1715
1716         ioc3_stop(ip);
1717         ioc3_init(ip);
1718         ioc3_mii_init(ip);
1719
1720         dev->trans_start = jiffies;
1721         netif_wake_queue(dev);
1722 }
1723
1724 /*
1725  * Given a multicast ethernet address, this routine calculates the
1726  * address's bit index in the logical address filter mask
1727  */
1728
1729 static inline unsigned int
1730 ioc3_hash(const unsigned char *addr)
1731 {
1732         unsigned int temp = 0;
1733         unsigned char byte;
1734         u32 crc;
1735         int bits;
1736
1737         crc = ether_crc_le(ETH_ALEN, addr);
1738
1739         crc &= 0x3f;    /* bit reverse lowest 6 bits for hash index */
1740         for (bits = 6; --bits >= 0; ) {
1741                 temp <<= 1;
1742                 temp |= (crc & 0x1);
1743                 crc >>= 1;
1744         }
1745
1746         return temp;
1747 }
1748
1749
1750 /* We provide both the mii-tools and the ethtool ioctls.  */
1751 static int ioc3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1752 {
1753         struct ioc3_private *ip = dev->priv;
1754         struct ethtool_cmd *ep_user = (struct ethtool_cmd *) rq->ifr_data;
1755         u16 *data = (u16 *)&rq->ifr_data;
1756         struct ioc3 *ioc3 = ip->regs;
1757         struct ethtool_cmd ecmd;
1758
1759         switch (cmd) {
1760         case SIOCGMIIPHY:       /* Get the address of the PHY in use.  */
1761                 if (ip->phy == -1)
1762                         return -ENODEV;
1763                 data[0] = ip->phy;
1764                 return 0;
1765
1766         case SIOCGMIIREG: {     /* Read a PHY register.  */
1767                 unsigned int phy = data[0];
1768                 unsigned int reg = data[1];
1769
1770                 if (phy > 0x1f || reg > 0x1f)
1771                         return -EINVAL;
1772
1773                 spin_lock_irq(&ip->ioc3_lock);
1774                 while (ioc3->micr & MICR_BUSY);
1775                 ioc3->micr = (phy << MICR_PHYADDR_SHIFT) | reg | MICR_READTRIG;
1776                 while (ioc3->micr & MICR_BUSY);
1777                 data[3] = (ioc3->midr_r & MIDR_DATA_MASK);
1778                 spin_unlock_irq(&ip->ioc3_lock);
1779
1780                 return 0;
1781
1782         case SIOCSMIIREG:       /* Write a PHY register.  */
1783                 phy = data[0];
1784                 reg = data[1];
1785
1786                 if (!capable(CAP_NET_ADMIN))
1787                         return -EPERM;
1788
1789                 if (phy > 0x1f || reg > 0x1f)
1790                         return -EINVAL;
1791
1792                 spin_lock_irq(&ip->ioc3_lock);
1793                 while (ioc3->micr & MICR_BUSY);
1794                 ioc3->midr_w = data[2];
1795                 ioc3->micr = (phy << MICR_PHYADDR_SHIFT) | reg;
1796                 while (ioc3->micr & MICR_BUSY);
1797                 spin_unlock_irq(&ip->ioc3_lock);
1798
1799                 return 0;
1800                 }
1801         case SIOCETHTOOL:
1802                 if (copy_from_user(&ecmd, ep_user, sizeof(ecmd)))
1803                         return -EFAULT;
1804
1805                 if (ecmd.cmd == ETHTOOL_GSET) {
1806                         ecmd.supported =
1807                                 (SUPPORTED_10baseT_Half |
1808                                  SUPPORTED_10baseT_Full |
1809                                  SUPPORTED_100baseT_Half |
1810                                  SUPPORTED_100baseT_Full | SUPPORTED_Autoneg |
1811                                  SUPPORTED_TP | SUPPORTED_MII);
1812
1813                         ecmd.port = PORT_TP;
1814                         ecmd.transceiver = XCVR_INTERNAL;
1815                         ecmd.phy_address = ip->phy;
1816
1817                         /* Record PHY settings. */
1818                         spin_lock_irq(&ip->ioc3_lock);
1819                         ip->sw_bmcr = mii_read(ip, MII_BMCR);
1820                         ip->sw_lpa = mii_read(ip, MII_LPA);
1821                         spin_unlock_irq(&ip->ioc3_lock);
1822                         if (ip->sw_bmcr & BMCR_ANENABLE) {
1823                                 ecmd.autoneg = AUTONEG_ENABLE;
1824                                 ecmd.speed = (ip->sw_lpa &
1825                                      (LPA_100HALF | LPA_100FULL)) ?
1826                                      SPEED_100 : SPEED_10;
1827                         if (ecmd.speed == SPEED_100)
1828                                 ecmd.duplex = (ip->sw_lpa & (LPA_100FULL)) ?
1829                                               DUPLEX_FULL : DUPLEX_HALF;
1830                         else
1831                                 ecmd.duplex = (ip->sw_lpa & (LPA_10FULL)) ?
1832                                               DUPLEX_FULL : DUPLEX_HALF;
1833                         } else {
1834                                 ecmd.autoneg = AUTONEG_DISABLE;
1835                                 ecmd.speed = (ip->sw_bmcr & BMCR_SPEED100) ?
1836                                              SPEED_100 : SPEED_10;
1837                                 ecmd.duplex = (ip->sw_bmcr & BMCR_FULLDPLX) ?
1838                                               DUPLEX_FULL : DUPLEX_HALF;
1839                         }
1840                         if (copy_to_user(ep_user, &ecmd, sizeof(ecmd)))
1841                                 return -EFAULT;
1842                         return 0;
1843                 } else if (ecmd.cmd == ETHTOOL_SSET) {
1844                         if (!capable(CAP_NET_ADMIN))
1845                                 return -EPERM;
1846
1847                         /* Verify the settings we care about. */
1848                         if (ecmd.autoneg != AUTONEG_ENABLE &&
1849                             ecmd.autoneg != AUTONEG_DISABLE)
1850                                 return -EINVAL;
1851
1852                         if (ecmd.autoneg == AUTONEG_DISABLE &&
1853                             ((ecmd.speed != SPEED_100 &&
1854                               ecmd.speed != SPEED_10) ||
1855                              (ecmd.duplex != DUPLEX_HALF &&
1856                               ecmd.duplex != DUPLEX_FULL)))
1857                                 return -EINVAL;
1858
1859                         /* Ok, do it to it. */
1860                         del_timer(&ip->ioc3_timer);
1861                         spin_lock_irq(&ip->ioc3_lock);
1862                         ioc3_start_auto_negotiation(ip, &ecmd);
1863                         spin_unlock_irq(&ip->ioc3_lock);
1864
1865                         return 0;
1866                 } else
1867                 default:
1868                         return -EOPNOTSUPP;
1869         }
1870
1871         return -EOPNOTSUPP;
1872 }
1873
1874 static void ioc3_set_multicast_list(struct net_device *dev)
1875 {
1876         struct dev_mc_list *dmi = dev->mc_list;
1877         struct ioc3_private *ip = dev->priv;
1878         struct ioc3 *ioc3 = ip->regs;
1879         u64 ehar = 0;
1880         int i;
1881
1882         netif_stop_queue(dev);                          /* Lock out others. */
1883
1884         if (dev->flags & IFF_PROMISC) {                 /* Set promiscuous.  */
1885                 /* Unconditionally log net taps.  */
1886                 printk(KERN_INFO "%s: Promiscuous mode enabled.\n", dev->name);
1887                 ip->emcr |= EMCR_PROMISC;
1888                 ioc3->emcr = ip->emcr;
1889                 ioc3->emcr;
1890         } else {
1891                 ip->emcr &= ~EMCR_PROMISC;
1892                 ioc3->emcr = ip->emcr;                  /* Clear promiscuous. */
1893                 ioc3->emcr;
1894
1895                 if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
1896                         /* Too many for hashing to make sense or we want all
1897                            multicast packets anyway,  so skip computing all the
1898                            hashes and just accept all packets.  */
1899                         ip->ehar_h = 0xffffffff;
1900                         ip->ehar_l = 0xffffffff;
1901                 } else {
1902                         for (i = 0; i < dev->mc_count; i++) {
1903                                 char *addr = dmi->dmi_addr;
1904                                 dmi = dmi->next;
1905
1906                                 if (!(*addr & 1))
1907                                         continue;
1908
1909                                 ehar |= (1UL << ioc3_hash(addr));
1910                         }
1911                         ip->ehar_h = ehar >> 32;
1912                         ip->ehar_l = ehar & 0xffffffff;
1913                 }
1914                 ioc3->ehar_h = ip->ehar_h;
1915                 ioc3->ehar_l = ip->ehar_l;
1916         }
1917
1918         netif_wake_queue(dev);                  /* Let us get going again. */
1919 }
1920
1921 MODULE_AUTHOR("Ralf Baechle <ralf@oss.sgi.com>");
1922 MODULE_DESCRIPTION("SGI IOC3 Ethernet driver");
1923 MODULE_LICENSE("GPL");
1924
1925 module_init(ioc3_init_module);
1926 module_exit(ioc3_cleanup_module);