blob: 16a54e6b8d4f002b31e1a2e967785a1fac538e91 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Garzikf3b197a2006-05-26 21:39:03 -04002 * xircom_cb: A driver for the (tulip-like) Xircom Cardbus ethernet cards
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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.
Jeff Garzikf3b197a2006-05-26 21:39:03 -04008 * Based on work by Jeff Garzik, Doug Ledford and Donald Becker
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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>
Al Viro82729972005-12-06 05:53:04 -050035#ifdef CONFIG_NET_POLL_CONTROLLER
36#include <asm/irq.h>
37#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
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
48MODULE_DESCRIPTION("Xircom Cardbus ethernet driver");
49MODULE_AUTHOR("Arjan van de Ven <arjanv@redhat.com>");
50MODULE_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
80static int bufferoffsets[NUMDESCRIPTORS] = {128,2048,4096,6144};
81
82
83struct 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;
Jeff Garzikf3b197a2006-05-26 21:39:03 -040096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 /* 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 */
115static int xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id);
116static void xircom_remove(struct pci_dev *pdev);
David Howells7d12e782006-10-05 14:55:46 +0100117static irqreturn_t xircom_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static int xircom_start_xmit(struct sk_buff *skb, struct net_device *dev);
119static int xircom_open(struct net_device *dev);
120static int xircom_close(struct net_device *dev);
121static void xircom_up(struct xircom_private *card);
122static struct net_device_stats *xircom_get_stats(struct net_device *dev);
Keith Owens3be034b2005-09-13 15:05:13 +1000123#ifdef CONFIG_NET_POLL_CONTROLLER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static void xircom_poll_controller(struct net_device *dev);
125#endif
126
127static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset);
128static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset);
129static void read_mac_address(struct xircom_private *card);
130static void transceiver_voodoo(struct xircom_private *card);
131static void initialize_card(struct xircom_private *card);
132static void trigger_transmit(struct xircom_private *card);
133static void trigger_receive(struct xircom_private *card);
134static void setup_descriptors(struct xircom_private *card);
135static void remove_descriptors(struct xircom_private *card);
136static int link_status_changed(struct xircom_private *card);
137static void activate_receiver(struct xircom_private *card);
138static void deactivate_receiver(struct xircom_private *card);
139static void activate_transmitter(struct xircom_private *card);
140static void deactivate_transmitter(struct xircom_private *card);
141static void enable_transmit_interrupt(struct xircom_private *card);
142static void enable_receive_interrupt(struct xircom_private *card);
143static void enable_link_interrupt(struct xircom_private *card);
144static void disable_all_interrupts(struct xircom_private *card);
145static int link_status(struct xircom_private *card);
146
147
148
149static struct pci_device_id xircom_pci_table[] = {
150 {0x115D, 0x0003, PCI_ANY_ID, PCI_ANY_ID,},
151 {0,},
152};
153MODULE_DEVICE_TABLE(pci, xircom_pci_table);
154
155static struct pci_driver xircom_ops = {
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400156 .name = "xircom_cb",
157 .id_table = xircom_pci_table,
158 .probe = xircom_probe,
159 .remove = xircom_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 .suspend =NULL,
161 .resume =NULL
162};
163
164
165#ifdef DEBUG
166static 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';
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400177 if ((i&3)==0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 buffer[i2++]=' ';
179 }
180 printk("%s\n",buffer);
181}
182#endif
183
184static 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
Jeff Garzik7282d492006-09-13 14:30:00 -0400193static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 .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.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 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.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 */
204static 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 unsigned long flags;
209 unsigned short tmp16;
210 enter("xircom_probe");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* 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);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 pci_set_master(pdev); /* Why isn't this done by pci_enable_device ?*/
221
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400222 /* clear PCI status, if any */
223 pci_read_config_word (pdev,PCI_STATUS, &tmp16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 pci_write_config_word (pdev, PCI_STATUS,tmp16);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 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
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400231 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 Before changing the hardware, allocate the memory.
233 This way, we can fail gracefully if not enough memory
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400234 is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
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);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /* 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;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 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;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 initialize_card(private);
267 read_mac_address(private);
268 setup_descriptors(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 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 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400285
Auke Kok44c10132007-06-08 15:46:36 -0700286 printk(KERN_INFO "%s: Xircom cardbus revision %i at irq %i \n", dev->name, pdev->revision, pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* start the transmitter to get a heartbeat */
288 /* TODO: send 2 dummy packets here */
289 transceiver_voodoo(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 spin_lock_irqsave(&private->lock,flags);
292 activate_transmitter(private);
293 activate_receiver(private);
294 spin_unlock_irqrestore(&private->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 trigger_receive(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 leave("xircom_probe");
299 return 0;
300
301reg_fail:
302 kfree(private->tx_buffer);
303tx_buf_fail:
304 kfree(private->rx_buffer);
305rx_buf_fail:
306 free_netdev(dev);
307device_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 */
318static 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");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400332}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
David Howells7d12e782006-10-05 14:55:46 +0100334static irqreturn_t xircom_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
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
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400346#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 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]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400350#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /* 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);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400369 /* Clear all remaining interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 status |= 0xffffffff; /* FIXME: make this clear only the
371 real existing bits */
372 outl(status,card->io_port+CSR5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400374
375 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 investigate_write_descriptor(dev,card,i,bufferoffsets[i]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400377 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 investigate_read_descriptor(dev,card,i,bufferoffsets[i]);
379
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 spin_unlock(&card->lock);
382 leave("xircom_interrupt");
383 return IRQ_HANDLED;
384}
385
386static 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");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 card = netdev_priv(dev);
395 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* First see if we can free some descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400398 for (desc=0;desc<NUMDESCRIPTORS;desc++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 investigate_write_descriptor(dev,card,desc,bufferoffsets[desc]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400400
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 nextdescriptor = (card->transmit_used +1) % (NUMDESCRIPTORS);
403 desc = card->transmit_used;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /* 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. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 memset(&card->tx_buffer[bufferoffsets[desc]/4],0,1536);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300411 skb_copy_from_linear_data(skb,
412 &(card->tx_buffer[bufferoffsets[desc] / 4]),
413 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /* FIXME: The specification tells us that the length we send HAS to be a multiple of
415 4 bytes. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 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;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400422 /* 0xF0... means want interrupts*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 card->tx_skb[desc] = skb;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 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;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400433 leave("xircom-start_xmit - sent");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 spin_unlock_irqrestore(&card->lock,flags);
435 return 0;
436 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
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);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return -EIO;
446}
447
448
449
450
451static 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);
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700457 retval = request_irq(dev->irq, &xircom_interrupt, IRQF_SHARED, dev->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (retval) {
459 leave("xircom_open - No IRQ");
460 return retval;
461 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 xircom_up(xp);
464 xp->open = 1;
465 leave("xircom_open");
466 return 0;
467}
468
469static int xircom_close(struct net_device *dev)
470{
471 struct xircom_private *card;
472 unsigned long flags;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 enter("xircom_close");
475 card = netdev_priv(dev);
476 netif_stop_queue(dev); /* we don't want new packets */
477
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 disable_all_interrupts(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400482#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /* We can enable this again once we send dummy packets on ifconfig ethX up */
484 deactivate_receiver(card);
485 deactivate_transmitter(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400486#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 remove_descriptors(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 spin_unlock_irqrestore(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 card->open = 0;
492 free_irq(dev->irq,dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 leave("xircom_close");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return 0;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500
501
502static struct net_device_stats *xircom_get_stats(struct net_device *dev)
503{
504 struct xircom_private *card = netdev_priv(dev);
505 return &card->stats;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400506}
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509#ifdef CONFIG_NET_POLL_CONTROLLER
510static void xircom_poll_controller(struct net_device *dev)
511{
512 disable_irq(dev->irq);
David Howells7d12e782006-10-05 14:55:46 +0100513 xircom_interrupt(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 enable_irq(dev->irq);
515}
516#endif
517
518
519static 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
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400540 val = 0; /* Value 0x00 is a safe and conservative value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 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/*
555trigger_transmit causes the card to check for frames to be transmitted.
556This is accomplished by writing to the CSR1 port. The documentation
557claims that the act of writing is sufficient and that the value is
558ignored; I chose zero.
559*/
560static 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/*
572trigger_receive causes the card to check for empty frames in the
573descriptor list in which packets can be received.
574This is accomplished by writing to the CSR2 port. The documentation
575claims that the act of writing is sufficient and that the value is
576ignored; I chose zero.
577*/
578static 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/*
590setup_descriptors initializes the send and receive buffers to be valid
591descriptors and programs the addresses into the card.
592*/
593static 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
Eric Sesterhenn / snakebytea707cd62006-01-26 22:02:51 +0100601 BUG_ON(card->rx_buffer == NULL);
602 BUG_ON(card->tx_buffer == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
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 */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 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 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 wmb();
625 /* Write the receive descriptor ring address to the card */
626 address = (unsigned long) card->rx_dma_handle;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400627 val = cpu_to_le32(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 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 */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 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" */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /* 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/*
660remove_descriptors informs the card the descriptors are no longer
661valid by setting the address in the card to 0x00.
662*/
663static 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/*
676link_status_changed returns 1 if the card has indicated that
677the link status has changed. The new link status has to be read from CSR12.
678
679This function also clears the status-bit.
680*/
681static 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/*
704transmit_active returns 1 if the transmitter on the card is
705in a non-stopped state.
706*/
707static 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/*
724receive_active returns 1 if the receiver on the card is
725in a non-stopped state.
726*/
727static 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/*
745activate_receiver enables the receiver on the card.
746Before being allowed to active the receiver, the receiver
747must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400748this code actually disables the receiver first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749receiver to become inactive, then it activates the receiver and then
750it waits for the receiver to be active.
751
752must be called with the lock held and interrupts disabled.
753*/
754static 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 */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /* 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;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400767
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 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/*
804deactivate_receiver disables the receiver on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400805To achieve this this code disables the receiver first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806then it waits for the receiver to become inactive.
807
808must be called with the lock held and interrupts disabled.
809*/
810static 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/*
837activate_transmitter enables the transmitter on the card.
838Before being allowed to active the transmitter, the transmitter
839must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400840this code actually disables the transmitter first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841transmitter to become inactive, then it activates the transmitter and then
842it waits for the transmitter to be active again.
843
844must be called with the lock held and interrupts disabled.
845*/
846static 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
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400856 active, no need to do the expensive thing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 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/*
895deactivate_transmitter disables the transmitter on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400896To achieve this this code disables the transmitter first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897then it waits for the transmitter to become inactive.
898
899must be called with the lock held and interrupts disabled.
900*/
901static 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/*
928enable_transmit_interrupt enables the transmit interrupt
929
930must be called with the lock held and interrupts disabled.
931*/
932static 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/*
946enable_receive_interrupt enables the receive interrupt
947
948must be called with the lock held and interrupts disabled.
949*/
950static 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/*
963enable_link_interrupt enables the link status change interrupt
964
965must be called with the lock held and interrupts disabled.
966*/
967static 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/*
982disable_all_interrupts disables all interrupts
983
984must be called with the lock held and interrupts disabled.
985*/
986static void disable_all_interrupts(struct xircom_private *card)
987{
988 unsigned int val;
989 enter("enable_all_interrupts");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 val = 0; /* disable all interrupts */
992 outl(val, card->io_port + CSR7);
993
994 leave("disable_all_interrupts");
995}
996
997/*
998enable_common_interrupts enables several weird interrupts
999
1000must be called with the lock held and interrupts disabled.
1001*/
1002static 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/*
1022enable_promisc starts promisc mode
1023
1024must be called with the lock held and interrupts disabled.
1025*/
1026static int enable_promisc(struct xircom_private *card)
1027{
1028 unsigned int val;
1029 enter("enable_promisc");
1030
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001031 val = inl(card->io_port + CSR6);
1032 val = val | (1 << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 outl(val, card->io_port + CSR6);
1034
1035 leave("enable_promisc");
1036 return 1;
1037}
1038
1039
1040
1041
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001042/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02001043link_status() checks the links status and will return 0 for no link, 10 for 10mbit link and 100 for.. guess what.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045Must be called in locked state with interrupts disabled
1046*/
1047static int link_status(struct xircom_private *card)
1048{
1049 unsigned int val;
1050 enter("link_status");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 val = inb(card->io_port + CSR12);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 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;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001058
1059 /* If we get here -> no link at all */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
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.
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 This function will take the spinlock itself and can, as a result, not be called with the lock helt.
1073 */
1074static 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");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 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)) {
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001095 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 * 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 */
1122static 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);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001136 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 outl(0xa8050000, card->io_port + CSR15);
1138 udelay(25);
1139 outl(0xa00f0000, card->io_port + CSR15);
1140 udelay(25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 spin_unlock_irqrestore(&card->lock, flags);
1143
1144 netif_start_queue(card->dev);
1145 leave("transceiver_voodoo");
1146}
1147
1148
1149static 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
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 enable_link_interrupt(card);
1165 enable_transmit_interrupt(card);
1166 enable_receive_interrupt(card);
1167 enable_common_interrupts(card);
1168 enable_promisc(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 /* The card can have received packets already, read them away now */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001171 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 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 */
1183static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset)
1184{
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001185 int status;
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 enter("investigate_read_descriptor");
1188 status = card->rx_buffer[4*descnr];
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if ((status > 0)) { /* packet received */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 /* TODO: discard error packets */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 skb_reserve(skb, 2);
David S. Miller8c7b7fa2007-07-10 22:08:12 -07001208 skb_copy_to_linear_data(skb, (unsigned char*)&card->rx_buffer[bufferoffset / 4], pkt_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 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;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 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 */
1228static 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");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 status = card->tx_buffer[4*descnr];
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001235#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 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");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
1260
1261static int __init xircom_init(void)
1262{
Alexey Dobriyanec42cdb2006-08-14 23:00:28 -07001263 return pci_register_driver(&xircom_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
1266static void __exit xircom_exit(void)
1267{
1268 pci_unregister_driver(&xircom_ops);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001269}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001271module_init(xircom_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272module_exit(xircom_exit)
1273