blob: 0f2ca5980c3c19981f2926d5474ce490b00d5dfd [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
Al Viro6f35d5d2007-12-23 20:01:04 +000086 __le32 *rx_buffer;
87 __le32 *tx_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct pci_dev *pdev;
108 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
111
112/* Function prototypes */
113static int xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id);
114static void xircom_remove(struct pci_dev *pdev);
David Howells7d12e782006-10-05 14:55:46 +0100115static irqreturn_t xircom_interrupt(int irq, void *dev_instance);
Stephen Hemmingerad096462009-08-31 19:50:53 +0000116static netdev_tx_t xircom_start_xmit(struct sk_buff *skb,
117 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static int xircom_open(struct net_device *dev);
119static int xircom_close(struct net_device *dev);
120static void xircom_up(struct xircom_private *card);
Keith Owens3be034b2005-09-13 15:05:13 +1000121#ifdef CONFIG_NET_POLL_CONTROLLER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static void xircom_poll_controller(struct net_device *dev);
123#endif
124
125static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset);
126static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset);
127static void read_mac_address(struct xircom_private *card);
128static void transceiver_voodoo(struct xircom_private *card);
129static void initialize_card(struct xircom_private *card);
130static void trigger_transmit(struct xircom_private *card);
131static void trigger_receive(struct xircom_private *card);
132static void setup_descriptors(struct xircom_private *card);
133static void remove_descriptors(struct xircom_private *card);
134static int link_status_changed(struct xircom_private *card);
135static void activate_receiver(struct xircom_private *card);
136static void deactivate_receiver(struct xircom_private *card);
137static void activate_transmitter(struct xircom_private *card);
138static void deactivate_transmitter(struct xircom_private *card);
139static void enable_transmit_interrupt(struct xircom_private *card);
140static void enable_receive_interrupt(struct xircom_private *card);
141static void enable_link_interrupt(struct xircom_private *card);
142static void disable_all_interrupts(struct xircom_private *card);
143static int link_status(struct xircom_private *card);
144
145
146
147static struct pci_device_id xircom_pci_table[] = {
148 {0x115D, 0x0003, PCI_ANY_ID, PCI_ANY_ID,},
149 {0,},
150};
151MODULE_DEVICE_TABLE(pci, xircom_pci_table);
152
153static struct pci_driver xircom_ops = {
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400154 .name = "xircom_cb",
155 .id_table = xircom_pci_table,
156 .probe = xircom_probe,
157 .remove = xircom_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .suspend =NULL,
159 .resume =NULL
160};
161
162
163#ifdef DEBUG
164static void print_binary(unsigned int number)
165{
166 int i,i2;
167 char buffer[64];
168 memset(buffer,0,64);
169 i2=0;
170 for (i=31;i>=0;i--) {
171 if (number & (1<<i))
172 buffer[i2++]='1';
173 else
174 buffer[i2++]='0';
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400175 if ((i&3)==0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 buffer[i2++]=' ';
177 }
178 printk("%s\n",buffer);
179}
180#endif
181
182static void netdev_get_drvinfo(struct net_device *dev,
183 struct ethtool_drvinfo *info)
184{
185 struct xircom_private *private = netdev_priv(dev);
186
187 strcpy(info->driver, "xircom_cb");
188 strcpy(info->bus_info, pci_name(private->pdev));
189}
190
Jeff Garzik7282d492006-09-13 14:30:00 -0400191static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .get_drvinfo = netdev_get_drvinfo,
193};
194
Stephen Hemminger1034c9f2009-01-07 18:00:55 -0800195static const struct net_device_ops netdev_ops = {
196 .ndo_open = xircom_open,
197 .ndo_stop = xircom_close,
198 .ndo_start_xmit = xircom_start_xmit,
199 .ndo_change_mtu = eth_change_mtu,
200 .ndo_set_mac_address = eth_mac_addr,
201 .ndo_validate_addr = eth_validate_addr,
202#ifdef CONFIG_NET_POLL_CONTROLLER
203 .ndo_poll_controller = xircom_poll_controller,
204#endif
205};
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/* xircom_probe is the code that gets called on device insertion.
208 it sets up the hardware and registers the device to the networklayer.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 TODO: Send 1 or 2 "dummy" packets here as the card seems to discard the
211 first two packets that get send, and pump hates that.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 */
214static int __devinit xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id)
215{
216 struct net_device *dev = NULL;
217 struct xircom_private *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 unsigned long flags;
219 unsigned short tmp16;
220 enter("xircom_probe");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* First do the PCI initialisation */
223
224 if (pci_enable_device(pdev))
225 return -ENODEV;
226
227 /* disable all powermanagement */
228 pci_write_config_dword(pdev, PCI_POWERMGMT, 0x0000);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 pci_set_master(pdev); /* Why isn't this done by pci_enable_device ?*/
231
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400232 /* clear PCI status, if any */
233 pci_read_config_word (pdev,PCI_STATUS, &tmp16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 pci_write_config_word (pdev, PCI_STATUS,tmp16);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (!request_region(pci_resource_start(pdev, 0), 128, "xircom_cb")) {
237 printk(KERN_ERR "xircom_probe: failed to allocate io-region\n");
238 return -ENODEV;
239 }
240
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400241 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 Before changing the hardware, allocate the memory.
243 This way, we can fail gracefully if not enough memory
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400244 is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
246 dev = alloc_etherdev(sizeof(struct xircom_private));
247 if (!dev) {
248 printk(KERN_ERR "xircom_probe: failed to allocate etherdev\n");
249 goto device_fail;
250 }
251 private = netdev_priv(dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /* Allocate the send/receive buffers */
254 private->rx_buffer = pci_alloc_consistent(pdev,8192,&private->rx_dma_handle);
255 if (private->rx_buffer == NULL) {
256 printk(KERN_ERR "xircom_probe: no memory for rx buffer \n");
257 goto rx_buf_fail;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 private->tx_buffer = pci_alloc_consistent(pdev,8192,&private->tx_dma_handle);
260 if (private->tx_buffer == NULL) {
261 printk(KERN_ERR "xircom_probe: no memory for tx buffer \n");
262 goto tx_buf_fail;
263 }
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 SET_NETDEV_DEV(dev, &pdev->dev);
266
267
268 private->dev = dev;
269 private->pdev = pdev;
270 private->io_port = pci_resource_start(pdev, 0);
271 spin_lock_init(&private->lock);
272 dev->irq = pdev->irq;
273 dev->base_addr = private->io_port;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 initialize_card(private);
276 read_mac_address(private);
277 setup_descriptors(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400278
Stephen Hemminger1034c9f2009-01-07 18:00:55 -0800279 dev->netdev_ops = &netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
281 pci_set_drvdata(pdev, dev);
282
283 if (register_netdev(dev)) {
284 printk(KERN_ERR "xircom_probe: netdevice registration failed.\n");
285 goto reg_fail;
286 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400287
Auke Kok44c10132007-06-08 15:46:36 -0700288 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 -0700289 /* start the transmitter to get a heartbeat */
290 /* TODO: send 2 dummy packets here */
291 transceiver_voodoo(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 spin_lock_irqsave(&private->lock,flags);
294 activate_transmitter(private);
295 activate_receiver(private);
296 spin_unlock_irqrestore(&private->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 trigger_receive(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 leave("xircom_probe");
301 return 0;
302
303reg_fail:
304 kfree(private->tx_buffer);
305tx_buf_fail:
306 kfree(private->rx_buffer);
307rx_buf_fail:
308 free_netdev(dev);
309device_fail:
310 return -ENODEV;
311}
312
313
314/*
315 xircom_remove is called on module-unload or on device-eject.
316 it unregisters the irq, io-region and network device.
317 Interrupts and such are already stopped in the "ifconfig ethX down"
318 code.
319 */
320static void __devexit xircom_remove(struct pci_dev *pdev)
321{
322 struct net_device *dev = pci_get_drvdata(pdev);
323 struct xircom_private *card = netdev_priv(dev);
324
325 enter("xircom_remove");
326 pci_free_consistent(pdev,8192,card->rx_buffer,card->rx_dma_handle);
327 pci_free_consistent(pdev,8192,card->tx_buffer,card->tx_dma_handle);
328
329 release_region(dev->base_addr, 128);
330 unregister_netdev(dev);
331 free_netdev(dev);
332 pci_set_drvdata(pdev, NULL);
333 leave("xircom_remove");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400334}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
David Howells7d12e782006-10-05 14:55:46 +0100336static irqreturn_t xircom_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct net_device *dev = (struct net_device *) dev_instance;
339 struct xircom_private *card = netdev_priv(dev);
340 unsigned int status;
341 int i;
342
343 enter("xircom_interrupt\n");
344
345 spin_lock(&card->lock);
346 status = inl(card->io_port+CSR5);
347
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400348#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 print_binary(status);
350 printk("tx status 0x%08x 0x%08x \n",card->tx_buffer[0],card->tx_buffer[4]);
351 printk("rx status 0x%08x 0x%08x \n",card->rx_buffer[0],card->rx_buffer[4]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400352#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /* Handle shared irq and hotplug */
354 if (status == 0 || status == 0xffffffff) {
355 spin_unlock(&card->lock);
356 return IRQ_NONE;
357 }
358
359 if (link_status_changed(card)) {
360 int newlink;
361 printk(KERN_DEBUG "xircom_cb: Link status has changed \n");
362 newlink = link_status(card);
363 printk(KERN_INFO "xircom_cb: Link is %i mbit \n",newlink);
364 if (newlink)
365 netif_carrier_on(dev);
366 else
367 netif_carrier_off(dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400371 /* Clear all remaining interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 status |= 0xffffffff; /* FIXME: make this clear only the
373 real existing bits */
374 outl(status,card->io_port+CSR5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400376
377 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 investigate_write_descriptor(dev,card,i,bufferoffsets[i]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400379 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 investigate_read_descriptor(dev,card,i,bufferoffsets[i]);
381
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 spin_unlock(&card->lock);
384 leave("xircom_interrupt");
385 return IRQ_HANDLED;
386}
387
Stephen Hemmingerad096462009-08-31 19:50:53 +0000388static netdev_tx_t xircom_start_xmit(struct sk_buff *skb,
389 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 struct xircom_private *card;
392 unsigned long flags;
393 int nextdescriptor;
394 int desc;
395 enter("xircom_start_xmit");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 card = netdev_priv(dev);
398 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /* First see if we can free some descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400401 for (desc=0;desc<NUMDESCRIPTORS;desc++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 investigate_write_descriptor(dev,card,desc,bufferoffsets[desc]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400403
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 nextdescriptor = (card->transmit_used +1) % (NUMDESCRIPTORS);
406 desc = card->transmit_used;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* only send the packet if the descriptor is free */
409 if (card->tx_buffer[4*desc]==0) {
410 /* Copy the packet data; zero the memory first as the card
411 sometimes sends more than you ask it to. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 memset(&card->tx_buffer[bufferoffsets[desc]/4],0,1536);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300414 skb_copy_from_linear_data(skb,
415 &(card->tx_buffer[bufferoffsets[desc] / 4]),
416 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* FIXME: The specification tells us that the length we send HAS to be a multiple of
418 4 bytes. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400419
Al Viro6f35d5d2007-12-23 20:01:04 +0000420 card->tx_buffer[4*desc+1] = cpu_to_le32(skb->len);
421 if (desc == NUMDESCRIPTORS - 1) /* bit 25: last descriptor of the ring */
422 card->tx_buffer[4*desc+1] |= cpu_to_le32(1<<25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Al Viro6f35d5d2007-12-23 20:01:04 +0000424 card->tx_buffer[4*desc+1] |= cpu_to_le32(0xF0000000);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400425 /* 0xF0... means want interrupts*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 card->tx_skb[desc] = skb;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 wmb();
429 /* This gives the descriptor to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000430 card->tx_buffer[4*desc] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 trigger_transmit(card);
Al Viro6f35d5d2007-12-23 20:01:04 +0000432 if (card->tx_buffer[nextdescriptor*4] & cpu_to_le32(0x8000000)) {
433 /* next descriptor is occupied... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 netif_stop_queue(dev);
435 }
436 card->transmit_used = nextdescriptor;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400437 leave("xircom-start_xmit - sent");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 spin_unlock_irqrestore(&card->lock,flags);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000439 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443
444 /* Uh oh... no free descriptor... drop the packet */
445 netif_stop_queue(dev);
446 spin_unlock_irqrestore(&card->lock,flags);
447 trigger_transmit(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400448
Erik Mouw06f75252008-02-04 18:56:54 +0100449 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452
453
454
455static int xircom_open(struct net_device *dev)
456{
457 struct xircom_private *xp = netdev_priv(dev);
458 int retval;
459 enter("xircom_open");
460 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 -0700461 retval = request_irq(dev->irq, &xircom_interrupt, IRQF_SHARED, dev->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (retval) {
463 leave("xircom_open - No IRQ");
464 return retval;
465 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 xircom_up(xp);
468 xp->open = 1;
469 leave("xircom_open");
470 return 0;
471}
472
473static int xircom_close(struct net_device *dev)
474{
475 struct xircom_private *card;
476 unsigned long flags;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 enter("xircom_close");
479 card = netdev_priv(dev);
480 netif_stop_queue(dev); /* we don't want new packets */
481
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 disable_all_interrupts(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400486#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /* We can enable this again once we send dummy packets on ifconfig ethX up */
488 deactivate_receiver(card);
489 deactivate_transmitter(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400490#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 remove_descriptors(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 spin_unlock_irqrestore(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 card->open = 0;
496 free_irq(dev->irq,dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 leave("xircom_close");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return 0;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505#ifdef CONFIG_NET_POLL_CONTROLLER
506static void xircom_poll_controller(struct net_device *dev)
507{
508 disable_irq(dev->irq);
David Howells7d12e782006-10-05 14:55:46 +0100509 xircom_interrupt(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 enable_irq(dev->irq);
511}
512#endif
513
514
515static void initialize_card(struct xircom_private *card)
516{
517 unsigned int val;
518 unsigned long flags;
519 enter("initialize_card");
520
521
522 spin_lock_irqsave(&card->lock, flags);
523
524 /* First: reset the card */
525 val = inl(card->io_port + CSR0);
526 val |= 0x01; /* Software reset */
527 outl(val, card->io_port + CSR0);
528
529 udelay(100); /* give the card some time to reset */
530
531 val = inl(card->io_port + CSR0);
532 val &= ~0x01; /* disable Software reset */
533 outl(val, card->io_port + CSR0);
534
535
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400536 val = 0; /* Value 0x00 is a safe and conservative value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 for the PCI configuration settings */
538 outl(val, card->io_port + CSR0);
539
540
541 disable_all_interrupts(card);
542 deactivate_receiver(card);
543 deactivate_transmitter(card);
544
545 spin_unlock_irqrestore(&card->lock, flags);
546
547 leave("initialize_card");
548}
549
550/*
551trigger_transmit causes the card to check for frames to be transmitted.
552This is accomplished by writing to the CSR1 port. The documentation
553claims that the act of writing is sufficient and that the value is
554ignored; I chose zero.
555*/
556static void trigger_transmit(struct xircom_private *card)
557{
558 unsigned int val;
559 enter("trigger_transmit");
560
561 val = 0;
562 outl(val, card->io_port + CSR1);
563
564 leave("trigger_transmit");
565}
566
567/*
568trigger_receive causes the card to check for empty frames in the
569descriptor list in which packets can be received.
570This is accomplished by writing to the CSR2 port. The documentation
571claims that the act of writing is sufficient and that the value is
572ignored; I chose zero.
573*/
574static void trigger_receive(struct xircom_private *card)
575{
576 unsigned int val;
577 enter("trigger_receive");
578
579 val = 0;
580 outl(val, card->io_port + CSR2);
581
582 leave("trigger_receive");
583}
584
585/*
586setup_descriptors initializes the send and receive buffers to be valid
587descriptors and programs the addresses into the card.
588*/
589static void setup_descriptors(struct xircom_private *card)
590{
Al Viro6f35d5d2007-12-23 20:01:04 +0000591 u32 address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 int i;
593 enter("setup_descriptors");
594
595
Eric Sesterhenn / snakebytea707cd62006-01-26 22:02:51 +0100596 BUG_ON(card->rx_buffer == NULL);
597 BUG_ON(card->tx_buffer == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 /* Receive descriptors */
600 memset(card->rx_buffer, 0, 128); /* clear the descriptors */
601 for (i=0;i<NUMDESCRIPTORS;i++ ) {
602
603 /* Rx Descr0: It's empty, let the card own it, no errors -> 0x80000000 */
Al Viro6f35d5d2007-12-23 20:01:04 +0000604 card->rx_buffer[i*4 + 0] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* Rx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
Al Viro6f35d5d2007-12-23 20:01:04 +0000606 card->rx_buffer[i*4 + 1] = cpu_to_le32(1536);
607 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
608 card->rx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /* Rx Descr2: address of the buffer
611 we store the buffer at the 2nd half of the page */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400612
Al Viro6f35d5d2007-12-23 20:01:04 +0000613 address = card->rx_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 card->rx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
615 /* Rx Desc3: address of 2nd buffer -> 0 */
616 card->rx_buffer[i*4 + 3] = 0;
617 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 wmb();
620 /* Write the receive descriptor ring address to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000621 address = card->rx_dma_handle;
622 outl(address, card->io_port + CSR3); /* Receive descr list address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624
625 /* transmit descriptors */
626 memset(card->tx_buffer, 0, 128); /* clear the descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 for (i=0;i<NUMDESCRIPTORS;i++ ) {
629 /* Tx Descr0: Empty, we own it, no errors -> 0x00000000 */
630 card->tx_buffer[i*4 + 0] = 0x00000000;
631 /* Tx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
Al Viro6f35d5d2007-12-23 20:01:04 +0000632 card->tx_buffer[i*4 + 1] = cpu_to_le32(1536);
633 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
634 card->tx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /* Tx Descr2: address of the buffer
637 we store the buffer at the 2nd half of the page */
Al Viro6f35d5d2007-12-23 20:01:04 +0000638 address = card->tx_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 card->tx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
640 /* Tx Desc3: address of 2nd buffer -> 0 */
641 card->tx_buffer[i*4 + 3] = 0;
642 }
643
644 wmb();
645 /* wite the transmit descriptor ring to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000646 address = card->tx_dma_handle;
647 outl(address, card->io_port + CSR4); /* xmit descr list address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 leave("setup_descriptors");
650}
651
652/*
653remove_descriptors informs the card the descriptors are no longer
654valid by setting the address in the card to 0x00.
655*/
656static void remove_descriptors(struct xircom_private *card)
657{
658 unsigned int val;
659 enter("remove_descriptors");
660
661 val = 0;
662 outl(val, card->io_port + CSR3); /* Receive descriptor address */
663 outl(val, card->io_port + CSR4); /* Send descriptor address */
664
665 leave("remove_descriptors");
666}
667
668/*
669link_status_changed returns 1 if the card has indicated that
670the link status has changed. The new link status has to be read from CSR12.
671
672This function also clears the status-bit.
673*/
674static int link_status_changed(struct xircom_private *card)
675{
676 unsigned int val;
677 enter("link_status_changed");
678
679 val = inl(card->io_port + CSR5); /* Status register */
680
681 if ((val & (1 << 27)) == 0) { /* no change */
682 leave("link_status_changed - nochange");
683 return 0;
684 }
685
686 /* clear the event by writing a 1 to the bit in the
687 status register. */
688 val = (1 << 27);
689 outl(val, card->io_port + CSR5);
690
691 leave("link_status_changed - changed");
692 return 1;
693}
694
695
696/*
697transmit_active returns 1 if the transmitter on the card is
698in a non-stopped state.
699*/
700static int transmit_active(struct xircom_private *card)
701{
702 unsigned int val;
703 enter("transmit_active");
704
705 val = inl(card->io_port + CSR5); /* Status register */
706
707 if ((val & (7 << 20)) == 0) { /* transmitter disabled */
708 leave("transmit_active - inactive");
709 return 0;
710 }
711
712 leave("transmit_active - active");
713 return 1;
714}
715
716/*
717receive_active returns 1 if the receiver on the card is
718in a non-stopped state.
719*/
720static int receive_active(struct xircom_private *card)
721{
722 unsigned int val;
723 enter("receive_active");
724
725
726 val = inl(card->io_port + CSR5); /* Status register */
727
728 if ((val & (7 << 17)) == 0) { /* receiver disabled */
729 leave("receive_active - inactive");
730 return 0;
731 }
732
733 leave("receive_active - active");
734 return 1;
735}
736
737/*
738activate_receiver enables the receiver on the card.
739Before being allowed to active the receiver, the receiver
740must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400741this code actually disables the receiver first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742receiver to become inactive, then it activates the receiver and then
743it waits for the receiver to be active.
744
745must be called with the lock held and interrupts disabled.
746*/
747static void activate_receiver(struct xircom_private *card)
748{
749 unsigned int val;
750 int counter;
751 enter("activate_receiver");
752
753
754 val = inl(card->io_port + CSR6); /* Operation mode */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /* If the "active" bit is set and the receiver is already
757 active, no need to do the expensive thing */
758 if ((val&2) && (receive_active(card)))
759 return;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400760
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 val = val & ~2; /* disable the receiver */
763 outl(val, card->io_port + CSR6);
764
765 counter = 10;
766 while (counter > 0) {
767 if (!receive_active(card))
768 break;
769 /* wait a while */
770 udelay(50);
771 counter--;
772 if (counter <= 0)
773 printk(KERN_ERR "xircom_cb: Receiver failed to deactivate\n");
774 }
775
776 /* enable the receiver */
777 val = inl(card->io_port + CSR6); /* Operation mode */
778 val = val | 2; /* enable the receiver */
779 outl(val, card->io_port + CSR6);
780
781 /* now wait for the card to activate again */
782 counter = 10;
783 while (counter > 0) {
784 if (receive_active(card))
785 break;
786 /* wait a while */
787 udelay(50);
788 counter--;
789 if (counter <= 0)
790 printk(KERN_ERR "xircom_cb: Receiver failed to re-activate\n");
791 }
792
793 leave("activate_receiver");
794}
795
796/*
797deactivate_receiver disables the receiver on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400798To achieve this this code disables the receiver first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799then it waits for the receiver to become inactive.
800
801must be called with the lock held and interrupts disabled.
802*/
803static void deactivate_receiver(struct xircom_private *card)
804{
805 unsigned int val;
806 int counter;
807 enter("deactivate_receiver");
808
809 val = inl(card->io_port + CSR6); /* Operation mode */
810 val = val & ~2; /* disable the receiver */
811 outl(val, card->io_port + CSR6);
812
813 counter = 10;
814 while (counter > 0) {
815 if (!receive_active(card))
816 break;
817 /* wait a while */
818 udelay(50);
819 counter--;
820 if (counter <= 0)
821 printk(KERN_ERR "xircom_cb: Receiver failed to deactivate\n");
822 }
823
824
825 leave("deactivate_receiver");
826}
827
828
829/*
830activate_transmitter enables the transmitter on the card.
831Before being allowed to active the transmitter, the transmitter
832must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400833this code actually disables the transmitter first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834transmitter to become inactive, then it activates the transmitter and then
835it waits for the transmitter to be active again.
836
837must be called with the lock held and interrupts disabled.
838*/
839static void activate_transmitter(struct xircom_private *card)
840{
841 unsigned int val;
842 int counter;
843 enter("activate_transmitter");
844
845
846 val = inl(card->io_port + CSR6); /* Operation mode */
847
848 /* If the "active" bit is set and the receiver is already
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400849 active, no need to do the expensive thing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if ((val&(1<<13)) && (transmit_active(card)))
851 return;
852
853 val = val & ~(1 << 13); /* disable the transmitter */
854 outl(val, card->io_port + CSR6);
855
856 counter = 10;
857 while (counter > 0) {
858 if (!transmit_active(card))
859 break;
860 /* wait a while */
861 udelay(50);
862 counter--;
863 if (counter <= 0)
864 printk(KERN_ERR "xircom_cb: Transmitter failed to deactivate\n");
865 }
866
867 /* enable the transmitter */
868 val = inl(card->io_port + CSR6); /* Operation mode */
869 val = val | (1 << 13); /* enable the transmitter */
870 outl(val, card->io_port + CSR6);
871
872 /* now wait for the card to activate again */
873 counter = 10;
874 while (counter > 0) {
875 if (transmit_active(card))
876 break;
877 /* wait a while */
878 udelay(50);
879 counter--;
880 if (counter <= 0)
881 printk(KERN_ERR "xircom_cb: Transmitter failed to re-activate\n");
882 }
883
884 leave("activate_transmitter");
885}
886
887/*
888deactivate_transmitter disables the transmitter on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400889To achieve this this code disables the transmitter first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890then it waits for the transmitter to become inactive.
891
892must be called with the lock held and interrupts disabled.
893*/
894static void deactivate_transmitter(struct xircom_private *card)
895{
896 unsigned int val;
897 int counter;
898 enter("deactivate_transmitter");
899
900 val = inl(card->io_port + CSR6); /* Operation mode */
901 val = val & ~2; /* disable the transmitter */
902 outl(val, card->io_port + CSR6);
903
904 counter = 20;
905 while (counter > 0) {
906 if (!transmit_active(card))
907 break;
908 /* wait a while */
909 udelay(50);
910 counter--;
911 if (counter <= 0)
912 printk(KERN_ERR "xircom_cb: Transmitter failed to deactivate\n");
913 }
914
915
916 leave("deactivate_transmitter");
917}
918
919
920/*
921enable_transmit_interrupt enables the transmit interrupt
922
923must be called with the lock held and interrupts disabled.
924*/
925static void enable_transmit_interrupt(struct xircom_private *card)
926{
927 unsigned int val;
928 enter("enable_transmit_interrupt");
929
930 val = inl(card->io_port + CSR7); /* Interrupt enable register */
931 val |= 1; /* enable the transmit interrupt */
932 outl(val, card->io_port + CSR7);
933
934 leave("enable_transmit_interrupt");
935}
936
937
938/*
939enable_receive_interrupt enables the receive interrupt
940
941must be called with the lock held and interrupts disabled.
942*/
943static void enable_receive_interrupt(struct xircom_private *card)
944{
945 unsigned int val;
946 enter("enable_receive_interrupt");
947
948 val = inl(card->io_port + CSR7); /* Interrupt enable register */
949 val = val | (1 << 6); /* enable the receive interrupt */
950 outl(val, card->io_port + CSR7);
951
952 leave("enable_receive_interrupt");
953}
954
955/*
956enable_link_interrupt enables the link status change interrupt
957
958must be called with the lock held and interrupts disabled.
959*/
960static void enable_link_interrupt(struct xircom_private *card)
961{
962 unsigned int val;
963 enter("enable_link_interrupt");
964
965 val = inl(card->io_port + CSR7); /* Interrupt enable register */
966 val = val | (1 << 27); /* enable the link status chage interrupt */
967 outl(val, card->io_port + CSR7);
968
969 leave("enable_link_interrupt");
970}
971
972
973
974/*
975disable_all_interrupts disables all interrupts
976
977must be called with the lock held and interrupts disabled.
978*/
979static void disable_all_interrupts(struct xircom_private *card)
980{
981 unsigned int val;
982 enter("enable_all_interrupts");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 val = 0; /* disable all interrupts */
985 outl(val, card->io_port + CSR7);
986
987 leave("disable_all_interrupts");
988}
989
990/*
991enable_common_interrupts enables several weird interrupts
992
993must be called with the lock held and interrupts disabled.
994*/
995static void enable_common_interrupts(struct xircom_private *card)
996{
997 unsigned int val;
998 enter("enable_link_interrupt");
999
1000 val = inl(card->io_port + CSR7); /* Interrupt enable register */
1001 val |= (1<<16); /* Normal Interrupt Summary */
1002 val |= (1<<15); /* Abnormal Interrupt Summary */
1003 val |= (1<<13); /* Fatal bus error */
1004 val |= (1<<8); /* Receive Process Stopped */
1005 val |= (1<<7); /* Receive Buffer Unavailable */
1006 val |= (1<<5); /* Transmit Underflow */
1007 val |= (1<<2); /* Transmit Buffer Unavailable */
1008 val |= (1<<1); /* Transmit Process Stopped */
1009 outl(val, card->io_port + CSR7);
1010
1011 leave("enable_link_interrupt");
1012}
1013
1014/*
1015enable_promisc starts promisc mode
1016
1017must be called with the lock held and interrupts disabled.
1018*/
1019static int enable_promisc(struct xircom_private *card)
1020{
1021 unsigned int val;
1022 enter("enable_promisc");
1023
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001024 val = inl(card->io_port + CSR6);
1025 val = val | (1 << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 outl(val, card->io_port + CSR6);
1027
1028 leave("enable_promisc");
1029 return 1;
1030}
1031
1032
1033
1034
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001035/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02001036link_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 -07001037
1038Must be called in locked state with interrupts disabled
1039*/
1040static int link_status(struct xircom_private *card)
1041{
1042 unsigned int val;
1043 enter("link_status");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 val = inb(card->io_port + CSR12);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (!(val&(1<<2))) /* bit 2 is 0 for 10mbit link, 1 for not an 10mbit link */
1048 return 10;
1049 if (!(val&(1<<1))) /* bit 1 is 0 for 100mbit link, 1 for not an 100mbit link */
1050 return 100;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001051
1052 /* If we get here -> no link at all */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 leave("link_status");
1055 return 0;
1056}
1057
1058
1059
1060
1061
1062/*
1063 read_mac_address() reads the MAC address from the NIC and stores it in the "dev" structure.
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 This function will take the spinlock itself and can, as a result, not be called with the lock helt.
1066 */
1067static void read_mac_address(struct xircom_private *card)
1068{
1069 unsigned char j, tuple, link, data_id, data_count;
1070 unsigned long flags;
1071 int i;
1072
1073 enter("read_mac_address");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 spin_lock_irqsave(&card->lock, flags);
1076
1077 outl(1 << 12, card->io_port + CSR9); /* enable boot rom access */
1078 for (i = 0x100; i < 0x1f7; i += link + 2) {
1079 outl(i, card->io_port + CSR10);
1080 tuple = inl(card->io_port + CSR9) & 0xff;
1081 outl(i + 1, card->io_port + CSR10);
1082 link = inl(card->io_port + CSR9) & 0xff;
1083 outl(i + 2, card->io_port + CSR10);
1084 data_id = inl(card->io_port + CSR9) & 0xff;
1085 outl(i + 3, card->io_port + CSR10);
1086 data_count = inl(card->io_port + CSR9) & 0xff;
1087 if ((tuple == 0x22) && (data_id == 0x04) && (data_count == 0x06)) {
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001088 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 * This is it. We have the data we want.
1090 */
1091 for (j = 0; j < 6; j++) {
1092 outl(i + j + 4, card->io_port + CSR10);
1093 card->dev->dev_addr[j] = inl(card->io_port + CSR9) & 0xff;
1094 }
1095 break;
1096 } else if (link == 0) {
1097 break;
1098 }
1099 }
1100 spin_unlock_irqrestore(&card->lock, flags);
Johannes Berge1749612008-10-27 15:59:26 -07001101 pr_debug(" %pM\n", card->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 leave("read_mac_address");
1103}
1104
1105
1106/*
1107 transceiver_voodoo() enables the external UTP plug thingy.
1108 it's called voodoo as I stole this code and cannot cross-reference
1109 it with the specification.
1110 */
1111static void transceiver_voodoo(struct xircom_private *card)
1112{
1113 unsigned long flags;
1114
1115 enter("transceiver_voodoo");
1116
1117 /* disable all powermanagement */
1118 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1119
1120 setup_descriptors(card);
1121
1122 spin_lock_irqsave(&card->lock, flags);
1123
1124 outl(0x0008, card->io_port + CSR15);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001125 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 outl(0xa8050000, card->io_port + CSR15);
1127 udelay(25);
1128 outl(0xa00f0000, card->io_port + CSR15);
1129 udelay(25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 spin_unlock_irqrestore(&card->lock, flags);
1132
1133 netif_start_queue(card->dev);
1134 leave("transceiver_voodoo");
1135}
1136
1137
1138static void xircom_up(struct xircom_private *card)
1139{
1140 unsigned long flags;
1141 int i;
1142
1143 enter("xircom_up");
1144
1145 /* disable all powermanagement */
1146 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1147
1148 setup_descriptors(card);
1149
1150 spin_lock_irqsave(&card->lock, flags);
1151
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 enable_link_interrupt(card);
1154 enable_transmit_interrupt(card);
1155 enable_receive_interrupt(card);
1156 enable_common_interrupts(card);
1157 enable_promisc(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /* The card can have received packets already, read them away now */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001160 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 investigate_read_descriptor(card->dev,card,i,bufferoffsets[i]);
1162
1163
1164 spin_unlock_irqrestore(&card->lock, flags);
1165 trigger_receive(card);
1166 trigger_transmit(card);
1167 netif_start_queue(card->dev);
1168 leave("xircom_up");
1169}
1170
1171/* Bufferoffset is in BYTES */
1172static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset)
1173{
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001174 int status;
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 enter("investigate_read_descriptor");
Al Viro6f35d5d2007-12-23 20:01:04 +00001177 status = le32_to_cpu(card->rx_buffer[4*descnr]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if ((status > 0)) { /* packet received */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /* TODO: discard error packets */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 short pkt_len = ((status >> 16) & 0x7ff) - 4; /* minus 4, we don't want the CRC */
1184 struct sk_buff *skb;
1185
1186 if (pkt_len > 1518) {
1187 printk(KERN_ERR "xircom_cb: Packet length %i is bogus \n",pkt_len);
1188 pkt_len = 1518;
1189 }
1190
1191 skb = dev_alloc_skb(pkt_len + 2);
1192 if (skb == NULL) {
Stephen Hemminger1034c9f2009-01-07 18:00:55 -08001193 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 goto out;
1195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 skb_reserve(skb, 2);
David S. Miller8c7b7fa2007-07-10 22:08:12 -07001197 skb_copy_to_linear_data(skb, (unsigned char*)&card->rx_buffer[bufferoffset / 4], pkt_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 skb_put(skb, pkt_len);
1199 skb->protocol = eth_type_trans(skb, dev);
1200 netif_rx(skb);
Stephen Hemminger1034c9f2009-01-07 18:00:55 -08001201 dev->stats.rx_packets++;
1202 dev->stats.rx_bytes += pkt_len;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 out:
1205 /* give the buffer back to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +00001206 card->rx_buffer[4*descnr] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 trigger_receive(card);
1208 }
1209
1210 leave("investigate_read_descriptor");
1211
1212}
1213
1214
1215/* Bufferoffset is in BYTES */
1216static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset)
1217{
1218 int status;
1219
1220 enter("investigate_write_descriptor");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001221
Al Viro6f35d5d2007-12-23 20:01:04 +00001222 status = le32_to_cpu(card->tx_buffer[4*descnr]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001223#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (status & 0x8000) { /* Major error */
1225 printk(KERN_ERR "Major transmit error status %x \n", status);
1226 card->tx_buffer[4*descnr] = 0;
1227 netif_wake_queue (dev);
1228 }
1229#endif
1230 if (status > 0) { /* bit 31 is 0 when done */
1231 if (card->tx_skb[descnr]!=NULL) {
Stephen Hemminger1034c9f2009-01-07 18:00:55 -08001232 dev->stats.tx_bytes += card->tx_skb[descnr]->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 dev_kfree_skb_irq(card->tx_skb[descnr]);
1234 }
1235 card->tx_skb[descnr] = NULL;
1236 /* Bit 8 in the status field is 1 if there was a collision */
1237 if (status&(1<<8))
Stephen Hemminger1034c9f2009-01-07 18:00:55 -08001238 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 card->tx_buffer[4*descnr] = 0; /* descriptor is free again */
1240 netif_wake_queue (dev);
Stephen Hemminger1034c9f2009-01-07 18:00:55 -08001241 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
1243
1244 leave("investigate_write_descriptor");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
1248
1249static int __init xircom_init(void)
1250{
Alexey Dobriyanec42cdb2006-08-14 23:00:28 -07001251 return pci_register_driver(&xircom_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
1254static void __exit xircom_exit(void)
1255{
1256 pci_unregister_driver(&xircom_ops);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001257}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001259module_init(xircom_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260module_exit(xircom_exit)
1261