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