1a88394cfSJeff Kirsher /*
2a88394cfSJeff Kirsher  * xircom_cb: A driver for the (tulip-like) Xircom Cardbus ethernet cards
3a88394cfSJeff Kirsher  *
4a88394cfSJeff Kirsher  * This software is (C) by the respective authors, and licensed under the GPL
5a88394cfSJeff Kirsher  * License.
6a88394cfSJeff Kirsher  *
7a88394cfSJeff Kirsher  * Written by Arjan van de Ven for Red Hat, Inc.
8a88394cfSJeff Kirsher  * Based on work by Jeff Garzik, Doug Ledford and Donald Becker
9a88394cfSJeff Kirsher  *
10a88394cfSJeff Kirsher  *  	This software may be used and distributed according to the terms
11a88394cfSJeff Kirsher  *      of the GNU General Public License, incorporated herein by reference.
12a88394cfSJeff Kirsher  *
13a88394cfSJeff Kirsher  *
14a88394cfSJeff Kirsher  * 	$Id: xircom_cb.c,v 1.33 2001/03/19 14:02:07 arjanv Exp $
15a88394cfSJeff Kirsher  */
16a88394cfSJeff Kirsher 
17a88394cfSJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18a88394cfSJeff Kirsher 
19a88394cfSJeff Kirsher #include <linux/module.h>
20a88394cfSJeff Kirsher #include <linux/kernel.h>
21a88394cfSJeff Kirsher #include <linux/string.h>
22a88394cfSJeff Kirsher #include <linux/errno.h>
23a88394cfSJeff Kirsher #include <linux/ioport.h>
24a88394cfSJeff Kirsher #include <linux/slab.h>
25a88394cfSJeff Kirsher #include <linux/interrupt.h>
26a88394cfSJeff Kirsher #include <linux/pci.h>
27a88394cfSJeff Kirsher #include <linux/netdevice.h>
28a88394cfSJeff Kirsher #include <linux/etherdevice.h>
29a88394cfSJeff Kirsher #include <linux/skbuff.h>
30a88394cfSJeff Kirsher #include <linux/delay.h>
31a88394cfSJeff Kirsher #include <linux/init.h>
32a88394cfSJeff Kirsher #include <linux/bitops.h>
33a88394cfSJeff Kirsher 
34a88394cfSJeff Kirsher #include <asm/uaccess.h>
35a88394cfSJeff Kirsher #include <asm/io.h>
36a88394cfSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
37a88394cfSJeff Kirsher #include <asm/irq.h>
38a88394cfSJeff Kirsher #endif
39a88394cfSJeff Kirsher 
40a88394cfSJeff Kirsher MODULE_DESCRIPTION("Xircom Cardbus ethernet driver");
41a88394cfSJeff Kirsher MODULE_AUTHOR("Arjan van de Ven <arjanv@redhat.com>");
42a88394cfSJeff Kirsher MODULE_LICENSE("GPL");
43a88394cfSJeff Kirsher 
44a88394cfSJeff Kirsher 
45a88394cfSJeff Kirsher 
46a88394cfSJeff Kirsher /* IO registers on the card, offsets */
47a88394cfSJeff Kirsher #define CSR0	0x00
48a88394cfSJeff Kirsher #define CSR1	0x08
49a88394cfSJeff Kirsher #define CSR2	0x10
50a88394cfSJeff Kirsher #define CSR3	0x18
51a88394cfSJeff Kirsher #define CSR4	0x20
52a88394cfSJeff Kirsher #define CSR5	0x28
53a88394cfSJeff Kirsher #define CSR6	0x30
54a88394cfSJeff Kirsher #define CSR7	0x38
55a88394cfSJeff Kirsher #define CSR8	0x40
56a88394cfSJeff Kirsher #define CSR9	0x48
57a88394cfSJeff Kirsher #define CSR10	0x50
58a88394cfSJeff Kirsher #define CSR11	0x58
59a88394cfSJeff Kirsher #define CSR12	0x60
60a88394cfSJeff Kirsher #define CSR13	0x68
61a88394cfSJeff Kirsher #define CSR14	0x70
62a88394cfSJeff Kirsher #define CSR15	0x78
63a88394cfSJeff Kirsher #define CSR16	0x80
64a88394cfSJeff Kirsher 
65a88394cfSJeff Kirsher /* PCI registers */
66a88394cfSJeff Kirsher #define PCI_POWERMGMT 	0x40
67a88394cfSJeff Kirsher 
68a88394cfSJeff Kirsher /* Offsets of the buffers within the descriptor pages, in bytes */
69a88394cfSJeff Kirsher 
70a88394cfSJeff Kirsher #define NUMDESCRIPTORS 4
71a88394cfSJeff Kirsher 
72a88394cfSJeff Kirsher static int bufferoffsets[NUMDESCRIPTORS] = {128,2048,4096,6144};
73a88394cfSJeff Kirsher 
74a88394cfSJeff Kirsher 
75a88394cfSJeff Kirsher struct xircom_private {
76a88394cfSJeff Kirsher 	/* Send and receive buffers, kernel-addressable and dma addressable forms */
77a88394cfSJeff Kirsher 
78a88394cfSJeff Kirsher 	__le32 *rx_buffer;
79a88394cfSJeff Kirsher 	__le32 *tx_buffer;
80a88394cfSJeff Kirsher 
81a88394cfSJeff Kirsher 	dma_addr_t rx_dma_handle;
82a88394cfSJeff Kirsher 	dma_addr_t tx_dma_handle;
83a88394cfSJeff Kirsher 
84a88394cfSJeff Kirsher 	struct sk_buff *tx_skb[4];
85a88394cfSJeff Kirsher 
86a88394cfSJeff Kirsher 	unsigned long io_port;
87a88394cfSJeff Kirsher 	int open;
88a88394cfSJeff Kirsher 
89a88394cfSJeff Kirsher 	/* transmit_used is the rotating counter that indicates which transmit
90a88394cfSJeff Kirsher 	   descriptor has to be used next */
91a88394cfSJeff Kirsher 	int transmit_used;
92a88394cfSJeff Kirsher 
93a88394cfSJeff Kirsher 	/* Spinlock to serialize register operations.
94a88394cfSJeff Kirsher 	   It must be helt while manipulating the following registers:
95a88394cfSJeff Kirsher 	   CSR0, CSR6, CSR7, CSR9, CSR10, CSR15
96a88394cfSJeff Kirsher 	 */
97a88394cfSJeff Kirsher 	spinlock_t lock;
98a88394cfSJeff Kirsher 
99a88394cfSJeff Kirsher 	struct pci_dev *pdev;
100a88394cfSJeff Kirsher 	struct net_device *dev;
101a88394cfSJeff Kirsher };
102a88394cfSJeff Kirsher 
103a88394cfSJeff Kirsher 
104a88394cfSJeff Kirsher /* Function prototypes */
105a88394cfSJeff Kirsher static int xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id);
106a88394cfSJeff Kirsher static void xircom_remove(struct pci_dev *pdev);
107a88394cfSJeff Kirsher static irqreturn_t xircom_interrupt(int irq, void *dev_instance);
108a88394cfSJeff Kirsher static netdev_tx_t xircom_start_xmit(struct sk_buff *skb,
109a88394cfSJeff Kirsher 					   struct net_device *dev);
110a88394cfSJeff Kirsher static int xircom_open(struct net_device *dev);
111a88394cfSJeff Kirsher static int xircom_close(struct net_device *dev);
112a88394cfSJeff Kirsher static void xircom_up(struct xircom_private *card);
113a88394cfSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
114a88394cfSJeff Kirsher static void xircom_poll_controller(struct net_device *dev);
115a88394cfSJeff Kirsher #endif
116a88394cfSJeff Kirsher 
117a88394cfSJeff Kirsher static void investigate_read_descriptor(struct net_device *dev,struct xircom_private *card, int descnr, unsigned int bufferoffset);
118a88394cfSJeff Kirsher static void investigate_write_descriptor(struct net_device *dev, struct xircom_private *card, int descnr, unsigned int bufferoffset);
119a88394cfSJeff Kirsher static void read_mac_address(struct xircom_private *card);
120a88394cfSJeff Kirsher static void transceiver_voodoo(struct xircom_private *card);
121a88394cfSJeff Kirsher static void initialize_card(struct xircom_private *card);
122a88394cfSJeff Kirsher static void trigger_transmit(struct xircom_private *card);
123a88394cfSJeff Kirsher static void trigger_receive(struct xircom_private *card);
124a88394cfSJeff Kirsher static void setup_descriptors(struct xircom_private *card);
125a88394cfSJeff Kirsher static void remove_descriptors(struct xircom_private *card);
126a88394cfSJeff Kirsher static int link_status_changed(struct xircom_private *card);
127a88394cfSJeff Kirsher static void activate_receiver(struct xircom_private *card);
128a88394cfSJeff Kirsher static void deactivate_receiver(struct xircom_private *card);
129a88394cfSJeff Kirsher static void activate_transmitter(struct xircom_private *card);
130a88394cfSJeff Kirsher static void deactivate_transmitter(struct xircom_private *card);
131a88394cfSJeff Kirsher static void enable_transmit_interrupt(struct xircom_private *card);
132a88394cfSJeff Kirsher static void enable_receive_interrupt(struct xircom_private *card);
133a88394cfSJeff Kirsher static void enable_link_interrupt(struct xircom_private *card);
134a88394cfSJeff Kirsher static void disable_all_interrupts(struct xircom_private *card);
135a88394cfSJeff Kirsher static int link_status(struct xircom_private *card);
136a88394cfSJeff Kirsher 
137a88394cfSJeff Kirsher 
138a88394cfSJeff Kirsher 
139a88394cfSJeff Kirsher static DEFINE_PCI_DEVICE_TABLE(xircom_pci_table) = {
140a88394cfSJeff Kirsher 	{0x115D, 0x0003, PCI_ANY_ID, PCI_ANY_ID,},
141a88394cfSJeff Kirsher 	{0,},
142a88394cfSJeff Kirsher };
143a88394cfSJeff Kirsher MODULE_DEVICE_TABLE(pci, xircom_pci_table);
144a88394cfSJeff Kirsher 
145a88394cfSJeff Kirsher static struct pci_driver xircom_ops = {
146a88394cfSJeff Kirsher 	.name		= "xircom_cb",
147a88394cfSJeff Kirsher 	.id_table	= xircom_pci_table,
148a88394cfSJeff Kirsher 	.probe		= xircom_probe,
149a88394cfSJeff Kirsher 	.remove		= xircom_remove,
150a88394cfSJeff Kirsher 	.suspend =NULL,
151a88394cfSJeff Kirsher 	.resume =NULL
152a88394cfSJeff Kirsher };
153a88394cfSJeff Kirsher 
154a88394cfSJeff Kirsher 
155a88394cfSJeff Kirsher #if defined DEBUG && DEBUG > 1
156a88394cfSJeff Kirsher static void print_binary(unsigned int number)
157a88394cfSJeff Kirsher {
158a88394cfSJeff Kirsher 	int i,i2;
159a88394cfSJeff Kirsher 	char buffer[64];
160a88394cfSJeff Kirsher 	memset(buffer,0,64);
161a88394cfSJeff Kirsher 	i2=0;
162a88394cfSJeff Kirsher 	for (i=31;i>=0;i--) {
163a88394cfSJeff Kirsher 		if (number & (1<<i))
164a88394cfSJeff Kirsher 			buffer[i2++]='1';
165a88394cfSJeff Kirsher 		else
166a88394cfSJeff Kirsher 			buffer[i2++]='0';
167a88394cfSJeff Kirsher 		if ((i&3)==0)
168a88394cfSJeff Kirsher 			buffer[i2++]=' ';
169a88394cfSJeff Kirsher 	}
170a88394cfSJeff Kirsher 	pr_debug("%s\n",buffer);
171a88394cfSJeff Kirsher }
172a88394cfSJeff Kirsher #endif
173a88394cfSJeff Kirsher 
174a88394cfSJeff Kirsher static const struct net_device_ops netdev_ops = {
175a88394cfSJeff Kirsher 	.ndo_open		= xircom_open,
176a88394cfSJeff Kirsher 	.ndo_stop		= xircom_close,
177a88394cfSJeff Kirsher 	.ndo_start_xmit		= xircom_start_xmit,
178a88394cfSJeff Kirsher 	.ndo_change_mtu		= eth_change_mtu,
179a88394cfSJeff Kirsher 	.ndo_set_mac_address	= eth_mac_addr,
180a88394cfSJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
181a88394cfSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
182a88394cfSJeff Kirsher 	.ndo_poll_controller	= xircom_poll_controller,
183a88394cfSJeff Kirsher #endif
184a88394cfSJeff Kirsher };
185a88394cfSJeff Kirsher 
186a88394cfSJeff Kirsher /* xircom_probe is the code that gets called on device insertion.
187a88394cfSJeff Kirsher    it sets up the hardware and registers the device to the networklayer.
188a88394cfSJeff Kirsher 
189a88394cfSJeff Kirsher    TODO: Send 1 or 2 "dummy" packets here as the card seems to discard the
190a88394cfSJeff Kirsher          first two packets that get send, and pump hates that.
191a88394cfSJeff Kirsher 
192a88394cfSJeff Kirsher  */
193a88394cfSJeff Kirsher static int __devinit xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id)
194a88394cfSJeff Kirsher {
195d59a1881SFrancois Romieu 	struct device *d = &pdev->dev;
196a88394cfSJeff Kirsher 	struct net_device *dev = NULL;
197a88394cfSJeff Kirsher 	struct xircom_private *private;
198a88394cfSJeff Kirsher 	unsigned long flags;
199a88394cfSJeff Kirsher 	unsigned short tmp16;
200d59a1881SFrancois Romieu 	int rc;
201a88394cfSJeff Kirsher 
202a88394cfSJeff Kirsher 	/* First do the PCI initialisation */
203a88394cfSJeff Kirsher 
204d59a1881SFrancois Romieu 	rc = pci_enable_device(pdev);
205d59a1881SFrancois Romieu 	if (rc < 0)
206d59a1881SFrancois Romieu 		goto out;
207a88394cfSJeff Kirsher 
208a88394cfSJeff Kirsher 	/* disable all powermanagement */
209a88394cfSJeff Kirsher 	pci_write_config_dword(pdev, PCI_POWERMGMT, 0x0000);
210a88394cfSJeff Kirsher 
211a88394cfSJeff Kirsher 	pci_set_master(pdev); /* Why isn't this done by pci_enable_device ?*/
212a88394cfSJeff Kirsher 
213a88394cfSJeff Kirsher 	/* clear PCI status, if any */
214a88394cfSJeff Kirsher 	pci_read_config_word (pdev,PCI_STATUS, &tmp16);
215a88394cfSJeff Kirsher 	pci_write_config_word (pdev, PCI_STATUS,tmp16);
216a88394cfSJeff Kirsher 
217d59a1881SFrancois Romieu 	rc = pci_request_regions(pdev, "xircom_cb");
218d59a1881SFrancois Romieu 	if (rc < 0) {
219a88394cfSJeff Kirsher 		pr_err("%s: failed to allocate io-region\n", __func__);
220d59a1881SFrancois Romieu 		goto err_disable;
221a88394cfSJeff Kirsher 	}
222a88394cfSJeff Kirsher 
223d59a1881SFrancois Romieu 	rc = -ENOMEM;
224a88394cfSJeff Kirsher 	/*
225a88394cfSJeff Kirsher 	   Before changing the hardware, allocate the memory.
226a88394cfSJeff Kirsher 	   This way, we can fail gracefully if not enough memory
227a88394cfSJeff Kirsher 	   is available.
228a88394cfSJeff Kirsher 	 */
229a88394cfSJeff Kirsher 	dev = alloc_etherdev(sizeof(struct xircom_private));
23041de8d4cSJoe Perches 	if (!dev)
231d59a1881SFrancois Romieu 		goto err_release;
23241de8d4cSJoe Perches 
233a88394cfSJeff Kirsher 	private = netdev_priv(dev);
234a88394cfSJeff Kirsher 
235a88394cfSJeff Kirsher 	/* Allocate the send/receive buffers */
236d59a1881SFrancois Romieu 	private->rx_buffer = dma_alloc_coherent(d, 8192,
237d59a1881SFrancois Romieu 						&private->rx_dma_handle,
238d59a1881SFrancois Romieu 						GFP_KERNEL);
239a88394cfSJeff Kirsher 	if (private->rx_buffer == NULL) {
240a88394cfSJeff Kirsher 		pr_err("%s: no memory for rx buffer\n", __func__);
241a88394cfSJeff Kirsher 		goto rx_buf_fail;
242a88394cfSJeff Kirsher 	}
243d59a1881SFrancois Romieu 	private->tx_buffer = dma_alloc_coherent(d, 8192,
244d59a1881SFrancois Romieu 						&private->tx_dma_handle,
245d59a1881SFrancois Romieu 						GFP_KERNEL);
246a88394cfSJeff Kirsher 	if (private->tx_buffer == NULL) {
247a88394cfSJeff Kirsher 		pr_err("%s: no memory for tx buffer\n", __func__);
248a88394cfSJeff Kirsher 		goto tx_buf_fail;
249a88394cfSJeff Kirsher 	}
250a88394cfSJeff Kirsher 
251a88394cfSJeff Kirsher 	SET_NETDEV_DEV(dev, &pdev->dev);
252a88394cfSJeff Kirsher 
253a88394cfSJeff Kirsher 
254a88394cfSJeff Kirsher 	private->dev = dev;
255a88394cfSJeff Kirsher 	private->pdev = pdev;
256a88394cfSJeff Kirsher 	private->io_port = pci_resource_start(pdev, 0);
257a88394cfSJeff Kirsher 	spin_lock_init(&private->lock);
258a88394cfSJeff Kirsher 	dev->irq = pdev->irq;
259a88394cfSJeff Kirsher 	dev->base_addr = private->io_port;
260a88394cfSJeff Kirsher 
261a88394cfSJeff Kirsher 	initialize_card(private);
262a88394cfSJeff Kirsher 	read_mac_address(private);
263a88394cfSJeff Kirsher 	setup_descriptors(private);
264a88394cfSJeff Kirsher 
265a88394cfSJeff Kirsher 	dev->netdev_ops = &netdev_ops;
266a88394cfSJeff Kirsher 	pci_set_drvdata(pdev, dev);
267a88394cfSJeff Kirsher 
268d59a1881SFrancois Romieu 	rc = register_netdev(dev);
269d59a1881SFrancois Romieu 	if (rc < 0) {
270a88394cfSJeff Kirsher 		pr_err("%s: netdevice registration failed\n", __func__);
271a88394cfSJeff Kirsher 		goto reg_fail;
272a88394cfSJeff Kirsher 	}
273a88394cfSJeff Kirsher 
274a88394cfSJeff Kirsher 	netdev_info(dev, "Xircom cardbus revision %i at irq %i\n",
275a88394cfSJeff Kirsher 		    pdev->revision, pdev->irq);
276a88394cfSJeff Kirsher 	/* start the transmitter to get a heartbeat */
277a88394cfSJeff Kirsher 	/* TODO: send 2 dummy packets here */
278a88394cfSJeff Kirsher 	transceiver_voodoo(private);
279a88394cfSJeff Kirsher 
280a88394cfSJeff Kirsher 	spin_lock_irqsave(&private->lock,flags);
281a88394cfSJeff Kirsher 	activate_transmitter(private);
282a88394cfSJeff Kirsher 	activate_receiver(private);
283a88394cfSJeff Kirsher 	spin_unlock_irqrestore(&private->lock,flags);
284a88394cfSJeff Kirsher 
285a88394cfSJeff Kirsher 	trigger_receive(private);
286d59a1881SFrancois Romieu out:
287d59a1881SFrancois Romieu 	return rc;
288a88394cfSJeff Kirsher 
289a88394cfSJeff Kirsher reg_fail:
290d59a1881SFrancois Romieu 	pci_set_drvdata(pdev, NULL);
291d59a1881SFrancois Romieu 	dma_free_coherent(d, 8192, private->tx_buffer, private->tx_dma_handle);
292a88394cfSJeff Kirsher tx_buf_fail:
293d59a1881SFrancois Romieu 	dma_free_coherent(d, 8192, private->rx_buffer, private->rx_dma_handle);
294a88394cfSJeff Kirsher rx_buf_fail:
295a88394cfSJeff Kirsher 	free_netdev(dev);
296d59a1881SFrancois Romieu err_release:
297d59a1881SFrancois Romieu 	pci_release_regions(pdev);
298d59a1881SFrancois Romieu err_disable:
299d59a1881SFrancois Romieu 	pci_disable_device(pdev);
300d59a1881SFrancois Romieu 	goto out;
301a88394cfSJeff Kirsher }
302a88394cfSJeff Kirsher 
303a88394cfSJeff Kirsher 
304a88394cfSJeff Kirsher /*
305a88394cfSJeff Kirsher  xircom_remove is called on module-unload or on device-eject.
306a88394cfSJeff Kirsher  it unregisters the irq, io-region and network device.
307a88394cfSJeff Kirsher  Interrupts and such are already stopped in the "ifconfig ethX down"
308a88394cfSJeff Kirsher  code.
309a88394cfSJeff Kirsher  */
310a88394cfSJeff Kirsher static void __devexit xircom_remove(struct pci_dev *pdev)
311a88394cfSJeff Kirsher {
312a88394cfSJeff Kirsher 	struct net_device *dev = pci_get_drvdata(pdev);
313a88394cfSJeff Kirsher 	struct xircom_private *card = netdev_priv(dev);
314d59a1881SFrancois Romieu 	struct device *d = &pdev->dev;
315a88394cfSJeff Kirsher 
316a88394cfSJeff Kirsher 	unregister_netdev(dev);
317a88394cfSJeff Kirsher 	pci_set_drvdata(pdev, NULL);
318d59a1881SFrancois Romieu 	dma_free_coherent(d, 8192, card->tx_buffer, card->tx_dma_handle);
319d59a1881SFrancois Romieu 	dma_free_coherent(d, 8192, card->rx_buffer, card->rx_dma_handle);
320d59a1881SFrancois Romieu 	free_netdev(dev);
321d59a1881SFrancois Romieu 	pci_release_regions(pdev);
322d59a1881SFrancois Romieu 	pci_disable_device(pdev);
323a88394cfSJeff Kirsher }
324a88394cfSJeff Kirsher 
325a88394cfSJeff Kirsher static irqreturn_t xircom_interrupt(int irq, void *dev_instance)
326a88394cfSJeff Kirsher {
327a88394cfSJeff Kirsher 	struct net_device *dev = (struct net_device *) dev_instance;
328a88394cfSJeff Kirsher 	struct xircom_private *card = netdev_priv(dev);
329a88394cfSJeff Kirsher 	unsigned int status;
330a88394cfSJeff Kirsher 	int i;
331a88394cfSJeff Kirsher 
332a88394cfSJeff Kirsher 	spin_lock(&card->lock);
333a88394cfSJeff Kirsher 	status = inl(card->io_port+CSR5);
334a88394cfSJeff Kirsher 
335a88394cfSJeff Kirsher #if defined DEBUG && DEBUG > 1
336a88394cfSJeff Kirsher 	print_binary(status);
337a88394cfSJeff Kirsher 	pr_debug("tx status 0x%08x 0x%08x\n",
338a88394cfSJeff Kirsher 		 card->tx_buffer[0], card->tx_buffer[4]);
339a88394cfSJeff Kirsher 	pr_debug("rx status 0x%08x 0x%08x\n",
340a88394cfSJeff Kirsher 		 card->rx_buffer[0], card->rx_buffer[4]);
341a88394cfSJeff Kirsher #endif
342a88394cfSJeff Kirsher 	/* Handle shared irq and hotplug */
343a88394cfSJeff Kirsher 	if (status == 0 || status == 0xffffffff) {
344a88394cfSJeff Kirsher 		spin_unlock(&card->lock);
345a88394cfSJeff Kirsher 		return IRQ_NONE;
346a88394cfSJeff Kirsher 	}
347a88394cfSJeff Kirsher 
348a88394cfSJeff Kirsher 	if (link_status_changed(card)) {
349a88394cfSJeff Kirsher 		int newlink;
350a88394cfSJeff Kirsher 		netdev_dbg(dev, "Link status has changed\n");
351a88394cfSJeff Kirsher 		newlink = link_status(card);
352a88394cfSJeff Kirsher 		netdev_info(dev, "Link is %d mbit\n", newlink);
353a88394cfSJeff Kirsher 		if (newlink)
354a88394cfSJeff Kirsher 			netif_carrier_on(dev);
355a88394cfSJeff Kirsher 		else
356a88394cfSJeff Kirsher 			netif_carrier_off(dev);
357a88394cfSJeff Kirsher 
358a88394cfSJeff Kirsher 	}
359a88394cfSJeff Kirsher 
360a88394cfSJeff Kirsher 	/* Clear all remaining interrupts */
361a88394cfSJeff Kirsher 	status |= 0xffffffff; /* FIXME: make this clear only the
362a88394cfSJeff Kirsher 				        real existing bits */
363a88394cfSJeff Kirsher 	outl(status,card->io_port+CSR5);
364a88394cfSJeff Kirsher 
365a88394cfSJeff Kirsher 
366a88394cfSJeff Kirsher 	for (i=0;i<NUMDESCRIPTORS;i++)
367a88394cfSJeff Kirsher 		investigate_write_descriptor(dev,card,i,bufferoffsets[i]);
368a88394cfSJeff Kirsher 	for (i=0;i<NUMDESCRIPTORS;i++)
369a88394cfSJeff Kirsher 		investigate_read_descriptor(dev,card,i,bufferoffsets[i]);
370a88394cfSJeff Kirsher 
371a88394cfSJeff Kirsher 	spin_unlock(&card->lock);
372a88394cfSJeff Kirsher 	return IRQ_HANDLED;
373a88394cfSJeff Kirsher }
374a88394cfSJeff Kirsher 
375a88394cfSJeff Kirsher static netdev_tx_t xircom_start_xmit(struct sk_buff *skb,
376a88394cfSJeff Kirsher 					   struct net_device *dev)
377a88394cfSJeff Kirsher {
378a88394cfSJeff Kirsher 	struct xircom_private *card;
379a88394cfSJeff Kirsher 	unsigned long flags;
380a88394cfSJeff Kirsher 	int nextdescriptor;
381a88394cfSJeff Kirsher 	int desc;
382a88394cfSJeff Kirsher 
383a88394cfSJeff Kirsher 	card = netdev_priv(dev);
384a88394cfSJeff Kirsher 	spin_lock_irqsave(&card->lock,flags);
385a88394cfSJeff Kirsher 
386a88394cfSJeff Kirsher 	/* First see if we can free some descriptors */
387a88394cfSJeff Kirsher 	for (desc=0;desc<NUMDESCRIPTORS;desc++)
388a88394cfSJeff Kirsher 		investigate_write_descriptor(dev,card,desc,bufferoffsets[desc]);
389a88394cfSJeff Kirsher 
390a88394cfSJeff Kirsher 
391a88394cfSJeff Kirsher 	nextdescriptor = (card->transmit_used +1) % (NUMDESCRIPTORS);
392a88394cfSJeff Kirsher 	desc = card->transmit_used;
393a88394cfSJeff Kirsher 
394a88394cfSJeff Kirsher 	/* only send the packet if the descriptor is free */
395a88394cfSJeff Kirsher 	if (card->tx_buffer[4*desc]==0) {
396a88394cfSJeff Kirsher 			/* Copy the packet data; zero the memory first as the card
397a88394cfSJeff Kirsher 			   sometimes sends more than you ask it to. */
398a88394cfSJeff Kirsher 
399a88394cfSJeff Kirsher 			memset(&card->tx_buffer[bufferoffsets[desc]/4],0,1536);
400a88394cfSJeff Kirsher 			skb_copy_from_linear_data(skb,
401a88394cfSJeff Kirsher 				  &(card->tx_buffer[bufferoffsets[desc] / 4]),
402a88394cfSJeff Kirsher 						  skb->len);
403a88394cfSJeff Kirsher 			/* FIXME: The specification tells us that the length we send HAS to be a multiple of
404a88394cfSJeff Kirsher 			   4 bytes. */
405a88394cfSJeff Kirsher 
406a88394cfSJeff Kirsher 			card->tx_buffer[4*desc+1] = cpu_to_le32(skb->len);
407a88394cfSJeff Kirsher 			if (desc == NUMDESCRIPTORS - 1) /* bit 25: last descriptor of the ring */
408a88394cfSJeff Kirsher 				card->tx_buffer[4*desc+1] |= cpu_to_le32(1<<25);
409a88394cfSJeff Kirsher 
410a88394cfSJeff Kirsher 			card->tx_buffer[4*desc+1] |= cpu_to_le32(0xF0000000);
411a88394cfSJeff Kirsher 						 /* 0xF0... means want interrupts*/
412a88394cfSJeff Kirsher 			card->tx_skb[desc] = skb;
413a88394cfSJeff Kirsher 
414a88394cfSJeff Kirsher 			wmb();
415a88394cfSJeff Kirsher 			/* This gives the descriptor to the card */
416a88394cfSJeff Kirsher 			card->tx_buffer[4*desc] = cpu_to_le32(0x80000000);
417a88394cfSJeff Kirsher 			trigger_transmit(card);
418a88394cfSJeff Kirsher 			if (card->tx_buffer[nextdescriptor*4] & cpu_to_le32(0x8000000)) {
419a88394cfSJeff Kirsher 				/* next descriptor is occupied... */
420a88394cfSJeff Kirsher 				netif_stop_queue(dev);
421a88394cfSJeff Kirsher 			}
422a88394cfSJeff Kirsher 			card->transmit_used = nextdescriptor;
423a88394cfSJeff Kirsher 			spin_unlock_irqrestore(&card->lock,flags);
424a88394cfSJeff Kirsher 			return NETDEV_TX_OK;
425a88394cfSJeff Kirsher 	}
426a88394cfSJeff Kirsher 
427a88394cfSJeff Kirsher 	/* Uh oh... no free descriptor... drop the packet */
428a88394cfSJeff Kirsher 	netif_stop_queue(dev);
429a88394cfSJeff Kirsher 	spin_unlock_irqrestore(&card->lock,flags);
430a88394cfSJeff Kirsher 	trigger_transmit(card);
431a88394cfSJeff Kirsher 
432a88394cfSJeff Kirsher 	return NETDEV_TX_BUSY;
433a88394cfSJeff Kirsher }
434a88394cfSJeff Kirsher 
435a88394cfSJeff Kirsher 
436a88394cfSJeff Kirsher 
437a88394cfSJeff Kirsher 
438a88394cfSJeff Kirsher static int xircom_open(struct net_device *dev)
439a88394cfSJeff Kirsher {
440a88394cfSJeff Kirsher 	struct xircom_private *xp = netdev_priv(dev);
441a88394cfSJeff Kirsher 	int retval;
442a88394cfSJeff Kirsher 
443a88394cfSJeff Kirsher 	netdev_info(dev, "xircom cardbus adaptor found, using irq %i\n",
444a88394cfSJeff Kirsher 		    dev->irq);
445a88394cfSJeff Kirsher 	retval = request_irq(dev->irq, xircom_interrupt, IRQF_SHARED, dev->name, dev);
446a88394cfSJeff Kirsher 	if (retval)
447a88394cfSJeff Kirsher 		return retval;
448a88394cfSJeff Kirsher 
449a88394cfSJeff Kirsher 	xircom_up(xp);
450a88394cfSJeff Kirsher 	xp->open = 1;
451a88394cfSJeff Kirsher 
452a88394cfSJeff Kirsher 	return 0;
453a88394cfSJeff Kirsher }
454a88394cfSJeff Kirsher 
455a88394cfSJeff Kirsher static int xircom_close(struct net_device *dev)
456a88394cfSJeff Kirsher {
457a88394cfSJeff Kirsher 	struct xircom_private *card;
458a88394cfSJeff Kirsher 	unsigned long flags;
459a88394cfSJeff Kirsher 
460a88394cfSJeff Kirsher 	card = netdev_priv(dev);
461a88394cfSJeff Kirsher 	netif_stop_queue(dev); /* we don't want new packets */
462a88394cfSJeff Kirsher 
463a88394cfSJeff Kirsher 
464a88394cfSJeff Kirsher 	spin_lock_irqsave(&card->lock,flags);
465a88394cfSJeff Kirsher 
466a88394cfSJeff Kirsher 	disable_all_interrupts(card);
467a88394cfSJeff Kirsher #if 0
468a88394cfSJeff Kirsher 	/* We can enable this again once we send dummy packets on ifconfig ethX up */
469a88394cfSJeff Kirsher 	deactivate_receiver(card);
470a88394cfSJeff Kirsher 	deactivate_transmitter(card);
471a88394cfSJeff Kirsher #endif
472a88394cfSJeff Kirsher 	remove_descriptors(card);
473a88394cfSJeff Kirsher 
474a88394cfSJeff Kirsher 	spin_unlock_irqrestore(&card->lock,flags);
475a88394cfSJeff Kirsher 
476a88394cfSJeff Kirsher 	card->open = 0;
477a88394cfSJeff Kirsher 	free_irq(dev->irq,dev);
478a88394cfSJeff Kirsher 
479a88394cfSJeff Kirsher 	return 0;
480a88394cfSJeff Kirsher 
481a88394cfSJeff Kirsher }
482a88394cfSJeff Kirsher 
483a88394cfSJeff Kirsher 
484a88394cfSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
485a88394cfSJeff Kirsher static void xircom_poll_controller(struct net_device *dev)
486a88394cfSJeff Kirsher {
487a88394cfSJeff Kirsher 	disable_irq(dev->irq);
488a88394cfSJeff Kirsher 	xircom_interrupt(dev->irq, dev);
489a88394cfSJeff Kirsher 	enable_irq(dev->irq);
490a88394cfSJeff Kirsher }
491a88394cfSJeff Kirsher #endif
492a88394cfSJeff Kirsher 
493a88394cfSJeff Kirsher 
494a88394cfSJeff Kirsher static void initialize_card(struct xircom_private *card)
495a88394cfSJeff Kirsher {
496a88394cfSJeff Kirsher 	unsigned int val;
497a88394cfSJeff Kirsher 	unsigned long flags;
498a88394cfSJeff Kirsher 
499a88394cfSJeff Kirsher 	spin_lock_irqsave(&card->lock, flags);
500a88394cfSJeff Kirsher 
501a88394cfSJeff Kirsher 	/* First: reset the card */
502a88394cfSJeff Kirsher 	val = inl(card->io_port + CSR0);
503a88394cfSJeff Kirsher 	val |= 0x01;		/* Software reset */
504a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR0);
505a88394cfSJeff Kirsher 
506a88394cfSJeff Kirsher 	udelay(100);		/* give the card some time to reset */
507a88394cfSJeff Kirsher 
508a88394cfSJeff Kirsher 	val = inl(card->io_port + CSR0);
509a88394cfSJeff Kirsher 	val &= ~0x01;		/* disable Software reset */
510a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR0);
511a88394cfSJeff Kirsher 
512a88394cfSJeff Kirsher 
513a88394cfSJeff Kirsher 	val = 0;		/* Value 0x00 is a safe and conservative value
514a88394cfSJeff Kirsher 				   for the PCI configuration settings */
515a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR0);
516a88394cfSJeff Kirsher 
517a88394cfSJeff Kirsher 
518a88394cfSJeff Kirsher 	disable_all_interrupts(card);
519a88394cfSJeff Kirsher 	deactivate_receiver(card);
520a88394cfSJeff Kirsher 	deactivate_transmitter(card);
521a88394cfSJeff Kirsher 
522a88394cfSJeff Kirsher 	spin_unlock_irqrestore(&card->lock, flags);
523a88394cfSJeff Kirsher }
524a88394cfSJeff Kirsher 
525a88394cfSJeff Kirsher /*
526a88394cfSJeff Kirsher trigger_transmit causes the card to check for frames to be transmitted.
527a88394cfSJeff Kirsher This is accomplished by writing to the CSR1 port. The documentation
528a88394cfSJeff Kirsher claims that the act of writing is sufficient and that the value is
529a88394cfSJeff Kirsher ignored; I chose zero.
530a88394cfSJeff Kirsher */
531a88394cfSJeff Kirsher static void trigger_transmit(struct xircom_private *card)
532a88394cfSJeff Kirsher {
533a88394cfSJeff Kirsher 	unsigned int val;
534a88394cfSJeff Kirsher 
535a88394cfSJeff Kirsher 	val = 0;
536a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR1);
537a88394cfSJeff Kirsher }
538a88394cfSJeff Kirsher 
539a88394cfSJeff Kirsher /*
540a88394cfSJeff Kirsher trigger_receive causes the card to check for empty frames in the
541a88394cfSJeff Kirsher descriptor list in which packets can be received.
542a88394cfSJeff Kirsher This is accomplished by writing to the CSR2 port. The documentation
543a88394cfSJeff Kirsher claims that the act of writing is sufficient and that the value is
544a88394cfSJeff Kirsher ignored; I chose zero.
545a88394cfSJeff Kirsher */
546a88394cfSJeff Kirsher static void trigger_receive(struct xircom_private *card)
547a88394cfSJeff Kirsher {
548a88394cfSJeff Kirsher 	unsigned int val;
549a88394cfSJeff Kirsher 
550a88394cfSJeff Kirsher 	val = 0;
551a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR2);
552a88394cfSJeff Kirsher }
553a88394cfSJeff Kirsher 
554a88394cfSJeff Kirsher /*
555a88394cfSJeff Kirsher setup_descriptors initializes the send and receive buffers to be valid
556a88394cfSJeff Kirsher descriptors and programs the addresses into the card.
557a88394cfSJeff Kirsher */
558a88394cfSJeff Kirsher static void setup_descriptors(struct xircom_private *card)
559a88394cfSJeff Kirsher {
560a88394cfSJeff Kirsher 	u32 address;
561a88394cfSJeff Kirsher 	int i;
562a88394cfSJeff Kirsher 
563a88394cfSJeff Kirsher 	BUG_ON(card->rx_buffer == NULL);
564a88394cfSJeff Kirsher 	BUG_ON(card->tx_buffer == NULL);
565a88394cfSJeff Kirsher 
566a88394cfSJeff Kirsher 	/* Receive descriptors */
567a88394cfSJeff Kirsher 	memset(card->rx_buffer, 0, 128);	/* clear the descriptors */
568a88394cfSJeff Kirsher 	for (i=0;i<NUMDESCRIPTORS;i++ ) {
569a88394cfSJeff Kirsher 
570a88394cfSJeff Kirsher 		/* Rx Descr0: It's empty, let the card own it, no errors -> 0x80000000 */
571a88394cfSJeff Kirsher 		card->rx_buffer[i*4 + 0] = cpu_to_le32(0x80000000);
572a88394cfSJeff Kirsher 		/* Rx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
573a88394cfSJeff Kirsher 		card->rx_buffer[i*4 + 1] = cpu_to_le32(1536);
574a88394cfSJeff Kirsher 		if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
575a88394cfSJeff Kirsher 			card->rx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
576a88394cfSJeff Kirsher 
577a88394cfSJeff Kirsher 		/* Rx Descr2: address of the buffer
578a88394cfSJeff Kirsher 		   we store the buffer at the 2nd half of the page */
579a88394cfSJeff Kirsher 
580a88394cfSJeff Kirsher 		address = card->rx_dma_handle;
581a88394cfSJeff Kirsher 		card->rx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
582a88394cfSJeff Kirsher 		/* Rx Desc3: address of 2nd buffer -> 0 */
583a88394cfSJeff Kirsher 		card->rx_buffer[i*4 + 3] = 0;
584a88394cfSJeff Kirsher 	}
585a88394cfSJeff Kirsher 
586a88394cfSJeff Kirsher 	wmb();
587a88394cfSJeff Kirsher 	/* Write the receive descriptor ring address to the card */
588a88394cfSJeff Kirsher 	address = card->rx_dma_handle;
589a88394cfSJeff Kirsher 	outl(address, card->io_port + CSR3);	/* Receive descr list address */
590a88394cfSJeff Kirsher 
591a88394cfSJeff Kirsher 
592a88394cfSJeff Kirsher 	/* transmit descriptors */
593a88394cfSJeff Kirsher 	memset(card->tx_buffer, 0, 128);	/* clear the descriptors */
594a88394cfSJeff Kirsher 
595a88394cfSJeff Kirsher 	for (i=0;i<NUMDESCRIPTORS;i++ ) {
596a88394cfSJeff Kirsher 		/* Tx Descr0: Empty, we own it, no errors -> 0x00000000 */
597a88394cfSJeff Kirsher 		card->tx_buffer[i*4 + 0] = 0x00000000;
598a88394cfSJeff Kirsher 		/* Tx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
599a88394cfSJeff Kirsher 		card->tx_buffer[i*4 + 1] = cpu_to_le32(1536);
600a88394cfSJeff Kirsher 		if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
601a88394cfSJeff Kirsher 			card->tx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
602a88394cfSJeff Kirsher 
603a88394cfSJeff Kirsher 		/* Tx Descr2: address of the buffer
604a88394cfSJeff Kirsher 		   we store the buffer at the 2nd half of the page */
605a88394cfSJeff Kirsher 		address = card->tx_dma_handle;
606a88394cfSJeff Kirsher 		card->tx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
607a88394cfSJeff Kirsher 		/* Tx Desc3: address of 2nd buffer -> 0 */
608a88394cfSJeff Kirsher 		card->tx_buffer[i*4 + 3] = 0;
609a88394cfSJeff Kirsher 	}
610a88394cfSJeff Kirsher 
611a88394cfSJeff Kirsher 	wmb();
612a88394cfSJeff Kirsher 	/* wite the transmit descriptor ring to the card */
613a88394cfSJeff Kirsher 	address = card->tx_dma_handle;
614a88394cfSJeff Kirsher 	outl(address, card->io_port + CSR4);	/* xmit descr list address */
615a88394cfSJeff Kirsher }
616a88394cfSJeff Kirsher 
617a88394cfSJeff Kirsher /*
618a88394cfSJeff Kirsher remove_descriptors informs the card the descriptors are no longer
619a88394cfSJeff Kirsher valid by setting the address in the card to 0x00.
620a88394cfSJeff Kirsher */
621a88394cfSJeff Kirsher static void remove_descriptors(struct xircom_private *card)
622a88394cfSJeff Kirsher {
623a88394cfSJeff Kirsher 	unsigned int val;
624a88394cfSJeff Kirsher 
625a88394cfSJeff Kirsher 	val = 0;
626a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR3);	/* Receive descriptor address */
627a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR4);	/* Send descriptor address */
628a88394cfSJeff Kirsher }
629a88394cfSJeff Kirsher 
630a88394cfSJeff Kirsher /*
631a88394cfSJeff Kirsher link_status_changed returns 1 if the card has indicated that
632a88394cfSJeff Kirsher the link status has changed. The new link status has to be read from CSR12.
633a88394cfSJeff Kirsher 
634a88394cfSJeff Kirsher This function also clears the status-bit.
635a88394cfSJeff Kirsher */
636a88394cfSJeff Kirsher static int link_status_changed(struct xircom_private *card)
637a88394cfSJeff Kirsher {
638a88394cfSJeff Kirsher 	unsigned int val;
639a88394cfSJeff Kirsher 
640a88394cfSJeff Kirsher 	val = inl(card->io_port + CSR5);	/* Status register */
641a88394cfSJeff Kirsher 
642a88394cfSJeff Kirsher 	if ((val & (1 << 27)) == 0)		/* no change */
643a88394cfSJeff Kirsher 		return 0;
644a88394cfSJeff Kirsher 
645a88394cfSJeff Kirsher 	/* clear the event by writing a 1 to the bit in the
646a88394cfSJeff Kirsher 	   status register. */
647a88394cfSJeff Kirsher 	val = (1 << 27);
648a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR5);
649a88394cfSJeff Kirsher 
650a88394cfSJeff Kirsher 	return 1;
651a88394cfSJeff Kirsher }
652a88394cfSJeff Kirsher 
653a88394cfSJeff Kirsher 
654a88394cfSJeff Kirsher /*
655a88394cfSJeff Kirsher transmit_active returns 1 if the transmitter on the card is
656a88394cfSJeff Kirsher in a non-stopped state.
657a88394cfSJeff Kirsher */
658a88394cfSJeff Kirsher static int transmit_active(struct xircom_private *card)
659a88394cfSJeff Kirsher {
660a88394cfSJeff Kirsher 	unsigned int val;
661a88394cfSJeff Kirsher 
662a88394cfSJeff Kirsher 	val = inl(card->io_port + CSR5);	/* Status register */
663a88394cfSJeff Kirsher 
664a88394cfSJeff Kirsher 	if ((val & (7 << 20)) == 0)		/* transmitter disabled */
665a88394cfSJeff Kirsher 		return 0;
666a88394cfSJeff Kirsher 
667a88394cfSJeff Kirsher 	return 1;
668a88394cfSJeff Kirsher }
669a88394cfSJeff Kirsher 
670a88394cfSJeff Kirsher /*
671a88394cfSJeff Kirsher receive_active returns 1 if the receiver on the card is
672a88394cfSJeff Kirsher in a non-stopped state.
673a88394cfSJeff Kirsher */
674a88394cfSJeff Kirsher static int receive_active(struct xircom_private *card)
675a88394cfSJeff Kirsher {
676a88394cfSJeff Kirsher 	unsigned int val;
677a88394cfSJeff Kirsher 
678a88394cfSJeff Kirsher 	val = inl(card->io_port + CSR5);	/* Status register */
679a88394cfSJeff Kirsher 
680a88394cfSJeff Kirsher 	if ((val & (7 << 17)) == 0)		/* receiver disabled */
681a88394cfSJeff Kirsher 		return 0;
682a88394cfSJeff Kirsher 
683a88394cfSJeff Kirsher 	return 1;
684a88394cfSJeff Kirsher }
685a88394cfSJeff Kirsher 
686a88394cfSJeff Kirsher /*
687a88394cfSJeff Kirsher activate_receiver enables the receiver on the card.
688a88394cfSJeff Kirsher Before being allowed to active the receiver, the receiver
689a88394cfSJeff Kirsher must be completely de-activated. To achieve this,
690a88394cfSJeff Kirsher this code actually disables the receiver first; then it waits for the
691a88394cfSJeff Kirsher receiver to become inactive, then it activates the receiver and then
692a88394cfSJeff Kirsher it waits for the receiver to be active.
693a88394cfSJeff Kirsher 
694a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
695a88394cfSJeff Kirsher */
696a88394cfSJeff Kirsher static void activate_receiver(struct xircom_private *card)
697a88394cfSJeff Kirsher {
698a88394cfSJeff Kirsher 	unsigned int val;
699a88394cfSJeff Kirsher 	int counter;
700a88394cfSJeff Kirsher 
701a88394cfSJeff Kirsher 	val = inl(card->io_port + CSR6);	/* Operation mode */
702a88394cfSJeff Kirsher 
703a88394cfSJeff Kirsher 	/* If the "active" bit is set and the receiver is already
704a88394cfSJeff Kirsher 	   active, no need to do the expensive thing */
705a88394cfSJeff Kirsher 	if ((val&2) && (receive_active(card)))
706a88394cfSJeff Kirsher 		return;
707a88394cfSJeff Kirsher 
708a88394cfSJeff Kirsher 
709a88394cfSJeff Kirsher 	val = val & ~2;		/* disable the receiver */
710a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR6);
711a88394cfSJeff Kirsher 
712a88394cfSJeff Kirsher 	counter = 10;
713a88394cfSJeff Kirsher 	while (counter > 0) {
714a88394cfSJeff Kirsher 		if (!receive_active(card))
715a88394cfSJeff Kirsher 			break;
716a88394cfSJeff Kirsher 		/* wait a while */
717a88394cfSJeff Kirsher 		udelay(50);
718a88394cfSJeff Kirsher 		counter--;
719a88394cfSJeff Kirsher 		if (counter <= 0)
720a88394cfSJeff Kirsher 			netdev_err(card->dev, "Receiver failed to deactivate\n");
721a88394cfSJeff Kirsher 	}
722a88394cfSJeff Kirsher 
723a88394cfSJeff Kirsher 	/* enable the receiver */
724a88394cfSJeff Kirsher 	val = inl(card->io_port + CSR6);	/* Operation mode */
725a88394cfSJeff Kirsher 	val = val | 2;				/* enable the receiver */
726a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR6);
727a88394cfSJeff Kirsher 
728a88394cfSJeff Kirsher 	/* now wait for the card to activate again */
729a88394cfSJeff Kirsher 	counter = 10;
730a88394cfSJeff Kirsher 	while (counter > 0) {
731a88394cfSJeff Kirsher 		if (receive_active(card))
732a88394cfSJeff Kirsher 			break;
733a88394cfSJeff Kirsher 		/* wait a while */
734a88394cfSJeff Kirsher 		udelay(50);
735a88394cfSJeff Kirsher 		counter--;
736a88394cfSJeff Kirsher 		if (counter <= 0)
737a88394cfSJeff Kirsher 			netdev_err(card->dev,
738a88394cfSJeff Kirsher 				   "Receiver failed to re-activate\n");
739a88394cfSJeff Kirsher 	}
740a88394cfSJeff Kirsher }
741a88394cfSJeff Kirsher 
742a88394cfSJeff Kirsher /*
743a88394cfSJeff Kirsher deactivate_receiver disables the receiver on the card.
744a88394cfSJeff Kirsher To achieve this this code disables the receiver first;
745a88394cfSJeff Kirsher then it waits for the receiver to become inactive.
746a88394cfSJeff Kirsher 
747a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
748a88394cfSJeff Kirsher */
749a88394cfSJeff Kirsher static void deactivate_receiver(struct xircom_private *card)
750a88394cfSJeff Kirsher {
751a88394cfSJeff Kirsher 	unsigned int val;
752a88394cfSJeff Kirsher 	int counter;
753a88394cfSJeff Kirsher 
754a88394cfSJeff Kirsher 	val = inl(card->io_port + CSR6);	/* Operation mode */
755a88394cfSJeff Kirsher 	val = val & ~2;				/* disable the receiver */
756a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR6);
757a88394cfSJeff Kirsher 
758a88394cfSJeff Kirsher 	counter = 10;
759a88394cfSJeff Kirsher 	while (counter > 0) {
760a88394cfSJeff Kirsher 		if (!receive_active(card))
761a88394cfSJeff Kirsher 			break;
762a88394cfSJeff Kirsher 		/* wait a while */
763a88394cfSJeff Kirsher 		udelay(50);
764a88394cfSJeff Kirsher 		counter--;
765a88394cfSJeff Kirsher 		if (counter <= 0)
766a88394cfSJeff Kirsher 			netdev_err(card->dev, "Receiver failed to deactivate\n");
767a88394cfSJeff Kirsher 	}
768a88394cfSJeff Kirsher }
769a88394cfSJeff Kirsher 
770a88394cfSJeff Kirsher 
771a88394cfSJeff Kirsher /*
772a88394cfSJeff Kirsher activate_transmitter enables the transmitter on the card.
773a88394cfSJeff Kirsher Before being allowed to active the transmitter, the transmitter
774a88394cfSJeff Kirsher must be completely de-activated. To achieve this,
775a88394cfSJeff Kirsher this code actually disables the transmitter first; then it waits for the
776a88394cfSJeff Kirsher transmitter to become inactive, then it activates the transmitter and then
777a88394cfSJeff Kirsher it waits for the transmitter to be active again.
778a88394cfSJeff Kirsher 
779a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
780a88394cfSJeff Kirsher */
781a88394cfSJeff Kirsher static void activate_transmitter(struct xircom_private *card)
782a88394cfSJeff Kirsher {
783a88394cfSJeff Kirsher 	unsigned int val;
784a88394cfSJeff Kirsher 	int counter;
785a88394cfSJeff Kirsher 
786a88394cfSJeff Kirsher 	val = inl(card->io_port + CSR6);	/* Operation mode */
787a88394cfSJeff Kirsher 
788a88394cfSJeff Kirsher 	/* If the "active" bit is set and the receiver is already
789a88394cfSJeff Kirsher 	   active, no need to do the expensive thing */
790a88394cfSJeff Kirsher 	if ((val&(1<<13)) && (transmit_active(card)))
791a88394cfSJeff Kirsher 		return;
792a88394cfSJeff Kirsher 
793a88394cfSJeff Kirsher 	val = val & ~(1 << 13);	/* disable the transmitter */
794a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR6);
795a88394cfSJeff Kirsher 
796a88394cfSJeff Kirsher 	counter = 10;
797a88394cfSJeff Kirsher 	while (counter > 0) {
798a88394cfSJeff Kirsher 		if (!transmit_active(card))
799a88394cfSJeff Kirsher 			break;
800a88394cfSJeff Kirsher 		/* wait a while */
801a88394cfSJeff Kirsher 		udelay(50);
802a88394cfSJeff Kirsher 		counter--;
803a88394cfSJeff Kirsher 		if (counter <= 0)
804a88394cfSJeff Kirsher 			netdev_err(card->dev,
805a88394cfSJeff Kirsher 				   "Transmitter failed to deactivate\n");
806a88394cfSJeff Kirsher 	}
807a88394cfSJeff Kirsher 
808a88394cfSJeff Kirsher 	/* enable the transmitter */
809a88394cfSJeff Kirsher 	val = inl(card->io_port + CSR6);	/* Operation mode */
810a88394cfSJeff Kirsher 	val = val | (1 << 13);	/* enable the transmitter */
811a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR6);
812a88394cfSJeff Kirsher 
813a88394cfSJeff Kirsher 	/* now wait for the card to activate again */
814a88394cfSJeff Kirsher 	counter = 10;
815a88394cfSJeff Kirsher 	while (counter > 0) {
816a88394cfSJeff Kirsher 		if (transmit_active(card))
817a88394cfSJeff Kirsher 			break;
818a88394cfSJeff Kirsher 		/* wait a while */
819a88394cfSJeff Kirsher 		udelay(50);
820a88394cfSJeff Kirsher 		counter--;
821a88394cfSJeff Kirsher 		if (counter <= 0)
822a88394cfSJeff Kirsher 			netdev_err(card->dev,
823a88394cfSJeff Kirsher 				   "Transmitter failed to re-activate\n");
824a88394cfSJeff Kirsher 	}
825a88394cfSJeff Kirsher }
826a88394cfSJeff Kirsher 
827a88394cfSJeff Kirsher /*
828a88394cfSJeff Kirsher deactivate_transmitter disables the transmitter on the card.
829a88394cfSJeff Kirsher To achieve this this code disables the transmitter first;
830a88394cfSJeff Kirsher then it waits for the transmitter to become inactive.
831a88394cfSJeff Kirsher 
832a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
833a88394cfSJeff Kirsher */
834a88394cfSJeff Kirsher static void deactivate_transmitter(struct xircom_private *card)
835a88394cfSJeff Kirsher {
836a88394cfSJeff Kirsher 	unsigned int val;
837a88394cfSJeff Kirsher 	int counter;
838a88394cfSJeff Kirsher 
839a88394cfSJeff Kirsher 	val = inl(card->io_port + CSR6);	/* Operation mode */
840a88394cfSJeff Kirsher 	val = val & ~2;		/* disable the transmitter */
841a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR6);
842a88394cfSJeff Kirsher 
843a88394cfSJeff Kirsher 	counter = 20;
844a88394cfSJeff Kirsher 	while (counter > 0) {
845a88394cfSJeff Kirsher 		if (!transmit_active(card))
846a88394cfSJeff Kirsher 			break;
847a88394cfSJeff Kirsher 		/* wait a while */
848a88394cfSJeff Kirsher 		udelay(50);
849a88394cfSJeff Kirsher 		counter--;
850a88394cfSJeff Kirsher 		if (counter <= 0)
851a88394cfSJeff Kirsher 			netdev_err(card->dev,
852a88394cfSJeff Kirsher 				   "Transmitter failed to deactivate\n");
853a88394cfSJeff Kirsher 	}
854a88394cfSJeff Kirsher }
855a88394cfSJeff Kirsher 
856a88394cfSJeff Kirsher 
857a88394cfSJeff Kirsher /*
858a88394cfSJeff Kirsher enable_transmit_interrupt enables the transmit interrupt
859a88394cfSJeff Kirsher 
860a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
861a88394cfSJeff Kirsher */
862a88394cfSJeff Kirsher static void enable_transmit_interrupt(struct xircom_private *card)
863a88394cfSJeff Kirsher {
864a88394cfSJeff Kirsher 	unsigned int val;
865a88394cfSJeff Kirsher 
866a88394cfSJeff Kirsher 	val = inl(card->io_port + CSR7);	/* Interrupt enable register */
867a88394cfSJeff Kirsher 	val |= 1;				/* enable the transmit interrupt */
868a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR7);
869a88394cfSJeff Kirsher }
870a88394cfSJeff Kirsher 
871a88394cfSJeff Kirsher 
872a88394cfSJeff Kirsher /*
873a88394cfSJeff Kirsher enable_receive_interrupt enables the receive interrupt
874a88394cfSJeff Kirsher 
875a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
876a88394cfSJeff Kirsher */
877a88394cfSJeff Kirsher static void enable_receive_interrupt(struct xircom_private *card)
878a88394cfSJeff Kirsher {
879a88394cfSJeff Kirsher 	unsigned int val;
880a88394cfSJeff Kirsher 
881a88394cfSJeff Kirsher 	val = inl(card->io_port + CSR7);	/* Interrupt enable register */
882a88394cfSJeff Kirsher 	val = val | (1 << 6);			/* enable the receive interrupt */
883a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR7);
884a88394cfSJeff Kirsher }
885a88394cfSJeff Kirsher 
886a88394cfSJeff Kirsher /*
887a88394cfSJeff Kirsher enable_link_interrupt enables the link status change interrupt
888a88394cfSJeff Kirsher 
889a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
890a88394cfSJeff Kirsher */
891a88394cfSJeff Kirsher static void enable_link_interrupt(struct xircom_private *card)
892a88394cfSJeff Kirsher {
893a88394cfSJeff Kirsher 	unsigned int val;
894a88394cfSJeff Kirsher 
895a88394cfSJeff Kirsher 	val = inl(card->io_port + CSR7);	/* Interrupt enable register */
896a88394cfSJeff Kirsher 	val = val | (1 << 27);			/* enable the link status chage interrupt */
897a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR7);
898a88394cfSJeff Kirsher }
899a88394cfSJeff Kirsher 
900a88394cfSJeff Kirsher 
901a88394cfSJeff Kirsher 
902a88394cfSJeff Kirsher /*
903a88394cfSJeff Kirsher disable_all_interrupts disables all interrupts
904a88394cfSJeff Kirsher 
905a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
906a88394cfSJeff Kirsher */
907a88394cfSJeff Kirsher static void disable_all_interrupts(struct xircom_private *card)
908a88394cfSJeff Kirsher {
909a88394cfSJeff Kirsher 	unsigned int val;
910a88394cfSJeff Kirsher 
911a88394cfSJeff Kirsher 	val = 0;				/* disable all interrupts */
912a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR7);
913a88394cfSJeff Kirsher }
914a88394cfSJeff Kirsher 
915a88394cfSJeff Kirsher /*
916a88394cfSJeff Kirsher enable_common_interrupts enables several weird interrupts
917a88394cfSJeff Kirsher 
918a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
919a88394cfSJeff Kirsher */
920a88394cfSJeff Kirsher static void enable_common_interrupts(struct xircom_private *card)
921a88394cfSJeff Kirsher {
922a88394cfSJeff Kirsher 	unsigned int val;
923a88394cfSJeff Kirsher 
924a88394cfSJeff Kirsher 	val = inl(card->io_port + CSR7);	/* Interrupt enable register */
925a88394cfSJeff Kirsher 	val |= (1<<16); /* Normal Interrupt Summary */
926a88394cfSJeff Kirsher 	val |= (1<<15); /* Abnormal Interrupt Summary */
927a88394cfSJeff Kirsher 	val |= (1<<13); /* Fatal bus error */
928a88394cfSJeff Kirsher 	val |= (1<<8);  /* Receive Process Stopped */
929a88394cfSJeff Kirsher 	val |= (1<<7);  /* Receive Buffer Unavailable */
930a88394cfSJeff Kirsher 	val |= (1<<5);  /* Transmit Underflow */
931a88394cfSJeff Kirsher 	val |= (1<<2);  /* Transmit Buffer Unavailable */
932a88394cfSJeff Kirsher 	val |= (1<<1);  /* Transmit Process Stopped */
933a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR7);
934a88394cfSJeff Kirsher }
935a88394cfSJeff Kirsher 
936a88394cfSJeff Kirsher /*
937a88394cfSJeff Kirsher enable_promisc starts promisc mode
938a88394cfSJeff Kirsher 
939a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
940a88394cfSJeff Kirsher */
941a88394cfSJeff Kirsher static int enable_promisc(struct xircom_private *card)
942a88394cfSJeff Kirsher {
943a88394cfSJeff Kirsher 	unsigned int val;
944a88394cfSJeff Kirsher 
945a88394cfSJeff Kirsher 	val = inl(card->io_port + CSR6);
946a88394cfSJeff Kirsher 	val = val | (1 << 6);
947a88394cfSJeff Kirsher 	outl(val, card->io_port + CSR6);
948a88394cfSJeff Kirsher 
949a88394cfSJeff Kirsher 	return 1;
950a88394cfSJeff Kirsher }
951a88394cfSJeff Kirsher 
952a88394cfSJeff Kirsher 
953a88394cfSJeff Kirsher 
954a88394cfSJeff Kirsher 
955a88394cfSJeff Kirsher /*
956a88394cfSJeff Kirsher link_status() checks the links status and will return 0 for no link, 10 for 10mbit link and 100 for.. guess what.
957a88394cfSJeff Kirsher 
958a88394cfSJeff Kirsher Must be called in locked state with interrupts disabled
959a88394cfSJeff Kirsher */
960a88394cfSJeff Kirsher static int link_status(struct xircom_private *card)
961a88394cfSJeff Kirsher {
962a88394cfSJeff Kirsher 	unsigned int val;
963a88394cfSJeff Kirsher 
964a88394cfSJeff Kirsher 	val = inb(card->io_port + CSR12);
965a88394cfSJeff Kirsher 
966a88394cfSJeff Kirsher 	if (!(val&(1<<2)))  /* bit 2 is 0 for 10mbit link, 1 for not an 10mbit link */
967a88394cfSJeff Kirsher 		return 10;
968a88394cfSJeff Kirsher 	if (!(val&(1<<1)))  /* bit 1 is 0 for 100mbit link, 1 for not an 100mbit link */
969a88394cfSJeff Kirsher 		return 100;
970a88394cfSJeff Kirsher 
971a88394cfSJeff Kirsher 	/* If we get here -> no link at all */
972a88394cfSJeff Kirsher 
973a88394cfSJeff Kirsher 	return 0;
974a88394cfSJeff Kirsher }
975a88394cfSJeff Kirsher 
976a88394cfSJeff Kirsher 
977a88394cfSJeff Kirsher 
978a88394cfSJeff Kirsher 
979a88394cfSJeff Kirsher 
980a88394cfSJeff Kirsher /*
981a88394cfSJeff Kirsher   read_mac_address() reads the MAC address from the NIC and stores it in the "dev" structure.
982a88394cfSJeff Kirsher 
983a88394cfSJeff Kirsher   This function will take the spinlock itself and can, as a result, not be called with the lock helt.
984a88394cfSJeff Kirsher  */
985a88394cfSJeff Kirsher static void read_mac_address(struct xircom_private *card)
986a88394cfSJeff Kirsher {
987a88394cfSJeff Kirsher 	unsigned char j, tuple, link, data_id, data_count;
988a88394cfSJeff Kirsher 	unsigned long flags;
989a88394cfSJeff Kirsher 	int i;
990a88394cfSJeff Kirsher 
991a88394cfSJeff Kirsher 	spin_lock_irqsave(&card->lock, flags);
992a88394cfSJeff Kirsher 
993a88394cfSJeff Kirsher 	outl(1 << 12, card->io_port + CSR9);	/* enable boot rom access */
994a88394cfSJeff Kirsher 	for (i = 0x100; i < 0x1f7; i += link + 2) {
995a88394cfSJeff Kirsher 		outl(i, card->io_port + CSR10);
996a88394cfSJeff Kirsher 		tuple = inl(card->io_port + CSR9) & 0xff;
997a88394cfSJeff Kirsher 		outl(i + 1, card->io_port + CSR10);
998a88394cfSJeff Kirsher 		link = inl(card->io_port + CSR9) & 0xff;
999a88394cfSJeff Kirsher 		outl(i + 2, card->io_port + CSR10);
1000a88394cfSJeff Kirsher 		data_id = inl(card->io_port + CSR9) & 0xff;
1001a88394cfSJeff Kirsher 		outl(i + 3, card->io_port + CSR10);
1002a88394cfSJeff Kirsher 		data_count = inl(card->io_port + CSR9) & 0xff;
1003a88394cfSJeff Kirsher 		if ((tuple == 0x22) && (data_id == 0x04) && (data_count == 0x06)) {
1004a88394cfSJeff Kirsher 			/*
1005a88394cfSJeff Kirsher 			 * This is it.  We have the data we want.
1006a88394cfSJeff Kirsher 			 */
1007a88394cfSJeff Kirsher 			for (j = 0; j < 6; j++) {
1008a88394cfSJeff Kirsher 				outl(i + j + 4, card->io_port + CSR10);
1009a88394cfSJeff Kirsher 				card->dev->dev_addr[j] = inl(card->io_port + CSR9) & 0xff;
1010a88394cfSJeff Kirsher 			}
1011a88394cfSJeff Kirsher 			break;
1012a88394cfSJeff Kirsher 		} else if (link == 0) {
1013a88394cfSJeff Kirsher 			break;
1014a88394cfSJeff Kirsher 		}
1015a88394cfSJeff Kirsher 	}
1016a88394cfSJeff Kirsher 	spin_unlock_irqrestore(&card->lock, flags);
1017a88394cfSJeff Kirsher 	pr_debug(" %pM\n", card->dev->dev_addr);
1018a88394cfSJeff Kirsher }
1019a88394cfSJeff Kirsher 
1020a88394cfSJeff Kirsher 
1021a88394cfSJeff Kirsher /*
1022a88394cfSJeff Kirsher  transceiver_voodoo() enables the external UTP plug thingy.
1023a88394cfSJeff Kirsher  it's called voodoo as I stole this code and cannot cross-reference
1024a88394cfSJeff Kirsher  it with the specification.
1025a88394cfSJeff Kirsher  */
1026a88394cfSJeff Kirsher static void transceiver_voodoo(struct xircom_private *card)
1027a88394cfSJeff Kirsher {
1028a88394cfSJeff Kirsher 	unsigned long flags;
1029a88394cfSJeff Kirsher 
1030a88394cfSJeff Kirsher 	/* disable all powermanagement */
1031a88394cfSJeff Kirsher 	pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1032a88394cfSJeff Kirsher 
1033a88394cfSJeff Kirsher 	setup_descriptors(card);
1034a88394cfSJeff Kirsher 
1035a88394cfSJeff Kirsher 	spin_lock_irqsave(&card->lock, flags);
1036a88394cfSJeff Kirsher 
1037a88394cfSJeff Kirsher 	outl(0x0008, card->io_port + CSR15);
1038a88394cfSJeff Kirsher         udelay(25);
1039a88394cfSJeff Kirsher         outl(0xa8050000, card->io_port + CSR15);
1040a88394cfSJeff Kirsher         udelay(25);
1041a88394cfSJeff Kirsher         outl(0xa00f0000, card->io_port + CSR15);
1042a88394cfSJeff Kirsher         udelay(25);
1043a88394cfSJeff Kirsher 
1044a88394cfSJeff Kirsher         spin_unlock_irqrestore(&card->lock, flags);
1045a88394cfSJeff Kirsher 
1046a88394cfSJeff Kirsher 	netif_start_queue(card->dev);
1047a88394cfSJeff Kirsher }
1048a88394cfSJeff Kirsher 
1049a88394cfSJeff Kirsher 
1050a88394cfSJeff Kirsher static void xircom_up(struct xircom_private *card)
1051a88394cfSJeff Kirsher {
1052a88394cfSJeff Kirsher 	unsigned long flags;
1053a88394cfSJeff Kirsher 	int i;
1054a88394cfSJeff Kirsher 
1055a88394cfSJeff Kirsher 	/* disable all powermanagement */
1056a88394cfSJeff Kirsher 	pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1057a88394cfSJeff Kirsher 
1058a88394cfSJeff Kirsher 	setup_descriptors(card);
1059a88394cfSJeff Kirsher 
1060a88394cfSJeff Kirsher 	spin_lock_irqsave(&card->lock, flags);
1061a88394cfSJeff Kirsher 
1062a88394cfSJeff Kirsher 
1063a88394cfSJeff Kirsher 	enable_link_interrupt(card);
1064a88394cfSJeff Kirsher 	enable_transmit_interrupt(card);
1065a88394cfSJeff Kirsher 	enable_receive_interrupt(card);
1066a88394cfSJeff Kirsher 	enable_common_interrupts(card);
1067a88394cfSJeff Kirsher 	enable_promisc(card);
1068a88394cfSJeff Kirsher 
1069a88394cfSJeff Kirsher 	/* The card can have received packets already, read them away now */
1070a88394cfSJeff Kirsher 	for (i=0;i<NUMDESCRIPTORS;i++)
1071a88394cfSJeff Kirsher 		investigate_read_descriptor(card->dev,card,i,bufferoffsets[i]);
1072a88394cfSJeff Kirsher 
1073a88394cfSJeff Kirsher 
1074a88394cfSJeff Kirsher 	spin_unlock_irqrestore(&card->lock, flags);
1075a88394cfSJeff Kirsher 	trigger_receive(card);
1076a88394cfSJeff Kirsher 	trigger_transmit(card);
1077a88394cfSJeff Kirsher 	netif_start_queue(card->dev);
1078a88394cfSJeff Kirsher }
1079a88394cfSJeff Kirsher 
1080a88394cfSJeff Kirsher /* Bufferoffset is in BYTES */
1081a88394cfSJeff Kirsher static void
1082a88394cfSJeff Kirsher investigate_read_descriptor(struct net_device *dev, struct xircom_private *card,
1083a88394cfSJeff Kirsher 			    int descnr, unsigned int bufferoffset)
1084a88394cfSJeff Kirsher {
1085a88394cfSJeff Kirsher 	int status;
1086a88394cfSJeff Kirsher 
1087a88394cfSJeff Kirsher 	status = le32_to_cpu(card->rx_buffer[4*descnr]);
1088a88394cfSJeff Kirsher 
1089a88394cfSJeff Kirsher 	if (status > 0) {		/* packet received */
1090a88394cfSJeff Kirsher 
1091a88394cfSJeff Kirsher 		/* TODO: discard error packets */
1092a88394cfSJeff Kirsher 
1093a88394cfSJeff Kirsher 		short pkt_len = ((status >> 16) & 0x7ff) - 4;
1094a88394cfSJeff Kirsher 					/* minus 4, we don't want the CRC */
1095a88394cfSJeff Kirsher 		struct sk_buff *skb;
1096a88394cfSJeff Kirsher 
1097a88394cfSJeff Kirsher 		if (pkt_len > 1518) {
1098a88394cfSJeff Kirsher 			netdev_err(dev, "Packet length %i is bogus\n", pkt_len);
1099a88394cfSJeff Kirsher 			pkt_len = 1518;
1100a88394cfSJeff Kirsher 		}
1101a88394cfSJeff Kirsher 
110221a4e469SPradeep A Dalvi 		skb = netdev_alloc_skb(dev, pkt_len + 2);
1103a88394cfSJeff Kirsher 		if (skb == NULL) {
1104a88394cfSJeff Kirsher 			dev->stats.rx_dropped++;
1105a88394cfSJeff Kirsher 			goto out;
1106a88394cfSJeff Kirsher 		}
1107a88394cfSJeff Kirsher 		skb_reserve(skb, 2);
1108a88394cfSJeff Kirsher 		skb_copy_to_linear_data(skb,
1109a88394cfSJeff Kirsher 					&card->rx_buffer[bufferoffset / 4],
1110a88394cfSJeff Kirsher 					pkt_len);
1111a88394cfSJeff Kirsher 		skb_put(skb, pkt_len);
1112a88394cfSJeff Kirsher 		skb->protocol = eth_type_trans(skb, dev);
1113a88394cfSJeff Kirsher 		netif_rx(skb);
1114a88394cfSJeff Kirsher 		dev->stats.rx_packets++;
1115a88394cfSJeff Kirsher 		dev->stats.rx_bytes += pkt_len;
1116a88394cfSJeff Kirsher 
1117a88394cfSJeff Kirsher out:
1118a88394cfSJeff Kirsher 		/* give the buffer back to the card */
1119a88394cfSJeff Kirsher 		card->rx_buffer[4*descnr] = cpu_to_le32(0x80000000);
1120a88394cfSJeff Kirsher 		trigger_receive(card);
1121a88394cfSJeff Kirsher 	}
1122a88394cfSJeff Kirsher }
1123a88394cfSJeff Kirsher 
1124a88394cfSJeff Kirsher 
1125a88394cfSJeff Kirsher /* Bufferoffset is in BYTES */
1126a88394cfSJeff Kirsher static void
1127a88394cfSJeff Kirsher investigate_write_descriptor(struct net_device *dev,
1128a88394cfSJeff Kirsher 			     struct xircom_private *card,
1129a88394cfSJeff Kirsher 			     int descnr, unsigned int bufferoffset)
1130a88394cfSJeff Kirsher {
1131a88394cfSJeff Kirsher 	int status;
1132a88394cfSJeff Kirsher 
1133a88394cfSJeff Kirsher 	status = le32_to_cpu(card->tx_buffer[4*descnr]);
1134a88394cfSJeff Kirsher #if 0
1135a88394cfSJeff Kirsher 	if (status & 0x8000) {	/* Major error */
1136a88394cfSJeff Kirsher 		pr_err("Major transmit error status %x\n", status);
1137a88394cfSJeff Kirsher 		card->tx_buffer[4*descnr] = 0;
1138a88394cfSJeff Kirsher 		netif_wake_queue (dev);
1139a88394cfSJeff Kirsher 	}
1140a88394cfSJeff Kirsher #endif
1141a88394cfSJeff Kirsher 	if (status > 0) {	/* bit 31 is 0 when done */
1142a88394cfSJeff Kirsher 		if (card->tx_skb[descnr]!=NULL) {
1143a88394cfSJeff Kirsher 			dev->stats.tx_bytes += card->tx_skb[descnr]->len;
1144a88394cfSJeff Kirsher 			dev_kfree_skb_irq(card->tx_skb[descnr]);
1145a88394cfSJeff Kirsher 		}
1146a88394cfSJeff Kirsher 		card->tx_skb[descnr] = NULL;
1147a88394cfSJeff Kirsher 		/* Bit 8 in the status field is 1 if there was a collision */
1148a88394cfSJeff Kirsher 		if (status & (1 << 8))
1149a88394cfSJeff Kirsher 			dev->stats.collisions++;
1150a88394cfSJeff Kirsher 		card->tx_buffer[4*descnr] = 0; /* descriptor is free again */
1151a88394cfSJeff Kirsher 		netif_wake_queue (dev);
1152a88394cfSJeff Kirsher 		dev->stats.tx_packets++;
1153a88394cfSJeff Kirsher 	}
1154a88394cfSJeff Kirsher }
1155a88394cfSJeff Kirsher 
1156a88394cfSJeff Kirsher static int __init xircom_init(void)
1157a88394cfSJeff Kirsher {
1158a88394cfSJeff Kirsher 	return pci_register_driver(&xircom_ops);
1159a88394cfSJeff Kirsher }
1160a88394cfSJeff Kirsher 
1161a88394cfSJeff Kirsher static void __exit xircom_exit(void)
1162a88394cfSJeff Kirsher {
1163a88394cfSJeff Kirsher 	pci_unregister_driver(&xircom_ops);
1164a88394cfSJeff Kirsher }
1165a88394cfSJeff Kirsher 
1166a88394cfSJeff Kirsher module_init(xircom_init)
1167a88394cfSJeff Kirsher module_exit(xircom_exit)
1168a88394cfSJeff Kirsher 
1169