patch-2_6_7-vs1_9_1_12
[linux-2.6.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, 2003 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
30 #define IOC3_NAME       "ioc3-eth"
31 #define IOC3_VERSION    "2.6.3-3"
32
33 #include <linux/config.h>
34 #include <linux/init.h>
35 #include <linux/delay.h>
36 #include <linux/kernel.h>
37 #include <linux/mm.h>
38 #include <linux/errno.h>
39 #include <linux/module.h>
40 #include <linux/pci.h>
41 #include <linux/crc32.h>
42 #include <linux/mii.h>
43 #include <linux/in.h>
44 #include <linux/ip.h>
45 #include <linux/tcp.h>
46 #include <linux/udp.h>
47
48 #ifdef CONFIG_SERIAL_8250
49 #include <linux/serial.h>
50 #include <asm/serial.h>
51 #define IOC3_BAUD (22000000 / (3*16))
52 #define IOC3_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
53 #endif
54
55 #include <linux/netdevice.h>
56 #include <linux/etherdevice.h>
57 #include <linux/ethtool.h>
58 #include <linux/skbuff.h>
59 #include <linux/dp83840.h>
60 #include <net/ip.h>
61
62 #include <asm/byteorder.h>
63 #include <asm/checksum.h>
64 #include <asm/io.h>
65 #include <asm/pgtable.h>
66 #include <asm/uaccess.h>
67 #include <asm/sn/types.h>
68 #include <asm/sn/sn0/addrs.h>
69 #include <asm/sn/sn0/hubni.h>
70 #include <asm/sn/sn0/hubio.h>
71 #include <asm/sn/klconfig.h>
72 #include <asm/sn/ioc3.h>
73 #include <asm/sn/sn0/ip27.h>
74 #include <asm/pci/bridge.h>
75
76 /*
77  * 64 RX buffers.  This is tunable in the range of 16 <= x < 512.  The
78  * value must be a power of two.
79  */
80 #define RX_BUFFS 64
81
82 #define ETCSR_FD        ((17<<ETCSR_IPGR2_SHIFT) | (11<<ETCSR_IPGR1_SHIFT) | 21)
83 #define ETCSR_HD        ((21<<ETCSR_IPGR2_SHIFT) | (21<<ETCSR_IPGR1_SHIFT) | 21)
84
85 /* Private per NIC data of the driver.  */
86 struct ioc3_private {
87         struct ioc3 *regs;
88         unsigned long *rxr;             /* pointer to receiver ring */
89         struct ioc3_etxd *txr;
90         struct sk_buff *rx_skbs[512];
91         struct sk_buff *tx_skbs[128];
92         struct net_device_stats stats;
93         int rx_ci;                      /* RX consumer index */
94         int rx_pi;                      /* RX producer index */
95         int tx_ci;                      /* TX consumer index */
96         int tx_pi;                      /* TX producer index */
97         int txqlen;
98         u32 emcr, ehar_h, ehar_l;
99         spinlock_t ioc3_lock;
100         struct mii_if_info mii;
101         struct pci_dev *pdev;
102
103         /* Members used by autonegotiation  */
104         struct timer_list ioc3_timer;
105 };
106
107 static inline struct net_device *priv_netdev(struct ioc3_private *dev)
108 {
109         return (void *)dev - ((sizeof(struct net_device) + 31) & ~31);
110 }
111
112 static int ioc3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
113 static void ioc3_set_multicast_list(struct net_device *dev);
114 static int ioc3_start_xmit(struct sk_buff *skb, struct net_device *dev);
115 static void ioc3_timeout(struct net_device *dev);
116 static inline unsigned int ioc3_hash(const unsigned char *addr);
117 static inline void ioc3_stop(struct ioc3_private *ip);
118 static void ioc3_init(struct net_device *dev);
119
120 static const char ioc3_str[] = "IOC3 Ethernet";
121 static struct ethtool_ops ioc3_ethtool_ops;
122
123 /* We use this to acquire receive skb's that we can DMA directly into. */
124
125 #define IOC3_CACHELINE  128UL
126
127 static inline unsigned long aligned_rx_skb_addr(unsigned long addr)
128 {
129         return (~addr + 1) & (IOC3_CACHELINE - 1UL);
130 }
131
132 static inline struct sk_buff * ioc3_alloc_skb(unsigned long length,
133         unsigned int gfp_mask)
134 {
135         struct sk_buff *skb;
136
137         skb = alloc_skb(length + IOC3_CACHELINE - 1, gfp_mask);
138         if (likely(skb)) {
139                 int offset = aligned_rx_skb_addr((unsigned long) skb->data);
140                 if (offset)
141                         skb_reserve(skb, offset);
142         }
143
144         return skb;
145 }
146
147 static inline unsigned long ioc3_map(void *ptr, unsigned long vdev)
148 {
149 #ifdef CONFIG_SGI_IP27
150         vdev <<= 58;   /* Shift to PCI64_ATTR_VIRTUAL */
151
152         return vdev | (0xaUL << PCI64_ATTR_TARG_SHFT) | PCI64_ATTR_PREF |
153                ((unsigned long)ptr & TO_PHYS_MASK);
154 #else
155         return virt_to_bus(ptr);
156 #endif
157 }
158
159 /* BEWARE: The IOC3 documentation documents the size of rx buffers as
160    1644 while it's actually 1664.  This one was nasty to track down ...  */
161 #define RX_OFFSET               10
162 #define RX_BUF_ALLOC_SIZE       (1664 + RX_OFFSET + IOC3_CACHELINE)
163
164 /* DMA barrier to separate cached and uncached accesses.  */
165 #define BARRIER()                                                       \
166         __asm__("sync" ::: "memory")
167
168
169 #define IOC3_SIZE 0x100000
170
171 /*
172  * IOC3 is a big endian device
173  *
174  * Unorthodox but makes the users of these macros more readable - the pointer
175  * to the IOC3's memory mapped registers is expected as struct ioc3 * ioc3
176  * in the environment.
177  */
178 #define ioc3_r_mcr()            be32_to_cpu(ioc3->mcr)
179 #define ioc3_w_mcr(v)           do { ioc3->mcr = cpu_to_be32(v); } while (0)
180 #define ioc3_w_gpcr_s(v)        do { ioc3->gpcr_s = cpu_to_be32(v); } while (0)
181 #define ioc3_r_emcr()           be32_to_cpu(ioc3->emcr)
182 #define ioc3_w_emcr(v)          do { ioc3->emcr = cpu_to_be32(v); } while (0)
183 #define ioc3_r_eisr()           be32_to_cpu(ioc3->eisr)
184 #define ioc3_w_eisr(v)          do { ioc3->eisr = cpu_to_be32(v); } while (0)
185 #define ioc3_r_eier()           be32_to_cpu(ioc3->eier)
186 #define ioc3_w_eier(v)          do { ioc3->eier = cpu_to_be32(v); } while (0)
187 #define ioc3_r_ercsr()          be32_to_cpu(ioc3->ercsr)
188 #define ioc3_w_ercsr(v)         do { ioc3->ercsr = cpu_to_be32(v); } while (0)
189 #define ioc3_r_erbr_h()         be32_to_cpu(ioc3->erbr_h)
190 #define ioc3_w_erbr_h(v)        do { ioc3->erbr_h = cpu_to_be32(v); } while (0)
191 #define ioc3_r_erbr_l()         be32_to_cpu(ioc3->erbr_l)
192 #define ioc3_w_erbr_l(v)        do { ioc3->erbr_l = cpu_to_be32(v); } while (0)
193 #define ioc3_r_erbar()          be32_to_cpu(ioc3->erbar)
194 #define ioc3_w_erbar(v)         do { ioc3->erbar = cpu_to_be32(v); } while (0)
195 #define ioc3_r_ercir()          be32_to_cpu(ioc3->ercir)
196 #define ioc3_w_ercir(v)         do { ioc3->ercir = cpu_to_be32(v); } while (0)
197 #define ioc3_r_erpir()          be32_to_cpu(ioc3->erpir)
198 #define ioc3_w_erpir(v)         do { ioc3->erpir = cpu_to_be32(v); } while (0)
199 #define ioc3_r_ertr()           be32_to_cpu(ioc3->ertr)
200 #define ioc3_w_ertr(v)          do { ioc3->ertr = cpu_to_be32(v); } while (0)
201 #define ioc3_r_etcsr()          be32_to_cpu(ioc3->etcsr)
202 #define ioc3_w_etcsr(v)         do { ioc3->etcsr = cpu_to_be32(v); } while (0)
203 #define ioc3_r_ersr()           be32_to_cpu(ioc3->ersr)
204 #define ioc3_w_ersr(v)          do { ioc3->ersr = cpu_to_be32(v); } while (0)
205 #define ioc3_r_etcdc()          be32_to_cpu(ioc3->etcdc)
206 #define ioc3_w_etcdc(v)         do { ioc3->etcdc = cpu_to_be32(v); } while (0)
207 #define ioc3_r_ebir()           be32_to_cpu(ioc3->ebir)
208 #define ioc3_w_ebir(v)          do { ioc3->ebir = cpu_to_be32(v); } while (0)
209 #define ioc3_r_etbr_h()         be32_to_cpu(ioc3->etbr_h)
210 #define ioc3_w_etbr_h(v)        do { ioc3->etbr_h = cpu_to_be32(v); } while (0)
211 #define ioc3_r_etbr_l()         be32_to_cpu(ioc3->etbr_l)
212 #define ioc3_w_etbr_l(v)        do { ioc3->etbr_l = cpu_to_be32(v); } while (0)
213 #define ioc3_r_etcir()          be32_to_cpu(ioc3->etcir)
214 #define ioc3_w_etcir(v)         do { ioc3->etcir = cpu_to_be32(v); } while (0)
215 #define ioc3_r_etpir()          be32_to_cpu(ioc3->etpir)
216 #define ioc3_w_etpir(v)         do { ioc3->etpir = cpu_to_be32(v); } while (0)
217 #define ioc3_r_emar_h()         be32_to_cpu(ioc3->emar_h)
218 #define ioc3_w_emar_h(v)        do { ioc3->emar_h = cpu_to_be32(v); } while (0)
219 #define ioc3_r_emar_l()         be32_to_cpu(ioc3->emar_l)
220 #define ioc3_w_emar_l(v)        do { ioc3->emar_l = cpu_to_be32(v); } while (0)
221 #define ioc3_r_ehar_h()         be32_to_cpu(ioc3->ehar_h)
222 #define ioc3_w_ehar_h(v)        do { ioc3->ehar_h = cpu_to_be32(v); } while (0)
223 #define ioc3_r_ehar_l()         be32_to_cpu(ioc3->ehar_l)
224 #define ioc3_w_ehar_l(v)        do { ioc3->ehar_l = cpu_to_be32(v); } while (0)
225 #define ioc3_r_micr()           be32_to_cpu(ioc3->micr)
226 #define ioc3_w_micr(v)          do { ioc3->micr = cpu_to_be32(v); } while (0)
227 #define ioc3_r_midr_r()         be32_to_cpu(ioc3->midr_r)
228 #define ioc3_w_midr_r(v)        do { ioc3->midr_r = cpu_to_be32(v); } while (0)
229 #define ioc3_r_midr_w()         be32_to_cpu(ioc3->midr_w)
230 #define ioc3_w_midr_w(v)        do { ioc3->midr_w = cpu_to_be32(v); } while (0)
231
232 static inline u32 mcr_pack(u32 pulse, u32 sample)
233 {
234         return (pulse << 10) | (sample << 2);
235 }
236
237 static int nic_wait(struct ioc3 *ioc3)
238 {
239         u32 mcr;
240
241         do {
242                 mcr = ioc3_r_mcr();
243         } while (!(mcr & 2));
244
245         return mcr & 1;
246 }
247
248 static int nic_reset(struct ioc3 *ioc3)
249 {
250         int presence;
251
252         ioc3_w_mcr(mcr_pack(500, 65));
253         presence = nic_wait(ioc3);
254
255         ioc3_w_mcr(mcr_pack(0, 500));
256         nic_wait(ioc3);
257
258         return presence;
259 }
260
261 static inline int nic_read_bit(struct ioc3 *ioc3)
262 {
263         int result;
264
265         ioc3_w_mcr(mcr_pack(6, 13));
266         result = nic_wait(ioc3);
267         ioc3_w_mcr(mcr_pack(0, 100));
268         nic_wait(ioc3);
269
270         return result;
271 }
272
273 static inline void nic_write_bit(struct ioc3 *ioc3, int bit)
274 {
275         if (bit)
276                 ioc3_w_mcr(mcr_pack(6, 110));
277         else
278                 ioc3_w_mcr(mcr_pack(80, 30));
279
280         nic_wait(ioc3);
281 }
282
283 /*
284  * Read a byte from an iButton device
285  */
286 static u32 nic_read_byte(struct ioc3 *ioc3)
287 {
288         u32 result = 0;
289         int i;
290
291         for (i = 0; i < 8; i++)
292                 result = (result >> 1) | (nic_read_bit(ioc3) << 7);
293
294         return result;
295 }
296
297 /*
298  * Write a byte to an iButton device
299  */
300 static void nic_write_byte(struct ioc3 *ioc3, int byte)
301 {
302         int i, bit;
303
304         for (i = 8; i; i--) {
305                 bit = byte & 1;
306                 byte >>= 1;
307
308                 nic_write_bit(ioc3, bit);
309         }
310 }
311
312 static u64 nic_find(struct ioc3 *ioc3, int *last)
313 {
314         int a, b, index, disc;
315         u64 address = 0;
316
317         nic_reset(ioc3);
318         /* Search ROM.  */
319         nic_write_byte(ioc3, 0xf0);
320
321         /* Algorithm from ``Book of iButton Standards''.  */
322         for (index = 0, disc = 0; index < 64; index++) {
323                 a = nic_read_bit(ioc3);
324                 b = nic_read_bit(ioc3);
325
326                 if (a && b) {
327                         printk("NIC search failed (not fatal).\n");
328                         *last = 0;
329                         return 0;
330                 }
331
332                 if (!a && !b) {
333                         if (index == *last) {
334                                 address |= 1UL << index;
335                         } else if (index > *last) {
336                                 address &= ~(1UL << index);
337                                 disc = index;
338                         } else if ((address & (1UL << index)) == 0)
339                                 disc = index;
340                         nic_write_bit(ioc3, address & (1UL << index));
341                         continue;
342                 } else {
343                         if (a)
344                                 address |= 1UL << index;
345                         else
346                                 address &= ~(1UL << index);
347                         nic_write_bit(ioc3, a);
348                         continue;
349                 }
350         }
351
352         *last = disc;
353
354         return address;
355 }
356
357 static int nic_init(struct ioc3 *ioc3)
358 {
359         const char *type;
360         u8 crc;
361         u8 serial[6];
362         int save = 0, i;
363
364         type = "unknown";
365
366         while (1) {
367                 u64 reg;
368                 reg = nic_find(ioc3, &save);
369
370                 switch (reg & 0xff) {
371                 case 0x91:
372                         type = "DS1981U";
373                         break;
374                 default:
375                         if (save == 0) {
376                                 /* Let the caller try again.  */
377                                 return -1;
378                         }
379                         continue;
380                 }
381
382                 nic_reset(ioc3);
383
384                 /* Match ROM.  */
385                 nic_write_byte(ioc3, 0x55);
386                 for (i = 0; i < 8; i++)
387                         nic_write_byte(ioc3, (reg >> (i << 3)) & 0xff);
388
389                 reg >>= 8; /* Shift out type.  */
390                 for (i = 0; i < 6; i++) {
391                         serial[i] = reg & 0xff;
392                         reg >>= 8;
393                 }
394                 crc = reg & 0xff;
395                 break;
396         }
397
398         printk("Found %s NIC", type);
399         if (type != "unknown") {
400                 printk (" registration number %02x:%02x:%02x:%02x:%02x:%02x,"
401                         " CRC %02x", serial[0], serial[1], serial[2],
402                         serial[3], serial[4], serial[5], crc);
403         }
404         printk(".\n");
405
406         return 0;
407 }
408
409 /*
410  * Read the NIC (Number-In-a-Can) device used to store the MAC address on
411  * SN0 / SN00 nodeboards and PCI cards.
412  */
413 static void ioc3_get_eaddr_nic(struct ioc3_private *ip)
414 {
415         struct ioc3 *ioc3 = ip->regs;
416         u8 nic[14];
417         int tries = 2; /* There may be some problem with the battery?  */
418         int i;
419
420         ioc3_w_gpcr_s(1 << 21);
421
422         while (tries--) {
423                 if (!nic_init(ioc3))
424                         break;
425                 udelay(500);
426         }
427
428         if (tries < 0) {
429                 printk("Failed to read MAC address\n");
430                 return;
431         }
432
433         /* Read Memory.  */
434         nic_write_byte(ioc3, 0xf0);
435         nic_write_byte(ioc3, 0x00);
436         nic_write_byte(ioc3, 0x00);
437
438         for (i = 13; i >= 0; i--)
439                 nic[i] = nic_read_byte(ioc3);
440
441         for (i = 2; i < 8; i++)
442                 priv_netdev(ip)->dev_addr[i - 2] = nic[i];
443 }
444
445 /*
446  * Ok, this is hosed by design.  It's necessary to know what machine the
447  * NIC is in in order to know how to read the NIC address.  We also have
448  * to know if it's a PCI card or a NIC in on the node board ...
449  */
450 static void ioc3_get_eaddr(struct ioc3_private *ip)
451 {
452         int i;
453
454
455         ioc3_get_eaddr_nic(ip);
456
457         printk("Ethernet address is ");
458         for (i = 0; i < 6; i++) {
459                 printk("%02x", priv_netdev(ip)->dev_addr[i]);
460                 if (i < 5)
461                         printk(":");
462         }
463         printk(".\n");
464 }
465
466
467 /*
468  * Caller must hold the ioc3_lock ever for MII readers.  This is also
469  * used to protect the transmitter side but it's low contention.
470  */
471 static int ioc3_mdio_read(struct net_device *dev, int phy, int reg)
472 {
473         struct ioc3_private *ip = netdev_priv(dev);
474         struct ioc3 *ioc3 = ip->regs;
475
476         while (ioc3_r_micr() & MICR_BUSY);
477         ioc3_w_micr((phy << MICR_PHYADDR_SHIFT) | reg | MICR_READTRIG);
478         while (ioc3_r_micr() & MICR_BUSY);
479
480         return ioc3_r_micr() & MIDR_DATA_MASK;
481 }
482
483 static void ioc3_mdio_write(struct net_device *dev, int phy, int reg, int data)
484 {
485         struct ioc3_private *ip = netdev_priv(dev);
486         struct ioc3 *ioc3 = ip->regs;
487
488         while (ioc3_r_micr() & MICR_BUSY);
489         ioc3_w_midr_w(data);
490         ioc3_w_micr((phy << MICR_PHYADDR_SHIFT) | reg);
491         while (ioc3_r_micr() & MICR_BUSY);
492 }
493
494 static int ioc3_mii_init(struct ioc3_private *ip);
495
496 static struct net_device_stats *ioc3_get_stats(struct net_device *dev)
497 {
498         struct ioc3_private *ip = netdev_priv(dev);
499         struct ioc3 *ioc3 = ip->regs;
500
501         ip->stats.collisions += (ioc3_r_etcdc() & ETCDC_COLLCNT_MASK);
502         return &ip->stats;
503 }
504
505 #ifdef CONFIG_SGI_IOC3_ETH_HW_RX_CSUM
506
507 static void ioc3_tcpudp_checksum(struct sk_buff *skb, uint32_t hwsum, int len)
508 {
509         struct ethhdr *eh = skb->mac.ethernet;
510         uint32_t csum, ehsum;
511         unsigned int proto;
512         struct iphdr *ih;
513         uint16_t *ew;
514         unsigned char *cp;
515
516         /*
517          * Did hardware handle the checksum at all?  The cases we can handle
518          * are:
519          *
520          * - TCP and UDP checksums of IPv4 only.
521          * - IPv6 would be doable but we keep that for later ...
522          * - Only unfragmented packets.  Did somebody already tell you
523          *   fragmentation is evil?
524          * - don't care about packet size.  Worst case when processing a
525          *   malformed packet we'll try to access the packet at ip header +
526          *   64 bytes which is still inside the skb.  Even in the unlikely
527          *   case where the checksum is right the higher layers will still
528          *   drop the packet as appropriate.
529          */
530         if (eh->h_proto != ntohs(ETH_P_IP))
531                 return;
532
533         ih = (struct iphdr *) ((char *)eh + ETH_HLEN);
534         if (ih->frag_off & htons(IP_MF | IP_OFFSET))
535                 return;
536
537         proto = ih->protocol;
538         if (proto != IPPROTO_TCP && proto != IPPROTO_UDP)
539                 return;
540
541         /* Same as tx - compute csum of pseudo header  */
542         csum = hwsum +
543                (ih->tot_len - (ih->ihl << 2)) +
544                htons((uint16_t)ih->protocol) +
545                (ih->saddr >> 16) + (ih->saddr & 0xffff) +
546                (ih->daddr >> 16) + (ih->daddr & 0xffff);
547
548         /* Sum up ethernet dest addr, src addr and protocol  */
549         ew = (uint16_t *) eh;
550         ehsum = ew[0] + ew[1] + ew[2] + ew[3] + ew[4] + ew[5] + ew[6];
551
552         ehsum = (ehsum & 0xffff) + (ehsum >> 16);
553         ehsum = (ehsum & 0xffff) + (ehsum >> 16);
554
555         csum += 0xffff ^ ehsum;
556
557         /* In the next step we also subtract the 1's complement
558            checksum of the trailing ethernet CRC.  */
559         cp = (char *)eh + len;  /* points at trailing CRC */
560         if (len & 1) {
561                 csum += 0xffff ^ (uint16_t) ((cp[1] << 8) | cp[0]);
562                 csum += 0xffff ^ (uint16_t) ((cp[3] << 8) | cp[2]);
563         } else {
564                 csum += 0xffff ^ (uint16_t) ((cp[0] << 8) | cp[1]);
565                 csum += 0xffff ^ (uint16_t) ((cp[2] << 8) | cp[3]);
566         }
567
568         csum = (csum & 0xffff) + (csum >> 16);
569         csum = (csum & 0xffff) + (csum >> 16);
570
571         if (csum == 0xffff)
572                 skb->ip_summed = CHECKSUM_UNNECESSARY;
573 }
574 #endif /* CONFIG_SGI_IOC3_ETH_HW_RX_CSUM */
575
576 static inline void ioc3_rx(struct ioc3_private *ip)
577 {
578         struct sk_buff *skb, *new_skb;
579         struct ioc3 *ioc3 = ip->regs;
580         int rx_entry, n_entry, len;
581         struct ioc3_erxbuf *rxb;
582         unsigned long *rxr;
583         u32 w0, err;
584
585         rxr = (unsigned long *) ip->rxr;                /* Ring base */
586         rx_entry = ip->rx_ci;                           /* RX consume index */
587         n_entry = ip->rx_pi;
588
589         skb = ip->rx_skbs[rx_entry];
590         rxb = (struct ioc3_erxbuf *) (skb->data - RX_OFFSET);
591         w0 = be32_to_cpu(rxb->w0);
592
593         while (w0 & ERXBUF_V) {
594                 err = be32_to_cpu(rxb->err);            /* It's valid ...  */
595                 if (err & ERXBUF_GOODPKT) {
596                         len = ((w0 >> ERXBUF_BYTECNT_SHIFT) & 0x7ff) - 4;
597                         skb_trim(skb, len);
598                         skb->protocol = eth_type_trans(skb, priv_netdev(ip));
599
600                         new_skb = ioc3_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
601                         if (!new_skb) {
602                                 /* Ouch, drop packet and just recycle packet
603                                    to keep the ring filled.  */
604                                 ip->stats.rx_dropped++;
605                                 new_skb = skb;
606                                 goto next;
607                         }
608
609 #ifdef CONFIG_SGI_IOC3_ETH_HW_RX_CSUM
610                         ioc3_tcpudp_checksum(skb, w0 & ERXBUF_IPCKSUM_MASK,len);
611 #endif
612
613                         netif_rx(skb);
614
615                         ip->rx_skbs[rx_entry] = NULL;   /* Poison  */
616
617                         new_skb->dev = priv_netdev(ip);
618
619                         /* Because we reserve afterwards. */
620                         skb_put(new_skb, (1664 + RX_OFFSET));
621                         rxb = (struct ioc3_erxbuf *) new_skb->data;
622                         skb_reserve(new_skb, RX_OFFSET);
623
624                         priv_netdev(ip)->last_rx = jiffies;
625                         ip->stats.rx_packets++;         /* Statistics */
626                         ip->stats.rx_bytes += len;
627                 } else {
628                         /* The frame is invalid and the skb never
629                            reached the network layer so we can just
630                            recycle it.  */
631                         new_skb = skb;
632                         ip->stats.rx_errors++;
633                 }
634                 if (err & ERXBUF_CRCERR)        /* Statistics */
635                         ip->stats.rx_crc_errors++;
636                 if (err & ERXBUF_FRAMERR)
637                         ip->stats.rx_frame_errors++;
638 next:
639                 ip->rx_skbs[n_entry] = new_skb;
640                 rxr[n_entry] = cpu_to_be64(ioc3_map(rxb, 1));
641                 rxb->w0 = 0;                            /* Clear valid flag */
642                 n_entry = (n_entry + 1) & 511;          /* Update erpir */
643
644                 /* Now go on to the next ring entry.  */
645                 rx_entry = (rx_entry + 1) & 511;
646                 skb = ip->rx_skbs[rx_entry];
647                 rxb = (struct ioc3_erxbuf *) (skb->data - RX_OFFSET);
648                 w0 = be32_to_cpu(rxb->w0);
649         }
650         ioc3_w_erpir((n_entry << 3) | ERPIR_ARM);
651         ip->rx_pi = n_entry;
652         ip->rx_ci = rx_entry;
653 }
654
655 static inline void ioc3_tx(struct ioc3_private *ip)
656 {
657         unsigned long packets, bytes;
658         struct ioc3 *ioc3 = ip->regs;
659         int tx_entry, o_entry;
660         struct sk_buff *skb;
661         u32 etcir;
662
663         spin_lock(&ip->ioc3_lock);
664         etcir = ioc3_r_etcir();
665
666         tx_entry = (etcir >> 7) & 127;
667         o_entry = ip->tx_ci;
668         packets = 0;
669         bytes = 0;
670
671         while (o_entry != tx_entry) {
672                 packets++;
673                 skb = ip->tx_skbs[o_entry];
674                 bytes += skb->len;
675                 dev_kfree_skb_irq(skb);
676                 ip->tx_skbs[o_entry] = NULL;
677
678                 o_entry = (o_entry + 1) & 127;          /* Next */
679
680                 etcir = ioc3_r_etcir();                 /* More pkts sent?  */
681                 tx_entry = (etcir >> 7) & 127;
682         }
683
684         ip->stats.tx_packets += packets;
685         ip->stats.tx_bytes += bytes;
686         ip->txqlen -= packets;
687
688         if (ip->txqlen < 128)
689                 netif_wake_queue(priv_netdev(ip));
690
691         ip->tx_ci = o_entry;
692         spin_unlock(&ip->ioc3_lock);
693 }
694
695 /*
696  * Deal with fatal IOC3 errors.  This condition might be caused by a hard or
697  * software problems, so we should try to recover
698  * more gracefully if this ever happens.  In theory we might be flooded
699  * with such error interrupts if something really goes wrong, so we might
700  * also consider to take the interface down.
701  */
702 static void ioc3_error(struct ioc3_private *ip, u32 eisr)
703 {
704         struct net_device *dev = priv_netdev(ip);
705         unsigned char *iface = dev->name;
706
707         spin_lock(&ip->ioc3_lock);
708
709         if (eisr & EISR_RXOFLO)
710                 printk(KERN_ERR "%s: RX overflow.\n", iface);
711         if (eisr & EISR_RXBUFOFLO)
712                 printk(KERN_ERR "%s: RX buffer overflow.\n", iface);
713         if (eisr & EISR_RXMEMERR)
714                 printk(KERN_ERR "%s: RX PCI error.\n", iface);
715         if (eisr & EISR_RXPARERR)
716                 printk(KERN_ERR "%s: RX SSRAM parity error.\n", iface);
717         if (eisr & EISR_TXBUFUFLO)
718                 printk(KERN_ERR "%s: TX buffer underflow.\n", iface);
719         if (eisr & EISR_TXMEMERR)
720                 printk(KERN_ERR "%s: TX PCI error.\n", iface);
721
722         ioc3_stop(ip);
723         ioc3_init(dev);
724         ioc3_mii_init(ip);
725
726         netif_wake_queue(dev);
727
728         spin_unlock(&ip->ioc3_lock);
729 }
730
731 /* The interrupt handler does all of the Rx thread work and cleans up
732    after the Tx thread.  */
733 static irqreturn_t ioc3_interrupt(int irq, void *_dev, struct pt_regs *regs)
734 {
735         struct net_device *dev = (struct net_device *)_dev;
736         struct ioc3_private *ip = netdev_priv(dev);
737         struct ioc3 *ioc3 = ip->regs;
738         const u32 enabled = EISR_RXTIMERINT | EISR_RXOFLO | EISR_RXBUFOFLO |
739                             EISR_RXMEMERR | EISR_RXPARERR | EISR_TXBUFUFLO |
740                             EISR_TXEXPLICIT | EISR_TXMEMERR;
741         u32 eisr;
742
743         eisr = ioc3_r_eisr() & enabled;
744
745         ioc3_w_eisr(eisr);
746         (void) ioc3_r_eisr();                           /* Flush */
747
748         if (eisr & (EISR_RXOFLO | EISR_RXBUFOFLO | EISR_RXMEMERR |
749                     EISR_RXPARERR | EISR_TXBUFUFLO | EISR_TXMEMERR))
750                 ioc3_error(ip, eisr);
751         if (eisr & EISR_RXTIMERINT)
752                 ioc3_rx(ip);
753         if (eisr & EISR_TXEXPLICIT)
754                 ioc3_tx(ip);
755
756         return IRQ_HANDLED;
757 }
758
759 static inline void ioc3_setup_duplex(struct ioc3_private *ip)
760 {
761         struct ioc3 *ioc3 = ip->regs;
762
763         if (ip->mii.full_duplex) {
764                 ioc3_w_etcsr(ETCSR_FD);
765                 ip->emcr |= EMCR_DUPLEX;
766         } else {
767                 ioc3_w_etcsr(ETCSR_HD);
768                 ip->emcr &= ~EMCR_DUPLEX;
769         }
770         ioc3_w_emcr(ip->emcr);
771 }
772
773 static void ioc3_timer(unsigned long data)
774 {
775         struct ioc3_private *ip = (struct ioc3_private *) data;
776
777         /* Print the link status if it has changed */
778         mii_check_media(&ip->mii, 1, 0);
779         ioc3_setup_duplex(ip);
780
781         ip->ioc3_timer.expires = jiffies + ((12 * HZ)/10); /* 1.2s */
782         add_timer(&ip->ioc3_timer);
783 }
784
785 /*
786  * Try to find a PHY.  There is no apparent relation between the MII addresses
787  * in the SGI documentation and what we find in reality, so we simply probe
788  * for the PHY.  It seems IOC3 PHYs usually live on address 31.  One of my
789  * onboard IOC3s has the special oddity that probing doesn't seem to find it
790  * yet the interface seems to work fine, so if probing fails we for now will
791  * simply default to PHY 31 instead of bailing out.
792  */
793 static int ioc3_mii_init(struct ioc3_private *ip)
794 {
795         struct net_device *dev = priv_netdev(ip);
796         int i, found = 0, res = 0;
797         int ioc3_phy_workaround = 1;
798         u16 word;
799
800         for (i = 0; i < 32; i++) {
801                 word = ioc3_mdio_read(dev, i, MII_PHYSID1);
802
803                 if (word != 0xffff && word != 0x0000) {
804                         found = 1;
805                         break;                  /* Found a PHY          */
806                 }
807         }
808
809         if (!found) {
810                 if (ioc3_phy_workaround)
811                         i = 31;
812                 else {
813                         ip->mii.phy_id = -1;
814                         res = -ENODEV;
815                         goto out;
816                 }
817         }
818
819         ip->mii.phy_id = i;
820         ip->ioc3_timer.expires = jiffies + (12 * HZ)/10;  /* 1.2 sec. */
821         ip->ioc3_timer.data = (unsigned long) ip;
822         ip->ioc3_timer.function = &ioc3_timer;
823         add_timer(&ip->ioc3_timer);
824
825 out:
826         return res;
827 }
828
829 static inline void ioc3_clean_rx_ring(struct ioc3_private *ip)
830 {
831         struct sk_buff *skb;
832         int i;
833
834         for (i = ip->rx_ci; i & 15; i++) {
835                 ip->rx_skbs[ip->rx_pi] = ip->rx_skbs[ip->rx_ci];
836                 ip->rxr[ip->rx_pi++] = ip->rxr[ip->rx_ci++];
837         }
838         ip->rx_pi &= 511;
839         ip->rx_ci &= 511;
840
841         for (i = ip->rx_ci; i != ip->rx_pi; i = (i+1) & 511) {
842                 struct ioc3_erxbuf *rxb;
843                 skb = ip->rx_skbs[i];
844                 rxb = (struct ioc3_erxbuf *) (skb->data - RX_OFFSET);
845                 rxb->w0 = 0;
846         }
847 }
848
849 static inline void ioc3_clean_tx_ring(struct ioc3_private *ip)
850 {
851         struct sk_buff *skb;
852         int i;
853
854         for (i=0; i < 128; i++) {
855                 skb = ip->tx_skbs[i];
856                 if (skb) {
857                         ip->tx_skbs[i] = NULL;
858                         dev_kfree_skb_any(skb);
859                 }
860                 ip->txr[i].cmd = 0;
861         }
862         ip->tx_pi = 0;
863         ip->tx_ci = 0;
864 }
865
866 static void ioc3_free_rings(struct ioc3_private *ip)
867 {
868         struct sk_buff *skb;
869         int rx_entry, n_entry;
870
871         if (ip->txr) {
872                 ioc3_clean_tx_ring(ip);
873                 free_pages((unsigned long)ip->txr, 2);
874                 ip->txr = NULL;
875         }
876
877         if (ip->rxr) {
878                 n_entry = ip->rx_ci;
879                 rx_entry = ip->rx_pi;
880
881                 while (n_entry != rx_entry) {
882                         skb = ip->rx_skbs[n_entry];
883                         if (skb)
884                                 dev_kfree_skb_any(skb);
885
886                         n_entry = (n_entry + 1) & 511;
887                 }
888                 free_page((unsigned long)ip->rxr);
889                 ip->rxr = NULL;
890         }
891 }
892
893 static void ioc3_alloc_rings(struct net_device *dev)
894 {
895         struct ioc3_private *ip = netdev_priv(dev);
896         struct ioc3_erxbuf *rxb;
897         unsigned long *rxr;
898         int i;
899
900         if (ip->rxr == NULL) {
901                 /* Allocate and initialize rx ring.  4kb = 512 entries  */
902                 ip->rxr = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
903                 rxr = (unsigned long *) ip->rxr;
904                 if (!rxr)
905                         printk("ioc3_alloc_rings(): get_zeroed_page() failed!\n");
906
907                 /* Now the rx buffers.  The RX ring may be larger but
908                    we only allocate 16 buffers for now.  Need to tune
909                    this for performance and memory later.  */
910                 for (i = 0; i < RX_BUFFS; i++) {
911                         struct sk_buff *skb;
912
913                         skb = ioc3_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
914                         if (!skb) {
915                                 show_free_areas();
916                                 continue;
917                         }
918
919                         ip->rx_skbs[i] = skb;
920                         skb->dev = dev;
921
922                         /* Because we reserve afterwards. */
923                         skb_put(skb, (1664 + RX_OFFSET));
924                         rxb = (struct ioc3_erxbuf *) skb->data;
925                         rxr[i] = cpu_to_be64(ioc3_map(rxb, 1));
926                         skb_reserve(skb, RX_OFFSET);
927                 }
928                 ip->rx_ci = 0;
929                 ip->rx_pi = RX_BUFFS;
930         }
931
932         if (ip->txr == NULL) {
933                 /* Allocate and initialize tx rings.  16kb = 128 bufs.  */
934                 ip->txr = (struct ioc3_etxd *)__get_free_pages(GFP_KERNEL, 2);
935                 if (!ip->txr)
936                         printk("ioc3_alloc_rings(): __get_free_pages() failed!\n");
937                 ip->tx_pi = 0;
938                 ip->tx_ci = 0;
939         }
940 }
941
942 static void ioc3_init_rings(struct net_device *dev)
943 {
944         struct ioc3_private *ip = netdev_priv(dev);
945         struct ioc3 *ioc3 = ip->regs;
946         unsigned long ring;
947
948         ioc3_free_rings(ip);
949         ioc3_alloc_rings(dev);
950
951         ioc3_clean_rx_ring(ip);
952         ioc3_clean_tx_ring(ip);
953
954         /* Now the rx ring base, consume & produce registers.  */
955         ring = ioc3_map(ip->rxr, 0);
956         ioc3_w_erbr_h(ring >> 32);
957         ioc3_w_erbr_l(ring & 0xffffffff);
958         ioc3_w_ercir(ip->rx_ci << 3);
959         ioc3_w_erpir((ip->rx_pi << 3) | ERPIR_ARM);
960
961         ring = ioc3_map(ip->txr, 0);
962
963         ip->txqlen = 0;                                 /* nothing queued  */
964
965         /* Now the tx ring base, consume & produce registers.  */
966         ioc3_w_etbr_h(ring >> 32);
967         ioc3_w_etbr_l(ring & 0xffffffff);
968         ioc3_w_etpir(ip->tx_pi << 7);
969         ioc3_w_etcir(ip->tx_ci << 7);
970         (void) ioc3_r_etcir();                          /* Flush */
971 }
972
973 static inline void ioc3_ssram_disc(struct ioc3_private *ip)
974 {
975         struct ioc3 *ioc3 = ip->regs;
976         volatile u32 *ssram0 = &ioc3->ssram[0x0000];
977         volatile u32 *ssram1 = &ioc3->ssram[0x4000];
978         unsigned int pattern = 0x5555;
979
980         /* Assume the larger size SSRAM and enable parity checking */
981         ioc3_w_emcr(ioc3_r_emcr() | (EMCR_BUFSIZ | EMCR_RAMPAR));
982
983         *ssram0 = pattern;
984         *ssram1 = ~pattern & IOC3_SSRAM_DM;
985
986         if ((*ssram0 & IOC3_SSRAM_DM) != pattern ||
987             (*ssram1 & IOC3_SSRAM_DM) != (~pattern & IOC3_SSRAM_DM)) {
988                 /* set ssram size to 64 KB */
989                 ip->emcr = EMCR_RAMPAR;
990                 ioc3_w_emcr(ioc3_r_emcr() & ~EMCR_BUFSIZ);
991         } else
992                 ip->emcr = EMCR_BUFSIZ | EMCR_RAMPAR;
993 }
994
995 static void ioc3_init(struct net_device *dev)
996 {
997         struct ioc3_private *ip = netdev_priv(dev);
998         struct ioc3 *ioc3 = ip->regs;
999
1000         del_timer(&ip->ioc3_timer);             /* Kill if running      */
1001
1002         ioc3_w_emcr(EMCR_RST);                  /* Reset                */
1003         (void) ioc3_r_emcr();                   /* Flush WB             */
1004         udelay(4);                              /* Give it time ...     */
1005         ioc3_w_emcr(0);
1006         (void) ioc3_r_emcr();
1007
1008         /* Misc registers  */
1009 #ifdef CONFIG_SGI_IP27
1010         ioc3_w_erbar(PCI64_ATTR_BAR >> 32);     /* Barrier on last store */
1011 #else
1012         ioc3_w_erbar(0);                        /* Let PCI API get it right */
1013 #endif
1014         (void) ioc3_r_etcdc();                  /* Clear on read */
1015         ioc3_w_ercsr(15);                       /* RX low watermark  */
1016         ioc3_w_ertr(0);                         /* Interrupt immediately */
1017         ioc3_w_emar_h((dev->dev_addr[5] <<  8) | dev->dev_addr[4]);
1018         ioc3_w_emar_l((dev->dev_addr[3] << 24) | (dev->dev_addr[2] << 16) |
1019                       (dev->dev_addr[1] <<  8) | dev->dev_addr[0]);
1020         ioc3_w_ehar_h(ip->ehar_h);
1021         ioc3_w_ehar_l(ip->ehar_l);
1022         ioc3_w_ersr(42);                        /* XXX should be random */
1023
1024         ioc3_init_rings(dev);
1025
1026         ip->emcr |= ((RX_OFFSET / 2) << EMCR_RXOFF_SHIFT) | EMCR_TXDMAEN |
1027                      EMCR_TXEN | EMCR_RXDMAEN | EMCR_RXEN | EMCR_PADEN;
1028         ioc3_w_emcr(ip->emcr);
1029         ioc3_w_eier(EISR_RXTIMERINT | EISR_RXOFLO | EISR_RXBUFOFLO |
1030                     EISR_RXMEMERR | EISR_RXPARERR | EISR_TXBUFUFLO |
1031                     EISR_TXEXPLICIT | EISR_TXMEMERR);
1032         (void) ioc3_r_eier();
1033 }
1034
1035 static inline void ioc3_stop(struct ioc3_private *ip)
1036 {
1037         struct ioc3 *ioc3 = ip->regs;
1038
1039         ioc3_w_emcr(0);                         /* Shutup */
1040         ioc3_w_eier(0);                         /* Disable interrupts */
1041         (void) ioc3_r_eier();                   /* Flush */
1042 }
1043
1044 static int ioc3_open(struct net_device *dev)
1045 {
1046         struct ioc3_private *ip = netdev_priv(dev);
1047
1048         if (request_irq(dev->irq, ioc3_interrupt, SA_SHIRQ, ioc3_str, dev)) {
1049                 printk(KERN_ERR "%s: Can't get irq %d\n", dev->name, dev->irq);
1050
1051                 return -EAGAIN;
1052         }
1053
1054         ip->ehar_h = 0;
1055         ip->ehar_l = 0;
1056         ioc3_init(dev);
1057
1058         netif_start_queue(dev);
1059         return 0;
1060 }
1061
1062 static int ioc3_close(struct net_device *dev)
1063 {
1064         struct ioc3_private *ip = netdev_priv(dev);
1065
1066         del_timer(&ip->ioc3_timer);
1067
1068         netif_stop_queue(dev);
1069
1070         ioc3_stop(ip);
1071         free_irq(dev->irq, dev);
1072
1073         ioc3_free_rings(ip);
1074         return 0;
1075 }
1076
1077 /*
1078  * MENET cards have four IOC3 chips, which are attached to two sets of
1079  * PCI slot resources each: the primary connections are on slots
1080  * 0..3 and the secondaries are on 4..7
1081  *
1082  * All four ethernets are brought out to connectors; six serial ports
1083  * (a pair from each of the first three IOC3s) are brought out to
1084  * MiniDINs; all other subdevices are left swinging in the wind, leave
1085  * them disabled.
1086  */
1087 static inline int ioc3_is_menet(struct pci_dev *pdev)
1088 {
1089         struct pci_dev *dev;
1090
1091         return pdev->bus->parent == NULL
1092                && (dev = pci_find_slot(pdev->bus->number, PCI_DEVFN(0, 0)))
1093                && dev->vendor == PCI_VENDOR_ID_SGI
1094                && dev->device == PCI_DEVICE_ID_SGI_IOC3
1095                && (dev = pci_find_slot(pdev->bus->number, PCI_DEVFN(1, 0)))
1096                && dev->vendor == PCI_VENDOR_ID_SGI
1097                && dev->device == PCI_DEVICE_ID_SGI_IOC3
1098                && (dev = pci_find_slot(pdev->bus->number, PCI_DEVFN(2, 0)))
1099                && dev->vendor == PCI_VENDOR_ID_SGI
1100                && dev->device == PCI_DEVICE_ID_SGI_IOC3;
1101 }
1102
1103 /*
1104  * Note about serial ports and consoles:
1105  * For console output, everyone uses the IOC3 UARTA (offset 0x178)
1106  * connected to the master node (look in ip27_setup_console() and
1107  * ip27prom_console_write()).
1108  *
1109  * For serial (/dev/ttyS0 etc), we can not have hardcoded serial port
1110  * addresses on a partitioned machine. Since we currently use the ioc3
1111  * serial ports, we use dynamic serial port discovery that the serial.c
1112  * driver uses for pci/pnp ports (there is an entry for the SGI ioc3
1113  * boards in pci_boards[]). Unfortunately, UARTA's pio address is greater
1114  * than UARTB's, although UARTA on o200s has traditionally been known as
1115  * port 0. So, we just use one serial port from each ioc3 (since the
1116  * serial driver adds addresses to get to higher ports).
1117  *
1118  * The first one to do a register_console becomes the preferred console
1119  * (if there is no kernel command line console= directive). /dev/console
1120  * (ie 5, 1) is then "aliased" into the device number returned by the
1121  * "device" routine referred to in this console structure
1122  * (ip27prom_console_dev).
1123  *
1124  * Also look in ip27-pci.c:pci_fixuop_ioc3() for some comments on working
1125  * around ioc3 oddities in this respect.
1126  *
1127  * The IOC3 serials use a 22MHz clock rate with an additional divider by 3.
1128  * (IOC3_BAUD = (22000000 / (3*16)))
1129  */
1130
1131 static inline void ioc3_serial_probe(struct pci_dev *pdev,
1132                                 struct ioc3 *ioc3)
1133 {
1134         struct serial_struct req;
1135
1136         /*
1137          * We need to recognice and treat the fourth MENET serial as it
1138          * does not have an SuperIO chip attached to it, therefore attempting
1139          * to access it will result in bus errors.  We call something an
1140          * MENET if PCI slot 0, 1, 2 and 3 of a master PCI bus all have an IOC3
1141          * in it.  This is paranoid but we want to avoid blowing up on a
1142          * showhorn PCI box that happens to have 4 IOC3 cards in it so it's
1143          * not paranoid enough ...
1144          */
1145         if (ioc3_is_menet(pdev) && PCI_SLOT(pdev->devfn) == 3)
1146                 return;
1147
1148         /* Register to interrupt zero because we share the interrupt with
1149            the serial driver which we don't properly support yet.  */
1150         memset(&req, 0, sizeof(req));
1151         req.irq             = 0;
1152         req.flags           = IOC3_COM_FLAGS;
1153         req.io_type         = SERIAL_IO_MEM;
1154         req.iomem_reg_shift = 0;
1155         req.baud_base       = IOC3_BAUD;
1156
1157         req.iomem_base      = (unsigned char *) &ioc3->sregs.uarta;
1158         register_serial(&req);
1159
1160         req.iomem_base      = (unsigned char *) &ioc3->sregs.uartb;
1161         register_serial(&req);
1162 }
1163
1164 static int __devinit ioc3_probe(struct pci_dev *pdev,
1165                                 const struct pci_device_id *ent)
1166 {
1167         unsigned int sw_physid1, sw_physid2;
1168         struct net_device *dev = NULL;
1169         struct ioc3_private *ip;
1170         struct ioc3 *ioc3;
1171         unsigned long ioc3_base, ioc3_size;
1172         u32 vendor, model, rev;
1173         int err;
1174
1175         dev = alloc_etherdev(sizeof(struct ioc3_private));
1176         if (!dev)
1177                 return -ENOMEM;
1178
1179         err = pci_request_regions(pdev, "ioc3");
1180         if (err)
1181                 goto out_free;
1182
1183         SET_MODULE_OWNER(dev);
1184         SET_NETDEV_DEV(dev, &pdev->dev);
1185
1186         ip = netdev_priv(dev);
1187
1188         dev->irq = pdev->irq;
1189
1190         ioc3_base = pci_resource_start(pdev, 0);
1191         ioc3_size = pci_resource_len(pdev, 0);
1192         ioc3 = (struct ioc3 *) ioremap(ioc3_base, ioc3_size);
1193         if (!ioc3) {
1194                 printk(KERN_CRIT "ioc3eth(%s): ioremap failed, goodbye.\n",
1195                        pci_name(pdev));
1196                 err = -ENOMEM;
1197                 goto out_res;
1198         }
1199         ip->regs = ioc3;
1200
1201 #ifdef CONFIG_SERIAL_8250
1202         ioc3_serial_probe(pdev, ioc3);
1203 #endif
1204
1205         spin_lock_init(&ip->ioc3_lock);
1206         init_timer(&ip->ioc3_timer);
1207
1208         ioc3_stop(ip);
1209         ioc3_init(dev);
1210
1211         ip->pdev = pdev;
1212
1213         ip->mii.phy_id_mask = 0x1f;
1214         ip->mii.reg_num_mask = 0x1f;
1215         ip->mii.dev = dev;
1216         ip->mii.mdio_read = ioc3_mdio_read;
1217         ip->mii.mdio_write = ioc3_mdio_write;
1218
1219         ioc3_mii_init(ip);
1220
1221         if (ip->mii.phy_id == -1) {
1222                 printk(KERN_CRIT "ioc3-eth(%s): Didn't find a PHY, goodbye.\n",
1223                        pci_name(pdev));
1224                 err = -ENODEV;
1225                 goto out_stop;
1226         }
1227
1228         ioc3_ssram_disc(ip);
1229         ioc3_get_eaddr(ip);
1230
1231         /* The IOC3-specific entries in the device structure. */
1232         dev->open               = ioc3_open;
1233         dev->hard_start_xmit    = ioc3_start_xmit;
1234         dev->tx_timeout         = ioc3_timeout;
1235         dev->watchdog_timeo     = 5 * HZ;
1236         dev->stop               = ioc3_close;
1237         dev->get_stats          = ioc3_get_stats;
1238         dev->do_ioctl           = ioc3_ioctl;
1239         dev->set_multicast_list = ioc3_set_multicast_list;
1240         dev->ethtool_ops        = &ioc3_ethtool_ops;
1241 #ifdef CONFIG_SGI_IOC3_ETH_HW_TX_CSUM
1242         dev->features           = NETIF_F_IP_CSUM;
1243 #endif
1244
1245         ioc3_setup_duplex(ip);
1246         sw_physid1 = ioc3_mdio_read(dev, ip->mii.phy_id, MII_PHYSID1);
1247         sw_physid2 = ioc3_mdio_read(dev, ip->mii.phy_id, MII_PHYSID2);
1248
1249         err = register_netdev(dev);
1250         if (err)
1251                 goto out_stop;
1252
1253         mii_check_media(&ip->mii, 1, 1);
1254
1255         vendor = (sw_physid1 << 12) | (sw_physid2 >> 4);
1256         model  = (sw_physid2 >> 4) & 0x3f;
1257         rev    = sw_physid2 & 0xf;
1258         printk(KERN_INFO "%s: Using PHY %d, vendor 0x%x, model %d, "
1259                "rev %d.\n", dev->name, ip->mii.phy_id, vendor, model, rev);
1260         printk(KERN_INFO "%s: IOC3 SSRAM has %d kbyte.\n", dev->name,
1261                ip->emcr & EMCR_BUFSIZ ? 128 : 64);
1262
1263         return 0;
1264
1265 out_stop:
1266         ioc3_stop(ip);
1267         ioc3_free_rings(ip);
1268 out_res:
1269         pci_release_regions(pdev);
1270 out_free:
1271         free_netdev(dev);
1272         return err;
1273 }
1274
1275 static void __devexit ioc3_remove_one (struct pci_dev *pdev)
1276 {
1277         struct net_device *dev = pci_get_drvdata(pdev);
1278         struct ioc3_private *ip = netdev_priv(dev);
1279         struct ioc3 *ioc3 = ip->regs;
1280
1281         unregister_netdev(dev);
1282         iounmap(ioc3);
1283         pci_release_regions(pdev);
1284         free_netdev(dev);
1285 }
1286
1287 static struct pci_device_id ioc3_pci_tbl[] = {
1288         { PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC3, PCI_ANY_ID, PCI_ANY_ID },
1289         { 0 }
1290 };
1291 MODULE_DEVICE_TABLE(pci, ioc3_pci_tbl);
1292
1293 static struct pci_driver ioc3_driver = {
1294         .name           = "ioc3-eth",
1295         .id_table       = ioc3_pci_tbl,
1296         .probe          = ioc3_probe,
1297         .remove         = __devexit_p(ioc3_remove_one),
1298 };
1299
1300 static int __init ioc3_init_module(void)
1301 {
1302         return pci_module_init(&ioc3_driver);
1303 }
1304
1305 static void __exit ioc3_cleanup_module(void)
1306 {
1307         pci_unregister_driver(&ioc3_driver);
1308 }
1309
1310 static int ioc3_start_xmit(struct sk_buff *skb, struct net_device *dev)
1311 {
1312         unsigned long data;
1313         struct ioc3_private *ip = netdev_priv(dev);
1314         struct ioc3 *ioc3 = ip->regs;
1315         unsigned int len;
1316         struct ioc3_etxd *desc;
1317         uint32_t w0 = 0;
1318         int produce;
1319
1320 #ifdef CONFIG_SGI_IOC3_ETH_HW_TX_CSUM
1321         /*
1322          * IOC3 has a fairly simple minded checksumming hardware which simply
1323          * adds up the 1's complement checksum for the entire packet and
1324          * inserts it at an offset which can be specified in the descriptor
1325          * into the transmit packet.  This means we have to compensate for the
1326          * MAC header which should not be summed and the TCP/UDP pseudo headers
1327          * manually.
1328          */
1329         if (skb->ip_summed == CHECKSUM_HW) {
1330                 int proto = ntohs(skb->nh.iph->protocol);
1331                 unsigned int csoff;
1332                 struct iphdr *ih = skb->nh.iph;
1333                 uint32_t csum, ehsum;
1334                 uint16_t *eh;
1335
1336                 /* The MAC header.  skb->mac.ethernet seem the logic approach
1337                    to find the MAC header - except it's a NULL pointer ...  */
1338                 eh = (uint16_t *) skb->data;
1339
1340                 /* Sum up dest addr, src addr and protocol  */
1341                 ehsum = eh[0] + eh[1] + eh[2] + eh[3] + eh[4] + eh[5] + eh[6];
1342
1343                 /* Fold ehsum.  can't use csum_fold which negates also ...  */
1344                 ehsum = (ehsum & 0xffff) + (ehsum >> 16);
1345                 ehsum = (ehsum & 0xffff) + (ehsum >> 16);
1346
1347                 /* Skip IP header; it's sum is always zero and was
1348                    already filled in by ip_output.c */
1349                 csum = csum_tcpudp_nofold(ih->saddr, ih->daddr,
1350                                           ih->tot_len - (ih->ihl << 2),
1351                                           proto, 0xffff ^ ehsum);
1352
1353                 csum = (csum & 0xffff) + (csum >> 16);  /* Fold again */
1354                 csum = (csum & 0xffff) + (csum >> 16);
1355
1356                 csoff = ETH_HLEN + (ih->ihl << 2);
1357                 if (proto == IPPROTO_UDP) {
1358                         csoff += offsetof(struct udphdr, check);
1359                         skb->h.uh->check = csum;
1360                 }
1361                 if (proto == IPPROTO_TCP) {
1362                         csoff += offsetof(struct tcphdr, check);
1363                         skb->h.th->check = csum;
1364                 }
1365
1366                 w0 = ETXD_DOCHECKSUM | (csoff << ETXD_CHKOFF_SHIFT);
1367         }
1368 #endif /* CONFIG_SGI_IOC3_ETH_HW_TX_CSUM */
1369
1370         spin_lock_irq(&ip->ioc3_lock);
1371
1372         data = (unsigned long) skb->data;
1373         len = skb->len;
1374
1375         produce = ip->tx_pi;
1376         desc = &ip->txr[produce];
1377
1378         if (len <= 104) {
1379                 /* Short packet, let's copy it directly into the ring.  */
1380                 memcpy(desc->data, skb->data, skb->len);
1381                 if (len < ETH_ZLEN) {
1382                         /* Very short packet, pad with zeros at the end. */
1383                         memset(desc->data + len, 0, ETH_ZLEN - len);
1384                         len = ETH_ZLEN;
1385                 }
1386                 desc->cmd = cpu_to_be32(len | ETXD_INTWHENDONE | ETXD_D0V | w0);
1387                 desc->bufcnt = cpu_to_be32(len);
1388         } else if ((data ^ (data + len - 1)) & 0x4000) {
1389                 unsigned long b2 = (data | 0x3fffUL) + 1UL;
1390                 unsigned long s1 = b2 - data;
1391                 unsigned long s2 = data + len - b2;
1392
1393                 desc->cmd    = cpu_to_be32(len | ETXD_INTWHENDONE |
1394                                            ETXD_B1V | ETXD_B2V | w0);
1395                 desc->bufcnt = cpu_to_be32((s1 << ETXD_B1CNT_SHIFT) |
1396                                            (s2 << ETXD_B2CNT_SHIFT));
1397                 desc->p1     = cpu_to_be64(ioc3_map(skb->data, 1));
1398                 desc->p2     = cpu_to_be64(ioc3_map((void *) b2, 1));
1399         } else {
1400                 /* Normal sized packet that doesn't cross a page boundary. */
1401                 desc->cmd = cpu_to_be32(len | ETXD_INTWHENDONE | ETXD_B1V | w0);
1402                 desc->bufcnt = cpu_to_be32(len << ETXD_B1CNT_SHIFT);
1403                 desc->p1     = cpu_to_be64(ioc3_map(skb->data, 1));
1404         }
1405
1406         BARRIER();
1407
1408         dev->trans_start = jiffies;
1409         ip->tx_skbs[produce] = skb;                     /* Remember skb */
1410         produce = (produce + 1) & 127;
1411         ip->tx_pi = produce;
1412         ioc3_w_etpir(produce << 7);                     /* Fire ... */
1413
1414         ip->txqlen++;
1415
1416         if (ip->txqlen >= 127)
1417                 netif_stop_queue(dev);
1418
1419         spin_unlock_irq(&ip->ioc3_lock);
1420
1421         return 0;
1422 }
1423
1424 static void ioc3_timeout(struct net_device *dev)
1425 {
1426         struct ioc3_private *ip = netdev_priv(dev);
1427
1428         printk(KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
1429
1430         spin_lock_irq(&ip->ioc3_lock);
1431
1432         ioc3_stop(ip);
1433         ioc3_init(dev);
1434         ioc3_mii_init(ip);
1435
1436         spin_unlock_irq(&ip->ioc3_lock);
1437
1438         netif_wake_queue(dev);
1439 }
1440
1441 /*
1442  * Given a multicast ethernet address, this routine calculates the
1443  * address's bit index in the logical address filter mask
1444  */
1445
1446 static inline unsigned int ioc3_hash(const unsigned char *addr)
1447 {
1448         unsigned int temp = 0;
1449         u32 crc;
1450         int bits;
1451
1452         crc = ether_crc_le(ETH_ALEN, addr);
1453
1454         crc &= 0x3f;    /* bit reverse lowest 6 bits for hash index */
1455         for (bits = 6; --bits >= 0; ) {
1456                 temp <<= 1;
1457                 temp |= (crc & 0x1);
1458                 crc >>= 1;
1459         }
1460
1461         return temp;
1462 }
1463
1464 static void ioc3_get_drvinfo (struct net_device *dev,
1465         struct ethtool_drvinfo *info)
1466 {
1467         struct ioc3_private *ip = netdev_priv(dev);
1468                                                                                 
1469         strcpy (info->driver, IOC3_NAME);
1470         strcpy (info->version, IOC3_VERSION);
1471         strcpy (info->bus_info, pci_name(ip->pdev));
1472 }
1473
1474 static int ioc3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1475 {
1476         struct ioc3_private *ip = netdev_priv(dev);
1477         int rc;
1478
1479         spin_lock_irq(&ip->ioc3_lock);
1480         rc = mii_ethtool_gset(&ip->mii, cmd);
1481         spin_unlock_irq(&ip->ioc3_lock);
1482
1483         return rc;
1484 }
1485
1486 static int ioc3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1487 {
1488         struct ioc3_private *ip = netdev_priv(dev);
1489         int rc;
1490
1491         spin_lock_irq(&ip->ioc3_lock);
1492         rc = mii_ethtool_sset(&ip->mii, cmd);
1493         spin_unlock_irq(&ip->ioc3_lock);
1494                                                                         
1495         return rc;
1496 }
1497
1498 static int ioc3_nway_reset(struct net_device *dev)
1499 {
1500         struct ioc3_private *ip = netdev_priv(dev);
1501         int rc;
1502
1503         spin_lock_irq(&ip->ioc3_lock);
1504         rc = mii_nway_restart(&ip->mii);
1505         spin_unlock_irq(&ip->ioc3_lock);
1506
1507         return rc;
1508 }
1509
1510 static u32 ioc3_get_link(struct net_device *dev)
1511 {
1512         struct ioc3_private *ip = netdev_priv(dev);
1513         int rc;
1514
1515         spin_lock_irq(&ip->ioc3_lock);
1516         rc = mii_link_ok(&ip->mii);
1517         spin_unlock_irq(&ip->ioc3_lock);
1518
1519         return rc;
1520 }
1521
1522 static struct ethtool_ops ioc3_ethtool_ops = {
1523         .get_drvinfo            = ioc3_get_drvinfo,
1524         .get_settings           = ioc3_get_settings,
1525         .set_settings           = ioc3_set_settings,
1526         .nway_reset             = ioc3_nway_reset,
1527         .get_link               = ioc3_get_link,
1528 };
1529
1530 static int ioc3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1531 {
1532         struct ioc3_private *ip = netdev_priv(dev);
1533         int rc;
1534
1535         spin_lock_irq(&ip->ioc3_lock);
1536         rc = generic_mii_ioctl(&ip->mii, if_mii(rq), cmd, NULL);
1537         spin_unlock_irq(&ip->ioc3_lock);
1538
1539         return rc;
1540 }
1541
1542 static void ioc3_set_multicast_list(struct net_device *dev)
1543 {
1544         struct dev_mc_list *dmi = dev->mc_list;
1545         struct ioc3_private *ip = netdev_priv(dev);
1546         struct ioc3 *ioc3 = ip->regs;
1547         u64 ehar = 0;
1548         int i;
1549
1550         netif_stop_queue(dev);                          /* Lock out others. */
1551
1552         if (dev->flags & IFF_PROMISC) {                 /* Set promiscuous.  */
1553                 /* Unconditionally log net taps.  */
1554                 printk(KERN_INFO "%s: Promiscuous mode enabled.\n", dev->name);
1555                 ip->emcr |= EMCR_PROMISC;
1556                 ioc3_w_emcr(ip->emcr);
1557                 (void) ioc3_r_emcr();
1558         } else {
1559                 ip->emcr &= ~EMCR_PROMISC;
1560                 ioc3_w_emcr(ip->emcr);                  /* Clear promiscuous. */
1561                 (void) ioc3_r_emcr();
1562
1563                 if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
1564                         /* Too many for hashing to make sense or we want all
1565                            multicast packets anyway,  so skip computing all the
1566                            hashes and just accept all packets.  */
1567                         ip->ehar_h = 0xffffffff;
1568                         ip->ehar_l = 0xffffffff;
1569                 } else {
1570                         for (i = 0; i < dev->mc_count; i++) {
1571                                 char *addr = dmi->dmi_addr;
1572                                 dmi = dmi->next;
1573
1574                                 if (!(*addr & 1))
1575                                         continue;
1576
1577                                 ehar |= (1UL << ioc3_hash(addr));
1578                         }
1579                         ip->ehar_h = ehar >> 32;
1580                         ip->ehar_l = ehar & 0xffffffff;
1581                 }
1582                 ioc3_w_ehar_h(ip->ehar_h);
1583                 ioc3_w_ehar_l(ip->ehar_l);
1584         }
1585
1586         netif_wake_queue(dev);                  /* Let us get going again. */
1587 }
1588
1589 MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
1590 MODULE_DESCRIPTION("SGI IOC3 Ethernet driver");
1591 MODULE_LICENSE("GPL");
1592
1593 module_init(ioc3_init_module);
1594 module_exit(ioc3_cleanup_module);