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