ca3fe29dd48b67d9faf923153876b23b5713fec8
[linux-flexiantxendom0-3.2.10.git] / drivers / net / wan / lmc / lmc_main.c
1  /*
2   * Copyright (c) 1997-2000 LAN Media Corporation (LMC)
3   * All rights reserved.  www.lanmedia.com
4   *
5   * This code is written by:
6   * Andrew Stanley-Jones (asj@cban.com)
7   * Rob Braun (bbraun@vix.com),
8   * Michael Graff (explorer@vix.com) and
9   * Matt Thomas (matt@3am-software.com).
10   *
11   * With Help By:
12   * David Boggs
13   * Ron Crane
14   * Alan Cox
15   *
16   * This software may be used and distributed according to the terms
17   * of the GNU General Public License version 2, incorporated herein by reference.
18   *
19   * Driver for the LanMedia LMC5200, LMC5245, LMC1000, LMC1200 cards.
20   *
21   * To control link specific options lmcctl is required.
22   * It can be obtained from ftp.lanmedia.com.
23   *
24   * Linux driver notes:
25   * Linux uses the device struct lmc_private to pass private information
26   * arround.
27   *
28   * The initialization portion of this driver (the lmc_reset() and the
29   * lmc_dec_reset() functions, as well as the led controls and the
30   * lmc_initcsrs() functions.
31   *
32   * The watchdog function runs every second and checks to see if
33   * we still have link, and that the timing source is what we expected
34   * it to be.  If link is lost, the interface is marked down, and
35   * we no longer can transmit.
36   *
37   */
38
39 /* $Id: lmc_main.c,v 1.36 2000/04/11 05:25:25 asj Exp $ */
40
41 #include <linux/kernel.h>
42 #include <linux/module.h>
43 #include <linux/string.h>
44 #include <linux/timer.h>
45 #include <linux/ptrace.h>
46 #include <linux/errno.h>
47 #include <linux/ioport.h>
48 #include <linux/slab.h>
49 #include <linux/interrupt.h>
50 #include <linux/pci.h>
51 #include <linux/delay.h>
52 #include <linux/init.h>
53 #include <linux/in.h>
54 #include <linux/if_arp.h>
55 #include <linux/netdevice.h>
56 #include <linux/etherdevice.h>
57 #include <linux/skbuff.h>
58 #include <linux/inet.h>
59
60 #include <net/syncppp.h>
61
62 #include <asm/processor.h>             /* Processor type for cache alignment. */
63 #include <asm/bitops.h>
64 #include <asm/io.h>
65 #include <asm/dma.h>
66 #include <asm/uaccess.h>
67 //#include <asm/spinlock.h>
68
69 #define DRIVER_MAJOR_VERSION     1
70 #define DRIVER_MINOR_VERSION    34
71 #define DRIVER_SUB_VERSION       0
72
73 #define DRIVER_VERSION  ((DRIVER_MAJOR_VERSION << 8) + DRIVER_MINOR_VERSION)
74
75 #include "lmc.h"
76 #include "lmc_var.h"
77 #include "lmc_ioctl.h"
78 #include "lmc_debug.h"
79 #include "lmc_proto.h"
80
81
82 static int Lmc_Count = 0;
83 static struct net_device *Lmc_root_dev = NULL;
84 static u8 cards_found = 0;
85
86 static int lmc_first_load = 0;
87
88 int LMC_PKT_BUF_SZ = 1542;
89
90 #ifdef MODULE
91 static struct pci_device_id lmc_pci_tbl[] __devinitdata = {
92     { 0x1011, 0x009, 0x1379, PCI_ANY_ID, 0, 0, 0},
93     { 0, }
94 };
95
96 MODULE_DEVICE_TABLE(pci, lmc_pci_tbl);
97
98 MODULE_LICENSE("GPL");
99 #endif
100
101
102 int lmc_probe_fake(struct net_device *dev);
103 static struct net_device *lmc_probe1(struct net_device *dev, unsigned long ioaddr, unsigned int irq,
104                                  int chip_id, int subdevice, int board_idx);
105 static int lmc_start_xmit(struct sk_buff *skb, struct net_device *dev);
106 static int lmc_start_xmit(struct sk_buff *skb, struct net_device *dev);
107 static int lmc_rx (struct net_device *dev);
108 static int lmc_open(struct net_device *dev);
109 static int lmc_close(struct net_device *dev);
110 static struct net_device_stats *lmc_get_stats(struct net_device *dev);
111 static irqreturn_t lmc_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
112 static int lmc_set_config(struct net_device *dev, struct ifmap *map);
113 static void lmc_initcsrs(lmc_softc_t * const sc, lmc_csrptr_t csr_base, size_t csr_size);
114 static void lmc_softreset(lmc_softc_t * const);
115 static void lmc_running_reset(struct net_device *dev);
116 static int lmc_ifdown(struct net_device * const);
117 static void lmc_watchdog(unsigned long data);
118 static int lmc_init(struct net_device * const);
119 static void lmc_reset(lmc_softc_t * const sc);
120 static void lmc_dec_reset(lmc_softc_t * const sc);
121 static void lmc_driver_timeout(struct net_device *dev);
122 int lmc_setup(void);
123
124
125 /*
126  * linux reserves 16 device specific IOCTLs.  We call them
127  * LMCIOC* to control various bits of our world.
128  */
129 int lmc_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd) /*fold00*/
130 {
131     lmc_softc_t *sc;
132     lmc_ctl_t ctl;
133     int ret;
134     u_int16_t regVal;
135     unsigned long flags;
136
137     struct sppp *sp;
138
139     ret = -EOPNOTSUPP;
140
141     sc = dev->priv;
142
143     lmc_trace(dev, "lmc_ioctl in");
144
145     /*
146      * Most functions mess with the structure
147      * Disable interrupts while we do the polling
148      */
149     spin_lock_irqsave(&sc->lmc_lock, flags);
150
151     switch (cmd) {
152         /*
153          * Return current driver state.  Since we keep this up
154          * To date internally, just copy this out to the user.
155          */
156     case LMCIOCGINFO: /*fold01*/
157         if (copy_to_user(ifr->ifr_data, &sc->ictl, sizeof (lmc_ctl_t)))
158             return -EFAULT;
159         ret = 0;
160         break;
161
162     case LMCIOCSINFO: /*fold01*/
163         sp = &((struct ppp_device *) dev)->sppp;
164         if (!capable(CAP_NET_ADMIN)) {
165             ret = -EPERM;
166             break;
167         }
168
169         if(dev->flags & IFF_UP){
170             ret = -EBUSY;
171             break;
172         }
173
174         if (copy_from_user(&ctl, ifr->ifr_data, sizeof (lmc_ctl_t)))
175             return -EFAULT;
176
177         sc->lmc_media->set_status (sc, &ctl);
178
179         if(ctl.crc_length != sc->ictl.crc_length) {
180             sc->lmc_media->set_crc_length(sc, ctl.crc_length);
181             if (sc->ictl.crc_length == LMC_CTL_CRC_LENGTH_16)
182                 sc->TxDescriptControlInit |=  LMC_TDES_ADD_CRC_DISABLE;
183             else
184                 sc->TxDescriptControlInit &= ~LMC_TDES_ADD_CRC_DISABLE;
185         }
186
187         if (ctl.keepalive_onoff == LMC_CTL_OFF)
188             sp->pp_flags &= ~PP_KEEPALIVE;      /* Turn off */
189         else
190             sp->pp_flags |= PP_KEEPALIVE;       /* Turn on */
191
192         ret = 0;
193         break;
194
195     case LMCIOCIFTYPE: /*fold01*/
196         {
197             u_int16_t   old_type = sc->if_type;
198             u_int16_t   new_type;
199
200             if (!capable(CAP_NET_ADMIN)) {
201                 ret = -EPERM;
202                 break;
203             }
204
205             if (copy_from_user(&new_type, ifr->ifr_data, sizeof(u_int16_t)))
206                 return -EFAULT;
207
208             
209             if (new_type == old_type)
210             {
211                 ret = 0 ;
212                 break;                          /* no change */
213             }
214             
215             lmc_proto_close(sc);
216             lmc_proto_detach(sc);
217
218             sc->if_type = new_type;
219 //            lmc_proto_init(sc);
220             lmc_proto_attach(sc);
221             lmc_proto_open(sc);
222
223             ret = 0 ;
224             break ;
225         }
226
227     case LMCIOCGETXINFO: /*fold01*/
228         sc->lmc_xinfo.Magic0 = 0xBEEFCAFE;
229
230         sc->lmc_xinfo.PciCardType = sc->lmc_cardtype;
231         sc->lmc_xinfo.PciSlotNumber = 0;
232         sc->lmc_xinfo.DriverMajorVersion = DRIVER_MAJOR_VERSION;
233         sc->lmc_xinfo.DriverMinorVersion = DRIVER_MINOR_VERSION;
234         sc->lmc_xinfo.DriverSubVersion = DRIVER_SUB_VERSION;
235         sc->lmc_xinfo.XilinxRevisionNumber =
236             lmc_mii_readreg (sc, 0, 3) & 0xf;
237         sc->lmc_xinfo.MaxFrameSize = LMC_PKT_BUF_SZ;
238         sc->lmc_xinfo.link_status = sc->lmc_media->get_link_status (sc);
239         sc->lmc_xinfo.mii_reg16 = lmc_mii_readreg (sc, 0, 16);
240
241         sc->lmc_xinfo.Magic1 = 0xDEADBEEF;
242
243         if (copy_to_user(ifr->ifr_data, &sc->lmc_xinfo,
244                          sizeof (struct lmc_xinfo)))
245             return -EFAULT;
246         ret = 0;
247
248         break;
249
250     case LMCIOCGETLMCSTATS: /*fold01*/
251         if (sc->lmc_cardtype == LMC_CARDTYPE_T1){
252             lmc_mii_writereg (sc, 0, 17, T1FRAMER_FERR_LSB);
253             sc->stats.framingBitErrorCount +=
254                 lmc_mii_readreg (sc, 0, 18) & 0xff;
255             lmc_mii_writereg (sc, 0, 17, T1FRAMER_FERR_MSB);
256             sc->stats.framingBitErrorCount +=
257                 (lmc_mii_readreg (sc, 0, 18) & 0xff) << 8;
258             lmc_mii_writereg (sc, 0, 17, T1FRAMER_LCV_LSB);
259             sc->stats.lineCodeViolationCount +=
260                 lmc_mii_readreg (sc, 0, 18) & 0xff;
261             lmc_mii_writereg (sc, 0, 17, T1FRAMER_LCV_MSB);
262             sc->stats.lineCodeViolationCount +=
263                 (lmc_mii_readreg (sc, 0, 18) & 0xff) << 8;
264             lmc_mii_writereg (sc, 0, 17, T1FRAMER_AERR);
265             regVal = lmc_mii_readreg (sc, 0, 18) & 0xff;
266
267             sc->stats.lossOfFrameCount +=
268                 (regVal & T1FRAMER_LOF_MASK) >> 4;
269             sc->stats.changeOfFrameAlignmentCount +=
270                 (regVal & T1FRAMER_COFA_MASK) >> 2;
271             sc->stats.severelyErroredFrameCount +=
272                 regVal & T1FRAMER_SEF_MASK;
273         }
274
275         if (copy_to_user(ifr->ifr_data, &sc->stats,
276                          sizeof (struct lmc_statistics)))
277             return -EFAULT;
278
279         ret = 0;
280         break;
281
282     case LMCIOCCLEARLMCSTATS: /*fold01*/
283         if (!capable(CAP_NET_ADMIN)){
284             ret = -EPERM;
285             break;
286         }
287
288         memset (&sc->stats, 0, sizeof (struct lmc_statistics));
289         sc->stats.check = STATCHECK;
290         sc->stats.version_size = (DRIVER_VERSION << 16) +
291             sizeof (struct lmc_statistics);
292         sc->stats.lmc_cardtype = sc->lmc_cardtype;
293         ret = 0;
294         break;
295
296     case LMCIOCSETCIRCUIT: /*fold01*/
297         if (!capable(CAP_NET_ADMIN)){
298             ret = -EPERM;
299             break;
300         }
301
302         if(dev->flags & IFF_UP){
303             ret = -EBUSY;
304             break;
305         }
306
307         if (copy_from_user(&ctl, ifr->ifr_data, sizeof (lmc_ctl_t)))
308             return -EFAULT;
309         sc->lmc_media->set_circuit_type(sc, ctl.circuit_type);
310         sc->ictl.circuit_type = ctl.circuit_type;
311         ret = 0;
312
313         break;
314
315     case LMCIOCRESET: /*fold01*/
316         if (!capable(CAP_NET_ADMIN)){
317             ret = -EPERM;
318             break;
319         }
320
321         /* Reset driver and bring back to current state */
322         printk (" REG16 before reset +%04x\n", lmc_mii_readreg (sc, 0, 16));
323         lmc_running_reset (dev);
324         printk (" REG16 after reset +%04x\n", lmc_mii_readreg (sc, 0, 16));
325
326         LMC_EVENT_LOG(LMC_EVENT_FORCEDRESET, LMC_CSR_READ (sc, csr_status), lmc_mii_readreg (sc, 0, 16));
327
328         ret = 0;
329         break;
330
331 #ifdef DEBUG
332     case LMCIOCDUMPEVENTLOG:
333         if (copy_to_user(ifr->ifr_data, &lmcEventLogIndex, sizeof (u32)))
334             return -EFAULT;
335         if (copy_to_user(ifr->ifr_data + sizeof (u32), lmcEventLogBuf, sizeof (lmcEventLogBuf)))
336             return -EFAULT;
337
338         ret = 0;
339         break;
340 #endif /* end ifdef _DBG_EVENTLOG */
341     case LMCIOCT1CONTROL: /*fold01*/
342         if (sc->lmc_cardtype != LMC_CARDTYPE_T1){
343             ret = -EOPNOTSUPP;
344             break;
345         }
346         break;
347     case LMCIOCXILINX: /*fold01*/
348         {
349             struct lmc_xilinx_control xc; /*fold02*/
350
351             if (!capable(CAP_NET_ADMIN)){
352                 ret = -EPERM;
353                 break;
354             }
355
356             /*
357              * Stop the xwitter whlie we restart the hardware
358              */
359             netif_stop_queue(dev);
360
361             if (copy_from_user(&xc, ifr->ifr_data, sizeof (struct lmc_xilinx_control)))
362                 return -EFAULT;
363             switch(xc.command){
364             case lmc_xilinx_reset: /*fold02*/
365                 {
366                     u16 mii;
367                     mii = lmc_mii_readreg (sc, 0, 16);
368
369                     /*
370                      * Make all of them 0 and make input
371                      */
372                     lmc_gpio_mkinput(sc, 0xff);
373
374                     /*
375                      * make the reset output
376                      */
377                     lmc_gpio_mkoutput(sc, LMC_GEP_RESET);
378
379                     /*
380                      * RESET low to force configuration.  This also forces
381                      * the transmitter clock to be internal, but we expect to reset
382                      * that later anyway.
383                      */
384
385                     sc->lmc_gpio &= ~LMC_GEP_RESET;
386                     LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
387
388
389                     /*
390                      * hold for more than 10 microseconds
391                      */
392                     udelay(50);
393
394                     sc->lmc_gpio |= LMC_GEP_RESET;
395                     LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
396
397
398                     /*
399                      * stop driving Xilinx-related signals
400                      */
401                     lmc_gpio_mkinput(sc, 0xff);
402
403                     /* Reset the frammer hardware */
404                     sc->lmc_media->set_link_status (sc, 1);
405                     sc->lmc_media->set_status (sc, NULL);
406 //                    lmc_softreset(sc);
407
408                     {
409                         int i;
410                         for(i = 0; i < 5; i++){
411                             lmc_led_on(sc, LMC_DS3_LED0);
412                             mdelay(100);
413                             lmc_led_off(sc, LMC_DS3_LED0);
414                             lmc_led_on(sc, LMC_DS3_LED1);
415                             mdelay(100);
416                             lmc_led_off(sc, LMC_DS3_LED1);
417                             lmc_led_on(sc, LMC_DS3_LED3);
418                             mdelay(100);
419                             lmc_led_off(sc, LMC_DS3_LED3);
420                             lmc_led_on(sc, LMC_DS3_LED2);
421                             mdelay(100);
422                             lmc_led_off(sc, LMC_DS3_LED2);
423                         }
424                     }
425                     
426                     
427
428                     ret = 0x0;
429
430                 }
431
432                 break;
433             case lmc_xilinx_load_prom: /*fold02*/
434                 {
435                     u16 mii;
436                     int timeout = 500000;
437                     mii = lmc_mii_readreg (sc, 0, 16);
438
439                     /*
440                      * Make all of them 0 and make input
441                      */
442                     lmc_gpio_mkinput(sc, 0xff);
443
444                     /*
445                      * make the reset output
446                      */
447                     lmc_gpio_mkoutput(sc,  LMC_GEP_DP | LMC_GEP_RESET);
448
449                     /*
450                      * RESET low to force configuration.  This also forces
451                      * the transmitter clock to be internal, but we expect to reset
452                      * that later anyway.
453                      */
454
455                     sc->lmc_gpio &= ~(LMC_GEP_RESET | LMC_GEP_DP);
456                     LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
457
458
459                     /*
460                      * hold for more than 10 microseconds
461                      */
462                     udelay(50);
463
464                     sc->lmc_gpio |= LMC_GEP_DP | LMC_GEP_RESET;
465                     LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
466
467                     /*
468                      * busy wait for the chip to reset
469                      */
470                     while( (LMC_CSR_READ(sc, csr_gp) & LMC_GEP_INIT) == 0 &&
471                            (timeout-- > 0))
472                         ;
473
474
475                     /*
476                      * stop driving Xilinx-related signals
477                      */
478                     lmc_gpio_mkinput(sc, 0xff);
479
480                     ret = 0x0;
481                     
482
483                     break;
484
485                 }
486
487             case lmc_xilinx_load: /*fold02*/
488                 {
489                     char *data;
490                     int pos;
491                     int timeout = 500000;
492
493                     if(xc.data == 0x0){
494                             ret = -EINVAL;
495                             break;
496                     }
497
498                     data = kmalloc(xc.len, GFP_KERNEL);
499                     if(data == 0x0){
500                             printk(KERN_WARNING "%s: Failed to allocate memory for copy\n", dev->name);
501                             ret = -ENOMEM;
502                             break;
503                     }
504                     
505                     if(copy_from_user(data, xc.data, xc.len))
506                     {
507                         kfree(data);
508                         ret = -ENOMEM;
509                         break;
510                     }
511
512                     printk("%s: Starting load of data Len: %d at 0x%p == 0x%p\n", dev->name, xc.len, xc.data, data);
513
514                     lmc_gpio_mkinput(sc, 0xff);
515
516                     /*
517                      * Clear the Xilinx and start prgramming from the DEC
518                      */
519
520                     /*
521                      * Set ouput as:
522                      * Reset: 0 (active)
523                      * DP:    0 (active)
524                      * Mode:  1
525                      *
526                      */
527                     sc->lmc_gpio = 0x00;
528                     sc->lmc_gpio &= ~LMC_GEP_DP;
529                     sc->lmc_gpio &= ~LMC_GEP_RESET;
530                     sc->lmc_gpio |=  LMC_GEP_MODE;
531                     LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
532
533                     lmc_gpio_mkoutput(sc, LMC_GEP_MODE | LMC_GEP_DP | LMC_GEP_RESET);
534
535                     /*
536                      * Wait at least 10 us 20 to be safe
537                      */
538                     udelay(50);
539
540                     /*
541                      * Clear reset and activate programming lines
542                      * Reset: Input
543                      * DP:    Input
544                      * Clock: Output
545                      * Data:  Output
546                      * Mode:  Output
547                      */
548                     lmc_gpio_mkinput(sc, LMC_GEP_DP | LMC_GEP_RESET);
549
550                     /*
551                      * Set LOAD, DATA, Clock to 1
552                      */
553                     sc->lmc_gpio = 0x00;
554                     sc->lmc_gpio |= LMC_GEP_MODE;
555                     sc->lmc_gpio |= LMC_GEP_DATA;
556                     sc->lmc_gpio |= LMC_GEP_CLK;
557                     LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
558                     
559                     lmc_gpio_mkoutput(sc, LMC_GEP_DATA | LMC_GEP_CLK | LMC_GEP_MODE );
560
561                     /*
562                      * busy wait for the chip to reset
563                      */
564                     while( (LMC_CSR_READ(sc, csr_gp) & LMC_GEP_INIT) == 0 &&
565                            (timeout-- > 0))
566                         ;
567
568                     printk(KERN_DEBUG "%s: Waited %d for the Xilinx to clear it's memory\n", dev->name, 500000-timeout);
569
570                     for(pos = 0; pos < xc.len; pos++){
571                         switch(data[pos]){
572                         case 0:
573                             sc->lmc_gpio &= ~LMC_GEP_DATA; /* Data is 0 */
574                             break;
575                         case 1:
576                             sc->lmc_gpio |= LMC_GEP_DATA; /* Data is 1 */
577                             break;
578                         default:
579                             printk(KERN_WARNING "%s Bad data in xilinx programming data at %d, got %d wanted 0 or 1\n", dev->name, pos, data[pos]);
580                             sc->lmc_gpio |= LMC_GEP_DATA; /* Assume it's 1 */
581                         }
582                         sc->lmc_gpio &= ~LMC_GEP_CLK; /* Clock to zero */
583                         sc->lmc_gpio |= LMC_GEP_MODE;
584                         LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
585                         udelay(1);
586                         
587                         sc->lmc_gpio |= LMC_GEP_CLK; /* Put the clack back to one */
588                         sc->lmc_gpio |= LMC_GEP_MODE;
589                         LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
590                         udelay(1);
591                     }
592                     if((LMC_CSR_READ(sc, csr_gp) & LMC_GEP_INIT) == 0){
593                         printk(KERN_WARNING "%s: Reprogramming FAILED. Needs to be reprogrammed. (corrupted data)\n", dev->name);
594                     }
595                     else if((LMC_CSR_READ(sc, csr_gp) & LMC_GEP_DP) == 0){
596                         printk(KERN_WARNING "%s: Reprogramming FAILED. Needs to be reprogrammed. (done)\n", dev->name);
597                     }
598                     else {
599                         printk(KERN_DEBUG "%s: Done reprogramming Xilinx, %d bits, good luck!\n", dev->name, pos);
600                     }
601
602                     lmc_gpio_mkinput(sc, 0xff);
603                     
604                     sc->lmc_miireg16 |= LMC_MII16_FIFO_RESET;
605                     lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
606
607                     sc->lmc_miireg16 &= ~LMC_MII16_FIFO_RESET;
608                     lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
609
610                     kfree(data);
611                     
612                     ret = 0;
613                     
614                     break;
615                 }
616             default: /*fold02*/
617                 ret = -EBADE;
618                 break;
619             }
620
621             netif_wake_queue(dev);
622             sc->lmc_txfull = 0;
623
624         }
625         break;
626     default: /*fold01*/
627         /* If we don't know what to do, give the protocol a shot. */
628         ret = lmc_proto_ioctl (sc, ifr, cmd);
629         break;
630     }
631
632     spin_unlock_irqrestore(&sc->lmc_lock, flags); /*fold01*/
633
634     lmc_trace(dev, "lmc_ioctl out");
635
636     return ret;
637 }
638
639
640 /* the watchdog process that cruises around */
641 static void lmc_watchdog (unsigned long data) /*fold00*/
642 {
643     struct net_device *dev = (struct net_device *) data;
644     lmc_softc_t *sc;
645     int link_status;
646     u_int32_t ticks;
647     unsigned long flags;
648
649     sc = dev->priv;
650
651     lmc_trace(dev, "lmc_watchdog in");
652
653     spin_lock_irqsave(&sc->lmc_lock, flags);
654
655     if(sc->check != 0xBEAFCAFE){
656         printk("LMC: Corrupt net_device stuct, breaking out\n");
657         spin_unlock_irqrestore(&sc->lmc_lock, flags);
658         return;
659     }
660
661
662     /* Make sure the tx jabber and rx watchdog are off,
663      * and the transmit and receive processes are running.
664      */
665
666     LMC_CSR_WRITE (sc, csr_15, 0x00000011);
667     sc->lmc_cmdmode |= TULIP_CMD_TXRUN | TULIP_CMD_RXRUN;
668     LMC_CSR_WRITE (sc, csr_command, sc->lmc_cmdmode);
669
670     if (sc->lmc_ok == 0)
671         goto kick_timer;
672
673     LMC_EVENT_LOG(LMC_EVENT_WATCHDOG, LMC_CSR_READ (sc, csr_status), lmc_mii_readreg (sc, 0, 16));
674
675     /* --- begin time out check -----------------------------------
676      * check for a transmit interrupt timeout
677      * Has the packet xmt vs xmt serviced threshold been exceeded */
678     if (sc->lmc_taint_tx == sc->lastlmc_taint_tx &&
679         sc->stats.tx_packets > sc->lasttx_packets &&
680         sc->tx_TimeoutInd == 0)
681     {
682
683         /* wait for the watchdog to come around again */
684         sc->tx_TimeoutInd = 1;
685     }
686     else if (sc->lmc_taint_tx == sc->lastlmc_taint_tx &&
687              sc->stats.tx_packets > sc->lasttx_packets &&
688              sc->tx_TimeoutInd)
689     {
690
691         LMC_EVENT_LOG(LMC_EVENT_XMTINTTMO, LMC_CSR_READ (sc, csr_status), 0);
692
693         sc->tx_TimeoutDisplay = 1;
694         sc->stats.tx_TimeoutCnt++;
695
696         /* DEC chip is stuck, hit it with a RESET!!!! */
697         lmc_running_reset (dev);
698
699
700         /* look at receive & transmit process state to make sure they are running */
701         LMC_EVENT_LOG(LMC_EVENT_RESET1, LMC_CSR_READ (sc, csr_status), 0);
702
703         /* look at: DSR - 02  for Reg 16
704          *                  CTS - 08
705          *                  DCD - 10
706          *                  RI  - 20
707          * for Reg 17
708          */
709         LMC_EVENT_LOG(LMC_EVENT_RESET2, lmc_mii_readreg (sc, 0, 16), lmc_mii_readreg (sc, 0, 17));
710
711         /* reset the transmit timeout detection flag */
712         sc->tx_TimeoutInd = 0;
713         sc->lastlmc_taint_tx = sc->lmc_taint_tx;
714         sc->lasttx_packets = sc->stats.tx_packets;
715     }
716     else
717     {
718         sc->tx_TimeoutInd = 0;
719         sc->lastlmc_taint_tx = sc->lmc_taint_tx;
720         sc->lasttx_packets = sc->stats.tx_packets;
721     }
722
723     /* --- end time out check ----------------------------------- */
724
725
726     link_status = sc->lmc_media->get_link_status (sc);
727
728     /*
729      * hardware level link lost, but the interface is marked as up.
730      * Mark it as down.
731      */
732     if ((link_status == 0) && (sc->last_link_status != 0)) {
733         printk(KERN_WARNING "%s: hardware/physical link down\n", dev->name);
734         sc->last_link_status = 0;
735         /* lmc_reset (sc); Why reset??? The link can go down ok */
736
737         /* Inform the world that link has been lost */
738         dev->flags &= ~IFF_RUNNING;
739     }
740
741     /*
742      * hardware link is up, but the interface is marked as down.
743      * Bring it back up again.
744      */
745      if (link_status != 0 && sc->last_link_status == 0) {
746          printk(KERN_WARNING "%s: hardware/physical link up\n", dev->name);
747          sc->last_link_status = 1;
748          /* lmc_reset (sc); Again why reset??? */
749
750          /* Inform the world that link protocol is back up. */
751          dev->flags |= IFF_RUNNING;
752
753          /* Now we have to tell the syncppp that we had an outage
754           * and that it should deal.  Calling sppp_reopen here
755           * should do the trick, but we may have to call sppp_close
756           * when the link goes down, and call sppp_open here.
757           * Subject to more testing.
758           * --bbraun
759           */
760
761          lmc_proto_reopen(sc);
762
763      }
764
765     /* Call media specific watchdog functions */
766     sc->lmc_media->watchdog(sc);
767
768     /*
769      * Poke the transmitter to make sure it
770      * never stops, even if we run out of mem
771      */
772     LMC_CSR_WRITE(sc, csr_rxpoll, 0);
773
774     /*
775      * Check for code that failed
776      * and try and fix it as appropriate
777      */
778     if(sc->failed_ring == 1){
779         /*
780          * Failed to setup the recv/xmit rin
781          * Try again
782          */
783         sc->failed_ring = 0;
784         lmc_softreset(sc);
785     }
786     if(sc->failed_recv_alloc == 1){
787         /*
788          * We failed to alloc mem in the
789          * interrupt handler, go through the rings
790          * and rebuild them
791          */
792         sc->failed_recv_alloc = 0;
793         lmc_softreset(sc);
794     }
795
796
797     /*
798      * remember the timer value
799      */
800 kick_timer:
801
802     ticks = LMC_CSR_READ (sc, csr_gp_timer);
803     LMC_CSR_WRITE (sc, csr_gp_timer, 0xffffffffUL);
804     sc->ictl.ticks = 0x0000ffff - (ticks & 0x0000ffff);
805
806     /*
807      * restart this timer.
808      */
809     sc->timer.expires = jiffies + (HZ);
810     add_timer (&sc->timer);
811
812     spin_unlock_irqrestore(&sc->lmc_lock, flags);
813
814     lmc_trace(dev, "lmc_watchdog out");
815
816 }
817
818 static int lmc_init(struct net_device * const dev) /*fold00*/
819 {
820     lmc_trace(dev, "lmc_init in");
821     lmc_trace(dev, "lmc_init out");
822         
823     return 0;
824 }
825
826 /* This initializes each card from lmc_probe() */
827 static struct net_device *lmc_probe1 (struct net_device *dev, unsigned long ioaddr, unsigned int irq, /*fold00*/
828                                   int chip_id, int subdevice, int board_idx)
829 {
830     lmc_softc_t *sc = NULL;
831     u_int16_t AdapModelNum;
832
833     /*
834      * Allocate our own device structure
835      */
836
837     dev = kmalloc (sizeof (struct net_device)+8, GFP_KERNEL);
838     if (dev == NULL){
839         printk (KERN_ERR "lmc: kmalloc for device failed\n");
840         return NULL;
841     }
842     memset (dev, 0, sizeof (struct net_device));
843
844 #ifndef GCOM
845     /*
846      *  Switch to common hdlc%d naming. We name by type not by vendor
847      */
848     
849     dev_alloc_name(dev, "hdlc%d");
850 #else
851     /*
852      * GCOM uses LMC vendor name so that clients can know which card
853      * to attach to.
854      */
855     dev_alloc_name(dev, "lmc%d");
856 #endif
857
858     lmc_trace(dev, "lmc_probe1 in");
859     
860     Lmc_Count++;
861
862     if(lmc_first_load == 0){
863         printk(KERN_INFO "Lan Media Corporation WAN Driver Version %d.%d.%d\n",DRIVER_MAJOR_VERSION, DRIVER_MINOR_VERSION,DRIVER_SUB_VERSION);
864         lmc_first_load = 1;
865     }
866     
867     /*
868      * Allocate space for the private data structure
869      */
870
871     sc = kmalloc (sizeof (lmc_softc_t), GFP_KERNEL);
872     if (sc == NULL) {
873         printk (KERN_WARNING "%s: Cannot allocate memory for device state\n",
874                 dev->name);
875         return (NULL);
876     }
877     memset (sc, 0, sizeof (lmc_softc_t));
878     dev->priv = sc;
879     sc->lmc_device = dev;
880     sc->name = dev->name;
881
882     /* Initialize the sppp layer */
883     /* An ioctl can cause a subsequent detach for raw frame interface */
884     sc->if_type = LMC_PPP;
885     sc->check = 0xBEAFCAFE;
886     dev->base_addr = ioaddr;
887     dev->irq = irq;
888     /*
889      * This will get the protocol layer ready and do any 1 time init's
890      * Must have a valid sc and dev structure
891      */
892     lmc_proto_init(sc);
893
894     lmc_proto_attach(sc);
895
896     /* Just fill in the entries for the device */
897
898     dev->init = lmc_init;
899     dev->type = ARPHRD_HDLC;
900     dev->hard_start_xmit = lmc_start_xmit;
901     dev->open = lmc_open;
902     dev->stop = lmc_close;
903     dev->get_stats = lmc_get_stats;
904     dev->do_ioctl = lmc_ioctl;
905     dev->set_config = lmc_set_config;
906     dev->tx_timeout = lmc_driver_timeout;
907     dev->watchdog_timeo = (HZ); /* 1 second */
908     
909     /*
910      * Why were we changing this???
911      dev->tx_queue_len = 100;
912      */
913
914     /* Init the spin lock so can call it latter */
915
916     spin_lock_init(&sc->lmc_lock);
917
918     printk ("%s: detected at %lx, irq %d\n", dev->name, ioaddr, dev->irq);
919
920     if (register_netdev (dev) != 0) {
921         printk (KERN_ERR "%s: register_netdev failed.\n", dev->name);
922         lmc_proto_detach(sc);
923         kfree (dev->priv);
924         kfree (dev);
925         return NULL;
926     }
927
928     /*
929      * Request the region of registers we need, so that
930      * later on, no one else will take our card away from
931      * us.
932      */
933     request_region (ioaddr, LMC_REG_RANGE, dev->name);
934
935     sc->lmc_cardtype = LMC_CARDTYPE_UNKNOWN;
936     sc->lmc_timing = LMC_CTL_CLOCK_SOURCE_EXT;
937
938     switch (subdevice) {
939     case PCI_PRODUCT_LMC_HSSI:
940         printk ("%s: LMC HSSI\n", dev->name);
941         sc->lmc_cardtype = LMC_CARDTYPE_HSSI;
942         sc->lmc_media = &lmc_hssi_media;
943         break;
944     case PCI_PRODUCT_LMC_DS3:
945         printk ("%s: LMC DS3\n", dev->name);
946         sc->lmc_cardtype = LMC_CARDTYPE_DS3;
947         sc->lmc_media = &lmc_ds3_media;
948         break;
949     case PCI_PRODUCT_LMC_SSI:
950         printk ("%s: LMC SSI\n", dev->name);
951         sc->lmc_cardtype = LMC_CARDTYPE_SSI;
952         sc->lmc_media = &lmc_ssi_media;
953         break;
954     case PCI_PRODUCT_LMC_T1:
955         printk ("%s: LMC T1\n", dev->name);
956         sc->lmc_cardtype = LMC_CARDTYPE_T1;
957         sc->lmc_media = &lmc_t1_media;
958         break;
959     default:
960         printk (KERN_WARNING "%s: LMC UNKOWN CARD!\n", dev->name);
961         break;
962     }
963
964     lmc_initcsrs (sc, dev->base_addr, 8);
965
966     lmc_gpio_mkinput (sc, 0xff);
967     sc->lmc_gpio = 0;           /* drive no signals yet */
968
969     sc->lmc_media->defaults (sc);
970
971     sc->lmc_media->set_link_status (sc, LMC_LINK_UP);
972
973     /* verify that the PCI Sub System ID matches the Adapter Model number
974      * from the MII register
975      */
976     AdapModelNum = (lmc_mii_readreg (sc, 0, 3) & 0x3f0) >> 4;
977
978     if ((AdapModelNum == LMC_ADAP_T1
979          && subdevice == PCI_PRODUCT_LMC_T1) ||         /* detect LMC1200 */
980         (AdapModelNum == LMC_ADAP_SSI
981          && subdevice == PCI_PRODUCT_LMC_SSI) ||        /* detect LMC1000 */
982         (AdapModelNum == LMC_ADAP_DS3
983          && subdevice == PCI_PRODUCT_LMC_DS3) ||        /* detect LMC5245 */
984         (AdapModelNum == LMC_ADAP_HSSI
985          && subdevice == PCI_PRODUCT_LMC_HSSI))
986     {                           /* detect LMC5200 */
987
988     }
989     else {
990         printk ("%s: Model number (%d) miscompare for PCI Subsystem ID = 0x%04x\n",
991                 dev->name, AdapModelNum, subdevice);
992 //        return (NULL);
993     }
994     /*
995      * reset clock
996      */
997     LMC_CSR_WRITE (sc, csr_gp_timer, 0xFFFFFFFFUL);
998
999     sc->board_idx = board_idx;
1000
1001     memset (&sc->stats, 0, sizeof (struct lmc_statistics));
1002
1003     sc->stats.check = STATCHECK;
1004     sc->stats.version_size = (DRIVER_VERSION << 16) +
1005         sizeof (struct lmc_statistics);
1006     sc->stats.lmc_cardtype = sc->lmc_cardtype;
1007
1008     sc->lmc_ok = 0;
1009     sc->last_link_status = 0;
1010
1011     lmc_trace(dev, "lmc_probe1 out");
1012
1013     return dev;
1014 }
1015
1016
1017 /* This is the entry point.  This is what is called immediately. */
1018 /* This goes out and finds the card */
1019
1020 int lmc_probe_fake(struct net_device *dev) /*fold00*/
1021 {
1022     lmc_probe(NULL);
1023     /* Return 1 to unloaded bogus device */
1024     return 1;
1025 }
1026
1027 int lmc_probe (struct net_device *dev) /*fold00*/
1028 {
1029     int pci_index = 0;
1030     unsigned long pci_ioaddr;
1031     unsigned int pci_irq_line;
1032     u16 vendor, subvendor, device, subdevice;
1033     u32 foundaddr = 0;
1034     u8 intcf = 0;
1035     struct pci_dev *pdev = NULL;
1036
1037     /* Loop basically until we don't find anymore. */
1038     while ((pdev = pci_find_class (PCI_CLASS_NETWORK_ETHERNET << 8, pdev))) {
1039         if (pci_enable_device(pdev))
1040                 break;
1041
1042         vendor = pdev->vendor;
1043         device = pdev->device;
1044         pci_irq_line = pdev->irq;
1045         pci_ioaddr = pci_resource_start (pdev, 0);
1046         subvendor = pdev->subsystem_vendor;
1047         subdevice = pdev->subsystem_device;
1048
1049         pci_set_master (pdev);
1050
1051         /*
1052          * Make sure it's the correct card.  CHECK SUBVENDOR ID!
1053          * There are lots of tulip's out there.
1054          * Also check the region of registers we will soon be
1055          * poking, to make sure no one else has reserved them.
1056          * This prevents taking someone else's device.
1057          *
1058          * Check either the subvendor or the subdevice, some systems reverse
1059          * the setting in the bois, seems to be version and arch dependent?
1060          * Fix the two variables
1061          *
1062          */
1063         if (!(check_region (pci_ioaddr, LMC_REG_RANGE)) &&
1064             (vendor == CORRECT_VENDOR_ID) &&
1065             (device == CORRECT_DEV_ID) &&
1066             ((subvendor == PCI_VENDOR_LMC)  || (subdevice == PCI_VENDOR_LMC))){
1067             struct net_device *cur, *prev = NULL;
1068
1069             /* Fix the error, exchange the two values */
1070             if(subdevice == PCI_VENDOR_LMC){
1071                 subdevice = subvendor;
1072                 subvendor = PCI_VENDOR_LMC ;
1073             }
1074
1075             /* Make the call to actually setup this card */
1076             dev = lmc_probe1 (dev, pci_ioaddr, pci_irq_line,
1077                               device, subdevice, cards_found);
1078             if (dev == NULL) {
1079                 printk ("lmc_probe: lmc_probe1 failed\n");
1080                 goto lmc_probe_next_card;
1081             }
1082             /* insert the device into the chain of lmc devices */
1083             for (cur = Lmc_root_dev;
1084                  cur != NULL;
1085                  cur = ((lmc_softc_t *) cur->priv)->next_module) {
1086                 prev = cur;
1087             }
1088
1089             if (prev == NULL)
1090                 Lmc_root_dev = dev;
1091             else
1092                 ((lmc_softc_t *) prev->priv)->next_module = dev;
1093
1094             ((lmc_softc_t *) dev->priv)->next_module = NULL;
1095             /* end insert */
1096
1097             foundaddr = dev->base_addr;
1098
1099             cards_found++;
1100             intcf++;
1101         }
1102     lmc_probe_next_card:
1103         pci_index++;
1104     }
1105
1106     if (cards_found < 1)
1107         return -1;
1108
1109     return foundaddr;
1110 }
1111
1112 /* After this is called, packets can be sent.
1113  * Does not initialize the addresses
1114  */
1115 static int lmc_open (struct net_device *dev) /*fold00*/
1116 {
1117     lmc_softc_t *sc = dev->priv;
1118
1119     lmc_trace(dev, "lmc_open in");
1120
1121     lmc_led_on(sc, LMC_DS3_LED0);
1122
1123     lmc_dec_reset (sc);
1124     lmc_reset (sc);
1125
1126     LMC_EVENT_LOG(LMC_EVENT_RESET1, LMC_CSR_READ (sc, csr_status), 0);
1127     LMC_EVENT_LOG(LMC_EVENT_RESET2,
1128                   lmc_mii_readreg (sc, 0, 16),
1129                   lmc_mii_readreg (sc, 0, 17));
1130
1131
1132     if (sc->lmc_ok){
1133         lmc_trace(dev, "lmc_open lmc_ok out");
1134         return (0);
1135     }
1136
1137     lmc_softreset (sc);
1138
1139     /* Since we have to use PCI bus, this should work on x86,alpha,ppc */
1140     if (request_irq (dev->irq, &lmc_interrupt, SA_SHIRQ, dev->name, dev)){
1141         printk(KERN_WARNING "%s: could not get irq: %d\n", dev->name, dev->irq);
1142         lmc_trace(dev, "lmc_open irq failed out");
1143         return -EAGAIN;
1144     }
1145     sc->got_irq = 1;
1146
1147     /* Assert Terminal Active */
1148     sc->lmc_miireg16 |= LMC_MII16_LED_ALL;
1149     sc->lmc_media->set_link_status (sc, LMC_LINK_UP);
1150
1151     /*
1152      * reset to last state.
1153      */
1154     sc->lmc_media->set_status (sc, NULL);
1155
1156     /* setup default bits to be used in tulip_desc_t transmit descriptor
1157      * -baz */
1158     sc->TxDescriptControlInit = (
1159                                  LMC_TDES_INTERRUPT_ON_COMPLETION
1160                                  | LMC_TDES_FIRST_SEGMENT
1161                                  | LMC_TDES_LAST_SEGMENT
1162                                  | LMC_TDES_SECOND_ADDR_CHAINED
1163                                  | LMC_TDES_DISABLE_PADDING
1164                                 );
1165
1166     if (sc->ictl.crc_length == LMC_CTL_CRC_LENGTH_16) {
1167         /* disable 32 bit CRC generated by ASIC */
1168         sc->TxDescriptControlInit |= LMC_TDES_ADD_CRC_DISABLE;
1169     }
1170     sc->lmc_media->set_crc_length(sc, sc->ictl.crc_length);
1171     /* Acknoledge the Terminal Active and light LEDs */
1172
1173     /* dev->flags |= IFF_UP; */
1174
1175     lmc_proto_open(sc);
1176
1177     dev->do_ioctl = lmc_ioctl;
1178
1179
1180     netif_start_queue(dev);
1181     
1182     sc->stats.tx_tbusy0++ ;
1183
1184     MOD_INC_USE_COUNT;
1185
1186     /*
1187      * select what interrupts we want to get
1188      */
1189     sc->lmc_intrmask = 0;
1190     /* Should be using the default interrupt mask defined in the .h file. */
1191     sc->lmc_intrmask |= (TULIP_STS_NORMALINTR
1192                          | TULIP_STS_RXINTR
1193                          | TULIP_STS_TXINTR
1194                          | TULIP_STS_ABNRMLINTR
1195                          | TULIP_STS_SYSERROR
1196                          | TULIP_STS_TXSTOPPED
1197                          | TULIP_STS_TXUNDERFLOW
1198                          | TULIP_STS_RXSTOPPED
1199                          | TULIP_STS_RXNOBUF
1200                         );
1201     LMC_CSR_WRITE (sc, csr_intr, sc->lmc_intrmask);
1202
1203     sc->lmc_cmdmode |= TULIP_CMD_TXRUN;
1204     sc->lmc_cmdmode |= TULIP_CMD_RXRUN;
1205     LMC_CSR_WRITE (sc, csr_command, sc->lmc_cmdmode);
1206
1207     sc->lmc_ok = 1; /* Run watchdog */
1208
1209     /*
1210      * Set the if up now - pfb
1211      */
1212
1213     sc->last_link_status = 1;
1214
1215     /*
1216      * Setup a timer for the watchdog on probe, and start it running.
1217      * Since lmc_ok == 0, it will be a NOP for now.
1218      */
1219     init_timer (&sc->timer);
1220     sc->timer.expires = jiffies + HZ;
1221     sc->timer.data = (unsigned long) dev;
1222     sc->timer.function = &lmc_watchdog;
1223     add_timer (&sc->timer);
1224
1225     lmc_trace(dev, "lmc_open out");
1226
1227     return (0);
1228 }
1229
1230 /* Total reset to compensate for the AdTran DSU doing bad things
1231  *  under heavy load
1232  */
1233
1234 static void lmc_running_reset (struct net_device *dev) /*fold00*/
1235 {
1236
1237     lmc_softc_t *sc = (lmc_softc_t *) dev->priv;
1238
1239     lmc_trace(dev, "lmc_runnig_reset in");
1240
1241     /* stop interrupts */
1242     /* Clear the interrupt mask */
1243     LMC_CSR_WRITE (sc, csr_intr, 0x00000000);
1244
1245     lmc_dec_reset (sc);
1246     lmc_reset (sc);
1247     lmc_softreset (sc);
1248     /* sc->lmc_miireg16 |= LMC_MII16_LED_ALL; */
1249     sc->lmc_media->set_link_status (sc, 1);
1250     sc->lmc_media->set_status (sc, NULL);
1251
1252     //dev->flags |= IFF_RUNNING;
1253     
1254     netif_wake_queue(dev);
1255
1256     sc->lmc_txfull = 0;
1257     sc->stats.tx_tbusy0++ ;
1258
1259     sc->lmc_intrmask = TULIP_DEFAULT_INTR_MASK;
1260     LMC_CSR_WRITE (sc, csr_intr, sc->lmc_intrmask);
1261
1262     sc->lmc_cmdmode |= (TULIP_CMD_TXRUN | TULIP_CMD_RXRUN);
1263     LMC_CSR_WRITE (sc, csr_command, sc->lmc_cmdmode);
1264
1265     lmc_trace(dev, "lmc_runnin_reset_out");
1266 }
1267
1268
1269 /* This is what is called when you ifconfig down a device.
1270  * This disables the timer for the watchdog and keepalives,
1271  * and disables the irq for dev.
1272  */
1273 static int lmc_close (struct net_device *dev) /*fold00*/
1274 {
1275     /* not calling release_region() as we should */
1276     lmc_softc_t *sc;
1277
1278     lmc_trace(dev, "lmc_close in");
1279     
1280     sc = dev->priv;
1281     sc->lmc_ok = 0;
1282     sc->lmc_media->set_link_status (sc, 0);
1283     del_timer (&sc->timer);
1284     lmc_proto_close(sc);
1285     lmc_ifdown (dev);
1286
1287     lmc_trace(dev, "lmc_close out");
1288     
1289     return 0;
1290 }
1291
1292 /* Ends the transfer of packets */
1293 /* When the interface goes down, this is called */
1294 static int lmc_ifdown (struct net_device *dev) /*fold00*/
1295 {
1296     lmc_softc_t *sc = dev->priv;
1297     u32 csr6;
1298     int i;
1299
1300     lmc_trace(dev, "lmc_ifdown in");
1301     
1302     /* Don't let anything else go on right now */
1303     //    dev->start = 0;
1304     netif_stop_queue(dev);
1305     sc->stats.tx_tbusy1++ ;
1306
1307     /* stop interrupts */
1308     /* Clear the interrupt mask */
1309     LMC_CSR_WRITE (sc, csr_intr, 0x00000000);
1310
1311     /* Stop Tx and Rx on the chip */
1312     csr6 = LMC_CSR_READ (sc, csr_command);
1313     csr6 &= ~LMC_DEC_ST;                /* Turn off the Transmission bit */
1314     csr6 &= ~LMC_DEC_SR;                /* Turn off the Receive bit */
1315     LMC_CSR_WRITE (sc, csr_command, csr6);
1316
1317     dev->flags &= ~IFF_RUNNING;
1318
1319     sc->stats.rx_missed_errors +=
1320         LMC_CSR_READ (sc, csr_missed_frames) & 0xffff;
1321
1322     /* release the interrupt */
1323     if(sc->got_irq == 1){
1324         free_irq (dev->irq, dev);
1325         sc->got_irq = 0;
1326     }
1327
1328     /* free skbuffs in the Rx queue */
1329     for (i = 0; i < LMC_RXDESCS; i++)
1330     {
1331         struct sk_buff *skb = sc->lmc_rxq[i];
1332         sc->lmc_rxq[i] = 0;
1333         sc->lmc_rxring[i].status = 0;
1334         sc->lmc_rxring[i].length = 0;
1335         sc->lmc_rxring[i].buffer1 = 0xDEADBEEF;
1336         if (skb != NULL)
1337             dev_kfree_skb(skb);
1338         sc->lmc_rxq[i] = NULL;
1339     }
1340
1341     for (i = 0; i < LMC_TXDESCS; i++)
1342     {
1343         if (sc->lmc_txq[i] != NULL)
1344             dev_kfree_skb(sc->lmc_txq[i]);
1345         sc->lmc_txq[i] = NULL;
1346     }
1347
1348     lmc_led_off (sc, LMC_MII16_LED_ALL);
1349
1350     netif_wake_queue(dev);
1351     sc->stats.tx_tbusy0++ ;
1352
1353     lmc_trace(dev, "lmc_ifdown out");
1354
1355     MOD_DEC_USE_COUNT;
1356     return 0;
1357 }
1358
1359 /* Interrupt handling routine.  This will take an incoming packet, or clean
1360  * up after a trasmit.
1361  */
1362 static irqreturn_t lmc_interrupt (int irq, void *dev_instance, struct pt_regs *regs) /*fold00*/
1363 {
1364     struct net_device *dev = (struct net_device *) dev_instance;
1365     lmc_softc_t *sc;
1366     u32 csr;
1367     int i;
1368     s32 stat;
1369     unsigned int badtx;
1370     u32 firstcsr;
1371     int max_work = LMC_RXDESCS;
1372     int handled = 0;
1373
1374     lmc_trace(dev, "lmc_interrupt in");
1375
1376     sc = dev->priv;
1377     
1378     spin_lock(&sc->lmc_lock);
1379
1380     /*
1381      * Read the csr to find what interrupts we have (if any)
1382      */
1383     csr = LMC_CSR_READ (sc, csr_status);
1384
1385     /*
1386      * Make sure this is our interrupt
1387      */
1388     if ( ! (csr & sc->lmc_intrmask)) {
1389         goto lmc_int_fail_out;
1390     }
1391
1392     firstcsr = csr;
1393
1394     /* always go through this loop at least once */
1395     while (csr & sc->lmc_intrmask) {
1396         handled = 1;
1397
1398         /*
1399          * Clear interrupt bits, we handle all case below
1400          */
1401         LMC_CSR_WRITE (sc, csr_status, csr);
1402
1403         /*
1404          * One of
1405          *  - Transmit process timed out CSR5<1>
1406          *  - Transmit jabber timeout    CSR5<3>
1407          *  - Transmit underflow         CSR5<5>
1408          *  - Transmit Receiver buffer unavailable CSR5<7>
1409          *  - Receive process stopped    CSR5<8>
1410          *  - Receive watchdog timeout   CSR5<9>
1411          *  - Early transmit interrupt   CSR5<10>
1412          *
1413          * Is this really right? Should we do a running reset for jabber?
1414          * (being a WAN card and all)
1415          */
1416         if (csr & TULIP_STS_ABNRMLINTR){
1417             lmc_running_reset (dev);
1418             break;
1419         }
1420         
1421         if (csr & TULIP_STS_RXINTR){
1422             lmc_trace(dev, "rx interrupt");
1423             lmc_rx (dev);
1424             
1425         }
1426         if (csr & (TULIP_STS_TXINTR | TULIP_STS_TXNOBUF | TULIP_STS_TXSTOPPED)) {
1427
1428             int         n_compl = 0 ;
1429             /* reset the transmit timeout detection flag -baz */
1430             sc->stats.tx_NoCompleteCnt = 0;
1431
1432             badtx = sc->lmc_taint_tx;
1433             i = badtx % LMC_TXDESCS;
1434
1435             while ((badtx < sc->lmc_next_tx)) {
1436                 stat = sc->lmc_txring[i].status;
1437
1438                 LMC_EVENT_LOG (LMC_EVENT_XMTINT, stat,
1439                                                  sc->lmc_txring[i].length);
1440                 /*
1441                  * If bit 31 is 1 the tulip owns it break out of the loop
1442                  */
1443                 if (stat & 0x80000000)
1444                     break;
1445
1446                 n_compl++ ;             /* i.e., have an empty slot in ring */
1447                 /*
1448                  * If we have no skbuff or have cleared it
1449                  * Already continue to the next buffer
1450                  */
1451                 if (sc->lmc_txq[i] == NULL)
1452                     continue;
1453
1454                 /*
1455                  * Check the total error summary to look for any errors
1456                  */
1457                 if (stat & 0x8000) {
1458                     sc->stats.tx_errors++;
1459                     if (stat & 0x4104)
1460                         sc->stats.tx_aborted_errors++;
1461                     if (stat & 0x0C00)
1462                         sc->stats.tx_carrier_errors++;
1463                     if (stat & 0x0200)
1464                         sc->stats.tx_window_errors++;
1465                     if (stat & 0x0002)
1466                         sc->stats.tx_fifo_errors++;
1467                 }
1468                 else {
1469                     
1470                     sc->stats.tx_bytes += sc->lmc_txring[i].length & 0x7ff;
1471                     
1472                     sc->stats.tx_packets++;
1473                 }
1474                 
1475                 //                dev_kfree_skb(sc->lmc_txq[i]);
1476                 dev_kfree_skb_irq(sc->lmc_txq[i]);
1477                 sc->lmc_txq[i] = 0;
1478
1479                 badtx++;
1480                 i = badtx % LMC_TXDESCS;
1481             }
1482
1483             if (sc->lmc_next_tx - badtx > LMC_TXDESCS)
1484             {
1485                 printk ("%s: out of sync pointer\n", dev->name);
1486                 badtx += LMC_TXDESCS;
1487             }
1488             LMC_EVENT_LOG(LMC_EVENT_TBUSY0, n_compl, 0);
1489             sc->lmc_txfull = 0;
1490             netif_wake_queue(dev);
1491             sc->stats.tx_tbusy0++ ;
1492
1493
1494 #ifdef DEBUG
1495             sc->stats.dirtyTx = badtx;
1496             sc->stats.lmc_next_tx = sc->lmc_next_tx;
1497             sc->stats.lmc_txfull = sc->lmc_txfull;
1498 #endif
1499             sc->lmc_taint_tx = badtx;
1500
1501             /*
1502              * Why was there a break here???
1503              */
1504         }                       /* end handle transmit interrupt */
1505
1506         if (csr & TULIP_STS_SYSERROR) {
1507             u32 error;
1508             printk (KERN_WARNING "%s: system bus error csr: %#8.8x\n", dev->name, csr);
1509             error = csr>>23 & 0x7;
1510             switch(error){
1511             case 0x000:
1512                 printk(KERN_WARNING "%s: Parity Fault (bad)\n", dev->name);
1513                 break;
1514             case 0x001:
1515                 printk(KERN_WARNING "%s: Master Abort (naughty)\n", dev->name);
1516                 break;
1517             case 0x010:
1518                 printk(KERN_WARNING "%s: Target Abort (not so naughty)\n", dev->name);
1519                 break;
1520             default:
1521                 printk(KERN_WARNING "%s: This bus error code was supposed to be reserved!\n", dev->name);
1522             }
1523             lmc_dec_reset (sc);
1524             lmc_reset (sc);
1525             LMC_EVENT_LOG(LMC_EVENT_RESET1, LMC_CSR_READ (sc, csr_status), 0);
1526             LMC_EVENT_LOG(LMC_EVENT_RESET2,
1527                           lmc_mii_readreg (sc, 0, 16),
1528                           lmc_mii_readreg (sc, 0, 17));
1529
1530         }
1531
1532         
1533         if(max_work-- <= 0)
1534             break;
1535         
1536         /*
1537          * Get current csr status to make sure
1538          * we've cleared all interrupts
1539          */
1540         csr = LMC_CSR_READ (sc, csr_status);
1541     }                           /* end interrupt loop */
1542     LMC_EVENT_LOG(LMC_EVENT_INT, firstcsr, csr);
1543
1544 lmc_int_fail_out:
1545
1546     spin_unlock(&sc->lmc_lock);
1547
1548     lmc_trace(dev, "lmc_interrupt out");
1549     return IRQ_RETVAL(handled);
1550 }
1551
1552 static int lmc_start_xmit (struct sk_buff *skb, struct net_device *dev) /*fold00*/
1553 {
1554     lmc_softc_t *sc;
1555     u32 flag;
1556     int entry;
1557     int ret = 0;
1558     unsigned long flags;
1559
1560     lmc_trace(dev, "lmc_start_xmit in");
1561
1562     sc = dev->priv;
1563
1564     spin_lock_irqsave(&sc->lmc_lock, flags);
1565
1566     /* normal path, tbusy known to be zero */
1567
1568     entry = sc->lmc_next_tx % LMC_TXDESCS;
1569
1570     sc->lmc_txq[entry] = skb;
1571     sc->lmc_txring[entry].buffer1 = virt_to_bus (skb->data);
1572
1573     LMC_CONSOLE_LOG("xmit", skb->data, skb->len);
1574
1575 #ifndef GCOM
1576     /* If the queue is less than half full, don't interrupt */
1577     if (sc->lmc_next_tx - sc->lmc_taint_tx < LMC_TXDESCS / 2)
1578     {
1579         /* Do not interrupt on completion of this packet */
1580         flag = 0x60000000;
1581         netif_wake_queue(dev);
1582     }
1583     else if (sc->lmc_next_tx - sc->lmc_taint_tx == LMC_TXDESCS / 2)
1584     {
1585         /* This generates an interrupt on completion of this packet */
1586         flag = 0xe0000000;
1587         netif_wake_queue(dev);
1588     }
1589     else if (sc->lmc_next_tx - sc->lmc_taint_tx < LMC_TXDESCS - 1)
1590     {
1591         /* Do not interrupt on completion of this packet */
1592         flag = 0x60000000;
1593         netif_wake_queue(dev);
1594     }
1595     else
1596     {
1597         /* This generates an interrupt on completion of this packet */
1598         flag = 0xe0000000;
1599         sc->lmc_txfull = 1;
1600         netif_stop_queue(dev);
1601     }
1602 #else
1603     flag = LMC_TDES_INTERRUPT_ON_COMPLETION;
1604
1605     if (sc->lmc_next_tx - sc->lmc_taint_tx >= LMC_TXDESCS - 1)
1606     {                           /* ring full, go busy */
1607         sc->lmc_txfull = 1;
1608         netif_stop_queue(dev);
1609         sc->stats.tx_tbusy1++ ;
1610         LMC_EVENT_LOG(LMC_EVENT_TBUSY1, entry, 0);
1611     }
1612 #endif
1613
1614
1615     if (entry == LMC_TXDESCS - 1)       /* last descriptor in ring */
1616         flag |= LMC_TDES_END_OF_RING;   /* flag as such for Tulip */
1617
1618     /* don't pad small packets either */
1619     flag = sc->lmc_txring[entry].length = (skb->len) | flag |
1620                                                 sc->TxDescriptControlInit;
1621
1622     /* set the transmit timeout flag to be checked in
1623      * the watchdog timer handler. -baz
1624      */
1625
1626     sc->stats.tx_NoCompleteCnt++;
1627     sc->lmc_next_tx++;
1628
1629     /* give ownership to the chip */
1630     LMC_EVENT_LOG(LMC_EVENT_XMT, flag, entry);
1631     sc->lmc_txring[entry].status = 0x80000000;
1632
1633     /* send now! */
1634     LMC_CSR_WRITE (sc, csr_txpoll, 0);
1635
1636     dev->trans_start = jiffies;
1637
1638     spin_unlock_irqrestore(&sc->lmc_lock, flags);
1639
1640     lmc_trace(dev, "lmc_start_xmit_out");
1641     return ret;
1642 }
1643
1644
1645 static int lmc_rx (struct net_device *dev) /*fold00*/
1646 {
1647     lmc_softc_t *sc;
1648     int i;
1649     int rx_work_limit = LMC_RXDESCS;
1650     unsigned int next_rx;
1651     int rxIntLoopCnt;           /* debug -baz */
1652     int localLengthErrCnt = 0;
1653     long stat;
1654     struct sk_buff *skb, *nsb;
1655     u16 len;
1656
1657     lmc_trace(dev, "lmc_rx in");
1658
1659     sc = dev->priv;
1660
1661     lmc_led_on(sc, LMC_DS3_LED3);
1662
1663     rxIntLoopCnt = 0;           /* debug -baz */
1664
1665     i = sc->lmc_next_rx % LMC_RXDESCS;
1666     next_rx = sc->lmc_next_rx;
1667
1668     while (((stat = sc->lmc_rxring[i].status) & LMC_RDES_OWN_BIT) != DESC_OWNED_BY_DC21X4)
1669     {
1670         rxIntLoopCnt++;         /* debug -baz */
1671         len = ((stat & LMC_RDES_FRAME_LENGTH) >> RDES_FRAME_LENGTH_BIT_NUMBER);
1672         if ((stat & 0x0300) != 0x0300) {  /* Check first segment and last segment */
1673             if ((stat & 0x0000ffff) != 0x7fff) {
1674                 /* Oversized frame */
1675                 sc->stats.rx_length_errors++;
1676                 goto skip_packet;
1677             }
1678         }
1679
1680         if(stat & 0x00000008){ /* Catch a dribbling bit error */
1681             sc->stats.rx_errors++;
1682             sc->stats.rx_frame_errors++;
1683             goto skip_packet;
1684         }
1685
1686
1687         if(stat & 0x00000004){ /* Catch a CRC error by the Xilinx */
1688             sc->stats.rx_errors++;
1689             sc->stats.rx_crc_errors++;
1690             goto skip_packet;
1691         }
1692
1693
1694         if (len > LMC_PKT_BUF_SZ){
1695             sc->stats.rx_length_errors++;
1696             localLengthErrCnt++;
1697             goto skip_packet;
1698         }
1699
1700         if (len < sc->lmc_crcSize + 2) {
1701             sc->stats.rx_length_errors++;
1702             sc->stats.rx_SmallPktCnt++;
1703             localLengthErrCnt++;
1704             goto skip_packet;
1705         }
1706
1707         if(stat & 0x00004000){
1708             printk(KERN_WARNING "%s: Receiver descriptor error, receiver out of sync?\n", dev->name);
1709         }
1710
1711         len -= sc->lmc_crcSize;
1712
1713         skb = sc->lmc_rxq[i];
1714
1715         /*
1716          * We ran out of memory at some point
1717          * just allocate an skb buff and continue.
1718          */
1719         
1720         if(skb == 0x0){
1721             nsb = dev_alloc_skb (LMC_PKT_BUF_SZ + 2);
1722             if (nsb) {
1723                 sc->lmc_rxq[i] = nsb;
1724                 nsb->dev = dev;
1725                 sc->lmc_rxring[i].buffer1 = virt_to_bus (nsb->tail);
1726             }
1727             sc->failed_recv_alloc = 1;
1728             goto skip_packet;
1729         }
1730         
1731         dev->last_rx = jiffies;
1732         sc->stats.rx_packets++;
1733         sc->stats.rx_bytes += len;
1734
1735         LMC_CONSOLE_LOG("recv", skb->data, len);
1736
1737         /*
1738          * I'm not sure of the sanity of this
1739          * Packets could be arriving at a constant
1740          * 44.210mbits/sec and we're going to copy
1741          * them into a new buffer??
1742          */
1743         
1744         if(len > (LMC_MTU - (LMC_MTU>>2))){ /* len > LMC_MTU * 0.75 */
1745             /*
1746              * If it's a large packet don't copy it just hand it up
1747              */
1748         give_it_anyways:
1749
1750             sc->lmc_rxq[i] = 0x0;
1751             sc->lmc_rxring[i].buffer1 = 0x0;
1752
1753             skb_put (skb, len);
1754             skb->protocol = lmc_proto_type(sc, skb);
1755             skb->protocol = htons(ETH_P_WAN_PPP);
1756             skb->mac.raw = skb->data;
1757 //            skb->nh.raw = skb->data;
1758             skb->dev = dev;
1759             lmc_proto_netif(sc, skb);
1760
1761             /*
1762              * This skb will be destroyed by the upper layers, make a new one
1763              */
1764             nsb = dev_alloc_skb (LMC_PKT_BUF_SZ + 2);
1765             if (nsb) {
1766                 sc->lmc_rxq[i] = nsb;
1767                 nsb->dev = dev;
1768                 sc->lmc_rxring[i].buffer1 = virt_to_bus (nsb->tail);
1769                 /* Transferred to 21140 below */
1770             }
1771             else {
1772                 /*
1773                  * We've run out of memory, stop trying to allocate
1774                  * memory and exit the interrupt handler
1775                  *
1776                  * The chip may run out of receivers and stop
1777                  * in which care we'll try to allocate the buffer
1778                  * again.  (once a second)
1779                  */
1780                 sc->stats.rx_BuffAllocErr++;
1781                 LMC_EVENT_LOG(LMC_EVENT_RCVINT, stat, len);
1782                 sc->failed_recv_alloc = 1;
1783                 goto skip_out_of_mem;
1784             }
1785         }
1786         else {
1787             nsb = dev_alloc_skb(len);
1788             if(!nsb) {
1789                 goto give_it_anyways;
1790             }
1791             memcpy(skb_put(nsb, len), skb->data, len);
1792             
1793             nsb->protocol = lmc_proto_type(sc, skb);
1794             nsb->mac.raw = nsb->data;
1795 //            nsb->nh.raw = nsb->data;
1796             nsb->dev = dev;
1797             lmc_proto_netif(sc, nsb);
1798         }
1799
1800     skip_packet:
1801         LMC_EVENT_LOG(LMC_EVENT_RCVINT, stat, len);
1802         sc->lmc_rxring[i].status = DESC_OWNED_BY_DC21X4;
1803
1804         sc->lmc_next_rx++;
1805         i = sc->lmc_next_rx % LMC_RXDESCS;
1806         rx_work_limit--;
1807         if (rx_work_limit < 0)
1808             break;
1809     }
1810
1811     /* detect condition for LMC1000 where DSU cable attaches and fills
1812      * descriptors with bogus packets
1813      *
1814     if (localLengthErrCnt > LMC_RXDESCS - 3) {
1815         sc->stats.rx_BadPktSurgeCnt++;
1816         LMC_EVENT_LOG(LMC_EVENT_BADPKTSURGE,
1817                       localLengthErrCnt,
1818                       sc->stats.rx_BadPktSurgeCnt);
1819     } */
1820
1821     /* save max count of receive descriptors serviced */
1822     if (rxIntLoopCnt > sc->stats.rxIntLoopCnt) {
1823         sc->stats.rxIntLoopCnt = rxIntLoopCnt;  /* debug -baz */
1824     }
1825
1826 #ifdef DEBUG
1827     if (rxIntLoopCnt == 0)
1828     {
1829         for (i = 0; i < LMC_RXDESCS; i++)
1830         {
1831             if ((sc->lmc_rxring[i].status & LMC_RDES_OWN_BIT)
1832                 != DESC_OWNED_BY_DC21X4)
1833             {
1834                 rxIntLoopCnt++;
1835             }
1836         }
1837         LMC_EVENT_LOG(LMC_EVENT_RCVEND, rxIntLoopCnt, 0);
1838     }
1839 #endif
1840
1841
1842     lmc_led_off(sc, LMC_DS3_LED3);
1843
1844 skip_out_of_mem:
1845
1846     lmc_trace(dev, "lmc_rx out");
1847
1848     return 0;
1849 }
1850
1851 static struct net_device_stats *lmc_get_stats (struct net_device *dev) /*fold00*/
1852 {
1853     lmc_softc_t *sc;
1854     unsigned long flags;
1855
1856     lmc_trace(dev, "lmc_get_stats in");
1857
1858     sc = dev->priv;
1859
1860     spin_lock_irqsave(&sc->lmc_lock, flags);
1861
1862     sc->stats.rx_missed_errors += LMC_CSR_READ (sc, csr_missed_frames) & 0xffff;
1863
1864     spin_unlock_irqrestore(&sc->lmc_lock, flags);
1865
1866     lmc_trace(dev, "lmc_get_stats out");
1867
1868     return (struct net_device_stats *) &sc->stats;
1869 }
1870
1871 static int __init init_lmc(void)
1872 {
1873     printk ("lmc: module loaded\n");
1874
1875     /* Have lmc_probe search for all the cards, and allocate devices */
1876     if (lmc_probe (NULL) < 0)
1877         return -EIO;
1878
1879     return 0;
1880 }
1881
1882 static void __exit exit_lmc(void)
1883 {
1884     struct net_device *dev, *next;
1885     lmc_softc_t *sc;
1886
1887     /* we have no pointer to our devices, since they are all dynamically
1888      * allocated.  So, here we loop through all the network devices
1889      * looking for ours.  When found, dispose of them properly.
1890      */
1891
1892     for (dev = Lmc_root_dev;
1893          dev != NULL;
1894          dev = next )
1895     {
1896
1897         next = ((lmc_softc_t *) dev->priv)->next_module; /* get it now before we deallocate it */
1898         printk ("%s: removing...\n", dev->name);
1899
1900         /* close the syncppp stuff, and release irq. Close is run on unreg net */
1901         lmc_close (dev);
1902         sc = dev->priv;
1903         if (sc != NULL)
1904             lmc_proto_detach(sc);
1905
1906         /* Remove the device from the linked list */
1907         unregister_netdev (dev);
1908
1909         /* Let go of the io region */;
1910         release_region (dev->base_addr, LMC_REG_RANGE);
1911
1912         /* free our allocated structures. */
1913         kfree (dev->priv);
1914         dev->priv = NULL;
1915
1916         kfree ((struct ppp_device *) dev);
1917         dev = NULL;
1918     }
1919
1920
1921     Lmc_root_dev = NULL;
1922     printk ("lmc module unloaded\n");
1923 }
1924
1925 module_init(init_lmc);
1926 module_exit(exit_lmc);
1927
1928 unsigned lmc_mii_readreg (lmc_softc_t * const sc, unsigned devaddr, unsigned regno) /*fold00*/
1929 {
1930     int i;
1931     int command = (0xf6 << 10) | (devaddr << 5) | regno;
1932     int retval = 0;
1933
1934     lmc_trace(sc->lmc_device, "lmc_mii_readreg in");
1935
1936     LMC_MII_SYNC (sc);
1937
1938     lmc_trace(sc->lmc_device, "lmc_mii_readreg: done sync");
1939
1940     for (i = 15; i >= 0; i--)
1941     {
1942         int dataval = (command & (1 << i)) ? 0x20000 : 0;
1943
1944         LMC_CSR_WRITE (sc, csr_9, dataval);
1945         lmc_delay ();
1946         /* __SLOW_DOWN_IO; */
1947         LMC_CSR_WRITE (sc, csr_9, dataval | 0x10000);
1948         lmc_delay ();
1949         /* __SLOW_DOWN_IO; */
1950     }
1951
1952     lmc_trace(sc->lmc_device, "lmc_mii_readreg: done1");
1953
1954     for (i = 19; i > 0; i--)
1955     {
1956         LMC_CSR_WRITE (sc, csr_9, 0x40000);
1957         lmc_delay ();
1958         /* __SLOW_DOWN_IO; */
1959         retval = (retval << 1) | ((LMC_CSR_READ (sc, csr_9) & 0x80000) ? 1 : 0);
1960         LMC_CSR_WRITE (sc, csr_9, 0x40000 | 0x10000);
1961         lmc_delay ();
1962         /* __SLOW_DOWN_IO; */
1963     }
1964
1965     lmc_trace(sc->lmc_device, "lmc_mii_readreg out");
1966
1967     return (retval >> 1) & 0xffff;
1968 }
1969
1970 void lmc_mii_writereg (lmc_softc_t * const sc, unsigned devaddr, unsigned regno, unsigned data) /*fold00*/
1971 {
1972     int i = 32;
1973     int command = (0x5002 << 16) | (devaddr << 23) | (regno << 18) | data;
1974
1975     lmc_trace(sc->lmc_device, "lmc_mii_writereg in");
1976
1977     LMC_MII_SYNC (sc);
1978
1979     i = 31;
1980     while (i >= 0)
1981     {
1982         int datav;
1983
1984         if (command & (1 << i))
1985             datav = 0x20000;
1986         else
1987             datav = 0x00000;
1988
1989         LMC_CSR_WRITE (sc, csr_9, datav);
1990         lmc_delay ();
1991         /* __SLOW_DOWN_IO; */
1992         LMC_CSR_WRITE (sc, csr_9, (datav | 0x10000));
1993         lmc_delay ();
1994         /* __SLOW_DOWN_IO; */
1995         i--;
1996     }
1997
1998     i = 2;
1999     while (i > 0)
2000     {
2001         LMC_CSR_WRITE (sc, csr_9, 0x40000);
2002         lmc_delay ();
2003         /* __SLOW_DOWN_IO; */
2004         LMC_CSR_WRITE (sc, csr_9, 0x50000);
2005         lmc_delay ();
2006         /* __SLOW_DOWN_IO; */
2007         i--;
2008     }
2009
2010     lmc_trace(sc->lmc_device, "lmc_mii_writereg out");
2011 }
2012
2013 static void lmc_softreset (lmc_softc_t * const sc) /*fold00*/
2014 {
2015     int i;
2016
2017     lmc_trace(sc->lmc_device, "lmc_softreset in");
2018
2019     /* Initialize the receive rings and buffers. */
2020     sc->lmc_txfull = 0;
2021     sc->lmc_next_rx = 0;
2022     sc->lmc_next_tx = 0;
2023     sc->lmc_taint_rx = 0;
2024     sc->lmc_taint_tx = 0;
2025
2026     /*
2027      * Setup each one of the receiver buffers
2028      * allocate an skbuff for each one, setup the descriptor table
2029      * and point each buffer at the next one
2030      */
2031
2032     for (i = 0; i < LMC_RXDESCS; i++)
2033     {
2034         struct sk_buff *skb;
2035
2036         if (sc->lmc_rxq[i] == NULL)
2037         {
2038             skb = dev_alloc_skb (LMC_PKT_BUF_SZ + 2);
2039             if(skb == NULL){
2040                 printk(KERN_WARNING "%s: Failed to allocate receiver ring, will try again\n", sc->name);
2041                 sc->failed_ring = 1;
2042                 break;
2043             }
2044             else{
2045                 sc->lmc_rxq[i] = skb;
2046             }
2047         }
2048         else
2049         {
2050             skb = sc->lmc_rxq[i];
2051         }
2052
2053         skb->dev = sc->lmc_device;
2054
2055         /* owned by 21140 */
2056         sc->lmc_rxring[i].status = 0x80000000;
2057
2058         /* used to be PKT_BUF_SZ now uses skb since we lose some to head room */
2059         sc->lmc_rxring[i].length = skb->end - skb->data;
2060
2061         /* use to be tail which is dumb since you're thinking why write
2062          * to the end of the packj,et but since there's nothing there tail == data
2063          */
2064         sc->lmc_rxring[i].buffer1 = virt_to_bus (skb->data);
2065
2066         /* This is fair since the structure is static and we have the next address */
2067         sc->lmc_rxring[i].buffer2 = virt_to_bus (&sc->lmc_rxring[i + 1]);
2068
2069     }
2070
2071     /*
2072      * Sets end of ring
2073      */
2074     sc->lmc_rxring[i - 1].length |= 0x02000000; /* Set end of buffers flag */
2075     sc->lmc_rxring[i - 1].buffer2 = virt_to_bus (&sc->lmc_rxring[0]); /* Point back to the start */
2076     LMC_CSR_WRITE (sc, csr_rxlist, virt_to_bus (sc->lmc_rxring)); /* write base address */
2077
2078
2079     /* Initialize the transmit rings and buffers */
2080     for (i = 0; i < LMC_TXDESCS; i++)
2081     {
2082         if (sc->lmc_txq[i] != NULL){            /* have buffer */
2083             dev_kfree_skb(sc->lmc_txq[i]);      /* free it */
2084             sc->stats.tx_dropped++;      /* We just dropped a packet */
2085         }
2086         sc->lmc_txq[i] = 0;
2087         sc->lmc_txring[i].status = 0x00000000;
2088         sc->lmc_txring[i].buffer2 = virt_to_bus (&sc->lmc_txring[i + 1]);
2089     }
2090     sc->lmc_txring[i - 1].buffer2 = virt_to_bus (&sc->lmc_txring[0]);
2091     LMC_CSR_WRITE (sc, csr_txlist, virt_to_bus (sc->lmc_txring));
2092
2093     lmc_trace(sc->lmc_device, "lmc_softreset out");
2094 }
2095
2096 static int lmc_set_config(struct net_device *dev, struct ifmap *map) /*fold00*/
2097 {
2098     lmc_trace(dev, "lmc_set_config in");
2099     lmc_trace(dev, "lmc_set_config out");
2100     return -EOPNOTSUPP;
2101 }
2102
2103 void lmc_gpio_mkinput(lmc_softc_t * const sc, u_int32_t bits) /*fold00*/
2104 {
2105     lmc_trace(sc->lmc_device, "lmc_gpio_mkinput in");
2106     sc->lmc_gpio_io &= ~bits;
2107     LMC_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET | (sc->lmc_gpio_io));
2108     lmc_trace(sc->lmc_device, "lmc_gpio_mkinput out");
2109 }
2110
2111 void lmc_gpio_mkoutput(lmc_softc_t * const sc, u_int32_t bits) /*fold00*/
2112 {
2113     lmc_trace(sc->lmc_device, "lmc_gpio_mkoutput in");
2114     sc->lmc_gpio_io |= bits;
2115     LMC_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET | (sc->lmc_gpio_io));
2116     lmc_trace(sc->lmc_device, "lmc_gpio_mkoutput out");
2117 }
2118
2119 void lmc_led_on(lmc_softc_t * const sc, u_int32_t led) /*fold00*/
2120 {
2121     lmc_trace(sc->lmc_device, "lmc_led_on in");
2122     if((~sc->lmc_miireg16) & led){ /* Already on! */
2123         lmc_trace(sc->lmc_device, "lmc_led_on aon out");
2124         return;
2125     }
2126     
2127     sc->lmc_miireg16 &= ~led;
2128     lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
2129     lmc_trace(sc->lmc_device, "lmc_led_on out");
2130 }
2131
2132 void lmc_led_off(lmc_softc_t * const sc, u_int32_t led) /*fold00*/
2133 {
2134     lmc_trace(sc->lmc_device, "lmc_led_off in");
2135     if(sc->lmc_miireg16 & led){ /* Already set don't do anything */
2136         lmc_trace(sc->lmc_device, "lmc_led_off aoff out");
2137         return;
2138     }
2139     
2140     sc->lmc_miireg16 |= led;
2141     lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
2142     lmc_trace(sc->lmc_device, "lmc_led_off out");
2143 }
2144
2145 static void lmc_reset(lmc_softc_t * const sc) /*fold00*/
2146 {
2147     lmc_trace(sc->lmc_device, "lmc_reset in");
2148     sc->lmc_miireg16 |= LMC_MII16_FIFO_RESET;
2149     lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
2150
2151     sc->lmc_miireg16 &= ~LMC_MII16_FIFO_RESET;
2152     lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
2153
2154     /*
2155      * make some of the GPIO pins be outputs
2156      */
2157     lmc_gpio_mkoutput(sc, LMC_GEP_RESET);
2158
2159     /*
2160      * RESET low to force state reset.  This also forces
2161      * the transmitter clock to be internal, but we expect to reset
2162      * that later anyway.
2163      */
2164     sc->lmc_gpio &= ~(LMC_GEP_RESET);
2165     LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
2166
2167     /*
2168      * hold for more than 10 microseconds
2169      */
2170     udelay(50);
2171
2172     /*
2173      * stop driving Xilinx-related signals
2174      */
2175     lmc_gpio_mkinput(sc, LMC_GEP_RESET);
2176
2177     /*
2178      * Call media specific init routine
2179      */
2180     sc->lmc_media->init(sc);
2181
2182     sc->stats.resetCount++;
2183     lmc_trace(sc->lmc_device, "lmc_reset out");
2184 }
2185
2186 static void lmc_dec_reset(lmc_softc_t * const sc) /*fold00*/
2187 {
2188     u_int32_t val;
2189     lmc_trace(sc->lmc_device, "lmc_dec_reset in");
2190
2191     /*
2192      * disable all interrupts
2193      */
2194     sc->lmc_intrmask = 0;
2195     LMC_CSR_WRITE(sc, csr_intr, sc->lmc_intrmask);
2196
2197     /*
2198      * Reset the chip with a software reset command.
2199      * Wait 10 microseconds (actually 50 PCI cycles but at
2200      * 33MHz that comes to two microseconds but wait a
2201      * bit longer anyways)
2202      */
2203     LMC_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
2204     udelay(25);
2205 #ifdef __sparc__
2206     sc->lmc_busmode = LMC_CSR_READ(sc, csr_busmode);
2207     sc->lmc_busmode = 0x00100000;
2208     sc->lmc_busmode &= ~TULIP_BUSMODE_SWRESET;
2209     LMC_CSR_WRITE(sc, csr_busmode, sc->lmc_busmode);
2210 #endif
2211     sc->lmc_cmdmode = LMC_CSR_READ(sc, csr_command);
2212
2213     /*
2214      * We want:
2215      *   no ethernet address in frames we write
2216      *   disable padding (txdesc, padding disable)
2217      *   ignore runt frames (rdes0 bit 15)
2218      *   no receiver watchdog or transmitter jabber timer
2219      *       (csr15 bit 0,14 == 1)
2220      *   if using 16-bit CRC, turn off CRC (trans desc, crc disable)
2221      */
2222
2223     sc->lmc_cmdmode |= ( TULIP_CMD_PROMISCUOUS
2224                          | TULIP_CMD_FULLDUPLEX
2225                          | TULIP_CMD_PASSBADPKT
2226                          | TULIP_CMD_NOHEARTBEAT
2227                          | TULIP_CMD_PORTSELECT
2228                          | TULIP_CMD_RECEIVEALL
2229                          | TULIP_CMD_MUSTBEONE
2230                        );
2231     sc->lmc_cmdmode &= ~( TULIP_CMD_OPERMODE
2232                           | TULIP_CMD_THRESHOLDCTL
2233                           | TULIP_CMD_STOREFWD
2234                           | TULIP_CMD_TXTHRSHLDCTL
2235                         );
2236
2237     LMC_CSR_WRITE(sc, csr_command, sc->lmc_cmdmode);
2238
2239     /*
2240      * disable receiver watchdog and transmit jabber
2241      */
2242     val = LMC_CSR_READ(sc, csr_sia_general);
2243     val |= (TULIP_WATCHDOG_TXDISABLE | TULIP_WATCHDOG_RXDISABLE);
2244     LMC_CSR_WRITE(sc, csr_sia_general, val);
2245
2246     lmc_trace(sc->lmc_device, "lmc_dec_reset out");
2247 }
2248
2249 static void lmc_initcsrs(lmc_softc_t * const sc, lmc_csrptr_t csr_base, /*fold00*/
2250                          size_t csr_size)
2251 {
2252     lmc_trace(sc->lmc_device, "lmc_initcsrs in");
2253     sc->lmc_csrs.csr_busmode            = csr_base +  0 * csr_size;
2254     sc->lmc_csrs.csr_txpoll             = csr_base +  1 * csr_size;
2255     sc->lmc_csrs.csr_rxpoll             = csr_base +  2 * csr_size;
2256     sc->lmc_csrs.csr_rxlist             = csr_base +  3 * csr_size;
2257     sc->lmc_csrs.csr_txlist             = csr_base +  4 * csr_size;
2258     sc->lmc_csrs.csr_status             = csr_base +  5 * csr_size;
2259     sc->lmc_csrs.csr_command            = csr_base +  6 * csr_size;
2260     sc->lmc_csrs.csr_intr               = csr_base +  7 * csr_size;
2261     sc->lmc_csrs.csr_missed_frames      = csr_base +  8 * csr_size;
2262     sc->lmc_csrs.csr_9                  = csr_base +  9 * csr_size;
2263     sc->lmc_csrs.csr_10                 = csr_base + 10 * csr_size;
2264     sc->lmc_csrs.csr_11                 = csr_base + 11 * csr_size;
2265     sc->lmc_csrs.csr_12                 = csr_base + 12 * csr_size;
2266     sc->lmc_csrs.csr_13                 = csr_base + 13 * csr_size;
2267     sc->lmc_csrs.csr_14                 = csr_base + 14 * csr_size;
2268     sc->lmc_csrs.csr_15                 = csr_base + 15 * csr_size;
2269     lmc_trace(sc->lmc_device, "lmc_initcsrs out");
2270 }
2271
2272 static void lmc_driver_timeout(struct net_device *dev) { /*fold00*/
2273     lmc_softc_t *sc;
2274     u32 csr6;
2275     unsigned long flags;
2276
2277     lmc_trace(dev, "lmc_driver_timeout in");
2278
2279     sc = dev->priv;
2280
2281     spin_lock_irqsave(&sc->lmc_lock, flags);
2282
2283     printk("%s: Xmitter busy|\n", dev->name);
2284
2285     sc->stats.tx_tbusy_calls++ ;
2286     if (jiffies - dev->trans_start < TX_TIMEOUT) {
2287         goto bug_out;
2288     }
2289
2290     /*
2291      * Chip seems to have locked up
2292      * Reset it
2293      * This whips out all our decriptor
2294      * table and starts from scartch
2295      */
2296
2297     LMC_EVENT_LOG(LMC_EVENT_XMTPRCTMO,
2298                   LMC_CSR_READ (sc, csr_status),
2299                   sc->stats.tx_ProcTimeout);
2300
2301     lmc_running_reset (dev);
2302
2303     LMC_EVENT_LOG(LMC_EVENT_RESET1, LMC_CSR_READ (sc, csr_status), 0);
2304     LMC_EVENT_LOG(LMC_EVENT_RESET2,
2305                   lmc_mii_readreg (sc, 0, 16),
2306                   lmc_mii_readreg (sc, 0, 17));
2307
2308     /* restart the tx processes */
2309     csr6 = LMC_CSR_READ (sc, csr_command);
2310     LMC_CSR_WRITE (sc, csr_command, csr6 | 0x0002);
2311     LMC_CSR_WRITE (sc, csr_command, csr6 | 0x2002);
2312
2313     /* immediate transmit */
2314     LMC_CSR_WRITE (sc, csr_txpoll, 0);
2315
2316     sc->stats.tx_errors++;
2317     sc->stats.tx_ProcTimeout++; /* -baz */
2318
2319     dev->trans_start = jiffies;
2320
2321 bug_out:
2322
2323     spin_unlock_irqrestore(&sc->lmc_lock, flags);
2324
2325     lmc_trace(dev, "lmc_driver_timout out");
2326
2327
2328 }
2329
2330 int lmc_setup(void) { /*FOLD00*/
2331    return lmc_probe(NULL);
2332 }
2333