[PATCH] remove unnecessary config.h includes from drivers/net/
[safe/jmp/linux-2.6] / drivers / net / smc911x.c
1 /*
2  * smc911x.c
3  * This is a driver for SMSC's LAN911{5,6,7,8} single-chip Ethernet devices.
4  *
5  * Copyright (C) 2005 Sensoria Corp
6  *         Derived from the unified SMC91x driver by Nicolas Pitre
7  *         and the smsc911x.c reference driver by SMSC
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  * Arguments:
24  *       watchdog  = TX watchdog timeout
25  *       tx_fifo_kb = Size of TX FIFO in KB
26  *
27  * History:
28  *        04/16/05      Dustin McIntire          Initial version
29  */
30 static const char version[] =
31          "smc911x.c: v1.0 04-16-2005 by Dustin McIntire <dustin@sensoria.com>\n";
32
33 /* Debugging options */
34 #define ENABLE_SMC_DEBUG_RX             0
35 #define ENABLE_SMC_DEBUG_TX             0
36 #define ENABLE_SMC_DEBUG_DMA            0
37 #define ENABLE_SMC_DEBUG_PKTS           0
38 #define ENABLE_SMC_DEBUG_MISC           0
39 #define ENABLE_SMC_DEBUG_FUNC           0
40
41 #define SMC_DEBUG_RX            ((ENABLE_SMC_DEBUG_RX   ? 1 : 0) << 0)
42 #define SMC_DEBUG_TX            ((ENABLE_SMC_DEBUG_TX   ? 1 : 0) << 1)
43 #define SMC_DEBUG_DMA           ((ENABLE_SMC_DEBUG_DMA  ? 1 : 0) << 2)
44 #define SMC_DEBUG_PKTS          ((ENABLE_SMC_DEBUG_PKTS ? 1 : 0) << 3)
45 #define SMC_DEBUG_MISC          ((ENABLE_SMC_DEBUG_MISC ? 1 : 0) << 4)
46 #define SMC_DEBUG_FUNC          ((ENABLE_SMC_DEBUG_FUNC ? 1 : 0) << 5)
47
48 #ifndef SMC_DEBUG
49 #define SMC_DEBUG        ( SMC_DEBUG_RX   | \
50                            SMC_DEBUG_TX   | \
51                            SMC_DEBUG_DMA  | \
52                            SMC_DEBUG_PKTS | \
53                            SMC_DEBUG_MISC | \
54                            SMC_DEBUG_FUNC   \
55                          )
56 #endif
57
58 #include <linux/init.h>
59 #include <linux/module.h>
60 #include <linux/kernel.h>
61 #include <linux/sched.h>
62 #include <linux/slab.h>
63 #include <linux/delay.h>
64 #include <linux/interrupt.h>
65 #include <linux/errno.h>
66 #include <linux/ioport.h>
67 #include <linux/crc32.h>
68 #include <linux/device.h>
69 #include <linux/platform_device.h>
70 #include <linux/spinlock.h>
71 #include <linux/ethtool.h>
72 #include <linux/mii.h>
73 #include <linux/workqueue.h>
74
75 #include <linux/netdevice.h>
76 #include <linux/etherdevice.h>
77 #include <linux/skbuff.h>
78
79 #include <asm/io.h>
80 #include <asm/irq.h>
81
82 #include "smc911x.h"
83
84 /*
85  * Transmit timeout, default 5 seconds.
86  */
87 static int watchdog = 5000;
88 module_param(watchdog, int, 0400);
89 MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
90
91 static int tx_fifo_kb=8;
92 module_param(tx_fifo_kb, int, 0400);
93 MODULE_PARM_DESC(tx_fifo_kb,"transmit FIFO size in KB (1<x<15)(default=8)");
94
95 MODULE_LICENSE("GPL");
96
97 /*
98  * The internal workings of the driver.  If you are changing anything
99  * here with the SMC stuff, you should have the datasheet and know
100  * what you are doing.
101  */
102 #define CARDNAME "smc911x"
103
104 /*
105  * Use power-down feature of the chip
106  */
107 #define POWER_DOWN               1
108
109
110 /* store this information for the driver.. */
111 struct smc911x_local {
112         /*
113          * If I have to wait until the DMA is finished and ready to reload a
114          * packet, I will store the skbuff here. Then, the DMA will send it
115          * out and free it.
116          */
117         struct sk_buff *pending_tx_skb;
118
119         /*
120          * these are things that the kernel wants me to keep, so users
121          * can find out semi-useless statistics of how well the card is
122          * performing
123          */
124         struct net_device_stats stats;
125
126         /* version/revision of the SMC911x chip */
127         u16 version;
128         u16 revision;
129
130         /* FIFO sizes */
131         int tx_fifo_kb;
132         int tx_fifo_size;
133         int rx_fifo_size;
134         int afc_cfg;
135
136         /* Contains the current active receive/phy mode */
137         int ctl_rfduplx;
138         int ctl_rspeed;
139
140         u32 msg_enable;
141         u32 phy_type;
142         struct mii_if_info mii;
143
144         /* work queue */
145         struct work_struct phy_configure;
146         int work_pending;
147
148         int tx_throttle;
149         spinlock_t lock;
150
151 #ifdef SMC_USE_DMA
152         /* DMA needs the physical address of the chip */
153         u_long physaddr;
154         int rxdma;
155         int txdma;
156         int rxdma_active;
157         int txdma_active;
158         struct sk_buff *current_rx_skb;
159         struct sk_buff *current_tx_skb;
160         struct device *dev;
161 #endif
162 };
163
164 #if SMC_DEBUG > 0
165 #define DBG(n, args...)                          \
166         do {                                     \
167                 if (SMC_DEBUG & (n))             \
168                         printk(args);            \
169         } while (0)
170
171 #define PRINTK(args...)   printk(args)
172 #else
173 #define DBG(n, args...)   do { } while (0)
174 #define PRINTK(args...)   printk(KERN_DEBUG args)
175 #endif
176
177 #if SMC_DEBUG_PKTS > 0
178 static void PRINT_PKT(u_char *buf, int length)
179 {
180         int i;
181         int remainder;
182         int lines;
183
184         lines = length / 16;
185         remainder = length % 16;
186
187         for (i = 0; i < lines ; i ++) {
188                 int cur;
189                 for (cur = 0; cur < 8; cur++) {
190                         u_char a, b;
191                         a = *buf++;
192                         b = *buf++;
193                         printk("%02x%02x ", a, b);
194                 }
195                 printk("\n");
196         }
197         for (i = 0; i < remainder/2 ; i++) {
198                 u_char a, b;
199                 a = *buf++;
200                 b = *buf++;
201                 printk("%02x%02x ", a, b);
202         }
203         printk("\n");
204 }
205 #else
206 #define PRINT_PKT(x...)  do { } while (0)
207 #endif
208
209
210 /* this enables an interrupt in the interrupt mask register */
211 #define SMC_ENABLE_INT(x) do {                          \
212         unsigned int  __mask;                           \
213         unsigned long __flags;                          \
214         spin_lock_irqsave(&lp->lock, __flags);          \
215         __mask = SMC_GET_INT_EN();                      \
216         __mask |= (x);                                  \
217         SMC_SET_INT_EN(__mask);                         \
218         spin_unlock_irqrestore(&lp->lock, __flags);     \
219 } while (0)
220
221 /* this disables an interrupt from the interrupt mask register */
222 #define SMC_DISABLE_INT(x) do {                         \
223         unsigned int  __mask;                           \
224         unsigned long __flags;                          \
225         spin_lock_irqsave(&lp->lock, __flags);          \
226         __mask = SMC_GET_INT_EN();                      \
227         __mask &= ~(x);                                 \
228         SMC_SET_INT_EN(__mask);                         \
229         spin_unlock_irqrestore(&lp->lock, __flags);     \
230 } while (0)
231
232 /*
233  * this does a soft reset on the device
234  */
235 static void smc911x_reset(struct net_device *dev)
236 {
237         unsigned long ioaddr = dev->base_addr;
238         struct smc911x_local *lp = netdev_priv(dev);
239         unsigned int reg, timeout=0, resets=1;
240         unsigned long flags;
241
242         DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
243
244         /*       Take out of PM setting first */
245         if ((SMC_GET_PMT_CTRL() & PMT_CTRL_READY_) == 0) {
246                 /* Write to the bytetest will take out of powerdown */
247                 SMC_SET_BYTE_TEST(0);
248                 timeout=10;
249                 do {
250                         udelay(10);
251                         reg = SMC_GET_PMT_CTRL() & PMT_CTRL_READY_;
252                 } while ( timeout-- && !reg);
253                 if (timeout == 0) {
254                         PRINTK("%s: smc911x_reset timeout waiting for PM restore\n", dev->name);
255                         return;
256                 }
257         }
258
259         /* Disable all interrupts */
260         spin_lock_irqsave(&lp->lock, flags);
261         SMC_SET_INT_EN(0);
262         spin_unlock_irqrestore(&lp->lock, flags);
263
264         while (resets--) {
265                 SMC_SET_HW_CFG(HW_CFG_SRST_);
266                 timeout=10;
267                 do {
268                         udelay(10);
269                         reg = SMC_GET_HW_CFG();
270                         /* If chip indicates reset timeout then try again */
271                         if (reg & HW_CFG_SRST_TO_) {
272                                 PRINTK("%s: chip reset timeout, retrying...\n", dev->name);
273                                 resets++;
274                                 break;
275                         }
276                 } while ( timeout-- && (reg & HW_CFG_SRST_));
277         }
278         if (timeout == 0) {
279                 PRINTK("%s: smc911x_reset timeout waiting for reset\n", dev->name);
280                 return;
281         }
282
283         /* make sure EEPROM has finished loading before setting GPIO_CFG */
284         timeout=1000;
285         while ( timeout-- && (SMC_GET_E2P_CMD() & E2P_CMD_EPC_BUSY_)) {
286                 udelay(10);
287         }
288         if (timeout == 0){
289                 PRINTK("%s: smc911x_reset timeout waiting for EEPROM busy\n", dev->name);
290                 return;
291         }
292
293         /* Initialize interrupts */
294         SMC_SET_INT_EN(0);
295         SMC_ACK_INT(-1);
296
297         /* Reset the FIFO level and flow control settings */
298         SMC_SET_HW_CFG((lp->tx_fifo_kb & 0xF) << 16);
299 //TODO: Figure out what appropriate pause time is
300         SMC_SET_FLOW(FLOW_FCPT_ | FLOW_FCEN_);
301         SMC_SET_AFC_CFG(lp->afc_cfg);
302
303
304         /* Set to LED outputs */
305         SMC_SET_GPIO_CFG(0x70070000);
306
307         /*
308          * Deassert IRQ for 1*10us for edge type interrupts
309          * and drive IRQ pin push-pull
310          */
311         SMC_SET_IRQ_CFG( (1 << 24) | INT_CFG_IRQ_EN_ | INT_CFG_IRQ_TYPE_ );
312
313         /* clear anything saved */
314         if (lp->pending_tx_skb != NULL) {
315                 dev_kfree_skb (lp->pending_tx_skb);
316                 lp->pending_tx_skb = NULL;
317                 lp->stats.tx_errors++;
318                 lp->stats.tx_aborted_errors++;
319         }
320 }
321
322 /*
323  * Enable Interrupts, Receive, and Transmit
324  */
325 static void smc911x_enable(struct net_device *dev)
326 {
327         unsigned long ioaddr = dev->base_addr;
328         struct smc911x_local *lp = netdev_priv(dev);
329         unsigned mask, cfg, cr;
330         unsigned long flags;
331
332         DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
333
334         SMC_SET_MAC_ADDR(dev->dev_addr);
335
336         /* Enable TX */
337         cfg = SMC_GET_HW_CFG();
338         cfg &= HW_CFG_TX_FIF_SZ_ | 0xFFF;
339         cfg |= HW_CFG_SF_;
340         SMC_SET_HW_CFG(cfg);
341         SMC_SET_FIFO_TDA(0xFF);
342         /* Update TX stats on every 64 packets received or every 1 sec */
343         SMC_SET_FIFO_TSL(64);
344         SMC_SET_GPT_CFG(GPT_CFG_TIMER_EN_ | 10000);
345
346         spin_lock_irqsave(&lp->lock, flags);
347         SMC_GET_MAC_CR(cr);
348         cr |= MAC_CR_TXEN_ | MAC_CR_HBDIS_;
349         SMC_SET_MAC_CR(cr);
350         SMC_SET_TX_CFG(TX_CFG_TX_ON_);
351         spin_unlock_irqrestore(&lp->lock, flags);
352
353         /* Add 2 byte padding to start of packets */
354         SMC_SET_RX_CFG((2<<8) & RX_CFG_RXDOFF_);
355
356         /* Turn on receiver and enable RX */
357         if (cr & MAC_CR_RXEN_)
358                 DBG(SMC_DEBUG_RX, "%s: Receiver already enabled\n", dev->name);
359
360         spin_lock_irqsave(&lp->lock, flags);
361         SMC_SET_MAC_CR( cr | MAC_CR_RXEN_ );
362         spin_unlock_irqrestore(&lp->lock, flags);
363
364         /* Interrupt on every received packet */
365         SMC_SET_FIFO_RSA(0x01);
366         SMC_SET_FIFO_RSL(0x00);
367
368         /* now, enable interrupts */
369         mask = INT_EN_TDFA_EN_ | INT_EN_TSFL_EN_ | INT_EN_RSFL_EN_ |
370                 INT_EN_GPT_INT_EN_ | INT_EN_RXDFH_INT_EN_ | INT_EN_RXE_EN_ |
371                 INT_EN_PHY_INT_EN_;
372         if (IS_REV_A(lp->revision))
373                 mask|=INT_EN_RDFL_EN_;
374         else {
375                 mask|=INT_EN_RDFO_EN_;
376         }
377         SMC_ENABLE_INT(mask);
378 }
379
380 /*
381  * this puts the device in an inactive state
382  */
383 static void smc911x_shutdown(struct net_device *dev)
384 {
385         unsigned long ioaddr = dev->base_addr;
386         struct smc911x_local *lp = netdev_priv(dev);
387         unsigned cr;
388         unsigned long flags;
389
390         DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", CARDNAME, __FUNCTION__);
391
392         /* Disable IRQ's */
393         SMC_SET_INT_EN(0);
394
395         /* Turn of Rx and TX */
396         spin_lock_irqsave(&lp->lock, flags);
397         SMC_GET_MAC_CR(cr);
398         cr &= ~(MAC_CR_TXEN_ | MAC_CR_RXEN_ | MAC_CR_HBDIS_);
399         SMC_SET_MAC_CR(cr);
400         SMC_SET_TX_CFG(TX_CFG_STOP_TX_);
401         spin_unlock_irqrestore(&lp->lock, flags);
402 }
403
404 static inline void smc911x_drop_pkt(struct net_device *dev)
405 {
406         unsigned long ioaddr = dev->base_addr;
407         unsigned int fifo_count, timeout, reg;
408
409         DBG(SMC_DEBUG_FUNC | SMC_DEBUG_RX, "%s: --> %s\n", CARDNAME, __FUNCTION__);
410         fifo_count = SMC_GET_RX_FIFO_INF() & 0xFFFF;
411         if (fifo_count <= 4) {
412                 /* Manually dump the packet data */
413                 while (fifo_count--)
414                         SMC_GET_RX_FIFO();
415         } else   {
416                 /* Fast forward through the bad packet */
417                 SMC_SET_RX_DP_CTRL(RX_DP_CTRL_FFWD_BUSY_);
418                 timeout=50;
419                 do {
420                         udelay(10);
421                         reg = SMC_GET_RX_DP_CTRL() & RX_DP_CTRL_FFWD_BUSY_;
422                 } while ( timeout-- && reg);
423                 if (timeout == 0) {
424                         PRINTK("%s: timeout waiting for RX fast forward\n", dev->name);
425                 }
426         }
427 }
428
429 /*
430  * This is the procedure to handle the receipt of a packet.
431  * It should be called after checking for packet presence in
432  * the RX status FIFO.   It must be called with the spin lock
433  * already held.
434  */
435 static inline void       smc911x_rcv(struct net_device *dev)
436 {
437         struct smc911x_local *lp = netdev_priv(dev);
438         unsigned long ioaddr = dev->base_addr;
439         unsigned int pkt_len, status;
440         struct sk_buff *skb;
441         unsigned char *data;
442
443         DBG(SMC_DEBUG_FUNC | SMC_DEBUG_RX, "%s: --> %s\n",
444                 dev->name, __FUNCTION__);
445         status = SMC_GET_RX_STS_FIFO();
446         DBG(SMC_DEBUG_RX, "%s: Rx pkt len %d status 0x%08x \n",
447                 dev->name, (status & 0x3fff0000) >> 16, status & 0xc000ffff);
448         pkt_len = (status & RX_STS_PKT_LEN_) >> 16;
449         if (status & RX_STS_ES_) {
450                 /* Deal with a bad packet */
451                 lp->stats.rx_errors++;
452                 if (status & RX_STS_CRC_ERR_)
453                         lp->stats.rx_crc_errors++;
454                 else {
455                         if (status & RX_STS_LEN_ERR_)
456                                 lp->stats.rx_length_errors++;
457                         if (status & RX_STS_MCAST_)
458                                 lp->stats.multicast++;
459                 }
460                 /* Remove the bad packet data from the RX FIFO */
461                 smc911x_drop_pkt(dev);
462         } else {
463                 /* Receive a valid packet */
464                 /* Alloc a buffer with extra room for DMA alignment */
465                 skb=dev_alloc_skb(pkt_len+32);
466                 if (unlikely(skb == NULL)) {
467                         PRINTK( "%s: Low memory, rcvd packet dropped.\n",
468                                 dev->name);
469                         lp->stats.rx_dropped++;
470                         smc911x_drop_pkt(dev);
471                         return;
472                 }
473                 /* Align IP header to 32 bits
474                  * Note that the device is configured to add a 2
475                  * byte padding to the packet start, so we really
476                  * want to write to the orignal data pointer */
477                 data = skb->data;
478                 skb_reserve(skb, 2);
479                 skb_put(skb,pkt_len-4);
480 #ifdef SMC_USE_DMA
481                 {
482                 unsigned int fifo;
483                 /* Lower the FIFO threshold if possible */
484                 fifo = SMC_GET_FIFO_INT();
485                 if (fifo & 0xFF) fifo--;
486                 DBG(SMC_DEBUG_RX, "%s: Setting RX stat FIFO threshold to %d\n",
487                         dev->name, fifo & 0xff);
488                 SMC_SET_FIFO_INT(fifo);
489                 /* Setup RX DMA */
490                 SMC_SET_RX_CFG(RX_CFG_RX_END_ALGN16_ | ((2<<8) & RX_CFG_RXDOFF_));
491                 lp->rxdma_active = 1;
492                 lp->current_rx_skb = skb;
493                 SMC_PULL_DATA(data, (pkt_len+2+15) & ~15);
494                 /* Packet processing deferred to DMA RX interrupt */
495                 }
496 #else
497                 SMC_SET_RX_CFG(RX_CFG_RX_END_ALGN4_ | ((2<<8) & RX_CFG_RXDOFF_));
498                 SMC_PULL_DATA(data, pkt_len+2+3);
499
500                 DBG(SMC_DEBUG_PKTS, "%s: Received packet\n", dev->name,);
501                 PRINT_PKT(data, ((pkt_len - 4) <= 64) ? pkt_len - 4 : 64);
502                 dev->last_rx = jiffies;
503                 skb->dev = dev;
504                 skb->protocol = eth_type_trans(skb, dev);
505                 netif_rx(skb);
506                 lp->stats.rx_packets++;
507                 lp->stats.rx_bytes += pkt_len-4;
508 #endif
509         }
510 }
511
512 /*
513  * This is called to actually send a packet to the chip.
514  */
515 static void smc911x_hardware_send_pkt(struct net_device *dev)
516 {
517         struct smc911x_local *lp = netdev_priv(dev);
518         unsigned long ioaddr = dev->base_addr;
519         struct sk_buff *skb;
520         unsigned int cmdA, cmdB, len;
521         unsigned char *buf;
522         unsigned long flags;
523
524         DBG(SMC_DEBUG_FUNC | SMC_DEBUG_TX, "%s: --> %s\n", dev->name, __FUNCTION__);
525         BUG_ON(lp->pending_tx_skb == NULL);
526
527         skb = lp->pending_tx_skb;
528         lp->pending_tx_skb = NULL;
529
530         /* cmdA {25:24] data alignment [20:16] start offset [10:0] buffer length */
531         /* cmdB {31:16] pkt tag [10:0] length */
532 #ifdef SMC_USE_DMA
533         /* 16 byte buffer alignment mode */
534         buf = (char*)((u32)(skb->data) & ~0xF);
535         len = (skb->len + 0xF + ((u32)skb->data & 0xF)) & ~0xF;
536         cmdA = (1<<24) | (((u32)skb->data & 0xF)<<16) |
537                         TX_CMD_A_INT_FIRST_SEG_ | TX_CMD_A_INT_LAST_SEG_ |
538                         skb->len;
539 #else
540         buf = (char*)((u32)skb->data & ~0x3);
541         len = (skb->len + 3 + ((u32)skb->data & 3)) & ~0x3;
542         cmdA = (((u32)skb->data & 0x3) << 16) |
543                         TX_CMD_A_INT_FIRST_SEG_ | TX_CMD_A_INT_LAST_SEG_ |
544                         skb->len;
545 #endif
546         /* tag is packet length so we can use this in stats update later */
547         cmdB = (skb->len  << 16) | (skb->len & 0x7FF);
548
549         DBG(SMC_DEBUG_TX, "%s: TX PKT LENGTH 0x%04x (%d) BUF 0x%p CMDA 0x%08x CMDB 0x%08x\n",
550                  dev->name, len, len, buf, cmdA, cmdB);
551         SMC_SET_TX_FIFO(cmdA);
552         SMC_SET_TX_FIFO(cmdB);
553
554         DBG(SMC_DEBUG_PKTS, "%s: Transmitted packet\n", dev->name);
555         PRINT_PKT(buf, len <= 64 ? len : 64);
556
557         /* Send pkt via PIO or DMA */
558 #ifdef SMC_USE_DMA
559         lp->current_tx_skb = skb;
560         SMC_PUSH_DATA(buf, len);
561         /* DMA complete IRQ will free buffer and set jiffies */
562 #else
563         SMC_PUSH_DATA(buf, len);
564         dev->trans_start = jiffies;
565         dev_kfree_skb(skb);
566 #endif
567         spin_lock_irqsave(&lp->lock, flags);
568         if (!lp->tx_throttle) {
569                 netif_wake_queue(dev);
570         }
571         spin_unlock_irqrestore(&lp->lock, flags);
572         SMC_ENABLE_INT(INT_EN_TDFA_EN_ | INT_EN_TSFL_EN_);
573 }
574
575 /*
576  * Since I am not sure if I will have enough room in the chip's ram
577  * to store the packet, I call this routine which either sends it
578  * now, or set the card to generates an interrupt when ready
579  * for the packet.
580  */
581 static int smc911x_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
582 {
583         struct smc911x_local *lp = netdev_priv(dev);
584         unsigned long ioaddr = dev->base_addr;
585         unsigned int free;
586         unsigned long flags;
587
588         DBG(SMC_DEBUG_FUNC | SMC_DEBUG_TX, "%s: --> %s\n",
589                 dev->name, __FUNCTION__);
590
591         BUG_ON(lp->pending_tx_skb != NULL);
592
593         free = SMC_GET_TX_FIFO_INF() & TX_FIFO_INF_TDFREE_;
594         DBG(SMC_DEBUG_TX, "%s: TX free space %d\n", dev->name, free);
595
596         /* Turn off the flow when running out of space in FIFO */
597         if (free <= SMC911X_TX_FIFO_LOW_THRESHOLD) {
598                 DBG(SMC_DEBUG_TX, "%s: Disabling data flow due to low FIFO space (%d)\n",
599                         dev->name, free);
600                 spin_lock_irqsave(&lp->lock, flags);
601                 /* Reenable when at least 1 packet of size MTU present */
602                 SMC_SET_FIFO_TDA((SMC911X_TX_FIFO_LOW_THRESHOLD)/64);
603                 lp->tx_throttle = 1;
604                 netif_stop_queue(dev);
605                 spin_unlock_irqrestore(&lp->lock, flags);
606         }
607
608         /* Drop packets when we run out of space in TX FIFO
609          * Account for overhead required for:
610          *
611          *        Tx command words                       8 bytes
612          *        Start offset                           15 bytes
613          *        End padding                            15 bytes
614          */
615         if (unlikely(free < (skb->len + 8 + 15 + 15))) {
616                 printk("%s: No Tx free space %d < %d\n",
617                         dev->name, free, skb->len);
618                 lp->pending_tx_skb = NULL;
619                 lp->stats.tx_errors++;
620                 lp->stats.tx_dropped++;
621                 dev_kfree_skb(skb);
622                 return 0;
623         }
624
625 #ifdef SMC_USE_DMA
626         {
627                 /* If the DMA is already running then defer this packet Tx until
628                  * the DMA IRQ starts it
629                  */
630                 spin_lock_irqsave(&lp->lock, flags);
631                 if (lp->txdma_active) {
632                         DBG(SMC_DEBUG_TX | SMC_DEBUG_DMA, "%s: Tx DMA running, deferring packet\n", dev->name);
633                         lp->pending_tx_skb = skb;
634                         netif_stop_queue(dev);
635                         spin_unlock_irqrestore(&lp->lock, flags);
636                         return 0;
637                 } else {
638                         DBG(SMC_DEBUG_TX | SMC_DEBUG_DMA, "%s: Activating Tx DMA\n", dev->name);
639                         lp->txdma_active = 1;
640                 }
641                 spin_unlock_irqrestore(&lp->lock, flags);
642         }
643 #endif
644         lp->pending_tx_skb = skb;
645         smc911x_hardware_send_pkt(dev);
646
647         return 0;
648 }
649
650 /*
651  * This handles a TX status interrupt, which is only called when:
652  * - a TX error occurred, or
653  * - TX of a packet completed.
654  */
655 static void smc911x_tx(struct net_device *dev)
656 {
657         unsigned long ioaddr = dev->base_addr;
658         struct smc911x_local *lp = netdev_priv(dev);
659         unsigned int tx_status;
660
661         DBG(SMC_DEBUG_FUNC | SMC_DEBUG_TX, "%s: --> %s\n",
662                 dev->name, __FUNCTION__);
663
664         /* Collect the TX status */
665         while (((SMC_GET_TX_FIFO_INF() & TX_FIFO_INF_TSUSED_) >> 16) != 0) {
666                 DBG(SMC_DEBUG_TX, "%s: Tx stat FIFO used 0x%04x\n",
667                         dev->name,
668                         (SMC_GET_TX_FIFO_INF() & TX_FIFO_INF_TSUSED_) >> 16);
669                 tx_status = SMC_GET_TX_STS_FIFO();
670                 lp->stats.tx_packets++;
671                 lp->stats.tx_bytes+=tx_status>>16;
672                 DBG(SMC_DEBUG_TX, "%s: Tx FIFO tag 0x%04x status 0x%04x\n",
673                         dev->name, (tx_status & 0xffff0000) >> 16,
674                         tx_status & 0x0000ffff);
675                 /* count Tx errors, but ignore lost carrier errors when in
676                  * full-duplex mode */
677                 if ((tx_status & TX_STS_ES_) && !(lp->ctl_rfduplx &&
678                     !(tx_status & 0x00000306))) {
679                         lp->stats.tx_errors++;
680                 }
681                 if (tx_status & TX_STS_MANY_COLL_) {
682                         lp->stats.collisions+=16;
683                         lp->stats.tx_aborted_errors++;
684                 } else {
685                         lp->stats.collisions+=(tx_status & TX_STS_COLL_CNT_) >> 3;
686                 }
687                 /* carrier error only has meaning for half-duplex communication */
688                 if ((tx_status & (TX_STS_LOC_ | TX_STS_NO_CARR_)) &&
689                     !lp->ctl_rfduplx) {
690                         lp->stats.tx_carrier_errors++;
691                 }
692                 if (tx_status & TX_STS_LATE_COLL_) {
693                         lp->stats.collisions++;
694                         lp->stats.tx_aborted_errors++;
695                 }
696         }
697 }
698
699
700 /*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
701 /*
702  * Reads a register from the MII Management serial interface
703  */
704
705 static int smc911x_phy_read(struct net_device *dev, int phyaddr, int phyreg)
706 {
707         unsigned long ioaddr = dev->base_addr;
708         unsigned int phydata;
709
710         SMC_GET_MII(phyreg, phyaddr, phydata);
711
712         DBG(SMC_DEBUG_MISC, "%s: phyaddr=0x%x, phyreg=0x%02x, phydata=0x%04x\n",
713                 __FUNCTION__, phyaddr, phyreg, phydata);
714         return phydata;
715 }
716
717
718 /*
719  * Writes a register to the MII Management serial interface
720  */
721 static void smc911x_phy_write(struct net_device *dev, int phyaddr, int phyreg,
722                         int phydata)
723 {
724         unsigned long ioaddr = dev->base_addr;
725
726         DBG(SMC_DEBUG_MISC, "%s: phyaddr=0x%x, phyreg=0x%x, phydata=0x%x\n",
727                 __FUNCTION__, phyaddr, phyreg, phydata);
728
729         SMC_SET_MII(phyreg, phyaddr, phydata);
730 }
731
732 /*
733  * Finds and reports the PHY address (115 and 117 have external
734  * PHY interface 118 has internal only
735  */
736 static void smc911x_phy_detect(struct net_device *dev)
737 {
738         unsigned long ioaddr = dev->base_addr;
739         struct smc911x_local *lp = netdev_priv(dev);
740         int phyaddr;
741         unsigned int cfg, id1, id2;
742
743         DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
744
745         lp->phy_type = 0;
746
747         /*
748          * Scan all 32 PHY addresses if necessary, starting at
749          * PHY#1 to PHY#31, and then PHY#0 last.
750          */
751         switch(lp->version) {
752                 case 0x115:
753                 case 0x117:
754                         cfg = SMC_GET_HW_CFG();
755                         if (cfg & HW_CFG_EXT_PHY_DET_) {
756                                 cfg &= ~HW_CFG_PHY_CLK_SEL_;
757                                 cfg |= HW_CFG_PHY_CLK_SEL_CLK_DIS_;
758                                 SMC_SET_HW_CFG(cfg);
759                                 udelay(10); /* Wait for clocks to stop */
760
761                                 cfg |= HW_CFG_EXT_PHY_EN_;
762                                 SMC_SET_HW_CFG(cfg);
763                                 udelay(10); /* Wait for clocks to stop */
764
765                                 cfg &= ~HW_CFG_PHY_CLK_SEL_;
766                                 cfg |= HW_CFG_PHY_CLK_SEL_EXT_PHY_;
767                                 SMC_SET_HW_CFG(cfg);
768                                 udelay(10); /* Wait for clocks to stop */
769
770                                 cfg |= HW_CFG_SMI_SEL_;
771                                 SMC_SET_HW_CFG(cfg);
772
773                                 for (phyaddr = 1; phyaddr < 32; ++phyaddr) {
774
775                                         /* Read the PHY identifiers */
776                                         SMC_GET_PHY_ID1(phyaddr & 31, id1);
777                                         SMC_GET_PHY_ID2(phyaddr & 31, id2);
778
779                                         /* Make sure it is a valid identifier */
780                                         if (id1 != 0x0000 && id1 != 0xffff &&
781                                             id1 != 0x8000 && id2 != 0x0000 &&
782                                             id2 != 0xffff && id2 != 0x8000) {
783                                                 /* Save the PHY's address */
784                                                 lp->mii.phy_id = phyaddr & 31;
785                                                 lp->phy_type = id1 << 16 | id2;
786                                                 break;
787                                         }
788                                 }
789                         }
790                 default:
791                         /* Internal media only */
792                         SMC_GET_PHY_ID1(1, id1);
793                         SMC_GET_PHY_ID2(1, id2);
794                         /* Save the PHY's address */
795                         lp->mii.phy_id = 1;
796                         lp->phy_type = id1 << 16 | id2;
797         }
798
799         DBG(SMC_DEBUG_MISC, "%s: phy_id1=0x%x, phy_id2=0x%x phyaddr=0x%d\n",
800                 dev->name, id1, id2, lp->mii.phy_id);
801 }
802
803 /*
804  * Sets the PHY to a configuration as determined by the user.
805  * Called with spin_lock held.
806  */
807 static int smc911x_phy_fixed(struct net_device *dev)
808 {
809         struct smc911x_local *lp = netdev_priv(dev);
810         unsigned long ioaddr = dev->base_addr;
811         int phyaddr = lp->mii.phy_id;
812         int bmcr;
813
814         DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
815
816         /* Enter Link Disable state */
817         SMC_GET_PHY_BMCR(phyaddr, bmcr);
818         bmcr |= BMCR_PDOWN;
819         SMC_SET_PHY_BMCR(phyaddr, bmcr);
820
821         /*
822          * Set our fixed capabilities
823          * Disable auto-negotiation
824          */
825         bmcr &= ~BMCR_ANENABLE;
826         if (lp->ctl_rfduplx)
827                 bmcr |= BMCR_FULLDPLX;
828
829         if (lp->ctl_rspeed == 100)
830                 bmcr |= BMCR_SPEED100;
831
832         /* Write our capabilities to the phy control register */
833         SMC_SET_PHY_BMCR(phyaddr, bmcr);
834
835         /* Re-Configure the Receive/Phy Control register */
836         bmcr &= ~BMCR_PDOWN;
837         SMC_SET_PHY_BMCR(phyaddr, bmcr);
838
839         return 1;
840 }
841
842 /*
843  * smc911x_phy_reset - reset the phy
844  * @dev: net device
845  * @phy: phy address
846  *
847  * Issue a software reset for the specified PHY and
848  * wait up to 100ms for the reset to complete.   We should
849  * not access the PHY for 50ms after issuing the reset.
850  *
851  * The time to wait appears to be dependent on the PHY.
852  *
853  */
854 static int smc911x_phy_reset(struct net_device *dev, int phy)
855 {
856         struct smc911x_local *lp = netdev_priv(dev);
857         unsigned long ioaddr = dev->base_addr;
858         int timeout;
859         unsigned long flags;
860         unsigned int reg;
861
862         DBG(SMC_DEBUG_FUNC, "%s: --> %s()\n", dev->name, __FUNCTION__);
863
864         spin_lock_irqsave(&lp->lock, flags);
865         reg = SMC_GET_PMT_CTRL();
866         reg &= ~0xfffff030;
867         reg |= PMT_CTRL_PHY_RST_;
868         SMC_SET_PMT_CTRL(reg);
869         spin_unlock_irqrestore(&lp->lock, flags);
870         for (timeout = 2; timeout; timeout--) {
871                 msleep(50);
872                 spin_lock_irqsave(&lp->lock, flags);
873                 reg = SMC_GET_PMT_CTRL();
874                 spin_unlock_irqrestore(&lp->lock, flags);
875                 if (!(reg & PMT_CTRL_PHY_RST_)) {
876                         /* extra delay required because the phy may
877                          * not be completed with its reset
878                          * when PHY_BCR_RESET_ is cleared. 256us
879                          * should suffice, but use 500us to be safe
880                          */
881                         udelay(500);
882                 break;
883                 }
884         }
885
886         return reg & PMT_CTRL_PHY_RST_;
887 }
888
889 /*
890  * smc911x_phy_powerdown - powerdown phy
891  * @dev: net device
892  * @phy: phy address
893  *
894  * Power down the specified PHY
895  */
896 static void smc911x_phy_powerdown(struct net_device *dev, int phy)
897 {
898         unsigned long ioaddr = dev->base_addr;
899         unsigned int bmcr;
900
901         /* Enter Link Disable state */
902         SMC_GET_PHY_BMCR(phy, bmcr);
903         bmcr |= BMCR_PDOWN;
904         SMC_SET_PHY_BMCR(phy, bmcr);
905 }
906
907 /*
908  * smc911x_phy_check_media - check the media status and adjust BMCR
909  * @dev: net device
910  * @init: set true for initialisation
911  *
912  * Select duplex mode depending on negotiation state.   This
913  * also updates our carrier state.
914  */
915 static void smc911x_phy_check_media(struct net_device *dev, int init)
916 {
917         struct smc911x_local *lp = netdev_priv(dev);
918         unsigned long ioaddr = dev->base_addr;
919         int phyaddr = lp->mii.phy_id;
920         unsigned int bmcr, cr;
921
922         DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
923
924         if (mii_check_media(&lp->mii, netif_msg_link(lp), init)) {
925                 /* duplex state has changed */
926                 SMC_GET_PHY_BMCR(phyaddr, bmcr);
927                 SMC_GET_MAC_CR(cr);
928                 if (lp->mii.full_duplex) {
929                         DBG(SMC_DEBUG_MISC, "%s: Configuring for full-duplex mode\n", dev->name);
930                         bmcr |= BMCR_FULLDPLX;
931                         cr |= MAC_CR_RCVOWN_;
932                 } else {
933                         DBG(SMC_DEBUG_MISC, "%s: Configuring for half-duplex mode\n", dev->name);
934                         bmcr &= ~BMCR_FULLDPLX;
935                         cr &= ~MAC_CR_RCVOWN_;
936                 }
937                 SMC_SET_PHY_BMCR(phyaddr, bmcr);
938                 SMC_SET_MAC_CR(cr);
939         }
940 }
941
942 /*
943  * Configures the specified PHY through the MII management interface
944  * using Autonegotiation.
945  * Calls smc911x_phy_fixed() if the user has requested a certain config.
946  * If RPC ANEG bit is set, the media selection is dependent purely on
947  * the selection by the MII (either in the MII BMCR reg or the result
948  * of autonegotiation.)  If the RPC ANEG bit is cleared, the selection
949  * is controlled by the RPC SPEED and RPC DPLX bits.
950  */
951 static void smc911x_phy_configure(void *data)
952 {
953         struct net_device *dev = data;
954         struct smc911x_local *lp = netdev_priv(dev);
955         unsigned long ioaddr = dev->base_addr;
956         int phyaddr = lp->mii.phy_id;
957         int my_phy_caps; /* My PHY capabilities */
958         int my_ad_caps; /* My Advertised capabilities */
959         int status;
960         unsigned long flags;
961
962         DBG(SMC_DEBUG_FUNC, "%s: --> %s()\n", dev->name, __FUNCTION__);
963
964         /*
965          * We should not be called if phy_type is zero.
966          */
967         if (lp->phy_type == 0)
968                  goto smc911x_phy_configure_exit;
969
970         if (smc911x_phy_reset(dev, phyaddr)) {
971                 printk("%s: PHY reset timed out\n", dev->name);
972                 goto smc911x_phy_configure_exit;
973         }
974         spin_lock_irqsave(&lp->lock, flags);
975
976         /*
977          * Enable PHY Interrupts (for register 18)
978          * Interrupts listed here are enabled
979          */
980         SMC_SET_PHY_INT_MASK(phyaddr, PHY_INT_MASK_ENERGY_ON_ |
981                  PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_REMOTE_FAULT_ |
982                  PHY_INT_MASK_LINK_DOWN_);
983
984         /* If the user requested no auto neg, then go set his request */
985         if (lp->mii.force_media) {
986                 smc911x_phy_fixed(dev);
987                 goto smc911x_phy_configure_exit;
988         }
989
990         /* Copy our capabilities from MII_BMSR to MII_ADVERTISE */
991         SMC_GET_PHY_BMSR(phyaddr, my_phy_caps);
992         if (!(my_phy_caps & BMSR_ANEGCAPABLE)) {
993                 printk(KERN_INFO "Auto negotiation NOT supported\n");
994                 smc911x_phy_fixed(dev);
995                 goto smc911x_phy_configure_exit;
996         }
997
998         /* CSMA capable w/ both pauses */
999         my_ad_caps = ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1000
1001         if (my_phy_caps & BMSR_100BASE4)
1002                 my_ad_caps |= ADVERTISE_100BASE4;
1003         if (my_phy_caps & BMSR_100FULL)
1004                 my_ad_caps |= ADVERTISE_100FULL;
1005         if (my_phy_caps & BMSR_100HALF)
1006                 my_ad_caps |= ADVERTISE_100HALF;
1007         if (my_phy_caps & BMSR_10FULL)
1008                 my_ad_caps |= ADVERTISE_10FULL;
1009         if (my_phy_caps & BMSR_10HALF)
1010                 my_ad_caps |= ADVERTISE_10HALF;
1011
1012         /* Disable capabilities not selected by our user */
1013         if (lp->ctl_rspeed != 100)
1014                 my_ad_caps &= ~(ADVERTISE_100BASE4|ADVERTISE_100FULL|ADVERTISE_100HALF);
1015
1016          if (!lp->ctl_rfduplx)
1017                 my_ad_caps &= ~(ADVERTISE_100FULL|ADVERTISE_10FULL);
1018
1019         /* Update our Auto-Neg Advertisement Register */
1020         SMC_SET_PHY_MII_ADV(phyaddr, my_ad_caps);
1021         lp->mii.advertising = my_ad_caps;
1022
1023         /*
1024          * Read the register back.       Without this, it appears that when
1025          * auto-negotiation is restarted, sometimes it isn't ready and
1026          * the link does not come up.
1027          */
1028         udelay(10);
1029         SMC_GET_PHY_MII_ADV(phyaddr, status);
1030
1031         DBG(SMC_DEBUG_MISC, "%s: phy caps=0x%04x\n", dev->name, my_phy_caps);
1032         DBG(SMC_DEBUG_MISC, "%s: phy advertised caps=0x%04x\n", dev->name, my_ad_caps);
1033
1034         /* Restart auto-negotiation process in order to advertise my caps */
1035         SMC_SET_PHY_BMCR(phyaddr, BMCR_ANENABLE | BMCR_ANRESTART);
1036
1037         smc911x_phy_check_media(dev, 1);
1038
1039 smc911x_phy_configure_exit:
1040         spin_unlock_irqrestore(&lp->lock, flags);
1041         lp->work_pending = 0;
1042 }
1043
1044 /*
1045  * smc911x_phy_interrupt
1046  *
1047  * Purpose:  Handle interrupts relating to PHY register 18. This is
1048  *       called from the "hard" interrupt handler under our private spinlock.
1049  */
1050 static void smc911x_phy_interrupt(struct net_device *dev)
1051 {
1052         struct smc911x_local *lp = netdev_priv(dev);
1053         unsigned long ioaddr = dev->base_addr;
1054         int phyaddr = lp->mii.phy_id;
1055         int status;
1056
1057         DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
1058
1059         if (lp->phy_type == 0)
1060                 return;
1061
1062         smc911x_phy_check_media(dev, 0);
1063         /* read to clear status bits */
1064         SMC_GET_PHY_INT_SRC(phyaddr,status);
1065         DBG(SMC_DEBUG_MISC, "%s: PHY interrupt status 0x%04x\n",
1066                 dev->name, status & 0xffff);
1067         DBG(SMC_DEBUG_MISC, "%s: AFC_CFG 0x%08x\n",
1068                 dev->name, SMC_GET_AFC_CFG());
1069 }
1070
1071 /*--- END PHY CONTROL AND CONFIGURATION-------------------------------------*/
1072
1073 /*
1074  * This is the main routine of the driver, to handle the device when
1075  * it needs some attention.
1076  */
1077 static irqreturn_t smc911x_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1078 {
1079         struct net_device *dev = dev_id;
1080         unsigned long ioaddr = dev->base_addr;
1081         struct smc911x_local *lp = netdev_priv(dev);
1082         unsigned int status, mask, timeout;
1083         unsigned int rx_overrun=0, cr, pkts;
1084         unsigned long flags;
1085
1086         DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
1087
1088         spin_lock_irqsave(&lp->lock, flags);
1089
1090         /* Spurious interrupt check */
1091         if ((SMC_GET_IRQ_CFG() & (INT_CFG_IRQ_INT_ | INT_CFG_IRQ_EN_)) !=
1092                 (INT_CFG_IRQ_INT_ | INT_CFG_IRQ_EN_)) {
1093                 return IRQ_NONE;
1094         }
1095
1096         mask = SMC_GET_INT_EN();
1097         SMC_SET_INT_EN(0);
1098
1099         /* set a timeout value, so I don't stay here forever */
1100         timeout = 8;
1101
1102
1103         do {
1104                 status = SMC_GET_INT();
1105
1106                 DBG(SMC_DEBUG_MISC, "%s: INT 0x%08x MASK 0x%08x OUTSIDE MASK 0x%08x\n",
1107                         dev->name, status, mask, status & ~mask);
1108
1109                 status &= mask;
1110                 if (!status)
1111                         break;
1112
1113                 /* Handle SW interrupt condition */
1114                 if (status & INT_STS_SW_INT_) {
1115                         SMC_ACK_INT(INT_STS_SW_INT_);
1116                         mask &= ~INT_EN_SW_INT_EN_;
1117                 }
1118                 /* Handle various error conditions */
1119                 if (status & INT_STS_RXE_) {
1120                         SMC_ACK_INT(INT_STS_RXE_);
1121                         lp->stats.rx_errors++;
1122                 }
1123                 if (status & INT_STS_RXDFH_INT_) {
1124                         SMC_ACK_INT(INT_STS_RXDFH_INT_);
1125                         lp->stats.rx_dropped+=SMC_GET_RX_DROP();
1126                  }
1127                 /* Undocumented interrupt-what is the right thing to do here? */
1128                 if (status & INT_STS_RXDF_INT_) {
1129                         SMC_ACK_INT(INT_STS_RXDF_INT_);
1130                 }
1131
1132                 /* Rx Data FIFO exceeds set level */
1133                 if (status & INT_STS_RDFL_) {
1134                         if (IS_REV_A(lp->revision)) {
1135                                 rx_overrun=1;
1136                                 SMC_GET_MAC_CR(cr);
1137                                 cr &= ~MAC_CR_RXEN_;
1138                                 SMC_SET_MAC_CR(cr);
1139                                 DBG(SMC_DEBUG_RX, "%s: RX overrun\n", dev->name);
1140                                 lp->stats.rx_errors++;
1141                                 lp->stats.rx_fifo_errors++;
1142                         }
1143                         SMC_ACK_INT(INT_STS_RDFL_);
1144                 }
1145                 if (status & INT_STS_RDFO_) {
1146                         if (!IS_REV_A(lp->revision)) {
1147                                 SMC_GET_MAC_CR(cr);
1148                                 cr &= ~MAC_CR_RXEN_;
1149                                 SMC_SET_MAC_CR(cr);
1150                                 rx_overrun=1;
1151                                 DBG(SMC_DEBUG_RX, "%s: RX overrun\n", dev->name);
1152                                 lp->stats.rx_errors++;
1153                                 lp->stats.rx_fifo_errors++;
1154                         }
1155                         SMC_ACK_INT(INT_STS_RDFO_);
1156                 }
1157                 /* Handle receive condition */
1158                 if ((status & INT_STS_RSFL_) || rx_overrun) {
1159                         unsigned int fifo;
1160                         DBG(SMC_DEBUG_RX, "%s: RX irq\n", dev->name);
1161                         fifo = SMC_GET_RX_FIFO_INF();
1162                         pkts = (fifo & RX_FIFO_INF_RXSUSED_) >> 16;
1163                         DBG(SMC_DEBUG_RX, "%s: Rx FIFO pkts %d, bytes %d\n",
1164                                 dev->name, pkts, fifo & 0xFFFF );
1165                         if (pkts != 0) {
1166 #ifdef SMC_USE_DMA
1167                                 unsigned int fifo;
1168                                 if (lp->rxdma_active){
1169                                         DBG(SMC_DEBUG_RX | SMC_DEBUG_DMA,
1170                                                 "%s: RX DMA active\n", dev->name);
1171                                         /* The DMA is already running so up the IRQ threshold */
1172                                         fifo = SMC_GET_FIFO_INT() & ~0xFF;
1173                                         fifo |= pkts & 0xFF;
1174                                         DBG(SMC_DEBUG_RX,
1175                                                 "%s: Setting RX stat FIFO threshold to %d\n",
1176                                                 dev->name, fifo & 0xff);
1177                                         SMC_SET_FIFO_INT(fifo);
1178                                 } else
1179 #endif
1180                                 smc911x_rcv(dev);
1181                         }
1182                         SMC_ACK_INT(INT_STS_RSFL_);
1183                 }
1184                 /* Handle transmit FIFO available */
1185                 if (status & INT_STS_TDFA_) {
1186                         DBG(SMC_DEBUG_TX, "%s: TX data FIFO space available irq\n", dev->name);
1187                         SMC_SET_FIFO_TDA(0xFF);
1188                         lp->tx_throttle = 0;
1189 #ifdef SMC_USE_DMA
1190                         if (!lp->txdma_active)
1191 #endif
1192                                 netif_wake_queue(dev);
1193                         SMC_ACK_INT(INT_STS_TDFA_);
1194                 }
1195                 /* Handle transmit done condition */
1196 #if 1
1197                 if (status & (INT_STS_TSFL_ | INT_STS_GPT_INT_)) {
1198                         DBG(SMC_DEBUG_TX | SMC_DEBUG_MISC,
1199                                 "%s: Tx stat FIFO limit (%d) /GPT irq\n",
1200                                 dev->name, (SMC_GET_FIFO_INT() & 0x00ff0000) >> 16);
1201                         smc911x_tx(dev);
1202                         SMC_SET_GPT_CFG(GPT_CFG_TIMER_EN_ | 10000);
1203                         SMC_ACK_INT(INT_STS_TSFL_);
1204                         SMC_ACK_INT(INT_STS_TSFL_ | INT_STS_GPT_INT_);
1205                 }
1206 #else
1207                 if (status & INT_STS_TSFL_) {
1208                         DBG(SMC_DEBUG_TX, "%s: TX status FIFO limit (%d) irq \n", dev->name, );
1209                         smc911x_tx(dev);
1210                         SMC_ACK_INT(INT_STS_TSFL_);
1211                 }
1212
1213                 if (status & INT_STS_GPT_INT_) {
1214                         DBG(SMC_DEBUG_RX, "%s: IRQ_CFG 0x%08x FIFO_INT 0x%08x RX_CFG 0x%08x\n",
1215                                 dev->name,
1216                                 SMC_GET_IRQ_CFG(),
1217                                 SMC_GET_FIFO_INT(),
1218                                 SMC_GET_RX_CFG());
1219                         DBG(SMC_DEBUG_RX, "%s: Rx Stat FIFO Used 0x%02x "
1220                                 "Data FIFO Used 0x%04x Stat FIFO 0x%08x\n",
1221                                 dev->name,
1222                                 (SMC_GET_RX_FIFO_INF() & 0x00ff0000) >> 16,
1223                                 SMC_GET_RX_FIFO_INF() & 0xffff,
1224                                 SMC_GET_RX_STS_FIFO_PEEK());
1225                         SMC_SET_GPT_CFG(GPT_CFG_TIMER_EN_ | 10000);
1226                         SMC_ACK_INT(INT_STS_GPT_INT_);
1227                 }
1228 #endif
1229
1230                 /* Handle PHY interupt condition */
1231                 if (status & INT_STS_PHY_INT_) {
1232                         DBG(SMC_DEBUG_MISC, "%s: PHY irq\n", dev->name);
1233                         smc911x_phy_interrupt(dev);
1234                         SMC_ACK_INT(INT_STS_PHY_INT_);
1235                 }
1236         } while (--timeout);
1237
1238         /* restore mask state */
1239         SMC_SET_INT_EN(mask);
1240
1241         DBG(SMC_DEBUG_MISC, "%s: Interrupt done (%d loops)\n",
1242                 dev->name, 8-timeout);
1243
1244         spin_unlock_irqrestore(&lp->lock, flags);
1245
1246         DBG(3, "%s: Interrupt done (%d loops)\n", dev->name, 8-timeout);
1247
1248         return IRQ_HANDLED;
1249 }
1250
1251 #ifdef SMC_USE_DMA
1252 static void
1253 smc911x_tx_dma_irq(int dma, void *data, struct pt_regs *regs)
1254 {
1255         struct net_device *dev = (struct net_device *)data;
1256         struct smc911x_local *lp = netdev_priv(dev);
1257         struct sk_buff *skb = lp->current_tx_skb;
1258         unsigned long flags;
1259
1260         DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
1261
1262         DBG(SMC_DEBUG_TX | SMC_DEBUG_DMA, "%s: TX DMA irq handler\n", dev->name);
1263         /* Clear the DMA interrupt sources */
1264         SMC_DMA_ACK_IRQ(dev, dma);
1265         BUG_ON(skb == NULL);
1266         dma_unmap_single(NULL, tx_dmabuf, tx_dmalen, DMA_TO_DEVICE);
1267         dev->trans_start = jiffies;
1268         dev_kfree_skb_irq(skb);
1269         lp->current_tx_skb = NULL;
1270         if (lp->pending_tx_skb != NULL)
1271                 smc911x_hardware_send_pkt(dev);
1272         else {
1273                 DBG(SMC_DEBUG_TX | SMC_DEBUG_DMA,
1274                         "%s: No pending Tx packets. DMA disabled\n", dev->name);
1275                 spin_lock_irqsave(&lp->lock, flags);
1276                 lp->txdma_active = 0;
1277                 if (!lp->tx_throttle) {
1278                         netif_wake_queue(dev);
1279                 }
1280                 spin_unlock_irqrestore(&lp->lock, flags);
1281         }
1282
1283         DBG(SMC_DEBUG_TX | SMC_DEBUG_DMA,
1284                 "%s: TX DMA irq completed\n", dev->name);
1285 }
1286 static void
1287 smc911x_rx_dma_irq(int dma, void *data, struct pt_regs *regs)
1288 {
1289         struct net_device *dev = (struct net_device *)data;
1290         unsigned long ioaddr = dev->base_addr;
1291         struct smc911x_local *lp = netdev_priv(dev);
1292         struct sk_buff *skb = lp->current_rx_skb;
1293         unsigned long flags;
1294         unsigned int pkts;
1295
1296         DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
1297         DBG(SMC_DEBUG_RX | SMC_DEBUG_DMA, "%s: RX DMA irq handler\n", dev->name);
1298         /* Clear the DMA interrupt sources */
1299         SMC_DMA_ACK_IRQ(dev, dma);
1300         dma_unmap_single(NULL, rx_dmabuf, rx_dmalen, DMA_FROM_DEVICE);
1301         BUG_ON(skb == NULL);
1302         lp->current_rx_skb = NULL;
1303         PRINT_PKT(skb->data, skb->len);
1304         dev->last_rx = jiffies;
1305         skb->dev = dev;
1306         skb->protocol = eth_type_trans(skb, dev);
1307         netif_rx(skb);
1308         lp->stats.rx_packets++;
1309         lp->stats.rx_bytes += skb->len;
1310
1311         spin_lock_irqsave(&lp->lock, flags);
1312         pkts = (SMC_GET_RX_FIFO_INF() & RX_FIFO_INF_RXSUSED_) >> 16;
1313         if (pkts != 0) {
1314                 smc911x_rcv(dev);
1315         }else {
1316                 lp->rxdma_active = 0;
1317         }
1318         spin_unlock_irqrestore(&lp->lock, flags);
1319         DBG(SMC_DEBUG_RX | SMC_DEBUG_DMA,
1320                 "%s: RX DMA irq completed. DMA RX FIFO PKTS %d\n",
1321                 dev->name, pkts);
1322 }
1323 #endif   /* SMC_USE_DMA */
1324
1325 #ifdef CONFIG_NET_POLL_CONTROLLER
1326 /*
1327  * Polling receive - used by netconsole and other diagnostic tools
1328  * to allow network i/o with interrupts disabled.
1329  */
1330 static void smc911x_poll_controller(struct net_device *dev)
1331 {
1332         disable_irq(dev->irq);
1333         smc911x_interrupt(dev->irq, dev, NULL);
1334         enable_irq(dev->irq);
1335 }
1336 #endif
1337
1338 /* Our watchdog timed out. Called by the networking layer */
1339 static void smc911x_timeout(struct net_device *dev)
1340 {
1341         struct smc911x_local *lp = netdev_priv(dev);
1342         unsigned long ioaddr = dev->base_addr;
1343         int status, mask;
1344         unsigned long flags;
1345
1346         DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
1347
1348         spin_lock_irqsave(&lp->lock, flags);
1349         status = SMC_GET_INT();
1350         mask = SMC_GET_INT_EN();
1351         spin_unlock_irqrestore(&lp->lock, flags);
1352         DBG(SMC_DEBUG_MISC, "%s: INT 0x%02x MASK 0x%02x \n",
1353                 dev->name, status, mask);
1354
1355         /* Dump the current TX FIFO contents and restart */
1356         mask = SMC_GET_TX_CFG();
1357         SMC_SET_TX_CFG(mask | TX_CFG_TXS_DUMP_ | TX_CFG_TXD_DUMP_);
1358         /*
1359          * Reconfiguring the PHY doesn't seem like a bad idea here, but
1360          * smc911x_phy_configure() calls msleep() which calls schedule_timeout()
1361          * which calls schedule().       Hence we use a work queue.
1362          */
1363         if (lp->phy_type != 0) {
1364                 if (schedule_work(&lp->phy_configure)) {
1365                         lp->work_pending = 1;
1366                 }
1367         }
1368
1369         /* We can accept TX packets again */
1370         dev->trans_start = jiffies;
1371         netif_wake_queue(dev);
1372 }
1373
1374 /*
1375  * This routine will, depending on the values passed to it,
1376  * either make it accept multicast packets, go into
1377  * promiscuous mode (for TCPDUMP and cousins) or accept
1378  * a select set of multicast packets
1379  */
1380 static void smc911x_set_multicast_list(struct net_device *dev)
1381 {
1382         struct smc911x_local *lp = netdev_priv(dev);
1383         unsigned long ioaddr = dev->base_addr;
1384         unsigned int multicast_table[2];
1385         unsigned int mcr, update_multicast = 0;
1386         unsigned long flags;
1387         /* table for flipping the order of 5 bits */
1388         static const unsigned char invert5[] =
1389                 {0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0C, 0x1C,
1390                  0x02, 0x12, 0x0A, 0x1A, 0x06, 0x16, 0x0E, 0x1E,
1391                  0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0D, 0x1D,
1392                  0x03, 0x13, 0x0B, 0x1B, 0x07, 0x17, 0x0F, 0x1F};
1393
1394
1395         DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
1396
1397         spin_lock_irqsave(&lp->lock, flags);
1398         SMC_GET_MAC_CR(mcr);
1399         spin_unlock_irqrestore(&lp->lock, flags);
1400
1401         if (dev->flags & IFF_PROMISC) {
1402
1403                 DBG(SMC_DEBUG_MISC, "%s: RCR_PRMS\n", dev->name);
1404                 mcr |= MAC_CR_PRMS_;
1405         }
1406         /*
1407          * Here, I am setting this to accept all multicast packets.
1408          * I don't need to zero the multicast table, because the flag is
1409          * checked before the table is
1410          */
1411         else if (dev->flags & IFF_ALLMULTI || dev->mc_count > 16) {
1412                 DBG(SMC_DEBUG_MISC, "%s: RCR_ALMUL\n", dev->name);
1413                 mcr |= MAC_CR_MCPAS_;
1414         }
1415
1416         /*
1417          * This sets the internal hardware table to filter out unwanted
1418          * multicast packets before they take up memory.
1419          *
1420          * The SMC chip uses a hash table where the high 6 bits of the CRC of
1421          * address are the offset into the table.       If that bit is 1, then the
1422          * multicast packet is accepted.  Otherwise, it's dropped silently.
1423          *
1424          * To use the 6 bits as an offset into the table, the high 1 bit is
1425          * the number of the 32 bit register, while the low 5 bits are the bit
1426          * within that register.
1427          */
1428         else if (dev->mc_count)  {
1429                 int i;
1430                 struct dev_mc_list *cur_addr;
1431
1432                 /* Set the Hash perfec mode */
1433                 mcr |= MAC_CR_HPFILT_;
1434
1435                 /* start with a table of all zeros: reject all */
1436                 memset(multicast_table, 0, sizeof(multicast_table));
1437
1438                 cur_addr = dev->mc_list;
1439                 for (i = 0; i < dev->mc_count; i++, cur_addr = cur_addr->next) {
1440                         int position;
1441
1442                         /* do we have a pointer here? */
1443                         if (!cur_addr)
1444                                 break;
1445                         /* make sure this is a multicast address -
1446                                 shouldn't this be a given if we have it here ? */
1447                         if (!(*cur_addr->dmi_addr & 1))
1448                                  continue;
1449
1450                         /* only use the low order bits */
1451                         position = crc32_le(~0, cur_addr->dmi_addr, 6) & 0x3f;
1452
1453                         /* do some messy swapping to put the bit in the right spot */
1454                         multicast_table[invert5[position&0x1F]&0x1] |=
1455                                 (1<<invert5[(position>>1)&0x1F]);
1456                 }
1457
1458                 /* be sure I get rid of flags I might have set */
1459                 mcr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
1460
1461                 /* now, the table can be loaded into the chipset */
1462                 update_multicast = 1;
1463         } else   {
1464                 DBG(SMC_DEBUG_MISC, "%s: ~(MAC_CR_PRMS_|MAC_CR_MCPAS_)\n",
1465                         dev->name);
1466                 mcr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
1467
1468                 /*
1469                  * since I'm disabling all multicast entirely, I need to
1470                  * clear the multicast list
1471                  */
1472                 memset(multicast_table, 0, sizeof(multicast_table));
1473                 update_multicast = 1;
1474         }
1475
1476         spin_lock_irqsave(&lp->lock, flags);
1477         SMC_SET_MAC_CR(mcr);
1478         if (update_multicast) {
1479                 DBG(SMC_DEBUG_MISC,
1480                         "%s: update mcast hash table 0x%08x 0x%08x\n",
1481                         dev->name, multicast_table[0], multicast_table[1]);
1482                 SMC_SET_HASHL(multicast_table[0]);
1483                 SMC_SET_HASHH(multicast_table[1]);
1484         }
1485         spin_unlock_irqrestore(&lp->lock, flags);
1486 }
1487
1488
1489 /*
1490  * Open and Initialize the board
1491  *
1492  * Set up everything, reset the card, etc..
1493  */
1494 static int
1495 smc911x_open(struct net_device *dev)
1496 {
1497         DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
1498
1499         /*
1500          * Check that the address is valid.  If its not, refuse
1501          * to bring the device up.       The user must specify an
1502          * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
1503          */
1504         if (!is_valid_ether_addr(dev->dev_addr)) {
1505                 PRINTK("%s: no valid ethernet hw addr\n", __FUNCTION__);
1506                 return -EINVAL;
1507         }
1508
1509         /* reset the hardware */
1510         smc911x_reset(dev);
1511
1512         /* Configure the PHY, initialize the link state */
1513         smc911x_phy_configure(dev);
1514
1515         /* Turn on Tx + Rx */
1516         smc911x_enable(dev);
1517
1518         netif_start_queue(dev);
1519
1520         return 0;
1521 }
1522
1523 /*
1524  * smc911x_close
1525  *
1526  * this makes the board clean up everything that it can
1527  * and not talk to the outside world.    Caused by
1528  * an 'ifconfig ethX down'
1529  */
1530 static int smc911x_close(struct net_device *dev)
1531 {
1532         struct smc911x_local *lp = netdev_priv(dev);
1533
1534         DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
1535
1536         netif_stop_queue(dev);
1537         netif_carrier_off(dev);
1538
1539         /* clear everything */
1540         smc911x_shutdown(dev);
1541
1542         if (lp->phy_type != 0) {
1543                 /* We need to ensure that no calls to
1544                  * smc911x_phy_configure are pending.
1545
1546                  * flush_scheduled_work() cannot be called because we
1547                  * are running with the netlink semaphore held (from
1548                  * devinet_ioctl()) and the pending work queue
1549                  * contains linkwatch_event() (scheduled by
1550                  * netif_carrier_off() above). linkwatch_event() also
1551                  * wants the netlink semaphore.
1552                  */
1553                 while (lp->work_pending)
1554                         schedule();
1555                 smc911x_phy_powerdown(dev, lp->mii.phy_id);
1556         }
1557
1558         if (lp->pending_tx_skb) {
1559                 dev_kfree_skb(lp->pending_tx_skb);
1560                 lp->pending_tx_skb = NULL;
1561         }
1562
1563         return 0;
1564 }
1565
1566 /*
1567  * Get the current statistics.
1568  * This may be called with the card open or closed.
1569  */
1570 static struct net_device_stats *smc911x_query_statistics(struct net_device *dev)
1571 {
1572         struct smc911x_local *lp = netdev_priv(dev);
1573         DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
1574
1575
1576         return &lp->stats;
1577 }
1578
1579 /*
1580  * Ethtool support
1581  */
1582 static int
1583 smc911x_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1584 {
1585         struct smc911x_local *lp = netdev_priv(dev);
1586         unsigned long ioaddr = dev->base_addr;
1587         int ret, status;
1588         unsigned long flags;
1589
1590         DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
1591         cmd->maxtxpkt = 1;
1592         cmd->maxrxpkt = 1;
1593
1594         if (lp->phy_type != 0) {
1595                 spin_lock_irqsave(&lp->lock, flags);
1596                 ret = mii_ethtool_gset(&lp->mii, cmd);
1597                 spin_unlock_irqrestore(&lp->lock, flags);
1598         } else {
1599                 cmd->supported = SUPPORTED_10baseT_Half |
1600                                 SUPPORTED_10baseT_Full |
1601                                 SUPPORTED_TP | SUPPORTED_AUI;
1602
1603                 if (lp->ctl_rspeed == 10)
1604                         cmd->speed = SPEED_10;
1605                 else if (lp->ctl_rspeed == 100)
1606                         cmd->speed = SPEED_100;
1607
1608                 cmd->autoneg = AUTONEG_DISABLE;
1609                 if (lp->mii.phy_id==1)
1610                         cmd->transceiver = XCVR_INTERNAL;
1611                 else
1612                         cmd->transceiver = XCVR_EXTERNAL;
1613                 cmd->port = 0;
1614                 SMC_GET_PHY_SPECIAL(lp->mii.phy_id, status);
1615                 cmd->duplex =
1616                         (status & (PHY_SPECIAL_SPD_10FULL_ | PHY_SPECIAL_SPD_100FULL_)) ?
1617                                 DUPLEX_FULL : DUPLEX_HALF;
1618                 ret = 0;
1619         }
1620
1621         return ret;
1622 }
1623
1624 static int
1625 smc911x_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1626 {
1627         struct smc911x_local *lp = netdev_priv(dev);
1628         int ret;
1629         unsigned long flags;
1630
1631         if (lp->phy_type != 0) {
1632                 spin_lock_irqsave(&lp->lock, flags);
1633                 ret = mii_ethtool_sset(&lp->mii, cmd);
1634                 spin_unlock_irqrestore(&lp->lock, flags);
1635         } else {
1636                 if (cmd->autoneg != AUTONEG_DISABLE ||
1637                         cmd->speed != SPEED_10 ||
1638                         (cmd->duplex != DUPLEX_HALF && cmd->duplex != DUPLEX_FULL) ||
1639                         (cmd->port != PORT_TP && cmd->port != PORT_AUI))
1640                         return -EINVAL;
1641
1642                 lp->ctl_rfduplx = cmd->duplex == DUPLEX_FULL;
1643
1644                 ret = 0;
1645         }
1646
1647         return ret;
1648 }
1649
1650 static void
1651 smc911x_ethtool_getdrvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1652 {
1653         strncpy(info->driver, CARDNAME, sizeof(info->driver));
1654         strncpy(info->version, version, sizeof(info->version));
1655         strncpy(info->bus_info, dev->class_dev.dev->bus_id, sizeof(info->bus_info));
1656 }
1657
1658 static int smc911x_ethtool_nwayreset(struct net_device *dev)
1659 {
1660         struct smc911x_local *lp = netdev_priv(dev);
1661         int ret = -EINVAL;
1662         unsigned long flags;
1663
1664         if (lp->phy_type != 0) {
1665                 spin_lock_irqsave(&lp->lock, flags);
1666                 ret = mii_nway_restart(&lp->mii);
1667                 spin_unlock_irqrestore(&lp->lock, flags);
1668         }
1669
1670         return ret;
1671 }
1672
1673 static u32 smc911x_ethtool_getmsglevel(struct net_device *dev)
1674 {
1675         struct smc911x_local *lp = netdev_priv(dev);
1676         return lp->msg_enable;
1677 }
1678
1679 static void smc911x_ethtool_setmsglevel(struct net_device *dev, u32 level)
1680 {
1681         struct smc911x_local *lp = netdev_priv(dev);
1682         lp->msg_enable = level;
1683 }
1684
1685 static int smc911x_ethtool_getregslen(struct net_device *dev)
1686 {
1687         /* System regs + MAC regs + PHY regs */
1688         return (((E2P_CMD - ID_REV)/4 + 1) +
1689                         (WUCSR - MAC_CR)+1 + 32) * sizeof(u32);
1690 }
1691
1692 static void smc911x_ethtool_getregs(struct net_device *dev,
1693                                                                                  struct ethtool_regs* regs, void *buf)
1694 {
1695         unsigned long ioaddr = dev->base_addr;
1696         struct smc911x_local *lp = netdev_priv(dev);
1697         unsigned long flags;
1698         u32 reg,i,j=0;
1699         u32 *data = (u32*)buf;
1700
1701         regs->version = lp->version;
1702         for(i=ID_REV;i<=E2P_CMD;i+=4) {
1703                 data[j++] = SMC_inl(ioaddr,i);
1704         }
1705         for(i=MAC_CR;i<=WUCSR;i++) {
1706                 spin_lock_irqsave(&lp->lock, flags);
1707                 SMC_GET_MAC_CSR(i, reg);
1708                 spin_unlock_irqrestore(&lp->lock, flags);
1709                 data[j++] = reg;
1710         }
1711         for(i=0;i<=31;i++) {
1712                 spin_lock_irqsave(&lp->lock, flags);
1713                 SMC_GET_MII(i, lp->mii.phy_id, reg);
1714                 spin_unlock_irqrestore(&lp->lock, flags);
1715                 data[j++] = reg & 0xFFFF;
1716         }
1717 }
1718
1719 static int smc911x_ethtool_wait_eeprom_ready(struct net_device *dev)
1720 {
1721         unsigned long ioaddr = dev->base_addr;
1722         unsigned int timeout;
1723         int e2p_cmd;
1724
1725         e2p_cmd = SMC_GET_E2P_CMD();
1726         for(timeout=10;(e2p_cmd & E2P_CMD_EPC_BUSY_) && timeout; timeout--) {
1727                 if (e2p_cmd & E2P_CMD_EPC_TIMEOUT_) {
1728                         PRINTK("%s: %s timeout waiting for EEPROM to respond\n",
1729                                 dev->name, __FUNCTION__);
1730                         return -EFAULT;
1731                 }
1732                 mdelay(1);
1733                 e2p_cmd = SMC_GET_E2P_CMD();
1734         }
1735         if (timeout == 0) {
1736                 PRINTK("%s: %s timeout waiting for EEPROM CMD not busy\n",
1737                         dev->name, __FUNCTION__);
1738                 return -ETIMEDOUT;
1739         }
1740         return 0;
1741 }
1742
1743 static inline int smc911x_ethtool_write_eeprom_cmd(struct net_device *dev,
1744                                                                                                         int cmd, int addr)
1745 {
1746         unsigned long ioaddr = dev->base_addr;
1747         int ret;
1748
1749         if ((ret = smc911x_ethtool_wait_eeprom_ready(dev))!=0)
1750                 return ret;
1751         SMC_SET_E2P_CMD(E2P_CMD_EPC_BUSY_ |
1752                 ((cmd) & (0x7<<28)) |
1753                 ((addr) & 0xFF));
1754         return 0;
1755 }
1756
1757 static inline int smc911x_ethtool_read_eeprom_byte(struct net_device *dev,
1758                                                                                                         u8 *data)
1759 {
1760         unsigned long ioaddr = dev->base_addr;
1761         int ret;
1762
1763         if ((ret = smc911x_ethtool_wait_eeprom_ready(dev))!=0)
1764                 return ret;
1765         *data = SMC_GET_E2P_DATA();
1766         return 0;
1767 }
1768
1769 static inline int smc911x_ethtool_write_eeprom_byte(struct net_device *dev,
1770                                                                                                          u8 data)
1771 {
1772         unsigned long ioaddr = dev->base_addr;
1773         int ret;
1774
1775         if ((ret = smc911x_ethtool_wait_eeprom_ready(dev))!=0)
1776                 return ret;
1777         SMC_SET_E2P_DATA(data);
1778         return 0;
1779 }
1780
1781 static int smc911x_ethtool_geteeprom(struct net_device *dev,
1782                                                                           struct ethtool_eeprom *eeprom, u8 *data)
1783 {
1784         u8 eebuf[SMC911X_EEPROM_LEN];
1785         int i, ret;
1786
1787         for(i=0;i<SMC911X_EEPROM_LEN;i++) {
1788                 if ((ret=smc911x_ethtool_write_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ_, i ))!=0)
1789                         return ret;
1790                 if ((ret=smc911x_ethtool_read_eeprom_byte(dev, &eebuf[i]))!=0)
1791                         return ret;
1792                 }
1793         memcpy(data, eebuf+eeprom->offset, eeprom->len);
1794         return 0;
1795 }
1796
1797 static int smc911x_ethtool_seteeprom(struct net_device *dev,
1798                                                                            struct ethtool_eeprom *eeprom, u8 *data)
1799 {
1800         int i, ret;
1801
1802         /* Enable erase */
1803         if ((ret=smc911x_ethtool_write_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN_, 0 ))!=0)
1804                 return ret;
1805         for(i=eeprom->offset;i<(eeprom->offset+eeprom->len);i++) {
1806                 /* erase byte */
1807                 if ((ret=smc911x_ethtool_write_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE_, i ))!=0)
1808                         return ret;
1809                 /* write byte */
1810                 if ((ret=smc911x_ethtool_write_eeprom_byte(dev, *data))!=0)
1811                          return ret;
1812                 if ((ret=smc911x_ethtool_write_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE_, i ))!=0)
1813                         return ret;
1814                 }
1815          return 0;
1816 }
1817
1818 static int smc911x_ethtool_geteeprom_len(struct net_device *dev)
1819 {
1820          return SMC911X_EEPROM_LEN;
1821 }
1822
1823 static struct ethtool_ops smc911x_ethtool_ops = {
1824         .get_settings    = smc911x_ethtool_getsettings,
1825         .set_settings    = smc911x_ethtool_setsettings,
1826         .get_drvinfo     = smc911x_ethtool_getdrvinfo,
1827         .get_msglevel    = smc911x_ethtool_getmsglevel,
1828         .set_msglevel    = smc911x_ethtool_setmsglevel,
1829         .nway_reset = smc911x_ethtool_nwayreset,
1830         .get_link        = ethtool_op_get_link,
1831         .get_regs_len    = smc911x_ethtool_getregslen,
1832         .get_regs        = smc911x_ethtool_getregs,
1833         .get_eeprom_len = smc911x_ethtool_geteeprom_len,
1834         .get_eeprom = smc911x_ethtool_geteeprom,
1835         .set_eeprom = smc911x_ethtool_seteeprom,
1836 };
1837
1838 /*
1839  * smc911x_findirq
1840  *
1841  * This routine has a simple purpose -- make the SMC chip generate an
1842  * interrupt, so an auto-detect routine can detect it, and find the IRQ,
1843  */
1844 static int __init smc911x_findirq(unsigned long ioaddr)
1845 {
1846         int timeout = 20;
1847         unsigned long cookie;
1848
1849         DBG(SMC_DEBUG_FUNC, "--> %s\n", __FUNCTION__);
1850
1851         cookie = probe_irq_on();
1852
1853         /*
1854          * Force a SW interrupt
1855          */
1856
1857         SMC_SET_INT_EN(INT_EN_SW_INT_EN_);
1858
1859         /*
1860          * Wait until positive that the interrupt has been generated
1861          */
1862         do {
1863                 int int_status;
1864                 udelay(10);
1865                 int_status = SMC_GET_INT_EN();
1866                 if (int_status & INT_EN_SW_INT_EN_)
1867                          break;         /* got the interrupt */
1868         } while (--timeout);
1869
1870         /*
1871          * there is really nothing that I can do here if timeout fails,
1872          * as autoirq_report will return a 0 anyway, which is what I
1873          * want in this case.    Plus, the clean up is needed in both
1874          * cases.
1875          */
1876
1877         /* and disable all interrupts again */
1878         SMC_SET_INT_EN(0);
1879
1880         /* and return what I found */
1881         return probe_irq_off(cookie);
1882 }
1883
1884 /*
1885  * Function: smc911x_probe(unsigned long ioaddr)
1886  *
1887  * Purpose:
1888  *       Tests to see if a given ioaddr points to an SMC911x chip.
1889  *       Returns a 0 on success
1890  *
1891  * Algorithm:
1892  *       (1) see if the endian word is OK
1893  *       (1) see if I recognize the chip ID in the appropriate register
1894  *
1895  * Here I do typical initialization tasks.
1896  *
1897  * o  Initialize the structure if needed
1898  * o  print out my vanity message if not done so already
1899  * o  print out what type of hardware is detected
1900  * o  print out the ethernet address
1901  * o  find the IRQ
1902  * o  set up my private data
1903  * o  configure the dev structure with my subroutines
1904  * o  actually GRAB the irq.
1905  * o  GRAB the region
1906  */
1907 static int __init smc911x_probe(struct net_device *dev, unsigned long ioaddr)
1908 {
1909         struct smc911x_local *lp = netdev_priv(dev);
1910         int i, retval;
1911         unsigned int val, chip_id, revision;
1912         const char *version_string;
1913
1914         DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
1915
1916         /* First, see if the endian word is recognized */
1917         val = SMC_GET_BYTE_TEST();
1918         DBG(SMC_DEBUG_MISC, "%s: endian probe returned 0x%04x\n", CARDNAME, val);
1919         if (val != 0x87654321) {
1920                 printk(KERN_ERR "Invalid chip endian 0x08%x\n",val);
1921                 retval = -ENODEV;
1922                 goto err_out;
1923         }
1924
1925         /*
1926          * check if the revision register is something that I
1927          * recognize.   These might need to be added to later,
1928          * as future revisions could be added.
1929          */
1930         chip_id = SMC_GET_PN();
1931         DBG(SMC_DEBUG_MISC, "%s: id probe returned 0x%04x\n", CARDNAME, chip_id);
1932         for(i=0;chip_ids[i].id != 0; i++) {
1933                 if (chip_ids[i].id == chip_id) break;
1934         }
1935         if (!chip_ids[i].id) {
1936                 printk(KERN_ERR "Unknown chip ID %04x\n", chip_id);
1937                 retval = -ENODEV;
1938                 goto err_out;
1939         }
1940         version_string = chip_ids[i].name;
1941
1942         revision = SMC_GET_REV();
1943         DBG(SMC_DEBUG_MISC, "%s: revision = 0x%04x\n", CARDNAME, revision);
1944
1945         /* At this point I'll assume that the chip is an SMC911x. */
1946         DBG(SMC_DEBUG_MISC, "%s: Found a %s\n", CARDNAME, chip_ids[i].name);
1947
1948         /* Validate the TX FIFO size requested */
1949         if ((tx_fifo_kb < 2) || (tx_fifo_kb > 14)) {
1950                 printk(KERN_ERR "Invalid TX FIFO size requested %d\n", tx_fifo_kb);
1951                 retval = -EINVAL;
1952                 goto err_out;
1953         }
1954
1955         /* fill in some of the fields */
1956         dev->base_addr = ioaddr;
1957         lp->version = chip_ids[i].id;
1958         lp->revision = revision;
1959         lp->tx_fifo_kb = tx_fifo_kb;
1960         /* Reverse calculate the RX FIFO size from the TX */
1961         lp->tx_fifo_size=(lp->tx_fifo_kb<<10) - 512;
1962         lp->rx_fifo_size= ((0x4000 - 512 - lp->tx_fifo_size) / 16) * 15;
1963
1964         /* Set the automatic flow control values */
1965         switch(lp->tx_fifo_kb) {
1966                 /*
1967                  *       AFC_HI is about ((Rx Data Fifo Size)*2/3)/64
1968                  *       AFC_LO is AFC_HI/2
1969                  *       BACK_DUR is about 5uS*(AFC_LO) rounded down
1970                  */
1971                 case 2:/* 13440 Rx Data Fifo Size */
1972                         lp->afc_cfg=0x008C46AF;break;
1973                 case 3:/* 12480 Rx Data Fifo Size */
1974                         lp->afc_cfg=0x0082419F;break;
1975                 case 4:/* 11520 Rx Data Fifo Size */
1976                         lp->afc_cfg=0x00783C9F;break;
1977                 case 5:/* 10560 Rx Data Fifo Size */
1978                         lp->afc_cfg=0x006E374F;break;
1979                 case 6:/* 9600 Rx Data Fifo Size */
1980                         lp->afc_cfg=0x0064328F;break;
1981                 case 7:/* 8640 Rx Data Fifo Size */
1982                         lp->afc_cfg=0x005A2D7F;break;
1983                 case 8:/* 7680 Rx Data Fifo Size */
1984                         lp->afc_cfg=0x0050287F;break;
1985                 case 9:/* 6720 Rx Data Fifo Size */
1986                         lp->afc_cfg=0x0046236F;break;
1987                 case 10:/* 5760 Rx Data Fifo Size */
1988                         lp->afc_cfg=0x003C1E6F;break;
1989                 case 11:/* 4800 Rx Data Fifo Size */
1990                         lp->afc_cfg=0x0032195F;break;
1991                 /*
1992                  *       AFC_HI is ~1520 bytes less than RX Data Fifo Size
1993                  *       AFC_LO is AFC_HI/2
1994                  *       BACK_DUR is about 5uS*(AFC_LO) rounded down
1995                  */
1996                 case 12:/* 3840 Rx Data Fifo Size */
1997                         lp->afc_cfg=0x0024124F;break;
1998                 case 13:/* 2880 Rx Data Fifo Size */
1999                         lp->afc_cfg=0x0015073F;break;
2000                 case 14:/* 1920 Rx Data Fifo Size */
2001                         lp->afc_cfg=0x0006032F;break;
2002                  default:
2003                          PRINTK("%s: ERROR -- no AFC_CFG setting found",
2004                                 dev->name);
2005                          break;
2006         }
2007
2008         DBG(SMC_DEBUG_MISC | SMC_DEBUG_TX | SMC_DEBUG_RX,
2009                 "%s: tx_fifo %d rx_fifo %d afc_cfg 0x%08x\n", CARDNAME,
2010                 lp->tx_fifo_size, lp->rx_fifo_size, lp->afc_cfg);
2011
2012         spin_lock_init(&lp->lock);
2013
2014         /* Get the MAC address */
2015         SMC_GET_MAC_ADDR(dev->dev_addr);
2016
2017         /* now, reset the chip, and put it into a known state */
2018         smc911x_reset(dev);
2019
2020         /*
2021          * If dev->irq is 0, then the device has to be banged on to see
2022          * what the IRQ is.
2023          *
2024          * Specifying an IRQ is done with the assumption that the user knows
2025          * what (s)he is doing.  No checking is done!!!!
2026          */
2027         if (dev->irq < 1) {
2028                 int trials;
2029
2030                 trials = 3;
2031                 while (trials--) {
2032                         dev->irq = smc911x_findirq(ioaddr);
2033                         if (dev->irq)
2034                                 break;
2035                         /* kick the card and try again */
2036                         smc911x_reset(dev);
2037                 }
2038         }
2039         if (dev->irq == 0) {
2040                 printk("%s: Couldn't autodetect your IRQ. Use irq=xx.\n",
2041                         dev->name);
2042                 retval = -ENODEV;
2043                 goto err_out;
2044         }
2045         dev->irq = irq_canonicalize(dev->irq);
2046
2047         /* Fill in the fields of the device structure with ethernet values. */
2048         ether_setup(dev);
2049
2050         dev->open = smc911x_open;
2051         dev->stop = smc911x_close;
2052         dev->hard_start_xmit = smc911x_hard_start_xmit;
2053         dev->tx_timeout = smc911x_timeout;
2054         dev->watchdog_timeo = msecs_to_jiffies(watchdog);
2055         dev->get_stats = smc911x_query_statistics;
2056         dev->set_multicast_list = smc911x_set_multicast_list;
2057         dev->ethtool_ops = &smc911x_ethtool_ops;
2058 #ifdef CONFIG_NET_POLL_CONTROLLER
2059         dev->poll_controller = smc911x_poll_controller;
2060 #endif
2061
2062         INIT_WORK(&lp->phy_configure, smc911x_phy_configure, dev);
2063         lp->mii.phy_id_mask = 0x1f;
2064         lp->mii.reg_num_mask = 0x1f;
2065         lp->mii.force_media = 0;
2066         lp->mii.full_duplex = 0;
2067         lp->mii.dev = dev;
2068         lp->mii.mdio_read = smc911x_phy_read;
2069         lp->mii.mdio_write = smc911x_phy_write;
2070
2071         /*
2072          * Locate the phy, if any.
2073          */
2074         smc911x_phy_detect(dev);
2075
2076         /* Set default parameters */
2077         lp->msg_enable = NETIF_MSG_LINK;
2078         lp->ctl_rfduplx = 1;
2079         lp->ctl_rspeed = 100;
2080
2081         /* Grab the IRQ */
2082         retval = request_irq(dev->irq, &smc911x_interrupt, IRQF_SHARED, dev->name, dev);
2083         if (retval)
2084                 goto err_out;
2085
2086         set_irq_type(dev->irq, IRQT_FALLING);
2087
2088 #ifdef SMC_USE_DMA
2089         lp->rxdma = SMC_DMA_REQUEST(dev, smc911x_rx_dma_irq);
2090         lp->txdma = SMC_DMA_REQUEST(dev, smc911x_tx_dma_irq);
2091         lp->rxdma_active = 0;
2092         lp->txdma_active = 0;
2093         dev->dma = lp->rxdma;
2094 #endif
2095
2096         retval = register_netdev(dev);
2097         if (retval == 0) {
2098                 /* now, print out the card info, in a short format.. */
2099                 printk("%s: %s (rev %d) at %#lx IRQ %d",
2100                         dev->name, version_string, lp->revision,
2101                         dev->base_addr, dev->irq);
2102
2103 #ifdef SMC_USE_DMA
2104                 if (lp->rxdma != -1)
2105                         printk(" RXDMA %d ", lp->rxdma);
2106
2107                 if (lp->txdma != -1)
2108                         printk("TXDMA %d", lp->txdma);
2109 #endif
2110                 printk("\n");
2111                 if (!is_valid_ether_addr(dev->dev_addr)) {
2112                         printk("%s: Invalid ethernet MAC address. Please "
2113                                         "set using ifconfig\n", dev->name);
2114                 } else {
2115                         /* Print the Ethernet address */
2116                         printk("%s: Ethernet addr: ", dev->name);
2117                         for (i = 0; i < 5; i++)
2118                                 printk("%2.2x:", dev->dev_addr[i]);
2119                         printk("%2.2x\n", dev->dev_addr[5]);
2120                 }
2121
2122                 if (lp->phy_type == 0) {
2123                         PRINTK("%s: No PHY found\n", dev->name);
2124                 } else if ((lp->phy_type & ~0xff) == LAN911X_INTERNAL_PHY_ID) {
2125                         PRINTK("%s: LAN911x Internal PHY\n", dev->name);
2126                 } else {
2127                         PRINTK("%s: External PHY 0x%08x\n", dev->name, lp->phy_type);
2128                 }
2129         }
2130
2131 err_out:
2132 #ifdef SMC_USE_DMA
2133         if (retval) {
2134                 if (lp->rxdma != -1) {
2135                         SMC_DMA_FREE(dev, lp->rxdma);
2136                 }
2137                 if (lp->txdma != -1) {
2138                         SMC_DMA_FREE(dev, lp->txdma);
2139                 }
2140         }
2141 #endif
2142         return retval;
2143 }
2144
2145 /*
2146  * smc911x_init(void)
2147  *
2148  *        Output:
2149  *       0 --> there is a device
2150  *       anything else, error
2151  */
2152 static int smc911x_drv_probe(struct platform_device *pdev)
2153 {
2154         struct net_device *ndev;
2155         struct resource *res;
2156         unsigned int *addr;
2157         int ret;
2158
2159         DBG(SMC_DEBUG_FUNC, "--> %s\n",  __FUNCTION__);
2160         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2161         if (!res) {
2162                 ret = -ENODEV;
2163                 goto out;
2164         }
2165
2166         /*
2167          * Request the regions.
2168          */
2169         if (!request_mem_region(res->start, SMC911X_IO_EXTENT, CARDNAME)) {
2170                  ret = -EBUSY;
2171                  goto out;
2172         }
2173
2174         ndev = alloc_etherdev(sizeof(struct smc911x_local));
2175         if (!ndev) {
2176                 printk("%s: could not allocate device.\n", CARDNAME);
2177                 ret = -ENOMEM;
2178                 goto release_1;
2179         }
2180         SET_MODULE_OWNER(ndev);
2181         SET_NETDEV_DEV(ndev, &pdev->dev);
2182
2183         ndev->dma = (unsigned char)-1;
2184         ndev->irq = platform_get_irq(pdev, 0);
2185
2186         addr = ioremap(res->start, SMC911X_IO_EXTENT);
2187         if (!addr) {
2188                 ret = -ENOMEM;
2189                 goto release_both;
2190         }
2191
2192         platform_set_drvdata(pdev, ndev);
2193         ret = smc911x_probe(ndev, (unsigned long)addr);
2194         if (ret != 0) {
2195                 platform_set_drvdata(pdev, NULL);
2196                 iounmap(addr);
2197 release_both:
2198                 free_netdev(ndev);
2199 release_1:
2200                 release_mem_region(res->start, SMC911X_IO_EXTENT);
2201 out:
2202                 printk("%s: not found (%d).\n", CARDNAME, ret);
2203         }
2204 #ifdef SMC_USE_DMA
2205         else {
2206                 struct smc911x_local *lp = netdev_priv(ndev);
2207                 lp->physaddr = res->start;
2208                 lp->dev = &pdev->dev;
2209         }
2210 #endif
2211
2212         return ret;
2213 }
2214
2215 static int smc911x_drv_remove(struct platform_device *pdev)
2216 {
2217         struct net_device *ndev = platform_get_drvdata(pdev);
2218         struct resource *res;
2219
2220         DBG(SMC_DEBUG_FUNC, "--> %s\n", __FUNCTION__);
2221         platform_set_drvdata(pdev, NULL);
2222
2223         unregister_netdev(ndev);
2224
2225         free_irq(ndev->irq, ndev);
2226
2227 #ifdef SMC_USE_DMA
2228         {
2229                 struct smc911x_local *lp = netdev_priv(ndev);
2230                 if (lp->rxdma != -1) {
2231                         SMC_DMA_FREE(dev, lp->rxdma);
2232                 }
2233                 if (lp->txdma != -1) {
2234                         SMC_DMA_FREE(dev, lp->txdma);
2235                 }
2236         }
2237 #endif
2238         iounmap((void *)ndev->base_addr);
2239         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2240         release_mem_region(res->start, SMC911X_IO_EXTENT);
2241
2242         free_netdev(ndev);
2243         return 0;
2244 }
2245
2246 static int smc911x_drv_suspend(struct platform_device *dev, pm_message_t state)
2247 {
2248         struct net_device *ndev = platform_get_drvdata(dev);
2249         unsigned long ioaddr = ndev->base_addr;
2250
2251         DBG(SMC_DEBUG_FUNC, "--> %s\n", __FUNCTION__);
2252         if (ndev) {
2253                 if (netif_running(ndev)) {
2254                         netif_device_detach(ndev);
2255                         smc911x_shutdown(ndev);
2256 #if POWER_DOWN
2257                         /* Set D2 - Energy detect only setting */
2258                         SMC_SET_PMT_CTRL(2<<12);
2259 #endif
2260                 }
2261         }
2262         return 0;
2263 }
2264
2265 static int smc911x_drv_resume(struct platform_device *dev)
2266 {
2267         struct net_device *ndev = platform_get_drvdata(dev);
2268
2269         DBG(SMC_DEBUG_FUNC, "--> %s\n", __FUNCTION__);
2270         if (ndev) {
2271                 struct smc911x_local *lp = netdev_priv(ndev);
2272
2273                 if (netif_running(ndev)) {
2274                         smc911x_reset(ndev);
2275                         smc911x_enable(ndev);
2276                         if (lp->phy_type != 0)
2277                                 smc911x_phy_configure(ndev);
2278                         netif_device_attach(ndev);
2279                 }
2280         }
2281         return 0;
2282 }
2283
2284 static struct platform_driver smc911x_driver = {
2285         .probe           = smc911x_drv_probe,
2286         .remove  = smc911x_drv_remove,
2287         .suspend         = smc911x_drv_suspend,
2288         .resume  = smc911x_drv_resume,
2289         .driver  = {
2290                 .name    = CARDNAME,
2291         },
2292 };
2293
2294 static int __init smc911x_init(void)
2295 {
2296         return platform_driver_register(&smc911x_driver);
2297 }
2298
2299 static void __exit smc911x_cleanup(void)
2300 {
2301         platform_driver_unregister(&smc911x_driver);
2302 }
2303
2304 module_init(smc911x_init);
2305 module_exit(smc911x_cleanup);