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