blob: 70befe33e454c63d1a8e4dd4ba2fab0fb728d4a3 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 SET_NETDEV_DEV(dev, &pdev->dev);
256
257
258 private->dev = dev;
259 private->pdev = pdev;
260 private->io_port = pci_resource_start(pdev, 0);
261 spin_lock_init(&private->lock);
262 dev->irq = pdev->irq;
263 dev->base_addr = private->io_port;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 initialize_card(private);
266 read_mac_address(private);
267 setup_descriptors(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 dev->open = &xircom_open;
270 dev->hard_start_xmit = &xircom_start_xmit;
271 dev->stop = &xircom_close;
272 dev->get_stats = &xircom_get_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#ifdef CONFIG_NET_POLL_CONTROLLER
274 dev->poll_controller = &xircom_poll_controller;
275#endif
276 SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
277 pci_set_drvdata(pdev, dev);
278
279 if (register_netdev(dev)) {
280 printk(KERN_ERR "xircom_probe: netdevice registration failed.\n");
281 goto reg_fail;
282 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400283
Auke Kok44c10132007-06-08 15:46:36 -0700284 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 -0700285 /* start the transmitter to get a heartbeat */
286 /* TODO: send 2 dummy packets here */
287 transceiver_voodoo(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 spin_lock_irqsave(&private->lock,flags);
290 activate_transmitter(private);
291 activate_receiver(private);
292 spin_unlock_irqrestore(&private->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 trigger_receive(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 leave("xircom_probe");
297 return 0;
298
299reg_fail:
300 kfree(private->tx_buffer);
301tx_buf_fail:
302 kfree(private->rx_buffer);
303rx_buf_fail:
304 free_netdev(dev);
305device_fail:
306 return -ENODEV;
307}
308
309
310/*
311 xircom_remove is called on module-unload or on device-eject.
312 it unregisters the irq, io-region and network device.
313 Interrupts and such are already stopped in the "ifconfig ethX down"
314 code.
315 */
316static void __devexit xircom_remove(struct pci_dev *pdev)
317{
318 struct net_device *dev = pci_get_drvdata(pdev);
319 struct xircom_private *card = netdev_priv(dev);
320
321 enter("xircom_remove");
322 pci_free_consistent(pdev,8192,card->rx_buffer,card->rx_dma_handle);
323 pci_free_consistent(pdev,8192,card->tx_buffer,card->tx_dma_handle);
324
325 release_region(dev->base_addr, 128);
326 unregister_netdev(dev);
327 free_netdev(dev);
328 pci_set_drvdata(pdev, NULL);
329 leave("xircom_remove");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400330}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
David Howells7d12e782006-10-05 14:55:46 +0100332static irqreturn_t xircom_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 struct net_device *dev = (struct net_device *) dev_instance;
335 struct xircom_private *card = netdev_priv(dev);
336 unsigned int status;
337 int i;
338
339 enter("xircom_interrupt\n");
340
341 spin_lock(&card->lock);
342 status = inl(card->io_port+CSR5);
343
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400344#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 print_binary(status);
346 printk("tx status 0x%08x 0x%08x \n",card->tx_buffer[0],card->tx_buffer[4]);
347 printk("rx status 0x%08x 0x%08x \n",card->rx_buffer[0],card->rx_buffer[4]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400348#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* Handle shared irq and hotplug */
350 if (status == 0 || status == 0xffffffff) {
351 spin_unlock(&card->lock);
352 return IRQ_NONE;
353 }
354
355 if (link_status_changed(card)) {
356 int newlink;
357 printk(KERN_DEBUG "xircom_cb: Link status has changed \n");
358 newlink = link_status(card);
359 printk(KERN_INFO "xircom_cb: Link is %i mbit \n",newlink);
360 if (newlink)
361 netif_carrier_on(dev);
362 else
363 netif_carrier_off(dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400367 /* Clear all remaining interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 status |= 0xffffffff; /* FIXME: make this clear only the
369 real existing bits */
370 outl(status,card->io_port+CSR5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400372
373 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 investigate_write_descriptor(dev,card,i,bufferoffsets[i]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400375 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 investigate_read_descriptor(dev,card,i,bufferoffsets[i]);
377
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 spin_unlock(&card->lock);
380 leave("xircom_interrupt");
381 return IRQ_HANDLED;
382}
383
384static int xircom_start_xmit(struct sk_buff *skb, struct net_device *dev)
385{
386 struct xircom_private *card;
387 unsigned long flags;
388 int nextdescriptor;
389 int desc;
390 enter("xircom_start_xmit");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 card = netdev_priv(dev);
393 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* First see if we can free some descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400396 for (desc=0;desc<NUMDESCRIPTORS;desc++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 investigate_write_descriptor(dev,card,desc,bufferoffsets[desc]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400398
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 nextdescriptor = (card->transmit_used +1) % (NUMDESCRIPTORS);
401 desc = card->transmit_used;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /* only send the packet if the descriptor is free */
404 if (card->tx_buffer[4*desc]==0) {
405 /* Copy the packet data; zero the memory first as the card
406 sometimes sends more than you ask it to. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 memset(&card->tx_buffer[bufferoffsets[desc]/4],0,1536);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300409 skb_copy_from_linear_data(skb,
410 &(card->tx_buffer[bufferoffsets[desc] / 4]),
411 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* FIXME: The specification tells us that the length we send HAS to be a multiple of
413 4 bytes. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 card->tx_buffer[4*desc+1] = skb->len;
416 if (desc == NUMDESCRIPTORS-1)
417 card->tx_buffer[4*desc+1] |= (1<<25); /* bit 25: last descriptor of the ring */
418
419 card->tx_buffer[4*desc+1] |= 0xF0000000;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400420 /* 0xF0... means want interrupts*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 card->tx_skb[desc] = skb;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 wmb();
424 /* This gives the descriptor to the card */
425 card->tx_buffer[4*desc] = 0x80000000;
426 trigger_transmit(card);
427 if (((int)card->tx_buffer[nextdescriptor*4])<0) { /* next descriptor is occupied... */
428 netif_stop_queue(dev);
429 }
430 card->transmit_used = nextdescriptor;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400431 leave("xircom-start_xmit - sent");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 spin_unlock_irqrestore(&card->lock,flags);
433 return 0;
434 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437
438 /* Uh oh... no free descriptor... drop the packet */
439 netif_stop_queue(dev);
440 spin_unlock_irqrestore(&card->lock,flags);
441 trigger_transmit(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return -EIO;
444}
445
446
447
448
449static int xircom_open(struct net_device *dev)
450{
451 struct xircom_private *xp = netdev_priv(dev);
452 int retval;
453 enter("xircom_open");
454 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 -0700455 retval = request_irq(dev->irq, &xircom_interrupt, IRQF_SHARED, dev->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (retval) {
457 leave("xircom_open - No IRQ");
458 return retval;
459 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 xircom_up(xp);
462 xp->open = 1;
463 leave("xircom_open");
464 return 0;
465}
466
467static int xircom_close(struct net_device *dev)
468{
469 struct xircom_private *card;
470 unsigned long flags;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 enter("xircom_close");
473 card = netdev_priv(dev);
474 netif_stop_queue(dev); /* we don't want new packets */
475
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 disable_all_interrupts(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400480#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 /* We can enable this again once we send dummy packets on ifconfig ethX up */
482 deactivate_receiver(card);
483 deactivate_transmitter(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400484#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 remove_descriptors(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 spin_unlock_irqrestore(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 card->open = 0;
490 free_irq(dev->irq,dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 leave("xircom_close");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return 0;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498
499
500static struct net_device_stats *xircom_get_stats(struct net_device *dev)
501{
502 struct xircom_private *card = netdev_priv(dev);
503 return &card->stats;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400504}
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507#ifdef CONFIG_NET_POLL_CONTROLLER
508static void xircom_poll_controller(struct net_device *dev)
509{
510 disable_irq(dev->irq);
David Howells7d12e782006-10-05 14:55:46 +0100511 xircom_interrupt(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 enable_irq(dev->irq);
513}
514#endif
515
516
517static void initialize_card(struct xircom_private *card)
518{
519 unsigned int val;
520 unsigned long flags;
521 enter("initialize_card");
522
523
524 spin_lock_irqsave(&card->lock, flags);
525
526 /* First: reset the card */
527 val = inl(card->io_port + CSR0);
528 val |= 0x01; /* Software reset */
529 outl(val, card->io_port + CSR0);
530
531 udelay(100); /* give the card some time to reset */
532
533 val = inl(card->io_port + CSR0);
534 val &= ~0x01; /* disable Software reset */
535 outl(val, card->io_port + CSR0);
536
537
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400538 val = 0; /* Value 0x00 is a safe and conservative value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 for the PCI configuration settings */
540 outl(val, card->io_port + CSR0);
541
542
543 disable_all_interrupts(card);
544 deactivate_receiver(card);
545 deactivate_transmitter(card);
546
547 spin_unlock_irqrestore(&card->lock, flags);
548
549 leave("initialize_card");
550}
551
552/*
553trigger_transmit causes the card to check for frames to be transmitted.
554This is accomplished by writing to the CSR1 port. The documentation
555claims that the act of writing is sufficient and that the value is
556ignored; I chose zero.
557*/
558static void trigger_transmit(struct xircom_private *card)
559{
560 unsigned int val;
561 enter("trigger_transmit");
562
563 val = 0;
564 outl(val, card->io_port + CSR1);
565
566 leave("trigger_transmit");
567}
568
569/*
570trigger_receive causes the card to check for empty frames in the
571descriptor list in which packets can be received.
572This is accomplished by writing to the CSR2 port. The documentation
573claims that the act of writing is sufficient and that the value is
574ignored; I chose zero.
575*/
576static void trigger_receive(struct xircom_private *card)
577{
578 unsigned int val;
579 enter("trigger_receive");
580
581 val = 0;
582 outl(val, card->io_port + CSR2);
583
584 leave("trigger_receive");
585}
586
587/*
588setup_descriptors initializes the send and receive buffers to be valid
589descriptors and programs the addresses into the card.
590*/
591static void setup_descriptors(struct xircom_private *card)
592{
593 unsigned int val;
594 unsigned int address;
595 int i;
596 enter("setup_descriptors");
597
598
Eric Sesterhenn / snakebytea707cd62006-01-26 22:02:51 +0100599 BUG_ON(card->rx_buffer == NULL);
600 BUG_ON(card->tx_buffer == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /* Receive descriptors */
603 memset(card->rx_buffer, 0, 128); /* clear the descriptors */
604 for (i=0;i<NUMDESCRIPTORS;i++ ) {
605
606 /* Rx Descr0: It's empty, let the card own it, no errors -> 0x80000000 */
607 card->rx_buffer[i*4 + 0] = 0x80000000;
608 /* Rx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
609 card->rx_buffer[i*4 + 1] = 1536;
610 if (i==NUMDESCRIPTORS-1)
611 card->rx_buffer[i*4 + 1] |= (1 << 25); /* bit 25 is "last descriptor" */
612
613 /* Rx Descr2: address of the buffer
614 we store the buffer at the 2nd half of the page */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 address = (unsigned long) card->rx_dma_handle;
617 card->rx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
618 /* Rx Desc3: address of 2nd buffer -> 0 */
619 card->rx_buffer[i*4 + 3] = 0;
620 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 wmb();
623 /* Write the receive descriptor ring address to the card */
624 address = (unsigned long) card->rx_dma_handle;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400625 val = cpu_to_le32(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 outl(val, card->io_port + CSR3); /* Receive descr list address */
627
628
629 /* transmit descriptors */
630 memset(card->tx_buffer, 0, 128); /* clear the descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 for (i=0;i<NUMDESCRIPTORS;i++ ) {
633 /* Tx Descr0: Empty, we own it, no errors -> 0x00000000 */
634 card->tx_buffer[i*4 + 0] = 0x00000000;
635 /* Tx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
636 card->tx_buffer[i*4 + 1] = 1536;
637 if (i==NUMDESCRIPTORS-1)
638 card->tx_buffer[i*4 + 1] |= (1 << 25); /* bit 25 is "last descriptor" */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /* Tx Descr2: address of the buffer
641 we store the buffer at the 2nd half of the page */
642 address = (unsigned long) card->tx_dma_handle;
643 card->tx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
644 /* Tx Desc3: address of 2nd buffer -> 0 */
645 card->tx_buffer[i*4 + 3] = 0;
646 }
647
648 wmb();
649 /* wite the transmit descriptor ring to the card */
650 address = (unsigned long) card->tx_dma_handle;
651 val =cpu_to_le32(address);
652 outl(val, card->io_port + CSR4); /* xmit descr list address */
653
654 leave("setup_descriptors");
655}
656
657/*
658remove_descriptors informs the card the descriptors are no longer
659valid by setting the address in the card to 0x00.
660*/
661static void remove_descriptors(struct xircom_private *card)
662{
663 unsigned int val;
664 enter("remove_descriptors");
665
666 val = 0;
667 outl(val, card->io_port + CSR3); /* Receive descriptor address */
668 outl(val, card->io_port + CSR4); /* Send descriptor address */
669
670 leave("remove_descriptors");
671}
672
673/*
674link_status_changed returns 1 if the card has indicated that
675the link status has changed. The new link status has to be read from CSR12.
676
677This function also clears the status-bit.
678*/
679static int link_status_changed(struct xircom_private *card)
680{
681 unsigned int val;
682 enter("link_status_changed");
683
684 val = inl(card->io_port + CSR5); /* Status register */
685
686 if ((val & (1 << 27)) == 0) { /* no change */
687 leave("link_status_changed - nochange");
688 return 0;
689 }
690
691 /* clear the event by writing a 1 to the bit in the
692 status register. */
693 val = (1 << 27);
694 outl(val, card->io_port + CSR5);
695
696 leave("link_status_changed - changed");
697 return 1;
698}
699
700
701/*
702transmit_active returns 1 if the transmitter on the card is
703in a non-stopped state.
704*/
705static int transmit_active(struct xircom_private *card)
706{
707 unsigned int val;
708 enter("transmit_active");
709
710 val = inl(card->io_port + CSR5); /* Status register */
711
712 if ((val & (7 << 20)) == 0) { /* transmitter disabled */
713 leave("transmit_active - inactive");
714 return 0;
715 }
716
717 leave("transmit_active - active");
718 return 1;
719}
720
721/*
722receive_active returns 1 if the receiver on the card is
723in a non-stopped state.
724*/
725static int receive_active(struct xircom_private *card)
726{
727 unsigned int val;
728 enter("receive_active");
729
730
731 val = inl(card->io_port + CSR5); /* Status register */
732
733 if ((val & (7 << 17)) == 0) { /* receiver disabled */
734 leave("receive_active - inactive");
735 return 0;
736 }
737
738 leave("receive_active - active");
739 return 1;
740}
741
742/*
743activate_receiver enables the receiver on the card.
744Before being allowed to active the receiver, the receiver
745must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400746this code actually disables the receiver first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747receiver to become inactive, then it activates the receiver and then
748it waits for the receiver to be active.
749
750must be called with the lock held and interrupts disabled.
751*/
752static void activate_receiver(struct xircom_private *card)
753{
754 unsigned int val;
755 int counter;
756 enter("activate_receiver");
757
758
759 val = inl(card->io_port + CSR6); /* Operation mode */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* If the "active" bit is set and the receiver is already
762 active, no need to do the expensive thing */
763 if ((val&2) && (receive_active(card)))
764 return;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400765
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 val = val & ~2; /* disable the receiver */
768 outl(val, card->io_port + CSR6);
769
770 counter = 10;
771 while (counter > 0) {
772 if (!receive_active(card))
773 break;
774 /* wait a while */
775 udelay(50);
776 counter--;
777 if (counter <= 0)
778 printk(KERN_ERR "xircom_cb: Receiver failed to deactivate\n");
779 }
780
781 /* enable the receiver */
782 val = inl(card->io_port + CSR6); /* Operation mode */
783 val = val | 2; /* enable the receiver */
784 outl(val, card->io_port + CSR6);
785
786 /* now wait for the card to activate again */
787 counter = 10;
788 while (counter > 0) {
789 if (receive_active(card))
790 break;
791 /* wait a while */
792 udelay(50);
793 counter--;
794 if (counter <= 0)
795 printk(KERN_ERR "xircom_cb: Receiver failed to re-activate\n");
796 }
797
798 leave("activate_receiver");
799}
800
801/*
802deactivate_receiver disables the receiver on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400803To achieve this this code disables the receiver first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804then it waits for the receiver to become inactive.
805
806must be called with the lock held and interrupts disabled.
807*/
808static void deactivate_receiver(struct xircom_private *card)
809{
810 unsigned int val;
811 int counter;
812 enter("deactivate_receiver");
813
814 val = inl(card->io_port + CSR6); /* Operation mode */
815 val = val & ~2; /* disable the receiver */
816 outl(val, card->io_port + CSR6);
817
818 counter = 10;
819 while (counter > 0) {
820 if (!receive_active(card))
821 break;
822 /* wait a while */
823 udelay(50);
824 counter--;
825 if (counter <= 0)
826 printk(KERN_ERR "xircom_cb: Receiver failed to deactivate\n");
827 }
828
829
830 leave("deactivate_receiver");
831}
832
833
834/*
835activate_transmitter enables the transmitter on the card.
836Before being allowed to active the transmitter, the transmitter
837must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400838this code actually disables the transmitter first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839transmitter to become inactive, then it activates the transmitter and then
840it waits for the transmitter to be active again.
841
842must be called with the lock held and interrupts disabled.
843*/
844static void activate_transmitter(struct xircom_private *card)
845{
846 unsigned int val;
847 int counter;
848 enter("activate_transmitter");
849
850
851 val = inl(card->io_port + CSR6); /* Operation mode */
852
853 /* If the "active" bit is set and the receiver is already
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400854 active, no need to do the expensive thing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if ((val&(1<<13)) && (transmit_active(card)))
856 return;
857
858 val = val & ~(1 << 13); /* disable the transmitter */
859 outl(val, card->io_port + CSR6);
860
861 counter = 10;
862 while (counter > 0) {
863 if (!transmit_active(card))
864 break;
865 /* wait a while */
866 udelay(50);
867 counter--;
868 if (counter <= 0)
869 printk(KERN_ERR "xircom_cb: Transmitter failed to deactivate\n");
870 }
871
872 /* enable the transmitter */
873 val = inl(card->io_port + CSR6); /* Operation mode */
874 val = val | (1 << 13); /* enable the transmitter */
875 outl(val, card->io_port + CSR6);
876
877 /* now wait for the card to activate again */
878 counter = 10;
879 while (counter > 0) {
880 if (transmit_active(card))
881 break;
882 /* wait a while */
883 udelay(50);
884 counter--;
885 if (counter <= 0)
886 printk(KERN_ERR "xircom_cb: Transmitter failed to re-activate\n");
887 }
888
889 leave("activate_transmitter");
890}
891
892/*
893deactivate_transmitter disables the transmitter on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400894To achieve this this code disables the transmitter first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895then it waits for the transmitter to become inactive.
896
897must be called with the lock held and interrupts disabled.
898*/
899static void deactivate_transmitter(struct xircom_private *card)
900{
901 unsigned int val;
902 int counter;
903 enter("deactivate_transmitter");
904
905 val = inl(card->io_port + CSR6); /* Operation mode */
906 val = val & ~2; /* disable the transmitter */
907 outl(val, card->io_port + CSR6);
908
909 counter = 20;
910 while (counter > 0) {
911 if (!transmit_active(card))
912 break;
913 /* wait a while */
914 udelay(50);
915 counter--;
916 if (counter <= 0)
917 printk(KERN_ERR "xircom_cb: Transmitter failed to deactivate\n");
918 }
919
920
921 leave("deactivate_transmitter");
922}
923
924
925/*
926enable_transmit_interrupt enables the transmit interrupt
927
928must be called with the lock held and interrupts disabled.
929*/
930static void enable_transmit_interrupt(struct xircom_private *card)
931{
932 unsigned int val;
933 enter("enable_transmit_interrupt");
934
935 val = inl(card->io_port + CSR7); /* Interrupt enable register */
936 val |= 1; /* enable the transmit interrupt */
937 outl(val, card->io_port + CSR7);
938
939 leave("enable_transmit_interrupt");
940}
941
942
943/*
944enable_receive_interrupt enables the receive interrupt
945
946must be called with the lock held and interrupts disabled.
947*/
948static void enable_receive_interrupt(struct xircom_private *card)
949{
950 unsigned int val;
951 enter("enable_receive_interrupt");
952
953 val = inl(card->io_port + CSR7); /* Interrupt enable register */
954 val = val | (1 << 6); /* enable the receive interrupt */
955 outl(val, card->io_port + CSR7);
956
957 leave("enable_receive_interrupt");
958}
959
960/*
961enable_link_interrupt enables the link status change interrupt
962
963must be called with the lock held and interrupts disabled.
964*/
965static void enable_link_interrupt(struct xircom_private *card)
966{
967 unsigned int val;
968 enter("enable_link_interrupt");
969
970 val = inl(card->io_port + CSR7); /* Interrupt enable register */
971 val = val | (1 << 27); /* enable the link status chage interrupt */
972 outl(val, card->io_port + CSR7);
973
974 leave("enable_link_interrupt");
975}
976
977
978
979/*
980disable_all_interrupts disables all interrupts
981
982must be called with the lock held and interrupts disabled.
983*/
984static void disable_all_interrupts(struct xircom_private *card)
985{
986 unsigned int val;
987 enter("enable_all_interrupts");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 val = 0; /* disable all interrupts */
990 outl(val, card->io_port + CSR7);
991
992 leave("disable_all_interrupts");
993}
994
995/*
996enable_common_interrupts enables several weird interrupts
997
998must be called with the lock held and interrupts disabled.
999*/
1000static void enable_common_interrupts(struct xircom_private *card)
1001{
1002 unsigned int val;
1003 enter("enable_link_interrupt");
1004
1005 val = inl(card->io_port + CSR7); /* Interrupt enable register */
1006 val |= (1<<16); /* Normal Interrupt Summary */
1007 val |= (1<<15); /* Abnormal Interrupt Summary */
1008 val |= (1<<13); /* Fatal bus error */
1009 val |= (1<<8); /* Receive Process Stopped */
1010 val |= (1<<7); /* Receive Buffer Unavailable */
1011 val |= (1<<5); /* Transmit Underflow */
1012 val |= (1<<2); /* Transmit Buffer Unavailable */
1013 val |= (1<<1); /* Transmit Process Stopped */
1014 outl(val, card->io_port + CSR7);
1015
1016 leave("enable_link_interrupt");
1017}
1018
1019/*
1020enable_promisc starts promisc mode
1021
1022must be called with the lock held and interrupts disabled.
1023*/
1024static int enable_promisc(struct xircom_private *card)
1025{
1026 unsigned int val;
1027 enter("enable_promisc");
1028
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001029 val = inl(card->io_port + CSR6);
1030 val = val | (1 << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 outl(val, card->io_port + CSR6);
1032
1033 leave("enable_promisc");
1034 return 1;
1035}
1036
1037
1038
1039
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001040/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02001041link_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 -07001042
1043Must be called in locked state with interrupts disabled
1044*/
1045static int link_status(struct xircom_private *card)
1046{
1047 unsigned int val;
1048 enter("link_status");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 val = inb(card->io_port + CSR12);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (!(val&(1<<2))) /* bit 2 is 0 for 10mbit link, 1 for not an 10mbit link */
1053 return 10;
1054 if (!(val&(1<<1))) /* bit 1 is 0 for 100mbit link, 1 for not an 100mbit link */
1055 return 100;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001056
1057 /* If we get here -> no link at all */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 leave("link_status");
1060 return 0;
1061}
1062
1063
1064
1065
1066
1067/*
1068 read_mac_address() reads the MAC address from the NIC and stores it in the "dev" structure.
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 This function will take the spinlock itself and can, as a result, not be called with the lock helt.
1071 */
1072static void read_mac_address(struct xircom_private *card)
1073{
1074 unsigned char j, tuple, link, data_id, data_count;
1075 unsigned long flags;
1076 int i;
Joe Perches0795af52007-10-03 17:59:30 -07001077 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 enter("read_mac_address");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 spin_lock_irqsave(&card->lock, flags);
1082
1083 outl(1 << 12, card->io_port + CSR9); /* enable boot rom access */
1084 for (i = 0x100; i < 0x1f7; i += link + 2) {
1085 outl(i, card->io_port + CSR10);
1086 tuple = inl(card->io_port + CSR9) & 0xff;
1087 outl(i + 1, card->io_port + CSR10);
1088 link = inl(card->io_port + CSR9) & 0xff;
1089 outl(i + 2, card->io_port + CSR10);
1090 data_id = inl(card->io_port + CSR9) & 0xff;
1091 outl(i + 3, card->io_port + CSR10);
1092 data_count = inl(card->io_port + CSR9) & 0xff;
1093 if ((tuple == 0x22) && (data_id == 0x04) && (data_count == 0x06)) {
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001094 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 * This is it. We have the data we want.
1096 */
1097 for (j = 0; j < 6; j++) {
1098 outl(i + j + 4, card->io_port + CSR10);
1099 card->dev->dev_addr[j] = inl(card->io_port + CSR9) & 0xff;
1100 }
1101 break;
1102 } else if (link == 0) {
1103 break;
1104 }
1105 }
1106 spin_unlock_irqrestore(&card->lock, flags);
Joe Perches0795af52007-10-03 17:59:30 -07001107 pr_debug(" %s\n", print_mac(mac, card->dev->dev_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 leave("read_mac_address");
1109}
1110
1111
1112/*
1113 transceiver_voodoo() enables the external UTP plug thingy.
1114 it's called voodoo as I stole this code and cannot cross-reference
1115 it with the specification.
1116 */
1117static void transceiver_voodoo(struct xircom_private *card)
1118{
1119 unsigned long flags;
1120
1121 enter("transceiver_voodoo");
1122
1123 /* disable all powermanagement */
1124 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1125
1126 setup_descriptors(card);
1127
1128 spin_lock_irqsave(&card->lock, flags);
1129
1130 outl(0x0008, card->io_port + CSR15);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001131 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 outl(0xa8050000, card->io_port + CSR15);
1133 udelay(25);
1134 outl(0xa00f0000, card->io_port + CSR15);
1135 udelay(25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 spin_unlock_irqrestore(&card->lock, flags);
1138
1139 netif_start_queue(card->dev);
1140 leave("transceiver_voodoo");
1141}
1142
1143
1144static void xircom_up(struct xircom_private *card)
1145{
1146 unsigned long flags;
1147 int i;
1148
1149 enter("xircom_up");
1150
1151 /* disable all powermanagement */
1152 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1153
1154 setup_descriptors(card);
1155
1156 spin_lock_irqsave(&card->lock, flags);
1157
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 enable_link_interrupt(card);
1160 enable_transmit_interrupt(card);
1161 enable_receive_interrupt(card);
1162 enable_common_interrupts(card);
1163 enable_promisc(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /* The card can have received packets already, read them away now */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001166 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 investigate_read_descriptor(card->dev,card,i,bufferoffsets[i]);
1168
1169
1170 spin_unlock_irqrestore(&card->lock, flags);
1171 trigger_receive(card);
1172 trigger_transmit(card);
1173 netif_start_queue(card->dev);
1174 leave("xircom_up");
1175}
1176
1177/* Bufferoffset is in BYTES */
1178static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset)
1179{
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001180 int status;
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 enter("investigate_read_descriptor");
1183 status = card->rx_buffer[4*descnr];
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if ((status > 0)) { /* packet received */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 /* TODO: discard error packets */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 short pkt_len = ((status >> 16) & 0x7ff) - 4; /* minus 4, we don't want the CRC */
1190 struct sk_buff *skb;
1191
1192 if (pkt_len > 1518) {
1193 printk(KERN_ERR "xircom_cb: Packet length %i is bogus \n",pkt_len);
1194 pkt_len = 1518;
1195 }
1196
1197 skb = dev_alloc_skb(pkt_len + 2);
1198 if (skb == NULL) {
1199 card->stats.rx_dropped++;
1200 goto out;
1201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 skb_reserve(skb, 2);
David S. Miller8c7b7fa2007-07-10 22:08:12 -07001203 skb_copy_to_linear_data(skb, (unsigned char*)&card->rx_buffer[bufferoffset / 4], pkt_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 skb_put(skb, pkt_len);
1205 skb->protocol = eth_type_trans(skb, dev);
1206 netif_rx(skb);
1207 dev->last_rx = jiffies;
1208 card->stats.rx_packets++;
1209 card->stats.rx_bytes += pkt_len;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 out:
1212 /* give the buffer back to the card */
1213 card->rx_buffer[4*descnr] = 0x80000000;
1214 trigger_receive(card);
1215 }
1216
1217 leave("investigate_read_descriptor");
1218
1219}
1220
1221
1222/* Bufferoffset is in BYTES */
1223static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset)
1224{
1225 int status;
1226
1227 enter("investigate_write_descriptor");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 status = card->tx_buffer[4*descnr];
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001230#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (status & 0x8000) { /* Major error */
1232 printk(KERN_ERR "Major transmit error status %x \n", status);
1233 card->tx_buffer[4*descnr] = 0;
1234 netif_wake_queue (dev);
1235 }
1236#endif
1237 if (status > 0) { /* bit 31 is 0 when done */
1238 if (card->tx_skb[descnr]!=NULL) {
1239 card->stats.tx_bytes += card->tx_skb[descnr]->len;
1240 dev_kfree_skb_irq(card->tx_skb[descnr]);
1241 }
1242 card->tx_skb[descnr] = NULL;
1243 /* Bit 8 in the status field is 1 if there was a collision */
1244 if (status&(1<<8))
1245 card->stats.collisions++;
1246 card->tx_buffer[4*descnr] = 0; /* descriptor is free again */
1247 netif_wake_queue (dev);
1248 card->stats.tx_packets++;
1249 }
1250
1251 leave("investigate_write_descriptor");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
1255
1256static int __init xircom_init(void)
1257{
Alexey Dobriyanec42cdb2006-08-14 23:00:28 -07001258 return pci_register_driver(&xircom_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
1261static void __exit xircom_exit(void)
1262{
1263 pci_unregister_driver(&xircom_ops);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001264}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001266module_init(xircom_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267module_exit(xircom_exit)
1268