blob: cdbcd16431411bf48ad3cf459c8cb12f3adbf537 [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
Francois Romieuebaf7f82012-03-13 09:27:47 +010044#define xw32(reg, val) iowrite32(val, ioaddr + (reg))
45#define xr32(reg) ioread32(ioaddr + (reg))
46#define xr8(reg) ioread8(ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/* IO registers on the card, offsets */
49#define CSR0 0x00
50#define CSR1 0x08
51#define CSR2 0x10
52#define CSR3 0x18
53#define CSR4 0x20
54#define CSR5 0x28
55#define CSR6 0x30
56#define CSR7 0x38
57#define CSR8 0x40
58#define CSR9 0x48
59#define CSR10 0x50
60#define CSR11 0x58
61#define CSR12 0x60
62#define CSR13 0x68
63#define CSR14 0x70
64#define CSR15 0x78
65#define CSR16 0x80
66
67/* PCI registers */
68#define PCI_POWERMGMT 0x40
69
70/* Offsets of the buffers within the descriptor pages, in bytes */
71
72#define NUMDESCRIPTORS 4
73
74static int bufferoffsets[NUMDESCRIPTORS] = {128,2048,4096,6144};
75
76
77struct xircom_private {
78 /* Send and receive buffers, kernel-addressable and dma addressable forms */
79
Al Viro6f35d5d2007-12-23 20:01:04 +000080 __le32 *rx_buffer;
81 __le32 *tx_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 dma_addr_t rx_dma_handle;
84 dma_addr_t tx_dma_handle;
85
86 struct sk_buff *tx_skb[4];
87
Francois Romieuebaf7f82012-03-13 09:27:47 +010088 void __iomem *ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 int open;
Jeff Garzikf3b197a2006-05-26 21:39:03 -040090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 /* transmit_used is the rotating counter that indicates which transmit
92 descriptor has to be used next */
93 int transmit_used;
94
95 /* Spinlock to serialize register operations.
96 It must be helt while manipulating the following registers:
97 CSR0, CSR6, CSR7, CSR9, CSR10, CSR15
98 */
99 spinlock_t lock;
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct pci_dev *pdev;
102 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105
106/* Function prototypes */
107static int xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id);
108static void xircom_remove(struct pci_dev *pdev);
David Howells7d12e782006-10-05 14:55:46 +0100109static irqreturn_t xircom_interrupt(int irq, void *dev_instance);
Stephen Hemmingerad096462009-08-31 19:50:53 +0000110static netdev_tx_t xircom_start_xmit(struct sk_buff *skb,
111 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static int xircom_open(struct net_device *dev);
113static int xircom_close(struct net_device *dev);
114static void xircom_up(struct xircom_private *card);
Keith Owens3be034b2005-09-13 15:05:13 +1000115#ifdef CONFIG_NET_POLL_CONTROLLER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static void xircom_poll_controller(struct net_device *dev);
117#endif
118
119static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset);
120static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset);
121static void read_mac_address(struct xircom_private *card);
122static void transceiver_voodoo(struct xircom_private *card);
123static void initialize_card(struct xircom_private *card);
124static void trigger_transmit(struct xircom_private *card);
125static void trigger_receive(struct xircom_private *card);
126static void setup_descriptors(struct xircom_private *card);
127static void remove_descriptors(struct xircom_private *card);
128static int link_status_changed(struct xircom_private *card);
129static void activate_receiver(struct xircom_private *card);
130static void deactivate_receiver(struct xircom_private *card);
131static void activate_transmitter(struct xircom_private *card);
132static void deactivate_transmitter(struct xircom_private *card);
133static void enable_transmit_interrupt(struct xircom_private *card);
134static void enable_receive_interrupt(struct xircom_private *card);
135static void enable_link_interrupt(struct xircom_private *card);
136static void disable_all_interrupts(struct xircom_private *card);
137static int link_status(struct xircom_private *card);
138
139
140
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000141static DEFINE_PCI_DEVICE_TABLE(xircom_pci_table) = {
Francois Romieuebaf7f82012-03-13 09:27:47 +0100142 { PCI_VDEVICE(XIRCOM, 0x0003), },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 {0,},
144};
145MODULE_DEVICE_TABLE(pci, xircom_pci_table);
146
147static struct pci_driver xircom_ops = {
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400148 .name = "xircom_cb",
149 .id_table = xircom_pci_table,
150 .probe = xircom_probe,
Bill Pemberton779c1a82012-12-03 09:23:41 -0500151 .remove = xircom_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};
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 */
Bill Pemberton779c1a82012-12-03 09:23:41 -0500193static int xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
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);
Joe Perchesd0320f72013-03-14 13:07:21 +0000239 if (private->rx_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 goto rx_buf_fail;
Joe Perchesd0320f72013-03-14 13:07:21 +0000241
Francois Romieud59a1882012-03-11 23:34:54 +0100242 private->tx_buffer = dma_alloc_coherent(d, 8192,
243 &private->tx_dma_handle,
244 GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +0000245 if (private->tx_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 goto tx_buf_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 SET_NETDEV_DEV(dev, &pdev->dev);
249
250
251 private->dev = dev;
252 private->pdev = pdev;
Francois Romieuebaf7f82012-03-13 09:27:47 +0100253
254 /* IO range. */
255 private->ioaddr = pci_iomap(pdev, 0, 0);
256 if (!private->ioaddr)
257 goto reg_fail;
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 spin_lock_init(&private->lock);
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__);
Francois Romieuebaf7f82012-03-13 09:27:47 +0100271 goto err_unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
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
Francois Romieuebaf7f82012-03-13 09:27:47 +0100289err_unmap:
290 pci_iounmap(pdev, private->ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291reg_fail:
Francois Romieud59a1882012-03-11 23:34:54 +0100292 pci_set_drvdata(pdev, NULL);
293 dma_free_coherent(d, 8192, private->tx_buffer, private->tx_dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294tx_buf_fail:
Francois Romieud59a1882012-03-11 23:34:54 +0100295 dma_free_coherent(d, 8192, private->rx_buffer, private->rx_dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296rx_buf_fail:
297 free_netdev(dev);
Francois Romieud59a1882012-03-11 23:34:54 +0100298err_release:
299 pci_release_regions(pdev);
300err_disable:
301 pci_disable_device(pdev);
302 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305
306/*
307 xircom_remove is called on module-unload or on device-eject.
308 it unregisters the irq, io-region and network device.
309 Interrupts and such are already stopped in the "ifconfig ethX down"
310 code.
311 */
Bill Pemberton779c1a82012-12-03 09:23:41 -0500312static void xircom_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 struct net_device *dev = pci_get_drvdata(pdev);
315 struct xircom_private *card = netdev_priv(dev);
Francois Romieud59a1882012-03-11 23:34:54 +0100316 struct device *d = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 unregister_netdev(dev);
Francois Romieuebaf7f82012-03-13 09:27:47 +0100319 pci_iounmap(pdev, card->ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 pci_set_drvdata(pdev, NULL);
Francois Romieud59a1882012-03-11 23:34:54 +0100321 dma_free_coherent(d, 8192, card->tx_buffer, card->tx_dma_handle);
322 dma_free_coherent(d, 8192, card->rx_buffer, card->rx_dma_handle);
323 free_netdev(dev);
324 pci_release_regions(pdev);
325 pci_disable_device(pdev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400326}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
David Howells7d12e782006-10-05 14:55:46 +0100328static irqreturn_t xircom_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 struct net_device *dev = (struct net_device *) dev_instance;
331 struct xircom_private *card = netdev_priv(dev);
Francois Romieuebaf7f82012-03-13 09:27:47 +0100332 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 unsigned int status;
334 int i;
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 spin_lock(&card->lock);
Francois Romieuebaf7f82012-03-13 09:27:47 +0100337 status = xr32(CSR5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Joe Perches54668b82011-05-09 09:45:20 +0000339#if defined DEBUG && DEBUG > 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 print_binary(status);
Joe Perches54668b82011-05-09 09:45:20 +0000341 pr_debug("tx status 0x%08x 0x%08x\n",
342 card->tx_buffer[0], card->tx_buffer[4]);
343 pr_debug("rx status 0x%08x 0x%08x\n",
344 card->rx_buffer[0], card->rx_buffer[4]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400345#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* Handle shared irq and hotplug */
347 if (status == 0 || status == 0xffffffff) {
348 spin_unlock(&card->lock);
349 return IRQ_NONE;
350 }
351
352 if (link_status_changed(card)) {
353 int newlink;
Joe Perches726b65a2011-05-09 09:45:22 +0000354 netdev_dbg(dev, "Link status has changed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 newlink = link_status(card);
Joe Perches163ef0b2011-05-09 09:45:21 +0000356 netdev_info(dev, "Link is %d mbit\n", newlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (newlink)
358 netif_carrier_on(dev);
359 else
360 netif_carrier_off(dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400364 /* Clear all remaining interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 status |= 0xffffffff; /* FIXME: make this clear only the
366 real existing bits */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100367 xw32(CSR5, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400369
370 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 investigate_write_descriptor(dev,card,i,bufferoffsets[i]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400372 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 investigate_read_descriptor(dev,card,i,bufferoffsets[i]);
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 spin_unlock(&card->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return IRQ_HANDLED;
377}
378
Stephen Hemmingerad096462009-08-31 19:50:53 +0000379static netdev_tx_t xircom_start_xmit(struct sk_buff *skb,
380 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct xircom_private *card;
383 unsigned long flags;
384 int nextdescriptor;
385 int desc;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 card = netdev_priv(dev);
388 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* First see if we can free some descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400391 for (desc=0;desc<NUMDESCRIPTORS;desc++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 investigate_write_descriptor(dev,card,desc,bufferoffsets[desc]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400393
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 nextdescriptor = (card->transmit_used +1) % (NUMDESCRIPTORS);
396 desc = card->transmit_used;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* only send the packet if the descriptor is free */
399 if (card->tx_buffer[4*desc]==0) {
400 /* Copy the packet data; zero the memory first as the card
401 sometimes sends more than you ask it to. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 memset(&card->tx_buffer[bufferoffsets[desc]/4],0,1536);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300404 skb_copy_from_linear_data(skb,
405 &(card->tx_buffer[bufferoffsets[desc] / 4]),
406 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /* FIXME: The specification tells us that the length we send HAS to be a multiple of
408 4 bytes. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400409
Al Viro6f35d5d2007-12-23 20:01:04 +0000410 card->tx_buffer[4*desc+1] = cpu_to_le32(skb->len);
411 if (desc == NUMDESCRIPTORS - 1) /* bit 25: last descriptor of the ring */
412 card->tx_buffer[4*desc+1] |= cpu_to_le32(1<<25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Al Viro6f35d5d2007-12-23 20:01:04 +0000414 card->tx_buffer[4*desc+1] |= cpu_to_le32(0xF0000000);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400415 /* 0xF0... means want interrupts*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 card->tx_skb[desc] = skb;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 wmb();
419 /* This gives the descriptor to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000420 card->tx_buffer[4*desc] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 trigger_transmit(card);
Al Viro6f35d5d2007-12-23 20:01:04 +0000422 if (card->tx_buffer[nextdescriptor*4] & cpu_to_le32(0x8000000)) {
423 /* next descriptor is occupied... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 netif_stop_queue(dev);
425 }
426 card->transmit_used = nextdescriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 spin_unlock_irqrestore(&card->lock,flags);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000428 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* Uh oh... no free descriptor... drop the packet */
432 netif_stop_queue(dev);
433 spin_unlock_irqrestore(&card->lock,flags);
434 trigger_transmit(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400435
Erik Mouw06f75252008-02-04 18:56:54 +0100436 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439
440
441
442static int xircom_open(struct net_device *dev)
443{
444 struct xircom_private *xp = netdev_priv(dev);
Francois Romieuebaf7f82012-03-13 09:27:47 +0100445 const int irq = xp->pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 int retval;
Joe Perches54668b82011-05-09 09:45:20 +0000447
Francois Romieuebaf7f82012-03-13 09:27:47 +0100448 netdev_info(dev, "xircom cardbus adaptor found, using irq %i\n", irq);
449 retval = request_irq(irq, xircom_interrupt, IRQF_SHARED, dev->name, dev);
Joe Perches54668b82011-05-09 09:45:20 +0000450 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return retval;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 xircom_up(xp);
454 xp->open = 1;
Joe Perches54668b82011-05-09 09:45:20 +0000455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return 0;
457}
458
459static int xircom_close(struct net_device *dev)
460{
461 struct xircom_private *card;
462 unsigned long flags;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 card = netdev_priv(dev);
465 netif_stop_queue(dev); /* we don't want new packets */
466
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 disable_all_interrupts(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400471#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* We can enable this again once we send dummy packets on ifconfig ethX up */
473 deactivate_receiver(card);
474 deactivate_transmitter(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400475#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 remove_descriptors(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 spin_unlock_irqrestore(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 card->open = 0;
Francois Romieuebaf7f82012-03-13 09:27:47 +0100481 free_irq(card->pdev->irq, dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return 0;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488#ifdef CONFIG_NET_POLL_CONTROLLER
489static void xircom_poll_controller(struct net_device *dev)
490{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100491 struct xircom_private *xp = netdev_priv(dev);
492 const int irq = xp->pdev->irq;
493
494 disable_irq(irq);
495 xircom_interrupt(irq, dev);
496 enable_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498#endif
499
500
501static void initialize_card(struct xircom_private *card)
502{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100503 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 unsigned long flags;
Francois Romieuebaf7f82012-03-13 09:27:47 +0100505 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 spin_lock_irqsave(&card->lock, flags);
508
509 /* First: reset the card */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100510 val = xr32(CSR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 val |= 0x01; /* Software reset */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100512 xw32(CSR0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 udelay(100); /* give the card some time to reset */
515
Francois Romieuebaf7f82012-03-13 09:27:47 +0100516 val = xr32(CSR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 val &= ~0x01; /* disable Software reset */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100518 xw32(CSR0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400521 val = 0; /* Value 0x00 is a safe and conservative value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 for the PCI configuration settings */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100523 xw32(CSR0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525
526 disable_all_interrupts(card);
527 deactivate_receiver(card);
528 deactivate_transmitter(card);
529
530 spin_unlock_irqrestore(&card->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533/*
534trigger_transmit causes the card to check for frames to be transmitted.
535This is accomplished by writing to the CSR1 port. The documentation
536claims that the act of writing is sufficient and that the value is
537ignored; I chose zero.
538*/
539static void trigger_transmit(struct xircom_private *card)
540{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100541 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Francois Romieuebaf7f82012-03-13 09:27:47 +0100543 xw32(CSR1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
546/*
547trigger_receive causes the card to check for empty frames in the
548descriptor list in which packets can be received.
549This is accomplished by writing to the CSR2 port. The documentation
550claims that the act of writing is sufficient and that the value is
551ignored; I chose zero.
552*/
553static void trigger_receive(struct xircom_private *card)
554{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100555 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Francois Romieuebaf7f82012-03-13 09:27:47 +0100557 xw32(CSR2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560/*
561setup_descriptors initializes the send and receive buffers to be valid
562descriptors and programs the addresses into the card.
563*/
564static void setup_descriptors(struct xircom_private *card)
565{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100566 void __iomem *ioaddr = card->ioaddr;
Al Viro6f35d5d2007-12-23 20:01:04 +0000567 u32 address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Eric Sesterhenn / snakebytea707cd62006-01-26 22:02:51 +0100570 BUG_ON(card->rx_buffer == NULL);
571 BUG_ON(card->tx_buffer == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /* Receive descriptors */
574 memset(card->rx_buffer, 0, 128); /* clear the descriptors */
575 for (i=0;i<NUMDESCRIPTORS;i++ ) {
576
577 /* Rx Descr0: It's empty, let the card own it, no errors -> 0x80000000 */
Al Viro6f35d5d2007-12-23 20:01:04 +0000578 card->rx_buffer[i*4 + 0] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* Rx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
Al Viro6f35d5d2007-12-23 20:01:04 +0000580 card->rx_buffer[i*4 + 1] = cpu_to_le32(1536);
581 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
582 card->rx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 /* Rx Descr2: address of the buffer
585 we store the buffer at the 2nd half of the page */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400586
Al Viro6f35d5d2007-12-23 20:01:04 +0000587 address = card->rx_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 card->rx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
589 /* Rx Desc3: address of 2nd buffer -> 0 */
590 card->rx_buffer[i*4 + 3] = 0;
591 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 wmb();
594 /* Write the receive descriptor ring address to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000595 address = card->rx_dma_handle;
Francois Romieuebaf7f82012-03-13 09:27:47 +0100596 xw32(CSR3, address); /* Receive descr list address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598
599 /* transmit descriptors */
600 memset(card->tx_buffer, 0, 128); /* clear the descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 for (i=0;i<NUMDESCRIPTORS;i++ ) {
603 /* Tx Descr0: Empty, we own it, no errors -> 0x00000000 */
604 card->tx_buffer[i*4 + 0] = 0x00000000;
605 /* Tx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
Al Viro6f35d5d2007-12-23 20:01:04 +0000606 card->tx_buffer[i*4 + 1] = cpu_to_le32(1536);
607 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
608 card->tx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /* Tx Descr2: address of the buffer
611 we store the buffer at the 2nd half of the page */
Al Viro6f35d5d2007-12-23 20:01:04 +0000612 address = card->tx_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 card->tx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
614 /* Tx Desc3: address of 2nd buffer -> 0 */
615 card->tx_buffer[i*4 + 3] = 0;
616 }
617
618 wmb();
619 /* wite the transmit descriptor ring to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000620 address = card->tx_dma_handle;
Francois Romieuebaf7f82012-03-13 09:27:47 +0100621 xw32(CSR4, address); /* xmit descr list address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624/*
625remove_descriptors informs the card the descriptors are no longer
626valid by setting the address in the card to 0x00.
627*/
628static void remove_descriptors(struct xircom_private *card)
629{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100630 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 val = 0;
Francois Romieuebaf7f82012-03-13 09:27:47 +0100634 xw32(CSR3, val); /* Receive descriptor address */
635 xw32(CSR4, val); /* Send descriptor address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638/*
639link_status_changed returns 1 if the card has indicated that
640the link status has changed. The new link status has to be read from CSR12.
641
642This function also clears the status-bit.
643*/
644static int link_status_changed(struct xircom_private *card)
645{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100646 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Francois Romieuebaf7f82012-03-13 09:27:47 +0100649 val = xr32(CSR5); /* Status register */
650 if (!(val & (1 << 27))) /* no change */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 /* clear the event by writing a 1 to the bit in the
654 status register. */
655 val = (1 << 27);
Francois Romieuebaf7f82012-03-13 09:27:47 +0100656 xw32(CSR5, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return 1;
659}
660
661
662/*
663transmit_active returns 1 if the transmitter on the card is
664in a non-stopped state.
665*/
666static int transmit_active(struct xircom_private *card)
667{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100668 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Francois Romieuebaf7f82012-03-13 09:27:47 +0100670 if (!(xr32(CSR5) & (7 << 20))) /* transmitter disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return 1;
674}
675
676/*
677receive_active returns 1 if the receiver on the card is
678in a non-stopped state.
679*/
680static int receive_active(struct xircom_private *card)
681{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100682 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Francois Romieuebaf7f82012-03-13 09:27:47 +0100684 if (!(xr32(CSR5) & (7 << 17))) /* receiver disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return 1;
688}
689
690/*
691activate_receiver enables the receiver on the card.
692Before being allowed to active the receiver, the receiver
693must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400694this code actually disables the receiver first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695receiver to become inactive, then it activates the receiver and then
696it waits for the receiver to be active.
697
698must be called with the lock held and interrupts disabled.
699*/
700static void activate_receiver(struct xircom_private *card)
701{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100702 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 unsigned int val;
704 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Francois Romieuebaf7f82012-03-13 09:27:47 +0100706 val = xr32(CSR6); /* Operation mode */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 /* If the "active" bit is set and the receiver is already
709 active, no need to do the expensive thing */
710 if ((val&2) && (receive_active(card)))
711 return;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400712
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 val = val & ~2; /* disable the receiver */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100715 xw32(CSR6, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 counter = 10;
718 while (counter > 0) {
719 if (!receive_active(card))
720 break;
721 /* wait a while */
722 udelay(50);
723 counter--;
724 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000725 netdev_err(card->dev, "Receiver failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
728 /* enable the receiver */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100729 val = xr32(CSR6); /* Operation mode */
730 val = val | 2; /* enable the receiver */
731 xw32(CSR6, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 /* now wait for the card to activate again */
734 counter = 10;
735 while (counter > 0) {
736 if (receive_active(card))
737 break;
738 /* wait a while */
739 udelay(50);
740 counter--;
741 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000742 netdev_err(card->dev,
743 "Receiver failed to re-activate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
747/*
748deactivate_receiver disables the receiver on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400749To achieve this this code disables the receiver first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750then it waits for the receiver to become inactive.
751
752must be called with the lock held and interrupts disabled.
753*/
754static void deactivate_receiver(struct xircom_private *card)
755{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100756 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 unsigned int val;
758 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Francois Romieuebaf7f82012-03-13 09:27:47 +0100760 val = xr32(CSR6); /* Operation mode */
761 val = val & ~2; /* disable the receiver */
762 xw32(CSR6, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 counter = 10;
765 while (counter > 0) {
766 if (!receive_active(card))
767 break;
768 /* wait a while */
769 udelay(50);
770 counter--;
771 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000772 netdev_err(card->dev, "Receiver failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
776
777/*
778activate_transmitter enables the transmitter on the card.
779Before being allowed to active the transmitter, the transmitter
780must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400781this code actually disables the transmitter first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782transmitter to become inactive, then it activates the transmitter and then
783it waits for the transmitter to be active again.
784
785must be called with the lock held and interrupts disabled.
786*/
787static void activate_transmitter(struct xircom_private *card)
788{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100789 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 unsigned int val;
791 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Francois Romieuebaf7f82012-03-13 09:27:47 +0100793 val = xr32(CSR6); /* Operation mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 /* If the "active" bit is set and the receiver is already
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400796 active, no need to do the expensive thing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if ((val&(1<<13)) && (transmit_active(card)))
798 return;
799
800 val = val & ~(1 << 13); /* disable the transmitter */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100801 xw32(CSR6, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 counter = 10;
804 while (counter > 0) {
805 if (!transmit_active(card))
806 break;
807 /* wait a while */
808 udelay(50);
809 counter--;
810 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000811 netdev_err(card->dev,
812 "Transmitter failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814
815 /* enable the transmitter */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100816 val = xr32(CSR6); /* Operation mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 val = val | (1 << 13); /* enable the transmitter */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100818 xw32(CSR6, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 /* now wait for the card to activate again */
821 counter = 10;
822 while (counter > 0) {
823 if (transmit_active(card))
824 break;
825 /* wait a while */
826 udelay(50);
827 counter--;
828 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000829 netdev_err(card->dev,
830 "Transmitter failed to re-activate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834/*
835deactivate_transmitter disables the transmitter on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400836To achieve this this code disables the transmitter first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837then it waits for the transmitter to become inactive.
838
839must be called with the lock held and interrupts disabled.
840*/
841static void deactivate_transmitter(struct xircom_private *card)
842{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100843 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 unsigned int val;
845 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Francois Romieuebaf7f82012-03-13 09:27:47 +0100847 val = xr32(CSR6); /* Operation mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 val = val & ~2; /* disable the transmitter */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100849 xw32(CSR6, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 counter = 20;
852 while (counter > 0) {
853 if (!transmit_active(card))
854 break;
855 /* wait a while */
856 udelay(50);
857 counter--;
858 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000859 netdev_err(card->dev,
860 "Transmitter failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
864
865/*
866enable_transmit_interrupt enables the transmit interrupt
867
868must be called with the lock held and interrupts disabled.
869*/
870static void enable_transmit_interrupt(struct xircom_private *card)
871{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100872 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Francois Romieuebaf7f82012-03-13 09:27:47 +0100875 val = xr32(CSR7); /* Interrupt enable register */
876 val |= 1; /* enable the transmit interrupt */
877 xw32(CSR7, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
880
881/*
882enable_receive_interrupt enables the receive interrupt
883
884must be called with the lock held and interrupts disabled.
885*/
886static void enable_receive_interrupt(struct xircom_private *card)
887{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100888 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Francois Romieuebaf7f82012-03-13 09:27:47 +0100891 val = xr32(CSR7); /* Interrupt enable register */
892 val = val | (1 << 6); /* enable the receive interrupt */
893 xw32(CSR7, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896/*
897enable_link_interrupt enables the link status change interrupt
898
899must be called with the lock held and interrupts disabled.
900*/
901static void enable_link_interrupt(struct xircom_private *card)
902{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100903 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Francois Romieuebaf7f82012-03-13 09:27:47 +0100906 val = xr32(CSR7); /* Interrupt enable register */
907 val = val | (1 << 27); /* enable the link status chage interrupt */
908 xw32(CSR7, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911
912
913/*
914disable_all_interrupts disables all interrupts
915
916must be called with the lock held and interrupts disabled.
917*/
918static void disable_all_interrupts(struct xircom_private *card)
919{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100920 void __iomem *ioaddr = card->ioaddr;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400921
Francois Romieuebaf7f82012-03-13 09:27:47 +0100922 xw32(CSR7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
925/*
926enable_common_interrupts enables several weird interrupts
927
928must be called with the lock held and interrupts disabled.
929*/
930static void enable_common_interrupts(struct xircom_private *card)
931{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100932 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Francois Romieuebaf7f82012-03-13 09:27:47 +0100935 val = xr32(CSR7); /* Interrupt enable register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 val |= (1<<16); /* Normal Interrupt Summary */
937 val |= (1<<15); /* Abnormal Interrupt Summary */
938 val |= (1<<13); /* Fatal bus error */
939 val |= (1<<8); /* Receive Process Stopped */
940 val |= (1<<7); /* Receive Buffer Unavailable */
941 val |= (1<<5); /* Transmit Underflow */
942 val |= (1<<2); /* Transmit Buffer Unavailable */
943 val |= (1<<1); /* Transmit Process Stopped */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100944 xw32(CSR7, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947/*
948enable_promisc starts promisc mode
949
950must be called with the lock held and interrupts disabled.
951*/
952static int enable_promisc(struct xircom_private *card)
953{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100954 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Francois Romieuebaf7f82012-03-13 09:27:47 +0100957 val = xr32(CSR6);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400958 val = val | (1 << 6);
Francois Romieuebaf7f82012-03-13 09:27:47 +0100959 xw32(CSR6, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return 1;
962}
963
964
965
966
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400967/*
Michael Opdenacker59c51592007-05-09 08:57:56 +0200968link_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 -0700969
970Must be called in locked state with interrupts disabled
971*/
972static int link_status(struct xircom_private *card)
973{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100974 void __iomem *ioaddr = card->ioaddr;
975 u8 val;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400976
Francois Romieuebaf7f82012-03-13 09:27:47 +0100977 val = xr8(CSR12);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400978
Francois Romieuebaf7f82012-03-13 09:27:47 +0100979 /* bit 2 is 0 for 10mbit link, 1 for not an 10mbit link */
980 if (!(val & (1 << 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return 10;
Francois Romieuebaf7f82012-03-13 09:27:47 +0100982 /* bit 1 is 0 for 100mbit link, 1 for not an 100mbit link */
983 if (!(val & (1 << 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return 100;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400985
986 /* If we get here -> no link at all */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return 0;
989}
990
991
992
993
994
995/*
996 read_mac_address() reads the MAC address from the NIC and stores it in the "dev" structure.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 This function will take the spinlock itself and can, as a result, not be called with the lock helt.
999 */
1000static void read_mac_address(struct xircom_private *card)
1001{
Francois Romieuebaf7f82012-03-13 09:27:47 +01001002 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 unsigned long flags;
Francois Romieuebaf7f82012-03-13 09:27:47 +01001004 u8 link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 int i;
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 spin_lock_irqsave(&card->lock, flags);
1008
Francois Romieuebaf7f82012-03-13 09:27:47 +01001009 xw32(CSR9, 1 << 12); /* enable boot rom access */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 for (i = 0x100; i < 0x1f7; i += link + 2) {
Francois Romieuebaf7f82012-03-13 09:27:47 +01001011 u8 tuple, data_id, data_count;
1012
1013 xw32(CSR10, i);
1014 tuple = xr32(CSR9);
1015 xw32(CSR10, i + 1);
1016 link = xr32(CSR9);
1017 xw32(CSR10, i + 2);
1018 data_id = xr32(CSR9);
1019 xw32(CSR10, i + 3);
1020 data_count = xr32(CSR9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if ((tuple == 0x22) && (data_id == 0x04) && (data_count == 0x06)) {
Francois Romieuebaf7f82012-03-13 09:27:47 +01001022 int j;
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 for (j = 0; j < 6; j++) {
Francois Romieuebaf7f82012-03-13 09:27:47 +01001025 xw32(CSR10, i + j + 4);
1026 card->dev->dev_addr[j] = xr32(CSR9) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028 break;
1029 } else if (link == 0) {
1030 break;
1031 }
1032 }
1033 spin_unlock_irqrestore(&card->lock, flags);
Johannes Berge1749612008-10-27 15:59:26 -07001034 pr_debug(" %pM\n", card->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037
1038/*
1039 transceiver_voodoo() enables the external UTP plug thingy.
1040 it's called voodoo as I stole this code and cannot cross-reference
1041 it with the specification.
1042 */
1043static void transceiver_voodoo(struct xircom_private *card)
1044{
Francois Romieuebaf7f82012-03-13 09:27:47 +01001045 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 unsigned long flags;
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /* disable all powermanagement */
1049 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1050
1051 setup_descriptors(card);
1052
1053 spin_lock_irqsave(&card->lock, flags);
1054
Francois Romieuebaf7f82012-03-13 09:27:47 +01001055 xw32(CSR15, 0x0008);
1056 udelay(25);
1057 xw32(CSR15, 0xa8050000);
1058 udelay(25);
1059 xw32(CSR15, 0xa00f0000);
1060 udelay(25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001061
Francois Romieuebaf7f82012-03-13 09:27:47 +01001062 spin_unlock_irqrestore(&card->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 netif_start_queue(card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
1067
1068static void xircom_up(struct xircom_private *card)
1069{
1070 unsigned long flags;
1071 int i;
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /* disable all powermanagement */
1074 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1075
1076 setup_descriptors(card);
1077
1078 spin_lock_irqsave(&card->lock, flags);
1079
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 enable_link_interrupt(card);
1082 enable_transmit_interrupt(card);
1083 enable_receive_interrupt(card);
1084 enable_common_interrupts(card);
1085 enable_promisc(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 /* The card can have received packets already, read them away now */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001088 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 investigate_read_descriptor(card->dev,card,i,bufferoffsets[i]);
1090
1091
1092 spin_unlock_irqrestore(&card->lock, flags);
1093 trigger_receive(card);
1094 trigger_transmit(card);
1095 netif_start_queue(card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
1098/* Bufferoffset is in BYTES */
Joe Perches163ef0b2011-05-09 09:45:21 +00001099static void
1100investigate_read_descriptor(struct net_device *dev, struct xircom_private *card,
1101 int descnr, unsigned int bufferoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Joe Perches163ef0b2011-05-09 09:45:21 +00001103 int status;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001104
Joe Perches163ef0b2011-05-09 09:45:21 +00001105 status = le32_to_cpu(card->rx_buffer[4*descnr]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001106
Joe Perches163ef0b2011-05-09 09:45:21 +00001107 if (status > 0) { /* packet received */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001108
Joe Perches163ef0b2011-05-09 09:45:21 +00001109 /* TODO: discard error packets */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001110
Joe Perches163ef0b2011-05-09 09:45:21 +00001111 short pkt_len = ((status >> 16) & 0x7ff) - 4;
1112 /* minus 4, we don't want the CRC */
1113 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Joe Perches163ef0b2011-05-09 09:45:21 +00001115 if (pkt_len > 1518) {
1116 netdev_err(dev, "Packet length %i is bogus\n", pkt_len);
1117 pkt_len = 1518;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
Joe Perches163ef0b2011-05-09 09:45:21 +00001119
Pradeep A Dalvi21a4e462012-02-05 02:50:10 +00001120 skb = netdev_alloc_skb(dev, pkt_len + 2);
Joe Perches163ef0b2011-05-09 09:45:21 +00001121 if (skb == NULL) {
1122 dev->stats.rx_dropped++;
1123 goto out;
1124 }
1125 skb_reserve(skb, 2);
1126 skb_copy_to_linear_data(skb,
1127 &card->rx_buffer[bufferoffset / 4],
1128 pkt_len);
1129 skb_put(skb, pkt_len);
1130 skb->protocol = eth_type_trans(skb, dev);
1131 netif_rx(skb);
1132 dev->stats.rx_packets++;
1133 dev->stats.rx_bytes += pkt_len;
1134
1135out:
1136 /* give the buffer back to the card */
1137 card->rx_buffer[4*descnr] = cpu_to_le32(0x80000000);
1138 trigger_receive(card);
1139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
1142
1143/* Bufferoffset is in BYTES */
Joe Perches163ef0b2011-05-09 09:45:21 +00001144static void
1145investigate_write_descriptor(struct net_device *dev,
1146 struct xircom_private *card,
1147 int descnr, unsigned int bufferoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Joe Perches163ef0b2011-05-09 09:45:21 +00001149 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Joe Perches163ef0b2011-05-09 09:45:21 +00001151 status = le32_to_cpu(card->tx_buffer[4*descnr]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001152#if 0
Joe Perches163ef0b2011-05-09 09:45:21 +00001153 if (status & 0x8000) { /* Major error */
1154 pr_err("Major transmit error status %x\n", status);
1155 card->tx_buffer[4*descnr] = 0;
1156 netif_wake_queue (dev);
1157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158#endif
Joe Perches163ef0b2011-05-09 09:45:21 +00001159 if (status > 0) { /* bit 31 is 0 when done */
1160 if (card->tx_skb[descnr]!=NULL) {
1161 dev->stats.tx_bytes += card->tx_skb[descnr]->len;
1162 dev_kfree_skb_irq(card->tx_skb[descnr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
Joe Perches163ef0b2011-05-09 09:45:21 +00001164 card->tx_skb[descnr] = NULL;
1165 /* Bit 8 in the status field is 1 if there was a collision */
1166 if (status & (1 << 8))
1167 dev->stats.collisions++;
1168 card->tx_buffer[4*descnr] = 0; /* descriptor is free again */
1169 netif_wake_queue (dev);
1170 dev->stats.tx_packets++;
1171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174static int __init xircom_init(void)
1175{
Alexey Dobriyanec42cdb2006-08-14 23:00:28 -07001176 return pci_register_driver(&xircom_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
1179static void __exit xircom_exit(void)
1180{
1181 pci_unregister_driver(&xircom_ops);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001182}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001184module_init(xircom_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185module_exit(xircom_exit)
1186