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