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