File: /usr/src/linux/drivers/net/tlan.c

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