2 * Copyright (c) 1997-2000 LAN Media Corporation (LMC)
3 * All rights reserved. www.lanmedia.com
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).
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.
19 * Driver for the LanMedia LMC5200, LMC5245, LMC1000, LMC1200 cards.
21 * To control link specific options lmcctl is required.
22 * It can be obtained from ftp.lanmedia.com.
25 * Linux uses the device struct lmc_private to pass private information
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.
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.
39 /* $Id: lmc_main.c,v 1.36 2000/04/11 05:25:25 asj Exp $ */
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>
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>
60 #include <net/syncppp.h>
62 #include <asm/processor.h> /* Processor type for cache alignment. */
63 #include <asm/bitops.h>
66 #include <asm/uaccess.h>
67 //#include <asm/spinlock.h>
69 #define DRIVER_MAJOR_VERSION 1
70 #define DRIVER_MINOR_VERSION 34
71 #define DRIVER_SUB_VERSION 0
73 #define DRIVER_VERSION ((DRIVER_MAJOR_VERSION << 8) + DRIVER_MINOR_VERSION)
77 #include "lmc_ioctl.h"
78 #include "lmc_debug.h"
79 #include "lmc_proto.h"
82 static int Lmc_Count = 0;
83 static struct net_device *Lmc_root_dev = NULL;
84 static u8 cards_found = 0;
86 static int lmc_first_load = 0;
88 int LMC_PKT_BUF_SZ = 1542;
91 static struct pci_device_id lmc_pci_tbl[] __devinitdata = {
92 { 0x1011, 0x009, 0x1379, PCI_ANY_ID, 0, 0, 0},
96 MODULE_DEVICE_TABLE(pci, lmc_pci_tbl);
98 MODULE_LICENSE("GPL");
102 int lmc_probe_fake(struct net_device *dev);
103 static struct net_device *lmc_probe1(struct net_device *dev, unsigned long ioaddr, unsigned int irq,
104 int chip_id, int subdevice, int board_idx);
105 static int lmc_start_xmit(struct sk_buff *skb, struct net_device *dev);
106 static int lmc_start_xmit(struct sk_buff *skb, struct net_device *dev);
107 static int lmc_rx (struct net_device *dev);
108 static int lmc_open(struct net_device *dev);
109 static int lmc_close(struct net_device *dev);
110 static struct net_device_stats *lmc_get_stats(struct net_device *dev);
111 static irqreturn_t lmc_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
112 static int lmc_set_config(struct net_device *dev, struct ifmap *map);
113 static void lmc_initcsrs(lmc_softc_t * const sc, lmc_csrptr_t csr_base, size_t csr_size);
114 static void lmc_softreset(lmc_softc_t * const);
115 static void lmc_running_reset(struct net_device *dev);
116 static int lmc_ifdown(struct net_device * const);
117 static void lmc_watchdog(unsigned long data);
118 static int lmc_init(struct net_device * const);
119 static void lmc_reset(lmc_softc_t * const sc);
120 static void lmc_dec_reset(lmc_softc_t * const sc);
121 static void lmc_driver_timeout(struct net_device *dev);
126 * linux reserves 16 device specific IOCTLs. We call them
127 * LMCIOC* to control various bits of our world.
129 int lmc_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd) /*fold00*/
143 lmc_trace(dev, "lmc_ioctl in");
146 * Most functions mess with the structure
147 * Disable interrupts while we do the polling
149 spin_lock_irqsave(&sc->lmc_lock, flags);
153 * Return current driver state. Since we keep this up
154 * To date internally, just copy this out to the user.
156 case LMCIOCGINFO: /*fold01*/
157 if (copy_to_user(ifr->ifr_data, &sc->ictl, sizeof (lmc_ctl_t)))
162 case LMCIOCSINFO: /*fold01*/
163 sp = &((struct ppp_device *) dev)->sppp;
164 if (!capable(CAP_NET_ADMIN)) {
169 if(dev->flags & IFF_UP){
174 if (copy_from_user(&ctl, ifr->ifr_data, sizeof (lmc_ctl_t)))
177 sc->lmc_media->set_status (sc, &ctl);
179 if(ctl.crc_length != sc->ictl.crc_length) {
180 sc->lmc_media->set_crc_length(sc, ctl.crc_length);
181 if (sc->ictl.crc_length == LMC_CTL_CRC_LENGTH_16)
182 sc->TxDescriptControlInit |= LMC_TDES_ADD_CRC_DISABLE;
184 sc->TxDescriptControlInit &= ~LMC_TDES_ADD_CRC_DISABLE;
187 if (ctl.keepalive_onoff == LMC_CTL_OFF)
188 sp->pp_flags &= ~PP_KEEPALIVE; /* Turn off */
190 sp->pp_flags |= PP_KEEPALIVE; /* Turn on */
195 case LMCIOCIFTYPE: /*fold01*/
197 u_int16_t old_type = sc->if_type;
200 if (!capable(CAP_NET_ADMIN)) {
205 if (copy_from_user(&new_type, ifr->ifr_data, sizeof(u_int16_t)))
209 if (new_type == old_type)
212 break; /* no change */
216 lmc_proto_detach(sc);
218 sc->if_type = new_type;
219 // lmc_proto_init(sc);
220 lmc_proto_attach(sc);
227 case LMCIOCGETXINFO: /*fold01*/
228 sc->lmc_xinfo.Magic0 = 0xBEEFCAFE;
230 sc->lmc_xinfo.PciCardType = sc->lmc_cardtype;
231 sc->lmc_xinfo.PciSlotNumber = 0;
232 sc->lmc_xinfo.DriverMajorVersion = DRIVER_MAJOR_VERSION;
233 sc->lmc_xinfo.DriverMinorVersion = DRIVER_MINOR_VERSION;
234 sc->lmc_xinfo.DriverSubVersion = DRIVER_SUB_VERSION;
235 sc->lmc_xinfo.XilinxRevisionNumber =
236 lmc_mii_readreg (sc, 0, 3) & 0xf;
237 sc->lmc_xinfo.MaxFrameSize = LMC_PKT_BUF_SZ;
238 sc->lmc_xinfo.link_status = sc->lmc_media->get_link_status (sc);
239 sc->lmc_xinfo.mii_reg16 = lmc_mii_readreg (sc, 0, 16);
241 sc->lmc_xinfo.Magic1 = 0xDEADBEEF;
243 if (copy_to_user(ifr->ifr_data, &sc->lmc_xinfo,
244 sizeof (struct lmc_xinfo)))
250 case LMCIOCGETLMCSTATS: /*fold01*/
251 if (sc->lmc_cardtype == LMC_CARDTYPE_T1){
252 lmc_mii_writereg (sc, 0, 17, T1FRAMER_FERR_LSB);
253 sc->stats.framingBitErrorCount +=
254 lmc_mii_readreg (sc, 0, 18) & 0xff;
255 lmc_mii_writereg (sc, 0, 17, T1FRAMER_FERR_MSB);
256 sc->stats.framingBitErrorCount +=
257 (lmc_mii_readreg (sc, 0, 18) & 0xff) << 8;
258 lmc_mii_writereg (sc, 0, 17, T1FRAMER_LCV_LSB);
259 sc->stats.lineCodeViolationCount +=
260 lmc_mii_readreg (sc, 0, 18) & 0xff;
261 lmc_mii_writereg (sc, 0, 17, T1FRAMER_LCV_MSB);
262 sc->stats.lineCodeViolationCount +=
263 (lmc_mii_readreg (sc, 0, 18) & 0xff) << 8;
264 lmc_mii_writereg (sc, 0, 17, T1FRAMER_AERR);
265 regVal = lmc_mii_readreg (sc, 0, 18) & 0xff;
267 sc->stats.lossOfFrameCount +=
268 (regVal & T1FRAMER_LOF_MASK) >> 4;
269 sc->stats.changeOfFrameAlignmentCount +=
270 (regVal & T1FRAMER_COFA_MASK) >> 2;
271 sc->stats.severelyErroredFrameCount +=
272 regVal & T1FRAMER_SEF_MASK;
275 if (copy_to_user(ifr->ifr_data, &sc->stats,
276 sizeof (struct lmc_statistics)))
282 case LMCIOCCLEARLMCSTATS: /*fold01*/
283 if (!capable(CAP_NET_ADMIN)){
288 memset (&sc->stats, 0, sizeof (struct lmc_statistics));
289 sc->stats.check = STATCHECK;
290 sc->stats.version_size = (DRIVER_VERSION << 16) +
291 sizeof (struct lmc_statistics);
292 sc->stats.lmc_cardtype = sc->lmc_cardtype;
296 case LMCIOCSETCIRCUIT: /*fold01*/
297 if (!capable(CAP_NET_ADMIN)){
302 if(dev->flags & IFF_UP){
307 if (copy_from_user(&ctl, ifr->ifr_data, sizeof (lmc_ctl_t)))
309 sc->lmc_media->set_circuit_type(sc, ctl.circuit_type);
310 sc->ictl.circuit_type = ctl.circuit_type;
315 case LMCIOCRESET: /*fold01*/
316 if (!capable(CAP_NET_ADMIN)){
321 /* Reset driver and bring back to current state */
322 printk (" REG16 before reset +%04x\n", lmc_mii_readreg (sc, 0, 16));
323 lmc_running_reset (dev);
324 printk (" REG16 after reset +%04x\n", lmc_mii_readreg (sc, 0, 16));
326 LMC_EVENT_LOG(LMC_EVENT_FORCEDRESET, LMC_CSR_READ (sc, csr_status), lmc_mii_readreg (sc, 0, 16));
332 case LMCIOCDUMPEVENTLOG:
333 if (copy_to_user(ifr->ifr_data, &lmcEventLogIndex, sizeof (u32)))
335 if (copy_to_user(ifr->ifr_data + sizeof (u32), lmcEventLogBuf, sizeof (lmcEventLogBuf)))
340 #endif /* end ifdef _DBG_EVENTLOG */
341 case LMCIOCT1CONTROL: /*fold01*/
342 if (sc->lmc_cardtype != LMC_CARDTYPE_T1){
347 case LMCIOCXILINX: /*fold01*/
349 struct lmc_xilinx_control xc; /*fold02*/
351 if (!capable(CAP_NET_ADMIN)){
357 * Stop the xwitter whlie we restart the hardware
359 netif_stop_queue(dev);
361 if (copy_from_user(&xc, ifr->ifr_data, sizeof (struct lmc_xilinx_control)))
364 case lmc_xilinx_reset: /*fold02*/
367 mii = lmc_mii_readreg (sc, 0, 16);
370 * Make all of them 0 and make input
372 lmc_gpio_mkinput(sc, 0xff);
375 * make the reset output
377 lmc_gpio_mkoutput(sc, LMC_GEP_RESET);
380 * RESET low to force configuration. This also forces
381 * the transmitter clock to be internal, but we expect to reset
385 sc->lmc_gpio &= ~LMC_GEP_RESET;
386 LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
390 * hold for more than 10 microseconds
394 sc->lmc_gpio |= LMC_GEP_RESET;
395 LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
399 * stop driving Xilinx-related signals
401 lmc_gpio_mkinput(sc, 0xff);
403 /* Reset the frammer hardware */
404 sc->lmc_media->set_link_status (sc, 1);
405 sc->lmc_media->set_status (sc, NULL);
406 // lmc_softreset(sc);
410 for(i = 0; i < 5; i++){
411 lmc_led_on(sc, LMC_DS3_LED0);
413 lmc_led_off(sc, LMC_DS3_LED0);
414 lmc_led_on(sc, LMC_DS3_LED1);
416 lmc_led_off(sc, LMC_DS3_LED1);
417 lmc_led_on(sc, LMC_DS3_LED3);
419 lmc_led_off(sc, LMC_DS3_LED3);
420 lmc_led_on(sc, LMC_DS3_LED2);
422 lmc_led_off(sc, LMC_DS3_LED2);
433 case lmc_xilinx_load_prom: /*fold02*/
436 int timeout = 500000;
437 mii = lmc_mii_readreg (sc, 0, 16);
440 * Make all of them 0 and make input
442 lmc_gpio_mkinput(sc, 0xff);
445 * make the reset output
447 lmc_gpio_mkoutput(sc, LMC_GEP_DP | LMC_GEP_RESET);
450 * RESET low to force configuration. This also forces
451 * the transmitter clock to be internal, but we expect to reset
455 sc->lmc_gpio &= ~(LMC_GEP_RESET | LMC_GEP_DP);
456 LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
460 * hold for more than 10 microseconds
464 sc->lmc_gpio |= LMC_GEP_DP | LMC_GEP_RESET;
465 LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
468 * busy wait for the chip to reset
470 while( (LMC_CSR_READ(sc, csr_gp) & LMC_GEP_INIT) == 0 &&
476 * stop driving Xilinx-related signals
478 lmc_gpio_mkinput(sc, 0xff);
487 case lmc_xilinx_load: /*fold02*/
491 int timeout = 500000;
498 data = kmalloc(xc.len, GFP_KERNEL);
500 printk(KERN_WARNING "%s: Failed to allocate memory for copy\n", dev->name);
505 if(copy_from_user(data, xc.data, xc.len))
512 printk("%s: Starting load of data Len: %d at 0x%p == 0x%p\n", dev->name, xc.len, xc.data, data);
514 lmc_gpio_mkinput(sc, 0xff);
517 * Clear the Xilinx and start prgramming from the DEC
528 sc->lmc_gpio &= ~LMC_GEP_DP;
529 sc->lmc_gpio &= ~LMC_GEP_RESET;
530 sc->lmc_gpio |= LMC_GEP_MODE;
531 LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
533 lmc_gpio_mkoutput(sc, LMC_GEP_MODE | LMC_GEP_DP | LMC_GEP_RESET);
536 * Wait at least 10 us 20 to be safe
541 * Clear reset and activate programming lines
548 lmc_gpio_mkinput(sc, LMC_GEP_DP | LMC_GEP_RESET);
551 * Set LOAD, DATA, Clock to 1
554 sc->lmc_gpio |= LMC_GEP_MODE;
555 sc->lmc_gpio |= LMC_GEP_DATA;
556 sc->lmc_gpio |= LMC_GEP_CLK;
557 LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
559 lmc_gpio_mkoutput(sc, LMC_GEP_DATA | LMC_GEP_CLK | LMC_GEP_MODE );
562 * busy wait for the chip to reset
564 while( (LMC_CSR_READ(sc, csr_gp) & LMC_GEP_INIT) == 0 &&
568 printk(KERN_DEBUG "%s: Waited %d for the Xilinx to clear it's memory\n", dev->name, 500000-timeout);
570 for(pos = 0; pos < xc.len; pos++){
573 sc->lmc_gpio &= ~LMC_GEP_DATA; /* Data is 0 */
576 sc->lmc_gpio |= LMC_GEP_DATA; /* Data is 1 */
579 printk(KERN_WARNING "%s Bad data in xilinx programming data at %d, got %d wanted 0 or 1\n", dev->name, pos, data[pos]);
580 sc->lmc_gpio |= LMC_GEP_DATA; /* Assume it's 1 */
582 sc->lmc_gpio &= ~LMC_GEP_CLK; /* Clock to zero */
583 sc->lmc_gpio |= LMC_GEP_MODE;
584 LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
587 sc->lmc_gpio |= LMC_GEP_CLK; /* Put the clack back to one */
588 sc->lmc_gpio |= LMC_GEP_MODE;
589 LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
592 if((LMC_CSR_READ(sc, csr_gp) & LMC_GEP_INIT) == 0){
593 printk(KERN_WARNING "%s: Reprogramming FAILED. Needs to be reprogrammed. (corrupted data)\n", dev->name);
595 else if((LMC_CSR_READ(sc, csr_gp) & LMC_GEP_DP) == 0){
596 printk(KERN_WARNING "%s: Reprogramming FAILED. Needs to be reprogrammed. (done)\n", dev->name);
599 printk(KERN_DEBUG "%s: Done reprogramming Xilinx, %d bits, good luck!\n", dev->name, pos);
602 lmc_gpio_mkinput(sc, 0xff);
604 sc->lmc_miireg16 |= LMC_MII16_FIFO_RESET;
605 lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
607 sc->lmc_miireg16 &= ~LMC_MII16_FIFO_RESET;
608 lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
621 netif_wake_queue(dev);
627 /* If we don't know what to do, give the protocol a shot. */
628 ret = lmc_proto_ioctl (sc, ifr, cmd);
632 spin_unlock_irqrestore(&sc->lmc_lock, flags); /*fold01*/
634 lmc_trace(dev, "lmc_ioctl out");
640 /* the watchdog process that cruises around */
641 static void lmc_watchdog (unsigned long data) /*fold00*/
643 struct net_device *dev = (struct net_device *) data;
651 lmc_trace(dev, "lmc_watchdog in");
653 spin_lock_irqsave(&sc->lmc_lock, flags);
655 if(sc->check != 0xBEAFCAFE){
656 printk("LMC: Corrupt net_device stuct, breaking out\n");
657 spin_unlock_irqrestore(&sc->lmc_lock, flags);
662 /* Make sure the tx jabber and rx watchdog are off,
663 * and the transmit and receive processes are running.
666 LMC_CSR_WRITE (sc, csr_15, 0x00000011);
667 sc->lmc_cmdmode |= TULIP_CMD_TXRUN | TULIP_CMD_RXRUN;
668 LMC_CSR_WRITE (sc, csr_command, sc->lmc_cmdmode);
673 LMC_EVENT_LOG(LMC_EVENT_WATCHDOG, LMC_CSR_READ (sc, csr_status), lmc_mii_readreg (sc, 0, 16));
675 /* --- begin time out check -----------------------------------
676 * check for a transmit interrupt timeout
677 * Has the packet xmt vs xmt serviced threshold been exceeded */
678 if (sc->lmc_taint_tx == sc->lastlmc_taint_tx &&
679 sc->stats.tx_packets > sc->lasttx_packets &&
680 sc->tx_TimeoutInd == 0)
683 /* wait for the watchdog to come around again */
684 sc->tx_TimeoutInd = 1;
686 else if (sc->lmc_taint_tx == sc->lastlmc_taint_tx &&
687 sc->stats.tx_packets > sc->lasttx_packets &&
691 LMC_EVENT_LOG(LMC_EVENT_XMTINTTMO, LMC_CSR_READ (sc, csr_status), 0);
693 sc->tx_TimeoutDisplay = 1;
694 sc->stats.tx_TimeoutCnt++;
696 /* DEC chip is stuck, hit it with a RESET!!!! */
697 lmc_running_reset (dev);
700 /* look at receive & transmit process state to make sure they are running */
701 LMC_EVENT_LOG(LMC_EVENT_RESET1, LMC_CSR_READ (sc, csr_status), 0);
703 /* look at: DSR - 02 for Reg 16
709 LMC_EVENT_LOG(LMC_EVENT_RESET2, lmc_mii_readreg (sc, 0, 16), lmc_mii_readreg (sc, 0, 17));
711 /* reset the transmit timeout detection flag */
712 sc->tx_TimeoutInd = 0;
713 sc->lastlmc_taint_tx = sc->lmc_taint_tx;
714 sc->lasttx_packets = sc->stats.tx_packets;
718 sc->tx_TimeoutInd = 0;
719 sc->lastlmc_taint_tx = sc->lmc_taint_tx;
720 sc->lasttx_packets = sc->stats.tx_packets;
723 /* --- end time out check ----------------------------------- */
726 link_status = sc->lmc_media->get_link_status (sc);
729 * hardware level link lost, but the interface is marked as up.
732 if ((link_status == 0) && (sc->last_link_status != 0)) {
733 printk(KERN_WARNING "%s: hardware/physical link down\n", dev->name);
734 sc->last_link_status = 0;
735 /* lmc_reset (sc); Why reset??? The link can go down ok */
737 /* Inform the world that link has been lost */
738 dev->flags &= ~IFF_RUNNING;
742 * hardware link is up, but the interface is marked as down.
743 * Bring it back up again.
745 if (link_status != 0 && sc->last_link_status == 0) {
746 printk(KERN_WARNING "%s: hardware/physical link up\n", dev->name);
747 sc->last_link_status = 1;
748 /* lmc_reset (sc); Again why reset??? */
750 /* Inform the world that link protocol is back up. */
751 dev->flags |= IFF_RUNNING;
753 /* Now we have to tell the syncppp that we had an outage
754 * and that it should deal. Calling sppp_reopen here
755 * should do the trick, but we may have to call sppp_close
756 * when the link goes down, and call sppp_open here.
757 * Subject to more testing.
761 lmc_proto_reopen(sc);
765 /* Call media specific watchdog functions */
766 sc->lmc_media->watchdog(sc);
769 * Poke the transmitter to make sure it
770 * never stops, even if we run out of mem
772 LMC_CSR_WRITE(sc, csr_rxpoll, 0);
775 * Check for code that failed
776 * and try and fix it as appropriate
778 if(sc->failed_ring == 1){
780 * Failed to setup the recv/xmit rin
786 if(sc->failed_recv_alloc == 1){
788 * We failed to alloc mem in the
789 * interrupt handler, go through the rings
792 sc->failed_recv_alloc = 0;
798 * remember the timer value
802 ticks = LMC_CSR_READ (sc, csr_gp_timer);
803 LMC_CSR_WRITE (sc, csr_gp_timer, 0xffffffffUL);
804 sc->ictl.ticks = 0x0000ffff - (ticks & 0x0000ffff);
807 * restart this timer.
809 sc->timer.expires = jiffies + (HZ);
810 add_timer (&sc->timer);
812 spin_unlock_irqrestore(&sc->lmc_lock, flags);
814 lmc_trace(dev, "lmc_watchdog out");
818 static int lmc_init(struct net_device * const dev) /*fold00*/
820 lmc_trace(dev, "lmc_init in");
821 lmc_trace(dev, "lmc_init out");
826 /* This initializes each card from lmc_probe() */
827 static struct net_device *lmc_probe1 (struct net_device *dev, unsigned long ioaddr, unsigned int irq, /*fold00*/
828 int chip_id, int subdevice, int board_idx)
830 lmc_softc_t *sc = NULL;
831 u_int16_t AdapModelNum;
834 * Allocate our own device structure
837 dev = kmalloc (sizeof (struct net_device)+8, GFP_KERNEL);
839 printk (KERN_ERR "lmc: kmalloc for device failed\n");
842 memset (dev, 0, sizeof (struct net_device));
846 * Switch to common hdlc%d naming. We name by type not by vendor
849 dev_alloc_name(dev, "hdlc%d");
852 * GCOM uses LMC vendor name so that clients can know which card
855 dev_alloc_name(dev, "lmc%d");
858 lmc_trace(dev, "lmc_probe1 in");
862 if(lmc_first_load == 0){
863 printk(KERN_INFO "Lan Media Corporation WAN Driver Version %d.%d.%d\n",DRIVER_MAJOR_VERSION, DRIVER_MINOR_VERSION,DRIVER_SUB_VERSION);
868 * Allocate space for the private data structure
871 sc = kmalloc (sizeof (lmc_softc_t), GFP_KERNEL);
873 printk (KERN_WARNING "%s: Cannot allocate memory for device state\n",
877 memset (sc, 0, sizeof (lmc_softc_t));
879 sc->lmc_device = dev;
880 sc->name = dev->name;
882 /* Initialize the sppp layer */
883 /* An ioctl can cause a subsequent detach for raw frame interface */
884 sc->if_type = LMC_PPP;
885 sc->check = 0xBEAFCAFE;
886 dev->base_addr = ioaddr;
889 * This will get the protocol layer ready and do any 1 time init's
890 * Must have a valid sc and dev structure
894 lmc_proto_attach(sc);
896 /* Just fill in the entries for the device */
898 dev->init = lmc_init;
899 dev->type = ARPHRD_HDLC;
900 dev->hard_start_xmit = lmc_start_xmit;
901 dev->open = lmc_open;
902 dev->stop = lmc_close;
903 dev->get_stats = lmc_get_stats;
904 dev->do_ioctl = lmc_ioctl;
905 dev->set_config = lmc_set_config;
906 dev->tx_timeout = lmc_driver_timeout;
907 dev->watchdog_timeo = (HZ); /* 1 second */
910 * Why were we changing this???
911 dev->tx_queue_len = 100;
914 /* Init the spin lock so can call it latter */
916 spin_lock_init(&sc->lmc_lock);
918 printk ("%s: detected at %lx, irq %d\n", dev->name, ioaddr, dev->irq);
920 if (register_netdev (dev) != 0) {
921 printk (KERN_ERR "%s: register_netdev failed.\n", dev->name);
922 lmc_proto_detach(sc);
929 * Request the region of registers we need, so that
930 * later on, no one else will take our card away from
933 request_region (ioaddr, LMC_REG_RANGE, dev->name);
935 sc->lmc_cardtype = LMC_CARDTYPE_UNKNOWN;
936 sc->lmc_timing = LMC_CTL_CLOCK_SOURCE_EXT;
939 case PCI_PRODUCT_LMC_HSSI:
940 printk ("%s: LMC HSSI\n", dev->name);
941 sc->lmc_cardtype = LMC_CARDTYPE_HSSI;
942 sc->lmc_media = &lmc_hssi_media;
944 case PCI_PRODUCT_LMC_DS3:
945 printk ("%s: LMC DS3\n", dev->name);
946 sc->lmc_cardtype = LMC_CARDTYPE_DS3;
947 sc->lmc_media = &lmc_ds3_media;
949 case PCI_PRODUCT_LMC_SSI:
950 printk ("%s: LMC SSI\n", dev->name);
951 sc->lmc_cardtype = LMC_CARDTYPE_SSI;
952 sc->lmc_media = &lmc_ssi_media;
954 case PCI_PRODUCT_LMC_T1:
955 printk ("%s: LMC T1\n", dev->name);
956 sc->lmc_cardtype = LMC_CARDTYPE_T1;
957 sc->lmc_media = &lmc_t1_media;
960 printk (KERN_WARNING "%s: LMC UNKOWN CARD!\n", dev->name);
964 lmc_initcsrs (sc, dev->base_addr, 8);
966 lmc_gpio_mkinput (sc, 0xff);
967 sc->lmc_gpio = 0; /* drive no signals yet */
969 sc->lmc_media->defaults (sc);
971 sc->lmc_media->set_link_status (sc, LMC_LINK_UP);
973 /* verify that the PCI Sub System ID matches the Adapter Model number
974 * from the MII register
976 AdapModelNum = (lmc_mii_readreg (sc, 0, 3) & 0x3f0) >> 4;
978 if ((AdapModelNum == LMC_ADAP_T1
979 && subdevice == PCI_PRODUCT_LMC_T1) || /* detect LMC1200 */
980 (AdapModelNum == LMC_ADAP_SSI
981 && subdevice == PCI_PRODUCT_LMC_SSI) || /* detect LMC1000 */
982 (AdapModelNum == LMC_ADAP_DS3
983 && subdevice == PCI_PRODUCT_LMC_DS3) || /* detect LMC5245 */
984 (AdapModelNum == LMC_ADAP_HSSI
985 && subdevice == PCI_PRODUCT_LMC_HSSI))
986 { /* detect LMC5200 */
990 printk ("%s: Model number (%d) miscompare for PCI Subsystem ID = 0x%04x\n",
991 dev->name, AdapModelNum, subdevice);
997 LMC_CSR_WRITE (sc, csr_gp_timer, 0xFFFFFFFFUL);
999 sc->board_idx = board_idx;
1001 memset (&sc->stats, 0, sizeof (struct lmc_statistics));
1003 sc->stats.check = STATCHECK;
1004 sc->stats.version_size = (DRIVER_VERSION << 16) +
1005 sizeof (struct lmc_statistics);
1006 sc->stats.lmc_cardtype = sc->lmc_cardtype;
1009 sc->last_link_status = 0;
1011 lmc_trace(dev, "lmc_probe1 out");
1017 /* This is the entry point. This is what is called immediately. */
1018 /* This goes out and finds the card */
1020 int lmc_probe_fake(struct net_device *dev) /*fold00*/
1023 /* Return 1 to unloaded bogus device */
1027 int lmc_probe (struct net_device *dev) /*fold00*/
1030 unsigned long pci_ioaddr;
1031 unsigned int pci_irq_line;
1032 u16 vendor, subvendor, device, subdevice;
1035 struct pci_dev *pdev = NULL;
1037 /* Loop basically until we don't find anymore. */
1038 while ((pdev = pci_find_class (PCI_CLASS_NETWORK_ETHERNET << 8, pdev))) {
1039 if (pci_enable_device(pdev))
1042 vendor = pdev->vendor;
1043 device = pdev->device;
1044 pci_irq_line = pdev->irq;
1045 pci_ioaddr = pci_resource_start (pdev, 0);
1046 subvendor = pdev->subsystem_vendor;
1047 subdevice = pdev->subsystem_device;
1049 pci_set_master (pdev);
1052 * Make sure it's the correct card. CHECK SUBVENDOR ID!
1053 * There are lots of tulip's out there.
1054 * Also check the region of registers we will soon be
1055 * poking, to make sure no one else has reserved them.
1056 * This prevents taking someone else's device.
1058 * Check either the subvendor or the subdevice, some systems reverse
1059 * the setting in the bois, seems to be version and arch dependent?
1060 * Fix the two variables
1063 if (!(check_region (pci_ioaddr, LMC_REG_RANGE)) &&
1064 (vendor == CORRECT_VENDOR_ID) &&
1065 (device == CORRECT_DEV_ID) &&
1066 ((subvendor == PCI_VENDOR_LMC) || (subdevice == PCI_VENDOR_LMC))){
1067 struct net_device *cur, *prev = NULL;
1069 /* Fix the error, exchange the two values */
1070 if(subdevice == PCI_VENDOR_LMC){
1071 subdevice = subvendor;
1072 subvendor = PCI_VENDOR_LMC ;
1075 /* Make the call to actually setup this card */
1076 dev = lmc_probe1 (dev, pci_ioaddr, pci_irq_line,
1077 device, subdevice, cards_found);
1079 printk ("lmc_probe: lmc_probe1 failed\n");
1080 goto lmc_probe_next_card;
1082 /* insert the device into the chain of lmc devices */
1083 for (cur = Lmc_root_dev;
1085 cur = ((lmc_softc_t *) cur->priv)->next_module) {
1092 ((lmc_softc_t *) prev->priv)->next_module = dev;
1094 ((lmc_softc_t *) dev->priv)->next_module = NULL;
1097 foundaddr = dev->base_addr;
1102 lmc_probe_next_card:
1106 if (cards_found < 1)
1112 /* After this is called, packets can be sent.
1113 * Does not initialize the addresses
1115 static int lmc_open (struct net_device *dev) /*fold00*/
1117 lmc_softc_t *sc = dev->priv;
1119 lmc_trace(dev, "lmc_open in");
1121 lmc_led_on(sc, LMC_DS3_LED0);
1126 LMC_EVENT_LOG(LMC_EVENT_RESET1, LMC_CSR_READ (sc, csr_status), 0);
1127 LMC_EVENT_LOG(LMC_EVENT_RESET2,
1128 lmc_mii_readreg (sc, 0, 16),
1129 lmc_mii_readreg (sc, 0, 17));
1133 lmc_trace(dev, "lmc_open lmc_ok out");
1139 /* Since we have to use PCI bus, this should work on x86,alpha,ppc */
1140 if (request_irq (dev->irq, &lmc_interrupt, SA_SHIRQ, dev->name, dev)){
1141 printk(KERN_WARNING "%s: could not get irq: %d\n", dev->name, dev->irq);
1142 lmc_trace(dev, "lmc_open irq failed out");
1147 /* Assert Terminal Active */
1148 sc->lmc_miireg16 |= LMC_MII16_LED_ALL;
1149 sc->lmc_media->set_link_status (sc, LMC_LINK_UP);
1152 * reset to last state.
1154 sc->lmc_media->set_status (sc, NULL);
1156 /* setup default bits to be used in tulip_desc_t transmit descriptor
1158 sc->TxDescriptControlInit = (
1159 LMC_TDES_INTERRUPT_ON_COMPLETION
1160 | LMC_TDES_FIRST_SEGMENT
1161 | LMC_TDES_LAST_SEGMENT
1162 | LMC_TDES_SECOND_ADDR_CHAINED
1163 | LMC_TDES_DISABLE_PADDING
1166 if (sc->ictl.crc_length == LMC_CTL_CRC_LENGTH_16) {
1167 /* disable 32 bit CRC generated by ASIC */
1168 sc->TxDescriptControlInit |= LMC_TDES_ADD_CRC_DISABLE;
1170 sc->lmc_media->set_crc_length(sc, sc->ictl.crc_length);
1171 /* Acknoledge the Terminal Active and light LEDs */
1173 /* dev->flags |= IFF_UP; */
1177 dev->do_ioctl = lmc_ioctl;
1180 netif_start_queue(dev);
1182 sc->stats.tx_tbusy0++ ;
1187 * select what interrupts we want to get
1189 sc->lmc_intrmask = 0;
1190 /* Should be using the default interrupt mask defined in the .h file. */
1191 sc->lmc_intrmask |= (TULIP_STS_NORMALINTR
1194 | TULIP_STS_ABNRMLINTR
1195 | TULIP_STS_SYSERROR
1196 | TULIP_STS_TXSTOPPED
1197 | TULIP_STS_TXUNDERFLOW
1198 | TULIP_STS_RXSTOPPED
1201 LMC_CSR_WRITE (sc, csr_intr, sc->lmc_intrmask);
1203 sc->lmc_cmdmode |= TULIP_CMD_TXRUN;
1204 sc->lmc_cmdmode |= TULIP_CMD_RXRUN;
1205 LMC_CSR_WRITE (sc, csr_command, sc->lmc_cmdmode);
1207 sc->lmc_ok = 1; /* Run watchdog */
1210 * Set the if up now - pfb
1213 sc->last_link_status = 1;
1216 * Setup a timer for the watchdog on probe, and start it running.
1217 * Since lmc_ok == 0, it will be a NOP for now.
1219 init_timer (&sc->timer);
1220 sc->timer.expires = jiffies + HZ;
1221 sc->timer.data = (unsigned long) dev;
1222 sc->timer.function = &lmc_watchdog;
1223 add_timer (&sc->timer);
1225 lmc_trace(dev, "lmc_open out");
1230 /* Total reset to compensate for the AdTran DSU doing bad things
1234 static void lmc_running_reset (struct net_device *dev) /*fold00*/
1237 lmc_softc_t *sc = (lmc_softc_t *) dev->priv;
1239 lmc_trace(dev, "lmc_runnig_reset in");
1241 /* stop interrupts */
1242 /* Clear the interrupt mask */
1243 LMC_CSR_WRITE (sc, csr_intr, 0x00000000);
1248 /* sc->lmc_miireg16 |= LMC_MII16_LED_ALL; */
1249 sc->lmc_media->set_link_status (sc, 1);
1250 sc->lmc_media->set_status (sc, NULL);
1252 //dev->flags |= IFF_RUNNING;
1254 netif_wake_queue(dev);
1257 sc->stats.tx_tbusy0++ ;
1259 sc->lmc_intrmask = TULIP_DEFAULT_INTR_MASK;
1260 LMC_CSR_WRITE (sc, csr_intr, sc->lmc_intrmask);
1262 sc->lmc_cmdmode |= (TULIP_CMD_TXRUN | TULIP_CMD_RXRUN);
1263 LMC_CSR_WRITE (sc, csr_command, sc->lmc_cmdmode);
1265 lmc_trace(dev, "lmc_runnin_reset_out");
1269 /* This is what is called when you ifconfig down a device.
1270 * This disables the timer for the watchdog and keepalives,
1271 * and disables the irq for dev.
1273 static int lmc_close (struct net_device *dev) /*fold00*/
1275 /* not calling release_region() as we should */
1278 lmc_trace(dev, "lmc_close in");
1282 sc->lmc_media->set_link_status (sc, 0);
1283 del_timer (&sc->timer);
1284 lmc_proto_close(sc);
1287 lmc_trace(dev, "lmc_close out");
1292 /* Ends the transfer of packets */
1293 /* When the interface goes down, this is called */
1294 static int lmc_ifdown (struct net_device *dev) /*fold00*/
1296 lmc_softc_t *sc = dev->priv;
1300 lmc_trace(dev, "lmc_ifdown in");
1302 /* Don't let anything else go on right now */
1304 netif_stop_queue(dev);
1305 sc->stats.tx_tbusy1++ ;
1307 /* stop interrupts */
1308 /* Clear the interrupt mask */
1309 LMC_CSR_WRITE (sc, csr_intr, 0x00000000);
1311 /* Stop Tx and Rx on the chip */
1312 csr6 = LMC_CSR_READ (sc, csr_command);
1313 csr6 &= ~LMC_DEC_ST; /* Turn off the Transmission bit */
1314 csr6 &= ~LMC_DEC_SR; /* Turn off the Receive bit */
1315 LMC_CSR_WRITE (sc, csr_command, csr6);
1317 dev->flags &= ~IFF_RUNNING;
1319 sc->stats.rx_missed_errors +=
1320 LMC_CSR_READ (sc, csr_missed_frames) & 0xffff;
1322 /* release the interrupt */
1323 if(sc->got_irq == 1){
1324 free_irq (dev->irq, dev);
1328 /* free skbuffs in the Rx queue */
1329 for (i = 0; i < LMC_RXDESCS; i++)
1331 struct sk_buff *skb = sc->lmc_rxq[i];
1333 sc->lmc_rxring[i].status = 0;
1334 sc->lmc_rxring[i].length = 0;
1335 sc->lmc_rxring[i].buffer1 = 0xDEADBEEF;
1338 sc->lmc_rxq[i] = NULL;
1341 for (i = 0; i < LMC_TXDESCS; i++)
1343 if (sc->lmc_txq[i] != NULL)
1344 dev_kfree_skb(sc->lmc_txq[i]);
1345 sc->lmc_txq[i] = NULL;
1348 lmc_led_off (sc, LMC_MII16_LED_ALL);
1350 netif_wake_queue(dev);
1351 sc->stats.tx_tbusy0++ ;
1353 lmc_trace(dev, "lmc_ifdown out");
1359 /* Interrupt handling routine. This will take an incoming packet, or clean
1360 * up after a trasmit.
1362 static irqreturn_t lmc_interrupt (int irq, void *dev_instance, struct pt_regs *regs) /*fold00*/
1364 struct net_device *dev = (struct net_device *) dev_instance;
1371 int max_work = LMC_RXDESCS;
1374 lmc_trace(dev, "lmc_interrupt in");
1378 spin_lock(&sc->lmc_lock);
1381 * Read the csr to find what interrupts we have (if any)
1383 csr = LMC_CSR_READ (sc, csr_status);
1386 * Make sure this is our interrupt
1388 if ( ! (csr & sc->lmc_intrmask)) {
1389 goto lmc_int_fail_out;
1394 /* always go through this loop at least once */
1395 while (csr & sc->lmc_intrmask) {
1399 * Clear interrupt bits, we handle all case below
1401 LMC_CSR_WRITE (sc, csr_status, csr);
1405 * - Transmit process timed out CSR5<1>
1406 * - Transmit jabber timeout CSR5<3>
1407 * - Transmit underflow CSR5<5>
1408 * - Transmit Receiver buffer unavailable CSR5<7>
1409 * - Receive process stopped CSR5<8>
1410 * - Receive watchdog timeout CSR5<9>
1411 * - Early transmit interrupt CSR5<10>
1413 * Is this really right? Should we do a running reset for jabber?
1414 * (being a WAN card and all)
1416 if (csr & TULIP_STS_ABNRMLINTR){
1417 lmc_running_reset (dev);
1421 if (csr & TULIP_STS_RXINTR){
1422 lmc_trace(dev, "rx interrupt");
1426 if (csr & (TULIP_STS_TXINTR | TULIP_STS_TXNOBUF | TULIP_STS_TXSTOPPED)) {
1429 /* reset the transmit timeout detection flag -baz */
1430 sc->stats.tx_NoCompleteCnt = 0;
1432 badtx = sc->lmc_taint_tx;
1433 i = badtx % LMC_TXDESCS;
1435 while ((badtx < sc->lmc_next_tx)) {
1436 stat = sc->lmc_txring[i].status;
1438 LMC_EVENT_LOG (LMC_EVENT_XMTINT, stat,
1439 sc->lmc_txring[i].length);
1441 * If bit 31 is 1 the tulip owns it break out of the loop
1443 if (stat & 0x80000000)
1446 n_compl++ ; /* i.e., have an empty slot in ring */
1448 * If we have no skbuff or have cleared it
1449 * Already continue to the next buffer
1451 if (sc->lmc_txq[i] == NULL)
1455 * Check the total error summary to look for any errors
1457 if (stat & 0x8000) {
1458 sc->stats.tx_errors++;
1460 sc->stats.tx_aborted_errors++;
1462 sc->stats.tx_carrier_errors++;
1464 sc->stats.tx_window_errors++;
1466 sc->stats.tx_fifo_errors++;
1470 sc->stats.tx_bytes += sc->lmc_txring[i].length & 0x7ff;
1472 sc->stats.tx_packets++;
1475 // dev_kfree_skb(sc->lmc_txq[i]);
1476 dev_kfree_skb_irq(sc->lmc_txq[i]);
1480 i = badtx % LMC_TXDESCS;
1483 if (sc->lmc_next_tx - badtx > LMC_TXDESCS)
1485 printk ("%s: out of sync pointer\n", dev->name);
1486 badtx += LMC_TXDESCS;
1488 LMC_EVENT_LOG(LMC_EVENT_TBUSY0, n_compl, 0);
1490 netif_wake_queue(dev);
1491 sc->stats.tx_tbusy0++ ;
1495 sc->stats.dirtyTx = badtx;
1496 sc->stats.lmc_next_tx = sc->lmc_next_tx;
1497 sc->stats.lmc_txfull = sc->lmc_txfull;
1499 sc->lmc_taint_tx = badtx;
1502 * Why was there a break here???
1504 } /* end handle transmit interrupt */
1506 if (csr & TULIP_STS_SYSERROR) {
1508 printk (KERN_WARNING "%s: system bus error csr: %#8.8x\n", dev->name, csr);
1509 error = csr>>23 & 0x7;
1512 printk(KERN_WARNING "%s: Parity Fault (bad)\n", dev->name);
1515 printk(KERN_WARNING "%s: Master Abort (naughty)\n", dev->name);
1518 printk(KERN_WARNING "%s: Target Abort (not so naughty)\n", dev->name);
1521 printk(KERN_WARNING "%s: This bus error code was supposed to be reserved!\n", dev->name);
1525 LMC_EVENT_LOG(LMC_EVENT_RESET1, LMC_CSR_READ (sc, csr_status), 0);
1526 LMC_EVENT_LOG(LMC_EVENT_RESET2,
1527 lmc_mii_readreg (sc, 0, 16),
1528 lmc_mii_readreg (sc, 0, 17));
1537 * Get current csr status to make sure
1538 * we've cleared all interrupts
1540 csr = LMC_CSR_READ (sc, csr_status);
1541 } /* end interrupt loop */
1542 LMC_EVENT_LOG(LMC_EVENT_INT, firstcsr, csr);
1546 spin_unlock(&sc->lmc_lock);
1548 lmc_trace(dev, "lmc_interrupt out");
1549 return IRQ_RETVAL(handled);
1552 static int lmc_start_xmit (struct sk_buff *skb, struct net_device *dev) /*fold00*/
1558 unsigned long flags;
1560 lmc_trace(dev, "lmc_start_xmit in");
1564 spin_lock_irqsave(&sc->lmc_lock, flags);
1566 /* normal path, tbusy known to be zero */
1568 entry = sc->lmc_next_tx % LMC_TXDESCS;
1570 sc->lmc_txq[entry] = skb;
1571 sc->lmc_txring[entry].buffer1 = virt_to_bus (skb->data);
1573 LMC_CONSOLE_LOG("xmit", skb->data, skb->len);
1576 /* If the queue is less than half full, don't interrupt */
1577 if (sc->lmc_next_tx - sc->lmc_taint_tx < LMC_TXDESCS / 2)
1579 /* Do not interrupt on completion of this packet */
1581 netif_wake_queue(dev);
1583 else if (sc->lmc_next_tx - sc->lmc_taint_tx == LMC_TXDESCS / 2)
1585 /* This generates an interrupt on completion of this packet */
1587 netif_wake_queue(dev);
1589 else if (sc->lmc_next_tx - sc->lmc_taint_tx < LMC_TXDESCS - 1)
1591 /* Do not interrupt on completion of this packet */
1593 netif_wake_queue(dev);
1597 /* This generates an interrupt on completion of this packet */
1600 netif_stop_queue(dev);
1603 flag = LMC_TDES_INTERRUPT_ON_COMPLETION;
1605 if (sc->lmc_next_tx - sc->lmc_taint_tx >= LMC_TXDESCS - 1)
1606 { /* ring full, go busy */
1608 netif_stop_queue(dev);
1609 sc->stats.tx_tbusy1++ ;
1610 LMC_EVENT_LOG(LMC_EVENT_TBUSY1, entry, 0);
1615 if (entry == LMC_TXDESCS - 1) /* last descriptor in ring */
1616 flag |= LMC_TDES_END_OF_RING; /* flag as such for Tulip */
1618 /* don't pad small packets either */
1619 flag = sc->lmc_txring[entry].length = (skb->len) | flag |
1620 sc->TxDescriptControlInit;
1622 /* set the transmit timeout flag to be checked in
1623 * the watchdog timer handler. -baz
1626 sc->stats.tx_NoCompleteCnt++;
1629 /* give ownership to the chip */
1630 LMC_EVENT_LOG(LMC_EVENT_XMT, flag, entry);
1631 sc->lmc_txring[entry].status = 0x80000000;
1634 LMC_CSR_WRITE (sc, csr_txpoll, 0);
1636 dev->trans_start = jiffies;
1638 spin_unlock_irqrestore(&sc->lmc_lock, flags);
1640 lmc_trace(dev, "lmc_start_xmit_out");
1645 static int lmc_rx (struct net_device *dev) /*fold00*/
1649 int rx_work_limit = LMC_RXDESCS;
1650 unsigned int next_rx;
1651 int rxIntLoopCnt; /* debug -baz */
1652 int localLengthErrCnt = 0;
1654 struct sk_buff *skb, *nsb;
1657 lmc_trace(dev, "lmc_rx in");
1661 lmc_led_on(sc, LMC_DS3_LED3);
1663 rxIntLoopCnt = 0; /* debug -baz */
1665 i = sc->lmc_next_rx % LMC_RXDESCS;
1666 next_rx = sc->lmc_next_rx;
1668 while (((stat = sc->lmc_rxring[i].status) & LMC_RDES_OWN_BIT) != DESC_OWNED_BY_DC21X4)
1670 rxIntLoopCnt++; /* debug -baz */
1671 len = ((stat & LMC_RDES_FRAME_LENGTH) >> RDES_FRAME_LENGTH_BIT_NUMBER);
1672 if ((stat & 0x0300) != 0x0300) { /* Check first segment and last segment */
1673 if ((stat & 0x0000ffff) != 0x7fff) {
1674 /* Oversized frame */
1675 sc->stats.rx_length_errors++;
1680 if(stat & 0x00000008){ /* Catch a dribbling bit error */
1681 sc->stats.rx_errors++;
1682 sc->stats.rx_frame_errors++;
1687 if(stat & 0x00000004){ /* Catch a CRC error by the Xilinx */
1688 sc->stats.rx_errors++;
1689 sc->stats.rx_crc_errors++;
1694 if (len > LMC_PKT_BUF_SZ){
1695 sc->stats.rx_length_errors++;
1696 localLengthErrCnt++;
1700 if (len < sc->lmc_crcSize + 2) {
1701 sc->stats.rx_length_errors++;
1702 sc->stats.rx_SmallPktCnt++;
1703 localLengthErrCnt++;
1707 if(stat & 0x00004000){
1708 printk(KERN_WARNING "%s: Receiver descriptor error, receiver out of sync?\n", dev->name);
1711 len -= sc->lmc_crcSize;
1713 skb = sc->lmc_rxq[i];
1716 * We ran out of memory at some point
1717 * just allocate an skb buff and continue.
1721 nsb = dev_alloc_skb (LMC_PKT_BUF_SZ + 2);
1723 sc->lmc_rxq[i] = nsb;
1725 sc->lmc_rxring[i].buffer1 = virt_to_bus (nsb->tail);
1727 sc->failed_recv_alloc = 1;
1731 dev->last_rx = jiffies;
1732 sc->stats.rx_packets++;
1733 sc->stats.rx_bytes += len;
1735 LMC_CONSOLE_LOG("recv", skb->data, len);
1738 * I'm not sure of the sanity of this
1739 * Packets could be arriving at a constant
1740 * 44.210mbits/sec and we're going to copy
1741 * them into a new buffer??
1744 if(len > (LMC_MTU - (LMC_MTU>>2))){ /* len > LMC_MTU * 0.75 */
1746 * If it's a large packet don't copy it just hand it up
1750 sc->lmc_rxq[i] = 0x0;
1751 sc->lmc_rxring[i].buffer1 = 0x0;
1754 skb->protocol = lmc_proto_type(sc, skb);
1755 skb->protocol = htons(ETH_P_WAN_PPP);
1756 skb->mac.raw = skb->data;
1757 // skb->nh.raw = skb->data;
1759 lmc_proto_netif(sc, skb);
1762 * This skb will be destroyed by the upper layers, make a new one
1764 nsb = dev_alloc_skb (LMC_PKT_BUF_SZ + 2);
1766 sc->lmc_rxq[i] = nsb;
1768 sc->lmc_rxring[i].buffer1 = virt_to_bus (nsb->tail);
1769 /* Transferred to 21140 below */
1773 * We've run out of memory, stop trying to allocate
1774 * memory and exit the interrupt handler
1776 * The chip may run out of receivers and stop
1777 * in which care we'll try to allocate the buffer
1778 * again. (once a second)
1780 sc->stats.rx_BuffAllocErr++;
1781 LMC_EVENT_LOG(LMC_EVENT_RCVINT, stat, len);
1782 sc->failed_recv_alloc = 1;
1783 goto skip_out_of_mem;
1787 nsb = dev_alloc_skb(len);
1789 goto give_it_anyways;
1791 memcpy(skb_put(nsb, len), skb->data, len);
1793 nsb->protocol = lmc_proto_type(sc, skb);
1794 nsb->mac.raw = nsb->data;
1795 // nsb->nh.raw = nsb->data;
1797 lmc_proto_netif(sc, nsb);
1801 LMC_EVENT_LOG(LMC_EVENT_RCVINT, stat, len);
1802 sc->lmc_rxring[i].status = DESC_OWNED_BY_DC21X4;
1805 i = sc->lmc_next_rx % LMC_RXDESCS;
1807 if (rx_work_limit < 0)
1811 /* detect condition for LMC1000 where DSU cable attaches and fills
1812 * descriptors with bogus packets
1814 if (localLengthErrCnt > LMC_RXDESCS - 3) {
1815 sc->stats.rx_BadPktSurgeCnt++;
1816 LMC_EVENT_LOG(LMC_EVENT_BADPKTSURGE,
1818 sc->stats.rx_BadPktSurgeCnt);
1821 /* save max count of receive descriptors serviced */
1822 if (rxIntLoopCnt > sc->stats.rxIntLoopCnt) {
1823 sc->stats.rxIntLoopCnt = rxIntLoopCnt; /* debug -baz */
1827 if (rxIntLoopCnt == 0)
1829 for (i = 0; i < LMC_RXDESCS; i++)
1831 if ((sc->lmc_rxring[i].status & LMC_RDES_OWN_BIT)
1832 != DESC_OWNED_BY_DC21X4)
1837 LMC_EVENT_LOG(LMC_EVENT_RCVEND, rxIntLoopCnt, 0);
1842 lmc_led_off(sc, LMC_DS3_LED3);
1846 lmc_trace(dev, "lmc_rx out");
1851 static struct net_device_stats *lmc_get_stats (struct net_device *dev) /*fold00*/
1854 unsigned long flags;
1856 lmc_trace(dev, "lmc_get_stats in");
1860 spin_lock_irqsave(&sc->lmc_lock, flags);
1862 sc->stats.rx_missed_errors += LMC_CSR_READ (sc, csr_missed_frames) & 0xffff;
1864 spin_unlock_irqrestore(&sc->lmc_lock, flags);
1866 lmc_trace(dev, "lmc_get_stats out");
1868 return (struct net_device_stats *) &sc->stats;
1871 static int __init init_lmc(void)
1873 printk ("lmc: module loaded\n");
1875 /* Have lmc_probe search for all the cards, and allocate devices */
1876 if (lmc_probe (NULL) < 0)
1882 static void __exit exit_lmc(void)
1884 struct net_device *dev, *next;
1887 /* we have no pointer to our devices, since they are all dynamically
1888 * allocated. So, here we loop through all the network devices
1889 * looking for ours. When found, dispose of them properly.
1892 for (dev = Lmc_root_dev;
1897 next = ((lmc_softc_t *) dev->priv)->next_module; /* get it now before we deallocate it */
1898 printk ("%s: removing...\n", dev->name);
1900 /* close the syncppp stuff, and release irq. Close is run on unreg net */
1904 lmc_proto_detach(sc);
1906 /* Remove the device from the linked list */
1907 unregister_netdev (dev);
1909 /* Let go of the io region */;
1910 release_region (dev->base_addr, LMC_REG_RANGE);
1912 /* free our allocated structures. */
1916 kfree ((struct ppp_device *) dev);
1921 Lmc_root_dev = NULL;
1922 printk ("lmc module unloaded\n");
1925 module_init(init_lmc);
1926 module_exit(exit_lmc);
1928 unsigned lmc_mii_readreg (lmc_softc_t * const sc, unsigned devaddr, unsigned regno) /*fold00*/
1931 int command = (0xf6 << 10) | (devaddr << 5) | regno;
1934 lmc_trace(sc->lmc_device, "lmc_mii_readreg in");
1938 lmc_trace(sc->lmc_device, "lmc_mii_readreg: done sync");
1940 for (i = 15; i >= 0; i--)
1942 int dataval = (command & (1 << i)) ? 0x20000 : 0;
1944 LMC_CSR_WRITE (sc, csr_9, dataval);
1946 /* __SLOW_DOWN_IO; */
1947 LMC_CSR_WRITE (sc, csr_9, dataval | 0x10000);
1949 /* __SLOW_DOWN_IO; */
1952 lmc_trace(sc->lmc_device, "lmc_mii_readreg: done1");
1954 for (i = 19; i > 0; i--)
1956 LMC_CSR_WRITE (sc, csr_9, 0x40000);
1958 /* __SLOW_DOWN_IO; */
1959 retval = (retval << 1) | ((LMC_CSR_READ (sc, csr_9) & 0x80000) ? 1 : 0);
1960 LMC_CSR_WRITE (sc, csr_9, 0x40000 | 0x10000);
1962 /* __SLOW_DOWN_IO; */
1965 lmc_trace(sc->lmc_device, "lmc_mii_readreg out");
1967 return (retval >> 1) & 0xffff;
1970 void lmc_mii_writereg (lmc_softc_t * const sc, unsigned devaddr, unsigned regno, unsigned data) /*fold00*/
1973 int command = (0x5002 << 16) | (devaddr << 23) | (regno << 18) | data;
1975 lmc_trace(sc->lmc_device, "lmc_mii_writereg in");
1984 if (command & (1 << i))
1989 LMC_CSR_WRITE (sc, csr_9, datav);
1991 /* __SLOW_DOWN_IO; */
1992 LMC_CSR_WRITE (sc, csr_9, (datav | 0x10000));
1994 /* __SLOW_DOWN_IO; */
2001 LMC_CSR_WRITE (sc, csr_9, 0x40000);
2003 /* __SLOW_DOWN_IO; */
2004 LMC_CSR_WRITE (sc, csr_9, 0x50000);
2006 /* __SLOW_DOWN_IO; */
2010 lmc_trace(sc->lmc_device, "lmc_mii_writereg out");
2013 static void lmc_softreset (lmc_softc_t * const sc) /*fold00*/
2017 lmc_trace(sc->lmc_device, "lmc_softreset in");
2019 /* Initialize the receive rings and buffers. */
2021 sc->lmc_next_rx = 0;
2022 sc->lmc_next_tx = 0;
2023 sc->lmc_taint_rx = 0;
2024 sc->lmc_taint_tx = 0;
2027 * Setup each one of the receiver buffers
2028 * allocate an skbuff for each one, setup the descriptor table
2029 * and point each buffer at the next one
2032 for (i = 0; i < LMC_RXDESCS; i++)
2034 struct sk_buff *skb;
2036 if (sc->lmc_rxq[i] == NULL)
2038 skb = dev_alloc_skb (LMC_PKT_BUF_SZ + 2);
2040 printk(KERN_WARNING "%s: Failed to allocate receiver ring, will try again\n", sc->name);
2041 sc->failed_ring = 1;
2045 sc->lmc_rxq[i] = skb;
2050 skb = sc->lmc_rxq[i];
2053 skb->dev = sc->lmc_device;
2055 /* owned by 21140 */
2056 sc->lmc_rxring[i].status = 0x80000000;
2058 /* used to be PKT_BUF_SZ now uses skb since we lose some to head room */
2059 sc->lmc_rxring[i].length = skb->end - skb->data;
2061 /* use to be tail which is dumb since you're thinking why write
2062 * to the end of the packj,et but since there's nothing there tail == data
2064 sc->lmc_rxring[i].buffer1 = virt_to_bus (skb->data);
2066 /* This is fair since the structure is static and we have the next address */
2067 sc->lmc_rxring[i].buffer2 = virt_to_bus (&sc->lmc_rxring[i + 1]);
2074 sc->lmc_rxring[i - 1].length |= 0x02000000; /* Set end of buffers flag */
2075 sc->lmc_rxring[i - 1].buffer2 = virt_to_bus (&sc->lmc_rxring[0]); /* Point back to the start */
2076 LMC_CSR_WRITE (sc, csr_rxlist, virt_to_bus (sc->lmc_rxring)); /* write base address */
2079 /* Initialize the transmit rings and buffers */
2080 for (i = 0; i < LMC_TXDESCS; i++)
2082 if (sc->lmc_txq[i] != NULL){ /* have buffer */
2083 dev_kfree_skb(sc->lmc_txq[i]); /* free it */
2084 sc->stats.tx_dropped++; /* We just dropped a packet */
2087 sc->lmc_txring[i].status = 0x00000000;
2088 sc->lmc_txring[i].buffer2 = virt_to_bus (&sc->lmc_txring[i + 1]);
2090 sc->lmc_txring[i - 1].buffer2 = virt_to_bus (&sc->lmc_txring[0]);
2091 LMC_CSR_WRITE (sc, csr_txlist, virt_to_bus (sc->lmc_txring));
2093 lmc_trace(sc->lmc_device, "lmc_softreset out");
2096 static int lmc_set_config(struct net_device *dev, struct ifmap *map) /*fold00*/
2098 lmc_trace(dev, "lmc_set_config in");
2099 lmc_trace(dev, "lmc_set_config out");
2103 void lmc_gpio_mkinput(lmc_softc_t * const sc, u_int32_t bits) /*fold00*/
2105 lmc_trace(sc->lmc_device, "lmc_gpio_mkinput in");
2106 sc->lmc_gpio_io &= ~bits;
2107 LMC_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET | (sc->lmc_gpio_io));
2108 lmc_trace(sc->lmc_device, "lmc_gpio_mkinput out");
2111 void lmc_gpio_mkoutput(lmc_softc_t * const sc, u_int32_t bits) /*fold00*/
2113 lmc_trace(sc->lmc_device, "lmc_gpio_mkoutput in");
2114 sc->lmc_gpio_io |= bits;
2115 LMC_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET | (sc->lmc_gpio_io));
2116 lmc_trace(sc->lmc_device, "lmc_gpio_mkoutput out");
2119 void lmc_led_on(lmc_softc_t * const sc, u_int32_t led) /*fold00*/
2121 lmc_trace(sc->lmc_device, "lmc_led_on in");
2122 if((~sc->lmc_miireg16) & led){ /* Already on! */
2123 lmc_trace(sc->lmc_device, "lmc_led_on aon out");
2127 sc->lmc_miireg16 &= ~led;
2128 lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
2129 lmc_trace(sc->lmc_device, "lmc_led_on out");
2132 void lmc_led_off(lmc_softc_t * const sc, u_int32_t led) /*fold00*/
2134 lmc_trace(sc->lmc_device, "lmc_led_off in");
2135 if(sc->lmc_miireg16 & led){ /* Already set don't do anything */
2136 lmc_trace(sc->lmc_device, "lmc_led_off aoff out");
2140 sc->lmc_miireg16 |= led;
2141 lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
2142 lmc_trace(sc->lmc_device, "lmc_led_off out");
2145 static void lmc_reset(lmc_softc_t * const sc) /*fold00*/
2147 lmc_trace(sc->lmc_device, "lmc_reset in");
2148 sc->lmc_miireg16 |= LMC_MII16_FIFO_RESET;
2149 lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
2151 sc->lmc_miireg16 &= ~LMC_MII16_FIFO_RESET;
2152 lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
2155 * make some of the GPIO pins be outputs
2157 lmc_gpio_mkoutput(sc, LMC_GEP_RESET);
2160 * RESET low to force state reset. This also forces
2161 * the transmitter clock to be internal, but we expect to reset
2162 * that later anyway.
2164 sc->lmc_gpio &= ~(LMC_GEP_RESET);
2165 LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
2168 * hold for more than 10 microseconds
2173 * stop driving Xilinx-related signals
2175 lmc_gpio_mkinput(sc, LMC_GEP_RESET);
2178 * Call media specific init routine
2180 sc->lmc_media->init(sc);
2182 sc->stats.resetCount++;
2183 lmc_trace(sc->lmc_device, "lmc_reset out");
2186 static void lmc_dec_reset(lmc_softc_t * const sc) /*fold00*/
2189 lmc_trace(sc->lmc_device, "lmc_dec_reset in");
2192 * disable all interrupts
2194 sc->lmc_intrmask = 0;
2195 LMC_CSR_WRITE(sc, csr_intr, sc->lmc_intrmask);
2198 * Reset the chip with a software reset command.
2199 * Wait 10 microseconds (actually 50 PCI cycles but at
2200 * 33MHz that comes to two microseconds but wait a
2201 * bit longer anyways)
2203 LMC_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
2206 sc->lmc_busmode = LMC_CSR_READ(sc, csr_busmode);
2207 sc->lmc_busmode = 0x00100000;
2208 sc->lmc_busmode &= ~TULIP_BUSMODE_SWRESET;
2209 LMC_CSR_WRITE(sc, csr_busmode, sc->lmc_busmode);
2211 sc->lmc_cmdmode = LMC_CSR_READ(sc, csr_command);
2215 * no ethernet address in frames we write
2216 * disable padding (txdesc, padding disable)
2217 * ignore runt frames (rdes0 bit 15)
2218 * no receiver watchdog or transmitter jabber timer
2219 * (csr15 bit 0,14 == 1)
2220 * if using 16-bit CRC, turn off CRC (trans desc, crc disable)
2223 sc->lmc_cmdmode |= ( TULIP_CMD_PROMISCUOUS
2224 | TULIP_CMD_FULLDUPLEX
2225 | TULIP_CMD_PASSBADPKT
2226 | TULIP_CMD_NOHEARTBEAT
2227 | TULIP_CMD_PORTSELECT
2228 | TULIP_CMD_RECEIVEALL
2229 | TULIP_CMD_MUSTBEONE
2231 sc->lmc_cmdmode &= ~( TULIP_CMD_OPERMODE
2232 | TULIP_CMD_THRESHOLDCTL
2233 | TULIP_CMD_STOREFWD
2234 | TULIP_CMD_TXTHRSHLDCTL
2237 LMC_CSR_WRITE(sc, csr_command, sc->lmc_cmdmode);
2240 * disable receiver watchdog and transmit jabber
2242 val = LMC_CSR_READ(sc, csr_sia_general);
2243 val |= (TULIP_WATCHDOG_TXDISABLE | TULIP_WATCHDOG_RXDISABLE);
2244 LMC_CSR_WRITE(sc, csr_sia_general, val);
2246 lmc_trace(sc->lmc_device, "lmc_dec_reset out");
2249 static void lmc_initcsrs(lmc_softc_t * const sc, lmc_csrptr_t csr_base, /*fold00*/
2252 lmc_trace(sc->lmc_device, "lmc_initcsrs in");
2253 sc->lmc_csrs.csr_busmode = csr_base + 0 * csr_size;
2254 sc->lmc_csrs.csr_txpoll = csr_base + 1 * csr_size;
2255 sc->lmc_csrs.csr_rxpoll = csr_base + 2 * csr_size;
2256 sc->lmc_csrs.csr_rxlist = csr_base + 3 * csr_size;
2257 sc->lmc_csrs.csr_txlist = csr_base + 4 * csr_size;
2258 sc->lmc_csrs.csr_status = csr_base + 5 * csr_size;
2259 sc->lmc_csrs.csr_command = csr_base + 6 * csr_size;
2260 sc->lmc_csrs.csr_intr = csr_base + 7 * csr_size;
2261 sc->lmc_csrs.csr_missed_frames = csr_base + 8 * csr_size;
2262 sc->lmc_csrs.csr_9 = csr_base + 9 * csr_size;
2263 sc->lmc_csrs.csr_10 = csr_base + 10 * csr_size;
2264 sc->lmc_csrs.csr_11 = csr_base + 11 * csr_size;
2265 sc->lmc_csrs.csr_12 = csr_base + 12 * csr_size;
2266 sc->lmc_csrs.csr_13 = csr_base + 13 * csr_size;
2267 sc->lmc_csrs.csr_14 = csr_base + 14 * csr_size;
2268 sc->lmc_csrs.csr_15 = csr_base + 15 * csr_size;
2269 lmc_trace(sc->lmc_device, "lmc_initcsrs out");
2272 static void lmc_driver_timeout(struct net_device *dev) { /*fold00*/
2275 unsigned long flags;
2277 lmc_trace(dev, "lmc_driver_timeout in");
2281 spin_lock_irqsave(&sc->lmc_lock, flags);
2283 printk("%s: Xmitter busy|\n", dev->name);
2285 sc->stats.tx_tbusy_calls++ ;
2286 if (jiffies - dev->trans_start < TX_TIMEOUT) {
2291 * Chip seems to have locked up
2293 * This whips out all our decriptor
2294 * table and starts from scartch
2297 LMC_EVENT_LOG(LMC_EVENT_XMTPRCTMO,
2298 LMC_CSR_READ (sc, csr_status),
2299 sc->stats.tx_ProcTimeout);
2301 lmc_running_reset (dev);
2303 LMC_EVENT_LOG(LMC_EVENT_RESET1, LMC_CSR_READ (sc, csr_status), 0);
2304 LMC_EVENT_LOG(LMC_EVENT_RESET2,
2305 lmc_mii_readreg (sc, 0, 16),
2306 lmc_mii_readreg (sc, 0, 17));
2308 /* restart the tx processes */
2309 csr6 = LMC_CSR_READ (sc, csr_command);
2310 LMC_CSR_WRITE (sc, csr_command, csr6 | 0x0002);
2311 LMC_CSR_WRITE (sc, csr_command, csr6 | 0x2002);
2313 /* immediate transmit */
2314 LMC_CSR_WRITE (sc, csr_txpoll, 0);
2316 sc->stats.tx_errors++;
2317 sc->stats.tx_ProcTimeout++; /* -baz */
2319 dev->trans_start = jiffies;
2323 spin_unlock_irqrestore(&sc->lmc_lock, flags);
2325 lmc_trace(dev, "lmc_driver_timout out");
2330 int lmc_setup(void) { /*FOLD00*/
2331 return lmc_probe(NULL);