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