blob: 6b93d016911650e0ef778ffeaf3dd9c06e370069 [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
17#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/string.h>
20#include <linux/errno.h>
21#include <linux/ioport.h>
22#include <linux/slab.h>
23#include <linux/interrupt.h>
24#include <linux/pci.h>
25#include <linux/netdevice.h>
26#include <linux/etherdevice.h>
27#include <linux/skbuff.h>
28#include <linux/delay.h>
29#include <linux/init.h>
30#include <linux/ethtool.h>
31#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
39#ifdef DEBUG
40#define enter(x) printk("Enter: %s, %s line %i\n",x,__FILE__,__LINE__)
41#define leave(x) printk("Leave: %s, %s line %i\n",x,__FILE__,__LINE__)
42#else
43#define enter(x) do {} while (0)
44#define leave(x) do {} while (0)
45#endif
46
47
48MODULE_DESCRIPTION("Xircom Cardbus ethernet driver");
49MODULE_AUTHOR("Arjan van de Ven <arjanv@redhat.com>");
50MODULE_LICENSE("GPL");
51
52
53
54/* IO registers on the card, offsets */
55#define CSR0 0x00
56#define CSR1 0x08
57#define CSR2 0x10
58#define CSR3 0x18
59#define CSR4 0x20
60#define CSR5 0x28
61#define CSR6 0x30
62#define CSR7 0x38
63#define CSR8 0x40
64#define CSR9 0x48
65#define CSR10 0x50
66#define CSR11 0x58
67#define CSR12 0x60
68#define CSR13 0x68
69#define CSR14 0x70
70#define CSR15 0x78
71#define CSR16 0x80
72
73/* PCI registers */
74#define PCI_POWERMGMT 0x40
75
76/* Offsets of the buffers within the descriptor pages, in bytes */
77
78#define NUMDESCRIPTORS 4
79
80static int bufferoffsets[NUMDESCRIPTORS] = {128,2048,4096,6144};
81
82
83struct xircom_private {
84 /* Send and receive buffers, kernel-addressable and dma addressable forms */
85
Al Viro6f35d5d2007-12-23 20:01:04 +000086 __le32 *rx_buffer;
87 __le32 *tx_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 dma_addr_t rx_dma_handle;
90 dma_addr_t tx_dma_handle;
91
92 struct sk_buff *tx_skb[4];
93
94 unsigned long io_port;
95 int open;
Jeff Garzikf3b197a2006-05-26 21:39:03 -040096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 /* transmit_used is the rotating counter that indicates which transmit
98 descriptor has to be used next */
99 int transmit_used;
100
101 /* Spinlock to serialize register operations.
102 It must be helt while manipulating the following registers:
103 CSR0, CSR6, CSR7, CSR9, CSR10, CSR15
104 */
105 spinlock_t lock;
106
107
108 struct pci_dev *pdev;
109 struct net_device *dev;
110 struct net_device_stats stats;
111};
112
113
114/* Function prototypes */
115static int xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id);
116static void xircom_remove(struct pci_dev *pdev);
David Howells7d12e782006-10-05 14:55:46 +0100117static irqreturn_t xircom_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static int xircom_start_xmit(struct sk_buff *skb, struct net_device *dev);
119static int xircom_open(struct net_device *dev);
120static int xircom_close(struct net_device *dev);
121static void xircom_up(struct xircom_private *card);
122static struct net_device_stats *xircom_get_stats(struct net_device *dev);
Keith Owens3be034b2005-09-13 15:05:13 +1000123#ifdef CONFIG_NET_POLL_CONTROLLER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static void xircom_poll_controller(struct net_device *dev);
125#endif
126
127static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset);
128static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset);
129static void read_mac_address(struct xircom_private *card);
130static void transceiver_voodoo(struct xircom_private *card);
131static void initialize_card(struct xircom_private *card);
132static void trigger_transmit(struct xircom_private *card);
133static void trigger_receive(struct xircom_private *card);
134static void setup_descriptors(struct xircom_private *card);
135static void remove_descriptors(struct xircom_private *card);
136static int link_status_changed(struct xircom_private *card);
137static void activate_receiver(struct xircom_private *card);
138static void deactivate_receiver(struct xircom_private *card);
139static void activate_transmitter(struct xircom_private *card);
140static void deactivate_transmitter(struct xircom_private *card);
141static void enable_transmit_interrupt(struct xircom_private *card);
142static void enable_receive_interrupt(struct xircom_private *card);
143static void enable_link_interrupt(struct xircom_private *card);
144static void disable_all_interrupts(struct xircom_private *card);
145static int link_status(struct xircom_private *card);
146
147
148
149static struct pci_device_id xircom_pci_table[] = {
150 {0x115D, 0x0003, PCI_ANY_ID, PCI_ANY_ID,},
151 {0,},
152};
153MODULE_DEVICE_TABLE(pci, xircom_pci_table);
154
155static struct pci_driver xircom_ops = {
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400156 .name = "xircom_cb",
157 .id_table = xircom_pci_table,
158 .probe = xircom_probe,
159 .remove = xircom_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 .suspend =NULL,
161 .resume =NULL
162};
163
164
165#ifdef DEBUG
166static void print_binary(unsigned int number)
167{
168 int i,i2;
169 char buffer[64];
170 memset(buffer,0,64);
171 i2=0;
172 for (i=31;i>=0;i--) {
173 if (number & (1<<i))
174 buffer[i2++]='1';
175 else
176 buffer[i2++]='0';
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400177 if ((i&3)==0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 buffer[i2++]=' ';
179 }
180 printk("%s\n",buffer);
181}
182#endif
183
184static void netdev_get_drvinfo(struct net_device *dev,
185 struct ethtool_drvinfo *info)
186{
187 struct xircom_private *private = netdev_priv(dev);
188
189 strcpy(info->driver, "xircom_cb");
190 strcpy(info->bus_info, pci_name(private->pdev));
191}
192
Jeff Garzik7282d492006-09-13 14:30:00 -0400193static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 .get_drvinfo = netdev_get_drvinfo,
195};
196
197/* xircom_probe is the code that gets called on device insertion.
198 it sets up the hardware and registers the device to the networklayer.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 TODO: Send 1 or 2 "dummy" packets here as the card seems to discard the
201 first two packets that get send, and pump hates that.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 */
204static int __devinit xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id)
205{
206 struct net_device *dev = NULL;
207 struct xircom_private *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 unsigned long flags;
209 unsigned short tmp16;
210 enter("xircom_probe");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* First do the PCI initialisation */
213
214 if (pci_enable_device(pdev))
215 return -ENODEV;
216
217 /* disable all powermanagement */
218 pci_write_config_dword(pdev, PCI_POWERMGMT, 0x0000);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 pci_set_master(pdev); /* Why isn't this done by pci_enable_device ?*/
221
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400222 /* clear PCI status, if any */
223 pci_read_config_word (pdev,PCI_STATUS, &tmp16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 pci_write_config_word (pdev, PCI_STATUS,tmp16);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (!request_region(pci_resource_start(pdev, 0), 128, "xircom_cb")) {
227 printk(KERN_ERR "xircom_probe: failed to allocate io-region\n");
228 return -ENODEV;
229 }
230
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400231 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 Before changing the hardware, allocate the memory.
233 This way, we can fail gracefully if not enough memory
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400234 is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
236 dev = alloc_etherdev(sizeof(struct xircom_private));
237 if (!dev) {
238 printk(KERN_ERR "xircom_probe: failed to allocate etherdev\n");
239 goto device_fail;
240 }
241 private = netdev_priv(dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /* Allocate the send/receive buffers */
244 private->rx_buffer = pci_alloc_consistent(pdev,8192,&private->rx_dma_handle);
245 if (private->rx_buffer == NULL) {
246 printk(KERN_ERR "xircom_probe: no memory for rx buffer \n");
247 goto rx_buf_fail;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 private->tx_buffer = pci_alloc_consistent(pdev,8192,&private->tx_dma_handle);
250 if (private->tx_buffer == NULL) {
251 printk(KERN_ERR "xircom_probe: no memory for tx buffer \n");
252 goto tx_buf_fail;
253 }
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 SET_NETDEV_DEV(dev, &pdev->dev);
256
257
258 private->dev = dev;
259 private->pdev = pdev;
260 private->io_port = pci_resource_start(pdev, 0);
261 spin_lock_init(&private->lock);
262 dev->irq = pdev->irq;
263 dev->base_addr = private->io_port;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 initialize_card(private);
266 read_mac_address(private);
267 setup_descriptors(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 dev->open = &xircom_open;
270 dev->hard_start_xmit = &xircom_start_xmit;
271 dev->stop = &xircom_close;
272 dev->get_stats = &xircom_get_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#ifdef CONFIG_NET_POLL_CONTROLLER
274 dev->poll_controller = &xircom_poll_controller;
275#endif
276 SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
277 pci_set_drvdata(pdev, dev);
278
279 if (register_netdev(dev)) {
280 printk(KERN_ERR "xircom_probe: netdevice registration failed.\n");
281 goto reg_fail;
282 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400283
Auke Kok44c10132007-06-08 15:46:36 -0700284 printk(KERN_INFO "%s: Xircom cardbus revision %i at irq %i \n", dev->name, pdev->revision, pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* start the transmitter to get a heartbeat */
286 /* TODO: send 2 dummy packets here */
287 transceiver_voodoo(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 spin_lock_irqsave(&private->lock,flags);
290 activate_transmitter(private);
291 activate_receiver(private);
292 spin_unlock_irqrestore(&private->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 trigger_receive(private);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 leave("xircom_probe");
297 return 0;
298
299reg_fail:
300 kfree(private->tx_buffer);
301tx_buf_fail:
302 kfree(private->rx_buffer);
303rx_buf_fail:
304 free_netdev(dev);
305device_fail:
306 return -ENODEV;
307}
308
309
310/*
311 xircom_remove is called on module-unload or on device-eject.
312 it unregisters the irq, io-region and network device.
313 Interrupts and such are already stopped in the "ifconfig ethX down"
314 code.
315 */
316static void __devexit xircom_remove(struct pci_dev *pdev)
317{
318 struct net_device *dev = pci_get_drvdata(pdev);
319 struct xircom_private *card = netdev_priv(dev);
320
321 enter("xircom_remove");
322 pci_free_consistent(pdev,8192,card->rx_buffer,card->rx_dma_handle);
323 pci_free_consistent(pdev,8192,card->tx_buffer,card->tx_dma_handle);
324
325 release_region(dev->base_addr, 128);
326 unregister_netdev(dev);
327 free_netdev(dev);
328 pci_set_drvdata(pdev, NULL);
329 leave("xircom_remove");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400330}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
David Howells7d12e782006-10-05 14:55:46 +0100332static irqreturn_t xircom_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 struct net_device *dev = (struct net_device *) dev_instance;
335 struct xircom_private *card = netdev_priv(dev);
336 unsigned int status;
337 int i;
338
339 enter("xircom_interrupt\n");
340
341 spin_lock(&card->lock);
342 status = inl(card->io_port+CSR5);
343
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400344#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 print_binary(status);
346 printk("tx status 0x%08x 0x%08x \n",card->tx_buffer[0],card->tx_buffer[4]);
347 printk("rx status 0x%08x 0x%08x \n",card->rx_buffer[0],card->rx_buffer[4]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400348#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* Handle shared irq and hotplug */
350 if (status == 0 || status == 0xffffffff) {
351 spin_unlock(&card->lock);
352 return IRQ_NONE;
353 }
354
355 if (link_status_changed(card)) {
356 int newlink;
357 printk(KERN_DEBUG "xircom_cb: Link status has changed \n");
358 newlink = link_status(card);
359 printk(KERN_INFO "xircom_cb: Link is %i mbit \n",newlink);
360 if (newlink)
361 netif_carrier_on(dev);
362 else
363 netif_carrier_off(dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400367 /* Clear all remaining interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 status |= 0xffffffff; /* FIXME: make this clear only the
369 real existing bits */
370 outl(status,card->io_port+CSR5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400372
373 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 investigate_write_descriptor(dev,card,i,bufferoffsets[i]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400375 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 investigate_read_descriptor(dev,card,i,bufferoffsets[i]);
377
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 spin_unlock(&card->lock);
380 leave("xircom_interrupt");
381 return IRQ_HANDLED;
382}
383
384static int xircom_start_xmit(struct sk_buff *skb, struct net_device *dev)
385{
386 struct xircom_private *card;
387 unsigned long flags;
388 int nextdescriptor;
389 int desc;
390 enter("xircom_start_xmit");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 card = netdev_priv(dev);
393 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* First see if we can free some descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400396 for (desc=0;desc<NUMDESCRIPTORS;desc++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 investigate_write_descriptor(dev,card,desc,bufferoffsets[desc]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400398
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 nextdescriptor = (card->transmit_used +1) % (NUMDESCRIPTORS);
401 desc = card->transmit_used;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /* only send the packet if the descriptor is free */
404 if (card->tx_buffer[4*desc]==0) {
405 /* Copy the packet data; zero the memory first as the card
406 sometimes sends more than you ask it to. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 memset(&card->tx_buffer[bufferoffsets[desc]/4],0,1536);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300409 skb_copy_from_linear_data(skb,
410 &(card->tx_buffer[bufferoffsets[desc] / 4]),
411 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* FIXME: The specification tells us that the length we send HAS to be a multiple of
413 4 bytes. */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400414
Al Viro6f35d5d2007-12-23 20:01:04 +0000415 card->tx_buffer[4*desc+1] = cpu_to_le32(skb->len);
416 if (desc == NUMDESCRIPTORS - 1) /* bit 25: last descriptor of the ring */
417 card->tx_buffer[4*desc+1] |= cpu_to_le32(1<<25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Al Viro6f35d5d2007-12-23 20:01:04 +0000419 card->tx_buffer[4*desc+1] |= cpu_to_le32(0xF0000000);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400420 /* 0xF0... means want interrupts*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 card->tx_skb[desc] = skb;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 wmb();
424 /* This gives the descriptor to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000425 card->tx_buffer[4*desc] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 trigger_transmit(card);
Al Viro6f35d5d2007-12-23 20:01:04 +0000427 if (card->tx_buffer[nextdescriptor*4] & cpu_to_le32(0x8000000)) {
428 /* next descriptor is occupied... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 netif_stop_queue(dev);
430 }
431 card->transmit_used = nextdescriptor;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400432 leave("xircom-start_xmit - sent");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 spin_unlock_irqrestore(&card->lock,flags);
434 return 0;
435 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438
439 /* Uh oh... no free descriptor... drop the packet */
440 netif_stop_queue(dev);
441 spin_unlock_irqrestore(&card->lock,flags);
442 trigger_transmit(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400443
Erik Mouw06f75252008-02-04 18:56:54 +0100444 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447
448
449
450static int xircom_open(struct net_device *dev)
451{
452 struct xircom_private *xp = netdev_priv(dev);
453 int retval;
454 enter("xircom_open");
455 printk(KERN_INFO "xircom cardbus adaptor found, registering as %s, using irq %i \n",dev->name,dev->irq);
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700456 retval = request_irq(dev->irq, &xircom_interrupt, IRQF_SHARED, dev->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (retval) {
458 leave("xircom_open - No IRQ");
459 return retval;
460 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 xircom_up(xp);
463 xp->open = 1;
464 leave("xircom_open");
465 return 0;
466}
467
468static int xircom_close(struct net_device *dev)
469{
470 struct xircom_private *card;
471 unsigned long flags;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 enter("xircom_close");
474 card = netdev_priv(dev);
475 netif_stop_queue(dev); /* we don't want new packets */
476
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 spin_lock_irqsave(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 disable_all_interrupts(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400481#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /* We can enable this again once we send dummy packets on ifconfig ethX up */
483 deactivate_receiver(card);
484 deactivate_transmitter(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400485#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 remove_descriptors(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 spin_unlock_irqrestore(&card->lock,flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 card->open = 0;
491 free_irq(dev->irq,dev);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 leave("xircom_close");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return 0;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499
500
501static struct net_device_stats *xircom_get_stats(struct net_device *dev)
502{
503 struct xircom_private *card = netdev_priv(dev);
504 return &card->stats;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508#ifdef CONFIG_NET_POLL_CONTROLLER
509static void xircom_poll_controller(struct net_device *dev)
510{
511 disable_irq(dev->irq);
David Howells7d12e782006-10-05 14:55:46 +0100512 xircom_interrupt(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 enable_irq(dev->irq);
514}
515#endif
516
517
518static void initialize_card(struct xircom_private *card)
519{
520 unsigned int val;
521 unsigned long flags;
522 enter("initialize_card");
523
524
525 spin_lock_irqsave(&card->lock, flags);
526
527 /* First: reset the card */
528 val = inl(card->io_port + CSR0);
529 val |= 0x01; /* Software reset */
530 outl(val, card->io_port + CSR0);
531
532 udelay(100); /* give the card some time to reset */
533
534 val = inl(card->io_port + CSR0);
535 val &= ~0x01; /* disable Software reset */
536 outl(val, card->io_port + CSR0);
537
538
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400539 val = 0; /* Value 0x00 is a safe and conservative value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 for the PCI configuration settings */
541 outl(val, card->io_port + CSR0);
542
543
544 disable_all_interrupts(card);
545 deactivate_receiver(card);
546 deactivate_transmitter(card);
547
548 spin_unlock_irqrestore(&card->lock, flags);
549
550 leave("initialize_card");
551}
552
553/*
554trigger_transmit causes the card to check for frames to be transmitted.
555This is accomplished by writing to the CSR1 port. The documentation
556claims that the act of writing is sufficient and that the value is
557ignored; I chose zero.
558*/
559static void trigger_transmit(struct xircom_private *card)
560{
561 unsigned int val;
562 enter("trigger_transmit");
563
564 val = 0;
565 outl(val, card->io_port + CSR1);
566
567 leave("trigger_transmit");
568}
569
570/*
571trigger_receive causes the card to check for empty frames in the
572descriptor list in which packets can be received.
573This is accomplished by writing to the CSR2 port. The documentation
574claims that the act of writing is sufficient and that the value is
575ignored; I chose zero.
576*/
577static void trigger_receive(struct xircom_private *card)
578{
579 unsigned int val;
580 enter("trigger_receive");
581
582 val = 0;
583 outl(val, card->io_port + CSR2);
584
585 leave("trigger_receive");
586}
587
588/*
589setup_descriptors initializes the send and receive buffers to be valid
590descriptors and programs the addresses into the card.
591*/
592static void setup_descriptors(struct xircom_private *card)
593{
Al Viro6f35d5d2007-12-23 20:01:04 +0000594 u32 address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int i;
596 enter("setup_descriptors");
597
598
Eric Sesterhenn / snakebytea707cd62006-01-26 22:02:51 +0100599 BUG_ON(card->rx_buffer == NULL);
600 BUG_ON(card->tx_buffer == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /* Receive descriptors */
603 memset(card->rx_buffer, 0, 128); /* clear the descriptors */
604 for (i=0;i<NUMDESCRIPTORS;i++ ) {
605
606 /* Rx Descr0: It's empty, let the card own it, no errors -> 0x80000000 */
Al Viro6f35d5d2007-12-23 20:01:04 +0000607 card->rx_buffer[i*4 + 0] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* Rx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
Al Viro6f35d5d2007-12-23 20:01:04 +0000609 card->rx_buffer[i*4 + 1] = cpu_to_le32(1536);
610 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
611 card->rx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 /* Rx Descr2: address of the buffer
614 we store the buffer at the 2nd half of the page */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400615
Al Viro6f35d5d2007-12-23 20:01:04 +0000616 address = card->rx_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 card->rx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
618 /* Rx Desc3: address of 2nd buffer -> 0 */
619 card->rx_buffer[i*4 + 3] = 0;
620 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 wmb();
623 /* Write the receive descriptor ring address to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000624 address = card->rx_dma_handle;
625 outl(address, card->io_port + CSR3); /* Receive descr list address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627
628 /* transmit descriptors */
629 memset(card->tx_buffer, 0, 128); /* clear the descriptors */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 for (i=0;i<NUMDESCRIPTORS;i++ ) {
632 /* Tx Descr0: Empty, we own it, no errors -> 0x00000000 */
633 card->tx_buffer[i*4 + 0] = 0x00000000;
634 /* Tx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
Al Viro6f35d5d2007-12-23 20:01:04 +0000635 card->tx_buffer[i*4 + 1] = cpu_to_le32(1536);
636 if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
637 card->tx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /* Tx Descr2: address of the buffer
640 we store the buffer at the 2nd half of the page */
Al Viro6f35d5d2007-12-23 20:01:04 +0000641 address = card->tx_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 card->tx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
643 /* Tx Desc3: address of 2nd buffer -> 0 */
644 card->tx_buffer[i*4 + 3] = 0;
645 }
646
647 wmb();
648 /* wite the transmit descriptor ring to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +0000649 address = card->tx_dma_handle;
650 outl(address, card->io_port + CSR4); /* xmit descr list address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 leave("setup_descriptors");
653}
654
655/*
656remove_descriptors informs the card the descriptors are no longer
657valid by setting the address in the card to 0x00.
658*/
659static void remove_descriptors(struct xircom_private *card)
660{
661 unsigned int val;
662 enter("remove_descriptors");
663
664 val = 0;
665 outl(val, card->io_port + CSR3); /* Receive descriptor address */
666 outl(val, card->io_port + CSR4); /* Send descriptor address */
667
668 leave("remove_descriptors");
669}
670
671/*
672link_status_changed returns 1 if the card has indicated that
673the link status has changed. The new link status has to be read from CSR12.
674
675This function also clears the status-bit.
676*/
677static int link_status_changed(struct xircom_private *card)
678{
679 unsigned int val;
680 enter("link_status_changed");
681
682 val = inl(card->io_port + CSR5); /* Status register */
683
684 if ((val & (1 << 27)) == 0) { /* no change */
685 leave("link_status_changed - nochange");
686 return 0;
687 }
688
689 /* clear the event by writing a 1 to the bit in the
690 status register. */
691 val = (1 << 27);
692 outl(val, card->io_port + CSR5);
693
694 leave("link_status_changed - changed");
695 return 1;
696}
697
698
699/*
700transmit_active returns 1 if the transmitter on the card is
701in a non-stopped state.
702*/
703static int transmit_active(struct xircom_private *card)
704{
705 unsigned int val;
706 enter("transmit_active");
707
708 val = inl(card->io_port + CSR5); /* Status register */
709
710 if ((val & (7 << 20)) == 0) { /* transmitter disabled */
711 leave("transmit_active - inactive");
712 return 0;
713 }
714
715 leave("transmit_active - active");
716 return 1;
717}
718
719/*
720receive_active returns 1 if the receiver on the card is
721in a non-stopped state.
722*/
723static int receive_active(struct xircom_private *card)
724{
725 unsigned int val;
726 enter("receive_active");
727
728
729 val = inl(card->io_port + CSR5); /* Status register */
730
731 if ((val & (7 << 17)) == 0) { /* receiver disabled */
732 leave("receive_active - inactive");
733 return 0;
734 }
735
736 leave("receive_active - active");
737 return 1;
738}
739
740/*
741activate_receiver enables the receiver on the card.
742Before being allowed to active the receiver, the receiver
743must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400744this code actually disables the receiver first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745receiver to become inactive, then it activates the receiver and then
746it waits for the receiver to be active.
747
748must be called with the lock held and interrupts disabled.
749*/
750static void activate_receiver(struct xircom_private *card)
751{
752 unsigned int val;
753 int counter;
754 enter("activate_receiver");
755
756
757 val = inl(card->io_port + CSR6); /* Operation mode */
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 /* If the "active" bit is set and the receiver is already
760 active, no need to do the expensive thing */
761 if ((val&2) && (receive_active(card)))
762 return;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400763
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 val = val & ~2; /* disable the receiver */
766 outl(val, card->io_port + CSR6);
767
768 counter = 10;
769 while (counter > 0) {
770 if (!receive_active(card))
771 break;
772 /* wait a while */
773 udelay(50);
774 counter--;
775 if (counter <= 0)
776 printk(KERN_ERR "xircom_cb: Receiver failed to deactivate\n");
777 }
778
779 /* enable the receiver */
780 val = inl(card->io_port + CSR6); /* Operation mode */
781 val = val | 2; /* enable the receiver */
782 outl(val, card->io_port + CSR6);
783
784 /* now wait for the card to activate again */
785 counter = 10;
786 while (counter > 0) {
787 if (receive_active(card))
788 break;
789 /* wait a while */
790 udelay(50);
791 counter--;
792 if (counter <= 0)
793 printk(KERN_ERR "xircom_cb: Receiver failed to re-activate\n");
794 }
795
796 leave("activate_receiver");
797}
798
799/*
800deactivate_receiver disables the receiver on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400801To achieve this this code disables the receiver first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802then it waits for the receiver to become inactive.
803
804must be called with the lock held and interrupts disabled.
805*/
806static void deactivate_receiver(struct xircom_private *card)
807{
808 unsigned int val;
809 int counter;
810 enter("deactivate_receiver");
811
812 val = inl(card->io_port + CSR6); /* Operation mode */
813 val = val & ~2; /* disable the receiver */
814 outl(val, card->io_port + CSR6);
815
816 counter = 10;
817 while (counter > 0) {
818 if (!receive_active(card))
819 break;
820 /* wait a while */
821 udelay(50);
822 counter--;
823 if (counter <= 0)
824 printk(KERN_ERR "xircom_cb: Receiver failed to deactivate\n");
825 }
826
827
828 leave("deactivate_receiver");
829}
830
831
832/*
833activate_transmitter enables the transmitter on the card.
834Before being allowed to active the transmitter, the transmitter
835must be completely de-activated. To achieve this,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400836this code actually disables the transmitter first; then it waits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837transmitter to become inactive, then it activates the transmitter and then
838it waits for the transmitter to be active again.
839
840must be called with the lock held and interrupts disabled.
841*/
842static void activate_transmitter(struct xircom_private *card)
843{
844 unsigned int val;
845 int counter;
846 enter("activate_transmitter");
847
848
849 val = inl(card->io_port + CSR6); /* Operation mode */
850
851 /* If the "active" bit is set and the receiver is already
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400852 active, no need to do the expensive thing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if ((val&(1<<13)) && (transmit_active(card)))
854 return;
855
856 val = val & ~(1 << 13); /* disable the transmitter */
857 outl(val, card->io_port + CSR6);
858
859 counter = 10;
860 while (counter > 0) {
861 if (!transmit_active(card))
862 break;
863 /* wait a while */
864 udelay(50);
865 counter--;
866 if (counter <= 0)
867 printk(KERN_ERR "xircom_cb: Transmitter failed to deactivate\n");
868 }
869
870 /* enable the transmitter */
871 val = inl(card->io_port + CSR6); /* Operation mode */
872 val = val | (1 << 13); /* enable the transmitter */
873 outl(val, card->io_port + CSR6);
874
875 /* now wait for the card to activate again */
876 counter = 10;
877 while (counter > 0) {
878 if (transmit_active(card))
879 break;
880 /* wait a while */
881 udelay(50);
882 counter--;
883 if (counter <= 0)
884 printk(KERN_ERR "xircom_cb: Transmitter failed to re-activate\n");
885 }
886
887 leave("activate_transmitter");
888}
889
890/*
891deactivate_transmitter disables the transmitter on the card.
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400892To achieve this this code disables the transmitter first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893then it waits for the transmitter to become inactive.
894
895must be called with the lock held and interrupts disabled.
896*/
897static void deactivate_transmitter(struct xircom_private *card)
898{
899 unsigned int val;
900 int counter;
901 enter("deactivate_transmitter");
902
903 val = inl(card->io_port + CSR6); /* Operation mode */
904 val = val & ~2; /* disable the transmitter */
905 outl(val, card->io_port + CSR6);
906
907 counter = 20;
908 while (counter > 0) {
909 if (!transmit_active(card))
910 break;
911 /* wait a while */
912 udelay(50);
913 counter--;
914 if (counter <= 0)
915 printk(KERN_ERR "xircom_cb: Transmitter failed to deactivate\n");
916 }
917
918
919 leave("deactivate_transmitter");
920}
921
922
923/*
924enable_transmit_interrupt enables the transmit interrupt
925
926must be called with the lock held and interrupts disabled.
927*/
928static void enable_transmit_interrupt(struct xircom_private *card)
929{
930 unsigned int val;
931 enter("enable_transmit_interrupt");
932
933 val = inl(card->io_port + CSR7); /* Interrupt enable register */
934 val |= 1; /* enable the transmit interrupt */
935 outl(val, card->io_port + CSR7);
936
937 leave("enable_transmit_interrupt");
938}
939
940
941/*
942enable_receive_interrupt enables the receive interrupt
943
944must be called with the lock held and interrupts disabled.
945*/
946static void enable_receive_interrupt(struct xircom_private *card)
947{
948 unsigned int val;
949 enter("enable_receive_interrupt");
950
951 val = inl(card->io_port + CSR7); /* Interrupt enable register */
952 val = val | (1 << 6); /* enable the receive interrupt */
953 outl(val, card->io_port + CSR7);
954
955 leave("enable_receive_interrupt");
956}
957
958/*
959enable_link_interrupt enables the link status change interrupt
960
961must be called with the lock held and interrupts disabled.
962*/
963static void enable_link_interrupt(struct xircom_private *card)
964{
965 unsigned int val;
966 enter("enable_link_interrupt");
967
968 val = inl(card->io_port + CSR7); /* Interrupt enable register */
969 val = val | (1 << 27); /* enable the link status chage interrupt */
970 outl(val, card->io_port + CSR7);
971
972 leave("enable_link_interrupt");
973}
974
975
976
977/*
978disable_all_interrupts disables all interrupts
979
980must be called with the lock held and interrupts disabled.
981*/
982static void disable_all_interrupts(struct xircom_private *card)
983{
984 unsigned int val;
985 enter("enable_all_interrupts");
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 val = 0; /* disable all interrupts */
988 outl(val, card->io_port + CSR7);
989
990 leave("disable_all_interrupts");
991}
992
993/*
994enable_common_interrupts enables several weird interrupts
995
996must be called with the lock held and interrupts disabled.
997*/
998static void enable_common_interrupts(struct xircom_private *card)
999{
1000 unsigned int val;
1001 enter("enable_link_interrupt");
1002
1003 val = inl(card->io_port + CSR7); /* Interrupt enable register */
1004 val |= (1<<16); /* Normal Interrupt Summary */
1005 val |= (1<<15); /* Abnormal Interrupt Summary */
1006 val |= (1<<13); /* Fatal bus error */
1007 val |= (1<<8); /* Receive Process Stopped */
1008 val |= (1<<7); /* Receive Buffer Unavailable */
1009 val |= (1<<5); /* Transmit Underflow */
1010 val |= (1<<2); /* Transmit Buffer Unavailable */
1011 val |= (1<<1); /* Transmit Process Stopped */
1012 outl(val, card->io_port + CSR7);
1013
1014 leave("enable_link_interrupt");
1015}
1016
1017/*
1018enable_promisc starts promisc mode
1019
1020must be called with the lock held and interrupts disabled.
1021*/
1022static int enable_promisc(struct xircom_private *card)
1023{
1024 unsigned int val;
1025 enter("enable_promisc");
1026
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001027 val = inl(card->io_port + CSR6);
1028 val = val | (1 << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 outl(val, card->io_port + CSR6);
1030
1031 leave("enable_promisc");
1032 return 1;
1033}
1034
1035
1036
1037
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001038/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02001039link_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 -07001040
1041Must be called in locked state with interrupts disabled
1042*/
1043static int link_status(struct xircom_private *card)
1044{
1045 unsigned int val;
1046 enter("link_status");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 val = inb(card->io_port + CSR12);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (!(val&(1<<2))) /* bit 2 is 0 for 10mbit link, 1 for not an 10mbit link */
1051 return 10;
1052 if (!(val&(1<<1))) /* bit 1 is 0 for 100mbit link, 1 for not an 100mbit link */
1053 return 100;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001054
1055 /* If we get here -> no link at all */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 leave("link_status");
1058 return 0;
1059}
1060
1061
1062
1063
1064
1065/*
1066 read_mac_address() reads the MAC address from the NIC and stores it in the "dev" structure.
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 This function will take the spinlock itself and can, as a result, not be called with the lock helt.
1069 */
1070static void read_mac_address(struct xircom_private *card)
1071{
1072 unsigned char j, tuple, link, data_id, data_count;
1073 unsigned long flags;
1074 int i;
Joe Perches0795af52007-10-03 17:59:30 -07001075 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 enter("read_mac_address");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 spin_lock_irqsave(&card->lock, flags);
1080
1081 outl(1 << 12, card->io_port + CSR9); /* enable boot rom access */
1082 for (i = 0x100; i < 0x1f7; i += link + 2) {
1083 outl(i, card->io_port + CSR10);
1084 tuple = inl(card->io_port + CSR9) & 0xff;
1085 outl(i + 1, card->io_port + CSR10);
1086 link = inl(card->io_port + CSR9) & 0xff;
1087 outl(i + 2, card->io_port + CSR10);
1088 data_id = inl(card->io_port + CSR9) & 0xff;
1089 outl(i + 3, card->io_port + CSR10);
1090 data_count = inl(card->io_port + CSR9) & 0xff;
1091 if ((tuple == 0x22) && (data_id == 0x04) && (data_count == 0x06)) {
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001092 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 * This is it. We have the data we want.
1094 */
1095 for (j = 0; j < 6; j++) {
1096 outl(i + j + 4, card->io_port + CSR10);
1097 card->dev->dev_addr[j] = inl(card->io_port + CSR9) & 0xff;
1098 }
1099 break;
1100 } else if (link == 0) {
1101 break;
1102 }
1103 }
1104 spin_unlock_irqrestore(&card->lock, flags);
Joe Perches0795af52007-10-03 17:59:30 -07001105 pr_debug(" %s\n", print_mac(mac, card->dev->dev_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 leave("read_mac_address");
1107}
1108
1109
1110/*
1111 transceiver_voodoo() enables the external UTP plug thingy.
1112 it's called voodoo as I stole this code and cannot cross-reference
1113 it with the specification.
1114 */
1115static void transceiver_voodoo(struct xircom_private *card)
1116{
1117 unsigned long flags;
1118
1119 enter("transceiver_voodoo");
1120
1121 /* disable all powermanagement */
1122 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1123
1124 setup_descriptors(card);
1125
1126 spin_lock_irqsave(&card->lock, flags);
1127
1128 outl(0x0008, card->io_port + CSR15);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001129 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 outl(0xa8050000, card->io_port + CSR15);
1131 udelay(25);
1132 outl(0xa00f0000, card->io_port + CSR15);
1133 udelay(25);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 spin_unlock_irqrestore(&card->lock, flags);
1136
1137 netif_start_queue(card->dev);
1138 leave("transceiver_voodoo");
1139}
1140
1141
1142static void xircom_up(struct xircom_private *card)
1143{
1144 unsigned long flags;
1145 int i;
1146
1147 enter("xircom_up");
1148
1149 /* disable all powermanagement */
1150 pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1151
1152 setup_descriptors(card);
1153
1154 spin_lock_irqsave(&card->lock, flags);
1155
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 enable_link_interrupt(card);
1158 enable_transmit_interrupt(card);
1159 enable_receive_interrupt(card);
1160 enable_common_interrupts(card);
1161 enable_promisc(card);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 /* The card can have received packets already, read them away now */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001164 for (i=0;i<NUMDESCRIPTORS;i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 investigate_read_descriptor(card->dev,card,i,bufferoffsets[i]);
1166
1167
1168 spin_unlock_irqrestore(&card->lock, flags);
1169 trigger_receive(card);
1170 trigger_transmit(card);
1171 netif_start_queue(card->dev);
1172 leave("xircom_up");
1173}
1174
1175/* Bufferoffset is in BYTES */
1176static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset)
1177{
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001178 int status;
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 enter("investigate_read_descriptor");
Al Viro6f35d5d2007-12-23 20:01:04 +00001181 status = le32_to_cpu(card->rx_buffer[4*descnr]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if ((status > 0)) { /* packet received */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /* TODO: discard error packets */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 short pkt_len = ((status >> 16) & 0x7ff) - 4; /* minus 4, we don't want the CRC */
1188 struct sk_buff *skb;
1189
1190 if (pkt_len > 1518) {
1191 printk(KERN_ERR "xircom_cb: Packet length %i is bogus \n",pkt_len);
1192 pkt_len = 1518;
1193 }
1194
1195 skb = dev_alloc_skb(pkt_len + 2);
1196 if (skb == NULL) {
1197 card->stats.rx_dropped++;
1198 goto out;
1199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 skb_reserve(skb, 2);
David S. Miller8c7b7fa2007-07-10 22:08:12 -07001201 skb_copy_to_linear_data(skb, (unsigned char*)&card->rx_buffer[bufferoffset / 4], pkt_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 skb_put(skb, pkt_len);
1203 skb->protocol = eth_type_trans(skb, dev);
1204 netif_rx(skb);
1205 dev->last_rx = jiffies;
1206 card->stats.rx_packets++;
1207 card->stats.rx_bytes += pkt_len;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 out:
1210 /* give the buffer back to the card */
Al Viro6f35d5d2007-12-23 20:01:04 +00001211 card->rx_buffer[4*descnr] = cpu_to_le32(0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 trigger_receive(card);
1213 }
1214
1215 leave("investigate_read_descriptor");
1216
1217}
1218
1219
1220/* Bufferoffset is in BYTES */
1221static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset)
1222{
1223 int status;
1224
1225 enter("investigate_write_descriptor");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001226
Al Viro6f35d5d2007-12-23 20:01:04 +00001227 status = le32_to_cpu(card->tx_buffer[4*descnr]);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001228#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (status & 0x8000) { /* Major error */
1230 printk(KERN_ERR "Major transmit error status %x \n", status);
1231 card->tx_buffer[4*descnr] = 0;
1232 netif_wake_queue (dev);
1233 }
1234#endif
1235 if (status > 0) { /* bit 31 is 0 when done */
1236 if (card->tx_skb[descnr]!=NULL) {
1237 card->stats.tx_bytes += card->tx_skb[descnr]->len;
1238 dev_kfree_skb_irq(card->tx_skb[descnr]);
1239 }
1240 card->tx_skb[descnr] = NULL;
1241 /* Bit 8 in the status field is 1 if there was a collision */
1242 if (status&(1<<8))
1243 card->stats.collisions++;
1244 card->tx_buffer[4*descnr] = 0; /* descriptor is free again */
1245 netif_wake_queue (dev);
1246 card->stats.tx_packets++;
1247 }
1248
1249 leave("investigate_write_descriptor");
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
1253
1254static int __init xircom_init(void)
1255{
Alexey Dobriyanec42cdb2006-08-14 23:00:28 -07001256 return pci_register_driver(&xircom_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
1259static void __exit xircom_exit(void)
1260{
1261 pci_unregister_driver(&xircom_ops);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001262}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001264module_init(xircom_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265module_exit(xircom_exit)
1266