MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / net / tulip / xircom_cb.c
blob6e65890391a64893433eaa370b2739f6c0e287d1
1 /*
2 * xircom_cb: A driver for the (tulip-like) Xircom Cardbus ethernet cards
4 * This software is (C) by the respective authors, and licensed under the GPL
5 * License.
7 * Written by Arjan van de Ven for Red Hat, Inc.
8 * Based on work by Jeff Garzik, Doug Ledford and Donald Becker
10 * This software may be used and distributed according to the terms
11 * of the GNU General Public License, incorporated herein by reference.
14 * $Id: xircom_cb.c,v 1.33 2001/03/19 14:02:07 arjanv Exp $
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>
32 #include <asm/uaccess.h>
33 #include <asm/bitops.h>
34 #include <asm/io.h>
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
45 MODULE_DESCRIPTION("Xircom Cardbus ethernet driver");
46 MODULE_AUTHOR("Arjan van de Ven <arjanv@redhat.com>");
47 MODULE_LICENSE("GPL");
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
70 /* PCI registers */
71 #define PCI_POWERMGMT 0x40
73 /* Offsets of the buffers within the descriptor pages, in bytes */
75 #define NUMDESCRIPTORS 4
77 static int bufferoffsets[NUMDESCRIPTORS] = {128,2048,4096,6144};
80 struct xircom_private {
81 /* Send and receive buffers, kernel-addressable and dma addressable forms */
83 unsigned int *rx_buffer;
84 unsigned int *tx_buffer;
86 dma_addr_t rx_dma_handle;
87 dma_addr_t tx_dma_handle;
89 struct sk_buff *tx_skb[4];
91 unsigned long io_port;
92 int open;
94 /* transmit_used is the rotating counter that indicates which transmit
95 descriptor has to be used next */
96 int transmit_used;
98 /* Spinlock to serialize register operations.
99 It must be helt while manipulating the following registers:
100 CSR0, CSR6, CSR7, CSR9, CSR10, CSR15
102 spinlock_t lock;
105 struct pci_dev *pdev;
106 struct net_device *dev;
107 struct net_device_stats stats;
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);
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);
143 static struct pci_device_id xircom_pci_table[] = {
144 {0x115D, 0x0003, PCI_ANY_ID, PCI_ANY_ID,},
145 {0,},
147 MODULE_DEVICE_TABLE(pci, xircom_pci_table);
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
159 #ifdef DEBUG
160 static void print_binary(unsigned int number)
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++]=' ';
174 printk("%s\n",buffer);
176 #endif
178 static void netdev_get_drvinfo(struct net_device *dev,
179 struct ethtool_drvinfo *info)
181 struct xircom_private *private = netdev_priv(dev);
183 strcpy(info->driver, "xircom_cb");
184 strcpy(info->bus_info, pci_name(private->pdev));
187 static struct ethtool_ops netdev_ethtool_ops = {
188 .get_drvinfo = netdev_get_drvinfo,
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.
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.
198 static int __devinit xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id)
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");
207 /* First do the PCI initialisation */
209 if (pci_enable_device(pdev))
210 return -ENODEV;
212 /* disable all powermanagement */
213 pci_write_config_dword(pdev, PCI_POWERMGMT, 0x0000);
215 pci_set_master(pdev); /* Why isn't this done by pci_enable_device ?*/
217 /* clear PCI status, if any */
218 pci_read_config_word (pdev,PCI_STATUS, &tmp16);
219 pci_write_config_word (pdev, PCI_STATUS,tmp16);
221 pci_read_config_byte(pdev, PCI_REVISION_ID, &chip_rev);
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;
229 Before changing the hardware, allocate the memory.
230 This way, we can fail gracefully if not enough memory
231 is available.
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;
238 private = netdev_priv(dev);
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;
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;
252 SET_MODULE_OWNER(dev);
253 SET_NETDEV_DEV(dev, &pdev->dev);
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;
263 initialize_card(private);
264 read_mac_address(private);
265 setup_descriptors(private);
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);
275 if (register_netdev(dev)) {
276 printk(KERN_ERR "xircom_probe: netdevice registration failed.\n");
277 goto reg_fail;
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);
285 spin_lock_irqsave(&private->lock,flags);
286 activate_transmitter(private);
287 activate_receiver(private);
288 spin_unlock_irqrestore(&private->lock,flags);
290 trigger_receive(private);
292 leave("xircom_probe");
293 return 0;
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;
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.
312 static void __devexit xircom_remove(struct pci_dev *pdev)
314 struct net_device *dev = pci_get_drvdata(pdev);
315 struct xircom_private *card = netdev_priv(dev);
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);
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");
328 static irqreturn_t xircom_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
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;
335 enter("xircom_interrupt\n");
337 spin_lock(&card->lock);
338 status = inl(card->io_port+CSR5);
340 #ifdef 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;
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);
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);
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]);
375 spin_unlock(&card->lock);
376 leave("xircom_interrupt");
377 return IRQ_HANDLED;
380 static int xircom_start_xmit(struct sk_buff *skb, struct net_device *dev)
382 struct xircom_private *card;
383 unsigned long flags;
384 int nextdescriptor;
385 int desc;
386 enter("xircom_start_xmit");
388 card = netdev_priv(dev);
389 spin_lock_irqsave(&card->lock,flags);
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]);
396 nextdescriptor = (card->transmit_used +1) % (NUMDESCRIPTORS);
397 desc = card->transmit_used;
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. */
404 memset(&card->tx_buffer[bufferoffsets[desc]/4],0,1536);
405 memcpy(&(card->tx_buffer[bufferoffsets[desc]/4]),skb->data,skb->len);
408 /* FIXME: The specification tells us that the length we send HAS to be a multiple of
409 4 bytes. */
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 */
415 card->tx_buffer[4*desc+1] |= 0xF0000000;
416 /* 0xF0... means want interrupts*/
417 card->tx_skb[desc] = skb;
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);
426 card->transmit_used = nextdescriptor;
427 leave("xircom-start_xmit - sent");
428 spin_unlock_irqrestore(&card->lock,flags);
429 return 0;
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);
439 return -EIO;
445 static int xircom_open(struct net_device *dev)
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;
457 xircom_up(xp);
458 xp->open = 1;
459 leave("xircom_open");
460 return 0;
463 static int xircom_close(struct net_device *dev)
465 struct xircom_private *card;
466 unsigned long flags;
468 enter("xircom_close");
469 card = netdev_priv(dev);
470 netif_stop_queue(dev); /* we don't want new packets */
473 spin_lock_irqsave(&card->lock,flags);
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);
483 spin_unlock_irqrestore(&card->lock,flags);
485 card->open = 0;
486 free_irq(dev->irq,dev);
488 leave("xircom_close");
490 return 0;
496 static struct net_device_stats *xircom_get_stats(struct net_device *dev)
498 struct xircom_private *card = netdev_priv(dev);
499 return &card->stats;
505 static void initialize_card(struct xircom_private *card)
507 unsigned int val;
508 unsigned long flags;
509 enter("initialize_card");
512 spin_lock_irqsave(&card->lock, flags);
514 /* First: reset the card */
515 val = inl(card->io_port + CSR0);
516 val |= 0x01; /* Software reset */
517 outl(val, card->io_port + CSR0);
519 udelay(100); /* give the card some time to reset */
521 val = inl(card->io_port + CSR0);
522 val &= ~0x01; /* disable Software reset */
523 outl(val, card->io_port + CSR0);
526 val = 0; /* Value 0x00 is a safe and conservative value
527 for the PCI configuration settings */
528 outl(val, card->io_port + CSR0);
531 disable_all_interrupts(card);
532 deactivate_receiver(card);
533 deactivate_transmitter(card);
535 spin_unlock_irqrestore(&card->lock, flags);
537 leave("initialize_card");
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.
546 static void trigger_transmit(struct xircom_private *card)
548 unsigned int val;
549 enter("trigger_transmit");
551 val = 0;
552 outl(val, card->io_port + CSR1);
554 leave("trigger_transmit");
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.
564 static void trigger_receive(struct xircom_private *card)
566 unsigned int val;
567 enter("trigger_receive");
569 val = 0;
570 outl(val, card->io_port + CSR2);
572 leave("trigger_receive");
576 setup_descriptors initializes the send and receive buffers to be valid
577 descriptors and programs the addresses into the card.
579 static void setup_descriptors(struct xircom_private *card)
581 unsigned int val;
582 unsigned int address;
583 int i;
584 enter("setup_descriptors");
587 if (card->rx_buffer == NULL)
588 BUG();
589 if (card->tx_buffer == NULL)
590 BUG();
592 /* Receive descriptors */
593 memset(card->rx_buffer, 0, 128); /* clear the descriptors */
594 for (i=0;i<NUMDESCRIPTORS;i++ ) {
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" */
603 /* Rx Descr2: address of the buffer
604 we store the buffer at the 2nd half of the page */
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;
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 */
619 /* transmit descriptors */
620 memset(card->tx_buffer, 0, 128); /* clear the descriptors */
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" */
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;
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 */
644 leave("setup_descriptors");
648 remove_descriptors informs the card the descriptors are no longer
649 valid by setting the address in the card to 0x00.
651 static void remove_descriptors(struct xircom_private *card)
653 unsigned int val;
654 enter("remove_descriptors");
656 val = 0;
657 outl(val, card->io_port + CSR3); /* Receive descriptor address */
658 outl(val, card->io_port + CSR4); /* Send descriptor address */
660 leave("remove_descriptors");
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.
667 This function also clears the status-bit.
669 static int link_status_changed(struct xircom_private *card)
671 unsigned int val;
672 enter("link_status_changed");
674 val = inl(card->io_port + CSR5); /* Status register */
676 if ((val & (1 << 27)) == 0) { /* no change */
677 leave("link_status_changed - nochange");
678 return 0;
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);
686 leave("link_status_changed - changed");
687 return 1;
692 transmit_active returns 1 if the transmitter on the card is
693 in a non-stopped state.
695 static int transmit_active(struct xircom_private *card)
697 unsigned int val;
698 enter("transmit_active");
700 val = inl(card->io_port + CSR5); /* Status register */
702 if ((val & (7 << 20)) == 0) { /* transmitter disabled */
703 leave("transmit_active - inactive");
704 return 0;
707 leave("transmit_active - active");
708 return 1;
712 receive_active returns 1 if the receiver on the card is
713 in a non-stopped state.
715 static int receive_active(struct xircom_private *card)
717 unsigned int val;
718 enter("receive_active");
721 val = inl(card->io_port + CSR5); /* Status register */
723 if ((val & (7 << 17)) == 0) { /* receiver disabled */
724 leave("receive_active - inactive");
725 return 0;
728 leave("receive_active - active");
729 return 1;
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.
740 must be called with the lock held and interrupts disabled.
742 static void activate_receiver(struct xircom_private *card)
744 unsigned int val;
745 int counter;
746 enter("activate_receiver");
749 val = inl(card->io_port + CSR6); /* Operation mode */
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;
757 val = val & ~2; /* disable the receiver */
758 outl(val, card->io_port + CSR6);
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");
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);
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");
788 leave("activate_receiver");
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.
796 must be called with the lock held and interrupts disabled.
798 static void deactivate_receiver(struct xircom_private *card)
800 unsigned int val;
801 int counter;
802 enter("deactivate_receiver");
804 val = inl(card->io_port + CSR6); /* Operation mode */
805 val = val & ~2; /* disable the receiver */
806 outl(val, card->io_port + CSR6);
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");
820 leave("deactivate_receiver");
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.
832 must be called with the lock held and interrupts disabled.
834 static void activate_transmitter(struct xircom_private *card)
836 unsigned int val;
837 int counter;
838 enter("activate_transmitter");
841 val = inl(card->io_port + CSR6); /* Operation mode */
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;
848 val = val & ~(1 << 13); /* disable the transmitter */
849 outl(val, card->io_port + CSR6);
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");
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);
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");
879 leave("activate_transmitter");
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.
887 must be called with the lock held and interrupts disabled.
889 static void deactivate_transmitter(struct xircom_private *card)
891 unsigned int val;
892 int counter;
893 enter("deactivate_transmitter");
895 val = inl(card->io_port + CSR6); /* Operation mode */
896 val = val & ~2; /* disable the transmitter */
897 outl(val, card->io_port + CSR6);
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");
911 leave("deactivate_transmitter");
916 enable_transmit_interrupt enables the transmit interrupt
918 must be called with the lock held and interrupts disabled.
920 static void enable_transmit_interrupt(struct xircom_private *card)
922 unsigned int val;
923 enter("enable_transmit_interrupt");
925 val = inl(card->io_port + CSR7); /* Interrupt enable register */
926 val |= 1; /* enable the transmit interrupt */
927 outl(val, card->io_port + CSR7);
929 leave("enable_transmit_interrupt");
934 enable_receive_interrupt enables the receive interrupt
936 must be called with the lock held and interrupts disabled.
938 static void enable_receive_interrupt(struct xircom_private *card)
940 unsigned int val;
941 enter("enable_receive_interrupt");
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);
947 leave("enable_receive_interrupt");
951 enable_link_interrupt enables the link status change interrupt
953 must be called with the lock held and interrupts disabled.
955 static void enable_link_interrupt(struct xircom_private *card)
957 unsigned int val;
958 enter("enable_link_interrupt");
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);
964 leave("enable_link_interrupt");
970 disable_all_interrupts disables all interrupts
972 must be called with the lock held and interrupts disabled.
974 static void disable_all_interrupts(struct xircom_private *card)
976 unsigned int val;
977 enter("enable_all_interrupts");
979 val = 0; /* disable all interrupts */
980 outl(val, card->io_port + CSR7);
982 leave("disable_all_interrupts");
986 enable_common_interrupts enables several weird interrupts
988 must be called with the lock held and interrupts disabled.
990 static void enable_common_interrupts(struct xircom_private *card)
992 unsigned int val;
993 enter("enable_link_interrupt");
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);
1006 leave("enable_link_interrupt");
1010 enable_promisc starts promisc mode
1012 must be called with the lock held and interrupts disabled.
1014 static int enable_promisc(struct xircom_private *card)
1016 unsigned int val;
1017 enter("enable_promisc");
1019 val = inl(card->io_port + CSR6);
1020 val = val | (1 << 6);
1021 outl(val, card->io_port + CSR6);
1023 leave("enable_promisc");
1024 return 1;
1031 link_status() checks the the links status and will return 0 for no link, 10 for 10mbit link and 100 for.. guess what.
1033 Must be called in locked state with interrupts disabled
1035 static int link_status(struct xircom_private *card)
1037 unsigned int val;
1038 enter("link_status");
1040 val = inb(card->io_port + CSR12);
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;
1047 /* If we get here -> no link at all */
1049 leave("link_status");
1050 return 0;
1058 read_mac_address() reads the MAC address from the NIC and stores it in the "dev" structure.
1060 This function will take the spinlock itself and can, as a result, not be called with the lock helt.
1062 static void read_mac_address(struct xircom_private *card)
1064 unsigned char j, tuple, link, data_id, data_count;
1065 unsigned long flags;
1066 int i;
1068 enter("read_mac_address");
1070 spin_lock_irqsave(&card->lock, flags);
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)) {
1084 * This is it. We have the data we want.
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;
1090 break;
1091 } else if (link == 0) {
1092 break;
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");
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.
1110 static void transceiver_voodoo(struct xircom_private *card)
1112 unsigned long flags;
1114 enter("transceiver_voodoo");
1116 /* disable all powermanagement */
1117 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1119 setup_descriptors(card);
1121 spin_lock_irqsave(&card->lock, flags);
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);
1130 spin_unlock_irqrestore(&card->lock, flags);
1132 netif_start_queue(card->dev);
1133 leave("transceiver_voodoo");
1137 static void xircom_up(struct xircom_private *card)
1139 unsigned long flags;
1140 int i;
1142 enter("xircom_up");
1144 /* disable all powermanagement */
1145 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1147 setup_descriptors(card);
1149 spin_lock_irqsave(&card->lock, flags);
1152 enable_link_interrupt(card);
1153 enable_transmit_interrupt(card);
1154 enable_receive_interrupt(card);
1155 enable_common_interrupts(card);
1156 enable_promisc(card);
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]);
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");
1170 /* Bufferoffset is in BYTES */
1171 static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset)
1173 int status;
1175 enter("investigate_read_descriptor");
1176 status = card->rx_buffer[4*descnr];
1178 if ((status > 0)) { /* packet received */
1180 /* TODO: discard error packets */
1182 short pkt_len = ((status >> 16) & 0x7ff) - 4; /* minus 4, we don't want the CRC */
1183 struct sk_buff *skb;
1185 if (pkt_len > 1518) {
1186 printk(KERN_ERR "xircom_cb: Packet length %i is bogus \n",pkt_len);
1187 pkt_len = 1518;
1190 skb = dev_alloc_skb(pkt_len + 2);
1191 if (skb == NULL) {
1192 card->stats.rx_dropped++;
1193 goto out;
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;
1205 out:
1206 /* give the buffer back to the card */
1207 card->rx_buffer[4*descnr] = 0x80000000;
1208 trigger_receive(card);
1211 leave("investigate_read_descriptor");
1216 /* Bufferoffset is in BYTES */
1217 static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset)
1219 int status;
1221 enter("investigate_write_descriptor");
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);
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]);
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++;
1245 leave("investigate_write_descriptor");
1250 static int __init xircom_init(void)
1252 pci_register_driver(&xircom_ops);
1253 return 0;
1256 static void __exit xircom_exit(void)
1258 pci_unregister_driver(&xircom_ops);
1261 module_init(xircom_init)
1262 module_exit(xircom_exit)