blob: 0e721cedfa67c1e1b60e3e9bf059c9660d6e78ce [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/bitops.h>
32
33#include <asm/uaccess.h>
34#include <asm/io.h>
Al Viro82729972005-12-06 05:53:04 -050035#ifdef CONFIG_NET_POLL_CONTROLLER
36#include <asm/irq.h>
37#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039MODULE_DESCRIPTION("Xircom Cardbus ethernet driver");
40MODULE_AUTHOR("Arjan van de Ven <arjanv@redhat.com>");
41MODULE_LICENSE("GPL");
42
Francois Romieuebaf7f82012-03-13 09:27:47 +010043#define xw32(reg, val) iowrite32(val, ioaddr + (reg))
44#define xr32(reg) ioread32(ioaddr + (reg))
45#define xr8(reg) ioread8(ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/* IO registers on the card, offsets */
48#define CSR0 0x00
49#define CSR1 0x08
50#define CSR2 0x10
51#define CSR3 0x18
52#define CSR4 0x20
53#define CSR5 0x28
54#define CSR6 0x30
55#define CSR7 0x38
56#define CSR8 0x40
57#define CSR9 0x48
58#define CSR10 0x50
59#define CSR11 0x58
60#define CSR12 0x60
61#define CSR13 0x68
62#define CSR14 0x70
63#define CSR15 0x78
64#define CSR16 0x80
65
66/* PCI registers */
67#define PCI_POWERMGMT 0x40
68
69/* Offsets of the buffers within the descriptor pages, in bytes */
70
71#define NUMDESCRIPTORS 4
72
73static int bufferoffsets[NUMDESCRIPTORS] = {128,2048,4096,6144};
74
75
76struct xircom_private {
77 /* Send and receive buffers, kernel-addressable and dma addressable forms */
78
Al Viro6f35d5d2007-12-23 20:01:04 +000079 __le32 *rx_buffer;
80 __le32 *tx_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 dma_addr_t rx_dma_handle;
83 dma_addr_t tx_dma_handle;
84
85 struct sk_buff *tx_skb[4];
86
Francois Romieuebaf7f82012-03-13 09:27:47 +010087 void __iomem *ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 int open;
Jeff Garzikf3b197a2006-05-26 21:39:03 -040089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 /* transmit_used is the rotating counter that indicates which transmit
91 descriptor has to be used next */
92 int transmit_used;
93
94 /* Spinlock to serialize register operations.
95 It must be helt while manipulating the following registers:
96 CSR0, CSR6, CSR7, CSR9, CSR10, CSR15
97 */
98 spinlock_t lock;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 struct pci_dev *pdev;
101 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
104
105/* Function prototypes */
106static int xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id);
107static void xircom_remove(struct pci_dev *pdev);
David Howells7d12e782006-10-05 14:55:46 +0100108static irqreturn_t xircom_interrupt(int irq, void *dev_instance);
Stephen Hemmingerad096462009-08-31 19:50:53 +0000109static netdev_tx_t xircom_start_xmit(struct sk_buff *skb,
110 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static int xircom_open(struct net_device *dev);
112static int xircom_close(struct net_device *dev);
113static void xircom_up(struct xircom_private *card);
Keith Owens3be034b2005-09-13 15:05:13 +1000114#ifdef CONFIG_NET_POLL_CONTROLLER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static void xircom_poll_controller(struct net_device *dev);
116#endif
117
118static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset);
119static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset);
120static void read_mac_address(struct xircom_private *card);
121static void transceiver_voodoo(struct xircom_private *card);
122static void initialize_card(struct xircom_private *card);
123static void trigger_transmit(struct xircom_private *card);
124static void trigger_receive(struct xircom_private *card);
125static void setup_descriptors(struct xircom_private *card);
126static void remove_descriptors(struct xircom_private *card);
127static int link_status_changed(struct xircom_private *card);
128static void activate_receiver(struct xircom_private *card);
129static void deactivate_receiver(struct xircom_private *card);
130static void activate_transmitter(struct xircom_private *card);
131static void deactivate_transmitter(struct xircom_private *card);
132static void enable_transmit_interrupt(struct xircom_private *card);
133static void enable_receive_interrupt(struct xircom_private *card);
134static void enable_link_interrupt(struct xircom_private *card);
135static void disable_all_interrupts(struct xircom_private *card);
136static int link_status(struct xircom_private *card);
137
138
139
Benoit Taine9baa3c32014-08-08 15:56:03 +0200140static const struct pci_device_id xircom_pci_table[] = {
Francois Romieuebaf7f82012-03-13 09:27:47 +0100141 { PCI_VDEVICE(XIRCOM, 0x0003), },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 {0,},
143};
144MODULE_DEVICE_TABLE(pci, xircom_pci_table);
145
146static struct pci_driver xircom_ops = {
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400147 .name = "xircom_cb",
148 .id_table = xircom_pci_table,
149 .probe = xircom_probe,
Bill Pemberton779c1a82012-12-03 09:23:41 -0500150 .remove = xircom_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
153
Joe Perches54668b82011-05-09 09:45:20 +0000154#if defined DEBUG && DEBUG > 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static void print_binary(unsigned int number)
156{
157 int i,i2;
158 char buffer[64];
159 memset(buffer,0,64);
160 i2=0;
161 for (i=31;i>=0;i--) {
162 if (number & (1<<i))
163 buffer[i2++]='1';
164 else
165 buffer[i2++]='0';
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400166 if ((i&3)==0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 buffer[i2++]=' ';
168 }
Joe Perches54668b82011-05-09 09:45:20 +0000169 pr_debug("%s\n",buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171#endif
172
Stephen Hemminger1034c9f2009-01-07 18:00:55 -0800173static const struct net_device_ops netdev_ops = {
174 .ndo_open = xircom_open,
175 .ndo_stop = xircom_close,
176 .ndo_start_xmit = xircom_start_xmit,
177 .ndo_change_mtu = eth_change_mtu,
178 .ndo_set_mac_address = eth_mac_addr,
179 .ndo_validate_addr = eth_validate_addr,
180#ifdef CONFIG_NET_POLL_CONTROLLER
181 .ndo_poll_controller = xircom_poll_controller,
182#endif
183};
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/* xircom_probe is the code that gets called on device insertion.
186 it sets up the hardware and registers the device to the networklayer.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 TODO: Send 1 or 2 "dummy" packets here as the card seems to discard the
189 first two packets that get send, and pump hates that.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 */
Bill Pemberton779c1a82012-12-03 09:23:41 -0500192static int xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Francois Romieud59a1882012-03-11 23:34:54 +0100194 struct device *d = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 struct net_device *dev = NULL;
196 struct xircom_private *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 unsigned long flags;
198 unsigned short tmp16;
Francois Romieud59a1882012-03-11 23:34:54 +0100199 int rc;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* First do the PCI initialisation */
202
Francois Romieud59a1882012-03-11 23:34:54 +0100203 rc = pci_enable_device(pdev);
204 if (rc < 0)
205 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 /* disable all powermanagement */
208 pci_write_config_dword(pdev, PCI_POWERMGMT, 0x0000);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 pci_set_master(pdev); /* Why isn't this done by pci_enable_device ?*/
211
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400212 /* clear PCI status, if any */
213 pci_read_config_word (pdev,PCI_STATUS, &tmp16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 pci_write_config_word (pdev, PCI_STATUS,tmp16);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400215
Francois Romieud59a1882012-03-11 23:34:54 +0100216 rc = pci_request_regions(pdev, "xircom_cb");
217 if (rc < 0) {
Joe Perches44298ec2010-01-28 20:59:29 +0000218 pr_err("%s: failed to allocate io-region\n", __func__);
Francois Romieud59a1882012-03-11 23:34:54 +0100219 goto err_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221
Francois Romieud59a1882012-03-11 23:34:54 +0100222 rc = -ENOMEM;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400223 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 Before changing the hardware, allocate the memory.
225 This way, we can fail gracefully if not enough memory
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400226 is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 */
228 dev = alloc_etherdev(sizeof(struct xircom_private));
Joe Perches41de8d42012-01-29 13:47:52 +0000229 if (!dev)
Francois Romieud59a1882012-03-11 23:34:54 +0100230 goto err_release;
Joe Perches41de8d42012-01-29 13:47:52 +0000231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 private = netdev_priv(dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* Allocate the send/receive buffers */
Francois Romieud59a1882012-03-11 23:34:54 +0100235 private->rx_buffer = dma_alloc_coherent(d, 8192,
236 &private->rx_dma_handle,
237 GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +0000238 if (private->rx_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 goto rx_buf_fail;
Joe Perchesd0320f72013-03-14 13:07:21 +0000240
Francois Romieud59a1882012-03-11 23:34:54 +0100241 private->tx_buffer = dma_alloc_coherent(d, 8192,
242 &private->tx_dma_handle,
243 GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +0000244 if (private->tx_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 goto tx_buf_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 SET_NETDEV_DEV(dev, &pdev->dev);
248
249
250 private->dev = dev;
251 private->pdev = pdev;
Francois Romieuebaf7f82012-03-13 09:27:47 +0100252
253 /* IO range. */
254 private->ioaddr = pci_iomap(pdev, 0, 0);
255 if (!private->ioaddr)
256 goto reg_fail;
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 spin_lock_init(&private->lock);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 initialize_card(private);
261 read_mac_address(private);
262 setup_descriptors(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400263
Stephen Hemminger1034c9f2009-01-07 18:00:55 -0800264 dev->netdev_ops = &netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 pci_set_drvdata(pdev, dev);
266
Francois Romieud59a1882012-03-11 23:34:54 +0100267 rc = register_netdev(dev);
268 if (rc < 0) {
Joe Perches44298ec2010-01-28 20:59:29 +0000269 pr_err("%s: netdevice registration failed\n", __func__);
Francois Romieuebaf7f82012-03-13 09:27:47 +0100270 goto err_unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400272
Joe Perches163ef0b2011-05-09 09:45:21 +0000273 netdev_info(dev, "Xircom cardbus revision %i at irq %i\n",
274 pdev->revision, pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /* start the transmitter to get a heartbeat */
276 /* TODO: send 2 dummy packets here */
277 transceiver_voodoo(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 spin_lock_irqsave(&private->lock,flags);
280 activate_transmitter(private);
281 activate_receiver(private);
282 spin_unlock_irqrestore(&private->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 trigger_receive(private);
Francois Romieud59a1882012-03-11 23:34:54 +0100285out:
286 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Francois Romieuebaf7f82012-03-13 09:27:47 +0100288err_unmap:
289 pci_iounmap(pdev, private->ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290reg_fail:
Francois Romieud59a1882012-03-11 23:34:54 +0100291 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 */
Bill Pemberton779c1a82012-12-03 09:23:41 -0500310static void xircom_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
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);
Francois Romieuebaf7f82012-03-13 09:27:47 +0100317 pci_iounmap(pdev, card->ioaddr);
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);
Francois Romieuebaf7f82012-03-13 09:27:47 +0100329 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 unsigned int status;
331 int i;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 spin_lock(&card->lock);
Francois Romieuebaf7f82012-03-13 09:27:47 +0100334 status = xr32(CSR5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Joe Perches54668b82011-05-09 09:45:20 +0000336#if defined DEBUG && DEBUG > 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 print_binary(status);
Joe Perches54668b82011-05-09 09:45:20 +0000338 pr_debug("tx status 0x%08x 0x%08x\n",
339 card->tx_buffer[0], card->tx_buffer[4]);
340 pr_debug("rx status 0x%08x 0x%08x\n",
341 card->rx_buffer[0], card->rx_buffer[4]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400342#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /* Handle shared irq and hotplug */
344 if (status == 0 || status == 0xffffffff) {
345 spin_unlock(&card->lock);
346 return IRQ_NONE;
347 }
348
349 if (link_status_changed(card)) {
350 int newlink;
Joe Perches726b65a2011-05-09 09:45:22 +0000351 netdev_dbg(dev, "Link status has changed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 newlink = link_status(card);
Joe Perches163ef0b2011-05-09 09:45:21 +0000353 netdev_info(dev, "Link is %d mbit\n", newlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (newlink)
355 netif_carrier_on(dev);
356 else
357 netif_carrier_off(dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400361 /* Clear all remaining interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 status |= 0xffffffff; /* FIXME: make this clear only the
363 real existing bits */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100364 xw32(CSR5, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400366
367 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 investigate_write_descriptor(dev,card,i,bufferoffsets[i]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400369 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 investigate_read_descriptor(dev,card,i,bufferoffsets[i]);
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 spin_unlock(&card->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return IRQ_HANDLED;
374}
375
Stephen Hemmingerad096462009-08-31 19:50:53 +0000376static netdev_tx_t xircom_start_xmit(struct sk_buff *skb,
377 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 struct xircom_private *card;
380 unsigned long flags;
381 int nextdescriptor;
382 int desc;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 card = netdev_priv(dev);
385 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /* First see if we can free some descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400388 for (desc=0;desc<NUMDESCRIPTORS;desc++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 investigate_write_descriptor(dev,card,desc,bufferoffsets[desc]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400390
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 nextdescriptor = (card->transmit_used +1) % (NUMDESCRIPTORS);
393 desc = card->transmit_used;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* only send the packet if the descriptor is free */
396 if (card->tx_buffer[4*desc]==0) {
397 /* Copy the packet data; zero the memory first as the card
398 sometimes sends more than you ask it to. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 memset(&card->tx_buffer[bufferoffsets[desc]/4],0,1536);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300401 skb_copy_from_linear_data(skb,
402 &(card->tx_buffer[bufferoffsets[desc] / 4]),
403 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* FIXME: The specification tells us that the length we send HAS to be a multiple of
405 4 bytes. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400406
Al Viro6f35d5d2007-12-23 20:01:04 +0000407 card->tx_buffer[4*desc+1] = cpu_to_le32(skb->len);
408 if (desc == NUMDESCRIPTORS - 1) /* bit 25: last descriptor of the ring */
409 card->tx_buffer[4*desc+1] |= cpu_to_le32(1<<25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Al Viro6f35d5d2007-12-23 20:01:04 +0000411 card->tx_buffer[4*desc+1] |= cpu_to_le32(0xF0000000);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400412 /* 0xF0... means want interrupts*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 card->tx_skb[desc] = skb;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 wmb();
416 /* This gives the descriptor to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000417 card->tx_buffer[4*desc] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 trigger_transmit(card);
Al Viro6f35d5d2007-12-23 20:01:04 +0000419 if (card->tx_buffer[nextdescriptor*4] & cpu_to_le32(0x8000000)) {
420 /* next descriptor is occupied... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 netif_stop_queue(dev);
422 }
423 card->transmit_used = nextdescriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 spin_unlock_irqrestore(&card->lock,flags);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000425 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* Uh oh... no free descriptor... drop the packet */
429 netif_stop_queue(dev);
430 spin_unlock_irqrestore(&card->lock,flags);
431 trigger_transmit(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400432
Erik Mouw06f75252008-02-04 18:56:54 +0100433 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436
437
438
439static int xircom_open(struct net_device *dev)
440{
441 struct xircom_private *xp = netdev_priv(dev);
Francois Romieuebaf7f82012-03-13 09:27:47 +0100442 const int irq = xp->pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 int retval;
Joe Perches54668b82011-05-09 09:45:20 +0000444
Francois Romieuebaf7f82012-03-13 09:27:47 +0100445 netdev_info(dev, "xircom cardbus adaptor found, using irq %i\n", irq);
446 retval = request_irq(irq, xircom_interrupt, IRQF_SHARED, dev->name, dev);
Joe Perches54668b82011-05-09 09:45:20 +0000447 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return retval;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 xircom_up(xp);
451 xp->open = 1;
Joe Perches54668b82011-05-09 09:45:20 +0000452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return 0;
454}
455
456static int xircom_close(struct net_device *dev)
457{
458 struct xircom_private *card;
459 unsigned long flags;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 card = netdev_priv(dev);
462 netif_stop_queue(dev); /* we don't want new packets */
463
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 disable_all_interrupts(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400468#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* We can enable this again once we send dummy packets on ifconfig ethX up */
470 deactivate_receiver(card);
471 deactivate_transmitter(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400472#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 remove_descriptors(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 spin_unlock_irqrestore(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 card->open = 0;
Francois Romieuebaf7f82012-03-13 09:27:47 +0100478 free_irq(card->pdev->irq, dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return 0;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485#ifdef CONFIG_NET_POLL_CONTROLLER
486static void xircom_poll_controller(struct net_device *dev)
487{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100488 struct xircom_private *xp = netdev_priv(dev);
489 const int irq = xp->pdev->irq;
490
491 disable_irq(irq);
492 xircom_interrupt(irq, dev);
493 enable_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495#endif
496
497
498static void initialize_card(struct xircom_private *card)
499{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100500 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 unsigned long flags;
Francois Romieuebaf7f82012-03-13 09:27:47 +0100502 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 spin_lock_irqsave(&card->lock, flags);
505
506 /* First: reset the card */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100507 val = xr32(CSR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 val |= 0x01; /* Software reset */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100509 xw32(CSR0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 udelay(100); /* give the card some time to reset */
512
Francois Romieuebaf7f82012-03-13 09:27:47 +0100513 val = xr32(CSR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 val &= ~0x01; /* disable Software reset */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100515 xw32(CSR0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400518 val = 0; /* Value 0x00 is a safe and conservative value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 for the PCI configuration settings */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100520 xw32(CSR0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522
523 disable_all_interrupts(card);
524 deactivate_receiver(card);
525 deactivate_transmitter(card);
526
527 spin_unlock_irqrestore(&card->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530/*
531trigger_transmit causes the card to check for frames to be transmitted.
532This is accomplished by writing to the CSR1 port. The documentation
533claims that the act of writing is sufficient and that the value is
534ignored; I chose zero.
535*/
536static void trigger_transmit(struct xircom_private *card)
537{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100538 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Francois Romieuebaf7f82012-03-13 09:27:47 +0100540 xw32(CSR1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
543/*
544trigger_receive causes the card to check for empty frames in the
545descriptor list in which packets can be received.
546This is accomplished by writing to the CSR2 port. The documentation
547claims that the act of writing is sufficient and that the value is
548ignored; I chose zero.
549*/
550static void trigger_receive(struct xircom_private *card)
551{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100552 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Francois Romieuebaf7f82012-03-13 09:27:47 +0100554 xw32(CSR2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557/*
558setup_descriptors initializes the send and receive buffers to be valid
559descriptors and programs the addresses into the card.
560*/
561static void setup_descriptors(struct xircom_private *card)
562{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100563 void __iomem *ioaddr = card->ioaddr;
Al Viro6f35d5d2007-12-23 20:01:04 +0000564 u32 address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Eric Sesterhenn / snakebytea707cd62006-01-26 22:02:51 +0100567 BUG_ON(card->rx_buffer == NULL);
568 BUG_ON(card->tx_buffer == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /* Receive descriptors */
571 memset(card->rx_buffer, 0, 128); /* clear the descriptors */
572 for (i=0;i<NUMDESCRIPTORS;i++ ) {
573
574 /* Rx Descr0: It's empty, let the card own it, no errors -> 0x80000000 */
Al Viro6f35d5d2007-12-23 20:01:04 +0000575 card->rx_buffer[i*4 + 0] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 /* Rx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
Al Viro6f35d5d2007-12-23 20:01:04 +0000577 card->rx_buffer[i*4 + 1] = cpu_to_le32(1536);
578 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
579 card->rx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 /* Rx Descr2: address of the buffer
582 we store the buffer at the 2nd half of the page */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400583
Al Viro6f35d5d2007-12-23 20:01:04 +0000584 address = card->rx_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 card->rx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
586 /* Rx Desc3: address of 2nd buffer -> 0 */
587 card->rx_buffer[i*4 + 3] = 0;
588 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 wmb();
591 /* Write the receive descriptor ring address to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000592 address = card->rx_dma_handle;
Francois Romieuebaf7f82012-03-13 09:27:47 +0100593 xw32(CSR3, address); /* Receive descr list address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595
596 /* transmit descriptors */
597 memset(card->tx_buffer, 0, 128); /* clear the descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 for (i=0;i<NUMDESCRIPTORS;i++ ) {
600 /* Tx Descr0: Empty, we own it, no errors -> 0x00000000 */
601 card->tx_buffer[i*4 + 0] = 0x00000000;
602 /* Tx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
Al Viro6f35d5d2007-12-23 20:01:04 +0000603 card->tx_buffer[i*4 + 1] = cpu_to_le32(1536);
604 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
605 card->tx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* Tx Descr2: address of the buffer
608 we store the buffer at the 2nd half of the page */
Al Viro6f35d5d2007-12-23 20:01:04 +0000609 address = card->tx_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 card->tx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
611 /* Tx Desc3: address of 2nd buffer -> 0 */
612 card->tx_buffer[i*4 + 3] = 0;
613 }
614
615 wmb();
616 /* wite the transmit descriptor ring to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000617 address = card->tx_dma_handle;
Francois Romieuebaf7f82012-03-13 09:27:47 +0100618 xw32(CSR4, address); /* xmit descr list address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621/*
622remove_descriptors informs the card the descriptors are no longer
623valid by setting the address in the card to 0x00.
624*/
625static void remove_descriptors(struct xircom_private *card)
626{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100627 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 val = 0;
Francois Romieuebaf7f82012-03-13 09:27:47 +0100631 xw32(CSR3, val); /* Receive descriptor address */
632 xw32(CSR4, val); /* Send descriptor address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635/*
636link_status_changed returns 1 if the card has indicated that
637the link status has changed. The new link status has to be read from CSR12.
638
639This function also clears the status-bit.
640*/
641static int link_status_changed(struct xircom_private *card)
642{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100643 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Francois Romieuebaf7f82012-03-13 09:27:47 +0100646 val = xr32(CSR5); /* Status register */
647 if (!(val & (1 << 27))) /* no change */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /* clear the event by writing a 1 to the bit in the
651 status register. */
652 val = (1 << 27);
Francois Romieuebaf7f82012-03-13 09:27:47 +0100653 xw32(CSR5, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return 1;
656}
657
658
659/*
660transmit_active returns 1 if the transmitter on the card is
661in a non-stopped state.
662*/
663static int transmit_active(struct xircom_private *card)
664{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100665 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Francois Romieuebaf7f82012-03-13 09:27:47 +0100667 if (!(xr32(CSR5) & (7 << 20))) /* transmitter disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return 1;
671}
672
673/*
674receive_active returns 1 if the receiver on the card is
675in a non-stopped state.
676*/
677static int receive_active(struct xircom_private *card)
678{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100679 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Francois Romieuebaf7f82012-03-13 09:27:47 +0100681 if (!(xr32(CSR5) & (7 << 17))) /* receiver disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return 1;
685}
686
687/*
688activate_receiver enables the receiver on the card.
689Before being allowed to active the receiver, the receiver
690must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400691this code actually disables the receiver first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692receiver to become inactive, then it activates the receiver and then
693it waits for the receiver to be active.
694
695must be called with the lock held and interrupts disabled.
696*/
697static void activate_receiver(struct xircom_private *card)
698{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100699 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 unsigned int val;
701 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Francois Romieuebaf7f82012-03-13 09:27:47 +0100703 val = xr32(CSR6); /* Operation mode */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /* If the "active" bit is set and the receiver is already
706 active, no need to do the expensive thing */
707 if ((val&2) && (receive_active(card)))
708 return;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400709
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 val = val & ~2; /* disable the receiver */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100712 xw32(CSR6, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 counter = 10;
715 while (counter > 0) {
716 if (!receive_active(card))
717 break;
718 /* wait a while */
719 udelay(50);
720 counter--;
721 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000722 netdev_err(card->dev, "Receiver failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724
725 /* enable the receiver */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100726 val = xr32(CSR6); /* Operation mode */
727 val = val | 2; /* enable the receiver */
728 xw32(CSR6, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 /* now wait for the card to activate again */
731 counter = 10;
732 while (counter > 0) {
733 if (receive_active(card))
734 break;
735 /* wait a while */
736 udelay(50);
737 counter--;
738 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000739 netdev_err(card->dev,
740 "Receiver failed to re-activate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
744/*
745deactivate_receiver disables the receiver on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400746To achieve this this code disables the receiver first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747then it waits for the receiver to become inactive.
748
749must be called with the lock held and interrupts disabled.
750*/
751static void deactivate_receiver(struct xircom_private *card)
752{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100753 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 unsigned int val;
755 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Francois Romieuebaf7f82012-03-13 09:27:47 +0100757 val = xr32(CSR6); /* Operation mode */
758 val = val & ~2; /* disable the receiver */
759 xw32(CSR6, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 counter = 10;
762 while (counter > 0) {
763 if (!receive_active(card))
764 break;
765 /* wait a while */
766 udelay(50);
767 counter--;
768 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000769 netdev_err(card->dev, "Receiver failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773
774/*
775activate_transmitter enables the transmitter on the card.
776Before being allowed to active the transmitter, the transmitter
777must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400778this code actually disables the transmitter first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779transmitter to become inactive, then it activates the transmitter and then
780it waits for the transmitter to be active again.
781
782must be called with the lock held and interrupts disabled.
783*/
784static void activate_transmitter(struct xircom_private *card)
785{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100786 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 unsigned int val;
788 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Francois Romieuebaf7f82012-03-13 09:27:47 +0100790 val = xr32(CSR6); /* Operation mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 /* If the "active" bit is set and the receiver is already
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400793 active, no need to do the expensive thing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if ((val&(1<<13)) && (transmit_active(card)))
795 return;
796
797 val = val & ~(1 << 13); /* disable the transmitter */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100798 xw32(CSR6, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 counter = 10;
801 while (counter > 0) {
802 if (!transmit_active(card))
803 break;
804 /* wait a while */
805 udelay(50);
806 counter--;
807 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000808 netdev_err(card->dev,
809 "Transmitter failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811
812 /* enable the transmitter */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100813 val = xr32(CSR6); /* Operation mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 val = val | (1 << 13); /* enable the transmitter */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100815 xw32(CSR6, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 /* now wait for the card to activate again */
818 counter = 10;
819 while (counter > 0) {
820 if (transmit_active(card))
821 break;
822 /* wait a while */
823 udelay(50);
824 counter--;
825 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000826 netdev_err(card->dev,
827 "Transmitter failed to re-activate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
831/*
832deactivate_transmitter disables the transmitter on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400833To achieve this this code disables the transmitter first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834then it waits for the transmitter to become inactive.
835
836must be called with the lock held and interrupts disabled.
837*/
838static void deactivate_transmitter(struct xircom_private *card)
839{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100840 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 unsigned int val;
842 int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Francois Romieuebaf7f82012-03-13 09:27:47 +0100844 val = xr32(CSR6); /* Operation mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 val = val & ~2; /* disable the transmitter */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100846 xw32(CSR6, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 counter = 20;
849 while (counter > 0) {
850 if (!transmit_active(card))
851 break;
852 /* wait a while */
853 udelay(50);
854 counter--;
855 if (counter <= 0)
Joe Perches163ef0b2011-05-09 09:45:21 +0000856 netdev_err(card->dev,
857 "Transmitter failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861
862/*
863enable_transmit_interrupt enables the transmit interrupt
864
865must be called with the lock held and interrupts disabled.
866*/
867static void enable_transmit_interrupt(struct xircom_private *card)
868{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100869 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Francois Romieuebaf7f82012-03-13 09:27:47 +0100872 val = xr32(CSR7); /* Interrupt enable register */
873 val |= 1; /* enable the transmit interrupt */
874 xw32(CSR7, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
877
878/*
879enable_receive_interrupt enables the receive interrupt
880
881must be called with the lock held and interrupts disabled.
882*/
883static void enable_receive_interrupt(struct xircom_private *card)
884{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100885 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Francois Romieuebaf7f82012-03-13 09:27:47 +0100888 val = xr32(CSR7); /* Interrupt enable register */
889 val = val | (1 << 6); /* enable the receive interrupt */
890 xw32(CSR7, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893/*
894enable_link_interrupt enables the link status change interrupt
895
896must be called with the lock held and interrupts disabled.
897*/
898static void enable_link_interrupt(struct xircom_private *card)
899{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100900 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Francois Romieuebaf7f82012-03-13 09:27:47 +0100903 val = xr32(CSR7); /* Interrupt enable register */
904 val = val | (1 << 27); /* enable the link status chage interrupt */
905 xw32(CSR7, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
908
909
910/*
911disable_all_interrupts disables all interrupts
912
913must be called with the lock held and interrupts disabled.
914*/
915static void disable_all_interrupts(struct xircom_private *card)
916{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100917 void __iomem *ioaddr = card->ioaddr;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400918
Francois Romieuebaf7f82012-03-13 09:27:47 +0100919 xw32(CSR7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
922/*
923enable_common_interrupts enables several weird interrupts
924
925must be called with the lock held and interrupts disabled.
926*/
927static void enable_common_interrupts(struct xircom_private *card)
928{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100929 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Francois Romieuebaf7f82012-03-13 09:27:47 +0100932 val = xr32(CSR7); /* Interrupt enable register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 val |= (1<<16); /* Normal Interrupt Summary */
934 val |= (1<<15); /* Abnormal Interrupt Summary */
935 val |= (1<<13); /* Fatal bus error */
936 val |= (1<<8); /* Receive Process Stopped */
937 val |= (1<<7); /* Receive Buffer Unavailable */
938 val |= (1<<5); /* Transmit Underflow */
939 val |= (1<<2); /* Transmit Buffer Unavailable */
940 val |= (1<<1); /* Transmit Process Stopped */
Francois Romieuebaf7f82012-03-13 09:27:47 +0100941 xw32(CSR7, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944/*
945enable_promisc starts promisc mode
946
947must be called with the lock held and interrupts disabled.
948*/
949static int enable_promisc(struct xircom_private *card)
950{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100951 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Francois Romieuebaf7f82012-03-13 09:27:47 +0100954 val = xr32(CSR6);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400955 val = val | (1 << 6);
Francois Romieuebaf7f82012-03-13 09:27:47 +0100956 xw32(CSR6, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return 1;
959}
960
961
962
963
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400964/*
Michael Opdenacker59c51592007-05-09 08:57:56 +0200965link_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 -0700966
967Must be called in locked state with interrupts disabled
968*/
969static int link_status(struct xircom_private *card)
970{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100971 void __iomem *ioaddr = card->ioaddr;
972 u8 val;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400973
Francois Romieuebaf7f82012-03-13 09:27:47 +0100974 val = xr8(CSR12);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400975
Francois Romieuebaf7f82012-03-13 09:27:47 +0100976 /* bit 2 is 0 for 10mbit link, 1 for not an 10mbit link */
977 if (!(val & (1 << 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return 10;
Francois Romieuebaf7f82012-03-13 09:27:47 +0100979 /* bit 1 is 0 for 100mbit link, 1 for not an 100mbit link */
980 if (!(val & (1 << 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return 100;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400982
983 /* If we get here -> no link at all */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return 0;
986}
987
988
989
990
991
992/*
993 read_mac_address() reads the MAC address from the NIC and stores it in the "dev" structure.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 This function will take the spinlock itself and can, as a result, not be called with the lock helt.
996 */
997static void read_mac_address(struct xircom_private *card)
998{
Francois Romieuebaf7f82012-03-13 09:27:47 +0100999 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 unsigned long flags;
Francois Romieuebaf7f82012-03-13 09:27:47 +01001001 u8 link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 int i;
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 spin_lock_irqsave(&card->lock, flags);
1005
Francois Romieuebaf7f82012-03-13 09:27:47 +01001006 xw32(CSR9, 1 << 12); /* enable boot rom access */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 for (i = 0x100; i < 0x1f7; i += link + 2) {
Francois Romieuebaf7f82012-03-13 09:27:47 +01001008 u8 tuple, data_id, data_count;
1009
1010 xw32(CSR10, i);
1011 tuple = xr32(CSR9);
1012 xw32(CSR10, i + 1);
1013 link = xr32(CSR9);
1014 xw32(CSR10, i + 2);
1015 data_id = xr32(CSR9);
1016 xw32(CSR10, i + 3);
1017 data_count = xr32(CSR9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if ((tuple == 0x22) && (data_id == 0x04) && (data_count == 0x06)) {
Francois Romieuebaf7f82012-03-13 09:27:47 +01001019 int j;
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 for (j = 0; j < 6; j++) {
Francois Romieuebaf7f82012-03-13 09:27:47 +01001022 xw32(CSR10, i + j + 4);
1023 card->dev->dev_addr[j] = xr32(CSR9) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025 break;
1026 } else if (link == 0) {
1027 break;
1028 }
1029 }
1030 spin_unlock_irqrestore(&card->lock, flags);
Johannes Berge1749612008-10-27 15:59:26 -07001031 pr_debug(" %pM\n", card->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
1034
1035/*
1036 transceiver_voodoo() enables the external UTP plug thingy.
1037 it's called voodoo as I stole this code and cannot cross-reference
1038 it with the specification.
1039 */
1040static void transceiver_voodoo(struct xircom_private *card)
1041{
Francois Romieuebaf7f82012-03-13 09:27:47 +01001042 void __iomem *ioaddr = card->ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 unsigned long flags;
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /* disable all powermanagement */
1046 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1047
1048 setup_descriptors(card);
1049
1050 spin_lock_irqsave(&card->lock, flags);
1051
Francois Romieuebaf7f82012-03-13 09:27:47 +01001052 xw32(CSR15, 0x0008);
1053 udelay(25);
1054 xw32(CSR15, 0xa8050000);
1055 udelay(25);
1056 xw32(CSR15, 0xa00f0000);
1057 udelay(25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001058
Francois Romieuebaf7f82012-03-13 09:27:47 +01001059 spin_unlock_irqrestore(&card->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 netif_start_queue(card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
1064
1065static void xircom_up(struct xircom_private *card)
1066{
1067 unsigned long flags;
1068 int i;
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /* disable all powermanagement */
1071 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1072
1073 setup_descriptors(card);
1074
1075 spin_lock_irqsave(&card->lock, flags);
1076
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 enable_link_interrupt(card);
1079 enable_transmit_interrupt(card);
1080 enable_receive_interrupt(card);
1081 enable_common_interrupts(card);
1082 enable_promisc(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /* The card can have received packets already, read them away now */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001085 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 investigate_read_descriptor(card->dev,card,i,bufferoffsets[i]);
1087
1088
1089 spin_unlock_irqrestore(&card->lock, flags);
1090 trigger_receive(card);
1091 trigger_transmit(card);
1092 netif_start_queue(card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
1095/* Bufferoffset is in BYTES */
Joe Perches163ef0b2011-05-09 09:45:21 +00001096static void
1097investigate_read_descriptor(struct net_device *dev, struct xircom_private *card,
1098 int descnr, unsigned int bufferoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Joe Perches163ef0b2011-05-09 09:45:21 +00001100 int status;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001101
Joe Perches163ef0b2011-05-09 09:45:21 +00001102 status = le32_to_cpu(card->rx_buffer[4*descnr]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001103
Joe Perches163ef0b2011-05-09 09:45:21 +00001104 if (status > 0) { /* packet received */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001105
Joe Perches163ef0b2011-05-09 09:45:21 +00001106 /* TODO: discard error packets */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001107
Joe Perches163ef0b2011-05-09 09:45:21 +00001108 short pkt_len = ((status >> 16) & 0x7ff) - 4;
1109 /* minus 4, we don't want the CRC */
1110 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Joe Perches163ef0b2011-05-09 09:45:21 +00001112 if (pkt_len > 1518) {
1113 netdev_err(dev, "Packet length %i is bogus\n", pkt_len);
1114 pkt_len = 1518;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
Joe Perches163ef0b2011-05-09 09:45:21 +00001116
Pradeep A Dalvi21a4e462012-02-05 02:50:10 +00001117 skb = netdev_alloc_skb(dev, pkt_len + 2);
Joe Perches163ef0b2011-05-09 09:45:21 +00001118 if (skb == NULL) {
1119 dev->stats.rx_dropped++;
1120 goto out;
1121 }
1122 skb_reserve(skb, 2);
1123 skb_copy_to_linear_data(skb,
1124 &card->rx_buffer[bufferoffset / 4],
1125 pkt_len);
1126 skb_put(skb, pkt_len);
1127 skb->protocol = eth_type_trans(skb, dev);
1128 netif_rx(skb);
1129 dev->stats.rx_packets++;
1130 dev->stats.rx_bytes += pkt_len;
1131
1132out:
1133 /* give the buffer back to the card */
1134 card->rx_buffer[4*descnr] = cpu_to_le32(0x80000000);
1135 trigger_receive(card);
1136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
1139
1140/* Bufferoffset is in BYTES */
Joe Perches163ef0b2011-05-09 09:45:21 +00001141static void
1142investigate_write_descriptor(struct net_device *dev,
1143 struct xircom_private *card,
1144 int descnr, unsigned int bufferoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Joe Perches163ef0b2011-05-09 09:45:21 +00001146 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Joe Perches163ef0b2011-05-09 09:45:21 +00001148 status = le32_to_cpu(card->tx_buffer[4*descnr]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001149#if 0
Joe Perches163ef0b2011-05-09 09:45:21 +00001150 if (status & 0x8000) { /* Major error */
1151 pr_err("Major transmit error status %x\n", status);
1152 card->tx_buffer[4*descnr] = 0;
1153 netif_wake_queue (dev);
1154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155#endif
Joe Perches163ef0b2011-05-09 09:45:21 +00001156 if (status > 0) { /* bit 31 is 0 when done */
1157 if (card->tx_skb[descnr]!=NULL) {
1158 dev->stats.tx_bytes += card->tx_skb[descnr]->len;
1159 dev_kfree_skb_irq(card->tx_skb[descnr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
Joe Perches163ef0b2011-05-09 09:45:21 +00001161 card->tx_skb[descnr] = NULL;
1162 /* Bit 8 in the status field is 1 if there was a collision */
1163 if (status & (1 << 8))
1164 dev->stats.collisions++;
1165 card->tx_buffer[4*descnr] = 0; /* descriptor is free again */
1166 netif_wake_queue (dev);
1167 dev->stats.tx_packets++;
1168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
Peter Hüwe31d60eb2013-05-21 12:42:11 +00001171module_pci_driver(xircom_ops);