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