ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / tulip / xircom_cb.c
1 /*
2  * xircom_cb: A driver for the (tulip-like) Xircom Cardbus ethernet cards 
3  *
4  * This software is (C) by the respective authors, and licensed under the GPL
5  * License.
6  *
7  * Written by Arjan van de Ven for Red Hat, Inc.
8  * Based on work by Jeff Garzik, Doug Ledford and Donald Becker 
9  *
10  *      This software may be used and distributed according to the terms
11  *      of the GNU General Public License, incorporated herein by reference.
12  *
13  *
14  *      $Id: xircom_cb.c,v 1.33 2001/03/19 14:02:07 arjanv Exp $
15  */
16
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/ioport.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/pci.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/skbuff.h>
28 #include <linux/delay.h>
29 #include <linux/init.h>
30 #include <linux/ethtool.h>
31
32 #include <asm/uaccess.h>
33 #include <asm/bitops.h>
34 #include <asm/io.h>
35
36 #ifdef DEBUG
37 #define enter(x)   printk("Enter: %s, %s line %i\n",x,__FILE__,__LINE__)
38 #define leave(x)   printk("Leave: %s, %s line %i\n",x,__FILE__,__LINE__)
39 #else
40 #define enter(x)   do {} while (0)
41 #define leave(x)   do {} while (0)
42 #endif
43
44
45 MODULE_DESCRIPTION("Xircom Cardbus ethernet driver");
46 MODULE_AUTHOR("Arjan van de Ven <arjanv@redhat.com>");
47 MODULE_LICENSE("GPL");
48
49
50
51 /* IO registers on the card, offsets */
52 #define CSR0    0x00
53 #define CSR1    0x08
54 #define CSR2    0x10
55 #define CSR3    0x18
56 #define CSR4    0x20
57 #define CSR5    0x28
58 #define CSR6    0x30
59 #define CSR7    0x38
60 #define CSR8    0x40
61 #define CSR9    0x48
62 #define CSR10   0x50
63 #define CSR11   0x58
64 #define CSR12   0x60
65 #define CSR13   0x68
66 #define CSR14   0x70
67 #define CSR15   0x78
68 #define CSR16   0x80
69
70 /* PCI registers */
71 #define PCI_POWERMGMT   0x40
72
73 /* Offsets of the buffers within the descriptor pages, in bytes */
74
75 #define NUMDESCRIPTORS 4
76
77 static int bufferoffsets[NUMDESCRIPTORS] = {128,2048,4096,6144};
78
79
80 struct xircom_private {
81         /* Send and receive buffers, kernel-addressable and dma addressable forms */
82
83         unsigned int *rx_buffer;
84         unsigned int *tx_buffer;
85
86         dma_addr_t rx_dma_handle;
87         dma_addr_t tx_dma_handle;
88
89         struct sk_buff *tx_skb[4];
90
91         unsigned long io_port;
92         int open;
93         
94         /* transmit_used is the rotating counter that indicates which transmit
95            descriptor has to be used next */
96         int transmit_used;
97
98         /* Spinlock to serialize register operations.
99            It must be helt while manipulating the following registers:
100            CSR0, CSR6, CSR7, CSR9, CSR10, CSR15
101          */
102         spinlock_t lock;
103
104
105         struct pci_dev *pdev;
106         struct net_device *dev;
107         struct net_device_stats stats;
108 };
109
110
111 /* Function prototypes */
112 static int xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id);
113 static void xircom_remove(struct pci_dev *pdev);
114 static irqreturn_t xircom_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
115 static int xircom_start_xmit(struct sk_buff *skb, struct net_device *dev);
116 static int xircom_open(struct net_device *dev);
117 static int xircom_close(struct net_device *dev);
118 static void xircom_up(struct xircom_private *card);
119 static struct net_device_stats *xircom_get_stats(struct net_device *dev);
120
121 static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset);
122 static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset);
123 static void read_mac_address(struct xircom_private *card);
124 static void transceiver_voodoo(struct xircom_private *card);
125 static void initialize_card(struct xircom_private *card);
126 static void trigger_transmit(struct xircom_private *card);
127 static void trigger_receive(struct xircom_private *card);
128 static void setup_descriptors(struct xircom_private *card);
129 static void remove_descriptors(struct xircom_private *card);
130 static int link_status_changed(struct xircom_private *card);
131 static void activate_receiver(struct xircom_private *card);
132 static void deactivate_receiver(struct xircom_private *card);
133 static void activate_transmitter(struct xircom_private *card);
134 static void deactivate_transmitter(struct xircom_private *card);
135 static void enable_transmit_interrupt(struct xircom_private *card);
136 static void enable_receive_interrupt(struct xircom_private *card);
137 static void enable_link_interrupt(struct xircom_private *card);
138 static void disable_all_interrupts(struct xircom_private *card);
139 static int link_status(struct xircom_private *card);
140
141
142
143 static struct pci_device_id xircom_pci_table[] = {
144         {0x115D, 0x0003, PCI_ANY_ID, PCI_ANY_ID,},
145         {0,},
146 };
147 MODULE_DEVICE_TABLE(pci, xircom_pci_table);
148
149 static struct pci_driver xircom_ops = {
150         .name           = "xircom_cb", 
151         .id_table       = xircom_pci_table, 
152         .probe          = xircom_probe, 
153         .remove         = xircom_remove, 
154         .suspend =NULL,
155         .resume =NULL
156 };
157
158
159 #ifdef DEBUG
160 static void print_binary(unsigned int number)
161 {
162         int i,i2;
163         char buffer[64];
164         memset(buffer,0,64);
165         i2=0;
166         for (i=31;i>=0;i--) {
167                 if (number & (1<<i))
168                         buffer[i2++]='1';
169                 else
170                         buffer[i2++]='0';
171                 if ((i&3)==0) 
172                         buffer[i2++]=' ';
173         }
174         printk("%s\n",buffer);
175 }
176 #endif
177
178 static void netdev_get_drvinfo(struct net_device *dev,
179                                struct ethtool_drvinfo *info)
180 {
181         struct xircom_private *private = netdev_priv(dev);
182
183         strcpy(info->driver, "xircom_cb");
184         strcpy(info->bus_info, pci_name(private->pdev));
185 }
186
187 static struct ethtool_ops netdev_ethtool_ops = {
188         .get_drvinfo            = netdev_get_drvinfo,
189 };
190
191 /* xircom_probe is the code that gets called on device insertion.
192    it sets up the hardware and registers the device to the networklayer.
193    
194    TODO: Send 1 or 2 "dummy" packets here as the card seems to discard the
195          first two packets that get send, and pump hates that.
196          
197  */
198 static int __devinit xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id)
199 {
200         struct net_device *dev = NULL;
201         struct xircom_private *private;
202         unsigned char chip_rev;
203         unsigned long flags;
204         unsigned short tmp16;
205         enter("xircom_probe");
206         
207         /* First do the PCI initialisation */
208
209         if (pci_enable_device(pdev))
210                 return -ENODEV;
211
212         /* disable all powermanagement */
213         pci_write_config_dword(pdev, PCI_POWERMGMT, 0x0000);
214         
215         pci_set_master(pdev); /* Why isn't this done by pci_enable_device ?*/
216
217         /* clear PCI status, if any */ 
218         pci_read_config_word (pdev,PCI_STATUS, &tmp16); 
219         pci_write_config_word (pdev, PCI_STATUS,tmp16);
220         
221         pci_read_config_byte(pdev, PCI_REVISION_ID, &chip_rev);
222         
223         if (!request_region(pci_resource_start(pdev, 0), 128, "xircom_cb")) {
224                 printk(KERN_ERR "xircom_probe: failed to allocate io-region\n");
225                 return -ENODEV;
226         }
227
228         /* 
229            Before changing the hardware, allocate the memory.
230            This way, we can fail gracefully if not enough memory
231            is available. 
232          */
233         dev = alloc_etherdev(sizeof(struct xircom_private));
234         if (!dev) {
235                 printk(KERN_ERR "xircom_probe: failed to allocate etherdev\n");
236                 goto device_fail;
237         }
238         private = netdev_priv(dev);
239         
240         /* Allocate the send/receive buffers */
241         private->rx_buffer = pci_alloc_consistent(pdev,8192,&private->rx_dma_handle);
242         if (private->rx_buffer == NULL) {
243                 printk(KERN_ERR "xircom_probe: no memory for rx buffer \n");
244                 goto rx_buf_fail;
245         }       
246         private->tx_buffer = pci_alloc_consistent(pdev,8192,&private->tx_dma_handle);
247         if (private->tx_buffer == NULL) {
248                 printk(KERN_ERR "xircom_probe: no memory for tx buffer \n");
249                 goto tx_buf_fail;
250         }
251
252         SET_MODULE_OWNER(dev);
253         SET_NETDEV_DEV(dev, &pdev->dev);
254
255
256         private->dev = dev;
257         private->pdev = pdev;
258         private->io_port = pci_resource_start(pdev, 0);
259         private->lock = SPIN_LOCK_UNLOCKED;
260         dev->irq = pdev->irq;
261         dev->base_addr = private->io_port;
262         
263         initialize_card(private);
264         read_mac_address(private);
265         setup_descriptors(private);
266         
267         dev->open = &xircom_open;
268         dev->hard_start_xmit = &xircom_start_xmit;
269         dev->stop = &xircom_close;
270         dev->get_stats = &xircom_get_stats;
271         dev->priv = private;
272         SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
273         pci_set_drvdata(pdev, dev);
274
275         if (register_netdev(dev)) {
276                 printk(KERN_ERR "xircom_probe: netdevice registration failed.\n");
277                 goto reg_fail;
278         }
279                 
280         printk(KERN_INFO "%s: Xircom cardbus revision %i at irq %i \n", dev->name, chip_rev, pdev->irq);
281         /* start the transmitter to get a heartbeat */
282         /* TODO: send 2 dummy packets here */
283         transceiver_voodoo(private);
284         
285         spin_lock_irqsave(&private->lock,flags);
286         activate_transmitter(private);
287         activate_receiver(private);
288         spin_unlock_irqrestore(&private->lock,flags);
289         
290         trigger_receive(private);
291         
292         leave("xircom_probe");
293         return 0;
294
295 reg_fail:
296         kfree(private->tx_buffer);
297 tx_buf_fail:
298         kfree(private->rx_buffer);
299 rx_buf_fail:
300         free_netdev(dev);
301 device_fail:
302         return -ENODEV;
303 }
304
305
306 /*
307  xircom_remove is called on module-unload or on device-eject.
308  it unregisters the irq, io-region and network device.
309  Interrupts and such are already stopped in the "ifconfig ethX down"
310  code.
311  */
312 static void __devexit xircom_remove(struct pci_dev *pdev)
313 {
314         struct net_device *dev = pci_get_drvdata(pdev);
315         struct xircom_private *card = netdev_priv(dev);
316
317         enter("xircom_remove");
318         pci_free_consistent(pdev,8192,card->rx_buffer,card->rx_dma_handle);
319         pci_free_consistent(pdev,8192,card->tx_buffer,card->tx_dma_handle);
320
321         release_region(dev->base_addr, 128);
322         unregister_netdev(dev);
323         free_netdev(dev);
324         pci_set_drvdata(pdev, NULL);
325         leave("xircom_remove");
326
327
328 static irqreturn_t xircom_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
329 {
330         struct net_device *dev = (struct net_device *) dev_instance;
331         struct xircom_private *card = netdev_priv(dev);
332         unsigned int status;
333         int i;
334
335         enter("xircom_interrupt\n");
336
337         spin_lock(&card->lock);
338         status = inl(card->io_port+CSR5);
339
340 #if DEBUG       
341         print_binary(status);
342         printk("tx status 0x%08x 0x%08x \n",card->tx_buffer[0],card->tx_buffer[4]);
343         printk("rx status 0x%08x 0x%08x \n",card->rx_buffer[0],card->rx_buffer[4]);
344 #endif  
345         /* Handle shared irq and hotplug */
346         if (status == 0 || status == 0xffffffff) {
347                 spin_unlock(&card->lock);
348                 return IRQ_NONE;
349         }
350
351         if (link_status_changed(card)) {
352                 int newlink;
353                 printk(KERN_DEBUG "xircom_cb: Link status has changed \n");
354                 newlink = link_status(card);
355                 printk(KERN_INFO  "xircom_cb: Link is %i mbit \n",newlink);
356                 if (newlink)
357                         netif_carrier_on(dev);
358                 else
359                         netif_carrier_off(dev);
360                 
361         }
362
363         /* Clear all remaining interrupts */    
364         status |= 0xffffffff; /* FIXME: make this clear only the
365                                         real existing bits */
366         outl(status,card->io_port+CSR5);
367         
368
369         for (i=0;i<NUMDESCRIPTORS;i++) 
370                 investigate_write_descriptor(dev,card,i,bufferoffsets[i]);
371         for (i=0;i<NUMDESCRIPTORS;i++) 
372                 investigate_read_descriptor(dev,card,i,bufferoffsets[i]);
373
374         
375         spin_unlock(&card->lock);
376         leave("xircom_interrupt");
377         return IRQ_HANDLED;
378 }
379
380 static int xircom_start_xmit(struct sk_buff *skb, struct net_device *dev)
381 {
382         struct xircom_private *card;
383         unsigned long flags;
384         int nextdescriptor;
385         int desc;
386         enter("xircom_start_xmit");
387         
388         card = netdev_priv(dev);
389         spin_lock_irqsave(&card->lock,flags);
390         
391         /* First see if we can free some descriptors */
392         for (desc=0;desc<NUMDESCRIPTORS;desc++) 
393                 investigate_write_descriptor(dev,card,desc,bufferoffsets[desc]);
394         
395         
396         nextdescriptor = (card->transmit_used +1) % (NUMDESCRIPTORS);
397         desc = card->transmit_used;
398         
399         /* only send the packet if the descriptor is free */
400         if (card->tx_buffer[4*desc]==0) {
401                         /* Copy the packet data; zero the memory first as the card
402                            sometimes sends more than you ask it to. */
403                         
404                         memset(&card->tx_buffer[bufferoffsets[desc]/4],0,1536);
405                         memcpy(&(card->tx_buffer[bufferoffsets[desc]/4]),skb->data,skb->len);
406         
407         
408                         /* FIXME: The specification tells us that the length we send HAS to be a multiple of
409                            4 bytes. */
410                            
411                         card->tx_buffer[4*desc+1] = skb->len;
412                         if (desc == NUMDESCRIPTORS-1)
413                                 card->tx_buffer[4*desc+1] |= (1<<25);  /* bit 25: last descriptor of the ring */
414
415                         card->tx_buffer[4*desc+1] |= 0xF0000000;
416                                                  /* 0xF0... means want interrupts*/ 
417                         card->tx_skb[desc] = skb;
418                         
419                         wmb();
420                         /* This gives the descriptor to the card */
421                         card->tx_buffer[4*desc] = 0x80000000;
422                         trigger_transmit(card);
423                         if (((int)card->tx_buffer[nextdescriptor*4])<0) {       /* next descriptor is occupied... */
424                                 netif_stop_queue(dev);
425                         }
426                         card->transmit_used = nextdescriptor;
427                         leave("xircom-start_xmit - sent");      
428                         spin_unlock_irqrestore(&card->lock,flags);
429                         return 0;
430         }
431         
432
433
434         /* Uh oh... no free descriptor... drop the packet */
435         netif_stop_queue(dev);
436         spin_unlock_irqrestore(&card->lock,flags);
437         trigger_transmit(card);
438         
439         return -EIO;
440 }
441
442
443
444
445 static int xircom_open(struct net_device *dev)
446 {
447         struct xircom_private *xp = netdev_priv(dev);
448         int retval;
449         enter("xircom_open");
450         printk(KERN_INFO "xircom cardbus adaptor found, registering as %s, using irq %i \n",dev->name,dev->irq);
451         retval = request_irq(dev->irq, &xircom_interrupt, SA_SHIRQ, dev->name, dev);
452         if (retval) {
453                 leave("xircom_open - No IRQ");
454                 return retval;
455         }
456         
457         xircom_up(xp);
458         xp->open = 1;
459         leave("xircom_open");
460         return 0;
461 }
462
463 static int xircom_close(struct net_device *dev)
464 {
465         struct xircom_private *card;
466         unsigned long flags;
467         
468         enter("xircom_close");
469         card = netdev_priv(dev);
470         netif_stop_queue(dev); /* we don't want new packets */
471
472         
473         spin_lock_irqsave(&card->lock,flags);
474         
475         disable_all_interrupts(card);
476 #if 0   
477         /* We can enable this again once we send dummy packets on ifconfig ethX up */
478         deactivate_receiver(card);
479         deactivate_transmitter(card);
480 #endif  
481         remove_descriptors(card);
482         
483         spin_unlock_irqrestore(&card->lock,flags);
484         
485         card->open = 0;
486         free_irq(dev->irq,dev);
487         
488         leave("xircom_close");
489         
490         return 0;
491         
492 }
493
494
495
496 static struct net_device_stats *xircom_get_stats(struct net_device *dev)
497 {
498         struct xircom_private *card = netdev_priv(dev);
499         return &card->stats;
500
501                                                  
502
503
504
505 static void initialize_card(struct xircom_private *card)
506 {
507         unsigned int val;
508         unsigned long flags;
509         enter("initialize_card");
510
511
512         spin_lock_irqsave(&card->lock, flags);
513
514         /* First: reset the card */
515         val = inl(card->io_port + CSR0);
516         val |= 0x01;            /* Software reset */
517         outl(val, card->io_port + CSR0);
518
519         udelay(100);            /* give the card some time to reset */
520
521         val = inl(card->io_port + CSR0);
522         val &= ~0x01;           /* disable Software reset */
523         outl(val, card->io_port + CSR0);
524
525
526         val = 0;                /* Value 0x00 is a safe and conservative value 
527                                    for the PCI configuration settings */
528         outl(val, card->io_port + CSR0);
529
530
531         disable_all_interrupts(card);
532         deactivate_receiver(card);
533         deactivate_transmitter(card);
534
535         spin_unlock_irqrestore(&card->lock, flags);
536
537         leave("initialize_card");
538 }
539
540 /*
541 trigger_transmit causes the card to check for frames to be transmitted.
542 This is accomplished by writing to the CSR1 port. The documentation
543 claims that the act of writing is sufficient and that the value is
544 ignored; I chose zero.
545 */
546 static void trigger_transmit(struct xircom_private *card)
547 {
548         unsigned int val;
549         enter("trigger_transmit");
550
551         val = 0;
552         outl(val, card->io_port + CSR1);
553
554         leave("trigger_transmit");
555 }
556
557 /*
558 trigger_receive causes the card to check for empty frames in the
559 descriptor list in which packets can be received.
560 This is accomplished by writing to the CSR2 port. The documentation
561 claims that the act of writing is sufficient and that the value is
562 ignored; I chose zero.
563 */
564 static void trigger_receive(struct xircom_private *card)
565 {
566         unsigned int val;
567         enter("trigger_receive");
568
569         val = 0;
570         outl(val, card->io_port + CSR2);
571
572         leave("trigger_receive");
573 }
574
575 /*
576 setup_descriptors initializes the send and receive buffers to be valid
577 descriptors and programs the addresses into the card.
578 */
579 static void setup_descriptors(struct xircom_private *card)
580 {
581         unsigned int val;
582         unsigned int address;
583         int i;
584         enter("setup_descriptors");
585
586
587         if (card->rx_buffer == NULL)
588                 BUG();
589         if (card->tx_buffer == NULL)
590                 BUG();
591
592         /* Receive descriptors */
593         memset(card->rx_buffer, 0, 128);        /* clear the descriptors */
594         for (i=0;i<NUMDESCRIPTORS;i++ ) {
595
596                 /* Rx Descr0: It's empty, let the card own it, no errors -> 0x80000000 */
597                 card->rx_buffer[i*4 + 0] = 0x80000000;
598                 /* Rx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
599                 card->rx_buffer[i*4 + 1] = 1536;
600                 if (i==NUMDESCRIPTORS-1)
601                         card->rx_buffer[i*4 + 1] |= (1 << 25); /* bit 25 is "last descriptor" */
602
603                 /* Rx Descr2: address of the buffer
604                    we store the buffer at the 2nd half of the page */
605         
606                 address = (unsigned long) card->rx_dma_handle;
607                 card->rx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
608                 /* Rx Desc3: address of 2nd buffer -> 0 */
609                 card->rx_buffer[i*4 + 3] = 0;
610         }
611         
612         wmb();
613         /* Write the receive descriptor ring address to the card */
614         address = (unsigned long) card->rx_dma_handle;
615         val = cpu_to_le32(address); 
616         outl(val, card->io_port + CSR3);        /* Receive descr list address */
617
618
619         /* transmit descriptors */
620         memset(card->tx_buffer, 0, 128);        /* clear the descriptors */
621         
622         for (i=0;i<NUMDESCRIPTORS;i++ ) {
623                 /* Tx Descr0: Empty, we own it, no errors -> 0x00000000 */
624                 card->tx_buffer[i*4 + 0] = 0x00000000;
625                 /* Tx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
626                 card->tx_buffer[i*4 + 1] = 1536;
627                 if (i==NUMDESCRIPTORS-1)
628                         card->tx_buffer[i*4 + 1] |= (1 << 25); /* bit 25 is "last descriptor" */
629                 
630                 /* Tx Descr2: address of the buffer
631                    we store the buffer at the 2nd half of the page */
632                 address = (unsigned long) card->tx_dma_handle;
633                 card->tx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
634                 /* Tx Desc3: address of 2nd buffer -> 0 */
635                 card->tx_buffer[i*4 + 3] = 0;
636         }
637
638         wmb();
639         /* wite the transmit descriptor ring to the card */
640         address = (unsigned long) card->tx_dma_handle;
641         val =cpu_to_le32(address);
642         outl(val, card->io_port + CSR4);        /* xmit descr list address */
643
644         leave("setup_descriptors");
645 }
646
647 /*
648 remove_descriptors informs the card the descriptors are no longer
649 valid by setting the address in the card to 0x00.
650 */
651 static void remove_descriptors(struct xircom_private *card)
652 {
653         unsigned int val;
654         enter("remove_descriptors");
655
656         val = 0;
657         outl(val, card->io_port + CSR3);        /* Receive descriptor address */
658         outl(val, card->io_port + CSR4);        /* Send descriptor address */
659
660         leave("remove_descriptors");
661 }
662
663 /*
664 link_status_changed returns 1 if the card has indicated that
665 the link status has changed. The new link status has to be read from CSR12.
666
667 This function also clears the status-bit.
668 */
669 static int link_status_changed(struct xircom_private *card)
670 {
671         unsigned int val;
672         enter("link_status_changed");
673
674         val = inl(card->io_port + CSR5);        /* Status register */
675
676         if ((val & (1 << 27)) == 0) {   /* no change */
677                 leave("link_status_changed - nochange");
678                 return 0;
679         }
680
681         /* clear the event by writing a 1 to the bit in the
682            status register. */
683         val = (1 << 27);
684         outl(val, card->io_port + CSR5);
685
686         leave("link_status_changed - changed");
687         return 1;
688 }
689
690
691 /*
692 transmit_active returns 1 if the transmitter on the card is
693 in a non-stopped state.
694 */
695 static int transmit_active(struct xircom_private *card)
696 {
697         unsigned int val;
698         enter("transmit_active");
699
700         val = inl(card->io_port + CSR5);        /* Status register */
701
702         if ((val & (7 << 20)) == 0) {   /* transmitter disabled */
703                 leave("transmit_active - inactive");
704                 return 0;
705         }
706
707         leave("transmit_active - active");
708         return 1;
709 }
710
711 /*
712 receive_active returns 1 if the receiver on the card is
713 in a non-stopped state.
714 */
715 static int receive_active(struct xircom_private *card)
716 {
717         unsigned int val;
718         enter("receive_active");
719
720
721         val = inl(card->io_port + CSR5);        /* Status register */
722
723         if ((val & (7 << 17)) == 0) {   /* receiver disabled */
724                 leave("receive_active - inactive");
725                 return 0;
726         }
727
728         leave("receive_active - active");
729         return 1;
730 }
731
732 /*
733 activate_receiver enables the receiver on the card.
734 Before being allowed to active the receiver, the receiver
735 must be completely de-activated. To achieve this,
736 this code actually disables the receiver first; then it waits for the 
737 receiver to become inactive, then it activates the receiver and then
738 it waits for the receiver to be active.
739
740 must be called with the lock held and interrupts disabled.
741 */
742 static void activate_receiver(struct xircom_private *card)
743 {
744         unsigned int val;
745         int counter;
746         enter("activate_receiver");
747
748
749         val = inl(card->io_port + CSR6);        /* Operation mode */
750         
751         /* If the "active" bit is set and the receiver is already
752            active, no need to do the expensive thing */
753         if ((val&2) && (receive_active(card)))
754                 return;
755         
756         
757         val = val & ~2;         /* disable the receiver */
758         outl(val, card->io_port + CSR6);
759
760         counter = 10;
761         while (counter > 0) {
762                 if (!receive_active(card))
763                         break;
764                 /* wait a while */
765                 udelay(50);
766                 counter--;
767                 if (counter <= 0)
768                         printk(KERN_ERR "xircom_cb: Receiver failed to deactivate\n");
769         }
770
771         /* enable the receiver */
772         val = inl(card->io_port + CSR6);        /* Operation mode */
773         val = val | 2;                          /* enable the receiver */
774         outl(val, card->io_port + CSR6);
775
776         /* now wait for the card to activate again */
777         counter = 10;
778         while (counter > 0) {
779                 if (receive_active(card))
780                         break;
781                 /* wait a while */
782                 udelay(50);
783                 counter--;
784                 if (counter <= 0)
785                         printk(KERN_ERR "xircom_cb: Receiver failed to re-activate\n");
786         }
787
788         leave("activate_receiver");
789 }
790
791 /*
792 deactivate_receiver disables the receiver on the card.
793 To achieve this this code disables the receiver first; 
794 then it waits for the receiver to become inactive.
795
796 must be called with the lock held and interrupts disabled.
797 */
798 static void deactivate_receiver(struct xircom_private *card)
799 {
800         unsigned int val;
801         int counter;
802         enter("deactivate_receiver");
803
804         val = inl(card->io_port + CSR6);        /* Operation mode */
805         val = val & ~2;                         /* disable the receiver */
806         outl(val, card->io_port + CSR6);
807
808         counter = 10;
809         while (counter > 0) {
810                 if (!receive_active(card))
811                         break;
812                 /* wait a while */
813                 udelay(50);
814                 counter--;
815                 if (counter <= 0)
816                         printk(KERN_ERR "xircom_cb: Receiver failed to deactivate\n");
817         }
818
819
820         leave("deactivate_receiver");
821 }
822
823
824 /*
825 activate_transmitter enables the transmitter on the card.
826 Before being allowed to active the transmitter, the transmitter
827 must be completely de-activated. To achieve this,
828 this code actually disables the transmitter first; then it waits for the 
829 transmitter to become inactive, then it activates the transmitter and then
830 it waits for the transmitter to be active again.
831
832 must be called with the lock held and interrupts disabled.
833 */
834 static void activate_transmitter(struct xircom_private *card)
835 {
836         unsigned int val;
837         int counter;
838         enter("activate_transmitter");
839
840
841         val = inl(card->io_port + CSR6);        /* Operation mode */
842
843         /* If the "active" bit is set and the receiver is already
844            active, no need to do the expensive thing */  
845         if ((val&(1<<13)) && (transmit_active(card)))
846                 return;
847
848         val = val & ~(1 << 13); /* disable the transmitter */
849         outl(val, card->io_port + CSR6);
850
851         counter = 10;
852         while (counter > 0) {
853                 if (!transmit_active(card))
854                         break;
855                 /* wait a while */
856                 udelay(50);
857                 counter--;
858                 if (counter <= 0)
859                         printk(KERN_ERR "xircom_cb: Transmitter failed to deactivate\n");
860         }
861
862         /* enable the transmitter */
863         val = inl(card->io_port + CSR6);        /* Operation mode */
864         val = val | (1 << 13);  /* enable the transmitter */
865         outl(val, card->io_port + CSR6);
866
867         /* now wait for the card to activate again */
868         counter = 10;
869         while (counter > 0) {
870                 if (transmit_active(card))
871                         break;
872                 /* wait a while */
873                 udelay(50);
874                 counter--;
875                 if (counter <= 0)
876                         printk(KERN_ERR "xircom_cb: Transmitter failed to re-activate\n");
877         }
878
879         leave("activate_transmitter");
880 }
881
882 /*
883 deactivate_transmitter disables the transmitter on the card.
884 To achieve this this code disables the transmitter first; 
885 then it waits for the transmitter to become inactive.
886
887 must be called with the lock held and interrupts disabled.
888 */
889 static void deactivate_transmitter(struct xircom_private *card)
890 {
891         unsigned int val;
892         int counter;
893         enter("deactivate_transmitter");
894
895         val = inl(card->io_port + CSR6);        /* Operation mode */
896         val = val & ~2;         /* disable the transmitter */
897         outl(val, card->io_port + CSR6);
898
899         counter = 20;
900         while (counter > 0) {
901                 if (!transmit_active(card))
902                         break;
903                 /* wait a while */
904                 udelay(50);
905                 counter--;
906                 if (counter <= 0)
907                         printk(KERN_ERR "xircom_cb: Transmitter failed to deactivate\n");
908         }
909
910
911         leave("deactivate_transmitter");
912 }
913
914
915 /*
916 enable_transmit_interrupt enables the transmit interrupt
917
918 must be called with the lock held and interrupts disabled.
919 */
920 static void enable_transmit_interrupt(struct xircom_private *card)
921 {
922         unsigned int val;
923         enter("enable_transmit_interrupt");
924
925         val = inl(card->io_port + CSR7);        /* Interrupt enable register */
926         val |= 1;                               /* enable the transmit interrupt */
927         outl(val, card->io_port + CSR7);
928
929         leave("enable_transmit_interrupt");
930 }
931
932
933 /*
934 enable_receive_interrupt enables the receive interrupt
935
936 must be called with the lock held and interrupts disabled.
937 */
938 static void enable_receive_interrupt(struct xircom_private *card)
939 {
940         unsigned int val;
941         enter("enable_receive_interrupt");
942
943         val = inl(card->io_port + CSR7);        /* Interrupt enable register */
944         val = val | (1 << 6);                   /* enable the receive interrupt */
945         outl(val, card->io_port + CSR7);
946
947         leave("enable_receive_interrupt");
948 }
949
950 /*
951 enable_link_interrupt enables the link status change interrupt
952
953 must be called with the lock held and interrupts disabled.
954 */
955 static void enable_link_interrupt(struct xircom_private *card)
956 {
957         unsigned int val;
958         enter("enable_link_interrupt");
959
960         val = inl(card->io_port + CSR7);        /* Interrupt enable register */
961         val = val | (1 << 27);                  /* enable the link status chage interrupt */
962         outl(val, card->io_port + CSR7);
963
964         leave("enable_link_interrupt");
965 }
966
967
968
969 /*
970 disable_all_interrupts disables all interrupts
971
972 must be called with the lock held and interrupts disabled.
973 */
974 static void disable_all_interrupts(struct xircom_private *card)
975 {
976         unsigned int val;
977         enter("enable_all_interrupts");
978         
979         val = 0;                                /* disable all interrupts */
980         outl(val, card->io_port + CSR7);
981
982         leave("disable_all_interrupts");
983 }
984
985 /*
986 enable_common_interrupts enables several weird interrupts
987
988 must be called with the lock held and interrupts disabled.
989 */
990 static void enable_common_interrupts(struct xircom_private *card)
991 {
992         unsigned int val;
993         enter("enable_link_interrupt");
994
995         val = inl(card->io_port + CSR7);        /* Interrupt enable register */
996         val |= (1<<16); /* Normal Interrupt Summary */
997         val |= (1<<15); /* Abnormal Interrupt Summary */
998         val |= (1<<13); /* Fatal bus error */
999         val |= (1<<8);  /* Receive Process Stopped */
1000         val |= (1<<7);  /* Receive Buffer Unavailable */
1001         val |= (1<<5);  /* Transmit Underflow */
1002         val |= (1<<2);  /* Transmit Buffer Unavailable */
1003         val |= (1<<1);  /* Transmit Process Stopped */
1004         outl(val, card->io_port + CSR7);
1005
1006         leave("enable_link_interrupt");
1007 }
1008
1009 /*
1010 enable_promisc starts promisc mode
1011
1012 must be called with the lock held and interrupts disabled.
1013 */
1014 static int enable_promisc(struct xircom_private *card)
1015 {
1016         unsigned int val;
1017         enter("enable_promisc");
1018
1019         val = inl(card->io_port + CSR6);        
1020         val = val | (1 << 6);   
1021         outl(val, card->io_port + CSR6);
1022
1023         leave("enable_promisc");
1024         return 1;
1025 }
1026
1027
1028
1029
1030 /* 
1031 link_status() checks the the links status and will return 0 for no link, 10 for 10mbit link and 100 for.. guess what.
1032
1033 Must be called in locked state with interrupts disabled
1034 */
1035 static int link_status(struct xircom_private *card)
1036 {
1037         unsigned int val;
1038         enter("link_status");
1039         
1040         val = inb(card->io_port + CSR12);
1041         
1042         if (!(val&(1<<2)))  /* bit 2 is 0 for 10mbit link, 1 for not an 10mbit link */
1043                 return 10;
1044         if (!(val&(1<<1)))  /* bit 1 is 0 for 100mbit link, 1 for not an 100mbit link */
1045                 return 100;
1046                 
1047         /* If we get here -> no link at all */  
1048
1049         leave("link_status");
1050         return 0;
1051 }
1052
1053
1054
1055
1056
1057 /*
1058   read_mac_address() reads the MAC address from the NIC and stores it in the "dev" structure.
1059  
1060   This function will take the spinlock itself and can, as a result, not be called with the lock helt.
1061  */
1062 static void read_mac_address(struct xircom_private *card)
1063 {
1064         unsigned char j, tuple, link, data_id, data_count;
1065         unsigned long flags;
1066         int i;
1067
1068         enter("read_mac_address");
1069                 
1070         spin_lock_irqsave(&card->lock, flags);
1071
1072         outl(1 << 12, card->io_port + CSR9);    /* enable boot rom access */
1073         for (i = 0x100; i < 0x1f7; i += link + 2) {
1074                 outl(i, card->io_port + CSR10);
1075                 tuple = inl(card->io_port + CSR9) & 0xff;
1076                 outl(i + 1, card->io_port + CSR10);
1077                 link = inl(card->io_port + CSR9) & 0xff;
1078                 outl(i + 2, card->io_port + CSR10);
1079                 data_id = inl(card->io_port + CSR9) & 0xff;
1080                 outl(i + 3, card->io_port + CSR10);
1081                 data_count = inl(card->io_port + CSR9) & 0xff;
1082                 if ((tuple == 0x22) && (data_id == 0x04) && (data_count == 0x06)) {
1083                         /* 
1084                          * This is it.  We have the data we want.
1085                          */
1086                         for (j = 0; j < 6; j++) {
1087                                 outl(i + j + 4, card->io_port + CSR10);
1088                                 card->dev->dev_addr[j] = inl(card->io_port + CSR9) & 0xff;
1089                         }
1090                         break;
1091                 } else if (link == 0) {
1092                         break;
1093                 }
1094         }
1095         spin_unlock_irqrestore(&card->lock, flags);
1096 #ifdef DEBUG
1097         for (i = 0; i < 6; i++)
1098                 printk("%c%2.2X", i ? ':' : ' ', card->dev->dev_addr[i]);
1099         printk("\n");
1100 #endif
1101         leave("read_mac_address");
1102 }
1103
1104
1105 /*
1106  transceiver_voodoo() enables the external UTP plug thingy.
1107  it's called voodoo as I stole this code and cannot cross-reference
1108  it with the specification.
1109  */
1110 static void transceiver_voodoo(struct xircom_private *card)
1111 {
1112         unsigned long flags;
1113
1114         enter("transceiver_voodoo");
1115
1116         /* disable all powermanagement */
1117         pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1118
1119         setup_descriptors(card);
1120
1121         spin_lock_irqsave(&card->lock, flags);
1122
1123         outl(0x0008, card->io_port + CSR15);
1124         udelay(25);  
1125         outl(0xa8050000, card->io_port + CSR15);
1126         udelay(25);
1127         outl(0xa00f0000, card->io_port + CSR15);
1128         udelay(25);
1129         
1130         spin_unlock_irqrestore(&card->lock, flags);
1131
1132         netif_start_queue(card->dev);
1133         leave("transceiver_voodoo");
1134 }
1135
1136
1137 static void xircom_up(struct xircom_private *card)
1138 {
1139         unsigned long flags;
1140         int i;
1141
1142         enter("xircom_up");
1143
1144         /* disable all powermanagement */
1145         pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1146
1147         setup_descriptors(card);
1148
1149         spin_lock_irqsave(&card->lock, flags);
1150
1151         
1152         enable_link_interrupt(card);
1153         enable_transmit_interrupt(card);
1154         enable_receive_interrupt(card);
1155         enable_common_interrupts(card);
1156         enable_promisc(card);
1157         
1158         /* The card can have received packets already, read them away now */
1159         for (i=0;i<NUMDESCRIPTORS;i++) 
1160                 investigate_read_descriptor(card->dev,card,i,bufferoffsets[i]);
1161
1162
1163         spin_unlock_irqrestore(&card->lock, flags);
1164         trigger_receive(card);
1165         trigger_transmit(card);
1166         netif_start_queue(card->dev);
1167         leave("xircom_up");
1168 }
1169
1170 /* Bufferoffset is in BYTES */
1171 static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset)
1172 {
1173                 int status;             
1174                 
1175                 enter("investigate_read_descriptor");
1176                 status = card->rx_buffer[4*descnr];
1177                 
1178                 if ((status > 0)) {     /* packet received */
1179                 
1180                         /* TODO: discard error packets */
1181                         
1182                         short pkt_len = ((status >> 16) & 0x7ff) - 4;   /* minus 4, we don't want the CRC */
1183                         struct sk_buff *skb;
1184
1185                         if (pkt_len > 1518) {
1186                                 printk(KERN_ERR "xircom_cb: Packet length %i is bogus \n",pkt_len);
1187                                 pkt_len = 1518;
1188                         }
1189
1190                         skb = dev_alloc_skb(pkt_len + 2);
1191                         if (skb == NULL) {
1192                                 card->stats.rx_dropped++;
1193                                 goto out;
1194                         }
1195                         skb->dev = dev;
1196                         skb_reserve(skb, 2);
1197                         eth_copy_and_sum(skb, (unsigned char*)&card->rx_buffer[bufferoffset / 4], pkt_len, 0);
1198                         skb_put(skb, pkt_len);
1199                         skb->protocol = eth_type_trans(skb, dev);
1200                         netif_rx(skb);
1201                         dev->last_rx = jiffies;
1202                         card->stats.rx_packets++;
1203                         card->stats.rx_bytes += pkt_len;
1204                         
1205                       out:
1206                         /* give the buffer back to the card */
1207                         card->rx_buffer[4*descnr] =  0x80000000;
1208                         trigger_receive(card);
1209                 }
1210
1211                 leave("investigate_read_descriptor");
1212
1213 }
1214
1215
1216 /* Bufferoffset is in BYTES */
1217 static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset)
1218 {
1219                 int status;
1220
1221                 enter("investigate_write_descriptor");
1222                 
1223                 status = card->tx_buffer[4*descnr];
1224 #if 0           
1225                 if (status & 0x8000) {  /* Major error */
1226                         printk(KERN_ERR "Major transmit error status %x \n", status);
1227                         card->tx_buffer[4*descnr] = 0;
1228                         netif_wake_queue (dev);
1229                 }
1230 #endif
1231                 if (status > 0) {       /* bit 31 is 0 when done */
1232                         if (card->tx_skb[descnr]!=NULL) {
1233                                 card->stats.tx_bytes += card->tx_skb[descnr]->len;
1234                                 dev_kfree_skb_irq(card->tx_skb[descnr]);
1235                         }
1236                         card->tx_skb[descnr] = NULL;
1237                         /* Bit 8 in the status field is 1 if there was a collision */
1238                         if (status&(1<<8))
1239                                 card->stats.collisions++;
1240                         card->tx_buffer[4*descnr] = 0; /* descriptor is free again */
1241                         netif_wake_queue (dev);
1242                         card->stats.tx_packets++;
1243                 }
1244
1245                 leave("investigate_write_descriptor");
1246                 
1247 }
1248
1249
1250 static int __init xircom_init(void)
1251 {
1252         pci_register_driver(&xircom_ops);
1253         return 0;
1254 }
1255
1256 static void __exit xircom_exit(void)
1257 {
1258         pci_unregister_driver(&xircom_ops);
1259
1260
1261 module_init(xircom_init) 
1262 module_exit(xircom_exit)
1263