blob: fdb329fe6e8ea8564bb7afb5a5718388ca314164 [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
Joe Perches44298ec2010-01-28 20:59:29 +000017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/string.h>
22#include <linux/errno.h>
23#include <linux/ioport.h>
24#include <linux/slab.h>
25#include <linux/interrupt.h>
26#include <linux/pci.h>
27#include <linux/netdevice.h>
28#include <linux/etherdevice.h>
29#include <linux/skbuff.h>
30#include <linux/delay.h>
31#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/bitops.h>
33
34#include <asm/uaccess.h>
35#include <asm/io.h>
Al Viro82729972005-12-06 05:53:04 -050036#ifdef CONFIG_NET_POLL_CONTROLLER
37#include <asm/irq.h>
38#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040MODULE_DESCRIPTION("Xircom Cardbus ethernet driver");
41MODULE_AUTHOR("Arjan van de Ven <arjanv@redhat.com>");
42MODULE_LICENSE("GPL");
43
44
45
46/* IO registers on the card, offsets */
47#define CSR0 0x00
48#define CSR1 0x08
49#define CSR2 0x10
50#define CSR3 0x18
51#define CSR4 0x20
52#define CSR5 0x28
53#define CSR6 0x30
54#define CSR7 0x38
55#define CSR8 0x40
56#define CSR9 0x48
57#define CSR10 0x50
58#define CSR11 0x58
59#define CSR12 0x60
60#define CSR13 0x68
61#define CSR14 0x70
62#define CSR15 0x78
63#define CSR16 0x80
64
65/* PCI registers */
66#define PCI_POWERMGMT 0x40
67
68/* Offsets of the buffers within the descriptor pages, in bytes */
69
70#define NUMDESCRIPTORS 4
71
72static int bufferoffsets[NUMDESCRIPTORS] = {128,2048,4096,6144};
73
74
75struct xircom_private {
76 /* Send and receive buffers, kernel-addressable and dma addressable forms */
77
Al Viro6f35d5d2007-12-23 20:01:04 +000078 __le32 *rx_buffer;
79 __le32 *tx_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 dma_addr_t rx_dma_handle;
82 dma_addr_t tx_dma_handle;
83
84 struct sk_buff *tx_skb[4];
85
86 unsigned long io_port;
87 int open;
Jeff Garzikf3b197a2006-05-26 21:39:03 -040088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 /* transmit_used is the rotating counter that indicates which transmit
90 descriptor has to be used next */
91 int transmit_used;
92
93 /* Spinlock to serialize register operations.
94 It must be helt while manipulating the following registers:
95 CSR0, CSR6, CSR7, CSR9, CSR10, CSR15
96 */
97 spinlock_t lock;
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 struct pci_dev *pdev;
100 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
103
104/* Function prototypes */
105static int xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id);
106static void xircom_remove(struct pci_dev *pdev);
David Howells7d12e782006-10-05 14:55:46 +0100107static irqreturn_t xircom_interrupt(int irq, void *dev_instance);
Stephen Hemmingerad096462009-08-31 19:50:53 +0000108static netdev_tx_t xircom_start_xmit(struct sk_buff *skb,
109 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static int xircom_open(struct net_device *dev);
111static int xircom_close(struct net_device *dev);
112static void xircom_up(struct xircom_private *card);
Keith Owens3be034b2005-09-13 15:05:13 +1000113#ifdef CONFIG_NET_POLL_CONTROLLER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static void xircom_poll_controller(struct net_device *dev);
115#endif
116
117static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset);
118static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset);
119static void read_mac_address(struct xircom_private *card);
120static void transceiver_voodoo(struct xircom_private *card);
121static void initialize_card(struct xircom_private *card);
122static void trigger_transmit(struct xircom_private *card);
123static void trigger_receive(struct xircom_private *card);
124static void setup_descriptors(struct xircom_private *card);
125static void remove_descriptors(struct xircom_private *card);
126static int link_status_changed(struct xircom_private *card);
127static void activate_receiver(struct xircom_private *card);
128static void deactivate_receiver(struct xircom_private *card);
129static void activate_transmitter(struct xircom_private *card);
130static void deactivate_transmitter(struct xircom_private *card);
131static void enable_transmit_interrupt(struct xircom_private *card);
132static void enable_receive_interrupt(struct xircom_private *card);
133static void enable_link_interrupt(struct xircom_private *card);
134static void disable_all_interrupts(struct xircom_private *card);
135static int link_status(struct xircom_private *card);
136
137
138
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000139static DEFINE_PCI_DEVICE_TABLE(xircom_pci_table) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 {0x115D, 0x0003, PCI_ANY_ID, PCI_ANY_ID,},
141 {0,},
142};
143MODULE_DEVICE_TABLE(pci, xircom_pci_table);
144
145static struct pci_driver xircom_ops = {
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400146 .name = "xircom_cb",
147 .id_table = xircom_pci_table,
148 .probe = xircom_probe,
149 .remove = xircom_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 .suspend =NULL,
151 .resume =NULL
152};
153
154
Joe Perches54668b82011-05-09 09:45:20 +0000155#if defined DEBUG && DEBUG > 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static void print_binary(unsigned int number)
157{
158 int i,i2;
159 char buffer[64];
160 memset(buffer,0,64);
161 i2=0;
162 for (i=31;i>=0;i--) {
163 if (number & (1<<i))
164 buffer[i2++]='1';
165 else
166 buffer[i2++]='0';
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400167 if ((i&3)==0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 buffer[i2++]=' ';
169 }
Joe Perches54668b82011-05-09 09:45:20 +0000170 pr_debug("%s\n",buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172#endif
173
Stephen Hemminger1034c9f2009-01-07 18:00:55 -0800174static const struct net_device_ops netdev_ops = {
175 .ndo_open = xircom_open,
176 .ndo_stop = xircom_close,
177 .ndo_start_xmit = xircom_start_xmit,
178 .ndo_change_mtu = eth_change_mtu,
179 .ndo_set_mac_address = eth_mac_addr,
180 .ndo_validate_addr = eth_validate_addr,
181#ifdef CONFIG_NET_POLL_CONTROLLER
182 .ndo_poll_controller = xircom_poll_controller,
183#endif
184};
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/* xircom_probe is the code that gets called on device insertion.
187 it sets up the hardware and registers the device to the networklayer.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 TODO: Send 1 or 2 "dummy" packets here as the card seems to discard the
190 first two packets that get send, and pump hates that.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
193static int __devinit xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id)
194{
195 struct net_device *dev = NULL;
196 struct xircom_private *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 unsigned long flags;
198 unsigned short tmp16;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* First do the PCI initialisation */
201
202 if (pci_enable_device(pdev))
203 return -ENODEV;
204
205 /* disable all powermanagement */
206 pci_write_config_dword(pdev, PCI_POWERMGMT, 0x0000);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 pci_set_master(pdev); /* Why isn't this done by pci_enable_device ?*/
209
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400210 /* clear PCI status, if any */
211 pci_read_config_word (pdev,PCI_STATUS, &tmp16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 pci_write_config_word (pdev, PCI_STATUS,tmp16);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (!request_region(pci_resource_start(pdev, 0), 128, "xircom_cb")) {
Joe Perches44298ec2010-01-28 20:59:29 +0000215 pr_err("%s: failed to allocate io-region\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return -ENODEV;
217 }
218
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400219 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 Before changing the hardware, allocate the memory.
221 This way, we can fail gracefully if not enough memory
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400222 is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 */
224 dev = alloc_etherdev(sizeof(struct xircom_private));
Joe Perches41de8d42012-01-29 13:47:52 +0000225 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 goto device_fail;
Joe Perches41de8d42012-01-29 13:47:52 +0000227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 private = netdev_priv(dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* Allocate the send/receive buffers */
231 private->rx_buffer = pci_alloc_consistent(pdev,8192,&private->rx_dma_handle);
232 if (private->rx_buffer == NULL) {
Joe Perches44298ec2010-01-28 20:59:29 +0000233 pr_err("%s: no memory for rx buffer\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 goto rx_buf_fail;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 private->tx_buffer = pci_alloc_consistent(pdev,8192,&private->tx_dma_handle);
237 if (private->tx_buffer == NULL) {
Joe Perches44298ec2010-01-28 20:59:29 +0000238 pr_err("%s: no memory for tx buffer\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 goto tx_buf_fail;
240 }
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 SET_NETDEV_DEV(dev, &pdev->dev);
243
244
245 private->dev = dev;
246 private->pdev = pdev;
247 private->io_port = pci_resource_start(pdev, 0);
248 spin_lock_init(&private->lock);
249 dev->irq = pdev->irq;
250 dev->base_addr = private->io_port;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 initialize_card(private);
253 read_mac_address(private);
254 setup_descriptors(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400255
Stephen Hemminger1034c9f2009-01-07 18:00:55 -0800256 dev->netdev_ops = &netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 pci_set_drvdata(pdev, dev);
258
259 if (register_netdev(dev)) {
Joe Perches44298ec2010-01-28 20:59:29 +0000260 pr_err("%s: netdevice registration failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 goto reg_fail;
262 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400263
Joe Perches163ef0b2011-05-09 09:45:21 +0000264 netdev_info(dev, "Xircom cardbus revision %i at irq %i\n",
265 pdev->revision, pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /* start the transmitter to get a heartbeat */
267 /* TODO: send 2 dummy packets here */
268 transceiver_voodoo(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 spin_lock_irqsave(&private->lock,flags);
271 activate_transmitter(private);
272 activate_receiver(private);
273 spin_unlock_irqrestore(&private->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 trigger_receive(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return 0;
278
279reg_fail:
280 kfree(private->tx_buffer);
281tx_buf_fail:
282 kfree(private->rx_buffer);
283rx_buf_fail:
284 free_netdev(dev);
285device_fail:
286 return -ENODEV;
287}
288
289
290/*
291 xircom_remove is called on module-unload or on device-eject.
292 it unregisters the irq, io-region and network device.
293 Interrupts and such are already stopped in the "ifconfig ethX down"
294 code.
295 */
296static void __devexit xircom_remove(struct pci_dev *pdev)
297{
298 struct net_device *dev = pci_get_drvdata(pdev);
299 struct xircom_private *card = netdev_priv(dev);
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 pci_free_consistent(pdev,8192,card->rx_buffer,card->rx_dma_handle);
302 pci_free_consistent(pdev,8192,card->tx_buffer,card->tx_dma_handle);
303
304 release_region(dev->base_addr, 128);
305 unregister_netdev(dev);
306 free_netdev(dev);
307 pci_set_drvdata(pdev, NULL);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400308}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
David Howells7d12e782006-10-05 14:55:46 +0100310static irqreturn_t xircom_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct net_device *dev = (struct net_device *) dev_instance;
313 struct xircom_private *card = netdev_priv(dev);
314 unsigned int status;
315 int i;
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 spin_lock(&card->lock);
318 status = inl(card->io_port+CSR5);
319
Joe Perches54668b82011-05-09 09:45:20 +0000320#if defined DEBUG && DEBUG > 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 print_binary(status);
Joe Perches54668b82011-05-09 09:45:20 +0000322 pr_debug("tx status 0x%08x 0x%08x\n",
323 card->tx_buffer[0], card->tx_buffer[4]);
324 pr_debug("rx status 0x%08x 0x%08x\n",
325 card->rx_buffer[0], card->rx_buffer[4]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400326#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* Handle shared irq and hotplug */
328 if (status == 0 || status == 0xffffffff) {
329 spin_unlock(&card->lock);
330 return IRQ_NONE;
331 }
332
333 if (link_status_changed(card)) {
334 int newlink;
Joe Perches726b65a2011-05-09 09:45:22 +0000335 netdev_dbg(dev, "Link status has changed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 newlink = link_status(card);
Joe Perches163ef0b2011-05-09 09:45:21 +0000337 netdev_info(dev, "Link is %d mbit\n", newlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (newlink)
339 netif_carrier_on(dev);
340 else
341 netif_carrier_off(dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400345 /* Clear all remaining interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 status |= 0xffffffff; /* FIXME: make this clear only the
347 real existing bits */
348 outl(status,card->io_port+CSR5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400350
351 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 investigate_write_descriptor(dev,card,i,bufferoffsets[i]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400353 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 investigate_read_descriptor(dev,card,i,bufferoffsets[i]);
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 spin_unlock(&card->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return IRQ_HANDLED;
358}
359
Stephen Hemmingerad096462009-08-31 19:50:53 +0000360static netdev_tx_t xircom_start_xmit(struct sk_buff *skb,
361 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct xircom_private *card;
364 unsigned long flags;
365 int nextdescriptor;
366 int desc;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 card = netdev_priv(dev);
369 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /* First see if we can free some descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400372 for (desc=0;desc<NUMDESCRIPTORS;desc++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 investigate_write_descriptor(dev,card,desc,bufferoffsets[desc]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400374
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 nextdescriptor = (card->transmit_used +1) % (NUMDESCRIPTORS);
377 desc = card->transmit_used;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* only send the packet if the descriptor is free */
380 if (card->tx_buffer[4*desc]==0) {
381 /* Copy the packet data; zero the memory first as the card
382 sometimes sends more than you ask it to. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 memset(&card->tx_buffer[bufferoffsets[desc]/4],0,1536);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300385 skb_copy_from_linear_data(skb,
386 &(card->tx_buffer[bufferoffsets[desc] / 4]),
387 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* FIXME: The specification tells us that the length we send HAS to be a multiple of
389 4 bytes. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400390
Al Viro6f35d5d2007-12-23 20:01:04 +0000391 card->tx_buffer[4*desc+1] = cpu_to_le32(skb->len);
392 if (desc == NUMDESCRIPTORS - 1) /* bit 25: last descriptor of the ring */
393 card->tx_buffer[4*desc+1] |= cpu_to_le32(1<<25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Al Viro6f35d5d2007-12-23 20:01:04 +0000395 card->tx_buffer[4*desc+1] |= cpu_to_le32(0xF0000000);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400396 /* 0xF0... means want interrupts*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 card->tx_skb[desc] = skb;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 wmb();
400 /* This gives the descriptor to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000401 card->tx_buffer[4*desc] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 trigger_transmit(card);
Al Viro6f35d5d2007-12-23 20:01:04 +0000403 if (card->tx_buffer[nextdescriptor*4] & cpu_to_le32(0x8000000)) {
404 /* next descriptor is occupied... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 netif_stop_queue(dev);
406 }
407 card->transmit_used = nextdescriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 spin_unlock_irqrestore(&card->lock,flags);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000409 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* Uh oh... no free descriptor... drop the packet */
413 netif_stop_queue(dev);
414 spin_unlock_irqrestore(&card->lock,flags);
415 trigger_transmit(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400416
Erik Mouw06f75252008-02-04 18:56:54 +0100417 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420
421
422
423static int xircom_open(struct net_device *dev)
424{
425 struct xircom_private *xp = netdev_priv(dev);
426 int retval;
Joe Perches54668b82011-05-09 09:45:20 +0000427
Joe Perches163ef0b2011-05-09 09:45:21 +0000428 netdev_info(dev, "xircom cardbus adaptor found, using irq %i\n",
429 dev->irq);
Joe Perchesa0607fd2009-11-18 23:29:17 -0800430 retval = request_irq(dev->irq, xircom_interrupt, IRQF_SHARED, dev->name, dev);
Joe Perches54668b82011-05-09 09:45:20 +0000431 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return retval;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 xircom_up(xp);
435 xp->open = 1;
Joe Perches54668b82011-05-09 09:45:20 +0000436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return 0;
438}
439
440static int xircom_close(struct net_device *dev)
441{
442 struct xircom_private *card;
443 unsigned long flags;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 card = netdev_priv(dev);
446 netif_stop_queue(dev); /* we don't want new packets */
447
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 disable_all_interrupts(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400452#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /* We can enable this again once we send dummy packets on ifconfig ethX up */
454 deactivate_receiver(card);
455 deactivate_transmitter(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400456#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 remove_descriptors(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 spin_unlock_irqrestore(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 card->open = 0;
462 free_irq(dev->irq,dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return 0;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469#ifdef CONFIG_NET_POLL_CONTROLLER
470static void xircom_poll_controller(struct net_device *dev)
471{
472 disable_irq(dev->irq);
David Howells7d12e782006-10-05 14:55:46 +0100473 xircom_interrupt(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 enable_irq(dev->irq);
475}
476#endif
477
478
479static void initialize_card(struct xircom_private *card)
480{
481 unsigned int val;
482 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 spin_lock_irqsave(&card->lock, flags);
485
486 /* First: reset the card */
487 val = inl(card->io_port + CSR0);
488 val |= 0x01; /* Software reset */
489 outl(val, card->io_port + CSR0);
490
491 udelay(100); /* give the card some time to reset */
492
493 val = inl(card->io_port + CSR0);
494 val &= ~0x01; /* disable Software reset */
495 outl(val, card->io_port + CSR0);
496
497
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400498 val = 0; /* Value 0x00 is a safe and conservative value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 for the PCI configuration settings */
500 outl(val, card->io_port + CSR0);
501
502
503 disable_all_interrupts(card);
504 deactivate_receiver(card);
505 deactivate_transmitter(card);
506
507 spin_unlock_irqrestore(&card->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510/*
511trigger_transmit causes the card to check for frames to be transmitted.
512This is accomplished by writing to the CSR1 port. The documentation
513claims that the act of writing is sufficient and that the value is
514ignored; I chose zero.
515*/
516static void trigger_transmit(struct xircom_private *card)
517{
518 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 val = 0;
521 outl(val, card->io_port + CSR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
524/*
525trigger_receive causes the card to check for empty frames in the
526descriptor list in which packets can be received.
527This is accomplished by writing to the CSR2 port. The documentation
528claims that the act of writing is sufficient and that the value is
529ignored; I chose zero.
530*/
531static void trigger_receive(struct xircom_private *card)
532{
533 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 val = 0;
536 outl(val, card->io_port + CSR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539/*
540setup_descriptors initializes the send and receive buffers to be valid
541descriptors and programs the addresses into the card.
542*/
543static void setup_descriptors(struct xircom_private *card)
544{
Al Viro6f35d5d2007-12-23 20:01:04 +0000545 u32 address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Eric Sesterhenn / snakebytea707cd62006-01-26 22:02:51 +0100548 BUG_ON(card->rx_buffer == NULL);
549 BUG_ON(card->tx_buffer == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /* Receive descriptors */
552 memset(card->rx_buffer, 0, 128); /* clear the descriptors */
553 for (i=0;i<NUMDESCRIPTORS;i++ ) {
554
555 /* Rx Descr0: It's empty, let the card own it, no errors -> 0x80000000 */
Al Viro6f35d5d2007-12-23 20:01:04 +0000556 card->rx_buffer[i*4 + 0] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* Rx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
Al Viro6f35d5d2007-12-23 20:01:04 +0000558 card->rx_buffer[i*4 + 1] = cpu_to_le32(1536);
559 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
560 card->rx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 /* Rx Descr2: address of the buffer
563 we store the buffer at the 2nd half of the page */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400564
Al Viro6f35d5d2007-12-23 20:01:04 +0000565 address = card->rx_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 card->rx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
567 /* Rx Desc3: address of 2nd buffer -> 0 */
568 card->rx_buffer[i*4 + 3] = 0;
569 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 wmb();
572 /* Write the receive descriptor ring address to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000573 address = card->rx_dma_handle;
574 outl(address, card->io_port + CSR3); /* Receive descr list address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576
577 /* transmit descriptors */
578 memset(card->tx_buffer, 0, 128); /* clear the descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 for (i=0;i<NUMDESCRIPTORS;i++ ) {
581 /* Tx Descr0: Empty, we own it, no errors -> 0x00000000 */
582 card->tx_buffer[i*4 + 0] = 0x00000000;
583 /* Tx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
Al Viro6f35d5d2007-12-23 20:01:04 +0000584 card->tx_buffer[i*4 + 1] = cpu_to_le32(1536);
585 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
586 card->tx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* Tx Descr2: address of the buffer
589 we store the buffer at the 2nd half of the page */
Al Viro6f35d5d2007-12-23 20:01:04 +0000590 address = card->tx_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 card->tx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
592 /* Tx Desc3: address of 2nd buffer -> 0 */
593 card->tx_buffer[i*4 + 3] = 0;
594 }
595
596 wmb();
597 /* wite the transmit descriptor ring to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000598 address = card->tx_dma_handle;
599 outl(address, card->io_port + CSR4); /* xmit descr list address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602/*
603remove_descriptors informs the card the descriptors are no longer
604valid by setting the address in the card to 0x00.
605*/
606static void remove_descriptors(struct xircom_private *card)
607{
608 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 val = 0;
611 outl(val, card->io_port + CSR3); /* Receive descriptor address */
612 outl(val, card->io_port + CSR4); /* Send descriptor address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615/*
616link_status_changed returns 1 if the card has indicated that
617the link status has changed. The new link status has to be read from CSR12.
618
619This function also clears the status-bit.
620*/
621static int link_status_changed(struct xircom_private *card)
622{
623 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 val = inl(card->io_port + CSR5); /* Status register */
626
Joe Perches54668b82011-05-09 09:45:20 +0000627 if ((val & (1 << 27)) == 0) /* no change */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 /* clear the event by writing a 1 to the bit in the
631 status register. */
632 val = (1 << 27);
633 outl(val, card->io_port + CSR5);
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return 1;
636}
637
638
639/*
640transmit_active returns 1 if the transmitter on the card is
641in a non-stopped state.
642*/
643static int transmit_active(struct xircom_private *card)
644{
645 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 val = inl(card->io_port + CSR5); /* Status register */
648
Joe Perches54668b82011-05-09 09:45:20 +0000649 if ((val & (7 << 20)) == 0) /* transmitter disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return 1;
653}
654
655/*
656receive_active returns 1 if the receiver on the card is
657in a non-stopped state.
658*/
659static int receive_active(struct xircom_private *card)
660{
661 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 val = inl(card->io_port + CSR5); /* Status register */
664
Joe Perches54668b82011-05-09 09:45:20 +0000665 if ((val & (7 << 17)) == 0) /* receiver disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return 1;
669}
670
671/*
672activate_receiver enables the receiver on the card.
673Before being allowed to active the receiver, the receiver
674must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400675this code actually disables the receiver first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676receiver to become inactive, then it activates the receiver and then
677it waits for the receiver to be active.
678
679must be called with the lock held and interrupts disabled.
680*/
681static void activate_receiver(struct xircom_private *card)
682{
683 unsigned int val;
684 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 val = inl(card->io_port + CSR6); /* Operation mode */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* If the "active" bit is set and the receiver is already
689 active, no need to do the expensive thing */
690 if ((val&2) && (receive_active(card)))
691 return;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400692
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 val = val & ~2; /* disable the receiver */
695 outl(val, card->io_port + CSR6);
696
697 counter = 10;
698 while (counter > 0) {
699 if (!receive_active(card))
700 break;
701 /* wait a while */
702 udelay(50);
703 counter--;
704 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000705 netdev_err(card->dev, "Receiver failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707
708 /* enable the receiver */
709 val = inl(card->io_port + CSR6); /* Operation mode */
710 val = val | 2; /* enable the receiver */
711 outl(val, card->io_port + CSR6);
712
713 /* now wait for the card to activate again */
714 counter = 10;
715 while (counter > 0) {
716 if (receive_active(card))
717 break;
718 /* wait a while */
719 udelay(50);
720 counter--;
721 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000722 netdev_err(card->dev,
723 "Receiver failed to re-activate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
727/*
728deactivate_receiver disables the receiver on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400729To achieve this this code disables the receiver first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730then it waits for the receiver to become inactive.
731
732must be called with the lock held and interrupts disabled.
733*/
734static void deactivate_receiver(struct xircom_private *card)
735{
736 unsigned int val;
737 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 val = inl(card->io_port + CSR6); /* Operation mode */
740 val = val & ~2; /* disable the receiver */
741 outl(val, card->io_port + CSR6);
742
743 counter = 10;
744 while (counter > 0) {
745 if (!receive_active(card))
746 break;
747 /* wait a while */
748 udelay(50);
749 counter--;
750 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000751 netdev_err(card->dev, "Receiver failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755
756/*
757activate_transmitter enables the transmitter on the card.
758Before being allowed to active the transmitter, the transmitter
759must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400760this code actually disables the transmitter first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761transmitter to become inactive, then it activates the transmitter and then
762it waits for the transmitter to be active again.
763
764must be called with the lock held and interrupts disabled.
765*/
766static void activate_transmitter(struct xircom_private *card)
767{
768 unsigned int val;
769 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 val = inl(card->io_port + CSR6); /* Operation mode */
772
773 /* If the "active" bit is set and the receiver is already
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400774 active, no need to do the expensive thing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if ((val&(1<<13)) && (transmit_active(card)))
776 return;
777
778 val = val & ~(1 << 13); /* disable the transmitter */
779 outl(val, card->io_port + CSR6);
780
781 counter = 10;
782 while (counter > 0) {
783 if (!transmit_active(card))
784 break;
785 /* wait a while */
786 udelay(50);
787 counter--;
788 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000789 netdev_err(card->dev,
790 "Transmitter failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
793 /* enable the transmitter */
794 val = inl(card->io_port + CSR6); /* Operation mode */
795 val = val | (1 << 13); /* enable the transmitter */
796 outl(val, card->io_port + CSR6);
797
798 /* now wait for the card to activate again */
799 counter = 10;
800 while (counter > 0) {
801 if (transmit_active(card))
802 break;
803 /* wait a while */
804 udelay(50);
805 counter--;
806 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000807 netdev_err(card->dev,
808 "Transmitter failed to re-activate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812/*
813deactivate_transmitter disables the transmitter on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400814To achieve this this code disables the transmitter first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815then it waits for the transmitter to become inactive.
816
817must be called with the lock held and interrupts disabled.
818*/
819static void deactivate_transmitter(struct xircom_private *card)
820{
821 unsigned int val;
822 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 val = inl(card->io_port + CSR6); /* Operation mode */
825 val = val & ~2; /* disable the transmitter */
826 outl(val, card->io_port + CSR6);
827
828 counter = 20;
829 while (counter > 0) {
830 if (!transmit_active(card))
831 break;
832 /* wait a while */
833 udelay(50);
834 counter--;
835 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000836 netdev_err(card->dev,
837 "Transmitter failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841
842/*
843enable_transmit_interrupt enables the transmit interrupt
844
845must be called with the lock held and interrupts disabled.
846*/
847static void enable_transmit_interrupt(struct xircom_private *card)
848{
849 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 val = inl(card->io_port + CSR7); /* Interrupt enable register */
852 val |= 1; /* enable the transmit interrupt */
853 outl(val, card->io_port + CSR7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
856
857/*
858enable_receive_interrupt enables the receive interrupt
859
860must be called with the lock held and interrupts disabled.
861*/
862static void enable_receive_interrupt(struct xircom_private *card)
863{
864 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 val = inl(card->io_port + CSR7); /* Interrupt enable register */
867 val = val | (1 << 6); /* enable the receive interrupt */
868 outl(val, card->io_port + CSR7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871/*
872enable_link_interrupt enables the link status change interrupt
873
874must be called with the lock held and interrupts disabled.
875*/
876static void enable_link_interrupt(struct xircom_private *card)
877{
878 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 val = inl(card->io_port + CSR7); /* Interrupt enable register */
881 val = val | (1 << 27); /* enable the link status chage interrupt */
882 outl(val, card->io_port + CSR7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885
886
887/*
888disable_all_interrupts disables all interrupts
889
890must be called with the lock held and interrupts disabled.
891*/
892static void disable_all_interrupts(struct xircom_private *card)
893{
894 unsigned int val;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 val = 0; /* disable all interrupts */
897 outl(val, card->io_port + CSR7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900/*
901enable_common_interrupts enables several weird interrupts
902
903must be called with the lock held and interrupts disabled.
904*/
905static void enable_common_interrupts(struct xircom_private *card)
906{
907 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 val = inl(card->io_port + CSR7); /* Interrupt enable register */
910 val |= (1<<16); /* Normal Interrupt Summary */
911 val |= (1<<15); /* Abnormal Interrupt Summary */
912 val |= (1<<13); /* Fatal bus error */
913 val |= (1<<8); /* Receive Process Stopped */
914 val |= (1<<7); /* Receive Buffer Unavailable */
915 val |= (1<<5); /* Transmit Underflow */
916 val |= (1<<2); /* Transmit Buffer Unavailable */
917 val |= (1<<1); /* Transmit Process Stopped */
918 outl(val, card->io_port + CSR7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921/*
922enable_promisc starts promisc mode
923
924must be called with the lock held and interrupts disabled.
925*/
926static int enable_promisc(struct xircom_private *card)
927{
928 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400930 val = inl(card->io_port + CSR6);
931 val = val | (1 << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 outl(val, card->io_port + CSR6);
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return 1;
935}
936
937
938
939
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400940/*
Michael Opdenacker59c51592007-05-09 08:57:56 +0200941link_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 -0700942
943Must be called in locked state with interrupts disabled
944*/
945static int link_status(struct xircom_private *card)
946{
947 unsigned int val;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 val = inb(card->io_port + CSR12);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (!(val&(1<<2))) /* bit 2 is 0 for 10mbit link, 1 for not an 10mbit link */
952 return 10;
953 if (!(val&(1<<1))) /* bit 1 is 0 for 100mbit link, 1 for not an 100mbit link */
954 return 100;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400955
956 /* If we get here -> no link at all */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return 0;
959}
960
961
962
963
964
965/*
966 read_mac_address() reads the MAC address from the NIC and stores it in the "dev" structure.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 This function will take the spinlock itself and can, as a result, not be called with the lock helt.
969 */
970static void read_mac_address(struct xircom_private *card)
971{
972 unsigned char j, tuple, link, data_id, data_count;
973 unsigned long flags;
974 int i;
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 spin_lock_irqsave(&card->lock, flags);
977
978 outl(1 << 12, card->io_port + CSR9); /* enable boot rom access */
979 for (i = 0x100; i < 0x1f7; i += link + 2) {
980 outl(i, card->io_port + CSR10);
981 tuple = inl(card->io_port + CSR9) & 0xff;
982 outl(i + 1, card->io_port + CSR10);
983 link = inl(card->io_port + CSR9) & 0xff;
984 outl(i + 2, card->io_port + CSR10);
985 data_id = inl(card->io_port + CSR9) & 0xff;
986 outl(i + 3, card->io_port + CSR10);
987 data_count = inl(card->io_port + CSR9) & 0xff;
988 if ((tuple == 0x22) && (data_id == 0x04) && (data_count == 0x06)) {
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400989 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 * This is it. We have the data we want.
991 */
992 for (j = 0; j < 6; j++) {
993 outl(i + j + 4, card->io_port + CSR10);
994 card->dev->dev_addr[j] = inl(card->io_port + CSR9) & 0xff;
995 }
996 break;
997 } else if (link == 0) {
998 break;
999 }
1000 }
1001 spin_unlock_irqrestore(&card->lock, flags);
Johannes Berge1749612008-10-27 15:59:26 -07001002 pr_debug(" %pM\n", card->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
1005
1006/*
1007 transceiver_voodoo() enables the external UTP plug thingy.
1008 it's called voodoo as I stole this code and cannot cross-reference
1009 it with the specification.
1010 */
1011static void transceiver_voodoo(struct xircom_private *card)
1012{
1013 unsigned long flags;
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /* disable all powermanagement */
1016 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1017
1018 setup_descriptors(card);
1019
1020 spin_lock_irqsave(&card->lock, flags);
1021
1022 outl(0x0008, card->io_port + CSR15);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001023 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 outl(0xa8050000, card->io_port + CSR15);
1025 udelay(25);
1026 outl(0xa00f0000, card->io_port + CSR15);
1027 udelay(25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 spin_unlock_irqrestore(&card->lock, flags);
1030
1031 netif_start_queue(card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
1034
1035static void xircom_up(struct xircom_private *card)
1036{
1037 unsigned long flags;
1038 int i;
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /* disable all powermanagement */
1041 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1042
1043 setup_descriptors(card);
1044
1045 spin_lock_irqsave(&card->lock, flags);
1046
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 enable_link_interrupt(card);
1049 enable_transmit_interrupt(card);
1050 enable_receive_interrupt(card);
1051 enable_common_interrupts(card);
1052 enable_promisc(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /* The card can have received packets already, read them away now */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001055 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 investigate_read_descriptor(card->dev,card,i,bufferoffsets[i]);
1057
1058
1059 spin_unlock_irqrestore(&card->lock, flags);
1060 trigger_receive(card);
1061 trigger_transmit(card);
1062 netif_start_queue(card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
1065/* Bufferoffset is in BYTES */
Joe Perches163ef0b2011-05-09 09:45:21 +00001066static void
1067investigate_read_descriptor(struct net_device *dev, struct xircom_private *card,
1068 int descnr, unsigned int bufferoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Joe Perches163ef0b2011-05-09 09:45:21 +00001070 int status;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001071
Joe Perches163ef0b2011-05-09 09:45:21 +00001072 status = le32_to_cpu(card->rx_buffer[4*descnr]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001073
Joe Perches163ef0b2011-05-09 09:45:21 +00001074 if (status > 0) { /* packet received */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001075
Joe Perches163ef0b2011-05-09 09:45:21 +00001076 /* TODO: discard error packets */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001077
Joe Perches163ef0b2011-05-09 09:45:21 +00001078 short pkt_len = ((status >> 16) & 0x7ff) - 4;
1079 /* minus 4, we don't want the CRC */
1080 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Joe Perches163ef0b2011-05-09 09:45:21 +00001082 if (pkt_len > 1518) {
1083 netdev_err(dev, "Packet length %i is bogus\n", pkt_len);
1084 pkt_len = 1518;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
Joe Perches163ef0b2011-05-09 09:45:21 +00001086
Pradeep A Dalvi21a4e462012-02-05 02:50:10 +00001087 skb = netdev_alloc_skb(dev, pkt_len + 2);
Joe Perches163ef0b2011-05-09 09:45:21 +00001088 if (skb == NULL) {
1089 dev->stats.rx_dropped++;
1090 goto out;
1091 }
1092 skb_reserve(skb, 2);
1093 skb_copy_to_linear_data(skb,
1094 &card->rx_buffer[bufferoffset / 4],
1095 pkt_len);
1096 skb_put(skb, pkt_len);
1097 skb->protocol = eth_type_trans(skb, dev);
1098 netif_rx(skb);
1099 dev->stats.rx_packets++;
1100 dev->stats.rx_bytes += pkt_len;
1101
1102out:
1103 /* give the buffer back to the card */
1104 card->rx_buffer[4*descnr] = cpu_to_le32(0x80000000);
1105 trigger_receive(card);
1106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
1109
1110/* Bufferoffset is in BYTES */
Joe Perches163ef0b2011-05-09 09:45:21 +00001111static void
1112investigate_write_descriptor(struct net_device *dev,
1113 struct xircom_private *card,
1114 int descnr, unsigned int bufferoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Joe Perches163ef0b2011-05-09 09:45:21 +00001116 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Joe Perches163ef0b2011-05-09 09:45:21 +00001118 status = le32_to_cpu(card->tx_buffer[4*descnr]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001119#if 0
Joe Perches163ef0b2011-05-09 09:45:21 +00001120 if (status & 0x8000) { /* Major error */
1121 pr_err("Major transmit error status %x\n", status);
1122 card->tx_buffer[4*descnr] = 0;
1123 netif_wake_queue (dev);
1124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125#endif
Joe Perches163ef0b2011-05-09 09:45:21 +00001126 if (status > 0) { /* bit 31 is 0 when done */
1127 if (card->tx_skb[descnr]!=NULL) {
1128 dev->stats.tx_bytes += card->tx_skb[descnr]->len;
1129 dev_kfree_skb_irq(card->tx_skb[descnr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
Joe Perches163ef0b2011-05-09 09:45:21 +00001131 card->tx_skb[descnr] = NULL;
1132 /* Bit 8 in the status field is 1 if there was a collision */
1133 if (status & (1 << 8))
1134 dev->stats.collisions++;
1135 card->tx_buffer[4*descnr] = 0; /* descriptor is free again */
1136 netif_wake_queue (dev);
1137 dev->stats.tx_packets++;
1138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141static int __init xircom_init(void)
1142{
Alexey Dobriyanec42cdb2006-08-14 23:00:28 -07001143 return pci_register_driver(&xircom_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146static void __exit xircom_exit(void)
1147{
1148 pci_unregister_driver(&xircom_ops);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001149}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001151module_init(xircom_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152module_exit(xircom_exit)
1153