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