blob: 5a73752be2ca2cdf455968855f82ae5b3bfa8f9e [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
40#ifdef DEBUG
41#define enter(x) printk("Enter: %s, %s line %i\n",x,__FILE__,__LINE__)
42#define leave(x) printk("Leave: %s, %s line %i\n",x,__FILE__,__LINE__)
43#else
44#define enter(x) do {} while (0)
45#define leave(x) do {} while (0)
46#endif
47
48
49MODULE_DESCRIPTION("Xircom Cardbus ethernet driver");
50MODULE_AUTHOR("Arjan van de Ven <arjanv@redhat.com>");
51MODULE_LICENSE("GPL");
52
53
54
55/* IO registers on the card, offsets */
56#define CSR0 0x00
57#define CSR1 0x08
58#define CSR2 0x10
59#define CSR3 0x18
60#define CSR4 0x20
61#define CSR5 0x28
62#define CSR6 0x30
63#define CSR7 0x38
64#define CSR8 0x40
65#define CSR9 0x48
66#define CSR10 0x50
67#define CSR11 0x58
68#define CSR12 0x60
69#define CSR13 0x68
70#define CSR14 0x70
71#define CSR15 0x78
72#define CSR16 0x80
73
74/* PCI registers */
75#define PCI_POWERMGMT 0x40
76
77/* Offsets of the buffers within the descriptor pages, in bytes */
78
79#define NUMDESCRIPTORS 4
80
81static int bufferoffsets[NUMDESCRIPTORS] = {128,2048,4096,6144};
82
83
84struct xircom_private {
85 /* Send and receive buffers, kernel-addressable and dma addressable forms */
86
Al Viro6f35d5d2007-12-23 20:01:04 +000087 __le32 *rx_buffer;
88 __le32 *tx_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 dma_addr_t rx_dma_handle;
91 dma_addr_t tx_dma_handle;
92
93 struct sk_buff *tx_skb[4];
94
95 unsigned long io_port;
96 int open;
Jeff Garzikf3b197a2006-05-26 21:39:03 -040097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 /* transmit_used is the rotating counter that indicates which transmit
99 descriptor has to be used next */
100 int transmit_used;
101
102 /* Spinlock to serialize register operations.
103 It must be helt while manipulating the following registers:
104 CSR0, CSR6, CSR7, CSR9, CSR10, CSR15
105 */
106 spinlock_t lock;
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 struct pci_dev *pdev;
109 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
112
113/* Function prototypes */
114static int xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id);
115static void xircom_remove(struct pci_dev *pdev);
David Howells7d12e782006-10-05 14:55:46 +0100116static irqreturn_t xircom_interrupt(int irq, void *dev_instance);
Stephen Hemmingerad096462009-08-31 19:50:53 +0000117static netdev_tx_t xircom_start_xmit(struct sk_buff *skb,
118 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static int xircom_open(struct net_device *dev);
120static int xircom_close(struct net_device *dev);
121static void xircom_up(struct xircom_private *card);
Keith Owens3be034b2005-09-13 15:05:13 +1000122#ifdef CONFIG_NET_POLL_CONTROLLER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static void xircom_poll_controller(struct net_device *dev);
124#endif
125
126static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset);
127static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset);
128static void read_mac_address(struct xircom_private *card);
129static void transceiver_voodoo(struct xircom_private *card);
130static void initialize_card(struct xircom_private *card);
131static void trigger_transmit(struct xircom_private *card);
132static void trigger_receive(struct xircom_private *card);
133static void setup_descriptors(struct xircom_private *card);
134static void remove_descriptors(struct xircom_private *card);
135static int link_status_changed(struct xircom_private *card);
136static void activate_receiver(struct xircom_private *card);
137static void deactivate_receiver(struct xircom_private *card);
138static void activate_transmitter(struct xircom_private *card);
139static void deactivate_transmitter(struct xircom_private *card);
140static void enable_transmit_interrupt(struct xircom_private *card);
141static void enable_receive_interrupt(struct xircom_private *card);
142static void enable_link_interrupt(struct xircom_private *card);
143static void disable_all_interrupts(struct xircom_private *card);
144static int link_status(struct xircom_private *card);
145
146
147
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000148static DEFINE_PCI_DEVICE_TABLE(xircom_pci_table) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 {0x115D, 0x0003, PCI_ANY_ID, PCI_ANY_ID,},
150 {0,},
151};
152MODULE_DEVICE_TABLE(pci, xircom_pci_table);
153
154static struct pci_driver xircom_ops = {
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400155 .name = "xircom_cb",
156 .id_table = xircom_pci_table,
157 .probe = xircom_probe,
158 .remove = xircom_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 .suspend =NULL,
160 .resume =NULL
161};
162
163
164#ifdef DEBUG
165static void print_binary(unsigned int number)
166{
167 int i,i2;
168 char buffer[64];
169 memset(buffer,0,64);
170 i2=0;
171 for (i=31;i>=0;i--) {
172 if (number & (1<<i))
173 buffer[i2++]='1';
174 else
175 buffer[i2++]='0';
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400176 if ((i&3)==0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 buffer[i2++]=' ';
178 }
179 printk("%s\n",buffer);
180}
181#endif
182
Stephen Hemminger1034c9f2009-01-07 18:00:55 -0800183static const struct net_device_ops netdev_ops = {
184 .ndo_open = xircom_open,
185 .ndo_stop = xircom_close,
186 .ndo_start_xmit = xircom_start_xmit,
187 .ndo_change_mtu = eth_change_mtu,
188 .ndo_set_mac_address = eth_mac_addr,
189 .ndo_validate_addr = eth_validate_addr,
190#ifdef CONFIG_NET_POLL_CONTROLLER
191 .ndo_poll_controller = xircom_poll_controller,
192#endif
193};
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/* xircom_probe is the code that gets called on device insertion.
196 it sets up the hardware and registers the device to the networklayer.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 TODO: Send 1 or 2 "dummy" packets here as the card seems to discard the
199 first two packets that get send, and pump hates that.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
202static int __devinit xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id)
203{
204 struct net_device *dev = NULL;
205 struct xircom_private *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 unsigned long flags;
207 unsigned short tmp16;
208 enter("xircom_probe");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* First do the PCI initialisation */
211
212 if (pci_enable_device(pdev))
213 return -ENODEV;
214
215 /* disable all powermanagement */
216 pci_write_config_dword(pdev, PCI_POWERMGMT, 0x0000);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 pci_set_master(pdev); /* Why isn't this done by pci_enable_device ?*/
219
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400220 /* clear PCI status, if any */
221 pci_read_config_word (pdev,PCI_STATUS, &tmp16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 pci_write_config_word (pdev, PCI_STATUS,tmp16);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (!request_region(pci_resource_start(pdev, 0), 128, "xircom_cb")) {
Joe Perches44298ec2010-01-28 20:59:29 +0000225 pr_err("%s: failed to allocate io-region\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return -ENODEV;
227 }
228
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400229 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 Before changing the hardware, allocate the memory.
231 This way, we can fail gracefully if not enough memory
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400232 is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
234 dev = alloc_etherdev(sizeof(struct xircom_private));
235 if (!dev) {
Joe Perches44298ec2010-01-28 20:59:29 +0000236 pr_err("%s: failed to allocate etherdev\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 goto device_fail;
238 }
239 private = netdev_priv(dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* Allocate the send/receive buffers */
242 private->rx_buffer = pci_alloc_consistent(pdev,8192,&private->rx_dma_handle);
243 if (private->rx_buffer == NULL) {
Joe Perches44298ec2010-01-28 20:59:29 +0000244 pr_err("%s: no memory for rx buffer\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 goto rx_buf_fail;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 private->tx_buffer = pci_alloc_consistent(pdev,8192,&private->tx_dma_handle);
248 if (private->tx_buffer == NULL) {
Joe Perches44298ec2010-01-28 20:59:29 +0000249 pr_err("%s: no memory for tx buffer\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 goto tx_buf_fail;
251 }
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 SET_NETDEV_DEV(dev, &pdev->dev);
254
255
256 private->dev = dev;
257 private->pdev = pdev;
258 private->io_port = pci_resource_start(pdev, 0);
259 spin_lock_init(&private->lock);
260 dev->irq = pdev->irq;
261 dev->base_addr = private->io_port;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 initialize_card(private);
264 read_mac_address(private);
265 setup_descriptors(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400266
Stephen Hemminger1034c9f2009-01-07 18:00:55 -0800267 dev->netdev_ops = &netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 pci_set_drvdata(pdev, dev);
269
270 if (register_netdev(dev)) {
Joe Perches44298ec2010-01-28 20:59:29 +0000271 pr_err("%s: netdevice registration failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 goto reg_fail;
273 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400274
Joe Perches44298ec2010-01-28 20:59:29 +0000275 dev_info(&dev->dev, "Xircom cardbus revision %i at irq %i\n",
276 pdev->revision, pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /* start the transmitter to get a heartbeat */
278 /* TODO: send 2 dummy packets here */
279 transceiver_voodoo(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 spin_lock_irqsave(&private->lock,flags);
282 activate_transmitter(private);
283 activate_receiver(private);
284 spin_unlock_irqrestore(&private->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 trigger_receive(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 leave("xircom_probe");
289 return 0;
290
291reg_fail:
292 kfree(private->tx_buffer);
293tx_buf_fail:
294 kfree(private->rx_buffer);
295rx_buf_fail:
296 free_netdev(dev);
297device_fail:
298 return -ENODEV;
299}
300
301
302/*
303 xircom_remove is called on module-unload or on device-eject.
304 it unregisters the irq, io-region and network device.
305 Interrupts and such are already stopped in the "ifconfig ethX down"
306 code.
307 */
308static void __devexit xircom_remove(struct pci_dev *pdev)
309{
310 struct net_device *dev = pci_get_drvdata(pdev);
311 struct xircom_private *card = netdev_priv(dev);
312
313 enter("xircom_remove");
314 pci_free_consistent(pdev,8192,card->rx_buffer,card->rx_dma_handle);
315 pci_free_consistent(pdev,8192,card->tx_buffer,card->tx_dma_handle);
316
317 release_region(dev->base_addr, 128);
318 unregister_netdev(dev);
319 free_netdev(dev);
320 pci_set_drvdata(pdev, NULL);
321 leave("xircom_remove");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400322}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
David Howells7d12e782006-10-05 14:55:46 +0100324static irqreturn_t xircom_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 struct net_device *dev = (struct net_device *) dev_instance;
327 struct xircom_private *card = netdev_priv(dev);
328 unsigned int status;
329 int i;
330
331 enter("xircom_interrupt\n");
332
333 spin_lock(&card->lock);
334 status = inl(card->io_port+CSR5);
335
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400336#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 print_binary(status);
Frans Popc2bb1b92010-03-24 07:57:34 +0000338 printk("tx status 0x%08x 0x%08x\n",
Joe Perches44298ec2010-01-28 20:59:29 +0000339 card->tx_buffer[0], card->tx_buffer[4]);
Frans Popc2bb1b92010-03-24 07:57:34 +0000340 printk("rx status 0x%08x 0x%08x\n",
Joe Perches44298ec2010-01-28 20:59:29 +0000341 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 Perches44298ec2010-01-28 20:59:29 +0000351 printk(KERN_DEBUG "xircom_cb: Link status has changed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 newlink = link_status(card);
Joe Perches44298ec2010-01-28 20:59:29 +0000353 dev_info(&dev->dev, "Link is %i 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 */
364 outl(status,card->io_port+CSR5);
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
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 spin_unlock(&card->lock);
374 leave("xircom_interrupt");
375 return IRQ_HANDLED;
376}
377
Stephen Hemmingerad096462009-08-31 19:50:53 +0000378static netdev_tx_t xircom_start_xmit(struct sk_buff *skb,
379 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct xircom_private *card;
382 unsigned long flags;
383 int nextdescriptor;
384 int desc;
385 enter("xircom_start_xmit");
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;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400427 leave("xircom-start_xmit - sent");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 spin_unlock_irqrestore(&card->lock,flags);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000429 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433
434 /* Uh oh... no free descriptor... drop the packet */
435 netif_stop_queue(dev);
436 spin_unlock_irqrestore(&card->lock,flags);
437 trigger_transmit(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400438
Erik Mouw06f75252008-02-04 18:56:54 +0100439 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442
443
444
445static int xircom_open(struct net_device *dev)
446{
447 struct xircom_private *xp = netdev_priv(dev);
448 int retval;
449 enter("xircom_open");
Frans Popc2bb1b92010-03-24 07:57:34 +0000450 pr_info("xircom cardbus adaptor found, registering as %s, using irq %i\n",
Joe Perches44298ec2010-01-28 20:59:29 +0000451 dev->name, dev->irq);
Joe Perchesa0607fd2009-11-18 23:29:17 -0800452 retval = request_irq(dev->irq, xircom_interrupt, IRQF_SHARED, dev->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (retval) {
454 leave("xircom_open - No IRQ");
455 return retval;
456 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 xircom_up(xp);
459 xp->open = 1;
460 leave("xircom_open");
461 return 0;
462}
463
464static int xircom_close(struct net_device *dev)
465{
466 struct xircom_private *card;
467 unsigned long flags;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 enter("xircom_close");
470 card = netdev_priv(dev);
471 netif_stop_queue(dev); /* we don't want new packets */
472
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 disable_all_interrupts(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400477#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /* We can enable this again once we send dummy packets on ifconfig ethX up */
479 deactivate_receiver(card);
480 deactivate_transmitter(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400481#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 remove_descriptors(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 spin_unlock_irqrestore(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 card->open = 0;
487 free_irq(dev->irq,dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 leave("xircom_close");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return 0;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496#ifdef CONFIG_NET_POLL_CONTROLLER
497static void xircom_poll_controller(struct net_device *dev)
498{
499 disable_irq(dev->irq);
David Howells7d12e782006-10-05 14:55:46 +0100500 xircom_interrupt(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 enable_irq(dev->irq);
502}
503#endif
504
505
506static void initialize_card(struct xircom_private *card)
507{
508 unsigned int val;
509 unsigned long flags;
510 enter("initialize_card");
511
512
513 spin_lock_irqsave(&card->lock, flags);
514
515 /* First: reset the card */
516 val = inl(card->io_port + CSR0);
517 val |= 0x01; /* Software reset */
518 outl(val, card->io_port + CSR0);
519
520 udelay(100); /* give the card some time to reset */
521
522 val = inl(card->io_port + CSR0);
523 val &= ~0x01; /* disable Software reset */
524 outl(val, card->io_port + CSR0);
525
526
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400527 val = 0; /* Value 0x00 is a safe and conservative value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 for the PCI configuration settings */
529 outl(val, card->io_port + CSR0);
530
531
532 disable_all_interrupts(card);
533 deactivate_receiver(card);
534 deactivate_transmitter(card);
535
536 spin_unlock_irqrestore(&card->lock, flags);
537
538 leave("initialize_card");
539}
540
541/*
542trigger_transmit causes the card to check for frames to be transmitted.
543This is accomplished by writing to the CSR1 port. The documentation
544claims that the act of writing is sufficient and that the value is
545ignored; I chose zero.
546*/
547static void trigger_transmit(struct xircom_private *card)
548{
549 unsigned int val;
550 enter("trigger_transmit");
551
552 val = 0;
553 outl(val, card->io_port + CSR1);
554
555 leave("trigger_transmit");
556}
557
558/*
559trigger_receive causes the card to check for empty frames in the
560descriptor list in which packets can be received.
561This is accomplished by writing to the CSR2 port. The documentation
562claims that the act of writing is sufficient and that the value is
563ignored; I chose zero.
564*/
565static void trigger_receive(struct xircom_private *card)
566{
567 unsigned int val;
568 enter("trigger_receive");
569
570 val = 0;
571 outl(val, card->io_port + CSR2);
572
573 leave("trigger_receive");
574}
575
576/*
577setup_descriptors initializes the send and receive buffers to be valid
578descriptors and programs the addresses into the card.
579*/
580static void setup_descriptors(struct xircom_private *card)
581{
Al Viro6f35d5d2007-12-23 20:01:04 +0000582 u32 address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 int i;
584 enter("setup_descriptors");
585
586
Eric Sesterhenn / snakebytea707cd62006-01-26 22:02:51 +0100587 BUG_ON(card->rx_buffer == NULL);
588 BUG_ON(card->tx_buffer == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /* Receive descriptors */
591 memset(card->rx_buffer, 0, 128); /* clear the descriptors */
592 for (i=0;i<NUMDESCRIPTORS;i++ ) {
593
594 /* Rx Descr0: It's empty, let the card own it, no errors -> 0x80000000 */
Al Viro6f35d5d2007-12-23 20:01:04 +0000595 card->rx_buffer[i*4 + 0] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* Rx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
Al Viro6f35d5d2007-12-23 20:01:04 +0000597 card->rx_buffer[i*4 + 1] = cpu_to_le32(1536);
598 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
599 card->rx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /* Rx Descr2: address of the buffer
602 we store the buffer at the 2nd half of the page */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400603
Al Viro6f35d5d2007-12-23 20:01:04 +0000604 address = card->rx_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 card->rx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
606 /* Rx Desc3: address of 2nd buffer -> 0 */
607 card->rx_buffer[i*4 + 3] = 0;
608 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 wmb();
611 /* Write the receive descriptor ring address to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000612 address = card->rx_dma_handle;
613 outl(address, card->io_port + CSR3); /* Receive descr list address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615
616 /* transmit descriptors */
617 memset(card->tx_buffer, 0, 128); /* clear the descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 for (i=0;i<NUMDESCRIPTORS;i++ ) {
620 /* Tx Descr0: Empty, we own it, no errors -> 0x00000000 */
621 card->tx_buffer[i*4 + 0] = 0x00000000;
622 /* Tx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
Al Viro6f35d5d2007-12-23 20:01:04 +0000623 card->tx_buffer[i*4 + 1] = cpu_to_le32(1536);
624 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
625 card->tx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* Tx Descr2: address of the buffer
628 we store the buffer at the 2nd half of the page */
Al Viro6f35d5d2007-12-23 20:01:04 +0000629 address = card->tx_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 card->tx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
631 /* Tx Desc3: address of 2nd buffer -> 0 */
632 card->tx_buffer[i*4 + 3] = 0;
633 }
634
635 wmb();
636 /* wite the transmit descriptor ring to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000637 address = card->tx_dma_handle;
638 outl(address, card->io_port + CSR4); /* xmit descr list address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 leave("setup_descriptors");
641}
642
643/*
644remove_descriptors informs the card the descriptors are no longer
645valid by setting the address in the card to 0x00.
646*/
647static void remove_descriptors(struct xircom_private *card)
648{
649 unsigned int val;
650 enter("remove_descriptors");
651
652 val = 0;
653 outl(val, card->io_port + CSR3); /* Receive descriptor address */
654 outl(val, card->io_port + CSR4); /* Send descriptor address */
655
656 leave("remove_descriptors");
657}
658
659/*
660link_status_changed returns 1 if the card has indicated that
661the link status has changed. The new link status has to be read from CSR12.
662
663This function also clears the status-bit.
664*/
665static int link_status_changed(struct xircom_private *card)
666{
667 unsigned int val;
668 enter("link_status_changed");
669
670 val = inl(card->io_port + CSR5); /* Status register */
671
672 if ((val & (1 << 27)) == 0) { /* no change */
673 leave("link_status_changed - nochange");
674 return 0;
675 }
676
677 /* clear the event by writing a 1 to the bit in the
678 status register. */
679 val = (1 << 27);
680 outl(val, card->io_port + CSR5);
681
682 leave("link_status_changed - changed");
683 return 1;
684}
685
686
687/*
688transmit_active returns 1 if the transmitter on the card is
689in a non-stopped state.
690*/
691static int transmit_active(struct xircom_private *card)
692{
693 unsigned int val;
694 enter("transmit_active");
695
696 val = inl(card->io_port + CSR5); /* Status register */
697
698 if ((val & (7 << 20)) == 0) { /* transmitter disabled */
699 leave("transmit_active - inactive");
700 return 0;
701 }
702
703 leave("transmit_active - active");
704 return 1;
705}
706
707/*
708receive_active returns 1 if the receiver on the card is
709in a non-stopped state.
710*/
711static int receive_active(struct xircom_private *card)
712{
713 unsigned int val;
714 enter("receive_active");
715
716
717 val = inl(card->io_port + CSR5); /* Status register */
718
719 if ((val & (7 << 17)) == 0) { /* receiver disabled */
720 leave("receive_active - inactive");
721 return 0;
722 }
723
724 leave("receive_active - active");
725 return 1;
726}
727
728/*
729activate_receiver enables the receiver on the card.
730Before being allowed to active the receiver, the receiver
731must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400732this code actually disables the receiver first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733receiver to become inactive, then it activates the receiver and then
734it waits for the receiver to be active.
735
736must be called with the lock held and interrupts disabled.
737*/
738static void activate_receiver(struct xircom_private *card)
739{
740 unsigned int val;
741 int counter;
742 enter("activate_receiver");
743
744
745 val = inl(card->io_port + CSR6); /* Operation mode */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /* If the "active" bit is set and the receiver is already
748 active, no need to do the expensive thing */
749 if ((val&2) && (receive_active(card)))
750 return;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400751
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 val = val & ~2; /* disable the receiver */
754 outl(val, card->io_port + CSR6);
755
756 counter = 10;
757 while (counter > 0) {
758 if (!receive_active(card))
759 break;
760 /* wait a while */
761 udelay(50);
762 counter--;
763 if (counter <= 0)
Joe Perches44298ec2010-01-28 20:59:29 +0000764 pr_err("Receiver failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766
767 /* enable the receiver */
768 val = inl(card->io_port + CSR6); /* Operation mode */
769 val = val | 2; /* enable the receiver */
770 outl(val, card->io_port + CSR6);
771
772 /* now wait for the card to activate again */
773 counter = 10;
774 while (counter > 0) {
775 if (receive_active(card))
776 break;
777 /* wait a while */
778 udelay(50);
779 counter--;
780 if (counter <= 0)
Joe Perches44298ec2010-01-28 20:59:29 +0000781 pr_err("Receiver failed to re-activate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783
784 leave("activate_receiver");
785}
786
787/*
788deactivate_receiver disables the receiver on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400789To achieve this this code disables the receiver first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790then it waits for the receiver to become inactive.
791
792must be called with the lock held and interrupts disabled.
793*/
794static void deactivate_receiver(struct xircom_private *card)
795{
796 unsigned int val;
797 int counter;
798 enter("deactivate_receiver");
799
800 val = inl(card->io_port + CSR6); /* Operation mode */
801 val = val & ~2; /* disable the receiver */
802 outl(val, card->io_port + CSR6);
803
804 counter = 10;
805 while (counter > 0) {
806 if (!receive_active(card))
807 break;
808 /* wait a while */
809 udelay(50);
810 counter--;
811 if (counter <= 0)
Joe Perches44298ec2010-01-28 20:59:29 +0000812 pr_err("Receiver failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814
815
816 leave("deactivate_receiver");
817}
818
819
820/*
821activate_transmitter enables the transmitter on the card.
822Before being allowed to active the transmitter, the transmitter
823must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400824this code actually disables the transmitter first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825transmitter to become inactive, then it activates the transmitter and then
826it waits for the transmitter to be active again.
827
828must be called with the lock held and interrupts disabled.
829*/
830static void activate_transmitter(struct xircom_private *card)
831{
832 unsigned int val;
833 int counter;
834 enter("activate_transmitter");
835
836
837 val = inl(card->io_port + CSR6); /* Operation mode */
838
839 /* If the "active" bit is set and the receiver is already
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400840 active, no need to do the expensive thing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if ((val&(1<<13)) && (transmit_active(card)))
842 return;
843
844 val = val & ~(1 << 13); /* disable the transmitter */
845 outl(val, card->io_port + CSR6);
846
847 counter = 10;
848 while (counter > 0) {
849 if (!transmit_active(card))
850 break;
851 /* wait a while */
852 udelay(50);
853 counter--;
854 if (counter <= 0)
Joe Perches44298ec2010-01-28 20:59:29 +0000855 pr_err("Transmitter failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857
858 /* enable the transmitter */
859 val = inl(card->io_port + CSR6); /* Operation mode */
860 val = val | (1 << 13); /* enable the transmitter */
861 outl(val, card->io_port + CSR6);
862
863 /* now wait for the card to activate again */
864 counter = 10;
865 while (counter > 0) {
866 if (transmit_active(card))
867 break;
868 /* wait a while */
869 udelay(50);
870 counter--;
871 if (counter <= 0)
Joe Perches44298ec2010-01-28 20:59:29 +0000872 pr_err("Transmitter failed to re-activate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874
875 leave("activate_transmitter");
876}
877
878/*
879deactivate_transmitter disables the transmitter on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400880To achieve this this code disables the transmitter first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881then it waits for the transmitter to become inactive.
882
883must be called with the lock held and interrupts disabled.
884*/
885static void deactivate_transmitter(struct xircom_private *card)
886{
887 unsigned int val;
888 int counter;
889 enter("deactivate_transmitter");
890
891 val = inl(card->io_port + CSR6); /* Operation mode */
892 val = val & ~2; /* disable the transmitter */
893 outl(val, card->io_port + CSR6);
894
895 counter = 20;
896 while (counter > 0) {
897 if (!transmit_active(card))
898 break;
899 /* wait a while */
900 udelay(50);
901 counter--;
902 if (counter <= 0)
Joe Perches44298ec2010-01-28 20:59:29 +0000903 pr_err("Transmitter failed to deactivate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905
906
907 leave("deactivate_transmitter");
908}
909
910
911/*
912enable_transmit_interrupt enables the transmit interrupt
913
914must be called with the lock held and interrupts disabled.
915*/
916static void enable_transmit_interrupt(struct xircom_private *card)
917{
918 unsigned int val;
919 enter("enable_transmit_interrupt");
920
921 val = inl(card->io_port + CSR7); /* Interrupt enable register */
922 val |= 1; /* enable the transmit interrupt */
923 outl(val, card->io_port + CSR7);
924
925 leave("enable_transmit_interrupt");
926}
927
928
929/*
930enable_receive_interrupt enables the receive interrupt
931
932must be called with the lock held and interrupts disabled.
933*/
934static void enable_receive_interrupt(struct xircom_private *card)
935{
936 unsigned int val;
937 enter("enable_receive_interrupt");
938
939 val = inl(card->io_port + CSR7); /* Interrupt enable register */
940 val = val | (1 << 6); /* enable the receive interrupt */
941 outl(val, card->io_port + CSR7);
942
943 leave("enable_receive_interrupt");
944}
945
946/*
947enable_link_interrupt enables the link status change interrupt
948
949must be called with the lock held and interrupts disabled.
950*/
951static void enable_link_interrupt(struct xircom_private *card)
952{
953 unsigned int val;
954 enter("enable_link_interrupt");
955
956 val = inl(card->io_port + CSR7); /* Interrupt enable register */
957 val = val | (1 << 27); /* enable the link status chage interrupt */
958 outl(val, card->io_port + CSR7);
959
960 leave("enable_link_interrupt");
961}
962
963
964
965/*
966disable_all_interrupts disables all interrupts
967
968must be called with the lock held and interrupts disabled.
969*/
970static void disable_all_interrupts(struct xircom_private *card)
971{
972 unsigned int val;
973 enter("enable_all_interrupts");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 val = 0; /* disable all interrupts */
976 outl(val, card->io_port + CSR7);
977
978 leave("disable_all_interrupts");
979}
980
981/*
982enable_common_interrupts enables several weird interrupts
983
984must be called with the lock held and interrupts disabled.
985*/
986static void enable_common_interrupts(struct xircom_private *card)
987{
988 unsigned int val;
989 enter("enable_link_interrupt");
990
991 val = inl(card->io_port + CSR7); /* Interrupt enable register */
992 val |= (1<<16); /* Normal Interrupt Summary */
993 val |= (1<<15); /* Abnormal Interrupt Summary */
994 val |= (1<<13); /* Fatal bus error */
995 val |= (1<<8); /* Receive Process Stopped */
996 val |= (1<<7); /* Receive Buffer Unavailable */
997 val |= (1<<5); /* Transmit Underflow */
998 val |= (1<<2); /* Transmit Buffer Unavailable */
999 val |= (1<<1); /* Transmit Process Stopped */
1000 outl(val, card->io_port + CSR7);
1001
1002 leave("enable_link_interrupt");
1003}
1004
1005/*
1006enable_promisc starts promisc mode
1007
1008must be called with the lock held and interrupts disabled.
1009*/
1010static int enable_promisc(struct xircom_private *card)
1011{
1012 unsigned int val;
1013 enter("enable_promisc");
1014
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001015 val = inl(card->io_port + CSR6);
1016 val = val | (1 << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 outl(val, card->io_port + CSR6);
1018
1019 leave("enable_promisc");
1020 return 1;
1021}
1022
1023
1024
1025
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001026/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02001027link_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 -07001028
1029Must be called in locked state with interrupts disabled
1030*/
1031static int link_status(struct xircom_private *card)
1032{
1033 unsigned int val;
1034 enter("link_status");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 val = inb(card->io_port + CSR12);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (!(val&(1<<2))) /* bit 2 is 0 for 10mbit link, 1 for not an 10mbit link */
1039 return 10;
1040 if (!(val&(1<<1))) /* bit 1 is 0 for 100mbit link, 1 for not an 100mbit link */
1041 return 100;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001042
1043 /* If we get here -> no link at all */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 leave("link_status");
1046 return 0;
1047}
1048
1049
1050
1051
1052
1053/*
1054 read_mac_address() reads the MAC address from the NIC and stores it in the "dev" structure.
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 This function will take the spinlock itself and can, as a result, not be called with the lock helt.
1057 */
1058static void read_mac_address(struct xircom_private *card)
1059{
1060 unsigned char j, tuple, link, data_id, data_count;
1061 unsigned long flags;
1062 int i;
1063
1064 enter("read_mac_address");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 spin_lock_irqsave(&card->lock, flags);
1067
1068 outl(1 << 12, card->io_port + CSR9); /* enable boot rom access */
1069 for (i = 0x100; i < 0x1f7; i += link + 2) {
1070 outl(i, card->io_port + CSR10);
1071 tuple = inl(card->io_port + CSR9) & 0xff;
1072 outl(i + 1, card->io_port + CSR10);
1073 link = inl(card->io_port + CSR9) & 0xff;
1074 outl(i + 2, card->io_port + CSR10);
1075 data_id = inl(card->io_port + CSR9) & 0xff;
1076 outl(i + 3, card->io_port + CSR10);
1077 data_count = inl(card->io_port + CSR9) & 0xff;
1078 if ((tuple == 0x22) && (data_id == 0x04) && (data_count == 0x06)) {
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001079 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 * This is it. We have the data we want.
1081 */
1082 for (j = 0; j < 6; j++) {
1083 outl(i + j + 4, card->io_port + CSR10);
1084 card->dev->dev_addr[j] = inl(card->io_port + CSR9) & 0xff;
1085 }
1086 break;
1087 } else if (link == 0) {
1088 break;
1089 }
1090 }
1091 spin_unlock_irqrestore(&card->lock, flags);
Johannes Berge1749612008-10-27 15:59:26 -07001092 pr_debug(" %pM\n", card->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 leave("read_mac_address");
1094}
1095
1096
1097/*
1098 transceiver_voodoo() enables the external UTP plug thingy.
1099 it's called voodoo as I stole this code and cannot cross-reference
1100 it with the specification.
1101 */
1102static void transceiver_voodoo(struct xircom_private *card)
1103{
1104 unsigned long flags;
1105
1106 enter("transceiver_voodoo");
1107
1108 /* disable all powermanagement */
1109 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1110
1111 setup_descriptors(card);
1112
1113 spin_lock_irqsave(&card->lock, flags);
1114
1115 outl(0x0008, card->io_port + CSR15);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001116 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 outl(0xa8050000, card->io_port + CSR15);
1118 udelay(25);
1119 outl(0xa00f0000, card->io_port + CSR15);
1120 udelay(25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 spin_unlock_irqrestore(&card->lock, flags);
1123
1124 netif_start_queue(card->dev);
1125 leave("transceiver_voodoo");
1126}
1127
1128
1129static void xircom_up(struct xircom_private *card)
1130{
1131 unsigned long flags;
1132 int i;
1133
1134 enter("xircom_up");
1135
1136 /* disable all powermanagement */
1137 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1138
1139 setup_descriptors(card);
1140
1141 spin_lock_irqsave(&card->lock, flags);
1142
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 enable_link_interrupt(card);
1145 enable_transmit_interrupt(card);
1146 enable_receive_interrupt(card);
1147 enable_common_interrupts(card);
1148 enable_promisc(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /* The card can have received packets already, read them away now */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001151 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 investigate_read_descriptor(card->dev,card,i,bufferoffsets[i]);
1153
1154
1155 spin_unlock_irqrestore(&card->lock, flags);
1156 trigger_receive(card);
1157 trigger_transmit(card);
1158 netif_start_queue(card->dev);
1159 leave("xircom_up");
1160}
1161
1162/* Bufferoffset is in BYTES */
1163static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset)
1164{
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001165 int status;
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 enter("investigate_read_descriptor");
Al Viro6f35d5d2007-12-23 20:01:04 +00001168 status = le32_to_cpu(card->rx_buffer[4*descnr]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if ((status > 0)) { /* packet received */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /* TODO: discard error packets */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 short pkt_len = ((status >> 16) & 0x7ff) - 4; /* minus 4, we don't want the CRC */
1175 struct sk_buff *skb;
1176
1177 if (pkt_len > 1518) {
Joe Perches44298ec2010-01-28 20:59:29 +00001178 pr_err("Packet length %i is bogus\n", pkt_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 pkt_len = 1518;
1180 }
1181
1182 skb = dev_alloc_skb(pkt_len + 2);
1183 if (skb == NULL) {
Stephen Hemminger1034c9f2009-01-07 18:00:55 -08001184 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 goto out;
1186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 skb_reserve(skb, 2);
David S. Miller8c7b7fa2007-07-10 22:08:12 -07001188 skb_copy_to_linear_data(skb, (unsigned char*)&card->rx_buffer[bufferoffset / 4], pkt_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 skb_put(skb, pkt_len);
1190 skb->protocol = eth_type_trans(skb, dev);
1191 netif_rx(skb);
Stephen Hemminger1034c9f2009-01-07 18:00:55 -08001192 dev->stats.rx_packets++;
1193 dev->stats.rx_bytes += pkt_len;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 out:
1196 /* give the buffer back to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +00001197 card->rx_buffer[4*descnr] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 trigger_receive(card);
1199 }
1200
1201 leave("investigate_read_descriptor");
1202
1203}
1204
1205
1206/* Bufferoffset is in BYTES */
1207static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset)
1208{
1209 int status;
1210
1211 enter("investigate_write_descriptor");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001212
Al Viro6f35d5d2007-12-23 20:01:04 +00001213 status = le32_to_cpu(card->tx_buffer[4*descnr]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001214#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (status & 0x8000) { /* Major error */
Joe Perches44298ec2010-01-28 20:59:29 +00001216 pr_err("Major transmit error status %x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 card->tx_buffer[4*descnr] = 0;
1218 netif_wake_queue (dev);
1219 }
1220#endif
1221 if (status > 0) { /* bit 31 is 0 when done */
1222 if (card->tx_skb[descnr]!=NULL) {
Stephen Hemminger1034c9f2009-01-07 18:00:55 -08001223 dev->stats.tx_bytes += card->tx_skb[descnr]->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 dev_kfree_skb_irq(card->tx_skb[descnr]);
1225 }
1226 card->tx_skb[descnr] = NULL;
1227 /* Bit 8 in the status field is 1 if there was a collision */
1228 if (status&(1<<8))
Stephen Hemminger1034c9f2009-01-07 18:00:55 -08001229 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 card->tx_buffer[4*descnr] = 0; /* descriptor is free again */
1231 netif_wake_queue (dev);
Stephen Hemminger1034c9f2009-01-07 18:00:55 -08001232 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234
1235 leave("investigate_write_descriptor");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
1239
1240static int __init xircom_init(void)
1241{
Alexey Dobriyanec42cdb2006-08-14 23:00:28 -07001242 return pci_register_driver(&xircom_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
1245static void __exit xircom_exit(void)
1246{
1247 pci_unregister_driver(&xircom_ops);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001248}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001250module_init(xircom_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251module_exit(xircom_exit)
1252