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