blob: d0d0cbe3dc1747aac652a05d66c8be26deb1c351 [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));
225 if (!dev) {
Joe Perches44298ec2010-01-28 20:59:29 +0000226 pr_err("%s: failed to allocate etherdev\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 goto device_fail;
228 }
229 private = netdev_priv(dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* Allocate the send/receive buffers */
232 private->rx_buffer = pci_alloc_consistent(pdev,8192,&private->rx_dma_handle);
233 if (private->rx_buffer == NULL) {
Joe Perches44298ec2010-01-28 20:59:29 +0000234 pr_err("%s: no memory for rx buffer\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 goto rx_buf_fail;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 private->tx_buffer = pci_alloc_consistent(pdev,8192,&private->tx_dma_handle);
238 if (private->tx_buffer == NULL) {
Joe Perches44298ec2010-01-28 20:59:29 +0000239 pr_err("%s: no memory for tx buffer\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 goto tx_buf_fail;
241 }
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 SET_NETDEV_DEV(dev, &pdev->dev);
244
245
246 private->dev = dev;
247 private->pdev = pdev;
248 private->io_port = pci_resource_start(pdev, 0);
249 spin_lock_init(&private->lock);
250 dev->irq = pdev->irq;
251 dev->base_addr = private->io_port;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 initialize_card(private);
254 read_mac_address(private);
255 setup_descriptors(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400256
Stephen Hemminger1034c9f2009-01-07 18:00:55 -0800257 dev->netdev_ops = &netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 pci_set_drvdata(pdev, dev);
259
260 if (register_netdev(dev)) {
Joe Perches44298ec2010-01-28 20:59:29 +0000261 pr_err("%s: netdevice registration failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 goto reg_fail;
263 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400264
Joe Perches44298ec2010-01-28 20:59:29 +0000265 dev_info(&dev->dev, "Xircom cardbus revision %i at irq %i\n",
266 pdev->revision, pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 /* start the transmitter to get a heartbeat */
268 /* TODO: send 2 dummy packets here */
269 transceiver_voodoo(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 spin_lock_irqsave(&private->lock,flags);
272 activate_transmitter(private);
273 activate_receiver(private);
274 spin_unlock_irqrestore(&private->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 trigger_receive(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return 0;
279
280reg_fail:
281 kfree(private->tx_buffer);
282tx_buf_fail:
283 kfree(private->rx_buffer);
284rx_buf_fail:
285 free_netdev(dev);
286device_fail:
287 return -ENODEV;
288}
289
290
291/*
292 xircom_remove is called on module-unload or on device-eject.
293 it unregisters the irq, io-region and network device.
294 Interrupts and such are already stopped in the "ifconfig ethX down"
295 code.
296 */
297static void __devexit xircom_remove(struct pci_dev *pdev)
298{
299 struct net_device *dev = pci_get_drvdata(pdev);
300 struct xircom_private *card = netdev_priv(dev);
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 pci_free_consistent(pdev,8192,card->rx_buffer,card->rx_dma_handle);
303 pci_free_consistent(pdev,8192,card->tx_buffer,card->tx_dma_handle);
304
305 release_region(dev->base_addr, 128);
306 unregister_netdev(dev);
307 free_netdev(dev);
308 pci_set_drvdata(pdev, NULL);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400309}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
David Howells7d12e782006-10-05 14:55:46 +0100311static irqreturn_t xircom_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 struct net_device *dev = (struct net_device *) dev_instance;
314 struct xircom_private *card = netdev_priv(dev);
315 unsigned int status;
316 int i;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 spin_lock(&card->lock);
319 status = inl(card->io_port+CSR5);
320
Joe Perches54668b82011-05-09 09:45:20 +0000321#if defined DEBUG && DEBUG > 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 print_binary(status);
Joe Perches54668b82011-05-09 09:45:20 +0000323 pr_debug("tx status 0x%08x 0x%08x\n",
324 card->tx_buffer[0], card->tx_buffer[4]);
325 pr_debug("rx status 0x%08x 0x%08x\n",
326 card->rx_buffer[0], card->rx_buffer[4]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400327#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* Handle shared irq and hotplug */
329 if (status == 0 || status == 0xffffffff) {
330 spin_unlock(&card->lock);
331 return IRQ_NONE;
332 }
333
334 if (link_status_changed(card)) {
335 int newlink;
Joe Perches44298ec2010-01-28 20:59:29 +0000336 printk(KERN_DEBUG "xircom_cb: Link status has changed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 newlink = link_status(card);
Joe Perches44298ec2010-01-28 20:59:29 +0000338 dev_info(&dev->dev, "Link is %i mbit\n", newlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (newlink)
340 netif_carrier_on(dev);
341 else
342 netif_carrier_off(dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400346 /* Clear all remaining interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 status |= 0xffffffff; /* FIXME: make this clear only the
348 real existing bits */
349 outl(status,card->io_port+CSR5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400351
352 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 investigate_write_descriptor(dev,card,i,bufferoffsets[i]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400354 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 investigate_read_descriptor(dev,card,i,bufferoffsets[i]);
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 spin_unlock(&card->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return IRQ_HANDLED;
359}
360
Stephen Hemmingerad096462009-08-31 19:50:53 +0000361static netdev_tx_t xircom_start_xmit(struct sk_buff *skb,
362 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 struct xircom_private *card;
365 unsigned long flags;
366 int nextdescriptor;
367 int desc;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 card = netdev_priv(dev);
370 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /* First see if we can free some descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400373 for (desc=0;desc<NUMDESCRIPTORS;desc++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 investigate_write_descriptor(dev,card,desc,bufferoffsets[desc]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400375
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 nextdescriptor = (card->transmit_used +1) % (NUMDESCRIPTORS);
378 desc = card->transmit_used;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* only send the packet if the descriptor is free */
381 if (card->tx_buffer[4*desc]==0) {
382 /* Copy the packet data; zero the memory first as the card
383 sometimes sends more than you ask it to. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 memset(&card->tx_buffer[bufferoffsets[desc]/4],0,1536);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300386 skb_copy_from_linear_data(skb,
387 &(card->tx_buffer[bufferoffsets[desc] / 4]),
388 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /* FIXME: The specification tells us that the length we send HAS to be a multiple of
390 4 bytes. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400391
Al Viro6f35d5d2007-12-23 20:01:04 +0000392 card->tx_buffer[4*desc+1] = cpu_to_le32(skb->len);
393 if (desc == NUMDESCRIPTORS - 1) /* bit 25: last descriptor of the ring */
394 card->tx_buffer[4*desc+1] |= cpu_to_le32(1<<25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Al Viro6f35d5d2007-12-23 20:01:04 +0000396 card->tx_buffer[4*desc+1] |= cpu_to_le32(0xF0000000);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400397 /* 0xF0... means want interrupts*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 card->tx_skb[desc] = skb;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 wmb();
401 /* This gives the descriptor to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000402 card->tx_buffer[4*desc] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 trigger_transmit(card);
Al Viro6f35d5d2007-12-23 20:01:04 +0000404 if (card->tx_buffer[nextdescriptor*4] & cpu_to_le32(0x8000000)) {
405 /* next descriptor is occupied... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 netif_stop_queue(dev);
407 }
408 card->transmit_used = nextdescriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 spin_unlock_irqrestore(&card->lock,flags);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000410 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /* Uh oh... no free descriptor... drop the packet */
414 netif_stop_queue(dev);
415 spin_unlock_irqrestore(&card->lock,flags);
416 trigger_transmit(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400417
Erik Mouw06f75252008-02-04 18:56:54 +0100418 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421
422
423
424static int xircom_open(struct net_device *dev)
425{
426 struct xircom_private *xp = netdev_priv(dev);
427 int retval;
Joe Perches54668b82011-05-09 09:45:20 +0000428
Frans Popc2bb1b92010-03-24 07:57:34 +0000429 pr_info("xircom cardbus adaptor found, registering as %s, using irq %i\n",
Joe Perches44298ec2010-01-28 20:59:29 +0000430 dev->name, dev->irq);
Joe Perchesa0607fd2009-11-18 23:29:17 -0800431 retval = request_irq(dev->irq, xircom_interrupt, IRQF_SHARED, dev->name, dev);
Joe Perches54668b82011-05-09 09:45:20 +0000432 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return retval;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 xircom_up(xp);
436 xp->open = 1;
Joe Perches54668b82011-05-09 09:45:20 +0000437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439}
440
441static int xircom_close(struct net_device *dev)
442{
443 struct xircom_private *card;
444 unsigned long flags;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 card = netdev_priv(dev);
447 netif_stop_queue(dev); /* we don't want new packets */
448
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 disable_all_interrupts(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400453#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /* We can enable this again once we send dummy packets on ifconfig ethX up */
455 deactivate_receiver(card);
456 deactivate_transmitter(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400457#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 remove_descriptors(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 spin_unlock_irqrestore(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 card->open = 0;
463 free_irq(dev->irq,dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return 0;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470#ifdef CONFIG_NET_POLL_CONTROLLER
471static void xircom_poll_controller(struct net_device *dev)
472{
473 disable_irq(dev->irq);
David Howells7d12e782006-10-05 14:55:46 +0100474 xircom_interrupt(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 enable_irq(dev->irq);
476}
477#endif
478
479
480static void initialize_card(struct xircom_private *card)
481{
482 unsigned int val;
483 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 spin_lock_irqsave(&card->lock, flags);
486
487 /* First: reset the card */
488 val = inl(card->io_port + CSR0);
489 val |= 0x01; /* Software reset */
490 outl(val, card->io_port + CSR0);
491
492 udelay(100); /* give the card some time to reset */
493
494 val = inl(card->io_port + CSR0);
495 val &= ~0x01; /* disable Software reset */
496 outl(val, card->io_port + CSR0);
497
498
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400499 val = 0; /* Value 0x00 is a safe and conservative value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 for the PCI configuration settings */
501 outl(val, card->io_port + CSR0);
502
503
504 disable_all_interrupts(card);
505 deactivate_receiver(card);
506 deactivate_transmitter(card);
507
508 spin_unlock_irqrestore(&card->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511/*
512trigger_transmit causes the card to check for frames to be transmitted.
513This is accomplished by writing to the CSR1 port. The documentation
514claims that the act of writing is sufficient and that the value is
515ignored; I chose zero.
516*/
517static void trigger_transmit(struct xircom_private *card)
518{
519 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 val = 0;
522 outl(val, card->io_port + CSR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
525/*
526trigger_receive causes the card to check for empty frames in the
527descriptor list in which packets can be received.
528This is accomplished by writing to the CSR2 port. The documentation
529claims that the act of writing is sufficient and that the value is
530ignored; I chose zero.
531*/
532static void trigger_receive(struct xircom_private *card)
533{
534 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 val = 0;
537 outl(val, card->io_port + CSR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
540/*
541setup_descriptors initializes the send and receive buffers to be valid
542descriptors and programs the addresses into the card.
543*/
544static void setup_descriptors(struct xircom_private *card)
545{
Al Viro6f35d5d2007-12-23 20:01:04 +0000546 u32 address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Eric Sesterhenn / snakebytea707cd62006-01-26 22:02:51 +0100549 BUG_ON(card->rx_buffer == NULL);
550 BUG_ON(card->tx_buffer == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 /* Receive descriptors */
553 memset(card->rx_buffer, 0, 128); /* clear the descriptors */
554 for (i=0;i<NUMDESCRIPTORS;i++ ) {
555
556 /* Rx Descr0: It's empty, let the card own it, no errors -> 0x80000000 */
Al Viro6f35d5d2007-12-23 20:01:04 +0000557 card->rx_buffer[i*4 + 0] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* Rx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
Al Viro6f35d5d2007-12-23 20:01:04 +0000559 card->rx_buffer[i*4 + 1] = cpu_to_le32(1536);
560 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
561 card->rx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 /* Rx Descr2: address of the buffer
564 we store the buffer at the 2nd half of the page */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400565
Al Viro6f35d5d2007-12-23 20:01:04 +0000566 address = card->rx_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 card->rx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
568 /* Rx Desc3: address of 2nd buffer -> 0 */
569 card->rx_buffer[i*4 + 3] = 0;
570 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 wmb();
573 /* Write the receive descriptor ring address to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000574 address = card->rx_dma_handle;
575 outl(address, card->io_port + CSR3); /* Receive descr list address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577
578 /* transmit descriptors */
579 memset(card->tx_buffer, 0, 128); /* clear the descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 for (i=0;i<NUMDESCRIPTORS;i++ ) {
582 /* Tx Descr0: Empty, we own it, no errors -> 0x00000000 */
583 card->tx_buffer[i*4 + 0] = 0x00000000;
584 /* Tx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
Al Viro6f35d5d2007-12-23 20:01:04 +0000585 card->tx_buffer[i*4 + 1] = cpu_to_le32(1536);
586 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
587 card->tx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* Tx Descr2: address of the buffer
590 we store the buffer at the 2nd half of the page */
Al Viro6f35d5d2007-12-23 20:01:04 +0000591 address = card->tx_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 card->tx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
593 /* Tx Desc3: address of 2nd buffer -> 0 */
594 card->tx_buffer[i*4 + 3] = 0;
595 }
596
597 wmb();
598 /* wite the transmit descriptor ring to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000599 address = card->tx_dma_handle;
600 outl(address, card->io_port + CSR4); /* xmit descr list address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603/*
604remove_descriptors informs the card the descriptors are no longer
605valid by setting the address in the card to 0x00.
606*/
607static void remove_descriptors(struct xircom_private *card)
608{
609 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 val = 0;
612 outl(val, card->io_port + CSR3); /* Receive descriptor address */
613 outl(val, card->io_port + CSR4); /* Send descriptor address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616/*
617link_status_changed returns 1 if the card has indicated that
618the link status has changed. The new link status has to be read from CSR12.
619
620This function also clears the status-bit.
621*/
622static int link_status_changed(struct xircom_private *card)
623{
624 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 val = inl(card->io_port + CSR5); /* Status register */
627
Joe Perches54668b82011-05-09 09:45:20 +0000628 if ((val & (1 << 27)) == 0) /* no change */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 /* clear the event by writing a 1 to the bit in the
632 status register. */
633 val = (1 << 27);
634 outl(val, card->io_port + CSR5);
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return 1;
637}
638
639
640/*
641transmit_active returns 1 if the transmitter on the card is
642in a non-stopped state.
643*/
644static int transmit_active(struct xircom_private *card)
645{
646 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 val = inl(card->io_port + CSR5); /* Status register */
649
Joe Perches54668b82011-05-09 09:45:20 +0000650 if ((val & (7 << 20)) == 0) /* transmitter disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return 1;
654}
655
656/*
657receive_active returns 1 if the receiver on the card is
658in a non-stopped state.
659*/
660static int receive_active(struct xircom_private *card)
661{
662 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 val = inl(card->io_port + CSR5); /* Status register */
665
Joe Perches54668b82011-05-09 09:45:20 +0000666 if ((val & (7 << 17)) == 0) /* receiver disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return 1;
670}
671
672/*
673activate_receiver enables the receiver on the card.
674Before being allowed to active the receiver, the receiver
675must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400676this code actually disables the receiver first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677receiver to become inactive, then it activates the receiver and then
678it waits for the receiver to be active.
679
680must be called with the lock held and interrupts disabled.
681*/
682static void activate_receiver(struct xircom_private *card)
683{
684 unsigned int val;
685 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 val = inl(card->io_port + CSR6); /* Operation mode */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /* If the "active" bit is set and the receiver is already
690 active, no need to do the expensive thing */
691 if ((val&2) && (receive_active(card)))
692 return;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400693
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 val = val & ~2; /* disable the receiver */
696 outl(val, card->io_port + CSR6);
697
698 counter = 10;
699 while (counter > 0) {
700 if (!receive_active(card))
701 break;
702 /* wait a while */
703 udelay(50);
704 counter--;
705 if (counter <= 0)
Joe Perches44298ec2010-01-28 20:59:29 +0000706 pr_err("Receiver failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708
709 /* enable the receiver */
710 val = inl(card->io_port + CSR6); /* Operation mode */
711 val = val | 2; /* enable the receiver */
712 outl(val, card->io_port + CSR6);
713
714 /* now wait for the card to activate again */
715 counter = 10;
716 while (counter > 0) {
717 if (receive_active(card))
718 break;
719 /* wait a while */
720 udelay(50);
721 counter--;
722 if (counter <= 0)
Joe Perches44298ec2010-01-28 20:59:29 +0000723 pr_err("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 Perches44298ec2010-01-28 20:59:29 +0000751 pr_err("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 Perches44298ec2010-01-28 20:59:29 +0000789 pr_err("Transmitter failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
792 /* enable the transmitter */
793 val = inl(card->io_port + CSR6); /* Operation mode */
794 val = val | (1 << 13); /* enable the transmitter */
795 outl(val, card->io_port + CSR6);
796
797 /* now wait for the card to activate again */
798 counter = 10;
799 while (counter > 0) {
800 if (transmit_active(card))
801 break;
802 /* wait a while */
803 udelay(50);
804 counter--;
805 if (counter <= 0)
Joe Perches44298ec2010-01-28 20:59:29 +0000806 pr_err("Transmitter failed to re-activate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810/*
811deactivate_transmitter disables the transmitter on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400812To achieve this this code disables the transmitter first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813then it waits for the transmitter to become inactive.
814
815must be called with the lock held and interrupts disabled.
816*/
817static void deactivate_transmitter(struct xircom_private *card)
818{
819 unsigned int val;
820 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 val = inl(card->io_port + CSR6); /* Operation mode */
823 val = val & ~2; /* disable the transmitter */
824 outl(val, card->io_port + CSR6);
825
826 counter = 20;
827 while (counter > 0) {
828 if (!transmit_active(card))
829 break;
830 /* wait a while */
831 udelay(50);
832 counter--;
833 if (counter <= 0)
Joe Perches44298ec2010-01-28 20:59:29 +0000834 pr_err("Transmitter failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
838
839/*
840enable_transmit_interrupt enables the transmit interrupt
841
842must be called with the lock held and interrupts disabled.
843*/
844static void enable_transmit_interrupt(struct xircom_private *card)
845{
846 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 val = inl(card->io_port + CSR7); /* Interrupt enable register */
849 val |= 1; /* enable the transmit interrupt */
850 outl(val, card->io_port + CSR7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853
854/*
855enable_receive_interrupt enables the receive interrupt
856
857must be called with the lock held and interrupts disabled.
858*/
859static void enable_receive_interrupt(struct xircom_private *card)
860{
861 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 val = inl(card->io_port + CSR7); /* Interrupt enable register */
864 val = val | (1 << 6); /* enable the receive interrupt */
865 outl(val, card->io_port + CSR7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
868/*
869enable_link_interrupt enables the link status change interrupt
870
871must be called with the lock held and interrupts disabled.
872*/
873static void enable_link_interrupt(struct xircom_private *card)
874{
875 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 val = inl(card->io_port + CSR7); /* Interrupt enable register */
878 val = val | (1 << 27); /* enable the link status chage interrupt */
879 outl(val, card->io_port + CSR7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
882
883
884/*
885disable_all_interrupts disables all interrupts
886
887must be called with the lock held and interrupts disabled.
888*/
889static void disable_all_interrupts(struct xircom_private *card)
890{
891 unsigned int val;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 val = 0; /* disable all interrupts */
894 outl(val, card->io_port + CSR7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
897/*
898enable_common_interrupts enables several weird interrupts
899
900must be called with the lock held and interrupts disabled.
901*/
902static void enable_common_interrupts(struct xircom_private *card)
903{
904 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 val = inl(card->io_port + CSR7); /* Interrupt enable register */
907 val |= (1<<16); /* Normal Interrupt Summary */
908 val |= (1<<15); /* Abnormal Interrupt Summary */
909 val |= (1<<13); /* Fatal bus error */
910 val |= (1<<8); /* Receive Process Stopped */
911 val |= (1<<7); /* Receive Buffer Unavailable */
912 val |= (1<<5); /* Transmit Underflow */
913 val |= (1<<2); /* Transmit Buffer Unavailable */
914 val |= (1<<1); /* Transmit Process Stopped */
915 outl(val, card->io_port + CSR7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918/*
919enable_promisc starts promisc mode
920
921must be called with the lock held and interrupts disabled.
922*/
923static int enable_promisc(struct xircom_private *card)
924{
925 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400927 val = inl(card->io_port + CSR6);
928 val = val | (1 << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 outl(val, card->io_port + CSR6);
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return 1;
932}
933
934
935
936
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400937/*
Michael Opdenacker59c51592007-05-09 08:57:56 +0200938link_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 -0700939
940Must be called in locked state with interrupts disabled
941*/
942static int link_status(struct xircom_private *card)
943{
944 unsigned int val;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 val = inb(card->io_port + CSR12);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (!(val&(1<<2))) /* bit 2 is 0 for 10mbit link, 1 for not an 10mbit link */
949 return 10;
950 if (!(val&(1<<1))) /* bit 1 is 0 for 100mbit link, 1 for not an 100mbit link */
951 return 100;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400952
953 /* If we get here -> no link at all */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return 0;
956}
957
958
959
960
961
962/*
963 read_mac_address() reads the MAC address from the NIC and stores it in the "dev" structure.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 This function will take the spinlock itself and can, as a result, not be called with the lock helt.
966 */
967static void read_mac_address(struct xircom_private *card)
968{
969 unsigned char j, tuple, link, data_id, data_count;
970 unsigned long flags;
971 int i;
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 spin_lock_irqsave(&card->lock, flags);
974
975 outl(1 << 12, card->io_port + CSR9); /* enable boot rom access */
976 for (i = 0x100; i < 0x1f7; i += link + 2) {
977 outl(i, card->io_port + CSR10);
978 tuple = inl(card->io_port + CSR9) & 0xff;
979 outl(i + 1, card->io_port + CSR10);
980 link = inl(card->io_port + CSR9) & 0xff;
981 outl(i + 2, card->io_port + CSR10);
982 data_id = inl(card->io_port + CSR9) & 0xff;
983 outl(i + 3, card->io_port + CSR10);
984 data_count = inl(card->io_port + CSR9) & 0xff;
985 if ((tuple == 0x22) && (data_id == 0x04) && (data_count == 0x06)) {
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400986 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 * This is it. We have the data we want.
988 */
989 for (j = 0; j < 6; j++) {
990 outl(i + j + 4, card->io_port + CSR10);
991 card->dev->dev_addr[j] = inl(card->io_port + CSR9) & 0xff;
992 }
993 break;
994 } else if (link == 0) {
995 break;
996 }
997 }
998 spin_unlock_irqrestore(&card->lock, flags);
Johannes Berge1749612008-10-27 15:59:26 -0700999 pr_debug(" %pM\n", card->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
1002
1003/*
1004 transceiver_voodoo() enables the external UTP plug thingy.
1005 it's called voodoo as I stole this code and cannot cross-reference
1006 it with the specification.
1007 */
1008static void transceiver_voodoo(struct xircom_private *card)
1009{
1010 unsigned long flags;
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /* disable all powermanagement */
1013 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1014
1015 setup_descriptors(card);
1016
1017 spin_lock_irqsave(&card->lock, flags);
1018
1019 outl(0x0008, card->io_port + CSR15);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001020 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 outl(0xa8050000, card->io_port + CSR15);
1022 udelay(25);
1023 outl(0xa00f0000, card->io_port + CSR15);
1024 udelay(25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 spin_unlock_irqrestore(&card->lock, flags);
1027
1028 netif_start_queue(card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
1031
1032static void xircom_up(struct xircom_private *card)
1033{
1034 unsigned long flags;
1035 int i;
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /* disable all powermanagement */
1038 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1039
1040 setup_descriptors(card);
1041
1042 spin_lock_irqsave(&card->lock, flags);
1043
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 enable_link_interrupt(card);
1046 enable_transmit_interrupt(card);
1047 enable_receive_interrupt(card);
1048 enable_common_interrupts(card);
1049 enable_promisc(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /* The card can have received packets already, read them away now */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001052 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 investigate_read_descriptor(card->dev,card,i,bufferoffsets[i]);
1054
1055
1056 spin_unlock_irqrestore(&card->lock, flags);
1057 trigger_receive(card);
1058 trigger_transmit(card);
1059 netif_start_queue(card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062/* Bufferoffset is in BYTES */
1063static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset)
1064{
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001065 int status;
1066
Al Viro6f35d5d2007-12-23 20:01:04 +00001067 status = le32_to_cpu(card->rx_buffer[4*descnr]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if ((status > 0)) { /* packet received */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /* TODO: discard error packets */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 short pkt_len = ((status >> 16) & 0x7ff) - 4; /* minus 4, we don't want the CRC */
1074 struct sk_buff *skb;
1075
1076 if (pkt_len > 1518) {
Joe Perches44298ec2010-01-28 20:59:29 +00001077 pr_err("Packet length %i is bogus\n", pkt_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 pkt_len = 1518;
1079 }
1080
1081 skb = dev_alloc_skb(pkt_len + 2);
1082 if (skb == NULL) {
Stephen Hemminger1034c9f2009-01-07 18:00:55 -08001083 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 goto out;
1085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 skb_reserve(skb, 2);
David S. Miller8c7b7fa2007-07-10 22:08:12 -07001087 skb_copy_to_linear_data(skb, (unsigned char*)&card->rx_buffer[bufferoffset / 4], pkt_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 skb_put(skb, pkt_len);
1089 skb->protocol = eth_type_trans(skb, dev);
1090 netif_rx(skb);
Stephen Hemminger1034c9f2009-01-07 18:00:55 -08001091 dev->stats.rx_packets++;
1092 dev->stats.rx_bytes += pkt_len;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 out:
1095 /* give the buffer back to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +00001096 card->rx_buffer[4*descnr] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 trigger_receive(card);
1098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
1101
1102/* Bufferoffset is in BYTES */
1103static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset)
1104{
1105 int status;
1106
Al Viro6f35d5d2007-12-23 20:01:04 +00001107 status = le32_to_cpu(card->tx_buffer[4*descnr]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001108#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (status & 0x8000) { /* Major error */
Joe Perches44298ec2010-01-28 20:59:29 +00001110 pr_err("Major transmit error status %x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 card->tx_buffer[4*descnr] = 0;
1112 netif_wake_queue (dev);
1113 }
1114#endif
1115 if (status > 0) { /* bit 31 is 0 when done */
1116 if (card->tx_skb[descnr]!=NULL) {
Stephen Hemminger1034c9f2009-01-07 18:00:55 -08001117 dev->stats.tx_bytes += card->tx_skb[descnr]->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 dev_kfree_skb_irq(card->tx_skb[descnr]);
1119 }
1120 card->tx_skb[descnr] = NULL;
1121 /* Bit 8 in the status field is 1 if there was a collision */
1122 if (status&(1<<8))
Stephen Hemminger1034c9f2009-01-07 18:00:55 -08001123 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 card->tx_buffer[4*descnr] = 0; /* descriptor is free again */
1125 netif_wake_queue (dev);
Stephen Hemminger1034c9f2009-01-07 18:00:55 -08001126 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131
1132static int __init xircom_init(void)
1133{
Alexey Dobriyanec42cdb2006-08-14 23:00:28 -07001134 return pci_register_driver(&xircom_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137static void __exit xircom_exit(void)
1138{
1139 pci_unregister_driver(&xircom_ops);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001140}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001142module_init(xircom_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143module_exit(xircom_exit)
1144