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 
337c0f6ba6SLinus Torvalds #include <linux/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
print_binary(unsigned int number)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_set_mac_address	= eth_mac_addr,
178a88394cfSJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
179a88394cfSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
180a88394cfSJeff Kirsher 	.ndo_poll_controller	= xircom_poll_controller,
181a88394cfSJeff Kirsher #endif
182a88394cfSJeff Kirsher };
183a88394cfSJeff Kirsher 
184a88394cfSJeff Kirsher /* xircom_probe is the code that gets called on device insertion.
185a88394cfSJeff Kirsher    it sets up the hardware and registers the device to the networklayer.
186a88394cfSJeff Kirsher 
187a88394cfSJeff Kirsher    TODO: Send 1 or 2 "dummy" packets here as the card seems to discard the
188a88394cfSJeff Kirsher          first two packets that get send, and pump hates that.
189a88394cfSJeff Kirsher 
190a88394cfSJeff Kirsher  */
xircom_probe(struct pci_dev * pdev,const struct pci_device_id * id)191779c1a85SBill Pemberton static int xircom_probe(struct pci_dev *pdev, const struct pci_device_id *id)
192a88394cfSJeff Kirsher {
193d59a1881SFrancois Romieu 	struct device *d = &pdev->dev;
194a88394cfSJeff Kirsher 	struct net_device *dev = NULL;
195a88394cfSJeff Kirsher 	struct xircom_private *private;
196a88394cfSJeff Kirsher 	unsigned long flags;
197a88394cfSJeff Kirsher 	unsigned short tmp16;
198d59a1881SFrancois Romieu 	int rc;
199a88394cfSJeff Kirsher 
200a88394cfSJeff Kirsher 	/* First do the PCI initialisation */
201a88394cfSJeff Kirsher 
202d59a1881SFrancois Romieu 	rc = pci_enable_device(pdev);
203d59a1881SFrancois Romieu 	if (rc < 0)
204d59a1881SFrancois Romieu 		goto out;
205a88394cfSJeff Kirsher 
206a88394cfSJeff Kirsher 	/* disable all powermanagement */
207a88394cfSJeff Kirsher 	pci_write_config_dword(pdev, PCI_POWERMGMT, 0x0000);
208a88394cfSJeff Kirsher 
209a88394cfSJeff Kirsher 	pci_set_master(pdev); /* Why isn't this done by pci_enable_device ?*/
210a88394cfSJeff Kirsher 
211a88394cfSJeff Kirsher 	/* clear PCI status, if any */
212a88394cfSJeff Kirsher 	pci_read_config_word (pdev,PCI_STATUS, &tmp16);
213a88394cfSJeff Kirsher 	pci_write_config_word (pdev, PCI_STATUS,tmp16);
214a88394cfSJeff Kirsher 
215d59a1881SFrancois Romieu 	rc = pci_request_regions(pdev, "xircom_cb");
216d59a1881SFrancois Romieu 	if (rc < 0) {
217a88394cfSJeff Kirsher 		pr_err("%s: failed to allocate io-region\n", __func__);
218d59a1881SFrancois Romieu 		goto err_disable;
219a88394cfSJeff Kirsher 	}
220a88394cfSJeff Kirsher 
221d59a1881SFrancois Romieu 	rc = -ENOMEM;
222a88394cfSJeff Kirsher 	/*
223a88394cfSJeff Kirsher 	   Before changing the hardware, allocate the memory.
224a88394cfSJeff Kirsher 	   This way, we can fail gracefully if not enough memory
225a88394cfSJeff Kirsher 	   is available.
226a88394cfSJeff Kirsher 	 */
227a88394cfSJeff Kirsher 	dev = alloc_etherdev(sizeof(struct xircom_private));
22841de8d4cSJoe Perches 	if (!dev)
229d59a1881SFrancois Romieu 		goto err_release;
23041de8d4cSJoe Perches 
231a88394cfSJeff Kirsher 	private = netdev_priv(dev);
232a88394cfSJeff Kirsher 
233a88394cfSJeff Kirsher 	/* Allocate the send/receive buffers */
234d59a1881SFrancois Romieu 	private->rx_buffer = dma_alloc_coherent(d, 8192,
235d59a1881SFrancois Romieu 						&private->rx_dma_handle,
236d59a1881SFrancois Romieu 						GFP_KERNEL);
237d0320f75SJoe Perches 	if (private->rx_buffer == NULL)
238a88394cfSJeff Kirsher 		goto rx_buf_fail;
239d0320f75SJoe Perches 
240d59a1881SFrancois Romieu 	private->tx_buffer = dma_alloc_coherent(d, 8192,
241d59a1881SFrancois Romieu 						&private->tx_dma_handle,
242d59a1881SFrancois Romieu 						GFP_KERNEL);
243d0320f75SJoe Perches 	if (private->tx_buffer == NULL)
244a88394cfSJeff Kirsher 		goto tx_buf_fail;
245a88394cfSJeff Kirsher 
246a88394cfSJeff Kirsher 	SET_NETDEV_DEV(dev, &pdev->dev);
247a88394cfSJeff Kirsher 
248a88394cfSJeff Kirsher 
249a88394cfSJeff Kirsher 	private->dev = dev;
250a88394cfSJeff Kirsher 	private->pdev = pdev;
251ebaf7f8fSFrancois Romieu 
252ebaf7f8fSFrancois Romieu 	/* IO range. */
253ebaf7f8fSFrancois Romieu 	private->ioaddr = pci_iomap(pdev, 0, 0);
254ebaf7f8fSFrancois Romieu 	if (!private->ioaddr)
255ebaf7f8fSFrancois Romieu 		goto reg_fail;
256ebaf7f8fSFrancois Romieu 
257a88394cfSJeff Kirsher 	spin_lock_init(&private->lock);
258a88394cfSJeff Kirsher 
259a88394cfSJeff Kirsher 	initialize_card(private);
260a88394cfSJeff Kirsher 	read_mac_address(private);
261a88394cfSJeff Kirsher 	setup_descriptors(private);
262a88394cfSJeff Kirsher 
263a88394cfSJeff Kirsher 	dev->netdev_ops = &netdev_ops;
264a88394cfSJeff Kirsher 	pci_set_drvdata(pdev, dev);
265a88394cfSJeff Kirsher 
266d59a1881SFrancois Romieu 	rc = register_netdev(dev);
267d59a1881SFrancois Romieu 	if (rc < 0) {
268a88394cfSJeff Kirsher 		pr_err("%s: netdevice registration failed\n", __func__);
269ebaf7f8fSFrancois Romieu 		goto err_unmap;
270a88394cfSJeff Kirsher 	}
271a88394cfSJeff Kirsher 
272a88394cfSJeff Kirsher 	netdev_info(dev, "Xircom cardbus revision %i at irq %i\n",
273a88394cfSJeff Kirsher 		    pdev->revision, pdev->irq);
274a88394cfSJeff Kirsher 	/* start the transmitter to get a heartbeat */
275a88394cfSJeff Kirsher 	/* TODO: send 2 dummy packets here */
276a88394cfSJeff Kirsher 	transceiver_voodoo(private);
277a88394cfSJeff Kirsher 
278a88394cfSJeff Kirsher 	spin_lock_irqsave(&private->lock,flags);
279a88394cfSJeff Kirsher 	activate_transmitter(private);
280a88394cfSJeff Kirsher 	activate_receiver(private);
281a88394cfSJeff Kirsher 	spin_unlock_irqrestore(&private->lock,flags);
282a88394cfSJeff Kirsher 
283a88394cfSJeff Kirsher 	trigger_receive(private);
284d59a1881SFrancois Romieu out:
285d59a1881SFrancois Romieu 	return rc;
286a88394cfSJeff Kirsher 
287ebaf7f8fSFrancois Romieu err_unmap:
288ebaf7f8fSFrancois Romieu 	pci_iounmap(pdev, private->ioaddr);
289a88394cfSJeff Kirsher reg_fail:
290d59a1881SFrancois Romieu 	dma_free_coherent(d, 8192, private->tx_buffer, private->tx_dma_handle);
291a88394cfSJeff Kirsher tx_buf_fail:
292d59a1881SFrancois Romieu 	dma_free_coherent(d, 8192, private->rx_buffer, private->rx_dma_handle);
293a88394cfSJeff Kirsher rx_buf_fail:
294a88394cfSJeff Kirsher 	free_netdev(dev);
295d59a1881SFrancois Romieu err_release:
296d59a1881SFrancois Romieu 	pci_release_regions(pdev);
297d59a1881SFrancois Romieu err_disable:
298d59a1881SFrancois Romieu 	pci_disable_device(pdev);
299d59a1881SFrancois Romieu 	goto out;
300a88394cfSJeff Kirsher }
301a88394cfSJeff Kirsher 
302a88394cfSJeff Kirsher 
303a88394cfSJeff Kirsher /*
304a88394cfSJeff Kirsher  xircom_remove is called on module-unload or on device-eject.
305a88394cfSJeff Kirsher  it unregisters the irq, io-region and network device.
306a88394cfSJeff Kirsher  Interrupts and such are already stopped in the "ifconfig ethX down"
307a88394cfSJeff Kirsher  code.
308a88394cfSJeff Kirsher  */
xircom_remove(struct pci_dev * pdev)309779c1a85SBill Pemberton static void xircom_remove(struct pci_dev *pdev)
310a88394cfSJeff Kirsher {
311a88394cfSJeff Kirsher 	struct net_device *dev = pci_get_drvdata(pdev);
312a88394cfSJeff Kirsher 	struct xircom_private *card = netdev_priv(dev);
313d59a1881SFrancois Romieu 	struct device *d = &pdev->dev;
314a88394cfSJeff Kirsher 
315a88394cfSJeff Kirsher 	unregister_netdev(dev);
316ebaf7f8fSFrancois Romieu 	pci_iounmap(pdev, card->ioaddr);
317d59a1881SFrancois Romieu 	dma_free_coherent(d, 8192, card->tx_buffer, card->tx_dma_handle);
318d59a1881SFrancois Romieu 	dma_free_coherent(d, 8192, card->rx_buffer, card->rx_dma_handle);
319d59a1881SFrancois Romieu 	free_netdev(dev);
320d59a1881SFrancois Romieu 	pci_release_regions(pdev);
321d59a1881SFrancois Romieu 	pci_disable_device(pdev);
322a88394cfSJeff Kirsher }
323a88394cfSJeff Kirsher 
xircom_interrupt(int irq,void * dev_instance)324a88394cfSJeff Kirsher static irqreturn_t xircom_interrupt(int irq, void *dev_instance)
325a88394cfSJeff Kirsher {
326a88394cfSJeff Kirsher 	struct net_device *dev = (struct net_device *) dev_instance;
327a88394cfSJeff Kirsher 	struct xircom_private *card = netdev_priv(dev);
328ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
329a88394cfSJeff Kirsher 	unsigned int status;
330a88394cfSJeff Kirsher 	int i;
331a88394cfSJeff Kirsher 
332a88394cfSJeff Kirsher 	spin_lock(&card->lock);
333ebaf7f8fSFrancois Romieu 	status = xr32(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 */
363ebaf7f8fSFrancois Romieu 	xw32(CSR5, status);
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 
xircom_start_xmit(struct sk_buff * skb,struct net_device * dev)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 
xircom_open(struct net_device * dev)438a88394cfSJeff Kirsher static int xircom_open(struct net_device *dev)
439a88394cfSJeff Kirsher {
440a88394cfSJeff Kirsher 	struct xircom_private *xp = netdev_priv(dev);
441ebaf7f8fSFrancois Romieu 	const int irq = xp->pdev->irq;
442a88394cfSJeff Kirsher 	int retval;
443a88394cfSJeff Kirsher 
444ebaf7f8fSFrancois Romieu 	netdev_info(dev, "xircom cardbus adaptor found, using irq %i\n", irq);
445ebaf7f8fSFrancois Romieu 	retval = request_irq(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 
xircom_close(struct net_device * dev)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;
477ebaf7f8fSFrancois Romieu 	free_irq(card->pdev->irq, dev);
478a88394cfSJeff Kirsher 
479a88394cfSJeff Kirsher 	return 0;
480a88394cfSJeff Kirsher 
481a88394cfSJeff Kirsher }
482a88394cfSJeff Kirsher 
483a88394cfSJeff Kirsher 
484a88394cfSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
xircom_poll_controller(struct net_device * dev)485a88394cfSJeff Kirsher static void xircom_poll_controller(struct net_device *dev)
486a88394cfSJeff Kirsher {
487ebaf7f8fSFrancois Romieu 	struct xircom_private *xp = netdev_priv(dev);
488ebaf7f8fSFrancois Romieu 	const int irq = xp->pdev->irq;
489ebaf7f8fSFrancois Romieu 
490ebaf7f8fSFrancois Romieu 	disable_irq(irq);
491ebaf7f8fSFrancois Romieu 	xircom_interrupt(irq, dev);
492ebaf7f8fSFrancois Romieu 	enable_irq(irq);
493a88394cfSJeff Kirsher }
494a88394cfSJeff Kirsher #endif
495a88394cfSJeff Kirsher 
496a88394cfSJeff Kirsher 
initialize_card(struct xircom_private * card)497a88394cfSJeff Kirsher static void initialize_card(struct xircom_private *card)
498a88394cfSJeff Kirsher {
499ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
500a88394cfSJeff Kirsher 	unsigned long flags;
501ebaf7f8fSFrancois Romieu 	u32 val;
502a88394cfSJeff Kirsher 
503a88394cfSJeff Kirsher 	spin_lock_irqsave(&card->lock, flags);
504a88394cfSJeff Kirsher 
505a88394cfSJeff Kirsher 	/* First: reset the card */
506ebaf7f8fSFrancois Romieu 	val = xr32(CSR0);
507a88394cfSJeff Kirsher 	val |= 0x01;		/* Software reset */
508ebaf7f8fSFrancois Romieu 	xw32(CSR0, val);
509a88394cfSJeff Kirsher 
510a88394cfSJeff Kirsher 	udelay(100);		/* give the card some time to reset */
511a88394cfSJeff Kirsher 
512ebaf7f8fSFrancois Romieu 	val = xr32(CSR0);
513a88394cfSJeff Kirsher 	val &= ~0x01;		/* disable Software reset */
514ebaf7f8fSFrancois Romieu 	xw32(CSR0, val);
515a88394cfSJeff Kirsher 
516a88394cfSJeff Kirsher 
517a88394cfSJeff Kirsher 	val = 0;		/* Value 0x00 is a safe and conservative value
518a88394cfSJeff Kirsher 				   for the PCI configuration settings */
519ebaf7f8fSFrancois Romieu 	xw32(CSR0, val);
520a88394cfSJeff Kirsher 
521a88394cfSJeff Kirsher 
522a88394cfSJeff Kirsher 	disable_all_interrupts(card);
523a88394cfSJeff Kirsher 	deactivate_receiver(card);
524a88394cfSJeff Kirsher 	deactivate_transmitter(card);
525a88394cfSJeff Kirsher 
526a88394cfSJeff Kirsher 	spin_unlock_irqrestore(&card->lock, flags);
527a88394cfSJeff Kirsher }
528a88394cfSJeff Kirsher 
529a88394cfSJeff Kirsher /*
530a88394cfSJeff Kirsher trigger_transmit causes the card to check for frames to be transmitted.
531a88394cfSJeff Kirsher This is accomplished by writing to the CSR1 port. The documentation
532a88394cfSJeff Kirsher claims that the act of writing is sufficient and that the value is
533a88394cfSJeff Kirsher ignored; I chose zero.
534a88394cfSJeff Kirsher */
trigger_transmit(struct xircom_private * card)535a88394cfSJeff Kirsher static void trigger_transmit(struct xircom_private *card)
536a88394cfSJeff Kirsher {
537ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
538a88394cfSJeff Kirsher 
539ebaf7f8fSFrancois Romieu 	xw32(CSR1, 0);
540a88394cfSJeff Kirsher }
541a88394cfSJeff Kirsher 
542a88394cfSJeff Kirsher /*
543a88394cfSJeff Kirsher trigger_receive causes the card to check for empty frames in the
544a88394cfSJeff Kirsher descriptor list in which packets can be received.
545a88394cfSJeff Kirsher This is accomplished by writing to the CSR2 port. The documentation
546a88394cfSJeff Kirsher claims that the act of writing is sufficient and that the value is
547a88394cfSJeff Kirsher ignored; I chose zero.
548a88394cfSJeff Kirsher */
trigger_receive(struct xircom_private * card)549a88394cfSJeff Kirsher static void trigger_receive(struct xircom_private *card)
550a88394cfSJeff Kirsher {
551ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
552a88394cfSJeff Kirsher 
553ebaf7f8fSFrancois Romieu 	xw32(CSR2, 0);
554a88394cfSJeff Kirsher }
555a88394cfSJeff Kirsher 
556a88394cfSJeff Kirsher /*
557a88394cfSJeff Kirsher setup_descriptors initializes the send and receive buffers to be valid
558a88394cfSJeff Kirsher descriptors and programs the addresses into the card.
559a88394cfSJeff Kirsher */
setup_descriptors(struct xircom_private * card)560a88394cfSJeff Kirsher static void setup_descriptors(struct xircom_private *card)
561a88394cfSJeff Kirsher {
562ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
563a88394cfSJeff Kirsher 	u32 address;
564a88394cfSJeff Kirsher 	int i;
565a88394cfSJeff Kirsher 
566a88394cfSJeff Kirsher 	BUG_ON(card->rx_buffer == NULL);
567a88394cfSJeff Kirsher 	BUG_ON(card->tx_buffer == NULL);
568a88394cfSJeff Kirsher 
569a88394cfSJeff Kirsher 	/* Receive descriptors */
570a88394cfSJeff Kirsher 	memset(card->rx_buffer, 0, 128);	/* clear the descriptors */
571a88394cfSJeff Kirsher 	for (i=0;i<NUMDESCRIPTORS;i++ ) {
572a88394cfSJeff Kirsher 
573a88394cfSJeff Kirsher 		/* Rx Descr0: It's empty, let the card own it, no errors -> 0x80000000 */
574a88394cfSJeff Kirsher 		card->rx_buffer[i*4 + 0] = cpu_to_le32(0x80000000);
575a88394cfSJeff Kirsher 		/* Rx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
576a88394cfSJeff Kirsher 		card->rx_buffer[i*4 + 1] = cpu_to_le32(1536);
577a88394cfSJeff Kirsher 		if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
578a88394cfSJeff Kirsher 			card->rx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
579a88394cfSJeff Kirsher 
580a88394cfSJeff Kirsher 		/* Rx Descr2: address of the buffer
581a88394cfSJeff Kirsher 		   we store the buffer at the 2nd half of the page */
582a88394cfSJeff Kirsher 
583a88394cfSJeff Kirsher 		address = card->rx_dma_handle;
584a88394cfSJeff Kirsher 		card->rx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
585a88394cfSJeff Kirsher 		/* Rx Desc3: address of 2nd buffer -> 0 */
586a88394cfSJeff Kirsher 		card->rx_buffer[i*4 + 3] = 0;
587a88394cfSJeff Kirsher 	}
588a88394cfSJeff Kirsher 
589a88394cfSJeff Kirsher 	wmb();
590a88394cfSJeff Kirsher 	/* Write the receive descriptor ring address to the card */
591a88394cfSJeff Kirsher 	address = card->rx_dma_handle;
592ebaf7f8fSFrancois Romieu 	xw32(CSR3, address);	/* Receive descr list address */
593a88394cfSJeff Kirsher 
594a88394cfSJeff Kirsher 
595a88394cfSJeff Kirsher 	/* transmit descriptors */
596a88394cfSJeff Kirsher 	memset(card->tx_buffer, 0, 128);	/* clear the descriptors */
597a88394cfSJeff Kirsher 
598a88394cfSJeff Kirsher 	for (i=0;i<NUMDESCRIPTORS;i++ ) {
599a88394cfSJeff Kirsher 		/* Tx Descr0: Empty, we own it, no errors -> 0x00000000 */
600a88394cfSJeff Kirsher 		card->tx_buffer[i*4 + 0] = 0x00000000;
601a88394cfSJeff Kirsher 		/* Tx Descr1: buffer 1 is 1536 bytes, buffer 2 is 0 bytes */
602a88394cfSJeff Kirsher 		card->tx_buffer[i*4 + 1] = cpu_to_le32(1536);
603a88394cfSJeff Kirsher 		if (i == NUMDESCRIPTORS - 1) /* bit 25 is "last descriptor" */
604a88394cfSJeff Kirsher 			card->tx_buffer[i*4 + 1] |= cpu_to_le32(1 << 25);
605a88394cfSJeff Kirsher 
606a88394cfSJeff Kirsher 		/* Tx Descr2: address of the buffer
607a88394cfSJeff Kirsher 		   we store the buffer at the 2nd half of the page */
608a88394cfSJeff Kirsher 		address = card->tx_dma_handle;
609a88394cfSJeff Kirsher 		card->tx_buffer[i*4 + 2] = cpu_to_le32(address + bufferoffsets[i]);
610a88394cfSJeff Kirsher 		/* Tx Desc3: address of 2nd buffer -> 0 */
611a88394cfSJeff Kirsher 		card->tx_buffer[i*4 + 3] = 0;
612a88394cfSJeff Kirsher 	}
613a88394cfSJeff Kirsher 
614a88394cfSJeff Kirsher 	wmb();
615a88394cfSJeff Kirsher 	/* wite the transmit descriptor ring to the card */
616a88394cfSJeff Kirsher 	address = card->tx_dma_handle;
617ebaf7f8fSFrancois Romieu 	xw32(CSR4, address);	/* xmit descr list address */
618a88394cfSJeff Kirsher }
619a88394cfSJeff Kirsher 
620a88394cfSJeff Kirsher /*
621a88394cfSJeff Kirsher remove_descriptors informs the card the descriptors are no longer
622a88394cfSJeff Kirsher valid by setting the address in the card to 0x00.
623a88394cfSJeff Kirsher */
remove_descriptors(struct xircom_private * card)624a88394cfSJeff Kirsher static void remove_descriptors(struct xircom_private *card)
625a88394cfSJeff Kirsher {
626ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
627a88394cfSJeff Kirsher 	unsigned int val;
628a88394cfSJeff Kirsher 
629a88394cfSJeff Kirsher 	val = 0;
630ebaf7f8fSFrancois Romieu 	xw32(CSR3, val);	/* Receive descriptor address */
631ebaf7f8fSFrancois Romieu 	xw32(CSR4, val);	/* Send descriptor address */
632a88394cfSJeff Kirsher }
633a88394cfSJeff Kirsher 
634a88394cfSJeff Kirsher /*
635a88394cfSJeff Kirsher link_status_changed returns 1 if the card has indicated that
636a88394cfSJeff Kirsher the link status has changed. The new link status has to be read from CSR12.
637a88394cfSJeff Kirsher 
638a88394cfSJeff Kirsher This function also clears the status-bit.
639a88394cfSJeff Kirsher */
link_status_changed(struct xircom_private * card)640a88394cfSJeff Kirsher static int link_status_changed(struct xircom_private *card)
641a88394cfSJeff Kirsher {
642ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
643a88394cfSJeff Kirsher 	unsigned int val;
644a88394cfSJeff Kirsher 
645ebaf7f8fSFrancois Romieu 	val = xr32(CSR5);	/* Status register */
646ebaf7f8fSFrancois Romieu 	if (!(val & (1 << 27)))	/* no change */
647a88394cfSJeff Kirsher 		return 0;
648a88394cfSJeff Kirsher 
649a88394cfSJeff Kirsher 	/* clear the event by writing a 1 to the bit in the
650a88394cfSJeff Kirsher 	   status register. */
651a88394cfSJeff Kirsher 	val = (1 << 27);
652ebaf7f8fSFrancois Romieu 	xw32(CSR5, val);
653a88394cfSJeff Kirsher 
654a88394cfSJeff Kirsher 	return 1;
655a88394cfSJeff Kirsher }
656a88394cfSJeff Kirsher 
657a88394cfSJeff Kirsher 
658a88394cfSJeff Kirsher /*
659a88394cfSJeff Kirsher transmit_active returns 1 if the transmitter on the card is
660a88394cfSJeff Kirsher in a non-stopped state.
661a88394cfSJeff Kirsher */
transmit_active(struct xircom_private * card)662a88394cfSJeff Kirsher static int transmit_active(struct xircom_private *card)
663a88394cfSJeff Kirsher {
664ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
665a88394cfSJeff Kirsher 
666ebaf7f8fSFrancois Romieu 	if (!(xr32(CSR5) & (7 << 20)))	/* transmitter disabled */
667a88394cfSJeff Kirsher 		return 0;
668a88394cfSJeff Kirsher 
669a88394cfSJeff Kirsher 	return 1;
670a88394cfSJeff Kirsher }
671a88394cfSJeff Kirsher 
672a88394cfSJeff Kirsher /*
673a88394cfSJeff Kirsher receive_active returns 1 if the receiver on the card is
674a88394cfSJeff Kirsher in a non-stopped state.
675a88394cfSJeff Kirsher */
receive_active(struct xircom_private * card)676a88394cfSJeff Kirsher static int receive_active(struct xircom_private *card)
677a88394cfSJeff Kirsher {
678ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
679a88394cfSJeff Kirsher 
680ebaf7f8fSFrancois Romieu 	if (!(xr32(CSR5) & (7 << 17)))	/* 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 */
activate_receiver(struct xircom_private * card)696a88394cfSJeff Kirsher static void activate_receiver(struct xircom_private *card)
697a88394cfSJeff Kirsher {
698ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
699a88394cfSJeff Kirsher 	unsigned int val;
700a88394cfSJeff Kirsher 	int counter;
701a88394cfSJeff Kirsher 
702ebaf7f8fSFrancois Romieu 	val = xr32(CSR6);	/* Operation mode */
703a88394cfSJeff Kirsher 
704a88394cfSJeff Kirsher 	/* If the "active" bit is set and the receiver is already
705a88394cfSJeff Kirsher 	   active, no need to do the expensive thing */
706a88394cfSJeff Kirsher 	if ((val&2) && (receive_active(card)))
707a88394cfSJeff Kirsher 		return;
708a88394cfSJeff Kirsher 
709a88394cfSJeff Kirsher 
710a88394cfSJeff Kirsher 	val = val & ~2;		/* disable the receiver */
711ebaf7f8fSFrancois Romieu 	xw32(CSR6, val);
712a88394cfSJeff Kirsher 
713a88394cfSJeff Kirsher 	counter = 10;
714a88394cfSJeff Kirsher 	while (counter > 0) {
715a88394cfSJeff Kirsher 		if (!receive_active(card))
716a88394cfSJeff Kirsher 			break;
717a88394cfSJeff Kirsher 		/* wait a while */
718a88394cfSJeff Kirsher 		udelay(50);
719a88394cfSJeff Kirsher 		counter--;
720a88394cfSJeff Kirsher 		if (counter <= 0)
721a88394cfSJeff Kirsher 			netdev_err(card->dev, "Receiver failed to deactivate\n");
722a88394cfSJeff Kirsher 	}
723a88394cfSJeff Kirsher 
724a88394cfSJeff Kirsher 	/* enable the receiver */
725ebaf7f8fSFrancois Romieu 	val = xr32(CSR6);	/* Operation mode */
726a88394cfSJeff Kirsher 	val = val | 2;		/* enable the receiver */
727ebaf7f8fSFrancois Romieu 	xw32(CSR6, val);
728a88394cfSJeff Kirsher 
729a88394cfSJeff Kirsher 	/* now wait for the card to activate again */
730a88394cfSJeff Kirsher 	counter = 10;
731a88394cfSJeff Kirsher 	while (counter > 0) {
732a88394cfSJeff Kirsher 		if (receive_active(card))
733a88394cfSJeff Kirsher 			break;
734a88394cfSJeff Kirsher 		/* wait a while */
735a88394cfSJeff Kirsher 		udelay(50);
736a88394cfSJeff Kirsher 		counter--;
737a88394cfSJeff Kirsher 		if (counter <= 0)
738a88394cfSJeff Kirsher 			netdev_err(card->dev,
739a88394cfSJeff Kirsher 				   "Receiver failed to re-activate\n");
740a88394cfSJeff Kirsher 	}
741a88394cfSJeff Kirsher }
742a88394cfSJeff Kirsher 
743a88394cfSJeff Kirsher /*
744a88394cfSJeff Kirsher deactivate_receiver disables the receiver on the card.
745a88394cfSJeff Kirsher To achieve this this code disables the receiver first;
746a88394cfSJeff Kirsher then it waits for the receiver to become inactive.
747a88394cfSJeff Kirsher 
748a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
749a88394cfSJeff Kirsher */
deactivate_receiver(struct xircom_private * card)750a88394cfSJeff Kirsher static void deactivate_receiver(struct xircom_private *card)
751a88394cfSJeff Kirsher {
752ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
753a88394cfSJeff Kirsher 	unsigned int val;
754a88394cfSJeff Kirsher 	int counter;
755a88394cfSJeff Kirsher 
756ebaf7f8fSFrancois Romieu 	val = xr32(CSR6);	/* Operation mode */
757a88394cfSJeff Kirsher 	val = val & ~2;		/* disable the receiver */
758ebaf7f8fSFrancois Romieu 	xw32(CSR6, val);
759a88394cfSJeff Kirsher 
760a88394cfSJeff Kirsher 	counter = 10;
761a88394cfSJeff Kirsher 	while (counter > 0) {
762a88394cfSJeff Kirsher 		if (!receive_active(card))
763a88394cfSJeff Kirsher 			break;
764a88394cfSJeff Kirsher 		/* wait a while */
765a88394cfSJeff Kirsher 		udelay(50);
766a88394cfSJeff Kirsher 		counter--;
767a88394cfSJeff Kirsher 		if (counter <= 0)
768a88394cfSJeff Kirsher 			netdev_err(card->dev, "Receiver failed to deactivate\n");
769a88394cfSJeff Kirsher 	}
770a88394cfSJeff Kirsher }
771a88394cfSJeff Kirsher 
772a88394cfSJeff Kirsher 
773a88394cfSJeff Kirsher /*
774a88394cfSJeff Kirsher activate_transmitter enables the transmitter on the card.
775a88394cfSJeff Kirsher Before being allowed to active the transmitter, the transmitter
776a88394cfSJeff Kirsher must be completely de-activated. To achieve this,
777a88394cfSJeff Kirsher this code actually disables the transmitter first; then it waits for the
778a88394cfSJeff Kirsher transmitter to become inactive, then it activates the transmitter and then
779a88394cfSJeff Kirsher it waits for the transmitter to be active again.
780a88394cfSJeff Kirsher 
781a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
782a88394cfSJeff Kirsher */
activate_transmitter(struct xircom_private * card)783a88394cfSJeff Kirsher static void activate_transmitter(struct xircom_private *card)
784a88394cfSJeff Kirsher {
785ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
786a88394cfSJeff Kirsher 	unsigned int val;
787a88394cfSJeff Kirsher 	int counter;
788a88394cfSJeff Kirsher 
789ebaf7f8fSFrancois Romieu 	val = xr32(CSR6);	/* Operation mode */
790a88394cfSJeff Kirsher 
791a88394cfSJeff Kirsher 	/* If the "active" bit is set and the receiver is already
792a88394cfSJeff Kirsher 	   active, no need to do the expensive thing */
793a88394cfSJeff Kirsher 	if ((val&(1<<13)) && (transmit_active(card)))
794a88394cfSJeff Kirsher 		return;
795a88394cfSJeff Kirsher 
796a88394cfSJeff Kirsher 	val = val & ~(1 << 13);	/* disable the transmitter */
797ebaf7f8fSFrancois Romieu 	xw32(CSR6, val);
798a88394cfSJeff Kirsher 
799a88394cfSJeff Kirsher 	counter = 10;
800a88394cfSJeff Kirsher 	while (counter > 0) {
801a88394cfSJeff Kirsher 		if (!transmit_active(card))
802a88394cfSJeff Kirsher 			break;
803a88394cfSJeff Kirsher 		/* wait a while */
804a88394cfSJeff Kirsher 		udelay(50);
805a88394cfSJeff Kirsher 		counter--;
806a88394cfSJeff Kirsher 		if (counter <= 0)
807a88394cfSJeff Kirsher 			netdev_err(card->dev,
808a88394cfSJeff Kirsher 				   "Transmitter failed to deactivate\n");
809a88394cfSJeff Kirsher 	}
810a88394cfSJeff Kirsher 
811a88394cfSJeff Kirsher 	/* enable the transmitter */
812ebaf7f8fSFrancois Romieu 	val = xr32(CSR6);	/* Operation mode */
813a88394cfSJeff Kirsher 	val = val | (1 << 13);	/* enable the transmitter */
814ebaf7f8fSFrancois Romieu 	xw32(CSR6, val);
815a88394cfSJeff Kirsher 
816a88394cfSJeff Kirsher 	/* now wait for the card to activate again */
817a88394cfSJeff Kirsher 	counter = 10;
818a88394cfSJeff Kirsher 	while (counter > 0) {
819a88394cfSJeff Kirsher 		if (transmit_active(card))
820a88394cfSJeff Kirsher 			break;
821a88394cfSJeff Kirsher 		/* wait a while */
822a88394cfSJeff Kirsher 		udelay(50);
823a88394cfSJeff Kirsher 		counter--;
824a88394cfSJeff Kirsher 		if (counter <= 0)
825a88394cfSJeff Kirsher 			netdev_err(card->dev,
826a88394cfSJeff Kirsher 				   "Transmitter failed to re-activate\n");
827a88394cfSJeff Kirsher 	}
828a88394cfSJeff Kirsher }
829a88394cfSJeff Kirsher 
830a88394cfSJeff Kirsher /*
831a88394cfSJeff Kirsher deactivate_transmitter disables the transmitter on the card.
832a88394cfSJeff Kirsher To achieve this this code disables the transmitter first;
833a88394cfSJeff Kirsher then it waits for the transmitter to become inactive.
834a88394cfSJeff Kirsher 
835a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
836a88394cfSJeff Kirsher */
deactivate_transmitter(struct xircom_private * card)837a88394cfSJeff Kirsher static void deactivate_transmitter(struct xircom_private *card)
838a88394cfSJeff Kirsher {
839ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
840a88394cfSJeff Kirsher 	unsigned int val;
841a88394cfSJeff Kirsher 	int counter;
842a88394cfSJeff Kirsher 
843ebaf7f8fSFrancois Romieu 	val = xr32(CSR6);	/* Operation mode */
844a88394cfSJeff Kirsher 	val = val & ~2;		/* disable the transmitter */
845ebaf7f8fSFrancois Romieu 	xw32(CSR6, val);
846a88394cfSJeff Kirsher 
847a88394cfSJeff Kirsher 	counter = 20;
848a88394cfSJeff Kirsher 	while (counter > 0) {
849a88394cfSJeff Kirsher 		if (!transmit_active(card))
850a88394cfSJeff Kirsher 			break;
851a88394cfSJeff Kirsher 		/* wait a while */
852a88394cfSJeff Kirsher 		udelay(50);
853a88394cfSJeff Kirsher 		counter--;
854a88394cfSJeff Kirsher 		if (counter <= 0)
855a88394cfSJeff Kirsher 			netdev_err(card->dev,
856a88394cfSJeff Kirsher 				   "Transmitter failed to deactivate\n");
857a88394cfSJeff Kirsher 	}
858a88394cfSJeff Kirsher }
859a88394cfSJeff Kirsher 
860a88394cfSJeff Kirsher 
861a88394cfSJeff Kirsher /*
862a88394cfSJeff Kirsher enable_transmit_interrupt enables the transmit interrupt
863a88394cfSJeff Kirsher 
864a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
865a88394cfSJeff Kirsher */
enable_transmit_interrupt(struct xircom_private * card)866a88394cfSJeff Kirsher static void enable_transmit_interrupt(struct xircom_private *card)
867a88394cfSJeff Kirsher {
868ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
869a88394cfSJeff Kirsher 	unsigned int val;
870a88394cfSJeff Kirsher 
871ebaf7f8fSFrancois Romieu 	val = xr32(CSR7);	/* Interrupt enable register */
872a88394cfSJeff Kirsher 	val |= 1;		/* enable the transmit interrupt */
873ebaf7f8fSFrancois Romieu 	xw32(CSR7, val);
874a88394cfSJeff Kirsher }
875a88394cfSJeff Kirsher 
876a88394cfSJeff Kirsher 
877a88394cfSJeff Kirsher /*
878a88394cfSJeff Kirsher enable_receive_interrupt enables the receive interrupt
879a88394cfSJeff Kirsher 
880a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
881a88394cfSJeff Kirsher */
enable_receive_interrupt(struct xircom_private * card)882a88394cfSJeff Kirsher static void enable_receive_interrupt(struct xircom_private *card)
883a88394cfSJeff Kirsher {
884ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
885a88394cfSJeff Kirsher 	unsigned int val;
886a88394cfSJeff Kirsher 
887ebaf7f8fSFrancois Romieu 	val = xr32(CSR7);	/* Interrupt enable register */
888a88394cfSJeff Kirsher 	val = val | (1 << 6);	/* enable the receive interrupt */
889ebaf7f8fSFrancois Romieu 	xw32(CSR7, val);
890a88394cfSJeff Kirsher }
891a88394cfSJeff Kirsher 
892a88394cfSJeff Kirsher /*
893a88394cfSJeff Kirsher enable_link_interrupt enables the link status change interrupt
894a88394cfSJeff Kirsher 
895a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
896a88394cfSJeff Kirsher */
enable_link_interrupt(struct xircom_private * card)897a88394cfSJeff Kirsher static void enable_link_interrupt(struct xircom_private *card)
898a88394cfSJeff Kirsher {
899ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
900a88394cfSJeff Kirsher 	unsigned int val;
901a88394cfSJeff Kirsher 
902ebaf7f8fSFrancois Romieu 	val = xr32(CSR7);	/* Interrupt enable register */
903a88394cfSJeff Kirsher 	val = val | (1 << 27);	/* enable the link status chage interrupt */
904ebaf7f8fSFrancois Romieu 	xw32(CSR7, val);
905a88394cfSJeff Kirsher }
906a88394cfSJeff Kirsher 
907a88394cfSJeff Kirsher 
908a88394cfSJeff Kirsher 
909a88394cfSJeff Kirsher /*
910a88394cfSJeff Kirsher disable_all_interrupts disables all interrupts
911a88394cfSJeff Kirsher 
912a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
913a88394cfSJeff Kirsher */
disable_all_interrupts(struct xircom_private * card)914a88394cfSJeff Kirsher static void disable_all_interrupts(struct xircom_private *card)
915a88394cfSJeff Kirsher {
916ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
917a88394cfSJeff Kirsher 
918ebaf7f8fSFrancois Romieu 	xw32(CSR7, 0);
919a88394cfSJeff Kirsher }
920a88394cfSJeff Kirsher 
921a88394cfSJeff Kirsher /*
922a88394cfSJeff Kirsher enable_common_interrupts enables several weird interrupts
923a88394cfSJeff Kirsher 
924a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
925a88394cfSJeff Kirsher */
enable_common_interrupts(struct xircom_private * card)926a88394cfSJeff Kirsher static void enable_common_interrupts(struct xircom_private *card)
927a88394cfSJeff Kirsher {
928ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
929a88394cfSJeff Kirsher 	unsigned int val;
930a88394cfSJeff Kirsher 
931ebaf7f8fSFrancois Romieu 	val = xr32(CSR7);	/* Interrupt enable register */
932a88394cfSJeff Kirsher 	val |= (1<<16); /* Normal Interrupt Summary */
933a88394cfSJeff Kirsher 	val |= (1<<15); /* Abnormal Interrupt Summary */
934a88394cfSJeff Kirsher 	val |= (1<<13); /* Fatal bus error */
935a88394cfSJeff Kirsher 	val |= (1<<8);  /* Receive Process Stopped */
936a88394cfSJeff Kirsher 	val |= (1<<7);  /* Receive Buffer Unavailable */
937a88394cfSJeff Kirsher 	val |= (1<<5);  /* Transmit Underflow */
938a88394cfSJeff Kirsher 	val |= (1<<2);  /* Transmit Buffer Unavailable */
939a88394cfSJeff Kirsher 	val |= (1<<1);  /* Transmit Process Stopped */
940ebaf7f8fSFrancois Romieu 	xw32(CSR7, val);
941a88394cfSJeff Kirsher }
942a88394cfSJeff Kirsher 
943a88394cfSJeff Kirsher /*
944a88394cfSJeff Kirsher enable_promisc starts promisc mode
945a88394cfSJeff Kirsher 
946a88394cfSJeff Kirsher must be called with the lock held and interrupts disabled.
947a88394cfSJeff Kirsher */
enable_promisc(struct xircom_private * card)948a88394cfSJeff Kirsher static int enable_promisc(struct xircom_private *card)
949a88394cfSJeff Kirsher {
950ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
951a88394cfSJeff Kirsher 	unsigned int val;
952a88394cfSJeff Kirsher 
953ebaf7f8fSFrancois Romieu 	val = xr32(CSR6);
954a88394cfSJeff Kirsher 	val = val | (1 << 6);
955ebaf7f8fSFrancois Romieu 	xw32(CSR6, val);
956a88394cfSJeff Kirsher 
957a88394cfSJeff Kirsher 	return 1;
958a88394cfSJeff Kirsher }
959a88394cfSJeff Kirsher 
960a88394cfSJeff Kirsher 
961a88394cfSJeff Kirsher 
962a88394cfSJeff Kirsher 
963a88394cfSJeff Kirsher /*
964a88394cfSJeff Kirsher link_status() checks the links status and will return 0 for no link, 10 for 10mbit link and 100 for.. guess what.
965a88394cfSJeff Kirsher 
966a88394cfSJeff Kirsher Must be called in locked state with interrupts disabled
967a88394cfSJeff Kirsher */
link_status(struct xircom_private * card)968a88394cfSJeff Kirsher static int link_status(struct xircom_private *card)
969a88394cfSJeff Kirsher {
970ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
971ebaf7f8fSFrancois Romieu 	u8 val;
972a88394cfSJeff Kirsher 
973ebaf7f8fSFrancois Romieu 	val = xr8(CSR12);
974a88394cfSJeff Kirsher 
975ebaf7f8fSFrancois Romieu 	/* bit 2 is 0 for 10mbit link, 1 for not an 10mbit link */
976ebaf7f8fSFrancois Romieu 	if (!(val & (1 << 2)))
977a88394cfSJeff Kirsher 		return 10;
978ebaf7f8fSFrancois Romieu 	/* bit 1 is 0 for 100mbit link, 1 for not an 100mbit link */
979ebaf7f8fSFrancois Romieu 	if (!(val & (1 << 1)))
980a88394cfSJeff Kirsher 		return 100;
981a88394cfSJeff Kirsher 
982a88394cfSJeff Kirsher 	/* If we get here -> no link at all */
983a88394cfSJeff Kirsher 
984a88394cfSJeff Kirsher 	return 0;
985a88394cfSJeff Kirsher }
986a88394cfSJeff Kirsher 
987a88394cfSJeff Kirsher 
988a88394cfSJeff Kirsher 
989a88394cfSJeff Kirsher 
990a88394cfSJeff Kirsher 
991a88394cfSJeff Kirsher /*
992a88394cfSJeff Kirsher   read_mac_address() reads the MAC address from the NIC and stores it in the "dev" structure.
993a88394cfSJeff Kirsher 
994a88394cfSJeff Kirsher   This function will take the spinlock itself and can, as a result, not be called with the lock helt.
995a88394cfSJeff Kirsher  */
read_mac_address(struct xircom_private * card)996a88394cfSJeff Kirsher static void read_mac_address(struct xircom_private *card)
997a88394cfSJeff Kirsher {
998ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
999a88394cfSJeff Kirsher 	unsigned long flags;
1000ebaf7f8fSFrancois Romieu 	u8 link;
1001a88394cfSJeff Kirsher 	int i;
1002a88394cfSJeff Kirsher 
1003a88394cfSJeff Kirsher 	spin_lock_irqsave(&card->lock, flags);
1004a88394cfSJeff Kirsher 
1005ebaf7f8fSFrancois Romieu 	xw32(CSR9, 1 << 12);	/* enable boot rom access */
1006a88394cfSJeff Kirsher 	for (i = 0x100; i < 0x1f7; i += link + 2) {
1007ebaf7f8fSFrancois Romieu 		u8 tuple, data_id, data_count;
1008ebaf7f8fSFrancois Romieu 
1009ebaf7f8fSFrancois Romieu 		xw32(CSR10, i);
1010ebaf7f8fSFrancois Romieu 		tuple = xr32(CSR9);
1011ebaf7f8fSFrancois Romieu 		xw32(CSR10, i + 1);
1012ebaf7f8fSFrancois Romieu 		link = xr32(CSR9);
1013ebaf7f8fSFrancois Romieu 		xw32(CSR10, i + 2);
1014ebaf7f8fSFrancois Romieu 		data_id = xr32(CSR9);
1015ebaf7f8fSFrancois Romieu 		xw32(CSR10, i + 3);
1016ebaf7f8fSFrancois Romieu 		data_count = xr32(CSR9);
1017a88394cfSJeff Kirsher 		if ((tuple == 0x22) && (data_id == 0x04) && (data_count == 0x06)) {
1018*ca879317SJakub Kicinski 			u8 addr[ETH_ALEN];
1019ebaf7f8fSFrancois Romieu 			int j;
1020ebaf7f8fSFrancois Romieu 
1021a88394cfSJeff Kirsher 			for (j = 0; j < 6; j++) {
1022ebaf7f8fSFrancois Romieu 				xw32(CSR10, i + j + 4);
1023*ca879317SJakub Kicinski 				addr[j] = xr32(CSR9) & 0xff;
1024a88394cfSJeff Kirsher 			}
1025*ca879317SJakub Kicinski 			eth_hw_addr_set(card->dev, addr);
1026a88394cfSJeff Kirsher 			break;
1027a88394cfSJeff Kirsher 		} else if (link == 0) {
1028a88394cfSJeff Kirsher 			break;
1029a88394cfSJeff Kirsher 		}
1030a88394cfSJeff Kirsher 	}
1031a88394cfSJeff Kirsher 	spin_unlock_irqrestore(&card->lock, flags);
1032a88394cfSJeff Kirsher 	pr_debug(" %pM\n", card->dev->dev_addr);
1033a88394cfSJeff Kirsher }
1034a88394cfSJeff Kirsher 
1035a88394cfSJeff Kirsher 
1036a88394cfSJeff Kirsher /*
1037a88394cfSJeff Kirsher  transceiver_voodoo() enables the external UTP plug thingy.
1038a88394cfSJeff Kirsher  it's called voodoo as I stole this code and cannot cross-reference
1039a88394cfSJeff Kirsher  it with the specification.
1040a88394cfSJeff Kirsher  */
transceiver_voodoo(struct xircom_private * card)1041a88394cfSJeff Kirsher static void transceiver_voodoo(struct xircom_private *card)
1042a88394cfSJeff Kirsher {
1043ebaf7f8fSFrancois Romieu 	void __iomem *ioaddr = card->ioaddr;
1044a88394cfSJeff Kirsher 	unsigned long flags;
1045a88394cfSJeff Kirsher 
1046a88394cfSJeff Kirsher 	/* disable all powermanagement */
1047a88394cfSJeff Kirsher 	pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1048a88394cfSJeff Kirsher 
1049a88394cfSJeff Kirsher 	setup_descriptors(card);
1050a88394cfSJeff Kirsher 
1051a88394cfSJeff Kirsher 	spin_lock_irqsave(&card->lock, flags);
1052a88394cfSJeff Kirsher 
1053ebaf7f8fSFrancois Romieu 	xw32(CSR15, 0x0008);
1054a88394cfSJeff Kirsher 	udelay(25);
1055ebaf7f8fSFrancois Romieu 	xw32(CSR15, 0xa8050000);
1056a88394cfSJeff Kirsher 	udelay(25);
1057ebaf7f8fSFrancois Romieu 	xw32(CSR15, 0xa00f0000);
1058a88394cfSJeff Kirsher 	udelay(25);
1059a88394cfSJeff Kirsher 
1060a88394cfSJeff Kirsher 	spin_unlock_irqrestore(&card->lock, flags);
1061a88394cfSJeff Kirsher 
1062a88394cfSJeff Kirsher 	netif_start_queue(card->dev);
1063a88394cfSJeff Kirsher }
1064a88394cfSJeff Kirsher 
1065a88394cfSJeff Kirsher 
xircom_up(struct xircom_private * card)1066a88394cfSJeff Kirsher static void xircom_up(struct xircom_private *card)
1067a88394cfSJeff Kirsher {
1068a88394cfSJeff Kirsher 	unsigned long flags;
1069a88394cfSJeff Kirsher 	int i;
1070a88394cfSJeff Kirsher 
1071a88394cfSJeff Kirsher 	/* disable all powermanagement */
1072a88394cfSJeff Kirsher 	pci_write_config_dword(card->pdev, PCI_POWERMGMT, 0x0000);
1073a88394cfSJeff Kirsher 
1074a88394cfSJeff Kirsher 	setup_descriptors(card);
1075a88394cfSJeff Kirsher 
1076a88394cfSJeff Kirsher 	spin_lock_irqsave(&card->lock, flags);
1077a88394cfSJeff Kirsher 
1078a88394cfSJeff Kirsher 
1079a88394cfSJeff Kirsher 	enable_link_interrupt(card);
1080a88394cfSJeff Kirsher 	enable_transmit_interrupt(card);
1081a88394cfSJeff Kirsher 	enable_receive_interrupt(card);
1082a88394cfSJeff Kirsher 	enable_common_interrupts(card);
1083a88394cfSJeff Kirsher 	enable_promisc(card);
1084a88394cfSJeff Kirsher 
1085a88394cfSJeff Kirsher 	/* The card can have received packets already, read them away now */
1086a88394cfSJeff Kirsher 	for (i=0;i<NUMDESCRIPTORS;i++)
1087a88394cfSJeff Kirsher 		investigate_read_descriptor(card->dev,card,i,bufferoffsets[i]);
1088a88394cfSJeff Kirsher 
1089a88394cfSJeff Kirsher 
1090a88394cfSJeff Kirsher 	spin_unlock_irqrestore(&card->lock, flags);
1091a88394cfSJeff Kirsher 	trigger_receive(card);
1092a88394cfSJeff Kirsher 	trigger_transmit(card);
1093a88394cfSJeff Kirsher 	netif_start_queue(card->dev);
1094a88394cfSJeff Kirsher }
1095a88394cfSJeff Kirsher 
1096a88394cfSJeff Kirsher /* Bufferoffset is in BYTES */
1097a88394cfSJeff Kirsher static void
investigate_read_descriptor(struct net_device * dev,struct xircom_private * card,int descnr,unsigned int bufferoffset)1098a88394cfSJeff Kirsher investigate_read_descriptor(struct net_device *dev, struct xircom_private *card,
1099a88394cfSJeff Kirsher 			    int descnr, unsigned int bufferoffset)
1100a88394cfSJeff Kirsher {
1101a88394cfSJeff Kirsher 	int status;
1102a88394cfSJeff Kirsher 
1103a88394cfSJeff Kirsher 	status = le32_to_cpu(card->rx_buffer[4*descnr]);
1104a88394cfSJeff Kirsher 
1105a88394cfSJeff Kirsher 	if (status > 0) {		/* packet received */
1106a88394cfSJeff Kirsher 
1107a88394cfSJeff Kirsher 		/* TODO: discard error packets */
1108a88394cfSJeff Kirsher 
1109a88394cfSJeff Kirsher 		short pkt_len = ((status >> 16) & 0x7ff) - 4;
1110a88394cfSJeff Kirsher 					/* minus 4, we don't want the CRC */
1111a88394cfSJeff Kirsher 		struct sk_buff *skb;
1112a88394cfSJeff Kirsher 
1113a88394cfSJeff Kirsher 		if (pkt_len > 1518) {
1114a88394cfSJeff Kirsher 			netdev_err(dev, "Packet length %i is bogus\n", pkt_len);
1115a88394cfSJeff Kirsher 			pkt_len = 1518;
1116a88394cfSJeff Kirsher 		}
1117a88394cfSJeff Kirsher 
111821a4e469SPradeep A Dalvi 		skb = netdev_alloc_skb(dev, pkt_len + 2);
1119a88394cfSJeff Kirsher 		if (skb == NULL) {
1120a88394cfSJeff Kirsher 			dev->stats.rx_dropped++;
1121a88394cfSJeff Kirsher 			goto out;
1122a88394cfSJeff Kirsher 		}
1123a88394cfSJeff Kirsher 		skb_reserve(skb, 2);
1124a88394cfSJeff Kirsher 		skb_copy_to_linear_data(skb,
1125a88394cfSJeff Kirsher 					&card->rx_buffer[bufferoffset / 4],
1126a88394cfSJeff Kirsher 					pkt_len);
1127a88394cfSJeff Kirsher 		skb_put(skb, pkt_len);
1128a88394cfSJeff Kirsher 		skb->protocol = eth_type_trans(skb, dev);
1129a88394cfSJeff Kirsher 		netif_rx(skb);
1130a88394cfSJeff Kirsher 		dev->stats.rx_packets++;
1131a88394cfSJeff Kirsher 		dev->stats.rx_bytes += pkt_len;
1132a88394cfSJeff Kirsher 
1133a88394cfSJeff Kirsher out:
1134a88394cfSJeff Kirsher 		/* give the buffer back to the card */
1135a88394cfSJeff Kirsher 		card->rx_buffer[4*descnr] = cpu_to_le32(0x80000000);
1136a88394cfSJeff Kirsher 		trigger_receive(card);
1137a88394cfSJeff Kirsher 	}
1138a88394cfSJeff Kirsher }
1139a88394cfSJeff Kirsher 
1140a88394cfSJeff Kirsher 
1141a88394cfSJeff Kirsher /* Bufferoffset is in BYTES */
1142a88394cfSJeff Kirsher static void
investigate_write_descriptor(struct net_device * dev,struct xircom_private * card,int descnr,unsigned int bufferoffset)1143a88394cfSJeff Kirsher investigate_write_descriptor(struct net_device *dev,
1144a88394cfSJeff Kirsher 			     struct xircom_private *card,
1145a88394cfSJeff Kirsher 			     int descnr, unsigned int bufferoffset)
1146a88394cfSJeff Kirsher {
1147a88394cfSJeff Kirsher 	int status;
1148a88394cfSJeff Kirsher 
1149a88394cfSJeff Kirsher 	status = le32_to_cpu(card->tx_buffer[4*descnr]);
1150a88394cfSJeff Kirsher #if 0
1151a88394cfSJeff Kirsher 	if (status & 0x8000) {	/* Major error */
1152a88394cfSJeff Kirsher 		pr_err("Major transmit error status %x\n", status);
1153a88394cfSJeff Kirsher 		card->tx_buffer[4*descnr] = 0;
1154a88394cfSJeff Kirsher 		netif_wake_queue (dev);
1155a88394cfSJeff Kirsher 	}
1156a88394cfSJeff Kirsher #endif
1157a88394cfSJeff Kirsher 	if (status > 0) {	/* bit 31 is 0 when done */
1158a88394cfSJeff Kirsher 		if (card->tx_skb[descnr]!=NULL) {
1159a88394cfSJeff Kirsher 			dev->stats.tx_bytes += card->tx_skb[descnr]->len;
1160a88394cfSJeff Kirsher 			dev_kfree_skb_irq(card->tx_skb[descnr]);
1161a88394cfSJeff Kirsher 		}
1162a88394cfSJeff Kirsher 		card->tx_skb[descnr] = NULL;
1163a88394cfSJeff Kirsher 		/* Bit 8 in the status field is 1 if there was a collision */
1164a88394cfSJeff Kirsher 		if (status & (1 << 8))
1165a88394cfSJeff Kirsher 			dev->stats.collisions++;
1166a88394cfSJeff Kirsher 		card->tx_buffer[4*descnr] = 0; /* descriptor is free again */
1167a88394cfSJeff Kirsher 		netif_wake_queue (dev);
1168a88394cfSJeff Kirsher 		dev->stats.tx_packets++;
1169a88394cfSJeff Kirsher 	}
1170a88394cfSJeff Kirsher }
1171a88394cfSJeff Kirsher 
117231d60ebfSPeter Hüwe module_pci_driver(xircom_ops);
1173