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