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