xref: /linux/drivers/net/ethernet/ti/tlan.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*******************************************************************************
2  *
3  *  Linux ThunderLAN Driver
4  *
5  *  tlan.c
6  *  by James Banks
7  *
8  *  (C) 1997-1998 Caldera, Inc.
9  *  (C) 1998 James Banks
10  *  (C) 1999-2001 Torben Mathiasen
11  *  (C) 2002 Samuel Chessman
12  *
13  *  This software may be used and distributed according to the terms
14  *  of the GNU General Public License, incorporated herein by reference.
15  *
16  ** Useful (if not required) reading:
17  *
18  *		Texas Instruments, ThunderLAN Programmer's Guide,
19  *			TI Literature Number SPWU013A
20  *			available in PDF format from www.ti.com
21  *		Level One, LXT901 and LXT970 Data Sheets
22  *			available in PDF format from www.level1.com
23  *		National Semiconductor, DP83840A Data Sheet
24  *			available in PDF format from www.national.com
25  *		Microchip Technology, 24C01A/02A/04A Data Sheet
26  *			available in PDF format from www.microchip.com
27  *
28  ******************************************************************************/
29 
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 
32 #include <linux/hardirq.h>
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/interrupt.h>
36 #include <linux/ioport.h>
37 #include <linux/eisa.h>
38 #include <linux/pci.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/netdevice.h>
41 #include <linux/etherdevice.h>
42 #include <linux/delay.h>
43 #include <linux/spinlock.h>
44 #include <linux/workqueue.h>
45 #include <linux/mii.h>
46 
47 #include "tlan.h"
48 
49 
50 /* For removing EISA devices */
51 static	struct net_device	*tlan_eisa_devices;
52 
53 static	int		tlan_devices_installed;
54 
55 /* Set speed, duplex and aui settings */
56 static  int aui[MAX_TLAN_BOARDS];
57 static  int duplex[MAX_TLAN_BOARDS];
58 static  int speed[MAX_TLAN_BOARDS];
59 static  int boards_found;
60 module_param_array(aui, int, NULL, 0);
61 module_param_array(duplex, int, NULL, 0);
62 module_param_array(speed, int, NULL, 0);
63 MODULE_PARM_DESC(aui, "ThunderLAN use AUI port(s) (0-1)");
64 MODULE_PARM_DESC(duplex,
65 		 "ThunderLAN duplex setting(s) (0-default, 1-half, 2-full)");
66 MODULE_PARM_DESC(speed, "ThunderLAN port speed setting(s) (0,10,100)");
67 
68 MODULE_AUTHOR("Maintainer: Samuel Chessman <chessman@tux.org>");
69 MODULE_DESCRIPTION("Driver for TI ThunderLAN based ethernet PCI adapters");
70 MODULE_LICENSE("GPL");
71 
72 /* Turn on debugging. See Documentation/networking/tlan.txt for details */
73 static  int		debug;
74 module_param(debug, int, 0);
75 MODULE_PARM_DESC(debug, "ThunderLAN debug mask");
76 
77 static	const char tlan_signature[] = "TLAN";
78 static  const char tlan_banner[] = "ThunderLAN driver v1.17\n";
79 static  int tlan_have_pci;
80 static  int tlan_have_eisa;
81 
82 static const char * const media[] = {
83 	"10BaseT-HD", "10BaseT-FD", "100baseTx-HD",
84 	"100BaseTx-FD", "100BaseT4", NULL
85 };
86 
87 static struct board {
88 	const char	*device_label;
89 	u32		flags;
90 	u16		addr_ofs;
91 } board_info[] = {
92 	{ "Compaq Netelligent 10 T PCI UTP", TLAN_ADAPTER_ACTIVITY_LED, 0x83 },
93 	{ "Compaq Netelligent 10/100 TX PCI UTP",
94 	  TLAN_ADAPTER_ACTIVITY_LED, 0x83 },
95 	{ "Compaq Integrated NetFlex-3/P", TLAN_ADAPTER_NONE, 0x83 },
96 	{ "Compaq NetFlex-3/P",
97 	  TLAN_ADAPTER_UNMANAGED_PHY | TLAN_ADAPTER_BIT_RATE_PHY, 0x83 },
98 	{ "Compaq NetFlex-3/P", TLAN_ADAPTER_NONE, 0x83 },
99 	{ "Compaq Netelligent Integrated 10/100 TX UTP",
100 	  TLAN_ADAPTER_ACTIVITY_LED, 0x83 },
101 	{ "Compaq Netelligent Dual 10/100 TX PCI UTP",
102 	  TLAN_ADAPTER_NONE, 0x83 },
103 	{ "Compaq Netelligent 10/100 TX Embedded UTP",
104 	  TLAN_ADAPTER_NONE, 0x83 },
105 	{ "Olicom OC-2183/2185", TLAN_ADAPTER_USE_INTERN_10, 0x83 },
106 	{ "Olicom OC-2325", TLAN_ADAPTER_ACTIVITY_LED |
107 	  TLAN_ADAPTER_UNMANAGED_PHY, 0xf8 },
108 	{ "Olicom OC-2326", TLAN_ADAPTER_ACTIVITY_LED |
109 	  TLAN_ADAPTER_USE_INTERN_10, 0xf8 },
110 	{ "Compaq Netelligent 10/100 TX UTP", TLAN_ADAPTER_ACTIVITY_LED, 0x83 },
111 	{ "Compaq Netelligent 10 T/2 PCI UTP/coax", TLAN_ADAPTER_NONE, 0x83 },
112 	{ "Compaq NetFlex-3/E",
113 	  TLAN_ADAPTER_ACTIVITY_LED |	/* EISA card */
114 	  TLAN_ADAPTER_UNMANAGED_PHY | TLAN_ADAPTER_BIT_RATE_PHY, 0x83 },
115 	{ "Compaq NetFlex-3/E",
116 	  TLAN_ADAPTER_ACTIVITY_LED, 0x83 }, /* EISA card */
117 };
118 
119 static const struct pci_device_id tlan_pci_tbl[] = {
120 	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL10,
121 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
122 	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL100,
123 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
124 	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETFLEX3I,
125 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2 },
126 	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_THUNDER,
127 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3 },
128 	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETFLEX3B,
129 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4 },
130 	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL100PI,
131 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5 },
132 	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL100D,
133 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 6 },
134 	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL100I,
135 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 7 },
136 	{ PCI_VENDOR_ID_OLICOM, PCI_DEVICE_ID_OLICOM_OC2183,
137 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 8 },
138 	{ PCI_VENDOR_ID_OLICOM, PCI_DEVICE_ID_OLICOM_OC2325,
139 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 9 },
140 	{ PCI_VENDOR_ID_OLICOM, PCI_DEVICE_ID_OLICOM_OC2326,
141 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 10 },
142 	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_NETELLIGENT_10_100_WS_5100,
143 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 11 },
144 	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_NETELLIGENT_10_T2,
145 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 12 },
146 	{ 0,}
147 };
148 MODULE_DEVICE_TABLE(pci, tlan_pci_tbl);
149 
150 static void	tlan_eisa_probe(void);
151 static void	tlan_eisa_cleanup(void);
152 static int      tlan_init(struct net_device *);
153 static int	tlan_open(struct net_device *dev);
154 static netdev_tx_t tlan_start_tx(struct sk_buff *, struct net_device *);
155 static irqreturn_t tlan_handle_interrupt(int, void *);
156 static int	tlan_close(struct net_device *);
157 static struct	net_device_stats *tlan_get_stats(struct net_device *);
158 static void	tlan_set_multicast_list(struct net_device *);
159 static int	tlan_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
160 static int      tlan_probe1(struct pci_dev *pdev, long ioaddr,
161 			    int irq, int rev, const struct pci_device_id *ent);
162 static void	tlan_tx_timeout(struct net_device *dev);
163 static void	tlan_tx_timeout_work(struct work_struct *work);
164 static int	tlan_init_one(struct pci_dev *pdev,
165 			      const struct pci_device_id *ent);
166 
167 static u32	tlan_handle_tx_eof(struct net_device *, u16);
168 static u32	tlan_handle_stat_overflow(struct net_device *, u16);
169 static u32	tlan_handle_rx_eof(struct net_device *, u16);
170 static u32	tlan_handle_dummy(struct net_device *, u16);
171 static u32	tlan_handle_tx_eoc(struct net_device *, u16);
172 static u32	tlan_handle_status_check(struct net_device *, u16);
173 static u32	tlan_handle_rx_eoc(struct net_device *, u16);
174 
175 static void	tlan_timer(unsigned long);
176 
177 static void	tlan_reset_lists(struct net_device *);
178 static void	tlan_free_lists(struct net_device *);
179 static void	tlan_print_dio(u16);
180 static void	tlan_print_list(struct tlan_list *, char *, int);
181 static void	tlan_read_and_clear_stats(struct net_device *, int);
182 static void	tlan_reset_adapter(struct net_device *);
183 static void	tlan_finish_reset(struct net_device *);
184 static void	tlan_set_mac(struct net_device *, int areg, char *mac);
185 
186 static void	tlan_phy_print(struct net_device *);
187 static void	tlan_phy_detect(struct net_device *);
188 static void	tlan_phy_power_down(struct net_device *);
189 static void	tlan_phy_power_up(struct net_device *);
190 static void	tlan_phy_reset(struct net_device *);
191 static void	tlan_phy_start_link(struct net_device *);
192 static void	tlan_phy_finish_auto_neg(struct net_device *);
193 static void     tlan_phy_monitor(unsigned long);
194 
195 /*
196   static int	tlan_phy_nop(struct net_device *);
197   static int	tlan_phy_internal_check(struct net_device *);
198   static int	tlan_phy_internal_service(struct net_device *);
199   static int	tlan_phy_dp83840a_check(struct net_device *);
200 */
201 
202 static bool	tlan_mii_read_reg(struct net_device *, u16, u16, u16 *);
203 static void	tlan_mii_send_data(u16, u32, unsigned);
204 static void	tlan_mii_sync(u16);
205 static void	tlan_mii_write_reg(struct net_device *, u16, u16, u16);
206 
207 static void	tlan_ee_send_start(u16);
208 static int	tlan_ee_send_byte(u16, u8, int);
209 static void	tlan_ee_receive_byte(u16, u8 *, int);
210 static int	tlan_ee_read_byte(struct net_device *, u8, u8 *);
211 
212 
213 static inline void
214 tlan_store_skb(struct tlan_list *tag, struct sk_buff *skb)
215 {
216 	unsigned long addr = (unsigned long)skb;
217 	tag->buffer[9].address = addr;
218 	tag->buffer[8].address = upper_32_bits(addr);
219 }
220 
221 static inline struct sk_buff *
222 tlan_get_skb(const struct tlan_list *tag)
223 {
224 	unsigned long addr;
225 
226 	addr = tag->buffer[9].address;
227 	addr |= ((unsigned long) tag->buffer[8].address << 16) << 16;
228 	return (struct sk_buff *) addr;
229 }
230 
231 static u32
232 (*tlan_int_vector[TLAN_INT_NUMBER_OF_INTS])(struct net_device *, u16) = {
233 	NULL,
234 	tlan_handle_tx_eof,
235 	tlan_handle_stat_overflow,
236 	tlan_handle_rx_eof,
237 	tlan_handle_dummy,
238 	tlan_handle_tx_eoc,
239 	tlan_handle_status_check,
240 	tlan_handle_rx_eoc
241 };
242 
243 static inline void
244 tlan_set_timer(struct net_device *dev, u32 ticks, u32 type)
245 {
246 	struct tlan_priv *priv = netdev_priv(dev);
247 	unsigned long flags = 0;
248 
249 	if (!in_irq())
250 		spin_lock_irqsave(&priv->lock, flags);
251 	if (priv->timer.function != NULL &&
252 	    priv->timer_type != TLAN_TIMER_ACTIVITY) {
253 		if (!in_irq())
254 			spin_unlock_irqrestore(&priv->lock, flags);
255 		return;
256 	}
257 	priv->timer.function = tlan_timer;
258 	if (!in_irq())
259 		spin_unlock_irqrestore(&priv->lock, flags);
260 
261 	priv->timer.data = (unsigned long) dev;
262 	priv->timer_set_at = jiffies;
263 	priv->timer_type = type;
264 	mod_timer(&priv->timer, jiffies + ticks);
265 
266 }
267 
268 
269 /*****************************************************************************
270 ******************************************************************************
271 
272 ThunderLAN driver primary functions
273 
274 these functions are more or less common to all linux network drivers.
275 
276 ******************************************************************************
277 *****************************************************************************/
278 
279 
280 
281 
282 
283 /***************************************************************
284  *	tlan_remove_one
285  *
286  *	Returns:
287  *		Nothing
288  *	Parms:
289  *		None
290  *
291  *	Goes through the TLanDevices list and frees the device
292  *	structs and memory associated with each device (lists
293  *	and buffers).  It also ureserves the IO port regions
294  *	associated with this device.
295  *
296  **************************************************************/
297 
298 
299 static void tlan_remove_one(struct pci_dev *pdev)
300 {
301 	struct net_device *dev = pci_get_drvdata(pdev);
302 	struct tlan_priv	*priv = netdev_priv(dev);
303 
304 	unregister_netdev(dev);
305 
306 	if (priv->dma_storage) {
307 		pci_free_consistent(priv->pci_dev,
308 				    priv->dma_size, priv->dma_storage,
309 				    priv->dma_storage_dma);
310 	}
311 
312 #ifdef CONFIG_PCI
313 	pci_release_regions(pdev);
314 #endif
315 
316 	free_netdev(dev);
317 
318 	cancel_work_sync(&priv->tlan_tqueue);
319 }
320 
321 static void tlan_start(struct net_device *dev)
322 {
323 	tlan_reset_lists(dev);
324 	/* NOTE: It might not be necessary to read the stats before a
325 	   reset if you don't care what the values are.
326 	*/
327 	tlan_read_and_clear_stats(dev, TLAN_IGNORE);
328 	tlan_reset_adapter(dev);
329 	netif_wake_queue(dev);
330 }
331 
332 static void tlan_stop(struct net_device *dev)
333 {
334 	struct tlan_priv *priv = netdev_priv(dev);
335 
336 	del_timer_sync(&priv->media_timer);
337 	tlan_read_and_clear_stats(dev, TLAN_RECORD);
338 	outl(TLAN_HC_AD_RST, dev->base_addr + TLAN_HOST_CMD);
339 	/* Reset and power down phy */
340 	tlan_reset_adapter(dev);
341 	if (priv->timer.function != NULL) {
342 		del_timer_sync(&priv->timer);
343 		priv->timer.function = NULL;
344 	}
345 }
346 
347 #ifdef CONFIG_PM
348 
349 static int tlan_suspend(struct pci_dev *pdev, pm_message_t state)
350 {
351 	struct net_device *dev = pci_get_drvdata(pdev);
352 
353 	if (netif_running(dev))
354 		tlan_stop(dev);
355 
356 	netif_device_detach(dev);
357 	pci_save_state(pdev);
358 	pci_disable_device(pdev);
359 	pci_wake_from_d3(pdev, false);
360 	pci_set_power_state(pdev, PCI_D3hot);
361 
362 	return 0;
363 }
364 
365 static int tlan_resume(struct pci_dev *pdev)
366 {
367 	struct net_device *dev = pci_get_drvdata(pdev);
368 	int rc = pci_enable_device(pdev);
369 
370 	if (rc)
371 		return rc;
372 	pci_restore_state(pdev);
373 	pci_enable_wake(pdev, PCI_D0, 0);
374 	netif_device_attach(dev);
375 
376 	if (netif_running(dev))
377 		tlan_start(dev);
378 
379 	return 0;
380 }
381 
382 #else /* CONFIG_PM */
383 
384 #define tlan_suspend   NULL
385 #define tlan_resume    NULL
386 
387 #endif /* CONFIG_PM */
388 
389 
390 static struct pci_driver tlan_driver = {
391 	.name		= "tlan",
392 	.id_table	= tlan_pci_tbl,
393 	.probe		= tlan_init_one,
394 	.remove		= tlan_remove_one,
395 	.suspend	= tlan_suspend,
396 	.resume		= tlan_resume,
397 };
398 
399 static int __init tlan_probe(void)
400 {
401 	int rc = -ENODEV;
402 
403 	pr_info("%s", tlan_banner);
404 
405 	TLAN_DBG(TLAN_DEBUG_PROBE, "Starting PCI Probe....\n");
406 
407 	/* Use new style PCI probing. Now the kernel will
408 	   do most of this for us */
409 	rc = pci_register_driver(&tlan_driver);
410 
411 	if (rc != 0) {
412 		pr_err("Could not register pci driver\n");
413 		goto err_out_pci_free;
414 	}
415 
416 	TLAN_DBG(TLAN_DEBUG_PROBE, "Starting EISA Probe....\n");
417 	tlan_eisa_probe();
418 
419 	pr_info("%d device%s installed, PCI: %d  EISA: %d\n",
420 		tlan_devices_installed, tlan_devices_installed == 1 ? "" : "s",
421 		tlan_have_pci, tlan_have_eisa);
422 
423 	if (tlan_devices_installed == 0) {
424 		rc = -ENODEV;
425 		goto  err_out_pci_unreg;
426 	}
427 	return 0;
428 
429 err_out_pci_unreg:
430 	pci_unregister_driver(&tlan_driver);
431 err_out_pci_free:
432 	return rc;
433 }
434 
435 
436 static int tlan_init_one(struct pci_dev *pdev,
437 				   const struct pci_device_id *ent)
438 {
439 	return tlan_probe1(pdev, -1, -1, 0, ent);
440 }
441 
442 
443 /*
444 ***************************************************************
445 *	tlan_probe1
446 *
447 *	Returns:
448 *		0 on success, error code on error
449 *	Parms:
450 *		none
451 *
452 *	The name is lower case to fit in with all the rest of
453 *	the netcard_probe names.  This function looks for
454 *	another TLan based adapter, setting it up with the
455 *	allocated device struct if one is found.
456 *	tlan_probe has been ported to the new net API and
457 *	now allocates its own device structure. This function
458 *	is also used by modules.
459 *
460 **************************************************************/
461 
462 static int tlan_probe1(struct pci_dev *pdev, long ioaddr, int irq, int rev,
463 		       const struct pci_device_id *ent)
464 {
465 
466 	struct net_device  *dev;
467 	struct tlan_priv  *priv;
468 	u16		   device_id;
469 	int		   reg, rc = -ENODEV;
470 
471 #ifdef CONFIG_PCI
472 	if (pdev) {
473 		rc = pci_enable_device(pdev);
474 		if (rc)
475 			return rc;
476 
477 		rc = pci_request_regions(pdev, tlan_signature);
478 		if (rc) {
479 			pr_err("Could not reserve IO regions\n");
480 			goto err_out;
481 		}
482 	}
483 #endif  /*  CONFIG_PCI  */
484 
485 	dev = alloc_etherdev(sizeof(struct tlan_priv));
486 	if (dev == NULL) {
487 		rc = -ENOMEM;
488 		goto err_out_regions;
489 	}
490 	SET_NETDEV_DEV(dev, &pdev->dev);
491 
492 	priv = netdev_priv(dev);
493 
494 	priv->pci_dev = pdev;
495 	priv->dev = dev;
496 
497 	/* Is this a PCI device? */
498 	if (pdev) {
499 		u32		   pci_io_base = 0;
500 
501 		priv->adapter = &board_info[ent->driver_data];
502 
503 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
504 		if (rc) {
505 			pr_err("No suitable PCI mapping available\n");
506 			goto err_out_free_dev;
507 		}
508 
509 		for (reg = 0; reg <= 5; reg++) {
510 			if (pci_resource_flags(pdev, reg) & IORESOURCE_IO) {
511 				pci_io_base = pci_resource_start(pdev, reg);
512 				TLAN_DBG(TLAN_DEBUG_GNRL,
513 					 "IO mapping is available at %x.\n",
514 					 pci_io_base);
515 				break;
516 			}
517 		}
518 		if (!pci_io_base) {
519 			pr_err("No IO mappings available\n");
520 			rc = -EIO;
521 			goto err_out_free_dev;
522 		}
523 
524 		dev->base_addr = pci_io_base;
525 		dev->irq = pdev->irq;
526 		priv->adapter_rev = pdev->revision;
527 		pci_set_master(pdev);
528 		pci_set_drvdata(pdev, dev);
529 
530 	} else	{     /* EISA card */
531 		/* This is a hack. We need to know which board structure
532 		 * is suited for this adapter */
533 		device_id = inw(ioaddr + EISA_ID2);
534 		if (device_id == 0x20F1) {
535 			priv->adapter = &board_info[13]; /* NetFlex-3/E */
536 			priv->adapter_rev = 23;		/* TLAN 2.3 */
537 		} else {
538 			priv->adapter = &board_info[14];
539 			priv->adapter_rev = 10;		/* TLAN 1.0 */
540 		}
541 		dev->base_addr = ioaddr;
542 		dev->irq = irq;
543 	}
544 
545 	/* Kernel parameters */
546 	if (dev->mem_start) {
547 		priv->aui    = dev->mem_start & 0x01;
548 		priv->duplex = ((dev->mem_start & 0x06) == 0x06) ? 0
549 			: (dev->mem_start & 0x06) >> 1;
550 		priv->speed  = ((dev->mem_start & 0x18) == 0x18) ? 0
551 			: (dev->mem_start & 0x18) >> 3;
552 
553 		if (priv->speed == 0x1)
554 			priv->speed = TLAN_SPEED_10;
555 		else if (priv->speed == 0x2)
556 			priv->speed = TLAN_SPEED_100;
557 
558 		debug = priv->debug = dev->mem_end;
559 	} else {
560 		priv->aui    = aui[boards_found];
561 		priv->speed  = speed[boards_found];
562 		priv->duplex = duplex[boards_found];
563 		priv->debug = debug;
564 	}
565 
566 	/* This will be used when we get an adapter error from
567 	 * within our irq handler */
568 	INIT_WORK(&priv->tlan_tqueue, tlan_tx_timeout_work);
569 
570 	spin_lock_init(&priv->lock);
571 
572 	rc = tlan_init(dev);
573 	if (rc) {
574 		pr_err("Could not set up device\n");
575 		goto err_out_free_dev;
576 	}
577 
578 	rc = register_netdev(dev);
579 	if (rc) {
580 		pr_err("Could not register device\n");
581 		goto err_out_uninit;
582 	}
583 
584 
585 	tlan_devices_installed++;
586 	boards_found++;
587 
588 	/* pdev is NULL if this is an EISA device */
589 	if (pdev)
590 		tlan_have_pci++;
591 	else {
592 		priv->next_device = tlan_eisa_devices;
593 		tlan_eisa_devices = dev;
594 		tlan_have_eisa++;
595 	}
596 
597 	netdev_info(dev, "irq=%2d, io=%04x, %s, Rev. %d\n",
598 		    (int)dev->irq,
599 		    (int)dev->base_addr,
600 		    priv->adapter->device_label,
601 		    priv->adapter_rev);
602 	return 0;
603 
604 err_out_uninit:
605 	pci_free_consistent(priv->pci_dev, priv->dma_size, priv->dma_storage,
606 			    priv->dma_storage_dma);
607 err_out_free_dev:
608 	free_netdev(dev);
609 err_out_regions:
610 #ifdef CONFIG_PCI
611 	if (pdev)
612 		pci_release_regions(pdev);
613 #endif
614 err_out:
615 	if (pdev)
616 		pci_disable_device(pdev);
617 	return rc;
618 }
619 
620 
621 static void tlan_eisa_cleanup(void)
622 {
623 	struct net_device *dev;
624 	struct tlan_priv *priv;
625 
626 	while (tlan_have_eisa) {
627 		dev = tlan_eisa_devices;
628 		priv = netdev_priv(dev);
629 		if (priv->dma_storage) {
630 			pci_free_consistent(priv->pci_dev, priv->dma_size,
631 					    priv->dma_storage,
632 					    priv->dma_storage_dma);
633 		}
634 		release_region(dev->base_addr, 0x10);
635 		unregister_netdev(dev);
636 		tlan_eisa_devices = priv->next_device;
637 		free_netdev(dev);
638 		tlan_have_eisa--;
639 	}
640 }
641 
642 
643 static void __exit tlan_exit(void)
644 {
645 	pci_unregister_driver(&tlan_driver);
646 
647 	if (tlan_have_eisa)
648 		tlan_eisa_cleanup();
649 
650 }
651 
652 
653 /* Module loading/unloading */
654 module_init(tlan_probe);
655 module_exit(tlan_exit);
656 
657 
658 
659 /**************************************************************
660  *	tlan_eisa_probe
661  *
662  *	Returns: 0 on success, 1 otherwise
663  *
664  *	Parms:	 None
665  *
666  *
667  *	This functions probes for EISA devices and calls
668  *	TLan_probe1 when one is found.
669  *
670  *************************************************************/
671 
672 static void  __init tlan_eisa_probe(void)
673 {
674 	long	ioaddr;
675 	int	rc = -ENODEV;
676 	int	irq;
677 	u16	device_id;
678 
679 	if (!EISA_bus) {
680 		TLAN_DBG(TLAN_DEBUG_PROBE, "No EISA bus present\n");
681 		return;
682 	}
683 
684 	/* Loop through all slots of the EISA bus */
685 	for (ioaddr = 0x1000; ioaddr < 0x9000; ioaddr += 0x1000) {
686 
687 		TLAN_DBG(TLAN_DEBUG_PROBE, "EISA_ID 0x%4x: 0x%4x\n",
688 			 (int) ioaddr + 0xc80, inw(ioaddr + EISA_ID));
689 		TLAN_DBG(TLAN_DEBUG_PROBE, "EISA_ID 0x%4x: 0x%4x\n",
690 			 (int) ioaddr + 0xc82, inw(ioaddr + EISA_ID2));
691 
692 
693 		TLAN_DBG(TLAN_DEBUG_PROBE,
694 			 "Probing for EISA adapter at IO: 0x%4x : ",
695 			 (int) ioaddr);
696 		if (request_region(ioaddr, 0x10, tlan_signature) == NULL)
697 			goto out;
698 
699 		if (inw(ioaddr + EISA_ID) != 0x110E) {
700 			release_region(ioaddr, 0x10);
701 			goto out;
702 		}
703 
704 		device_id = inw(ioaddr + EISA_ID2);
705 		if (device_id !=  0x20F1 && device_id != 0x40F1) {
706 			release_region(ioaddr, 0x10);
707 			goto out;
708 		}
709 
710 		/* check if adapter is enabled */
711 		if (inb(ioaddr + EISA_CR) != 0x1) {
712 			release_region(ioaddr, 0x10);
713 			goto out2;
714 		}
715 
716 		if (debug == 0x10)
717 			pr_info("Found one\n");
718 
719 
720 		/* Get irq from board */
721 		switch (inb(ioaddr + 0xcc0)) {
722 		case(0x10):
723 			irq = 5;
724 			break;
725 		case(0x20):
726 			irq = 9;
727 			break;
728 		case(0x40):
729 			irq = 10;
730 			break;
731 		case(0x80):
732 			irq = 11;
733 			break;
734 		default:
735 			goto out;
736 		}
737 
738 
739 		/* Setup the newly found eisa adapter */
740 		rc = tlan_probe1(NULL, ioaddr, irq,
741 				 12, NULL);
742 		continue;
743 
744 out:
745 		if (debug == 0x10)
746 			pr_info("None found\n");
747 		continue;
748 
749 out2:
750 		if (debug == 0x10)
751 			pr_info("Card found but it is not enabled, skipping\n");
752 		continue;
753 
754 	}
755 
756 }
757 
758 #ifdef CONFIG_NET_POLL_CONTROLLER
759 static void tlan_poll(struct net_device *dev)
760 {
761 	disable_irq(dev->irq);
762 	tlan_handle_interrupt(dev->irq, dev);
763 	enable_irq(dev->irq);
764 }
765 #endif
766 
767 static const struct net_device_ops tlan_netdev_ops = {
768 	.ndo_open		= tlan_open,
769 	.ndo_stop		= tlan_close,
770 	.ndo_start_xmit		= tlan_start_tx,
771 	.ndo_tx_timeout		= tlan_tx_timeout,
772 	.ndo_get_stats		= tlan_get_stats,
773 	.ndo_set_rx_mode	= tlan_set_multicast_list,
774 	.ndo_do_ioctl		= tlan_ioctl,
775 	.ndo_change_mtu		= eth_change_mtu,
776 	.ndo_set_mac_address	= eth_mac_addr,
777 	.ndo_validate_addr	= eth_validate_addr,
778 #ifdef CONFIG_NET_POLL_CONTROLLER
779 	.ndo_poll_controller	 = tlan_poll,
780 #endif
781 };
782 
783 static void tlan_get_drvinfo(struct net_device *dev,
784 			     struct ethtool_drvinfo *info)
785 {
786 	struct tlan_priv *priv = netdev_priv(dev);
787 
788 	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
789 	if (priv->pci_dev)
790 		strlcpy(info->bus_info, pci_name(priv->pci_dev),
791 			sizeof(info->bus_info));
792 	else
793 		strlcpy(info->bus_info, "EISA",	sizeof(info->bus_info));
794 	info->eedump_len = TLAN_EEPROM_SIZE;
795 }
796 
797 static int tlan_get_eeprom_len(struct net_device *dev)
798 {
799 	return TLAN_EEPROM_SIZE;
800 }
801 
802 static int tlan_get_eeprom(struct net_device *dev,
803 			   struct ethtool_eeprom *eeprom, u8 *data)
804 {
805 	int i;
806 
807 	for (i = 0; i < TLAN_EEPROM_SIZE; i++)
808 		if (tlan_ee_read_byte(dev, i, &data[i]))
809 			return -EIO;
810 
811 	return 0;
812 }
813 
814 static const struct ethtool_ops tlan_ethtool_ops = {
815 	.get_drvinfo	= tlan_get_drvinfo,
816 	.get_link	= ethtool_op_get_link,
817 	.get_eeprom_len	= tlan_get_eeprom_len,
818 	.get_eeprom	= tlan_get_eeprom,
819 };
820 
821 /***************************************************************
822  *	tlan_init
823  *
824  *	Returns:
825  *		0 on success, error code otherwise.
826  *	Parms:
827  *		dev	The structure of the device to be
828  *			init'ed.
829  *
830  *	This function completes the initialization of the
831  *	device structure and driver.  It reserves the IO
832  *	addresses, allocates memory for the lists and bounce
833  *	buffers, retrieves the MAC address from the eeprom
834  *	and assignes the device's methods.
835  *
836  **************************************************************/
837 
838 static int tlan_init(struct net_device *dev)
839 {
840 	int		dma_size;
841 	int		err;
842 	int		i;
843 	struct tlan_priv	*priv;
844 
845 	priv = netdev_priv(dev);
846 
847 	dma_size = (TLAN_NUM_RX_LISTS + TLAN_NUM_TX_LISTS)
848 		* (sizeof(struct tlan_list));
849 	priv->dma_storage = pci_alloc_consistent(priv->pci_dev,
850 						 dma_size,
851 						 &priv->dma_storage_dma);
852 	priv->dma_size = dma_size;
853 
854 	if (priv->dma_storage == NULL) {
855 		pr_err("Could not allocate lists and buffers for %s\n",
856 		       dev->name);
857 		return -ENOMEM;
858 	}
859 	memset(priv->dma_storage, 0, dma_size);
860 	priv->rx_list = (struct tlan_list *)
861 		ALIGN((unsigned long)priv->dma_storage, 8);
862 	priv->rx_list_dma = ALIGN(priv->dma_storage_dma, 8);
863 	priv->tx_list = priv->rx_list + TLAN_NUM_RX_LISTS;
864 	priv->tx_list_dma =
865 		priv->rx_list_dma + sizeof(struct tlan_list)*TLAN_NUM_RX_LISTS;
866 
867 	err = 0;
868 	for (i = 0; i < ETH_ALEN; i++)
869 		err |= tlan_ee_read_byte(dev,
870 					 (u8) priv->adapter->addr_ofs + i,
871 					 (u8 *) &dev->dev_addr[i]);
872 	if (err) {
873 		pr_err("%s: Error reading MAC from eeprom: %d\n",
874 		       dev->name, err);
875 	}
876 	/* Olicom OC-2325/OC-2326 have the address byte-swapped */
877 	if (priv->adapter->addr_ofs == 0xf8) {
878 		for (i = 0; i < ETH_ALEN; i += 2) {
879 			char tmp = dev->dev_addr[i];
880 			dev->dev_addr[i] = dev->dev_addr[i + 1];
881 			dev->dev_addr[i + 1] = tmp;
882 		}
883 	}
884 
885 	netif_carrier_off(dev);
886 
887 	/* Device methods */
888 	dev->netdev_ops = &tlan_netdev_ops;
889 	dev->ethtool_ops = &tlan_ethtool_ops;
890 	dev->watchdog_timeo = TX_TIMEOUT;
891 
892 	return 0;
893 
894 }
895 
896 
897 
898 
899 /***************************************************************
900  *	tlan_open
901  *
902  *	Returns:
903  *		0 on success, error code otherwise.
904  *	Parms:
905  *		dev	Structure of device to be opened.
906  *
907  *	This routine puts the driver and TLAN adapter in a
908  *	state where it is ready to send and receive packets.
909  *	It allocates the IRQ, resets and brings the adapter
910  *	out of reset, and allows interrupts.  It also delays
911  *	the startup for autonegotiation or sends a Rx GO
912  *	command to the adapter, as appropriate.
913  *
914  **************************************************************/
915 
916 static int tlan_open(struct net_device *dev)
917 {
918 	struct tlan_priv	*priv = netdev_priv(dev);
919 	int		err;
920 
921 	priv->tlan_rev = tlan_dio_read8(dev->base_addr, TLAN_DEF_REVISION);
922 	err = request_irq(dev->irq, tlan_handle_interrupt, IRQF_SHARED,
923 			  dev->name, dev);
924 
925 	if (err) {
926 		netdev_err(dev, "Cannot open because IRQ %d is already in use\n",
927 			   dev->irq);
928 		return err;
929 	}
930 
931 	init_timer(&priv->timer);
932 	init_timer(&priv->media_timer);
933 
934 	tlan_start(dev);
935 
936 	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Opened.  TLAN Chip Rev: %x\n",
937 		 dev->name, priv->tlan_rev);
938 
939 	return 0;
940 
941 }
942 
943 
944 
945 /**************************************************************
946  *	tlan_ioctl
947  *
948  *	Returns:
949  *		0 on success, error code otherwise
950  *	Params:
951  *		dev	structure of device to receive ioctl.
952  *
953  *		rq	ifreq structure to hold userspace data.
954  *
955  *		cmd	ioctl command.
956  *
957  *
958  *************************************************************/
959 
960 static int tlan_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
961 {
962 	struct tlan_priv *priv = netdev_priv(dev);
963 	struct mii_ioctl_data *data = if_mii(rq);
964 	u32 phy   = priv->phy[priv->phy_num];
965 
966 	if (!priv->phy_online)
967 		return -EAGAIN;
968 
969 	switch (cmd) {
970 	case SIOCGMIIPHY:		/* get address of MII PHY in use. */
971 		data->phy_id = phy;
972 
973 
974 	case SIOCGMIIREG:		/* read MII PHY register. */
975 		tlan_mii_read_reg(dev, data->phy_id & 0x1f,
976 				  data->reg_num & 0x1f, &data->val_out);
977 		return 0;
978 
979 
980 	case SIOCSMIIREG:		/* write MII PHY register. */
981 		tlan_mii_write_reg(dev, data->phy_id & 0x1f,
982 				   data->reg_num & 0x1f, data->val_in);
983 		return 0;
984 	default:
985 		return -EOPNOTSUPP;
986 	}
987 }
988 
989 
990 /***************************************************************
991  *	tlan_tx_timeout
992  *
993  *	Returns: nothing
994  *
995  *	Params:
996  *		dev	structure of device which timed out
997  *			during transmit.
998  *
999  **************************************************************/
1000 
1001 static void tlan_tx_timeout(struct net_device *dev)
1002 {
1003 
1004 	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Transmit timed out.\n", dev->name);
1005 
1006 	/* Ok so we timed out, lets see what we can do about it...*/
1007 	tlan_free_lists(dev);
1008 	tlan_reset_lists(dev);
1009 	tlan_read_and_clear_stats(dev, TLAN_IGNORE);
1010 	tlan_reset_adapter(dev);
1011 	dev->trans_start = jiffies; /* prevent tx timeout */
1012 	netif_wake_queue(dev);
1013 
1014 }
1015 
1016 
1017 /***************************************************************
1018  *	tlan_tx_timeout_work
1019  *
1020  *	Returns: nothing
1021  *
1022  *	Params:
1023  *		work	work item of device which timed out
1024  *
1025  **************************************************************/
1026 
1027 static void tlan_tx_timeout_work(struct work_struct *work)
1028 {
1029 	struct tlan_priv	*priv =
1030 		container_of(work, struct tlan_priv, tlan_tqueue);
1031 
1032 	tlan_tx_timeout(priv->dev);
1033 }
1034 
1035 
1036 
1037 /***************************************************************
1038  *	tlan_start_tx
1039  *
1040  *	Returns:
1041  *		0 on success, non-zero on failure.
1042  *	Parms:
1043  *		skb	A pointer to the sk_buff containing the
1044  *			frame to be sent.
1045  *		dev	The device to send the data on.
1046  *
1047  *	This function adds a frame to the Tx list to be sent
1048  *	ASAP.  First it	verifies that the adapter is ready and
1049  *	there is room in the queue.  Then it sets up the next
1050  *	available list, copies the frame to the	corresponding
1051  *	buffer.  If the adapter Tx channel is idle, it gives
1052  *	the adapter a Tx Go command on the list, otherwise it
1053  *	sets the forward address of the previous list to point
1054  *	to this one.  Then it frees the sk_buff.
1055  *
1056  **************************************************************/
1057 
1058 static netdev_tx_t tlan_start_tx(struct sk_buff *skb, struct net_device *dev)
1059 {
1060 	struct tlan_priv *priv = netdev_priv(dev);
1061 	dma_addr_t	tail_list_phys;
1062 	struct tlan_list	*tail_list;
1063 	unsigned long	flags;
1064 	unsigned int    txlen;
1065 
1066 	if (!priv->phy_online) {
1067 		TLAN_DBG(TLAN_DEBUG_TX, "TRANSMIT:  %s PHY is not ready\n",
1068 			 dev->name);
1069 		dev_kfree_skb_any(skb);
1070 		return NETDEV_TX_OK;
1071 	}
1072 
1073 	if (skb_padto(skb, TLAN_MIN_FRAME_SIZE))
1074 		return NETDEV_TX_OK;
1075 	txlen = max(skb->len, (unsigned int)TLAN_MIN_FRAME_SIZE);
1076 
1077 	tail_list = priv->tx_list + priv->tx_tail;
1078 	tail_list_phys =
1079 		priv->tx_list_dma + sizeof(struct tlan_list)*priv->tx_tail;
1080 
1081 	if (tail_list->c_stat != TLAN_CSTAT_UNUSED) {
1082 		TLAN_DBG(TLAN_DEBUG_TX,
1083 			 "TRANSMIT:  %s is busy (Head=%d Tail=%d)\n",
1084 			 dev->name, priv->tx_head, priv->tx_tail);
1085 		netif_stop_queue(dev);
1086 		priv->tx_busy_count++;
1087 		return NETDEV_TX_BUSY;
1088 	}
1089 
1090 	tail_list->forward = 0;
1091 
1092 	tail_list->buffer[0].address = pci_map_single(priv->pci_dev,
1093 						      skb->data, txlen,
1094 						      PCI_DMA_TODEVICE);
1095 	tlan_store_skb(tail_list, skb);
1096 
1097 	tail_list->frame_size = (u16) txlen;
1098 	tail_list->buffer[0].count = TLAN_LAST_BUFFER | (u32) txlen;
1099 	tail_list->buffer[1].count = 0;
1100 	tail_list->buffer[1].address = 0;
1101 
1102 	spin_lock_irqsave(&priv->lock, flags);
1103 	tail_list->c_stat = TLAN_CSTAT_READY;
1104 	if (!priv->tx_in_progress) {
1105 		priv->tx_in_progress = 1;
1106 		TLAN_DBG(TLAN_DEBUG_TX,
1107 			 "TRANSMIT:  Starting TX on buffer %d\n",
1108 			 priv->tx_tail);
1109 		outl(tail_list_phys, dev->base_addr + TLAN_CH_PARM);
1110 		outl(TLAN_HC_GO, dev->base_addr + TLAN_HOST_CMD);
1111 	} else {
1112 		TLAN_DBG(TLAN_DEBUG_TX,
1113 			 "TRANSMIT:  Adding buffer %d to TX channel\n",
1114 			 priv->tx_tail);
1115 		if (priv->tx_tail == 0) {
1116 			(priv->tx_list + (TLAN_NUM_TX_LISTS - 1))->forward
1117 				= tail_list_phys;
1118 		} else {
1119 			(priv->tx_list + (priv->tx_tail - 1))->forward
1120 				= tail_list_phys;
1121 		}
1122 	}
1123 	spin_unlock_irqrestore(&priv->lock, flags);
1124 
1125 	CIRC_INC(priv->tx_tail, TLAN_NUM_TX_LISTS);
1126 
1127 	return NETDEV_TX_OK;
1128 
1129 }
1130 
1131 
1132 
1133 
1134 /***************************************************************
1135  *	tlan_handle_interrupt
1136  *
1137  *	Returns:
1138  *		Nothing
1139  *	Parms:
1140  *		irq	The line on which the interrupt
1141  *			occurred.
1142  *		dev_id	A pointer to the device assigned to
1143  *			this irq line.
1144  *
1145  *	This function handles an interrupt generated by its
1146  *	assigned TLAN adapter.  The function deactivates
1147  *	interrupts on its adapter, records the type of
1148  *	interrupt, executes the appropriate subhandler, and
1149  *	acknowdges the interrupt to the adapter (thus
1150  *	re-enabling adapter interrupts.
1151  *
1152  **************************************************************/
1153 
1154 static irqreturn_t tlan_handle_interrupt(int irq, void *dev_id)
1155 {
1156 	struct net_device	*dev = dev_id;
1157 	struct tlan_priv *priv = netdev_priv(dev);
1158 	u16		host_int;
1159 	u16		type;
1160 
1161 	spin_lock(&priv->lock);
1162 
1163 	host_int = inw(dev->base_addr + TLAN_HOST_INT);
1164 	type = (host_int & TLAN_HI_IT_MASK) >> 2;
1165 	if (type) {
1166 		u32	ack;
1167 		u32	host_cmd;
1168 
1169 		outw(host_int, dev->base_addr + TLAN_HOST_INT);
1170 		ack = tlan_int_vector[type](dev, host_int);
1171 
1172 		if (ack) {
1173 			host_cmd = TLAN_HC_ACK | ack | (type << 18);
1174 			outl(host_cmd, dev->base_addr + TLAN_HOST_CMD);
1175 		}
1176 	}
1177 
1178 	spin_unlock(&priv->lock);
1179 
1180 	return IRQ_RETVAL(type);
1181 }
1182 
1183 
1184 
1185 
1186 /***************************************************************
1187  *	tlan_close
1188  *
1189  *	Returns:
1190  *		An error code.
1191  *	Parms:
1192  *		dev	The device structure of the device to
1193  *			close.
1194  *
1195  *	This function shuts down the adapter.  It records any
1196  *	stats, puts the adapter into reset state, deactivates
1197  *	its time as needed, and	frees the irq it is using.
1198  *
1199  **************************************************************/
1200 
1201 static int tlan_close(struct net_device *dev)
1202 {
1203 	tlan_stop(dev);
1204 
1205 	free_irq(dev->irq, dev);
1206 	tlan_free_lists(dev);
1207 	TLAN_DBG(TLAN_DEBUG_GNRL, "Device %s closed.\n", dev->name);
1208 
1209 	return 0;
1210 
1211 }
1212 
1213 
1214 
1215 
1216 /***************************************************************
1217  *	tlan_get_stats
1218  *
1219  *	Returns:
1220  *		A pointer to the device's statistics structure.
1221  *	Parms:
1222  *		dev	The device structure to return the
1223  *			stats for.
1224  *
1225  *	This function updates the devices statistics by reading
1226  *	the TLAN chip's onboard registers.  Then it returns the
1227  *	address of the statistics structure.
1228  *
1229  **************************************************************/
1230 
1231 static struct net_device_stats *tlan_get_stats(struct net_device *dev)
1232 {
1233 	struct tlan_priv	*priv = netdev_priv(dev);
1234 	int i;
1235 
1236 	/* Should only read stats if open ? */
1237 	tlan_read_and_clear_stats(dev, TLAN_RECORD);
1238 
1239 	TLAN_DBG(TLAN_DEBUG_RX, "RECEIVE:  %s EOC count = %d\n", dev->name,
1240 		 priv->rx_eoc_count);
1241 	TLAN_DBG(TLAN_DEBUG_TX, "TRANSMIT:  %s Busy count = %d\n", dev->name,
1242 		 priv->tx_busy_count);
1243 	if (debug & TLAN_DEBUG_GNRL) {
1244 		tlan_print_dio(dev->base_addr);
1245 		tlan_phy_print(dev);
1246 	}
1247 	if (debug & TLAN_DEBUG_LIST) {
1248 		for (i = 0; i < TLAN_NUM_RX_LISTS; i++)
1249 			tlan_print_list(priv->rx_list + i, "RX", i);
1250 		for (i = 0; i < TLAN_NUM_TX_LISTS; i++)
1251 			tlan_print_list(priv->tx_list + i, "TX", i);
1252 	}
1253 
1254 	return &dev->stats;
1255 
1256 }
1257 
1258 
1259 
1260 
1261 /***************************************************************
1262  *	tlan_set_multicast_list
1263  *
1264  *	Returns:
1265  *		Nothing
1266  *	Parms:
1267  *		dev	The device structure to set the
1268  *			multicast list for.
1269  *
1270  *	This function sets the TLAN adaptor to various receive
1271  *	modes.  If the IFF_PROMISC flag is set, promiscuous
1272  *	mode is acitviated.  Otherwise,	promiscuous mode is
1273  *	turned off.  If the IFF_ALLMULTI flag is set, then
1274  *	the hash table is set to receive all group addresses.
1275  *	Otherwise, the first three multicast addresses are
1276  *	stored in AREG_1-3, and the rest are selected via the
1277  *	hash table, as necessary.
1278  *
1279  **************************************************************/
1280 
1281 static void tlan_set_multicast_list(struct net_device *dev)
1282 {
1283 	struct netdev_hw_addr *ha;
1284 	u32			hash1 = 0;
1285 	u32			hash2 = 0;
1286 	int			i;
1287 	u32			offset;
1288 	u8			tmp;
1289 
1290 	if (dev->flags & IFF_PROMISC) {
1291 		tmp = tlan_dio_read8(dev->base_addr, TLAN_NET_CMD);
1292 		tlan_dio_write8(dev->base_addr,
1293 				TLAN_NET_CMD, tmp | TLAN_NET_CMD_CAF);
1294 	} else {
1295 		tmp = tlan_dio_read8(dev->base_addr, TLAN_NET_CMD);
1296 		tlan_dio_write8(dev->base_addr,
1297 				TLAN_NET_CMD, tmp & ~TLAN_NET_CMD_CAF);
1298 		if (dev->flags & IFF_ALLMULTI) {
1299 			for (i = 0; i < 3; i++)
1300 				tlan_set_mac(dev, i + 1, NULL);
1301 			tlan_dio_write32(dev->base_addr, TLAN_HASH_1,
1302 					 0xffffffff);
1303 			tlan_dio_write32(dev->base_addr, TLAN_HASH_2,
1304 					 0xffffffff);
1305 		} else {
1306 			i = 0;
1307 			netdev_for_each_mc_addr(ha, dev) {
1308 				if (i < 3) {
1309 					tlan_set_mac(dev, i + 1,
1310 						     (char *) &ha->addr);
1311 				} else {
1312 					offset =
1313 						tlan_hash_func((u8 *)&ha->addr);
1314 					if (offset < 32)
1315 						hash1 |= (1 << offset);
1316 					else
1317 						hash2 |= (1 << (offset - 32));
1318 				}
1319 				i++;
1320 			}
1321 			for ( ; i < 3; i++)
1322 				tlan_set_mac(dev, i + 1, NULL);
1323 			tlan_dio_write32(dev->base_addr, TLAN_HASH_1, hash1);
1324 			tlan_dio_write32(dev->base_addr, TLAN_HASH_2, hash2);
1325 		}
1326 	}
1327 
1328 }
1329 
1330 
1331 
1332 /*****************************************************************************
1333 ******************************************************************************
1334 
1335 ThunderLAN driver interrupt vectors and table
1336 
1337 please see chap. 4, "Interrupt Handling" of the "ThunderLAN
1338 Programmer's Guide" for more informations on handling interrupts
1339 generated by TLAN based adapters.
1340 
1341 ******************************************************************************
1342 *****************************************************************************/
1343 
1344 
1345 
1346 
1347 /***************************************************************
1348  *	tlan_handle_tx_eof
1349  *
1350  *	Returns:
1351  *		1
1352  *	Parms:
1353  *		dev		Device assigned the IRQ that was
1354  *				raised.
1355  *		host_int	The contents of the HOST_INT
1356  *				port.
1357  *
1358  *	This function handles Tx EOF interrupts which are raised
1359  *	by the adapter when it has completed sending the
1360  *	contents of a buffer.  If detemines which list/buffer
1361  *	was completed and resets it.  If the buffer was the last
1362  *	in the channel (EOC), then the function checks to see if
1363  *	another buffer is ready to send, and if so, sends a Tx
1364  *	Go command.  Finally, the driver activates/continues the
1365  *	activity LED.
1366  *
1367  **************************************************************/
1368 
1369 static u32 tlan_handle_tx_eof(struct net_device *dev, u16 host_int)
1370 {
1371 	struct tlan_priv	*priv = netdev_priv(dev);
1372 	int		eoc = 0;
1373 	struct tlan_list	*head_list;
1374 	dma_addr_t	head_list_phys;
1375 	u32		ack = 0;
1376 	u16		tmp_c_stat;
1377 
1378 	TLAN_DBG(TLAN_DEBUG_TX,
1379 		 "TRANSMIT:  Handling TX EOF (Head=%d Tail=%d)\n",
1380 		 priv->tx_head, priv->tx_tail);
1381 	head_list = priv->tx_list + priv->tx_head;
1382 
1383 	while (((tmp_c_stat = head_list->c_stat) & TLAN_CSTAT_FRM_CMP)
1384 	       && (ack < 255)) {
1385 		struct sk_buff *skb = tlan_get_skb(head_list);
1386 
1387 		ack++;
1388 		pci_unmap_single(priv->pci_dev, head_list->buffer[0].address,
1389 				 max(skb->len,
1390 				     (unsigned int)TLAN_MIN_FRAME_SIZE),
1391 				 PCI_DMA_TODEVICE);
1392 		dev_kfree_skb_any(skb);
1393 		head_list->buffer[8].address = 0;
1394 		head_list->buffer[9].address = 0;
1395 
1396 		if (tmp_c_stat & TLAN_CSTAT_EOC)
1397 			eoc = 1;
1398 
1399 		dev->stats.tx_bytes += head_list->frame_size;
1400 
1401 		head_list->c_stat = TLAN_CSTAT_UNUSED;
1402 		netif_start_queue(dev);
1403 		CIRC_INC(priv->tx_head, TLAN_NUM_TX_LISTS);
1404 		head_list = priv->tx_list + priv->tx_head;
1405 	}
1406 
1407 	if (!ack)
1408 		netdev_info(dev,
1409 			    "Received interrupt for uncompleted TX frame\n");
1410 
1411 	if (eoc) {
1412 		TLAN_DBG(TLAN_DEBUG_TX,
1413 			 "TRANSMIT:  handling TX EOC (Head=%d Tail=%d)\n",
1414 			 priv->tx_head, priv->tx_tail);
1415 		head_list = priv->tx_list + priv->tx_head;
1416 		head_list_phys = priv->tx_list_dma
1417 			+ sizeof(struct tlan_list)*priv->tx_head;
1418 		if ((head_list->c_stat & TLAN_CSTAT_READY)
1419 		    == TLAN_CSTAT_READY) {
1420 			outl(head_list_phys, dev->base_addr + TLAN_CH_PARM);
1421 			ack |= TLAN_HC_GO;
1422 		} else {
1423 			priv->tx_in_progress = 0;
1424 		}
1425 	}
1426 
1427 	if (priv->adapter->flags & TLAN_ADAPTER_ACTIVITY_LED) {
1428 		tlan_dio_write8(dev->base_addr,
1429 				TLAN_LED_REG, TLAN_LED_LINK | TLAN_LED_ACT);
1430 		if (priv->timer.function == NULL) {
1431 			priv->timer.function = tlan_timer;
1432 			priv->timer.data = (unsigned long) dev;
1433 			priv->timer.expires = jiffies + TLAN_TIMER_ACT_DELAY;
1434 			priv->timer_set_at = jiffies;
1435 			priv->timer_type = TLAN_TIMER_ACTIVITY;
1436 			add_timer(&priv->timer);
1437 		} else if (priv->timer_type == TLAN_TIMER_ACTIVITY) {
1438 			priv->timer_set_at = jiffies;
1439 		}
1440 	}
1441 
1442 	return ack;
1443 
1444 }
1445 
1446 
1447 
1448 
1449 /***************************************************************
1450  *	TLan_HandleStatOverflow
1451  *
1452  *	Returns:
1453  *		1
1454  *	Parms:
1455  *		dev		Device assigned the IRQ that was
1456  *				raised.
1457  *		host_int	The contents of the HOST_INT
1458  *				port.
1459  *
1460  *	This function handles the Statistics Overflow interrupt
1461  *	which means that one or more of the TLAN statistics
1462  *	registers has reached 1/2 capacity and needs to be read.
1463  *
1464  **************************************************************/
1465 
1466 static u32 tlan_handle_stat_overflow(struct net_device *dev, u16 host_int)
1467 {
1468 	tlan_read_and_clear_stats(dev, TLAN_RECORD);
1469 
1470 	return 1;
1471 
1472 }
1473 
1474 
1475 
1476 
1477 /***************************************************************
1478  *	TLan_HandleRxEOF
1479  *
1480  *	Returns:
1481  *		1
1482  *	Parms:
1483  *		dev		Device assigned the IRQ that was
1484  *				raised.
1485  *		host_int	The contents of the HOST_INT
1486  *				port.
1487  *
1488  *	This function handles the Rx EOF interrupt which
1489  *	indicates a frame has been received by the adapter from
1490  *	the net and the frame has been transferred to memory.
1491  *	The function determines the bounce buffer the frame has
1492  *	been loaded into, creates a new sk_buff big enough to
1493  *	hold the frame, and sends it to protocol stack.  It
1494  *	then resets the used buffer and appends it to the end
1495  *	of the list.  If the frame was the last in the Rx
1496  *	channel (EOC), the function restarts the receive channel
1497  *	by sending an Rx Go command to the adapter.  Then it
1498  *	activates/continues the activity LED.
1499  *
1500  **************************************************************/
1501 
1502 static u32 tlan_handle_rx_eof(struct net_device *dev, u16 host_int)
1503 {
1504 	struct tlan_priv	*priv = netdev_priv(dev);
1505 	u32		ack = 0;
1506 	int		eoc = 0;
1507 	struct tlan_list	*head_list;
1508 	struct sk_buff	*skb;
1509 	struct tlan_list	*tail_list;
1510 	u16		tmp_c_stat;
1511 	dma_addr_t	head_list_phys;
1512 
1513 	TLAN_DBG(TLAN_DEBUG_RX, "RECEIVE:  handling RX EOF (Head=%d Tail=%d)\n",
1514 		 priv->rx_head, priv->rx_tail);
1515 	head_list = priv->rx_list + priv->rx_head;
1516 	head_list_phys =
1517 		priv->rx_list_dma + sizeof(struct tlan_list)*priv->rx_head;
1518 
1519 	while (((tmp_c_stat = head_list->c_stat) & TLAN_CSTAT_FRM_CMP)
1520 	       && (ack < 255)) {
1521 		dma_addr_t frame_dma = head_list->buffer[0].address;
1522 		u32 frame_size = head_list->frame_size;
1523 		struct sk_buff *new_skb;
1524 
1525 		ack++;
1526 		if (tmp_c_stat & TLAN_CSTAT_EOC)
1527 			eoc = 1;
1528 
1529 		new_skb = netdev_alloc_skb_ip_align(dev,
1530 						    TLAN_MAX_FRAME_SIZE + 5);
1531 		if (!new_skb)
1532 			goto drop_and_reuse;
1533 
1534 		skb = tlan_get_skb(head_list);
1535 		pci_unmap_single(priv->pci_dev, frame_dma,
1536 				 TLAN_MAX_FRAME_SIZE, PCI_DMA_FROMDEVICE);
1537 		skb_put(skb, frame_size);
1538 
1539 		dev->stats.rx_bytes += frame_size;
1540 
1541 		skb->protocol = eth_type_trans(skb, dev);
1542 		netif_rx(skb);
1543 
1544 		head_list->buffer[0].address =
1545 			pci_map_single(priv->pci_dev, new_skb->data,
1546 				       TLAN_MAX_FRAME_SIZE, PCI_DMA_FROMDEVICE);
1547 
1548 		tlan_store_skb(head_list, new_skb);
1549 drop_and_reuse:
1550 		head_list->forward = 0;
1551 		head_list->c_stat = 0;
1552 		tail_list = priv->rx_list + priv->rx_tail;
1553 		tail_list->forward = head_list_phys;
1554 
1555 		CIRC_INC(priv->rx_head, TLAN_NUM_RX_LISTS);
1556 		CIRC_INC(priv->rx_tail, TLAN_NUM_RX_LISTS);
1557 		head_list = priv->rx_list + priv->rx_head;
1558 		head_list_phys = priv->rx_list_dma
1559 			+ sizeof(struct tlan_list)*priv->rx_head;
1560 	}
1561 
1562 	if (!ack)
1563 		netdev_info(dev,
1564 			    "Received interrupt for uncompleted RX frame\n");
1565 
1566 
1567 	if (eoc) {
1568 		TLAN_DBG(TLAN_DEBUG_RX,
1569 			 "RECEIVE:  handling RX EOC (Head=%d Tail=%d)\n",
1570 			 priv->rx_head, priv->rx_tail);
1571 		head_list = priv->rx_list + priv->rx_head;
1572 		head_list_phys = priv->rx_list_dma
1573 			+ sizeof(struct tlan_list)*priv->rx_head;
1574 		outl(head_list_phys, dev->base_addr + TLAN_CH_PARM);
1575 		ack |= TLAN_HC_GO | TLAN_HC_RT;
1576 		priv->rx_eoc_count++;
1577 	}
1578 
1579 	if (priv->adapter->flags & TLAN_ADAPTER_ACTIVITY_LED) {
1580 		tlan_dio_write8(dev->base_addr,
1581 				TLAN_LED_REG, TLAN_LED_LINK | TLAN_LED_ACT);
1582 		if (priv->timer.function == NULL)  {
1583 			priv->timer.function = tlan_timer;
1584 			priv->timer.data = (unsigned long) dev;
1585 			priv->timer.expires = jiffies + TLAN_TIMER_ACT_DELAY;
1586 			priv->timer_set_at = jiffies;
1587 			priv->timer_type = TLAN_TIMER_ACTIVITY;
1588 			add_timer(&priv->timer);
1589 		} else if (priv->timer_type == TLAN_TIMER_ACTIVITY) {
1590 			priv->timer_set_at = jiffies;
1591 		}
1592 	}
1593 
1594 	return ack;
1595 
1596 }
1597 
1598 
1599 
1600 
1601 /***************************************************************
1602  *	tlan_handle_dummy
1603  *
1604  *	Returns:
1605  *		1
1606  *	Parms:
1607  *		dev		Device assigned the IRQ that was
1608  *				raised.
1609  *		host_int	The contents of the HOST_INT
1610  *				port.
1611  *
1612  *	This function handles the Dummy interrupt, which is
1613  *	raised whenever a test interrupt is generated by setting
1614  *	the Req_Int bit of HOST_CMD to 1.
1615  *
1616  **************************************************************/
1617 
1618 static u32 tlan_handle_dummy(struct net_device *dev, u16 host_int)
1619 {
1620 	netdev_info(dev, "Test interrupt\n");
1621 	return 1;
1622 
1623 }
1624 
1625 
1626 
1627 
1628 /***************************************************************
1629  *	tlan_handle_tx_eoc
1630  *
1631  *	Returns:
1632  *		1
1633  *	Parms:
1634  *		dev		Device assigned the IRQ that was
1635  *				raised.
1636  *		host_int	The contents of the HOST_INT
1637  *				port.
1638  *
1639  *	This driver is structured to determine EOC occurrences by
1640  *	reading the CSTAT member of the list structure.  Tx EOC
1641  *	interrupts are disabled via the DIO INTDIS register.
1642  *	However, TLAN chips before revision 3.0 didn't have this
1643  *	functionality, so process EOC events if this is the
1644  *	case.
1645  *
1646  **************************************************************/
1647 
1648 static u32 tlan_handle_tx_eoc(struct net_device *dev, u16 host_int)
1649 {
1650 	struct tlan_priv	*priv = netdev_priv(dev);
1651 	struct tlan_list		*head_list;
1652 	dma_addr_t		head_list_phys;
1653 	u32			ack = 1;
1654 
1655 	host_int = 0;
1656 	if (priv->tlan_rev < 0x30) {
1657 		TLAN_DBG(TLAN_DEBUG_TX,
1658 			 "TRANSMIT:  handling TX EOC (Head=%d Tail=%d) -- IRQ\n",
1659 			 priv->tx_head, priv->tx_tail);
1660 		head_list = priv->tx_list + priv->tx_head;
1661 		head_list_phys = priv->tx_list_dma
1662 			+ sizeof(struct tlan_list)*priv->tx_head;
1663 		if ((head_list->c_stat & TLAN_CSTAT_READY)
1664 		    == TLAN_CSTAT_READY) {
1665 			netif_stop_queue(dev);
1666 			outl(head_list_phys, dev->base_addr + TLAN_CH_PARM);
1667 			ack |= TLAN_HC_GO;
1668 		} else {
1669 			priv->tx_in_progress = 0;
1670 		}
1671 	}
1672 
1673 	return ack;
1674 
1675 }
1676 
1677 
1678 
1679 
1680 /***************************************************************
1681  *	tlan_handle_status_check
1682  *
1683  *	Returns:
1684  *		0 if Adapter check, 1 if Network Status check.
1685  *	Parms:
1686  *		dev		Device assigned the IRQ that was
1687  *				raised.
1688  *		host_int	The contents of the HOST_INT
1689  *				port.
1690  *
1691  *	This function handles Adapter Check/Network Status
1692  *	interrupts generated by the adapter.  It checks the
1693  *	vector in the HOST_INT register to determine if it is
1694  *	an Adapter Check interrupt.  If so, it resets the
1695  *	adapter.  Otherwise it clears the status registers
1696  *	and services the PHY.
1697  *
1698  **************************************************************/
1699 
1700 static u32 tlan_handle_status_check(struct net_device *dev, u16 host_int)
1701 {
1702 	struct tlan_priv	*priv = netdev_priv(dev);
1703 	u32		ack;
1704 	u32		error;
1705 	u8		net_sts;
1706 	u32		phy;
1707 	u16		tlphy_ctl;
1708 	u16		tlphy_sts;
1709 
1710 	ack = 1;
1711 	if (host_int & TLAN_HI_IV_MASK) {
1712 		netif_stop_queue(dev);
1713 		error = inl(dev->base_addr + TLAN_CH_PARM);
1714 		netdev_info(dev, "Adaptor Error = 0x%x\n", error);
1715 		tlan_read_and_clear_stats(dev, TLAN_RECORD);
1716 		outl(TLAN_HC_AD_RST, dev->base_addr + TLAN_HOST_CMD);
1717 
1718 		schedule_work(&priv->tlan_tqueue);
1719 
1720 		netif_wake_queue(dev);
1721 		ack = 0;
1722 	} else {
1723 		TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Status Check\n", dev->name);
1724 		phy = priv->phy[priv->phy_num];
1725 
1726 		net_sts = tlan_dio_read8(dev->base_addr, TLAN_NET_STS);
1727 		if (net_sts) {
1728 			tlan_dio_write8(dev->base_addr, TLAN_NET_STS, net_sts);
1729 			TLAN_DBG(TLAN_DEBUG_GNRL, "%s:    Net_Sts = %x\n",
1730 				 dev->name, (unsigned) net_sts);
1731 		}
1732 		if ((net_sts & TLAN_NET_STS_MIRQ) &&  (priv->phy_num == 0)) {
1733 			tlan_mii_read_reg(dev, phy, TLAN_TLPHY_STS, &tlphy_sts);
1734 			tlan_mii_read_reg(dev, phy, TLAN_TLPHY_CTL, &tlphy_ctl);
1735 			if (!(tlphy_sts & TLAN_TS_POLOK) &&
1736 			    !(tlphy_ctl & TLAN_TC_SWAPOL)) {
1737 				tlphy_ctl |= TLAN_TC_SWAPOL;
1738 				tlan_mii_write_reg(dev, phy, TLAN_TLPHY_CTL,
1739 						   tlphy_ctl);
1740 			} else if ((tlphy_sts & TLAN_TS_POLOK) &&
1741 				   (tlphy_ctl & TLAN_TC_SWAPOL)) {
1742 				tlphy_ctl &= ~TLAN_TC_SWAPOL;
1743 				tlan_mii_write_reg(dev, phy, TLAN_TLPHY_CTL,
1744 						   tlphy_ctl);
1745 			}
1746 
1747 			if (debug)
1748 				tlan_phy_print(dev);
1749 		}
1750 	}
1751 
1752 	return ack;
1753 
1754 }
1755 
1756 
1757 
1758 
1759 /***************************************************************
1760  *	tlan_handle_rx_eoc
1761  *
1762  *	Returns:
1763  *		1
1764  *	Parms:
1765  *		dev		Device assigned the IRQ that was
1766  *				raised.
1767  *		host_int	The contents of the HOST_INT
1768  *				port.
1769  *
1770  *	This driver is structured to determine EOC occurrences by
1771  *	reading the CSTAT member of the list structure.  Rx EOC
1772  *	interrupts are disabled via the DIO INTDIS register.
1773  *	However, TLAN chips before revision 3.0 didn't have this
1774  *	CSTAT member or a INTDIS register, so if this chip is
1775  *	pre-3.0, process EOC interrupts normally.
1776  *
1777  **************************************************************/
1778 
1779 static u32 tlan_handle_rx_eoc(struct net_device *dev, u16 host_int)
1780 {
1781 	struct tlan_priv	*priv = netdev_priv(dev);
1782 	dma_addr_t	head_list_phys;
1783 	u32		ack = 1;
1784 
1785 	if (priv->tlan_rev < 0x30) {
1786 		TLAN_DBG(TLAN_DEBUG_RX,
1787 			 "RECEIVE:  Handling RX EOC (head=%d tail=%d) -- IRQ\n",
1788 			 priv->rx_head, priv->rx_tail);
1789 		head_list_phys = priv->rx_list_dma
1790 			+ sizeof(struct tlan_list)*priv->rx_head;
1791 		outl(head_list_phys, dev->base_addr + TLAN_CH_PARM);
1792 		ack |= TLAN_HC_GO | TLAN_HC_RT;
1793 		priv->rx_eoc_count++;
1794 	}
1795 
1796 	return ack;
1797 
1798 }
1799 
1800 
1801 
1802 
1803 /*****************************************************************************
1804 ******************************************************************************
1805 
1806 ThunderLAN driver timer function
1807 
1808 ******************************************************************************
1809 *****************************************************************************/
1810 
1811 
1812 /***************************************************************
1813  *	tlan_timer
1814  *
1815  *	Returns:
1816  *		Nothing
1817  *	Parms:
1818  *		data	A value given to add timer when
1819  *			add_timer was called.
1820  *
1821  *	This function handles timed functionality for the
1822  *	TLAN driver.  The two current timer uses are for
1823  *	delaying for autonegotionation and driving the ACT LED.
1824  *	-	Autonegotiation requires being allowed about
1825  *		2 1/2 seconds before attempting to transmit a
1826  *		packet.  It would be a very bad thing to hang
1827  *		the kernel this long, so the driver doesn't
1828  *		allow transmission 'til after this time, for
1829  *		certain PHYs.  It would be much nicer if all
1830  *		PHYs were interrupt-capable like the internal
1831  *		PHY.
1832  *	-	The ACT LED, which shows adapter activity, is
1833  *		driven by the driver, and so must be left on
1834  *		for a short period to power up the LED so it
1835  *		can be seen.  This delay can be changed by
1836  *		changing the TLAN_TIMER_ACT_DELAY in tlan.h,
1837  *		if desired.  100 ms  produces a slightly
1838  *		sluggish response.
1839  *
1840  **************************************************************/
1841 
1842 static void tlan_timer(unsigned long data)
1843 {
1844 	struct net_device	*dev = (struct net_device *) data;
1845 	struct tlan_priv	*priv = netdev_priv(dev);
1846 	u32		elapsed;
1847 	unsigned long	flags = 0;
1848 
1849 	priv->timer.function = NULL;
1850 
1851 	switch (priv->timer_type) {
1852 	case TLAN_TIMER_PHY_PDOWN:
1853 		tlan_phy_power_down(dev);
1854 		break;
1855 	case TLAN_TIMER_PHY_PUP:
1856 		tlan_phy_power_up(dev);
1857 		break;
1858 	case TLAN_TIMER_PHY_RESET:
1859 		tlan_phy_reset(dev);
1860 		break;
1861 	case TLAN_TIMER_PHY_START_LINK:
1862 		tlan_phy_start_link(dev);
1863 		break;
1864 	case TLAN_TIMER_PHY_FINISH_AN:
1865 		tlan_phy_finish_auto_neg(dev);
1866 		break;
1867 	case TLAN_TIMER_FINISH_RESET:
1868 		tlan_finish_reset(dev);
1869 		break;
1870 	case TLAN_TIMER_ACTIVITY:
1871 		spin_lock_irqsave(&priv->lock, flags);
1872 		if (priv->timer.function == NULL) {
1873 			elapsed = jiffies - priv->timer_set_at;
1874 			if (elapsed >= TLAN_TIMER_ACT_DELAY) {
1875 				tlan_dio_write8(dev->base_addr,
1876 						TLAN_LED_REG, TLAN_LED_LINK);
1877 			} else  {
1878 				priv->timer.function = tlan_timer;
1879 				priv->timer.expires = priv->timer_set_at
1880 					+ TLAN_TIMER_ACT_DELAY;
1881 				spin_unlock_irqrestore(&priv->lock, flags);
1882 				add_timer(&priv->timer);
1883 				break;
1884 			}
1885 		}
1886 		spin_unlock_irqrestore(&priv->lock, flags);
1887 		break;
1888 	default:
1889 		break;
1890 	}
1891 
1892 }
1893 
1894 
1895 /*****************************************************************************
1896 ******************************************************************************
1897 
1898 ThunderLAN driver adapter related routines
1899 
1900 ******************************************************************************
1901 *****************************************************************************/
1902 
1903 
1904 /***************************************************************
1905  *	tlan_reset_lists
1906  *
1907  *	Returns:
1908  *		Nothing
1909  *	Parms:
1910  *		dev	The device structure with the list
1911  *			stuctures to be reset.
1912  *
1913  *	This routine sets the variables associated with managing
1914  *	the TLAN lists to their initial values.
1915  *
1916  **************************************************************/
1917 
1918 static void tlan_reset_lists(struct net_device *dev)
1919 {
1920 	struct tlan_priv *priv = netdev_priv(dev);
1921 	int		i;
1922 	struct tlan_list	*list;
1923 	dma_addr_t	list_phys;
1924 	struct sk_buff	*skb;
1925 
1926 	priv->tx_head = 0;
1927 	priv->tx_tail = 0;
1928 	for (i = 0; i < TLAN_NUM_TX_LISTS; i++) {
1929 		list = priv->tx_list + i;
1930 		list->c_stat = TLAN_CSTAT_UNUSED;
1931 		list->buffer[0].address = 0;
1932 		list->buffer[2].count = 0;
1933 		list->buffer[2].address = 0;
1934 		list->buffer[8].address = 0;
1935 		list->buffer[9].address = 0;
1936 	}
1937 
1938 	priv->rx_head = 0;
1939 	priv->rx_tail = TLAN_NUM_RX_LISTS - 1;
1940 	for (i = 0; i < TLAN_NUM_RX_LISTS; i++) {
1941 		list = priv->rx_list + i;
1942 		list_phys = priv->rx_list_dma + sizeof(struct tlan_list)*i;
1943 		list->c_stat = TLAN_CSTAT_READY;
1944 		list->frame_size = TLAN_MAX_FRAME_SIZE;
1945 		list->buffer[0].count = TLAN_MAX_FRAME_SIZE | TLAN_LAST_BUFFER;
1946 		skb = netdev_alloc_skb_ip_align(dev, TLAN_MAX_FRAME_SIZE + 5);
1947 		if (!skb)
1948 			break;
1949 
1950 		list->buffer[0].address = pci_map_single(priv->pci_dev,
1951 							 skb->data,
1952 							 TLAN_MAX_FRAME_SIZE,
1953 							 PCI_DMA_FROMDEVICE);
1954 		tlan_store_skb(list, skb);
1955 		list->buffer[1].count = 0;
1956 		list->buffer[1].address = 0;
1957 		list->forward = list_phys + sizeof(struct tlan_list);
1958 	}
1959 
1960 	/* in case ran out of memory early, clear bits */
1961 	while (i < TLAN_NUM_RX_LISTS) {
1962 		tlan_store_skb(priv->rx_list + i, NULL);
1963 		++i;
1964 	}
1965 	list->forward = 0;
1966 
1967 }
1968 
1969 
1970 static void tlan_free_lists(struct net_device *dev)
1971 {
1972 	struct tlan_priv *priv = netdev_priv(dev);
1973 	int		i;
1974 	struct tlan_list	*list;
1975 	struct sk_buff	*skb;
1976 
1977 	for (i = 0; i < TLAN_NUM_TX_LISTS; i++) {
1978 		list = priv->tx_list + i;
1979 		skb = tlan_get_skb(list);
1980 		if (skb) {
1981 			pci_unmap_single(
1982 				priv->pci_dev,
1983 				list->buffer[0].address,
1984 				max(skb->len,
1985 				    (unsigned int)TLAN_MIN_FRAME_SIZE),
1986 				PCI_DMA_TODEVICE);
1987 			dev_kfree_skb_any(skb);
1988 			list->buffer[8].address = 0;
1989 			list->buffer[9].address = 0;
1990 		}
1991 	}
1992 
1993 	for (i = 0; i < TLAN_NUM_RX_LISTS; i++) {
1994 		list = priv->rx_list + i;
1995 		skb = tlan_get_skb(list);
1996 		if (skb) {
1997 			pci_unmap_single(priv->pci_dev,
1998 					 list->buffer[0].address,
1999 					 TLAN_MAX_FRAME_SIZE,
2000 					 PCI_DMA_FROMDEVICE);
2001 			dev_kfree_skb_any(skb);
2002 			list->buffer[8].address = 0;
2003 			list->buffer[9].address = 0;
2004 		}
2005 	}
2006 }
2007 
2008 
2009 
2010 
2011 /***************************************************************
2012  *	tlan_print_dio
2013  *
2014  *	Returns:
2015  *		Nothing
2016  *	Parms:
2017  *		io_base		Base IO port of the device of
2018  *				which to print DIO registers.
2019  *
2020  *	This function prints out all the internal (DIO)
2021  *	registers of a TLAN chip.
2022  *
2023  **************************************************************/
2024 
2025 static void tlan_print_dio(u16 io_base)
2026 {
2027 	u32 data0, data1;
2028 	int	i;
2029 
2030 	pr_info("Contents of internal registers for io base 0x%04hx\n",
2031 		io_base);
2032 	pr_info("Off.  +0        +4\n");
2033 	for (i = 0; i < 0x4C; i += 8) {
2034 		data0 = tlan_dio_read32(io_base, i);
2035 		data1 = tlan_dio_read32(io_base, i + 0x4);
2036 		pr_info("0x%02x  0x%08x 0x%08x\n", i, data0, data1);
2037 	}
2038 
2039 }
2040 
2041 
2042 
2043 
2044 /***************************************************************
2045  *	TLan_PrintList
2046  *
2047  *	Returns:
2048  *		Nothing
2049  *	Parms:
2050  *		list	A pointer to the struct tlan_list structure to
2051  *			be printed.
2052  *		type	A string to designate type of list,
2053  *			"Rx" or "Tx".
2054  *		num	The index of the list.
2055  *
2056  *	This function prints out the contents of the list
2057  *	pointed to by the list parameter.
2058  *
2059  **************************************************************/
2060 
2061 static void tlan_print_list(struct tlan_list *list, char *type, int num)
2062 {
2063 	int i;
2064 
2065 	pr_info("%s List %d at %p\n", type, num, list);
2066 	pr_info("   Forward    = 0x%08x\n",  list->forward);
2067 	pr_info("   CSTAT      = 0x%04hx\n", list->c_stat);
2068 	pr_info("   Frame Size = 0x%04hx\n", list->frame_size);
2069 	/* for (i = 0; i < 10; i++) { */
2070 	for (i = 0; i < 2; i++) {
2071 		pr_info("   Buffer[%d].count, addr = 0x%08x, 0x%08x\n",
2072 			i, list->buffer[i].count, list->buffer[i].address);
2073 	}
2074 
2075 }
2076 
2077 
2078 
2079 
2080 /***************************************************************
2081  *	tlan_read_and_clear_stats
2082  *
2083  *	Returns:
2084  *		Nothing
2085  *	Parms:
2086  *		dev	Pointer to device structure of adapter
2087  *			to which to read stats.
2088  *		record	Flag indicating whether to add
2089  *
2090  *	This functions reads all the internal status registers
2091  *	of the TLAN chip, which clears them as a side effect.
2092  *	It then either adds the values to the device's status
2093  *	struct, or discards them, depending on whether record
2094  *	is TLAN_RECORD (!=0)  or TLAN_IGNORE (==0).
2095  *
2096  **************************************************************/
2097 
2098 static void tlan_read_and_clear_stats(struct net_device *dev, int record)
2099 {
2100 	u32		tx_good, tx_under;
2101 	u32		rx_good, rx_over;
2102 	u32		def_tx, crc, code;
2103 	u32		multi_col, single_col;
2104 	u32		excess_col, late_col, loss;
2105 
2106 	outw(TLAN_GOOD_TX_FRMS, dev->base_addr + TLAN_DIO_ADR);
2107 	tx_good  = inb(dev->base_addr + TLAN_DIO_DATA);
2108 	tx_good += inb(dev->base_addr + TLAN_DIO_DATA + 1) << 8;
2109 	tx_good += inb(dev->base_addr + TLAN_DIO_DATA + 2) << 16;
2110 	tx_under = inb(dev->base_addr + TLAN_DIO_DATA + 3);
2111 
2112 	outw(TLAN_GOOD_RX_FRMS, dev->base_addr + TLAN_DIO_ADR);
2113 	rx_good  = inb(dev->base_addr + TLAN_DIO_DATA);
2114 	rx_good += inb(dev->base_addr + TLAN_DIO_DATA + 1) << 8;
2115 	rx_good += inb(dev->base_addr + TLAN_DIO_DATA + 2) << 16;
2116 	rx_over  = inb(dev->base_addr + TLAN_DIO_DATA + 3);
2117 
2118 	outw(TLAN_DEFERRED_TX, dev->base_addr + TLAN_DIO_ADR);
2119 	def_tx  = inb(dev->base_addr + TLAN_DIO_DATA);
2120 	def_tx += inb(dev->base_addr + TLAN_DIO_DATA + 1) << 8;
2121 	crc     = inb(dev->base_addr + TLAN_DIO_DATA + 2);
2122 	code    = inb(dev->base_addr + TLAN_DIO_DATA + 3);
2123 
2124 	outw(TLAN_MULTICOL_FRMS, dev->base_addr + TLAN_DIO_ADR);
2125 	multi_col   = inb(dev->base_addr + TLAN_DIO_DATA);
2126 	multi_col  += inb(dev->base_addr + TLAN_DIO_DATA + 1) << 8;
2127 	single_col  = inb(dev->base_addr + TLAN_DIO_DATA + 2);
2128 	single_col += inb(dev->base_addr + TLAN_DIO_DATA + 3) << 8;
2129 
2130 	outw(TLAN_EXCESSCOL_FRMS, dev->base_addr + TLAN_DIO_ADR);
2131 	excess_col = inb(dev->base_addr + TLAN_DIO_DATA);
2132 	late_col   = inb(dev->base_addr + TLAN_DIO_DATA + 1);
2133 	loss       = inb(dev->base_addr + TLAN_DIO_DATA + 2);
2134 
2135 	if (record) {
2136 		dev->stats.rx_packets += rx_good;
2137 		dev->stats.rx_errors  += rx_over + crc + code;
2138 		dev->stats.tx_packets += tx_good;
2139 		dev->stats.tx_errors  += tx_under + loss;
2140 		dev->stats.collisions += multi_col
2141 			+ single_col + excess_col + late_col;
2142 
2143 		dev->stats.rx_over_errors    += rx_over;
2144 		dev->stats.rx_crc_errors     += crc;
2145 		dev->stats.rx_frame_errors   += code;
2146 
2147 		dev->stats.tx_aborted_errors += tx_under;
2148 		dev->stats.tx_carrier_errors += loss;
2149 	}
2150 
2151 }
2152 
2153 
2154 
2155 
2156 /***************************************************************
2157  *	TLan_Reset
2158  *
2159  *	Returns:
2160  *		0
2161  *	Parms:
2162  *		dev	Pointer to device structure of adapter
2163  *			to be reset.
2164  *
2165  *	This function resets the adapter and it's physical
2166  *	device.  See Chap. 3, pp. 9-10 of the "ThunderLAN
2167  *	Programmer's Guide" for details.  The routine tries to
2168  *	implement what is detailed there, though adjustments
2169  *	have been made.
2170  *
2171  **************************************************************/
2172 
2173 static void
2174 tlan_reset_adapter(struct net_device *dev)
2175 {
2176 	struct tlan_priv	*priv = netdev_priv(dev);
2177 	int		i;
2178 	u32		addr;
2179 	u32		data;
2180 	u8		data8;
2181 
2182 	priv->tlan_full_duplex = false;
2183 	priv->phy_online = 0;
2184 	netif_carrier_off(dev);
2185 
2186 /*  1.	Assert reset bit. */
2187 
2188 	data = inl(dev->base_addr + TLAN_HOST_CMD);
2189 	data |= TLAN_HC_AD_RST;
2190 	outl(data, dev->base_addr + TLAN_HOST_CMD);
2191 
2192 	udelay(1000);
2193 
2194 /*  2.	Turn off interrupts. (Probably isn't necessary) */
2195 
2196 	data = inl(dev->base_addr + TLAN_HOST_CMD);
2197 	data |= TLAN_HC_INT_OFF;
2198 	outl(data, dev->base_addr + TLAN_HOST_CMD);
2199 
2200 /*  3.	Clear AREGs and HASHs. */
2201 
2202 	for (i = TLAN_AREG_0; i <= TLAN_HASH_2; i += 4)
2203 		tlan_dio_write32(dev->base_addr, (u16) i, 0);
2204 
2205 /*  4.	Setup NetConfig register. */
2206 
2207 	data = TLAN_NET_CFG_1FRAG | TLAN_NET_CFG_1CHAN | TLAN_NET_CFG_PHY_EN;
2208 	tlan_dio_write16(dev->base_addr, TLAN_NET_CONFIG, (u16) data);
2209 
2210 /*  5.	Load Ld_Tmr and Ld_Thr in HOST_CMD. */
2211 
2212 	outl(TLAN_HC_LD_TMR | 0x3f, dev->base_addr + TLAN_HOST_CMD);
2213 	outl(TLAN_HC_LD_THR | 0x9, dev->base_addr + TLAN_HOST_CMD);
2214 
2215 /*  6.	Unreset the MII by setting NMRST (in NetSio) to 1. */
2216 
2217 	outw(TLAN_NET_SIO, dev->base_addr + TLAN_DIO_ADR);
2218 	addr = dev->base_addr + TLAN_DIO_DATA + TLAN_NET_SIO;
2219 	tlan_set_bit(TLAN_NET_SIO_NMRST, addr);
2220 
2221 /*  7.	Setup the remaining registers. */
2222 
2223 	if (priv->tlan_rev >= 0x30) {
2224 		data8 = TLAN_ID_TX_EOC | TLAN_ID_RX_EOC;
2225 		tlan_dio_write8(dev->base_addr, TLAN_INT_DIS, data8);
2226 	}
2227 	tlan_phy_detect(dev);
2228 	data = TLAN_NET_CFG_1FRAG | TLAN_NET_CFG_1CHAN;
2229 
2230 	if (priv->adapter->flags & TLAN_ADAPTER_BIT_RATE_PHY) {
2231 		data |= TLAN_NET_CFG_BIT;
2232 		if (priv->aui == 1) {
2233 			tlan_dio_write8(dev->base_addr, TLAN_ACOMMIT, 0x0a);
2234 		} else if (priv->duplex == TLAN_DUPLEX_FULL) {
2235 			tlan_dio_write8(dev->base_addr, TLAN_ACOMMIT, 0x00);
2236 			priv->tlan_full_duplex = true;
2237 		} else {
2238 			tlan_dio_write8(dev->base_addr, TLAN_ACOMMIT, 0x08);
2239 		}
2240 	}
2241 
2242 	/* don't power down internal PHY if we're going to use it */
2243 	if (priv->phy_num == 0 ||
2244 	   (priv->adapter->flags & TLAN_ADAPTER_USE_INTERN_10))
2245 		data |= TLAN_NET_CFG_PHY_EN;
2246 	tlan_dio_write16(dev->base_addr, TLAN_NET_CONFIG, (u16) data);
2247 
2248 	if (priv->adapter->flags & TLAN_ADAPTER_UNMANAGED_PHY)
2249 		tlan_finish_reset(dev);
2250 	else
2251 		tlan_phy_power_down(dev);
2252 
2253 }
2254 
2255 
2256 
2257 
2258 static void
2259 tlan_finish_reset(struct net_device *dev)
2260 {
2261 	struct tlan_priv	*priv = netdev_priv(dev);
2262 	u8		data;
2263 	u32		phy;
2264 	u8		sio;
2265 	u16		status;
2266 	u16		partner;
2267 	u16		tlphy_ctl;
2268 	u16		tlphy_par;
2269 	u16		tlphy_id1, tlphy_id2;
2270 	int		i;
2271 
2272 	phy = priv->phy[priv->phy_num];
2273 
2274 	data = TLAN_NET_CMD_NRESET | TLAN_NET_CMD_NWRAP;
2275 	if (priv->tlan_full_duplex)
2276 		data |= TLAN_NET_CMD_DUPLEX;
2277 	tlan_dio_write8(dev->base_addr, TLAN_NET_CMD, data);
2278 	data = TLAN_NET_MASK_MASK4 | TLAN_NET_MASK_MASK5;
2279 	if (priv->phy_num == 0)
2280 		data |= TLAN_NET_MASK_MASK7;
2281 	tlan_dio_write8(dev->base_addr, TLAN_NET_MASK, data);
2282 	tlan_dio_write16(dev->base_addr, TLAN_MAX_RX, ((1536)+7)&~7);
2283 	tlan_mii_read_reg(dev, phy, MII_GEN_ID_HI, &tlphy_id1);
2284 	tlan_mii_read_reg(dev, phy, MII_GEN_ID_LO, &tlphy_id2);
2285 
2286 	if ((priv->adapter->flags & TLAN_ADAPTER_UNMANAGED_PHY) ||
2287 	    (priv->aui)) {
2288 		status = MII_GS_LINK;
2289 		netdev_info(dev, "Link forced\n");
2290 	} else {
2291 		tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
2292 		udelay(1000);
2293 		tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
2294 		if (status & MII_GS_LINK) {
2295 			/* We only support link info on Nat.Sem. PHY's */
2296 			if ((tlphy_id1 == NAT_SEM_ID1) &&
2297 			    (tlphy_id2 == NAT_SEM_ID2)) {
2298 				tlan_mii_read_reg(dev, phy, MII_AN_LPA,
2299 					&partner);
2300 				tlan_mii_read_reg(dev, phy, TLAN_TLPHY_PAR,
2301 					&tlphy_par);
2302 
2303 				netdev_info(dev,
2304 					"Link active, %s %uMbps %s-Duplex\n",
2305 					!(tlphy_par & TLAN_PHY_AN_EN_STAT)
2306 					? "forced" : "Autonegotiation enabled,",
2307 					tlphy_par & TLAN_PHY_SPEED_100
2308 					? 100 : 10,
2309 					tlphy_par & TLAN_PHY_DUPLEX_FULL
2310 					? "Full" : "Half");
2311 
2312 				if (tlphy_par & TLAN_PHY_AN_EN_STAT) {
2313 					netdev_info(dev, "Partner capability:");
2314 					for (i = 5; i < 10; i++)
2315 						if (partner & (1 << i))
2316 							pr_cont(" %s",
2317 								media[i-5]);
2318 					pr_cont("\n");
2319 				}
2320 			} else
2321 				netdev_info(dev, "Link active\n");
2322 			/* Enabling link beat monitoring */
2323 			priv->media_timer.function = tlan_phy_monitor;
2324 			priv->media_timer.data = (unsigned long) dev;
2325 			priv->media_timer.expires = jiffies + HZ;
2326 			add_timer(&priv->media_timer);
2327 		}
2328 	}
2329 
2330 	if (priv->phy_num == 0) {
2331 		tlan_mii_read_reg(dev, phy, TLAN_TLPHY_CTL, &tlphy_ctl);
2332 		tlphy_ctl |= TLAN_TC_INTEN;
2333 		tlan_mii_write_reg(dev, phy, TLAN_TLPHY_CTL, tlphy_ctl);
2334 		sio = tlan_dio_read8(dev->base_addr, TLAN_NET_SIO);
2335 		sio |= TLAN_NET_SIO_MINTEN;
2336 		tlan_dio_write8(dev->base_addr, TLAN_NET_SIO, sio);
2337 	}
2338 
2339 	if (status & MII_GS_LINK) {
2340 		tlan_set_mac(dev, 0, dev->dev_addr);
2341 		priv->phy_online = 1;
2342 		outb((TLAN_HC_INT_ON >> 8), dev->base_addr + TLAN_HOST_CMD + 1);
2343 		if (debug >= 1 && debug != TLAN_DEBUG_PROBE)
2344 			outb((TLAN_HC_REQ_INT >> 8),
2345 			     dev->base_addr + TLAN_HOST_CMD + 1);
2346 		outl(priv->rx_list_dma, dev->base_addr + TLAN_CH_PARM);
2347 		outl(TLAN_HC_GO | TLAN_HC_RT, dev->base_addr + TLAN_HOST_CMD);
2348 		tlan_dio_write8(dev->base_addr, TLAN_LED_REG, TLAN_LED_LINK);
2349 		netif_carrier_on(dev);
2350 	} else {
2351 		netdev_info(dev, "Link inactive, will retry in 10 secs...\n");
2352 		tlan_set_timer(dev, (10*HZ), TLAN_TIMER_FINISH_RESET);
2353 		return;
2354 	}
2355 	tlan_set_multicast_list(dev);
2356 
2357 }
2358 
2359 
2360 
2361 
2362 /***************************************************************
2363  *	tlan_set_mac
2364  *
2365  *	Returns:
2366  *		Nothing
2367  *	Parms:
2368  *		dev	Pointer to device structure of adapter
2369  *			on which to change the AREG.
2370  *		areg	The AREG to set the address in (0 - 3).
2371  *		mac	A pointer to an array of chars.  Each
2372  *			element stores one byte of the address.
2373  *			IE, it isn't in ascii.
2374  *
2375  *	This function transfers a MAC address to one of the
2376  *	TLAN AREGs (address registers).  The TLAN chip locks
2377  *	the register on writing to offset 0 and unlocks the
2378  *	register after writing to offset 5.  If NULL is passed
2379  *	in mac, then the AREG is filled with 0's.
2380  *
2381  **************************************************************/
2382 
2383 static void tlan_set_mac(struct net_device *dev, int areg, char *mac)
2384 {
2385 	int i;
2386 
2387 	areg *= 6;
2388 
2389 	if (mac != NULL) {
2390 		for (i = 0; i < 6; i++)
2391 			tlan_dio_write8(dev->base_addr,
2392 					TLAN_AREG_0 + areg + i, mac[i]);
2393 	} else {
2394 		for (i = 0; i < 6; i++)
2395 			tlan_dio_write8(dev->base_addr,
2396 					TLAN_AREG_0 + areg + i, 0);
2397 	}
2398 
2399 }
2400 
2401 
2402 
2403 
2404 /*****************************************************************************
2405 ******************************************************************************
2406 
2407 ThunderLAN driver PHY layer routines
2408 
2409 ******************************************************************************
2410 *****************************************************************************/
2411 
2412 
2413 
2414 /*********************************************************************
2415  *	tlan_phy_print
2416  *
2417  *	Returns:
2418  *		Nothing
2419  *	Parms:
2420  *		dev	A pointer to the device structure of the
2421  *			TLAN device having the PHYs to be detailed.
2422  *
2423  *	This function prints the registers a PHY (aka transceiver).
2424  *
2425  ********************************************************************/
2426 
2427 static void tlan_phy_print(struct net_device *dev)
2428 {
2429 	struct tlan_priv *priv = netdev_priv(dev);
2430 	u16 i, data0, data1, data2, data3, phy;
2431 
2432 	phy = priv->phy[priv->phy_num];
2433 
2434 	if (priv->adapter->flags & TLAN_ADAPTER_UNMANAGED_PHY) {
2435 		netdev_info(dev, "Unmanaged PHY\n");
2436 	} else if (phy <= TLAN_PHY_MAX_ADDR) {
2437 		netdev_info(dev, "PHY 0x%02x\n", phy);
2438 		pr_info("   Off.  +0     +1     +2     +3\n");
2439 		for (i = 0; i < 0x20; i += 4) {
2440 			tlan_mii_read_reg(dev, phy, i, &data0);
2441 			tlan_mii_read_reg(dev, phy, i + 1, &data1);
2442 			tlan_mii_read_reg(dev, phy, i + 2, &data2);
2443 			tlan_mii_read_reg(dev, phy, i + 3, &data3);
2444 			pr_info("   0x%02x 0x%04hx 0x%04hx 0x%04hx 0x%04hx\n",
2445 				i, data0, data1, data2, data3);
2446 		}
2447 	} else {
2448 		netdev_info(dev, "Invalid PHY\n");
2449 	}
2450 
2451 }
2452 
2453 
2454 
2455 
2456 /*********************************************************************
2457  *	tlan_phy_detect
2458  *
2459  *	Returns:
2460  *		Nothing
2461  *	Parms:
2462  *		dev	A pointer to the device structure of the adapter
2463  *			for which the PHY needs determined.
2464  *
2465  *	So far I've found that adapters which have external PHYs
2466  *	may also use the internal PHY for part of the functionality.
2467  *	(eg, AUI/Thinnet).  This function finds out if this TLAN
2468  *	chip has an internal PHY, and then finds the first external
2469  *	PHY (starting from address 0) if it exists).
2470  *
2471  ********************************************************************/
2472 
2473 static void tlan_phy_detect(struct net_device *dev)
2474 {
2475 	struct tlan_priv *priv = netdev_priv(dev);
2476 	u16		control;
2477 	u16		hi;
2478 	u16		lo;
2479 	u32		phy;
2480 
2481 	if (priv->adapter->flags & TLAN_ADAPTER_UNMANAGED_PHY) {
2482 		priv->phy_num = 0xffff;
2483 		return;
2484 	}
2485 
2486 	tlan_mii_read_reg(dev, TLAN_PHY_MAX_ADDR, MII_GEN_ID_HI, &hi);
2487 
2488 	if (hi != 0xffff)
2489 		priv->phy[0] = TLAN_PHY_MAX_ADDR;
2490 	else
2491 		priv->phy[0] = TLAN_PHY_NONE;
2492 
2493 	priv->phy[1] = TLAN_PHY_NONE;
2494 	for (phy = 0; phy <= TLAN_PHY_MAX_ADDR; phy++) {
2495 		tlan_mii_read_reg(dev, phy, MII_GEN_CTL, &control);
2496 		tlan_mii_read_reg(dev, phy, MII_GEN_ID_HI, &hi);
2497 		tlan_mii_read_reg(dev, phy, MII_GEN_ID_LO, &lo);
2498 		if ((control != 0xffff) ||
2499 		    (hi != 0xffff) || (lo != 0xffff)) {
2500 			TLAN_DBG(TLAN_DEBUG_GNRL,
2501 				 "PHY found at %02x %04x %04x %04x\n",
2502 				 phy, control, hi, lo);
2503 			if ((priv->phy[1] == TLAN_PHY_NONE) &&
2504 			    (phy != TLAN_PHY_MAX_ADDR)) {
2505 				priv->phy[1] = phy;
2506 			}
2507 		}
2508 	}
2509 
2510 	if (priv->phy[1] != TLAN_PHY_NONE)
2511 		priv->phy_num = 1;
2512 	else if (priv->phy[0] != TLAN_PHY_NONE)
2513 		priv->phy_num = 0;
2514 	else
2515 		netdev_info(dev, "Cannot initialize device, no PHY was found!\n");
2516 
2517 }
2518 
2519 
2520 
2521 
2522 static void tlan_phy_power_down(struct net_device *dev)
2523 {
2524 	struct tlan_priv	*priv = netdev_priv(dev);
2525 	u16		value;
2526 
2527 	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Powering down PHY(s).\n", dev->name);
2528 	value = MII_GC_PDOWN | MII_GC_LOOPBK | MII_GC_ISOLATE;
2529 	tlan_mii_sync(dev->base_addr);
2530 	tlan_mii_write_reg(dev, priv->phy[priv->phy_num], MII_GEN_CTL, value);
2531 	if ((priv->phy_num == 0) && (priv->phy[1] != TLAN_PHY_NONE)) {
2532 		/* if using internal PHY, the external PHY must be powered on */
2533 		if (priv->adapter->flags & TLAN_ADAPTER_USE_INTERN_10)
2534 			value = MII_GC_ISOLATE; /* just isolate it from MII */
2535 		tlan_mii_sync(dev->base_addr);
2536 		tlan_mii_write_reg(dev, priv->phy[1], MII_GEN_CTL, value);
2537 	}
2538 
2539 	/* Wait for 50 ms and powerup
2540 	 * This is abitrary.  It is intended to make sure the
2541 	 * transceiver settles.
2542 	 */
2543 	tlan_set_timer(dev, msecs_to_jiffies(50), TLAN_TIMER_PHY_PUP);
2544 
2545 }
2546 
2547 
2548 
2549 
2550 static void tlan_phy_power_up(struct net_device *dev)
2551 {
2552 	struct tlan_priv	*priv = netdev_priv(dev);
2553 	u16		value;
2554 
2555 	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Powering up PHY.\n", dev->name);
2556 	tlan_mii_sync(dev->base_addr);
2557 	value = MII_GC_LOOPBK;
2558 	tlan_mii_write_reg(dev, priv->phy[priv->phy_num], MII_GEN_CTL, value);
2559 	tlan_mii_sync(dev->base_addr);
2560 	/* Wait for 500 ms and reset the
2561 	 * transceiver.  The TLAN docs say both 50 ms and
2562 	 * 500 ms, so do the longer, just in case.
2563 	 */
2564 	tlan_set_timer(dev, msecs_to_jiffies(500), TLAN_TIMER_PHY_RESET);
2565 
2566 }
2567 
2568 
2569 
2570 
2571 static void tlan_phy_reset(struct net_device *dev)
2572 {
2573 	struct tlan_priv	*priv = netdev_priv(dev);
2574 	u16		phy;
2575 	u16		value;
2576 	unsigned long timeout = jiffies + HZ;
2577 
2578 	phy = priv->phy[priv->phy_num];
2579 
2580 	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Resetting PHY.\n", dev->name);
2581 	tlan_mii_sync(dev->base_addr);
2582 	value = MII_GC_LOOPBK | MII_GC_RESET;
2583 	tlan_mii_write_reg(dev, phy, MII_GEN_CTL, value);
2584 	do {
2585 		tlan_mii_read_reg(dev, phy, MII_GEN_CTL, &value);
2586 		if (time_after(jiffies, timeout)) {
2587 			netdev_err(dev, "PHY reset timeout\n");
2588 			return;
2589 		}
2590 	} while (value & MII_GC_RESET);
2591 
2592 	/* Wait for 500 ms and initialize.
2593 	 * I don't remember why I wait this long.
2594 	 * I've changed this to 50ms, as it seems long enough.
2595 	 */
2596 	tlan_set_timer(dev, msecs_to_jiffies(50), TLAN_TIMER_PHY_START_LINK);
2597 
2598 }
2599 
2600 
2601 
2602 
2603 static void tlan_phy_start_link(struct net_device *dev)
2604 {
2605 	struct tlan_priv	*priv = netdev_priv(dev);
2606 	u16		ability;
2607 	u16		control;
2608 	u16		data;
2609 	u16		phy;
2610 	u16		status;
2611 	u16		tctl;
2612 
2613 	phy = priv->phy[priv->phy_num];
2614 	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Trying to activate link.\n", dev->name);
2615 	tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
2616 	tlan_mii_read_reg(dev, phy, MII_GEN_STS, &ability);
2617 
2618 	if ((status & MII_GS_AUTONEG) &&
2619 	    (!priv->aui)) {
2620 		ability = status >> 11;
2621 		if (priv->speed  == TLAN_SPEED_10 &&
2622 		    priv->duplex == TLAN_DUPLEX_HALF) {
2623 			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x0000);
2624 		} else if (priv->speed == TLAN_SPEED_10 &&
2625 			   priv->duplex == TLAN_DUPLEX_FULL) {
2626 			priv->tlan_full_duplex = true;
2627 			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x0100);
2628 		} else if (priv->speed == TLAN_SPEED_100 &&
2629 			   priv->duplex == TLAN_DUPLEX_HALF) {
2630 			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x2000);
2631 		} else if (priv->speed == TLAN_SPEED_100 &&
2632 			   priv->duplex == TLAN_DUPLEX_FULL) {
2633 			priv->tlan_full_duplex = true;
2634 			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x2100);
2635 		} else {
2636 
2637 			/* Set Auto-Neg advertisement */
2638 			tlan_mii_write_reg(dev, phy, MII_AN_ADV,
2639 					   (ability << 5) | 1);
2640 			/* Enablee Auto-Neg */
2641 			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x1000);
2642 			/* Restart Auto-Neg */
2643 			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x1200);
2644 			/* Wait for 4 sec for autonegotiation
2645 			 * to complete.  The max spec time is less than this
2646 			 * but the card need additional time to start AN.
2647 			 * .5 sec should be plenty extra.
2648 			 */
2649 			netdev_info(dev, "Starting autonegotiation\n");
2650 			tlan_set_timer(dev, (2*HZ), TLAN_TIMER_PHY_FINISH_AN);
2651 			return;
2652 		}
2653 
2654 	}
2655 
2656 	if ((priv->aui) && (priv->phy_num != 0)) {
2657 		priv->phy_num = 0;
2658 		data = TLAN_NET_CFG_1FRAG | TLAN_NET_CFG_1CHAN
2659 			| TLAN_NET_CFG_PHY_EN;
2660 		tlan_dio_write16(dev->base_addr, TLAN_NET_CONFIG, data);
2661 		tlan_set_timer(dev, msecs_to_jiffies(40), TLAN_TIMER_PHY_PDOWN);
2662 		return;
2663 	} else if (priv->phy_num == 0) {
2664 		control = 0;
2665 		tlan_mii_read_reg(dev, phy, TLAN_TLPHY_CTL, &tctl);
2666 		if (priv->aui) {
2667 			tctl |= TLAN_TC_AUISEL;
2668 		} else {
2669 			tctl &= ~TLAN_TC_AUISEL;
2670 			if (priv->duplex == TLAN_DUPLEX_FULL) {
2671 				control |= MII_GC_DUPLEX;
2672 				priv->tlan_full_duplex = true;
2673 			}
2674 			if (priv->speed == TLAN_SPEED_100)
2675 				control |= MII_GC_SPEEDSEL;
2676 		}
2677 		tlan_mii_write_reg(dev, phy, MII_GEN_CTL, control);
2678 		tlan_mii_write_reg(dev, phy, TLAN_TLPHY_CTL, tctl);
2679 	}
2680 
2681 	/* Wait for 2 sec to give the transceiver time
2682 	 * to establish link.
2683 	 */
2684 	tlan_set_timer(dev, (4*HZ), TLAN_TIMER_FINISH_RESET);
2685 
2686 }
2687 
2688 
2689 
2690 
2691 static void tlan_phy_finish_auto_neg(struct net_device *dev)
2692 {
2693 	struct tlan_priv	*priv = netdev_priv(dev);
2694 	u16		an_adv;
2695 	u16		an_lpa;
2696 	u16		mode;
2697 	u16		phy;
2698 	u16		status;
2699 
2700 	phy = priv->phy[priv->phy_num];
2701 
2702 	tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
2703 	udelay(1000);
2704 	tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
2705 
2706 	if (!(status & MII_GS_AUTOCMPLT)) {
2707 		/* Wait for 8 sec to give the process
2708 		 * more time.  Perhaps we should fail after a while.
2709 		 */
2710 		tlan_set_timer(dev, 2 * HZ, TLAN_TIMER_PHY_FINISH_AN);
2711 		return;
2712 	}
2713 
2714 	netdev_info(dev, "Autonegotiation complete\n");
2715 	tlan_mii_read_reg(dev, phy, MII_AN_ADV, &an_adv);
2716 	tlan_mii_read_reg(dev, phy, MII_AN_LPA, &an_lpa);
2717 	mode = an_adv & an_lpa & 0x03E0;
2718 	if (mode & 0x0100)
2719 		priv->tlan_full_duplex = true;
2720 	else if (!(mode & 0x0080) && (mode & 0x0040))
2721 		priv->tlan_full_duplex = true;
2722 
2723 	/* switch to internal PHY for 10 Mbps */
2724 	if ((!(mode & 0x0180)) &&
2725 	    (priv->adapter->flags & TLAN_ADAPTER_USE_INTERN_10) &&
2726 	    (priv->phy_num != 0)) {
2727 		priv->phy_num = 0;
2728 		tlan_set_timer(dev, msecs_to_jiffies(400), TLAN_TIMER_PHY_PDOWN);
2729 		return;
2730 	}
2731 
2732 	if (priv->phy_num == 0) {
2733 		if ((priv->duplex == TLAN_DUPLEX_FULL) ||
2734 		    (an_adv & an_lpa & 0x0040)) {
2735 			tlan_mii_write_reg(dev, phy, MII_GEN_CTL,
2736 					   MII_GC_AUTOENB | MII_GC_DUPLEX);
2737 			netdev_info(dev, "Starting internal PHY with FULL-DUPLEX\n");
2738 		} else {
2739 			tlan_mii_write_reg(dev, phy, MII_GEN_CTL,
2740 					   MII_GC_AUTOENB);
2741 			netdev_info(dev, "Starting internal PHY with HALF-DUPLEX\n");
2742 		}
2743 	}
2744 
2745 	/* Wait for 100 ms.  No reason in partiticular.
2746 	 */
2747 	tlan_set_timer(dev, msecs_to_jiffies(100), TLAN_TIMER_FINISH_RESET);
2748 
2749 }
2750 
2751 
2752 /*********************************************************************
2753  *
2754  *     tlan_phy_monitor
2755  *
2756  *     Returns:
2757  *	      None
2758  *
2759  *     Params:
2760  *	      data	     The device structure of this device.
2761  *
2762  *
2763  *     This function monitors PHY condition by reading the status
2764  *     register via the MII bus, controls LINK LED and notifies the
2765  *     kernel about link state.
2766  *
2767  *******************************************************************/
2768 
2769 static void tlan_phy_monitor(unsigned long data)
2770 {
2771 	struct net_device *dev = (struct net_device *) data;
2772 	struct tlan_priv *priv = netdev_priv(dev);
2773 	u16     phy;
2774 	u16     phy_status;
2775 
2776 	phy = priv->phy[priv->phy_num];
2777 
2778 	/* Get PHY status register */
2779 	tlan_mii_read_reg(dev, phy, MII_GEN_STS, &phy_status);
2780 
2781 	/* Check if link has been lost */
2782 	if (!(phy_status & MII_GS_LINK)) {
2783 		if (netif_carrier_ok(dev)) {
2784 			printk(KERN_DEBUG "TLAN: %s has lost link\n",
2785 			       dev->name);
2786 			tlan_dio_write8(dev->base_addr, TLAN_LED_REG, 0);
2787 			netif_carrier_off(dev);
2788 			if (priv->adapter->flags & TLAN_ADAPTER_USE_INTERN_10) {
2789 				/* power down internal PHY */
2790 				u16 data = MII_GC_PDOWN | MII_GC_LOOPBK |
2791 					   MII_GC_ISOLATE;
2792 
2793 				tlan_mii_sync(dev->base_addr);
2794 				tlan_mii_write_reg(dev, priv->phy[0],
2795 						   MII_GEN_CTL, data);
2796 				/* set to external PHY */
2797 				priv->phy_num = 1;
2798 				/* restart autonegotiation */
2799 				tlan_set_timer(dev, msecs_to_jiffies(400),
2800 					       TLAN_TIMER_PHY_PDOWN);
2801 				return;
2802 			}
2803 		}
2804 	}
2805 
2806 	/* Link restablished? */
2807 	if ((phy_status & MII_GS_LINK) && !netif_carrier_ok(dev)) {
2808 		tlan_dio_write8(dev->base_addr, TLAN_LED_REG, TLAN_LED_LINK);
2809 		printk(KERN_DEBUG "TLAN: %s has reestablished link\n",
2810 		       dev->name);
2811 		netif_carrier_on(dev);
2812 	}
2813 	priv->media_timer.expires = jiffies + HZ;
2814 	add_timer(&priv->media_timer);
2815 }
2816 
2817 
2818 /*****************************************************************************
2819 ******************************************************************************
2820 
2821 ThunderLAN driver MII routines
2822 
2823 these routines are based on the information in chap. 2 of the
2824 "ThunderLAN Programmer's Guide", pp. 15-24.
2825 
2826 ******************************************************************************
2827 *****************************************************************************/
2828 
2829 
2830 /***************************************************************
2831  *	tlan_mii_read_reg
2832  *
2833  *	Returns:
2834  *		false	if ack received ok
2835  *		true	if no ack received or other error
2836  *
2837  *	Parms:
2838  *		dev		The device structure containing
2839  *				The io address and interrupt count
2840  *				for this device.
2841  *		phy		The address of the PHY to be queried.
2842  *		reg		The register whose contents are to be
2843  *				retrieved.
2844  *		val		A pointer to a variable to store the
2845  *				retrieved value.
2846  *
2847  *	This function uses the TLAN's MII bus to retrieve the contents
2848  *	of a given register on a PHY.  It sends the appropriate info
2849  *	and then reads the 16-bit register value from the MII bus via
2850  *	the TLAN SIO register.
2851  *
2852  **************************************************************/
2853 
2854 static bool
2855 tlan_mii_read_reg(struct net_device *dev, u16 phy, u16 reg, u16 *val)
2856 {
2857 	u8	nack;
2858 	u16	sio, tmp;
2859 	u32	i;
2860 	bool	err;
2861 	int	minten;
2862 	struct tlan_priv *priv = netdev_priv(dev);
2863 	unsigned long flags = 0;
2864 
2865 	err = false;
2866 	outw(TLAN_NET_SIO, dev->base_addr + TLAN_DIO_ADR);
2867 	sio = dev->base_addr + TLAN_DIO_DATA + TLAN_NET_SIO;
2868 
2869 	if (!in_irq())
2870 		spin_lock_irqsave(&priv->lock, flags);
2871 
2872 	tlan_mii_sync(dev->base_addr);
2873 
2874 	minten = tlan_get_bit(TLAN_NET_SIO_MINTEN, sio);
2875 	if (minten)
2876 		tlan_clear_bit(TLAN_NET_SIO_MINTEN, sio);
2877 
2878 	tlan_mii_send_data(dev->base_addr, 0x1, 2);	/* start (01b) */
2879 	tlan_mii_send_data(dev->base_addr, 0x2, 2);	/* read  (10b) */
2880 	tlan_mii_send_data(dev->base_addr, phy, 5);	/* device #      */
2881 	tlan_mii_send_data(dev->base_addr, reg, 5);	/* register #    */
2882 
2883 
2884 	tlan_clear_bit(TLAN_NET_SIO_MTXEN, sio);	/* change direction */
2885 
2886 	tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);		/* clock idle bit */
2887 	tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
2888 	tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);		/* wait 300ns */
2889 
2890 	nack = tlan_get_bit(TLAN_NET_SIO_MDATA, sio);	/* check for ACK */
2891 	tlan_set_bit(TLAN_NET_SIO_MCLK, sio);		/* finish ACK */
2892 	if (nack) {					/* no ACK, so fake it */
2893 		for (i = 0; i < 16; i++) {
2894 			tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);
2895 			tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
2896 		}
2897 		tmp = 0xffff;
2898 		err = true;
2899 	} else {					/* ACK, so read data */
2900 		for (tmp = 0, i = 0x8000; i; i >>= 1) {
2901 			tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);
2902 			if (tlan_get_bit(TLAN_NET_SIO_MDATA, sio))
2903 				tmp |= i;
2904 			tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
2905 		}
2906 	}
2907 
2908 
2909 	tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);		/* idle cycle */
2910 	tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
2911 
2912 	if (minten)
2913 		tlan_set_bit(TLAN_NET_SIO_MINTEN, sio);
2914 
2915 	*val = tmp;
2916 
2917 	if (!in_irq())
2918 		spin_unlock_irqrestore(&priv->lock, flags);
2919 
2920 	return err;
2921 
2922 }
2923 
2924 
2925 
2926 
2927 /***************************************************************
2928  *	tlan_mii_send_data
2929  *
2930  *	Returns:
2931  *		Nothing
2932  *	Parms:
2933  *		base_port	The base IO port of the adapter	in
2934  *				question.
2935  *		dev		The address of the PHY to be queried.
2936  *		data		The value to be placed on the MII bus.
2937  *		num_bits	The number of bits in data that are to
2938  *				be placed on the MII bus.
2939  *
2940  *	This function sends on sequence of bits on the MII
2941  *	configuration bus.
2942  *
2943  **************************************************************/
2944 
2945 static void tlan_mii_send_data(u16 base_port, u32 data, unsigned num_bits)
2946 {
2947 	u16 sio;
2948 	u32 i;
2949 
2950 	if (num_bits == 0)
2951 		return;
2952 
2953 	outw(TLAN_NET_SIO, base_port + TLAN_DIO_ADR);
2954 	sio = base_port + TLAN_DIO_DATA + TLAN_NET_SIO;
2955 	tlan_set_bit(TLAN_NET_SIO_MTXEN, sio);
2956 
2957 	for (i = (0x1 << (num_bits - 1)); i; i >>= 1) {
2958 		tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);
2959 		(void) tlan_get_bit(TLAN_NET_SIO_MCLK, sio);
2960 		if (data & i)
2961 			tlan_set_bit(TLAN_NET_SIO_MDATA, sio);
2962 		else
2963 			tlan_clear_bit(TLAN_NET_SIO_MDATA, sio);
2964 		tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
2965 		(void) tlan_get_bit(TLAN_NET_SIO_MCLK, sio);
2966 	}
2967 
2968 }
2969 
2970 
2971 
2972 
2973 /***************************************************************
2974  *	TLan_MiiSync
2975  *
2976  *	Returns:
2977  *		Nothing
2978  *	Parms:
2979  *		base_port	The base IO port of the adapter in
2980  *				question.
2981  *
2982  *	This functions syncs all PHYs in terms of the MII configuration
2983  *	bus.
2984  *
2985  **************************************************************/
2986 
2987 static void tlan_mii_sync(u16 base_port)
2988 {
2989 	int i;
2990 	u16 sio;
2991 
2992 	outw(TLAN_NET_SIO, base_port + TLAN_DIO_ADR);
2993 	sio = base_port + TLAN_DIO_DATA + TLAN_NET_SIO;
2994 
2995 	tlan_clear_bit(TLAN_NET_SIO_MTXEN, sio);
2996 	for (i = 0; i < 32; i++) {
2997 		tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);
2998 		tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
2999 	}
3000 
3001 }
3002 
3003 
3004 
3005 
3006 /***************************************************************
3007  *	tlan_mii_write_reg
3008  *
3009  *	Returns:
3010  *		Nothing
3011  *	Parms:
3012  *		dev		The device structure for the device
3013  *				to write to.
3014  *		phy		The address of the PHY to be written to.
3015  *		reg		The register whose contents are to be
3016  *				written.
3017  *		val		The value to be written to the register.
3018  *
3019  *	This function uses the TLAN's MII bus to write the contents of a
3020  *	given register on a PHY.  It sends the appropriate info and then
3021  *	writes the 16-bit register value from the MII configuration bus
3022  *	via the TLAN SIO register.
3023  *
3024  **************************************************************/
3025 
3026 static void
3027 tlan_mii_write_reg(struct net_device *dev, u16 phy, u16 reg, u16 val)
3028 {
3029 	u16	sio;
3030 	int	minten;
3031 	unsigned long flags = 0;
3032 	struct tlan_priv *priv = netdev_priv(dev);
3033 
3034 	outw(TLAN_NET_SIO, dev->base_addr + TLAN_DIO_ADR);
3035 	sio = dev->base_addr + TLAN_DIO_DATA + TLAN_NET_SIO;
3036 
3037 	if (!in_irq())
3038 		spin_lock_irqsave(&priv->lock, flags);
3039 
3040 	tlan_mii_sync(dev->base_addr);
3041 
3042 	minten = tlan_get_bit(TLAN_NET_SIO_MINTEN, sio);
3043 	if (minten)
3044 		tlan_clear_bit(TLAN_NET_SIO_MINTEN, sio);
3045 
3046 	tlan_mii_send_data(dev->base_addr, 0x1, 2);	/* start (01b) */
3047 	tlan_mii_send_data(dev->base_addr, 0x1, 2);	/* write (01b) */
3048 	tlan_mii_send_data(dev->base_addr, phy, 5);	/* device #      */
3049 	tlan_mii_send_data(dev->base_addr, reg, 5);	/* register #    */
3050 
3051 	tlan_mii_send_data(dev->base_addr, 0x2, 2);	/* send ACK */
3052 	tlan_mii_send_data(dev->base_addr, val, 16);	/* send data */
3053 
3054 	tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);	/* idle cycle */
3055 	tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
3056 
3057 	if (minten)
3058 		tlan_set_bit(TLAN_NET_SIO_MINTEN, sio);
3059 
3060 	if (!in_irq())
3061 		spin_unlock_irqrestore(&priv->lock, flags);
3062 
3063 }
3064 
3065 
3066 
3067 
3068 /*****************************************************************************
3069 ******************************************************************************
3070 
3071 ThunderLAN driver eeprom routines
3072 
3073 the Compaq netelligent 10 and 10/100 cards use a microchip 24C02A
3074 EEPROM.  these functions are based on information in microchip's
3075 data sheet.  I don't know how well this functions will work with
3076 other Eeproms.
3077 
3078 ******************************************************************************
3079 *****************************************************************************/
3080 
3081 
3082 /***************************************************************
3083  *	tlan_ee_send_start
3084  *
3085  *	Returns:
3086  *		Nothing
3087  *	Parms:
3088  *		io_base		The IO port base address for the
3089  *				TLAN device with the EEPROM to
3090  *				use.
3091  *
3092  *	This function sends a start cycle to an EEPROM attached
3093  *	to a TLAN chip.
3094  *
3095  **************************************************************/
3096 
3097 static void tlan_ee_send_start(u16 io_base)
3098 {
3099 	u16	sio;
3100 
3101 	outw(TLAN_NET_SIO, io_base + TLAN_DIO_ADR);
3102 	sio = io_base + TLAN_DIO_DATA + TLAN_NET_SIO;
3103 
3104 	tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
3105 	tlan_set_bit(TLAN_NET_SIO_EDATA, sio);
3106 	tlan_set_bit(TLAN_NET_SIO_ETXEN, sio);
3107 	tlan_clear_bit(TLAN_NET_SIO_EDATA, sio);
3108 	tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
3109 
3110 }
3111 
3112 
3113 
3114 
3115 /***************************************************************
3116  *	tlan_ee_send_byte
3117  *
3118  *	Returns:
3119  *		If the correct ack was received, 0, otherwise 1
3120  *	Parms:	io_base		The IO port base address for the
3121  *				TLAN device with the EEPROM to
3122  *				use.
3123  *		data		The 8 bits of information to
3124  *				send to the EEPROM.
3125  *		stop		If TLAN_EEPROM_STOP is passed, a
3126  *				stop cycle is sent after the
3127  *				byte is sent after the ack is
3128  *				read.
3129  *
3130  *	This function sends a byte on the serial EEPROM line,
3131  *	driving the clock to send each bit. The function then
3132  *	reverses transmission direction and reads an acknowledge
3133  *	bit.
3134  *
3135  **************************************************************/
3136 
3137 static int tlan_ee_send_byte(u16 io_base, u8 data, int stop)
3138 {
3139 	int	err;
3140 	u8	place;
3141 	u16	sio;
3142 
3143 	outw(TLAN_NET_SIO, io_base + TLAN_DIO_ADR);
3144 	sio = io_base + TLAN_DIO_DATA + TLAN_NET_SIO;
3145 
3146 	/* Assume clock is low, tx is enabled; */
3147 	for (place = 0x80; place != 0; place >>= 1) {
3148 		if (place & data)
3149 			tlan_set_bit(TLAN_NET_SIO_EDATA, sio);
3150 		else
3151 			tlan_clear_bit(TLAN_NET_SIO_EDATA, sio);
3152 		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
3153 		tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
3154 	}
3155 	tlan_clear_bit(TLAN_NET_SIO_ETXEN, sio);
3156 	tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
3157 	err = tlan_get_bit(TLAN_NET_SIO_EDATA, sio);
3158 	tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
3159 	tlan_set_bit(TLAN_NET_SIO_ETXEN, sio);
3160 
3161 	if ((!err) && stop) {
3162 		/* STOP, raise data while clock is high */
3163 		tlan_clear_bit(TLAN_NET_SIO_EDATA, sio);
3164 		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
3165 		tlan_set_bit(TLAN_NET_SIO_EDATA, sio);
3166 	}
3167 
3168 	return err;
3169 
3170 }
3171 
3172 
3173 
3174 
3175 /***************************************************************
3176  *	tlan_ee_receive_byte
3177  *
3178  *	Returns:
3179  *		Nothing
3180  *	Parms:
3181  *		io_base		The IO port base address for the
3182  *				TLAN device with the EEPROM to
3183  *				use.
3184  *		data		An address to a char to hold the
3185  *				data sent from the EEPROM.
3186  *		stop		If TLAN_EEPROM_STOP is passed, a
3187  *				stop cycle is sent after the
3188  *				byte is received, and no ack is
3189  *				sent.
3190  *
3191  *	This function receives 8 bits of data from the EEPROM
3192  *	over the serial link.  It then sends and ack bit, or no
3193  *	ack and a stop bit.  This function is used to retrieve
3194  *	data after the address of a byte in the EEPROM has been
3195  *	sent.
3196  *
3197  **************************************************************/
3198 
3199 static void tlan_ee_receive_byte(u16 io_base, u8 *data, int stop)
3200 {
3201 	u8  place;
3202 	u16 sio;
3203 
3204 	outw(TLAN_NET_SIO, io_base + TLAN_DIO_ADR);
3205 	sio = io_base + TLAN_DIO_DATA + TLAN_NET_SIO;
3206 	*data = 0;
3207 
3208 	/* Assume clock is low, tx is enabled; */
3209 	tlan_clear_bit(TLAN_NET_SIO_ETXEN, sio);
3210 	for (place = 0x80; place; place >>= 1) {
3211 		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
3212 		if (tlan_get_bit(TLAN_NET_SIO_EDATA, sio))
3213 			*data |= place;
3214 		tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
3215 	}
3216 
3217 	tlan_set_bit(TLAN_NET_SIO_ETXEN, sio);
3218 	if (!stop) {
3219 		tlan_clear_bit(TLAN_NET_SIO_EDATA, sio); /* ack = 0 */
3220 		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
3221 		tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
3222 	} else {
3223 		tlan_set_bit(TLAN_NET_SIO_EDATA, sio);	/* no ack = 1 (?) */
3224 		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
3225 		tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
3226 		/* STOP, raise data while clock is high */
3227 		tlan_clear_bit(TLAN_NET_SIO_EDATA, sio);
3228 		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
3229 		tlan_set_bit(TLAN_NET_SIO_EDATA, sio);
3230 	}
3231 
3232 }
3233 
3234 
3235 
3236 
3237 /***************************************************************
3238  *	tlan_ee_read_byte
3239  *
3240  *	Returns:
3241  *		No error = 0, else, the stage at which the error
3242  *		occurred.
3243  *	Parms:
3244  *		io_base		The IO port base address for the
3245  *				TLAN device with the EEPROM to
3246  *				use.
3247  *		ee_addr		The address of the byte in the
3248  *				EEPROM whose contents are to be
3249  *				retrieved.
3250  *		data		An address to a char to hold the
3251  *				data obtained from the EEPROM.
3252  *
3253  *	This function reads a byte of information from an byte
3254  *	cell in the EEPROM.
3255  *
3256  **************************************************************/
3257 
3258 static int tlan_ee_read_byte(struct net_device *dev, u8 ee_addr, u8 *data)
3259 {
3260 	int err;
3261 	struct tlan_priv *priv = netdev_priv(dev);
3262 	unsigned long flags = 0;
3263 	int ret = 0;
3264 
3265 	spin_lock_irqsave(&priv->lock, flags);
3266 
3267 	tlan_ee_send_start(dev->base_addr);
3268 	err = tlan_ee_send_byte(dev->base_addr, 0xa0, TLAN_EEPROM_ACK);
3269 	if (err) {
3270 		ret = 1;
3271 		goto fail;
3272 	}
3273 	err = tlan_ee_send_byte(dev->base_addr, ee_addr, TLAN_EEPROM_ACK);
3274 	if (err) {
3275 		ret = 2;
3276 		goto fail;
3277 	}
3278 	tlan_ee_send_start(dev->base_addr);
3279 	err = tlan_ee_send_byte(dev->base_addr, 0xa1, TLAN_EEPROM_ACK);
3280 	if (err) {
3281 		ret = 3;
3282 		goto fail;
3283 	}
3284 	tlan_ee_receive_byte(dev->base_addr, data, TLAN_EEPROM_STOP);
3285 fail:
3286 	spin_unlock_irqrestore(&priv->lock, flags);
3287 
3288 	return ret;
3289 
3290 }
3291 
3292 
3293 
3294