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