Merge branch 'omap-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tmlind...
[pandora-kernel.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, 01, 03, 06 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-4"
32
33 #include <linux/init.h>
34 #include <linux/delay.h>
35 #include <linux/kernel.h>
36 #include <linux/mm.h>
37 #include <linux/errno.h>
38 #include <linux/module.h>
39 #include <linux/pci.h>
40 #include <linux/crc32.h>
41 #include <linux/mii.h>
42 #include <linux/in.h>
43 #include <linux/ip.h>
44 #include <linux/tcp.h>
45 #include <linux/udp.h>
46 #include <linux/dma-mapping.h>
47 #include <linux/gfp.h>
48
49 #ifdef CONFIG_SERIAL_8250
50 #include <linux/serial_core.h>
51 #include <linux/serial_8250.h>
52 #include <linux/serial_reg.h>
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 <net/ip.h>
60
61 #include <asm/byteorder.h>
62 #include <asm/io.h>
63 #include <asm/pgtable.h>
64 #include <asm/uaccess.h>
65 #include <asm/sn/types.h>
66 #include <asm/sn/ioc3.h>
67 #include <asm/pci/bridge.h>
68
69 /*
70  * 64 RX buffers.  This is tunable in the range of 16 <= x < 512.  The
71  * value must be a power of two.
72  */
73 #define RX_BUFFS 64
74
75 #define ETCSR_FD        ((17<<ETCSR_IPGR2_SHIFT) | (11<<ETCSR_IPGR1_SHIFT) | 21)
76 #define ETCSR_HD        ((21<<ETCSR_IPGR2_SHIFT) | (21<<ETCSR_IPGR1_SHIFT) | 21)
77
78 /* Private per NIC data of the driver.  */
79 struct ioc3_private {
80         struct ioc3 *regs;
81         unsigned long *rxr;             /* pointer to receiver ring */
82         struct ioc3_etxd *txr;
83         struct sk_buff *rx_skbs[512];
84         struct sk_buff *tx_skbs[128];
85         struct net_device_stats stats;
86         int rx_ci;                      /* RX consumer index */
87         int rx_pi;                      /* RX producer index */
88         int tx_ci;                      /* TX consumer index */
89         int tx_pi;                      /* TX producer index */
90         int txqlen;
91         u32 emcr, ehar_h, ehar_l;
92         spinlock_t ioc3_lock;
93         struct mii_if_info mii;
94         unsigned long flags;
95 #define IOC3_FLAG_RX_CHECKSUMS  1
96
97         struct pci_dev *pdev;
98
99         /* Members used by autonegotiation  */
100         struct timer_list ioc3_timer;
101 };
102
103 static inline struct net_device *priv_netdev(struct ioc3_private *dev)
104 {
105         return (void *)dev - ((sizeof(struct net_device) + 31) & ~31);
106 }
107
108 static int ioc3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
109 static void ioc3_set_multicast_list(struct net_device *dev);
110 static int ioc3_start_xmit(struct sk_buff *skb, struct net_device *dev);
111 static void ioc3_timeout(struct net_device *dev);
112 static inline unsigned int ioc3_hash(const unsigned char *addr);
113 static inline void ioc3_stop(struct ioc3_private *ip);
114 static void ioc3_init(struct net_device *dev);
115
116 static const char ioc3_str[] = "IOC3 Ethernet";
117 static const struct ethtool_ops ioc3_ethtool_ops;
118
119 /* We use this to acquire receive skb's that we can DMA directly into. */
120
121 #define IOC3_CACHELINE  128UL
122
123 static inline unsigned long aligned_rx_skb_addr(unsigned long addr)
124 {
125         return (~addr + 1) & (IOC3_CACHELINE - 1UL);
126 }
127
128 static inline struct sk_buff * ioc3_alloc_skb(unsigned long length,
129         unsigned int gfp_mask)
130 {
131         struct sk_buff *skb;
132
133         skb = alloc_skb(length + IOC3_CACHELINE - 1, gfp_mask);
134         if (likely(skb)) {
135                 int offset = aligned_rx_skb_addr((unsigned long) skb->data);
136                 if (offset)
137                         skb_reserve(skb, offset);
138         }
139
140         return skb;
141 }
142
143 static inline unsigned long ioc3_map(void *ptr, unsigned long vdev)
144 {
145 #ifdef CONFIG_SGI_IP27
146         vdev <<= 57;   /* Shift to PCI64_ATTR_VIRTUAL */
147
148         return vdev | (0xaUL << PCI64_ATTR_TARG_SHFT) | PCI64_ATTR_PREF |
149                ((unsigned long)ptr & TO_PHYS_MASK);
150 #else
151         return virt_to_bus(ptr);
152 #endif
153 }
154
155 /* BEWARE: The IOC3 documentation documents the size of rx buffers as
156    1644 while it's actually 1664.  This one was nasty to track down ...  */
157 #define RX_OFFSET               10
158 #define RX_BUF_ALLOC_SIZE       (1664 + RX_OFFSET + IOC3_CACHELINE)
159
160 /* DMA barrier to separate cached and uncached accesses.  */
161 #define BARRIER()                                                       \
162         __asm__("sync" ::: "memory")
163
164
165 #define IOC3_SIZE 0x100000
166
167 /*
168  * IOC3 is a big endian device
169  *
170  * Unorthodox but makes the users of these macros more readable - the pointer
171  * to the IOC3's memory mapped registers is expected as struct ioc3 * ioc3
172  * in the environment.
173  */
174 #define ioc3_r_mcr()            be32_to_cpu(ioc3->mcr)
175 #define ioc3_w_mcr(v)           do { ioc3->mcr = cpu_to_be32(v); } while (0)
176 #define ioc3_w_gpcr_s(v)        do { ioc3->gpcr_s = cpu_to_be32(v); } while (0)
177 #define ioc3_r_emcr()           be32_to_cpu(ioc3->emcr)
178 #define ioc3_w_emcr(v)          do { ioc3->emcr = cpu_to_be32(v); } while (0)
179 #define ioc3_r_eisr()           be32_to_cpu(ioc3->eisr)
180 #define ioc3_w_eisr(v)          do { ioc3->eisr = cpu_to_be32(v); } while (0)
181 #define ioc3_r_eier()           be32_to_cpu(ioc3->eier)
182 #define ioc3_w_eier(v)          do { ioc3->eier = cpu_to_be32(v); } while (0)
183 #define ioc3_r_ercsr()          be32_to_cpu(ioc3->ercsr)
184 #define ioc3_w_ercsr(v)         do { ioc3->ercsr = cpu_to_be32(v); } while (0)
185 #define ioc3_r_erbr_h()         be32_to_cpu(ioc3->erbr_h)
186 #define ioc3_w_erbr_h(v)        do { ioc3->erbr_h = cpu_to_be32(v); } while (0)
187 #define ioc3_r_erbr_l()         be32_to_cpu(ioc3->erbr_l)
188 #define ioc3_w_erbr_l(v)        do { ioc3->erbr_l = cpu_to_be32(v); } while (0)
189 #define ioc3_r_erbar()          be32_to_cpu(ioc3->erbar)
190 #define ioc3_w_erbar(v)         do { ioc3->erbar = cpu_to_be32(v); } while (0)
191 #define ioc3_r_ercir()          be32_to_cpu(ioc3->ercir)
192 #define ioc3_w_ercir(v)         do { ioc3->ercir = cpu_to_be32(v); } while (0)
193 #define ioc3_r_erpir()          be32_to_cpu(ioc3->erpir)
194 #define ioc3_w_erpir(v)         do { ioc3->erpir = cpu_to_be32(v); } while (0)
195 #define ioc3_r_ertr()           be32_to_cpu(ioc3->ertr)
196 #define ioc3_w_ertr(v)          do { ioc3->ertr = cpu_to_be32(v); } while (0)
197 #define ioc3_r_etcsr()          be32_to_cpu(ioc3->etcsr)
198 #define ioc3_w_etcsr(v)         do { ioc3->etcsr = cpu_to_be32(v); } while (0)
199 #define ioc3_r_ersr()           be32_to_cpu(ioc3->ersr)
200 #define ioc3_w_ersr(v)          do { ioc3->ersr = cpu_to_be32(v); } while (0)
201 #define ioc3_r_etcdc()          be32_to_cpu(ioc3->etcdc)
202 #define ioc3_w_etcdc(v)         do { ioc3->etcdc = cpu_to_be32(v); } while (0)
203 #define ioc3_r_ebir()           be32_to_cpu(ioc3->ebir)
204 #define ioc3_w_ebir(v)          do { ioc3->ebir = cpu_to_be32(v); } while (0)
205 #define ioc3_r_etbr_h()         be32_to_cpu(ioc3->etbr_h)
206 #define ioc3_w_etbr_h(v)        do { ioc3->etbr_h = cpu_to_be32(v); } while (0)
207 #define ioc3_r_etbr_l()         be32_to_cpu(ioc3->etbr_l)
208 #define ioc3_w_etbr_l(v)        do { ioc3->etbr_l = cpu_to_be32(v); } while (0)
209 #define ioc3_r_etcir()          be32_to_cpu(ioc3->etcir)
210 #define ioc3_w_etcir(v)         do { ioc3->etcir = cpu_to_be32(v); } while (0)
211 #define ioc3_r_etpir()          be32_to_cpu(ioc3->etpir)
212 #define ioc3_w_etpir(v)         do { ioc3->etpir = cpu_to_be32(v); } while (0)
213 #define ioc3_r_emar_h()         be32_to_cpu(ioc3->emar_h)
214 #define ioc3_w_emar_h(v)        do { ioc3->emar_h = cpu_to_be32(v); } while (0)
215 #define ioc3_r_emar_l()         be32_to_cpu(ioc3->emar_l)
216 #define ioc3_w_emar_l(v)        do { ioc3->emar_l = cpu_to_be32(v); } while (0)
217 #define ioc3_r_ehar_h()         be32_to_cpu(ioc3->ehar_h)
218 #define ioc3_w_ehar_h(v)        do { ioc3->ehar_h = cpu_to_be32(v); } while (0)
219 #define ioc3_r_ehar_l()         be32_to_cpu(ioc3->ehar_l)
220 #define ioc3_w_ehar_l(v)        do { ioc3->ehar_l = cpu_to_be32(v); } while (0)
221 #define ioc3_r_micr()           be32_to_cpu(ioc3->micr)
222 #define ioc3_w_micr(v)          do { ioc3->micr = cpu_to_be32(v); } while (0)
223 #define ioc3_r_midr_r()         be32_to_cpu(ioc3->midr_r)
224 #define ioc3_w_midr_r(v)        do { ioc3->midr_r = cpu_to_be32(v); } while (0)
225 #define ioc3_r_midr_w()         be32_to_cpu(ioc3->midr_w)
226 #define ioc3_w_midr_w(v)        do { ioc3->midr_w = cpu_to_be32(v); } while (0)
227
228 static inline u32 mcr_pack(u32 pulse, u32 sample)
229 {
230         return (pulse << 10) | (sample << 2);
231 }
232
233 static int nic_wait(struct ioc3 *ioc3)
234 {
235         u32 mcr;
236
237         do {
238                 mcr = ioc3_r_mcr();
239         } while (!(mcr & 2));
240
241         return mcr & 1;
242 }
243
244 static int nic_reset(struct ioc3 *ioc3)
245 {
246         int presence;
247
248         ioc3_w_mcr(mcr_pack(500, 65));
249         presence = nic_wait(ioc3);
250
251         ioc3_w_mcr(mcr_pack(0, 500));
252         nic_wait(ioc3);
253
254         return presence;
255 }
256
257 static inline int nic_read_bit(struct ioc3 *ioc3)
258 {
259         int result;
260
261         ioc3_w_mcr(mcr_pack(6, 13));
262         result = nic_wait(ioc3);
263         ioc3_w_mcr(mcr_pack(0, 100));
264         nic_wait(ioc3);
265
266         return result;
267 }
268
269 static inline void nic_write_bit(struct ioc3 *ioc3, int bit)
270 {
271         if (bit)
272                 ioc3_w_mcr(mcr_pack(6, 110));
273         else
274                 ioc3_w_mcr(mcr_pack(80, 30));
275
276         nic_wait(ioc3);
277 }
278
279 /*
280  * Read a byte from an iButton device
281  */
282 static u32 nic_read_byte(struct ioc3 *ioc3)
283 {
284         u32 result = 0;
285         int i;
286
287         for (i = 0; i < 8; i++)
288                 result = (result >> 1) | (nic_read_bit(ioc3) << 7);
289
290         return result;
291 }
292
293 /*
294  * Write a byte to an iButton device
295  */
296 static void nic_write_byte(struct ioc3 *ioc3, int byte)
297 {
298         int i, bit;
299
300         for (i = 8; i; i--) {
301                 bit = byte & 1;
302                 byte >>= 1;
303
304                 nic_write_bit(ioc3, bit);
305         }
306 }
307
308 static u64 nic_find(struct ioc3 *ioc3, int *last)
309 {
310         int a, b, index, disc;
311         u64 address = 0;
312
313         nic_reset(ioc3);
314         /* Search ROM.  */
315         nic_write_byte(ioc3, 0xf0);
316
317         /* Algorithm from ``Book of iButton Standards''.  */
318         for (index = 0, disc = 0; index < 64; index++) {
319                 a = nic_read_bit(ioc3);
320                 b = nic_read_bit(ioc3);
321
322                 if (a && b) {
323                         printk("NIC search failed (not fatal).\n");
324                         *last = 0;
325                         return 0;
326                 }
327
328                 if (!a && !b) {
329                         if (index == *last) {
330                                 address |= 1UL << index;
331                         } else if (index > *last) {
332                                 address &= ~(1UL << index);
333                                 disc = index;
334                         } else if ((address & (1UL << index)) == 0)
335                                 disc = index;
336                         nic_write_bit(ioc3, address & (1UL << index));
337                         continue;
338                 } else {
339                         if (a)
340                                 address |= 1UL << index;
341                         else
342                                 address &= ~(1UL << index);
343                         nic_write_bit(ioc3, a);
344                         continue;
345                 }
346         }
347
348         *last = disc;
349
350         return address;
351 }
352
353 static int nic_init(struct ioc3 *ioc3)
354 {
355         const char *unknown = "unknown";
356         const char *type = unknown;
357         u8 crc;
358         u8 serial[6];
359         int save = 0, i;
360
361         while (1) {
362                 u64 reg;
363                 reg = nic_find(ioc3, &save);
364
365                 switch (reg & 0xff) {
366                 case 0x91:
367                         type = "DS1981U";
368                         break;
369                 default:
370                         if (save == 0) {
371                                 /* Let the caller try again.  */
372                                 return -1;
373                         }
374                         continue;
375                 }
376
377                 nic_reset(ioc3);
378
379                 /* Match ROM.  */
380                 nic_write_byte(ioc3, 0x55);
381                 for (i = 0; i < 8; i++)
382                         nic_write_byte(ioc3, (reg >> (i << 3)) & 0xff);
383
384                 reg >>= 8; /* Shift out type.  */
385                 for (i = 0; i < 6; i++) {
386                         serial[i] = reg & 0xff;
387                         reg >>= 8;
388                 }
389                 crc = reg & 0xff;
390                 break;
391         }
392
393         printk("Found %s NIC", type);
394         if (type != unknown)
395                 printk (" registration number %pM, CRC %02x", serial, crc);
396         printk(".\n");
397
398         return 0;
399 }
400
401 /*
402  * Read the NIC (Number-In-a-Can) device used to store the MAC address on
403  * SN0 / SN00 nodeboards and PCI cards.
404  */
405 static void ioc3_get_eaddr_nic(struct ioc3_private *ip)
406 {
407         struct ioc3 *ioc3 = ip->regs;
408         u8 nic[14];
409         int tries = 2; /* There may be some problem with the battery?  */
410         int i;
411
412         ioc3_w_gpcr_s(1 << 21);
413
414         while (tries--) {
415                 if (!nic_init(ioc3))
416                         break;
417                 udelay(500);
418         }
419
420         if (tries < 0) {
421                 printk("Failed to read MAC address\n");
422                 return;
423         }
424
425         /* Read Memory.  */
426         nic_write_byte(ioc3, 0xf0);
427         nic_write_byte(ioc3, 0x00);
428         nic_write_byte(ioc3, 0x00);
429
430         for (i = 13; i >= 0; i--)
431                 nic[i] = nic_read_byte(ioc3);
432
433         for (i = 2; i < 8; i++)
434                 priv_netdev(ip)->dev_addr[i - 2] = nic[i];
435 }
436
437 /*
438  * Ok, this is hosed by design.  It's necessary to know what machine the
439  * NIC is in in order to know how to read the NIC address.  We also have
440  * to know if it's a PCI card or a NIC in on the node board ...
441  */
442 static void ioc3_get_eaddr(struct ioc3_private *ip)
443 {
444         ioc3_get_eaddr_nic(ip);
445
446         printk("Ethernet address is %pM.\n", priv_netdev(ip)->dev_addr);
447 }
448
449 static void __ioc3_set_mac_address(struct net_device *dev)
450 {
451         struct ioc3_private *ip = netdev_priv(dev);
452         struct ioc3 *ioc3 = ip->regs;
453
454         ioc3_w_emar_h((dev->dev_addr[5] <<  8) | dev->dev_addr[4]);
455         ioc3_w_emar_l((dev->dev_addr[3] << 24) | (dev->dev_addr[2] << 16) |
456                       (dev->dev_addr[1] <<  8) | dev->dev_addr[0]);
457 }
458
459 static int ioc3_set_mac_address(struct net_device *dev, void *addr)
460 {
461         struct ioc3_private *ip = netdev_priv(dev);
462         struct sockaddr *sa = addr;
463
464         memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
465
466         spin_lock_irq(&ip->ioc3_lock);
467         __ioc3_set_mac_address(dev);
468         spin_unlock_irq(&ip->ioc3_lock);
469
470         return 0;
471 }
472
473 /*
474  * Caller must hold the ioc3_lock ever for MII readers.  This is also
475  * used to protect the transmitter side but it's low contention.
476  */
477 static int ioc3_mdio_read(struct net_device *dev, int phy, int reg)
478 {
479         struct ioc3_private *ip = netdev_priv(dev);
480         struct ioc3 *ioc3 = ip->regs;
481
482         while (ioc3_r_micr() & MICR_BUSY);
483         ioc3_w_micr((phy << MICR_PHYADDR_SHIFT) | reg | MICR_READTRIG);
484         while (ioc3_r_micr() & MICR_BUSY);
485
486         return ioc3_r_midr_r() & MIDR_DATA_MASK;
487 }
488
489 static void ioc3_mdio_write(struct net_device *dev, int phy, int reg, int data)
490 {
491         struct ioc3_private *ip = netdev_priv(dev);
492         struct ioc3 *ioc3 = ip->regs;
493
494         while (ioc3_r_micr() & MICR_BUSY);
495         ioc3_w_midr_w(data);
496         ioc3_w_micr((phy << MICR_PHYADDR_SHIFT) | reg);
497         while (ioc3_r_micr() & MICR_BUSY);
498 }
499
500 static int ioc3_mii_init(struct ioc3_private *ip);
501
502 static struct net_device_stats *ioc3_get_stats(struct net_device *dev)
503 {
504         struct ioc3_private *ip = netdev_priv(dev);
505         struct ioc3 *ioc3 = ip->regs;
506
507         ip->stats.collisions += (ioc3_r_etcdc() & ETCDC_COLLCNT_MASK);
508         return &ip->stats;
509 }
510
511 static void ioc3_tcpudp_checksum(struct sk_buff *skb, uint32_t hwsum, int len)
512 {
513         struct ethhdr *eh = eth_hdr(skb);
514         uint32_t csum, ehsum;
515         unsigned int proto;
516         struct iphdr *ih;
517         uint16_t *ew;
518         unsigned char *cp;
519
520         /*
521          * Did hardware handle the checksum at all?  The cases we can handle
522          * are:
523          *
524          * - TCP and UDP checksums of IPv4 only.
525          * - IPv6 would be doable but we keep that for later ...
526          * - Only unfragmented packets.  Did somebody already tell you
527          *   fragmentation is evil?
528          * - don't care about packet size.  Worst case when processing a
529          *   malformed packet we'll try to access the packet at ip header +
530          *   64 bytes which is still inside the skb.  Even in the unlikely
531          *   case where the checksum is right the higher layers will still
532          *   drop the packet as appropriate.
533          */
534         if (eh->h_proto != htons(ETH_P_IP))
535                 return;
536
537         ih = (struct iphdr *) ((char *)eh + ETH_HLEN);
538         if (ih->frag_off & htons(IP_MF | IP_OFFSET))
539                 return;
540
541         proto = ih->protocol;
542         if (proto != IPPROTO_TCP && proto != IPPROTO_UDP)
543                 return;
544
545         /* Same as tx - compute csum of pseudo header  */
546         csum = hwsum +
547                (ih->tot_len - (ih->ihl << 2)) +
548                htons((uint16_t)ih->protocol) +
549                (ih->saddr >> 16) + (ih->saddr & 0xffff) +
550                (ih->daddr >> 16) + (ih->daddr & 0xffff);
551
552         /* Sum up ethernet dest addr, src addr and protocol  */
553         ew = (uint16_t *) eh;
554         ehsum = ew[0] + ew[1] + ew[2] + ew[3] + ew[4] + ew[5] + ew[6];
555
556         ehsum = (ehsum & 0xffff) + (ehsum >> 16);
557         ehsum = (ehsum & 0xffff) + (ehsum >> 16);
558
559         csum += 0xffff ^ ehsum;
560
561         /* In the next step we also subtract the 1's complement
562            checksum of the trailing ethernet CRC.  */
563         cp = (char *)eh + len;  /* points at trailing CRC */
564         if (len & 1) {
565                 csum += 0xffff ^ (uint16_t) ((cp[1] << 8) | cp[0]);
566                 csum += 0xffff ^ (uint16_t) ((cp[3] << 8) | cp[2]);
567         } else {
568                 csum += 0xffff ^ (uint16_t) ((cp[0] << 8) | cp[1]);
569                 csum += 0xffff ^ (uint16_t) ((cp[2] << 8) | cp[3]);
570         }
571
572         csum = (csum & 0xffff) + (csum >> 16);
573         csum = (csum & 0xffff) + (csum >> 16);
574
575         if (csum == 0xffff)
576                 skb->ip_summed = CHECKSUM_UNNECESSARY;
577 }
578
579 static inline void ioc3_rx(struct ioc3_private *ip)
580 {
581         struct sk_buff *skb, *new_skb;
582         struct ioc3 *ioc3 = ip->regs;
583         int rx_entry, n_entry, len;
584         struct ioc3_erxbuf *rxb;
585         unsigned long *rxr;
586         u32 w0, err;
587
588         rxr = (unsigned long *) ip->rxr;                /* Ring base */
589         rx_entry = ip->rx_ci;                           /* RX consume index */
590         n_entry = ip->rx_pi;
591
592         skb = ip->rx_skbs[rx_entry];
593         rxb = (struct ioc3_erxbuf *) (skb->data - RX_OFFSET);
594         w0 = be32_to_cpu(rxb->w0);
595
596         while (w0 & ERXBUF_V) {
597                 err = be32_to_cpu(rxb->err);            /* It's valid ...  */
598                 if (err & ERXBUF_GOODPKT) {
599                         len = ((w0 >> ERXBUF_BYTECNT_SHIFT) & 0x7ff) - 4;
600                         skb_trim(skb, len);
601                         skb->protocol = eth_type_trans(skb, priv_netdev(ip));
602
603                         new_skb = ioc3_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
604                         if (!new_skb) {
605                                 /* Ouch, drop packet and just recycle packet
606                                    to keep the ring filled.  */
607                                 ip->stats.rx_dropped++;
608                                 new_skb = skb;
609                                 goto next;
610                         }
611
612                         if (likely(ip->flags & IOC3_FLAG_RX_CHECKSUMS))
613                                 ioc3_tcpudp_checksum(skb,
614                                         w0 & ERXBUF_IPCKSUM_MASK, len);
615
616                         netif_rx(skb);
617
618                         ip->rx_skbs[rx_entry] = NULL;   /* Poison  */
619
620                         /* Because we reserve afterwards. */
621                         skb_put(new_skb, (1664 + RX_OFFSET));
622                         rxb = (struct ioc3_erxbuf *) new_skb->data;
623                         skb_reserve(new_skb, RX_OFFSET);
624
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)
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
821 out:
822         return res;
823 }
824
825 static void ioc3_mii_start(struct ioc3_private *ip)
826 {
827         ip->ioc3_timer.expires = jiffies + (12 * HZ)/10;  /* 1.2 sec. */
828         ip->ioc3_timer.data = (unsigned long) ip;
829         ip->ioc3_timer.function = &ioc3_timer;
830         add_timer(&ip->ioc3_timer);
831 }
832
833 static inline void ioc3_clean_rx_ring(struct ioc3_private *ip)
834 {
835         struct sk_buff *skb;
836         int i;
837
838         for (i = ip->rx_ci; i & 15; i++) {
839                 ip->rx_skbs[ip->rx_pi] = ip->rx_skbs[ip->rx_ci];
840                 ip->rxr[ip->rx_pi++] = ip->rxr[ip->rx_ci++];
841         }
842         ip->rx_pi &= 511;
843         ip->rx_ci &= 511;
844
845         for (i = ip->rx_ci; i != ip->rx_pi; i = (i+1) & 511) {
846                 struct ioc3_erxbuf *rxb;
847                 skb = ip->rx_skbs[i];
848                 rxb = (struct ioc3_erxbuf *) (skb->data - RX_OFFSET);
849                 rxb->w0 = 0;
850         }
851 }
852
853 static inline void ioc3_clean_tx_ring(struct ioc3_private *ip)
854 {
855         struct sk_buff *skb;
856         int i;
857
858         for (i=0; i < 128; i++) {
859                 skb = ip->tx_skbs[i];
860                 if (skb) {
861                         ip->tx_skbs[i] = NULL;
862                         dev_kfree_skb_any(skb);
863                 }
864                 ip->txr[i].cmd = 0;
865         }
866         ip->tx_pi = 0;
867         ip->tx_ci = 0;
868 }
869
870 static void ioc3_free_rings(struct ioc3_private *ip)
871 {
872         struct sk_buff *skb;
873         int rx_entry, n_entry;
874
875         if (ip->txr) {
876                 ioc3_clean_tx_ring(ip);
877                 free_pages((unsigned long)ip->txr, 2);
878                 ip->txr = NULL;
879         }
880
881         if (ip->rxr) {
882                 n_entry = ip->rx_ci;
883                 rx_entry = ip->rx_pi;
884
885                 while (n_entry != rx_entry) {
886                         skb = ip->rx_skbs[n_entry];
887                         if (skb)
888                                 dev_kfree_skb_any(skb);
889
890                         n_entry = (n_entry + 1) & 511;
891                 }
892                 free_page((unsigned long)ip->rxr);
893                 ip->rxr = NULL;
894         }
895 }
896
897 static void ioc3_alloc_rings(struct net_device *dev)
898 {
899         struct ioc3_private *ip = netdev_priv(dev);
900         struct ioc3_erxbuf *rxb;
901         unsigned long *rxr;
902         int i;
903
904         if (ip->rxr == NULL) {
905                 /* Allocate and initialize rx ring.  4kb = 512 entries  */
906                 ip->rxr = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
907                 rxr = (unsigned long *) ip->rxr;
908                 if (!rxr)
909                         printk("ioc3_alloc_rings(): get_zeroed_page() failed!\n");
910
911                 /* Now the rx buffers.  The RX ring may be larger but
912                    we only allocate 16 buffers for now.  Need to tune
913                    this for performance and memory later.  */
914                 for (i = 0; i < RX_BUFFS; i++) {
915                         struct sk_buff *skb;
916
917                         skb = ioc3_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
918                         if (!skb) {
919                                 show_free_areas();
920                                 continue;
921                         }
922
923                         ip->rx_skbs[i] = skb;
924
925                         /* Because we reserve afterwards. */
926                         skb_put(skb, (1664 + RX_OFFSET));
927                         rxb = (struct ioc3_erxbuf *) skb->data;
928                         rxr[i] = cpu_to_be64(ioc3_map(rxb, 1));
929                         skb_reserve(skb, RX_OFFSET);
930                 }
931                 ip->rx_ci = 0;
932                 ip->rx_pi = RX_BUFFS;
933         }
934
935         if (ip->txr == NULL) {
936                 /* Allocate and initialize tx rings.  16kb = 128 bufs.  */
937                 ip->txr = (struct ioc3_etxd *)__get_free_pages(GFP_KERNEL, 2);
938                 if (!ip->txr)
939                         printk("ioc3_alloc_rings(): __get_free_pages() failed!\n");
940                 ip->tx_pi = 0;
941                 ip->tx_ci = 0;
942         }
943 }
944
945 static void ioc3_init_rings(struct net_device *dev)
946 {
947         struct ioc3_private *ip = netdev_priv(dev);
948         struct ioc3 *ioc3 = ip->regs;
949         unsigned long ring;
950
951         ioc3_free_rings(ip);
952         ioc3_alloc_rings(dev);
953
954         ioc3_clean_rx_ring(ip);
955         ioc3_clean_tx_ring(ip);
956
957         /* Now the rx ring base, consume & produce registers.  */
958         ring = ioc3_map(ip->rxr, 0);
959         ioc3_w_erbr_h(ring >> 32);
960         ioc3_w_erbr_l(ring & 0xffffffff);
961         ioc3_w_ercir(ip->rx_ci << 3);
962         ioc3_w_erpir((ip->rx_pi << 3) | ERPIR_ARM);
963
964         ring = ioc3_map(ip->txr, 0);
965
966         ip->txqlen = 0;                                 /* nothing queued  */
967
968         /* Now the tx ring base, consume & produce registers.  */
969         ioc3_w_etbr_h(ring >> 32);
970         ioc3_w_etbr_l(ring & 0xffffffff);
971         ioc3_w_etpir(ip->tx_pi << 7);
972         ioc3_w_etcir(ip->tx_ci << 7);
973         (void) ioc3_r_etcir();                          /* Flush */
974 }
975
976 static inline void ioc3_ssram_disc(struct ioc3_private *ip)
977 {
978         struct ioc3 *ioc3 = ip->regs;
979         volatile u32 *ssram0 = &ioc3->ssram[0x0000];
980         volatile u32 *ssram1 = &ioc3->ssram[0x4000];
981         unsigned int pattern = 0x5555;
982
983         /* Assume the larger size SSRAM and enable parity checking */
984         ioc3_w_emcr(ioc3_r_emcr() | (EMCR_BUFSIZ | EMCR_RAMPAR));
985
986         *ssram0 = pattern;
987         *ssram1 = ~pattern & IOC3_SSRAM_DM;
988
989         if ((*ssram0 & IOC3_SSRAM_DM) != pattern ||
990             (*ssram1 & IOC3_SSRAM_DM) != (~pattern & IOC3_SSRAM_DM)) {
991                 /* set ssram size to 64 KB */
992                 ip->emcr = EMCR_RAMPAR;
993                 ioc3_w_emcr(ioc3_r_emcr() & ~EMCR_BUFSIZ);
994         } else
995                 ip->emcr = EMCR_BUFSIZ | EMCR_RAMPAR;
996 }
997
998 static void ioc3_init(struct net_device *dev)
999 {
1000         struct ioc3_private *ip = netdev_priv(dev);
1001         struct ioc3 *ioc3 = ip->regs;
1002
1003         del_timer_sync(&ip->ioc3_timer);        /* Kill if running      */
1004
1005         ioc3_w_emcr(EMCR_RST);                  /* Reset                */
1006         (void) ioc3_r_emcr();                   /* Flush WB             */
1007         udelay(4);                              /* Give it time ...     */
1008         ioc3_w_emcr(0);
1009         (void) ioc3_r_emcr();
1010
1011         /* Misc registers  */
1012 #ifdef CONFIG_SGI_IP27
1013         ioc3_w_erbar(PCI64_ATTR_BAR >> 32);     /* Barrier on last store */
1014 #else
1015         ioc3_w_erbar(0);                        /* Let PCI API get it right */
1016 #endif
1017         (void) ioc3_r_etcdc();                  /* Clear on read */
1018         ioc3_w_ercsr(15);                       /* RX low watermark  */
1019         ioc3_w_ertr(0);                         /* Interrupt immediately */
1020         __ioc3_set_mac_address(dev);
1021         ioc3_w_ehar_h(ip->ehar_h);
1022         ioc3_w_ehar_l(ip->ehar_l);
1023         ioc3_w_ersr(42);                        /* XXX should be random */
1024
1025         ioc3_init_rings(dev);
1026
1027         ip->emcr |= ((RX_OFFSET / 2) << EMCR_RXOFF_SHIFT) | EMCR_TXDMAEN |
1028                      EMCR_TXEN | EMCR_RXDMAEN | EMCR_RXEN | EMCR_PADEN;
1029         ioc3_w_emcr(ip->emcr);
1030         ioc3_w_eier(EISR_RXTIMERINT | EISR_RXOFLO | EISR_RXBUFOFLO |
1031                     EISR_RXMEMERR | EISR_RXPARERR | EISR_TXBUFUFLO |
1032                     EISR_TXEXPLICIT | EISR_TXMEMERR);
1033         (void) ioc3_r_eier();
1034 }
1035
1036 static inline void ioc3_stop(struct ioc3_private *ip)
1037 {
1038         struct ioc3 *ioc3 = ip->regs;
1039
1040         ioc3_w_emcr(0);                         /* Shutup */
1041         ioc3_w_eier(0);                         /* Disable interrupts */
1042         (void) ioc3_r_eier();                   /* Flush */
1043 }
1044
1045 static int ioc3_open(struct net_device *dev)
1046 {
1047         struct ioc3_private *ip = netdev_priv(dev);
1048
1049         if (request_irq(dev->irq, ioc3_interrupt, IRQF_SHARED, ioc3_str, dev)) {
1050                 printk(KERN_ERR "%s: Can't get irq %d\n", dev->name, dev->irq);
1051
1052                 return -EAGAIN;
1053         }
1054
1055         ip->ehar_h = 0;
1056         ip->ehar_l = 0;
1057         ioc3_init(dev);
1058         ioc3_mii_start(ip);
1059
1060         netif_start_queue(dev);
1061         return 0;
1062 }
1063
1064 static int ioc3_close(struct net_device *dev)
1065 {
1066         struct ioc3_private *ip = netdev_priv(dev);
1067
1068         del_timer_sync(&ip->ioc3_timer);
1069
1070         netif_stop_queue(dev);
1071
1072         ioc3_stop(ip);
1073         free_irq(dev->irq, dev);
1074
1075         ioc3_free_rings(ip);
1076         return 0;
1077 }
1078
1079 /*
1080  * MENET cards have four IOC3 chips, which are attached to two sets of
1081  * PCI slot resources each: the primary connections are on slots
1082  * 0..3 and the secondaries are on 4..7
1083  *
1084  * All four ethernets are brought out to connectors; six serial ports
1085  * (a pair from each of the first three IOC3s) are brought out to
1086  * MiniDINs; all other subdevices are left swinging in the wind, leave
1087  * them disabled.
1088  */
1089
1090 static int ioc3_adjacent_is_ioc3(struct pci_dev *pdev, int slot)
1091 {
1092         struct pci_dev *dev = pci_get_slot(pdev->bus, PCI_DEVFN(slot, 0));
1093         int ret = 0;
1094
1095         if (dev) {
1096                 if (dev->vendor == PCI_VENDOR_ID_SGI &&
1097                         dev->device == PCI_DEVICE_ID_SGI_IOC3)
1098                         ret = 1;
1099                 pci_dev_put(dev);
1100         }
1101
1102         return ret;
1103 }
1104
1105 static int ioc3_is_menet(struct pci_dev *pdev)
1106 {
1107         return pdev->bus->parent == NULL &&
1108                ioc3_adjacent_is_ioc3(pdev, 0) &&
1109                ioc3_adjacent_is_ioc3(pdev, 1) &&
1110                ioc3_adjacent_is_ioc3(pdev, 2);
1111 }
1112
1113 #ifdef CONFIG_SERIAL_8250
1114 /*
1115  * Note about serial ports and consoles:
1116  * For console output, everyone uses the IOC3 UARTA (offset 0x178)
1117  * connected to the master node (look in ip27_setup_console() and
1118  * ip27prom_console_write()).
1119  *
1120  * For serial (/dev/ttyS0 etc), we can not have hardcoded serial port
1121  * addresses on a partitioned machine. Since we currently use the ioc3
1122  * serial ports, we use dynamic serial port discovery that the serial.c
1123  * driver uses for pci/pnp ports (there is an entry for the SGI ioc3
1124  * boards in pci_boards[]). Unfortunately, UARTA's pio address is greater
1125  * than UARTB's, although UARTA on o200s has traditionally been known as
1126  * port 0. So, we just use one serial port from each ioc3 (since the
1127  * serial driver adds addresses to get to higher ports).
1128  *
1129  * The first one to do a register_console becomes the preferred console
1130  * (if there is no kernel command line console= directive). /dev/console
1131  * (ie 5, 1) is then "aliased" into the device number returned by the
1132  * "device" routine referred to in this console structure
1133  * (ip27prom_console_dev).
1134  *
1135  * Also look in ip27-pci.c:pci_fixup_ioc3() for some comments on working
1136  * around ioc3 oddities in this respect.
1137  *
1138  * The IOC3 serials use a 22MHz clock rate with an additional divider which
1139  * can be programmed in the SCR register if the DLAB bit is set.
1140  *
1141  * Register to interrupt zero because we share the interrupt with
1142  * the serial driver which we don't properly support yet.
1143  *
1144  * Can't use UPF_IOREMAP as the whole of IOC3 resources have already been
1145  * registered.
1146  */
1147 static void __devinit ioc3_8250_register(struct ioc3_uartregs __iomem *uart)
1148 {
1149 #define COSMISC_CONSTANT 6
1150
1151         struct uart_port port = {
1152                 .irq            = 0,
1153                 .flags          = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF,
1154                 .iotype         = UPIO_MEM,
1155                 .regshift       = 0,
1156                 .uartclk        = (22000000 << 1) / COSMISC_CONSTANT,
1157
1158                 .membase        = (unsigned char __iomem *) uart,
1159                 .mapbase        = (unsigned long) uart,
1160         };
1161         unsigned char lcr;
1162
1163         lcr = uart->iu_lcr;
1164         uart->iu_lcr = lcr | UART_LCR_DLAB;
1165         uart->iu_scr = COSMISC_CONSTANT,
1166         uart->iu_lcr = lcr;
1167         uart->iu_lcr;
1168         serial8250_register_port(&port);
1169 }
1170
1171 static void __devinit ioc3_serial_probe(struct pci_dev *pdev, struct ioc3 *ioc3)
1172 {
1173         /*
1174          * We need to recognice and treat the fourth MENET serial as it
1175          * does not have an SuperIO chip attached to it, therefore attempting
1176          * to access it will result in bus errors.  We call something an
1177          * MENET if PCI slot 0, 1, 2 and 3 of a master PCI bus all have an IOC3
1178          * in it.  This is paranoid but we want to avoid blowing up on a
1179          * showhorn PCI box that happens to have 4 IOC3 cards in it so it's
1180          * not paranoid enough ...
1181          */
1182         if (ioc3_is_menet(pdev) && PCI_SLOT(pdev->devfn) == 3)
1183                 return;
1184
1185         /*
1186          * Switch IOC3 to PIO mode.  It probably already was but let's be
1187          * paranoid
1188          */
1189         ioc3->gpcr_s = GPCR_UARTA_MODESEL | GPCR_UARTB_MODESEL;
1190         ioc3->gpcr_s;
1191         ioc3->gppr_6 = 0;
1192         ioc3->gppr_6;
1193         ioc3->gppr_7 = 0;
1194         ioc3->gppr_7;
1195         ioc3->sscr_a = ioc3->sscr_a & ~SSCR_DMA_EN;
1196         ioc3->sscr_a;
1197         ioc3->sscr_b = ioc3->sscr_b & ~SSCR_DMA_EN;
1198         ioc3->sscr_b;
1199         /* Disable all SA/B interrupts except for SA/B_INT in SIO_IEC. */
1200         ioc3->sio_iec &= ~ (SIO_IR_SA_TX_MT | SIO_IR_SA_RX_FULL |
1201                             SIO_IR_SA_RX_HIGH | SIO_IR_SA_RX_TIMER |
1202                             SIO_IR_SA_DELTA_DCD | SIO_IR_SA_DELTA_CTS |
1203                             SIO_IR_SA_TX_EXPLICIT | SIO_IR_SA_MEMERR);
1204         ioc3->sio_iec |= SIO_IR_SA_INT;
1205         ioc3->sscr_a = 0;
1206         ioc3->sio_iec &= ~ (SIO_IR_SB_TX_MT | SIO_IR_SB_RX_FULL |
1207                             SIO_IR_SB_RX_HIGH | SIO_IR_SB_RX_TIMER |
1208                             SIO_IR_SB_DELTA_DCD | SIO_IR_SB_DELTA_CTS |
1209                             SIO_IR_SB_TX_EXPLICIT | SIO_IR_SB_MEMERR);
1210         ioc3->sio_iec |= SIO_IR_SB_INT;
1211         ioc3->sscr_b = 0;
1212
1213         ioc3_8250_register(&ioc3->sregs.uarta);
1214         ioc3_8250_register(&ioc3->sregs.uartb);
1215 }
1216 #endif
1217
1218 static const struct net_device_ops ioc3_netdev_ops = {
1219         .ndo_open               = ioc3_open,
1220         .ndo_stop               = ioc3_close,
1221         .ndo_start_xmit         = ioc3_start_xmit,
1222         .ndo_tx_timeout         = ioc3_timeout,
1223         .ndo_get_stats          = ioc3_get_stats,
1224         .ndo_set_multicast_list = ioc3_set_multicast_list,
1225         .ndo_do_ioctl           = ioc3_ioctl,
1226         .ndo_validate_addr      = eth_validate_addr,
1227         .ndo_set_mac_address    = ioc3_set_mac_address,
1228         .ndo_change_mtu         = eth_change_mtu,
1229 };
1230
1231 static int __devinit ioc3_probe(struct pci_dev *pdev,
1232         const struct pci_device_id *ent)
1233 {
1234         unsigned int sw_physid1, sw_physid2;
1235         struct net_device *dev = NULL;
1236         struct ioc3_private *ip;
1237         struct ioc3 *ioc3;
1238         unsigned long ioc3_base, ioc3_size;
1239         u32 vendor, model, rev;
1240         int err, pci_using_dac;
1241
1242         /* Configure DMA attributes. */
1243         err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1244         if (!err) {
1245                 pci_using_dac = 1;
1246                 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1247                 if (err < 0) {
1248                         printk(KERN_ERR "%s: Unable to obtain 64 bit DMA "
1249                                "for consistent allocations\n", pci_name(pdev));
1250                         goto out;
1251                 }
1252         } else {
1253                 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1254                 if (err) {
1255                         printk(KERN_ERR "%s: No usable DMA configuration, "
1256                                "aborting.\n", pci_name(pdev));
1257                         goto out;
1258                 }
1259                 pci_using_dac = 0;
1260         }
1261
1262         if (pci_enable_device(pdev))
1263                 return -ENODEV;
1264
1265         dev = alloc_etherdev(sizeof(struct ioc3_private));
1266         if (!dev) {
1267                 err = -ENOMEM;
1268                 goto out_disable;
1269         }
1270
1271         if (pci_using_dac)
1272                 dev->features |= NETIF_F_HIGHDMA;
1273
1274         err = pci_request_regions(pdev, "ioc3");
1275         if (err)
1276                 goto out_free;
1277
1278         SET_NETDEV_DEV(dev, &pdev->dev);
1279
1280         ip = netdev_priv(dev);
1281
1282         dev->irq = pdev->irq;
1283
1284         ioc3_base = pci_resource_start(pdev, 0);
1285         ioc3_size = pci_resource_len(pdev, 0);
1286         ioc3 = (struct ioc3 *) ioremap(ioc3_base, ioc3_size);
1287         if (!ioc3) {
1288                 printk(KERN_CRIT "ioc3eth(%s): ioremap failed, goodbye.\n",
1289                        pci_name(pdev));
1290                 err = -ENOMEM;
1291                 goto out_res;
1292         }
1293         ip->regs = ioc3;
1294
1295 #ifdef CONFIG_SERIAL_8250
1296         ioc3_serial_probe(pdev, ioc3);
1297 #endif
1298
1299         spin_lock_init(&ip->ioc3_lock);
1300         init_timer(&ip->ioc3_timer);
1301
1302         ioc3_stop(ip);
1303         ioc3_init(dev);
1304
1305         ip->pdev = pdev;
1306
1307         ip->mii.phy_id_mask = 0x1f;
1308         ip->mii.reg_num_mask = 0x1f;
1309         ip->mii.dev = dev;
1310         ip->mii.mdio_read = ioc3_mdio_read;
1311         ip->mii.mdio_write = ioc3_mdio_write;
1312
1313         ioc3_mii_init(ip);
1314
1315         if (ip->mii.phy_id == -1) {
1316                 printk(KERN_CRIT "ioc3-eth(%s): Didn't find a PHY, goodbye.\n",
1317                        pci_name(pdev));
1318                 err = -ENODEV;
1319                 goto out_stop;
1320         }
1321
1322         ioc3_mii_start(ip);
1323         ioc3_ssram_disc(ip);
1324         ioc3_get_eaddr(ip);
1325
1326         /* The IOC3-specific entries in the device structure. */
1327         dev->watchdog_timeo     = 5 * HZ;
1328         dev->netdev_ops         = &ioc3_netdev_ops;
1329         dev->ethtool_ops        = &ioc3_ethtool_ops;
1330         dev->features           = NETIF_F_IP_CSUM;
1331
1332         sw_physid1 = ioc3_mdio_read(dev, ip->mii.phy_id, MII_PHYSID1);
1333         sw_physid2 = ioc3_mdio_read(dev, ip->mii.phy_id, MII_PHYSID2);
1334
1335         err = register_netdev(dev);
1336         if (err)
1337                 goto out_stop;
1338
1339         mii_check_media(&ip->mii, 1, 1);
1340         ioc3_setup_duplex(ip);
1341
1342         vendor = (sw_physid1 << 12) | (sw_physid2 >> 4);
1343         model  = (sw_physid2 >> 4) & 0x3f;
1344         rev    = sw_physid2 & 0xf;
1345         printk(KERN_INFO "%s: Using PHY %d, vendor 0x%x, model %d, "
1346                "rev %d.\n", dev->name, ip->mii.phy_id, vendor, model, rev);
1347         printk(KERN_INFO "%s: IOC3 SSRAM has %d kbyte.\n", dev->name,
1348                ip->emcr & EMCR_BUFSIZ ? 128 : 64);
1349
1350         return 0;
1351
1352 out_stop:
1353         ioc3_stop(ip);
1354         del_timer_sync(&ip->ioc3_timer);
1355         ioc3_free_rings(ip);
1356 out_res:
1357         pci_release_regions(pdev);
1358 out_free:
1359         free_netdev(dev);
1360 out_disable:
1361         /*
1362          * We should call pci_disable_device(pdev); here if the IOC3 wasn't
1363          * such a weird device ...
1364          */
1365 out:
1366         return err;
1367 }
1368
1369 static void __devexit ioc3_remove_one (struct pci_dev *pdev)
1370 {
1371         struct net_device *dev = pci_get_drvdata(pdev);
1372         struct ioc3_private *ip = netdev_priv(dev);
1373         struct ioc3 *ioc3 = ip->regs;
1374
1375         unregister_netdev(dev);
1376         del_timer_sync(&ip->ioc3_timer);
1377
1378         iounmap(ioc3);
1379         pci_release_regions(pdev);
1380         free_netdev(dev);
1381         /*
1382          * We should call pci_disable_device(pdev); here if the IOC3 wasn't
1383          * such a weird device ...
1384          */
1385 }
1386
1387 static DEFINE_PCI_DEVICE_TABLE(ioc3_pci_tbl) = {
1388         { PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC3, PCI_ANY_ID, PCI_ANY_ID },
1389         { 0 }
1390 };
1391 MODULE_DEVICE_TABLE(pci, ioc3_pci_tbl);
1392
1393 static struct pci_driver ioc3_driver = {
1394         .name           = "ioc3-eth",
1395         .id_table       = ioc3_pci_tbl,
1396         .probe          = ioc3_probe,
1397         .remove         = __devexit_p(ioc3_remove_one),
1398 };
1399
1400 static int __init ioc3_init_module(void)
1401 {
1402         return pci_register_driver(&ioc3_driver);
1403 }
1404
1405 static void __exit ioc3_cleanup_module(void)
1406 {
1407         pci_unregister_driver(&ioc3_driver);
1408 }
1409
1410 static int ioc3_start_xmit(struct sk_buff *skb, struct net_device *dev)
1411 {
1412         unsigned long data;
1413         struct ioc3_private *ip = netdev_priv(dev);
1414         struct ioc3 *ioc3 = ip->regs;
1415         unsigned int len;
1416         struct ioc3_etxd *desc;
1417         uint32_t w0 = 0;
1418         int produce;
1419
1420         /*
1421          * IOC3 has a fairly simple minded checksumming hardware which simply
1422          * adds up the 1's complement checksum for the entire packet and
1423          * inserts it at an offset which can be specified in the descriptor
1424          * into the transmit packet.  This means we have to compensate for the
1425          * MAC header which should not be summed and the TCP/UDP pseudo headers
1426          * manually.
1427          */
1428         if (skb->ip_summed == CHECKSUM_PARTIAL) {
1429                 const struct iphdr *ih = ip_hdr(skb);
1430                 const int proto = ntohs(ih->protocol);
1431                 unsigned int csoff;
1432                 uint32_t csum, ehsum;
1433                 uint16_t *eh;
1434
1435                 /* The MAC header.  skb->mac seem the logic approach
1436                    to find the MAC header - except it's a NULL pointer ...  */
1437                 eh = (uint16_t *) skb->data;
1438
1439                 /* Sum up dest addr, src addr and protocol  */
1440                 ehsum = eh[0] + eh[1] + eh[2] + eh[3] + eh[4] + eh[5] + eh[6];
1441
1442                 /* Fold ehsum.  can't use csum_fold which negates also ...  */
1443                 ehsum = (ehsum & 0xffff) + (ehsum >> 16);
1444                 ehsum = (ehsum & 0xffff) + (ehsum >> 16);
1445
1446                 /* Skip IP header; it's sum is always zero and was
1447                    already filled in by ip_output.c */
1448                 csum = csum_tcpudp_nofold(ih->saddr, ih->daddr,
1449                                           ih->tot_len - (ih->ihl << 2),
1450                                           proto, 0xffff ^ ehsum);
1451
1452                 csum = (csum & 0xffff) + (csum >> 16);  /* Fold again */
1453                 csum = (csum & 0xffff) + (csum >> 16);
1454
1455                 csoff = ETH_HLEN + (ih->ihl << 2);
1456                 if (proto == IPPROTO_UDP) {
1457                         csoff += offsetof(struct udphdr, check);
1458                         udp_hdr(skb)->check = csum;
1459                 }
1460                 if (proto == IPPROTO_TCP) {
1461                         csoff += offsetof(struct tcphdr, check);
1462                         tcp_hdr(skb)->check = csum;
1463                 }
1464
1465                 w0 = ETXD_DOCHECKSUM | (csoff << ETXD_CHKOFF_SHIFT);
1466         }
1467
1468         spin_lock_irq(&ip->ioc3_lock);
1469
1470         data = (unsigned long) skb->data;
1471         len = skb->len;
1472
1473         produce = ip->tx_pi;
1474         desc = &ip->txr[produce];
1475
1476         if (len <= 104) {
1477                 /* Short packet, let's copy it directly into the ring.  */
1478                 skb_copy_from_linear_data(skb, desc->data, skb->len);
1479                 if (len < ETH_ZLEN) {
1480                         /* Very short packet, pad with zeros at the end. */
1481                         memset(desc->data + len, 0, ETH_ZLEN - len);
1482                         len = ETH_ZLEN;
1483                 }
1484                 desc->cmd = cpu_to_be32(len | ETXD_INTWHENDONE | ETXD_D0V | w0);
1485                 desc->bufcnt = cpu_to_be32(len);
1486         } else if ((data ^ (data + len - 1)) & 0x4000) {
1487                 unsigned long b2 = (data | 0x3fffUL) + 1UL;
1488                 unsigned long s1 = b2 - data;
1489                 unsigned long s2 = data + len - b2;
1490
1491                 desc->cmd    = cpu_to_be32(len | ETXD_INTWHENDONE |
1492                                            ETXD_B1V | ETXD_B2V | w0);
1493                 desc->bufcnt = cpu_to_be32((s1 << ETXD_B1CNT_SHIFT) |
1494                                            (s2 << ETXD_B2CNT_SHIFT));
1495                 desc->p1     = cpu_to_be64(ioc3_map(skb->data, 1));
1496                 desc->p2     = cpu_to_be64(ioc3_map((void *) b2, 1));
1497         } else {
1498                 /* Normal sized packet that doesn't cross a page boundary. */
1499                 desc->cmd = cpu_to_be32(len | ETXD_INTWHENDONE | ETXD_B1V | w0);
1500                 desc->bufcnt = cpu_to_be32(len << ETXD_B1CNT_SHIFT);
1501                 desc->p1     = cpu_to_be64(ioc3_map(skb->data, 1));
1502         }
1503
1504         BARRIER();
1505
1506         ip->tx_skbs[produce] = skb;                     /* Remember skb */
1507         produce = (produce + 1) & 127;
1508         ip->tx_pi = produce;
1509         ioc3_w_etpir(produce << 7);                     /* Fire ... */
1510
1511         ip->txqlen++;
1512
1513         if (ip->txqlen >= 127)
1514                 netif_stop_queue(dev);
1515
1516         spin_unlock_irq(&ip->ioc3_lock);
1517
1518         return NETDEV_TX_OK;
1519 }
1520
1521 static void ioc3_timeout(struct net_device *dev)
1522 {
1523         struct ioc3_private *ip = netdev_priv(dev);
1524
1525         printk(KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
1526
1527         spin_lock_irq(&ip->ioc3_lock);
1528
1529         ioc3_stop(ip);
1530         ioc3_init(dev);
1531         ioc3_mii_init(ip);
1532         ioc3_mii_start(ip);
1533
1534         spin_unlock_irq(&ip->ioc3_lock);
1535
1536         netif_wake_queue(dev);
1537 }
1538
1539 /*
1540  * Given a multicast ethernet address, this routine calculates the
1541  * address's bit index in the logical address filter mask
1542  */
1543
1544 static inline unsigned int ioc3_hash(const unsigned char *addr)
1545 {
1546         unsigned int temp = 0;
1547         u32 crc;
1548         int bits;
1549
1550         crc = ether_crc_le(ETH_ALEN, addr);
1551
1552         crc &= 0x3f;    /* bit reverse lowest 6 bits for hash index */
1553         for (bits = 6; --bits >= 0; ) {
1554                 temp <<= 1;
1555                 temp |= (crc & 0x1);
1556                 crc >>= 1;
1557         }
1558
1559         return temp;
1560 }
1561
1562 static void ioc3_get_drvinfo (struct net_device *dev,
1563         struct ethtool_drvinfo *info)
1564 {
1565         struct ioc3_private *ip = netdev_priv(dev);
1566
1567         strcpy (info->driver, IOC3_NAME);
1568         strcpy (info->version, IOC3_VERSION);
1569         strcpy (info->bus_info, pci_name(ip->pdev));
1570 }
1571
1572 static int ioc3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1573 {
1574         struct ioc3_private *ip = netdev_priv(dev);
1575         int rc;
1576
1577         spin_lock_irq(&ip->ioc3_lock);
1578         rc = mii_ethtool_gset(&ip->mii, cmd);
1579         spin_unlock_irq(&ip->ioc3_lock);
1580
1581         return rc;
1582 }
1583
1584 static int ioc3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1585 {
1586         struct ioc3_private *ip = netdev_priv(dev);
1587         int rc;
1588
1589         spin_lock_irq(&ip->ioc3_lock);
1590         rc = mii_ethtool_sset(&ip->mii, cmd);
1591         spin_unlock_irq(&ip->ioc3_lock);
1592
1593         return rc;
1594 }
1595
1596 static int ioc3_nway_reset(struct net_device *dev)
1597 {
1598         struct ioc3_private *ip = netdev_priv(dev);
1599         int rc;
1600
1601         spin_lock_irq(&ip->ioc3_lock);
1602         rc = mii_nway_restart(&ip->mii);
1603         spin_unlock_irq(&ip->ioc3_lock);
1604
1605         return rc;
1606 }
1607
1608 static u32 ioc3_get_link(struct net_device *dev)
1609 {
1610         struct ioc3_private *ip = netdev_priv(dev);
1611         int rc;
1612
1613         spin_lock_irq(&ip->ioc3_lock);
1614         rc = mii_link_ok(&ip->mii);
1615         spin_unlock_irq(&ip->ioc3_lock);
1616
1617         return rc;
1618 }
1619
1620 static u32 ioc3_get_rx_csum(struct net_device *dev)
1621 {
1622         struct ioc3_private *ip = netdev_priv(dev);
1623
1624         return ip->flags & IOC3_FLAG_RX_CHECKSUMS;
1625 }
1626
1627 static int ioc3_set_rx_csum(struct net_device *dev, u32 data)
1628 {
1629         struct ioc3_private *ip = netdev_priv(dev);
1630
1631         spin_lock_bh(&ip->ioc3_lock);
1632         if (data)
1633                 ip->flags |= IOC3_FLAG_RX_CHECKSUMS;
1634         else
1635                 ip->flags &= ~IOC3_FLAG_RX_CHECKSUMS;
1636         spin_unlock_bh(&ip->ioc3_lock);
1637
1638         return 0;
1639 }
1640
1641 static const struct ethtool_ops ioc3_ethtool_ops = {
1642         .get_drvinfo            = ioc3_get_drvinfo,
1643         .get_settings           = ioc3_get_settings,
1644         .set_settings           = ioc3_set_settings,
1645         .nway_reset             = ioc3_nway_reset,
1646         .get_link               = ioc3_get_link,
1647         .get_rx_csum            = ioc3_get_rx_csum,
1648         .set_rx_csum            = ioc3_set_rx_csum,
1649         .get_tx_csum            = ethtool_op_get_tx_csum,
1650         .set_tx_csum            = ethtool_op_set_tx_csum
1651 };
1652
1653 static int ioc3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1654 {
1655         struct ioc3_private *ip = netdev_priv(dev);
1656         int rc;
1657
1658         spin_lock_irq(&ip->ioc3_lock);
1659         rc = generic_mii_ioctl(&ip->mii, if_mii(rq), cmd, NULL);
1660         spin_unlock_irq(&ip->ioc3_lock);
1661
1662         return rc;
1663 }
1664
1665 static void ioc3_set_multicast_list(struct net_device *dev)
1666 {
1667         struct netdev_hw_addr *ha;
1668         struct ioc3_private *ip = netdev_priv(dev);
1669         struct ioc3 *ioc3 = ip->regs;
1670         u64 ehar = 0;
1671
1672         netif_stop_queue(dev);                          /* Lock out others. */
1673
1674         if (dev->flags & IFF_PROMISC) {                 /* Set promiscuous.  */
1675                 ip->emcr |= EMCR_PROMISC;
1676                 ioc3_w_emcr(ip->emcr);
1677                 (void) ioc3_r_emcr();
1678         } else {
1679                 ip->emcr &= ~EMCR_PROMISC;
1680                 ioc3_w_emcr(ip->emcr);                  /* Clear promiscuous. */
1681                 (void) ioc3_r_emcr();
1682
1683                 if ((dev->flags & IFF_ALLMULTI) ||
1684                     (netdev_mc_count(dev) > 64)) {
1685                         /* Too many for hashing to make sense or we want all
1686                            multicast packets anyway,  so skip computing all the
1687                            hashes and just accept all packets.  */
1688                         ip->ehar_h = 0xffffffff;
1689                         ip->ehar_l = 0xffffffff;
1690                 } else {
1691                         netdev_for_each_mc_addr(ha, dev) {
1692                                 char *addr = ha->addr;
1693
1694                                 if (!(*addr & 1))
1695                                         continue;
1696
1697                                 ehar |= (1UL << ioc3_hash(addr));
1698                         }
1699                         ip->ehar_h = ehar >> 32;
1700                         ip->ehar_l = ehar & 0xffffffff;
1701                 }
1702                 ioc3_w_ehar_h(ip->ehar_h);
1703                 ioc3_w_ehar_l(ip->ehar_l);
1704         }
1705
1706         netif_wake_queue(dev);                  /* Let us get going again. */
1707 }
1708
1709 MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
1710 MODULE_DESCRIPTION("SGI IOC3 Ethernet driver");
1711 MODULE_LICENSE("GPL");
1712
1713 module_init(ioc3_init_module);
1714 module_exit(ioc3_cleanup_module);