blob: cbcc6d6f3f31481eca977a202090617fb4a64619 [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{
Francois Romieud59a1882012-03-11 23:34:54 +0100195 struct device *d = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 struct net_device *dev = NULL;
197 struct xircom_private *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 unsigned long flags;
199 unsigned short tmp16;
Francois Romieud59a1882012-03-11 23:34:54 +0100200 int rc;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* First do the PCI initialisation */
203
Francois Romieud59a1882012-03-11 23:34:54 +0100204 rc = pci_enable_device(pdev);
205 if (rc < 0)
206 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 /* disable all powermanagement */
209 pci_write_config_dword(pdev, PCI_POWERMGMT, 0x0000);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 pci_set_master(pdev); /* Why isn't this done by pci_enable_device ?*/
212
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400213 /* clear PCI status, if any */
214 pci_read_config_word (pdev,PCI_STATUS, &tmp16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 pci_write_config_word (pdev, PCI_STATUS,tmp16);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400216
Francois Romieud59a1882012-03-11 23:34:54 +0100217 rc = pci_request_regions(pdev, "xircom_cb");
218 if (rc < 0) {
Joe Perches44298ec2010-01-28 20:59:29 +0000219 pr_err("%s: failed to allocate io-region\n", __func__);
Francois Romieud59a1882012-03-11 23:34:54 +0100220 goto err_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222
Francois Romieud59a1882012-03-11 23:34:54 +0100223 rc = -ENOMEM;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400224 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 Before changing the hardware, allocate the memory.
226 This way, we can fail gracefully if not enough memory
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400227 is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
229 dev = alloc_etherdev(sizeof(struct xircom_private));
Joe Perches41de8d42012-01-29 13:47:52 +0000230 if (!dev)
Francois Romieud59a1882012-03-11 23:34:54 +0100231 goto err_release;
Joe Perches41de8d42012-01-29 13:47:52 +0000232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 private = netdev_priv(dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* Allocate the send/receive buffers */
Francois Romieud59a1882012-03-11 23:34:54 +0100236 private->rx_buffer = dma_alloc_coherent(d, 8192,
237 &private->rx_dma_handle,
238 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (private->rx_buffer == NULL) {
Joe Perches44298ec2010-01-28 20:59:29 +0000240 pr_err("%s: no memory for rx buffer\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto rx_buf_fail;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400242 }
Francois Romieud59a1882012-03-11 23:34:54 +0100243 private->tx_buffer = dma_alloc_coherent(d, 8192,
244 &private->tx_dma_handle,
245 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (private->tx_buffer == NULL) {
Joe Perches44298ec2010-01-28 20:59:29 +0000247 pr_err("%s: no memory for tx buffer\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto tx_buf_fail;
249 }
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 SET_NETDEV_DEV(dev, &pdev->dev);
252
253
254 private->dev = dev;
255 private->pdev = pdev;
256 private->io_port = pci_resource_start(pdev, 0);
257 spin_lock_init(&private->lock);
258 dev->irq = pdev->irq;
259 dev->base_addr = private->io_port;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 initialize_card(private);
262 read_mac_address(private);
263 setup_descriptors(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400264
Stephen Hemminger1034c9f2009-01-07 18:00:55 -0800265 dev->netdev_ops = &netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 pci_set_drvdata(pdev, dev);
267
Francois Romieud59a1882012-03-11 23:34:54 +0100268 rc = register_netdev(dev);
269 if (rc < 0) {
Joe Perches44298ec2010-01-28 20:59:29 +0000270 pr_err("%s: netdevice registration failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 goto reg_fail;
272 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400273
Joe Perches163ef0b2011-05-09 09:45:21 +0000274 netdev_info(dev, "Xircom cardbus revision %i at irq %i\n",
275 pdev->revision, pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* start the transmitter to get a heartbeat */
277 /* TODO: send 2 dummy packets here */
278 transceiver_voodoo(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 spin_lock_irqsave(&private->lock,flags);
281 activate_transmitter(private);
282 activate_receiver(private);
283 spin_unlock_irqrestore(&private->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 trigger_receive(private);
Francois Romieud59a1882012-03-11 23:34:54 +0100286out:
287 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289reg_fail:
Francois Romieud59a1882012-03-11 23:34:54 +0100290 pci_set_drvdata(pdev, NULL);
291 dma_free_coherent(d, 8192, private->tx_buffer, private->tx_dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292tx_buf_fail:
Francois Romieud59a1882012-03-11 23:34:54 +0100293 dma_free_coherent(d, 8192, private->rx_buffer, private->rx_dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294rx_buf_fail:
295 free_netdev(dev);
Francois Romieud59a1882012-03-11 23:34:54 +0100296err_release:
297 pci_release_regions(pdev);
298err_disable:
299 pci_disable_device(pdev);
300 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303
304/*
305 xircom_remove is called on module-unload or on device-eject.
306 it unregisters the irq, io-region and network device.
307 Interrupts and such are already stopped in the "ifconfig ethX down"
308 code.
309 */
310static void __devexit xircom_remove(struct pci_dev *pdev)
311{
312 struct net_device *dev = pci_get_drvdata(pdev);
313 struct xircom_private *card = netdev_priv(dev);
Francois Romieud59a1882012-03-11 23:34:54 +0100314 struct device *d = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 unregister_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 pci_set_drvdata(pdev, NULL);
Francois Romieud59a1882012-03-11 23:34:54 +0100318 dma_free_coherent(d, 8192, card->tx_buffer, card->tx_dma_handle);
319 dma_free_coherent(d, 8192, card->rx_buffer, card->rx_dma_handle);
320 free_netdev(dev);
321 pci_release_regions(pdev);
322 pci_disable_device(pdev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400323}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
David Howells7d12e782006-10-05 14:55:46 +0100325static irqreturn_t xircom_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 struct net_device *dev = (struct net_device *) dev_instance;
328 struct xircom_private *card = netdev_priv(dev);
329 unsigned int status;
330 int i;
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 spin_lock(&card->lock);
333 status = inl(card->io_port+CSR5);
334
Joe Perches54668b82011-05-09 09:45:20 +0000335#if defined DEBUG && DEBUG > 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 print_binary(status);
Joe Perches54668b82011-05-09 09:45:20 +0000337 pr_debug("tx status 0x%08x 0x%08x\n",
338 card->tx_buffer[0], card->tx_buffer[4]);
339 pr_debug("rx status 0x%08x 0x%08x\n",
340 card->rx_buffer[0], card->rx_buffer[4]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400341#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* Handle shared irq and hotplug */
343 if (status == 0 || status == 0xffffffff) {
344 spin_unlock(&card->lock);
345 return IRQ_NONE;
346 }
347
348 if (link_status_changed(card)) {
349 int newlink;
Joe Perches726b65a2011-05-09 09:45:22 +0000350 netdev_dbg(dev, "Link status has changed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 newlink = link_status(card);
Joe Perches163ef0b2011-05-09 09:45:21 +0000352 netdev_info(dev, "Link is %d mbit\n", newlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (newlink)
354 netif_carrier_on(dev);
355 else
356 netif_carrier_off(dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400360 /* Clear all remaining interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 status |= 0xffffffff; /* FIXME: make this clear only the
362 real existing bits */
363 outl(status,card->io_port+CSR5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400365
366 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 investigate_write_descriptor(dev,card,i,bufferoffsets[i]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400368 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 investigate_read_descriptor(dev,card,i,bufferoffsets[i]);
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 spin_unlock(&card->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return IRQ_HANDLED;
373}
374
Stephen Hemmingerad096462009-08-31 19:50:53 +0000375static netdev_tx_t xircom_start_xmit(struct sk_buff *skb,
376 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 struct xircom_private *card;
379 unsigned long flags;
380 int nextdescriptor;
381 int desc;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 card = netdev_priv(dev);
384 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* First see if we can free some descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400387 for (desc=0;desc<NUMDESCRIPTORS;desc++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 investigate_write_descriptor(dev,card,desc,bufferoffsets[desc]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400389
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 nextdescriptor = (card->transmit_used +1) % (NUMDESCRIPTORS);
392 desc = card->transmit_used;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /* only send the packet if the descriptor is free */
395 if (card->tx_buffer[4*desc]==0) {
396 /* Copy the packet data; zero the memory first as the card
397 sometimes sends more than you ask it to. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 memset(&card->tx_buffer[bufferoffsets[desc]/4],0,1536);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300400 skb_copy_from_linear_data(skb,
401 &(card->tx_buffer[bufferoffsets[desc] / 4]),
402 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /* FIXME: The specification tells us that the length we send HAS to be a multiple of
404 4 bytes. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400405
Al Viro6f35d5d2007-12-23 20:01:04 +0000406 card->tx_buffer[4*desc+1] = cpu_to_le32(skb->len);
407 if (desc == NUMDESCRIPTORS - 1) /* bit 25: last descriptor of the ring */
408 card->tx_buffer[4*desc+1] |= cpu_to_le32(1<<25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Al Viro6f35d5d2007-12-23 20:01:04 +0000410 card->tx_buffer[4*desc+1] |= cpu_to_le32(0xF0000000);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400411 /* 0xF0... means want interrupts*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 card->tx_skb[desc] = skb;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 wmb();
415 /* This gives the descriptor to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000416 card->tx_buffer[4*desc] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 trigger_transmit(card);
Al Viro6f35d5d2007-12-23 20:01:04 +0000418 if (card->tx_buffer[nextdescriptor*4] & cpu_to_le32(0x8000000)) {
419 /* next descriptor is occupied... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 netif_stop_queue(dev);
421 }
422 card->transmit_used = nextdescriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 spin_unlock_irqrestore(&card->lock,flags);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000424 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /* Uh oh... no free descriptor... drop the packet */
428 netif_stop_queue(dev);
429 spin_unlock_irqrestore(&card->lock,flags);
430 trigger_transmit(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400431
Erik Mouw06f75252008-02-04 18:56:54 +0100432 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435
436
437
438static int xircom_open(struct net_device *dev)
439{
440 struct xircom_private *xp = netdev_priv(dev);
441 int retval;
Joe Perches54668b82011-05-09 09:45:20 +0000442
Joe Perches163ef0b2011-05-09 09:45:21 +0000443 netdev_info(dev, "xircom cardbus adaptor found, using irq %i\n",
444 dev->irq);
Joe Perchesa0607fd2009-11-18 23:29:17 -0800445 retval = request_irq(dev->irq, xircom_interrupt, IRQF_SHARED, dev->name, dev);
Joe Perches54668b82011-05-09 09:45:20 +0000446 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return retval;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 xircom_up(xp);
450 xp->open = 1;
Joe Perches54668b82011-05-09 09:45:20 +0000451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return 0;
453}
454
455static int xircom_close(struct net_device *dev)
456{
457 struct xircom_private *card;
458 unsigned long flags;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 card = netdev_priv(dev);
461 netif_stop_queue(dev); /* we don't want new packets */
462
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 disable_all_interrupts(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400467#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* We can enable this again once we send dummy packets on ifconfig ethX up */
469 deactivate_receiver(card);
470 deactivate_transmitter(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400471#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 remove_descriptors(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 spin_unlock_irqrestore(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 card->open = 0;
477 free_irq(dev->irq,dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484#ifdef CONFIG_NET_POLL_CONTROLLER
485static void xircom_poll_controller(struct net_device *dev)
486{
487 disable_irq(dev->irq);
David Howells7d12e782006-10-05 14:55:46 +0100488 xircom_interrupt(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 enable_irq(dev->irq);
490}
491#endif
492
493
494static void initialize_card(struct xircom_private *card)
495{
496 unsigned int val;
497 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 spin_lock_irqsave(&card->lock, flags);
500
501 /* First: reset the card */
502 val = inl(card->io_port + CSR0);
503 val |= 0x01; /* Software reset */
504 outl(val, card->io_port + CSR0);
505
506 udelay(100); /* give the card some time to reset */
507
508 val = inl(card->io_port + CSR0);
509 val &= ~0x01; /* disable Software reset */
510 outl(val, card->io_port + CSR0);
511
512
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400513 val = 0; /* Value 0x00 is a safe and conservative value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 for the PCI configuration settings */
515 outl(val, card->io_port + CSR0);
516
517
518 disable_all_interrupts(card);
519 deactivate_receiver(card);
520 deactivate_transmitter(card);
521
522 spin_unlock_irqrestore(&card->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
525/*
526trigger_transmit causes the card to check for frames to be transmitted.
527This is accomplished by writing to the CSR1 port. The documentation
528claims that the act of writing is sufficient and that the value is
529ignored; I chose zero.
530*/
531static void trigger_transmit(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 + CSR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539/*
540trigger_receive causes the card to check for empty frames in the
541descriptor list in which packets can be received.
542This is accomplished by writing to the CSR2 port. The documentation
543claims that the act of writing is sufficient and that the value is
544ignored; I chose zero.
545*/
546static void trigger_receive(struct xircom_private *card)
547{
548 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 val = 0;
551 outl(val, card->io_port + CSR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554/*
555setup_descriptors initializes the send and receive buffers to be valid
556descriptors and programs the addresses into the card.
557*/
558static void setup_descriptors(struct xircom_private *card)
559{
Al Viro6f35d5d2007-12-23 20:01:04 +0000560 u32 address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Eric Sesterhenn / snakebytea707cd62006-01-26 22:02:51 +0100563 BUG_ON(card->rx_buffer == NULL);
564 BUG_ON(card->tx_buffer == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /* Receive descriptors */
567 memset(card->rx_buffer, 0, 128); /* clear the descriptors */
568 for (i=0;i<NUMDESCRIPTORS;i++ ) {
569
570 /* Rx Descr0: It's empty, let the card own it, no errors -> 0x80000000 */
Al Viro6f35d5d2007-12-23 20:01:04 +0000571 card->rx_buffer[i*4 + 0] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* Rx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
Al Viro6f35d5d2007-12-23 20:01:04 +0000573 card->rx_buffer[i*4 + 1] = cpu_to_le32(1536);
574 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
575 card->rx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 /* Rx Descr2: address of the buffer
578 we store the buffer at the 2nd half of the page */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400579
Al Viro6f35d5d2007-12-23 20:01:04 +0000580 address = card->rx_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 card->rx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
582 /* Rx Desc3: address of 2nd buffer -> 0 */
583 card->rx_buffer[i*4 + 3] = 0;
584 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 wmb();
587 /* Write the receive descriptor ring address to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000588 address = card->rx_dma_handle;
589 outl(address, card->io_port + CSR3); /* Receive descr list address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591
592 /* transmit descriptors */
593 memset(card->tx_buffer, 0, 128); /* clear the descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 for (i=0;i<NUMDESCRIPTORS;i++ ) {
596 /* Tx Descr0: Empty, we own it, no errors -> 0x00000000 */
597 card->tx_buffer[i*4 + 0] = 0x00000000;
598 /* Tx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
Al Viro6f35d5d2007-12-23 20:01:04 +0000599 card->tx_buffer[i*4 + 1] = cpu_to_le32(1536);
600 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
601 card->tx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /* Tx Descr2: address of the buffer
604 we store the buffer at the 2nd half of the page */
Al Viro6f35d5d2007-12-23 20:01:04 +0000605 address = card->tx_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 card->tx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
607 /* Tx Desc3: address of 2nd buffer -> 0 */
608 card->tx_buffer[i*4 + 3] = 0;
609 }
610
611 wmb();
612 /* wite the transmit descriptor ring to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000613 address = card->tx_dma_handle;
614 outl(address, card->io_port + CSR4); /* xmit descr list address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617/*
618remove_descriptors informs the card the descriptors are no longer
619valid by setting the address in the card to 0x00.
620*/
621static void remove_descriptors(struct xircom_private *card)
622{
623 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 val = 0;
626 outl(val, card->io_port + CSR3); /* Receive descriptor address */
627 outl(val, card->io_port + CSR4); /* Send descriptor address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630/*
631link_status_changed returns 1 if the card has indicated that
632the link status has changed. The new link status has to be read from CSR12.
633
634This function also clears the status-bit.
635*/
636static int link_status_changed(struct xircom_private *card)
637{
638 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 val = inl(card->io_port + CSR5); /* Status register */
641
Joe Perches54668b82011-05-09 09:45:20 +0000642 if ((val & (1 << 27)) == 0) /* no change */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /* clear the event by writing a 1 to the bit in the
646 status register. */
647 val = (1 << 27);
648 outl(val, card->io_port + CSR5);
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return 1;
651}
652
653
654/*
655transmit_active returns 1 if the transmitter on the card is
656in a non-stopped state.
657*/
658static int transmit_active(struct xircom_private *card)
659{
660 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 val = inl(card->io_port + CSR5); /* Status register */
663
Joe Perches54668b82011-05-09 09:45:20 +0000664 if ((val & (7 << 20)) == 0) /* transmitter disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return 1;
668}
669
670/*
671receive_active returns 1 if the receiver on the card is
672in a non-stopped state.
673*/
674static int receive_active(struct xircom_private *card)
675{
676 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 val = inl(card->io_port + CSR5); /* Status register */
679
Joe Perches54668b82011-05-09 09:45:20 +0000680 if ((val & (7 << 17)) == 0) /* receiver disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 1;
684}
685
686/*
687activate_receiver enables the receiver on the card.
688Before being allowed to active the receiver, the receiver
689must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400690this code actually disables the receiver first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691receiver to become inactive, then it activates the receiver and then
692it waits for the receiver to be active.
693
694must be called with the lock held and interrupts disabled.
695*/
696static void activate_receiver(struct xircom_private *card)
697{
698 unsigned int val;
699 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 val = inl(card->io_port + CSR6); /* Operation mode */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /* If the "active" bit is set and the receiver is already
704 active, no need to do the expensive thing */
705 if ((val&2) && (receive_active(card)))
706 return;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400707
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 val = val & ~2; /* disable the receiver */
710 outl(val, card->io_port + CSR6);
711
712 counter = 10;
713 while (counter > 0) {
714 if (!receive_active(card))
715 break;
716 /* wait a while */
717 udelay(50);
718 counter--;
719 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000720 netdev_err(card->dev, "Receiver failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722
723 /* enable the receiver */
724 val = inl(card->io_port + CSR6); /* Operation mode */
725 val = val | 2; /* enable the receiver */
726 outl(val, card->io_port + CSR6);
727
728 /* now wait for the card to activate again */
729 counter = 10;
730 while (counter > 0) {
731 if (receive_active(card))
732 break;
733 /* wait a while */
734 udelay(50);
735 counter--;
736 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000737 netdev_err(card->dev,
738 "Receiver failed to re-activate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742/*
743deactivate_receiver disables the receiver on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400744To achieve this this code disables the receiver first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745then it waits for the receiver to become inactive.
746
747must be called with the lock held and interrupts disabled.
748*/
749static void deactivate_receiver(struct xircom_private *card)
750{
751 unsigned int val;
752 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 val = inl(card->io_port + CSR6); /* Operation mode */
755 val = val & ~2; /* disable the receiver */
756 outl(val, card->io_port + CSR6);
757
758 counter = 10;
759 while (counter > 0) {
760 if (!receive_active(card))
761 break;
762 /* wait a while */
763 udelay(50);
764 counter--;
765 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000766 netdev_err(card->dev, "Receiver failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770
771/*
772activate_transmitter enables the transmitter on the card.
773Before being allowed to active the transmitter, the transmitter
774must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400775this code actually disables the transmitter first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776transmitter to become inactive, then it activates the transmitter and then
777it waits for the transmitter to be active again.
778
779must be called with the lock held and interrupts disabled.
780*/
781static void activate_transmitter(struct xircom_private *card)
782{
783 unsigned int val;
784 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 val = inl(card->io_port + CSR6); /* Operation mode */
787
788 /* If the "active" bit is set and the receiver is already
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400789 active, no need to do the expensive thing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if ((val&(1<<13)) && (transmit_active(card)))
791 return;
792
793 val = val & ~(1 << 13); /* disable the transmitter */
794 outl(val, card->io_port + CSR6);
795
796 counter = 10;
797 while (counter > 0) {
798 if (!transmit_active(card))
799 break;
800 /* wait a while */
801 udelay(50);
802 counter--;
803 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000804 netdev_err(card->dev,
805 "Transmitter failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
808 /* enable the transmitter */
809 val = inl(card->io_port + CSR6); /* Operation mode */
810 val = val | (1 << 13); /* enable the transmitter */
811 outl(val, card->io_port + CSR6);
812
813 /* now wait for the card to activate again */
814 counter = 10;
815 while (counter > 0) {
816 if (transmit_active(card))
817 break;
818 /* wait a while */
819 udelay(50);
820 counter--;
821 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000822 netdev_err(card->dev,
823 "Transmitter failed to re-activate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
827/*
828deactivate_transmitter disables the transmitter on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400829To achieve this this code disables the transmitter first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830then it waits for the transmitter to become inactive.
831
832must be called with the lock held and interrupts disabled.
833*/
834static void deactivate_transmitter(struct xircom_private *card)
835{
836 unsigned int val;
837 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 val = inl(card->io_port + CSR6); /* Operation mode */
840 val = val & ~2; /* disable the transmitter */
841 outl(val, card->io_port + CSR6);
842
843 counter = 20;
844 while (counter > 0) {
845 if (!transmit_active(card))
846 break;
847 /* wait a while */
848 udelay(50);
849 counter--;
850 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000851 netdev_err(card->dev,
852 "Transmitter failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
856
857/*
858enable_transmit_interrupt enables the transmit interrupt
859
860must be called with the lock held and interrupts disabled.
861*/
862static void enable_transmit_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 |= 1; /* enable the transmit interrupt */
868 outl(val, card->io_port + CSR7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871
872/*
873enable_receive_interrupt enables the receive interrupt
874
875must be called with the lock held and interrupts disabled.
876*/
877static void enable_receive_interrupt(struct xircom_private *card)
878{
879 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 val = inl(card->io_port + CSR7); /* Interrupt enable register */
882 val = val | (1 << 6); /* enable the receive interrupt */
883 outl(val, card->io_port + CSR7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886/*
887enable_link_interrupt enables the link status change interrupt
888
889must be called with the lock held and interrupts disabled.
890*/
891static void enable_link_interrupt(struct xircom_private *card)
892{
893 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 val = inl(card->io_port + CSR7); /* Interrupt enable register */
896 val = val | (1 << 27); /* enable the link status chage interrupt */
897 outl(val, card->io_port + CSR7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900
901
902/*
903disable_all_interrupts disables all interrupts
904
905must be called with the lock held and interrupts disabled.
906*/
907static void disable_all_interrupts(struct xircom_private *card)
908{
909 unsigned int val;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 val = 0; /* disable all interrupts */
912 outl(val, card->io_port + CSR7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915/*
916enable_common_interrupts enables several weird interrupts
917
918must be called with the lock held and interrupts disabled.
919*/
920static void enable_common_interrupts(struct xircom_private *card)
921{
922 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 val = inl(card->io_port + CSR7); /* Interrupt enable register */
925 val |= (1<<16); /* Normal Interrupt Summary */
926 val |= (1<<15); /* Abnormal Interrupt Summary */
927 val |= (1<<13); /* Fatal bus error */
928 val |= (1<<8); /* Receive Process Stopped */
929 val |= (1<<7); /* Receive Buffer Unavailable */
930 val |= (1<<5); /* Transmit Underflow */
931 val |= (1<<2); /* Transmit Buffer Unavailable */
932 val |= (1<<1); /* Transmit Process Stopped */
933 outl(val, card->io_port + CSR7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936/*
937enable_promisc starts promisc mode
938
939must be called with the lock held and interrupts disabled.
940*/
941static int enable_promisc(struct xircom_private *card)
942{
943 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400945 val = inl(card->io_port + CSR6);
946 val = val | (1 << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 outl(val, card->io_port + CSR6);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return 1;
950}
951
952
953
954
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400955/*
Michael Opdenacker59c51592007-05-09 08:57:56 +0200956link_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 -0700957
958Must be called in locked state with interrupts disabled
959*/
960static int link_status(struct xircom_private *card)
961{
962 unsigned int val;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 val = inb(card->io_port + CSR12);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (!(val&(1<<2))) /* bit 2 is 0 for 10mbit link, 1 for not an 10mbit link */
967 return 10;
968 if (!(val&(1<<1))) /* bit 1 is 0 for 100mbit link, 1 for not an 100mbit link */
969 return 100;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400970
971 /* If we get here -> no link at all */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return 0;
974}
975
976
977
978
979
980/*
981 read_mac_address() reads the MAC address from the NIC and stores it in the "dev" structure.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 This function will take the spinlock itself and can, as a result, not be called with the lock helt.
984 */
985static void read_mac_address(struct xircom_private *card)
986{
987 unsigned char j, tuple, link, data_id, data_count;
988 unsigned long flags;
989 int i;
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 spin_lock_irqsave(&card->lock, flags);
992
993 outl(1 << 12, card->io_port + CSR9); /* enable boot rom access */
994 for (i = 0x100; i < 0x1f7; i += link + 2) {
995 outl(i, card->io_port + CSR10);
996 tuple = inl(card->io_port + CSR9) & 0xff;
997 outl(i + 1, card->io_port + CSR10);
998 link = inl(card->io_port + CSR9) & 0xff;
999 outl(i + 2, card->io_port + CSR10);
1000 data_id = inl(card->io_port + CSR9) & 0xff;
1001 outl(i + 3, card->io_port + CSR10);
1002 data_count = inl(card->io_port + CSR9) & 0xff;
1003 if ((tuple == 0x22) && (data_id == 0x04) && (data_count == 0x06)) {
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001004 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 * This is it. We have the data we want.
1006 */
1007 for (j = 0; j < 6; j++) {
1008 outl(i + j + 4, card->io_port + CSR10);
1009 card->dev->dev_addr[j] = inl(card->io_port + CSR9) & 0xff;
1010 }
1011 break;
1012 } else if (link == 0) {
1013 break;
1014 }
1015 }
1016 spin_unlock_irqrestore(&card->lock, flags);
Johannes Berge1749612008-10-27 15:59:26 -07001017 pr_debug(" %pM\n", card->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
1020
1021/*
1022 transceiver_voodoo() enables the external UTP plug thingy.
1023 it's called voodoo as I stole this code and cannot cross-reference
1024 it with the specification.
1025 */
1026static void transceiver_voodoo(struct xircom_private *card)
1027{
1028 unsigned long flags;
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /* disable all powermanagement */
1031 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1032
1033 setup_descriptors(card);
1034
1035 spin_lock_irqsave(&card->lock, flags);
1036
1037 outl(0x0008, card->io_port + CSR15);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001038 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 outl(0xa8050000, card->io_port + CSR15);
1040 udelay(25);
1041 outl(0xa00f0000, card->io_port + CSR15);
1042 udelay(25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 spin_unlock_irqrestore(&card->lock, flags);
1045
1046 netif_start_queue(card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
1049
1050static void xircom_up(struct xircom_private *card)
1051{
1052 unsigned long flags;
1053 int i;
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* disable all powermanagement */
1056 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1057
1058 setup_descriptors(card);
1059
1060 spin_lock_irqsave(&card->lock, flags);
1061
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 enable_link_interrupt(card);
1064 enable_transmit_interrupt(card);
1065 enable_receive_interrupt(card);
1066 enable_common_interrupts(card);
1067 enable_promisc(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /* The card can have received packets already, read them away now */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001070 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 investigate_read_descriptor(card->dev,card,i,bufferoffsets[i]);
1072
1073
1074 spin_unlock_irqrestore(&card->lock, flags);
1075 trigger_receive(card);
1076 trigger_transmit(card);
1077 netif_start_queue(card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
1080/* Bufferoffset is in BYTES */
Joe Perches163ef0b2011-05-09 09:45:21 +00001081static void
1082investigate_read_descriptor(struct net_device *dev, struct xircom_private *card,
1083 int descnr, unsigned int bufferoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Joe Perches163ef0b2011-05-09 09:45:21 +00001085 int status;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001086
Joe Perches163ef0b2011-05-09 09:45:21 +00001087 status = le32_to_cpu(card->rx_buffer[4*descnr]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001088
Joe Perches163ef0b2011-05-09 09:45:21 +00001089 if (status > 0) { /* packet received */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001090
Joe Perches163ef0b2011-05-09 09:45:21 +00001091 /* TODO: discard error packets */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001092
Joe Perches163ef0b2011-05-09 09:45:21 +00001093 short pkt_len = ((status >> 16) & 0x7ff) - 4;
1094 /* minus 4, we don't want the CRC */
1095 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Joe Perches163ef0b2011-05-09 09:45:21 +00001097 if (pkt_len > 1518) {
1098 netdev_err(dev, "Packet length %i is bogus\n", pkt_len);
1099 pkt_len = 1518;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Joe Perches163ef0b2011-05-09 09:45:21 +00001101
Pradeep A Dalvi21a4e462012-02-05 02:50:10 +00001102 skb = netdev_alloc_skb(dev, pkt_len + 2);
Joe Perches163ef0b2011-05-09 09:45:21 +00001103 if (skb == NULL) {
1104 dev->stats.rx_dropped++;
1105 goto out;
1106 }
1107 skb_reserve(skb, 2);
1108 skb_copy_to_linear_data(skb,
1109 &card->rx_buffer[bufferoffset / 4],
1110 pkt_len);
1111 skb_put(skb, pkt_len);
1112 skb->protocol = eth_type_trans(skb, dev);
1113 netif_rx(skb);
1114 dev->stats.rx_packets++;
1115 dev->stats.rx_bytes += pkt_len;
1116
1117out:
1118 /* give the buffer back to the card */
1119 card->rx_buffer[4*descnr] = cpu_to_le32(0x80000000);
1120 trigger_receive(card);
1121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
1124
1125/* Bufferoffset is in BYTES */
Joe Perches163ef0b2011-05-09 09:45:21 +00001126static void
1127investigate_write_descriptor(struct net_device *dev,
1128 struct xircom_private *card,
1129 int descnr, unsigned int bufferoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Joe Perches163ef0b2011-05-09 09:45:21 +00001131 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Joe Perches163ef0b2011-05-09 09:45:21 +00001133 status = le32_to_cpu(card->tx_buffer[4*descnr]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001134#if 0
Joe Perches163ef0b2011-05-09 09:45:21 +00001135 if (status & 0x8000) { /* Major error */
1136 pr_err("Major transmit error status %x\n", status);
1137 card->tx_buffer[4*descnr] = 0;
1138 netif_wake_queue (dev);
1139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140#endif
Joe Perches163ef0b2011-05-09 09:45:21 +00001141 if (status > 0) { /* bit 31 is 0 when done */
1142 if (card->tx_skb[descnr]!=NULL) {
1143 dev->stats.tx_bytes += card->tx_skb[descnr]->len;
1144 dev_kfree_skb_irq(card->tx_skb[descnr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
Joe Perches163ef0b2011-05-09 09:45:21 +00001146 card->tx_skb[descnr] = NULL;
1147 /* Bit 8 in the status field is 1 if there was a collision */
1148 if (status & (1 << 8))
1149 dev->stats.collisions++;
1150 card->tx_buffer[4*descnr] = 0; /* descriptor is free again */
1151 netif_wake_queue (dev);
1152 dev->stats.tx_packets++;
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156static int __init xircom_init(void)
1157{
Alexey Dobriyanec42cdb2006-08-14 23:00:28 -07001158 return pci_register_driver(&xircom_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
1161static void __exit xircom_exit(void)
1162{
1163 pci_unregister_driver(&xircom_ops);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001164}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001166module_init(xircom_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167module_exit(xircom_exit)
1168