patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / net / tulip / de4x5.c
1 /*  de4x5.c: A DIGITAL DC21x4x DECchip and DE425/DE434/DE435/DE450/DE500
2              ethernet driver for Linux.
3
4     Copyright 1994, 1995 Digital Equipment Corporation.
5
6     Testing resources for this driver have been made available
7     in part by NASA Ames Research Center (mjacob@nas.nasa.gov).
8
9     The author may be reached at davies@maniac.ultranet.com.
10
11     This program is free software; you can redistribute  it and/or modify it
12     under  the terms of  the GNU General  Public License as published by the
13     Free Software Foundation;  either version 2 of the  License, or (at your
14     option) any later version.
15
16     THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR   IMPLIED
17     WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
18     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
19     NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT,  INDIRECT,
20     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21     NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
22     USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23     ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
24     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27     You should have received a copy of the  GNU General Public License along
28     with this program; if not, write  to the Free Software Foundation, Inc.,
29     675 Mass Ave, Cambridge, MA 02139, USA.
30
31     Originally,   this  driver  was    written  for the  Digital   Equipment
32     Corporation series of EtherWORKS ethernet cards:
33
34         DE425 TP/COAX EISA
35         DE434 TP PCI
36         DE435 TP/COAX/AUI PCI
37         DE450 TP/COAX/AUI PCI
38         DE500 10/100 PCI Fasternet
39
40     but it  will  now attempt  to  support all  cards which   conform to the
41     Digital Semiconductor   SROM   Specification.    The  driver   currently
42     recognises the following chips:
43
44         DC21040  (no SROM) 
45         DC21041[A]  
46         DC21140[A] 
47         DC21142 
48         DC21143 
49
50     So far the driver is known to work with the following cards:
51
52         KINGSTON
53         Linksys
54         ZNYX342
55         SMC8432
56         SMC9332 (w/new SROM)
57         ZNYX31[45]
58         ZNYX346 10/100 4 port (can act as a 10/100 bridge!) 
59
60     The driver has been tested on a relatively busy network using the DE425,
61     DE434, DE435 and DE500 cards and benchmarked with 'ttcp': it transferred
62     16M of data to a DECstation 5000/200 as follows:
63
64                 TCP           UDP
65              TX     RX     TX     RX
66     DE425   1030k  997k   1170k  1128k
67     DE434   1063k  995k   1170k  1125k
68     DE435   1063k  995k   1170k  1125k
69     DE500   1063k  998k   1170k  1125k  in 10Mb/s mode
70
71     All  values are typical (in   kBytes/sec) from a  sample  of 4 for  each
72     measurement. Their error is +/-20k on a quiet (private) network and also
73     depend on what load the CPU has.
74
75     =========================================================================
76     This driver  has been written substantially  from  scratch, although its
77     inheritance of style and stack interface from 'ewrk3.c' and in turn from
78     Donald Becker's 'lance.c' should be obvious. With the module autoload of
79     every  usable DECchip board,  I  pinched Donald's 'next_module' field to
80     link my modules together.
81
82     Upto 15 EISA cards can be supported under this driver, limited primarily
83     by the available IRQ lines.  I have  checked different configurations of
84     multiple depca, EtherWORKS 3 cards and de4x5 cards and  have not found a
85     problem yet (provided you have at least depca.c v0.38) ...
86
87     PCI support has been added  to allow the driver  to work with the DE434,
88     DE435, DE450 and DE500 cards. The I/O accesses are a bit of a kludge due
89     to the differences in the EISA and PCI CSR address offsets from the base
90     address.
91
92     The ability to load this  driver as a loadable  module has been included
93     and used extensively  during the driver development  (to save those long
94     reboot sequences).  Loadable module support  under PCI and EISA has been
95     achieved by letting the driver autoprobe as if it were compiled into the
96     kernel. Do make sure  you're not sharing  interrupts with anything  that
97     cannot accommodate  interrupt  sharing!
98
99     To utilise this ability, you have to do 8 things:
100
101     0) have a copy of the loadable modules code installed on your system.
102     1) copy de4x5.c from the  /linux/drivers/net directory to your favourite
103     temporary directory.
104     2) for fixed  autoprobes (not  recommended),  edit the source code  near
105     line 5594 to reflect the I/O address  you're using, or assign these when
106     loading by:
107
108                    insmod de4x5 io=0xghh           where g = bus number
109                                                         hh = device number   
110
111        NB: autoprobing for modules is now supported by default. You may just
112            use:
113
114                    insmod de4x5
115
116            to load all available boards. For a specific board, still use
117            the 'io=?' above.
118     3) compile  de4x5.c, but include -DMODULE in  the command line to ensure
119     that the correct bits are compiled (see end of source code).
120     4) if you are wanting to add a new  card, goto 5. Otherwise, recompile a
121     kernel with the de4x5 configuration turned off and reboot.
122     5) insmod de4x5 [io=0xghh]
123     6) run the net startup bits for your new eth?? interface(s) manually 
124     (usually /etc/rc.inet[12] at boot time). 
125     7) enjoy!
126
127     To unload a module, turn off the associated interface(s) 
128     'ifconfig eth?? down' then 'rmmod de4x5'.
129
130     Automedia detection is included so that in  principal you can disconnect
131     from, e.g.  TP, reconnect  to BNC  and  things will still work  (after a
132     pause whilst the   driver figures out   where its media went).  My tests
133     using ping showed that it appears to work....
134
135     By  default,  the driver will  now   autodetect any  DECchip based card.
136     Should you have a need to restrict the driver to DIGITAL only cards, you
137     can compile with a  DEC_ONLY define, or if  loading as a module, use the
138     'dec_only=1'  parameter. 
139
140     I've changed the timing routines to  use the kernel timer and scheduling
141     functions  so that the  hangs  and other assorted problems that occurred
142     while autosensing the  media  should be gone.  A  bonus  for the DC21040
143     auto  media sense algorithm is  that it can now  use one that is more in
144     line with the  rest (the DC21040  chip doesn't  have a hardware  timer).
145     The downside is the 1 'jiffies' (10ms) resolution.
146
147     IEEE 802.3u MII interface code has  been added in anticipation that some
148     products may use it in the future.
149
150     The SMC9332 card  has a non-compliant SROM  which needs fixing -  I have
151     patched this  driver to detect it  because the SROM format used complies
152     to a previous DEC-STD format.
153
154     I have removed the buffer copies needed for receive on Intels.  I cannot
155     remove them for   Alphas since  the  Tulip hardware   only does longword
156     aligned  DMA transfers  and  the  Alphas get   alignment traps with  non
157     longword aligned data copies (which makes them really slow). No comment.
158
159     I  have added SROM decoding  routines to make this  driver work with any
160     card that  supports the Digital  Semiconductor SROM spec. This will help
161     all  cards running the dc2114x  series chips in particular.  Cards using
162     the dc2104x  chips should run correctly with  the basic  driver.  I'm in
163     debt to <mjacob@feral.com> for the  testing and feedback that helped get
164     this feature working.  So far we have  tested KINGSTON, SMC8432, SMC9332
165     (with the latest SROM complying  with the SROM spec  V3: their first was
166     broken), ZNYX342  and  LinkSys. ZYNX314 (dual  21041  MAC) and  ZNYX 315
167     (quad 21041 MAC)  cards also  appear  to work despite their  incorrectly
168     wired IRQs.
169
170     I have added a temporary fix for interrupt problems when some SCSI cards
171     share the same interrupt as the DECchip based  cards. The problem occurs
172     because  the SCSI card wants to  grab the interrupt  as a fast interrupt
173     (runs the   service routine with interrupts turned   off) vs.  this card
174     which really needs to run the service routine with interrupts turned on.
175     This driver will  now   add the interrupt service   routine  as  a  fast
176     interrupt if it   is bounced from the   slow interrupt.  THIS IS NOT   A
177     RECOMMENDED WAY TO RUN THE DRIVER  and has been done  for a limited time
178     until  people   sort  out their  compatibility    issues and the  kernel
179     interrupt  service code  is  fixed.   YOU  SHOULD SEPARATE OUT  THE FAST
180     INTERRUPT CARDS FROM THE SLOW INTERRUPT CARDS to ensure that they do not
181     run on the same interrupt. PCMCIA/CardBus is another can of worms...
182
183     Finally, I think  I have really  fixed  the module  loading problem with
184     more than one DECchip based  card.  As a  side effect, I don't mess with
185     the  device structure any  more which means that  if more than 1 card in
186     2.0.x is    installed (4  in   2.1.x),  the  user   will have   to  edit
187     linux/drivers/net/Space.c  to make room for  them. Hence, module loading
188     is  the preferred way to use   this driver, since  it  doesn't have this
189     limitation.
190
191     Where SROM media  detection is used and  full duplex is specified in the
192     SROM,  the feature is  ignored unless  lp->params.fdx  is set at compile
193     time  OR during  a   module load  (insmod  de4x5   args='eth??:fdx' [see
194     below]).  This is because there  is no way  to automatically detect full
195     duplex   links  except through   autonegotiation.    When I  include the
196     autonegotiation feature in  the SROM autoconf  code, this detection will
197     occur automatically for that case.
198
199     Command  line arguments are  now  allowed, similar  to passing arguments
200     through LILO. This will allow a per adapter board  set up of full duplex
201     and media. The only lexical constraints  are: the board name (dev->name)
202     appears in the list before its  parameters.  The list of parameters ends
203     either at the end of the parameter list or with another board name.  The
204     following parameters are allowed:
205
206             fdx        for full duplex
207             autosense  to set the media/speed; with the following 
208                        sub-parameters:
209                        TP, TP_NW, BNC, AUI, BNC_AUI, 100Mb, 10Mb, AUTO
210
211     Case sensitivity is important  for  the sub-parameters. They *must*   be
212     upper case. Examples:
213
214         insmod de4x5 args='eth1:fdx autosense=BNC eth0:autosense=100Mb'.
215
216     For a compiled in driver, at or above line 548, place e.g.
217         #define DE4X5_PARM "eth0:fdx autosense=AUI eth2:autosense=TP"
218
219     Yes,  I know full duplex isn't  permissible on BNC  or AUI; they're just
220     examples. By default, full duplex is turned off and  AUTO is the default
221     autosense setting.  In reality, I expect only  the full duplex option to
222     be used. Note the use of single quotes in the two examples above and the
223     lack of commas to separate items. ALSO, you must get the requested media
224     correct in relation to what the adapter SROM says it has. There's no way
225     to  determine this in  advance other than by  trial and error and common
226     sense, e.g. call a BNC connectored port 'BNC', not '10Mb'.
227
228     Changed the bus probing.  EISA used to be  done first,  followed by PCI.
229     Most people probably don't even know  what a de425 is today and the EISA
230     probe has messed  up some SCSI cards  in the past,  so now PCI is always
231     probed  first  followed by  EISA if  a) the architecture allows EISA and
232     either  b) there have been no PCI cards detected or  c) an EISA probe is
233     forced by  the user.  To force  a probe  include  "force_eisa"  in  your
234     insmod "args" line;  for built-in kernels either change the driver to do
235     this  automatically  or include  #define DE4X5_FORCE_EISA  on or  before
236     line 1040 in the driver.
237
238     TO DO: 
239     ------
240
241     Revision History
242     ----------------
243
244     Version   Date        Description
245   
246       0.1     17-Nov-94   Initial writing. ALPHA code release.
247       0.2     13-Jan-95   Added PCI support for DE435's.
248       0.21    19-Jan-95   Added auto media detection.
249       0.22    10-Feb-95   Fix interrupt handler call <chris@cosy.sbg.ac.at>.
250                           Fix recognition bug reported by <bkm@star.rl.ac.uk>.
251                           Add request/release_region code.
252                           Add loadable modules support for PCI.
253                           Clean up loadable modules support.
254       0.23    28-Feb-95   Added DC21041 and DC21140 support. 
255                           Fix missed frame counter value and initialisation.
256                           Fixed EISA probe.
257       0.24    11-Apr-95   Change delay routine to use <linux/udelay>.
258                           Change TX_BUFFS_AVAIL macro.
259                           Change media autodetection to allow manual setting.
260                           Completed DE500 (DC21140) support.
261       0.241   18-Apr-95   Interim release without DE500 Autosense Algorithm.
262       0.242   10-May-95   Minor changes.
263       0.30    12-Jun-95   Timer fix for DC21140.
264                           Portability changes.
265                           Add ALPHA changes from <jestabro@ant.tay1.dec.com>.
266                           Add DE500 semi automatic autosense.
267                           Add Link Fail interrupt TP failure detection.
268                           Add timer based link change detection.
269                           Plugged a memory leak in de4x5_queue_pkt().
270       0.31    13-Jun-95   Fixed PCI stuff for 1.3.1.
271       0.32    26-Jun-95   Added verify_area() calls in de4x5_ioctl() from a
272                           suggestion by <heiko@colossus.escape.de>.
273       0.33     8-Aug-95   Add shared interrupt support (not released yet).
274       0.331   21-Aug-95   Fix de4x5_open() with fast CPUs.
275                           Fix de4x5_interrupt().
276                           Fix dc21140_autoconf() mess.
277                           No shared interrupt support.
278       0.332   11-Sep-95   Added MII management interface routines.
279       0.40     5-Mar-96   Fix setup frame timeout <maartenb@hpkuipc.cern.ch>.
280                           Add kernel timer code (h/w is too flaky).
281                           Add MII based PHY autosense.
282                           Add new multicasting code.
283                           Add new autosense algorithms for media/mode 
284                           selection using kernel scheduling/timing.
285                           Re-formatted.
286                           Made changes suggested by <jeff@router.patch.net>:
287                             Change driver to detect all DECchip based cards
288                             with DEC_ONLY restriction a special case.
289                             Changed driver to autoprobe as a module. No irq
290                             checking is done now - assume BIOS is good!
291                           Added SMC9332 detection <manabe@Roy.dsl.tutics.ac.jp>
292       0.41    21-Mar-96   Don't check for get_hw_addr checksum unless DEC card
293                           only <niles@axp745gsfc.nasa.gov>
294                           Fix for multiple PCI cards reported by <jos@xos.nl>
295                           Duh, put the SA_SHIRQ flag into request_interrupt().
296                           Fix SMC ethernet address in enet_det[].
297                           Print chip name instead of "UNKNOWN" during boot.
298       0.42    26-Apr-96   Fix MII write TA bit error.
299                           Fix bug in dc21040 and dc21041 autosense code.
300                           Remove buffer copies on receive for Intels.
301                           Change sk_buff handling during media disconnects to
302                            eliminate DUP packets.
303                           Add dynamic TX thresholding.
304                           Change all chips to use perfect multicast filtering.
305                           Fix alloc_device() bug <jari@markkus2.fimr.fi>
306       0.43   21-Jun-96    Fix unconnected media TX retry bug.
307                           Add Accton to the list of broken cards.
308                           Fix TX under-run bug for non DC21140 chips.
309                           Fix boot command probe bug in alloc_device() as
310                            reported by <koen.gadeyne@barco.com> and 
311                            <orava@nether.tky.hut.fi>.
312                           Add cache locks to prevent a race condition as
313                            reported by <csd@microplex.com> and 
314                            <baba@beckman.uiuc.edu>.
315                           Upgraded alloc_device() code.
316       0.431  28-Jun-96    Fix potential bug in queue_pkt() from discussion
317                           with <csd@microplex.com>
318       0.44   13-Aug-96    Fix RX overflow bug in 2114[023] chips.
319                           Fix EISA probe bugs reported by <os2@kpi.kharkov.ua>
320                           and <michael@compurex.com>.
321       0.441   9-Sep-96    Change dc21041_autoconf() to probe quiet BNC media
322                            with a loopback packet.
323       0.442   9-Sep-96    Include AUI in dc21041 media printout. Bug reported
324                            by <bhat@mundook.cs.mu.OZ.AU>
325       0.45    8-Dec-96    Include endian functions for PPC use, from work 
326                            by <cort@cs.nmt.edu> and <g.thomas@opengroup.org>.
327       0.451  28-Dec-96    Added fix to allow autoprobe for modules after
328                            suggestion from <mjacob@feral.com>.
329       0.5    30-Jan-97    Added SROM decoding functions.
330                           Updated debug flags.
331                           Fix sleep/wakeup calls for PCI cards, bug reported
332                            by <cross@gweep.lkg.dec.com>.
333                           Added multi-MAC, one SROM feature from discussion
334                            with <mjacob@feral.com>.
335                           Added full module autoprobe capability.
336                           Added attempt to use an SMC9332 with broken SROM.
337                           Added fix for ZYNX multi-mac cards that didn't
338                            get their IRQs wired correctly.
339       0.51   13-Feb-97    Added endian fixes for the SROM accesses from
340                            <paubert@iram.es>
341                           Fix init_connection() to remove extra device reset.
342                           Fix MAC/PHY reset ordering in dc21140m_autoconf().
343                           Fix initialisation problem with lp->timeout in
344                            typeX_infoblock() from <paubert@iram.es>.
345                           Fix MII PHY reset problem from work done by
346                            <paubert@iram.es>.
347       0.52   26-Apr-97    Some changes may not credit the right people -
348                            a disk crash meant I lost some mail.
349                           Change RX interrupt routine to drop rather than 
350                            defer packets to avoid hang reported by 
351                            <g.thomas@opengroup.org>.
352                           Fix srom_exec() to return for COMPACT and type 1
353                            infoblocks.
354                           Added DC21142 and DC21143 functions.
355                           Added byte counters from <phil@tazenda.demon.co.uk>
356                           Added SA_INTERRUPT temporary fix from 
357                            <mjacob@feral.com>.
358       0.53   12-Nov-97    Fix the *_probe() to include 'eth??' name during
359                            module load: bug reported by
360                            <Piete.Brooks@cl.cam.ac.uk>
361                           Fix multi-MAC, one SROM, to work with 2114x chips:
362                            bug reported by <cmetz@inner.net>.
363                           Make above search independent of BIOS device scan
364                            direction.
365                           Completed DC2114[23] autosense functions.
366       0.531  21-Dec-97    Fix DE500-XA 100Mb/s bug reported by 
367                            <robin@intercore.com
368                           Fix type1_infoblock() bug introduced in 0.53, from
369                            problem reports by 
370                            <parmee@postecss.ncrfran.france.ncr.com> and
371                            <jo@ice.dillingen.baynet.de>.
372                           Added argument list to set up each board from either
373                            a module's command line or a compiled in #define.
374                           Added generic MII PHY functionality to deal with
375                            newer PHY chips.
376                           Fix the mess in 2.1.67.
377       0.532   5-Jan-98    Fix bug in mii_get_phy() reported by 
378                            <redhat@cococo.net>.
379                           Fix bug in pci_probe() for 64 bit systems reported
380                            by <belliott@accessone.com>.
381       0.533   9-Jan-98    Fix more 64 bit bugs reported by <jal@cs.brown.edu>.
382       0.534  24-Jan-98    Fix last (?) endian bug from <geert@linux-m68k.org>
383       0.535  21-Feb-98    Fix Ethernet Address PROM reset bug for DC21040.
384       0.536  21-Mar-98    Change pci_probe() to use the pci_dev structure.
385                           **Incompatible with 2.0.x from here.**
386       0.540   5-Jul-98    Atomicize assertion of dev->interrupt for SMP
387                            from <lma@varesearch.com>
388                           Add TP, AUI and BNC cases to 21140m_autoconf() for
389                            case where a 21140 under SROM control uses, e.g. AUI
390                            from problem report by <delchini@lpnp09.in2p3.fr>
391                           Add MII parallel detection to 2114x_autoconf() for
392                            case where no autonegotiation partner exists from
393                            problem report by <mlapsley@ndirect.co.uk>.
394                           Add ability to force connection type directly even
395                            when using SROM control from problem report by
396                            <earl@exis.net>.
397                           Updated the PCI interface to conform with the latest
398                            version. I hope nothing is broken...
399                           Add TX done interrupt modification from suggestion
400                            by <Austin.Donnelly@cl.cam.ac.uk>.
401                           Fix is_anc_capable() bug reported by 
402                            <Austin.Donnelly@cl.cam.ac.uk>.
403                           Fix type[13]_infoblock() bug: during MII search, PHY
404                            lp->rst not run because lp->ibn not initialised -
405                            from report & fix by <paubert@iram.es>.
406                           Fix probe bug with EISA & PCI cards present from
407                            report by <eirik@netcom.com>.
408       0.541  24-Aug-98    Fix compiler problems associated with i386-string
409                            ops from multiple bug reports and temporary fix
410                            from <paubert@iram.es>.
411                           Fix pci_probe() to correctly emulate the old
412                            pcibios_find_class() function.
413                           Add an_exception() for old ZYNX346 and fix compile
414                            warning on PPC & SPARC, from <ecd@skynet.be>.
415                           Fix lastPCI to correctly work with compiled in
416                            kernels and modules from bug report by 
417                            <Zlatko.Calusic@CARNet.hr> et al.
418       0.542  15-Sep-98    Fix dc2114x_autoconf() to stop multiple messages
419                            when media is unconnected.
420                           Change dev->interrupt to lp->interrupt to ensure
421                            alignment for Alpha's and avoid their unaligned
422                            access traps. This flag is merely for log messages:
423                            should do something more definitive though...
424       0.543  30-Dec-98    Add SMP spin locking.
425       0.544   8-May-99    Fix for buggy SROM in Motorola embedded boards using
426                            a 21143 by <mmporter@home.com>.
427                           Change PCI/EISA bus probing order.
428       0.545  28-Nov-99    Further Moto SROM bug fix from 
429                            <mporter@eng.mcd.mot.com>
430                           Remove double checking for DEBUG_RX in de4x5_dbg_rx()
431                            from report by <geert@linux-m68k.org>
432       0.546  22-Feb-01    Fixes Alpha XP1000 oops.  The srom_search function
433                            was causing a page fault when initializing the
434                            variable 'pb', on a non de4x5 PCI device, in this
435                            case a PCI bridge (DEC chip 21152). The value of
436                            'pb' is now only initialized if a de4x5 chip is
437                            present. 
438                            <france@handhelds.org>  
439       0.547  08-Nov-01    Use library crc32 functions by <Matt_Domsch@dell.com>
440       0.548  30-Aug-03    Big 2.6 cleanup. Ported to PCI/EISA probing and
441                            generic DMA APIs. Fixed DE425 support on Alpha.
442                            <maz@wild-wind.fr.eu.org>
443     =========================================================================
444 */
445
446 #include <linux/config.h>
447 #include <linux/module.h>
448 #include <linux/kernel.h>
449 #include <linux/string.h>
450 #include <linux/interrupt.h>
451 #include <linux/ptrace.h>
452 #include <linux/errno.h>
453 #include <linux/ioport.h>
454 #include <linux/slab.h>
455 #include <linux/pci.h>
456 #include <linux/eisa.h>
457 #include <linux/delay.h>
458 #include <linux/init.h>
459 #include <linux/spinlock.h>
460 #include <linux/crc32.h>
461 #include <linux/netdevice.h>
462 #include <linux/etherdevice.h>
463 #include <linux/skbuff.h>
464 #include <linux/time.h>
465 #include <linux/types.h>
466 #include <linux/unistd.h>
467 #include <linux/ctype.h>
468 #include <linux/dma-mapping.h>
469 #include <linux/moduleparam.h>
470
471 #include <asm/bitops.h>
472 #include <asm/io.h>
473 #include <asm/dma.h>
474 #include <asm/byteorder.h>
475 #include <asm/unaligned.h>
476 #include <asm/uaccess.h>
477 #ifdef CONFIG_PPC_MULTIPLATFORM
478 #include <asm/machdep.h>
479 #endif /* CONFIG_PPC_MULTIPLATFORM */
480
481 #include "de4x5.h"
482
483 static char version[] __devinitdata = "de4x5.c:V0.546 2001/02/22 davies@maniac.ultranet.com\n";
484
485 #define c_char const char
486 #define TWIDDLE(a) (u_short)le16_to_cpu(get_unaligned((u_short *)(a)))
487
488 /*
489 ** MII Information
490 */
491 struct phy_table {
492     int reset;              /* Hard reset required?                         */
493     int id;                 /* IEEE OUI                                     */
494     int ta;                 /* One cycle TA time - 802.3u is confusing here */
495     struct {                /* Non autonegotiation (parallel) speed det.    */
496         int reg;
497         int mask;
498         int value;
499     } spd;
500 };
501
502 struct mii_phy {
503     int reset;              /* Hard reset required?                      */
504     int id;                 /* IEEE OUI                                  */
505     int ta;                 /* One cycle TA time                         */
506     struct {                /* Non autonegotiation (parallel) speed det. */
507         int reg;
508         int mask;
509         int value;
510     } spd;
511     int addr;               /* MII address for the PHY                   */
512     u_char  *gep;           /* Start of GEP sequence block in SROM       */
513     u_char  *rst;           /* Start of reset sequence in SROM           */
514     u_int mc;               /* Media Capabilities                        */
515     u_int ana;              /* NWay Advertisement                        */
516     u_int fdx;              /* Full DupleX capabilites for each media    */
517     u_int ttm;              /* Transmit Threshold Mode for each media    */
518     u_int mci;              /* 21142 MII Connector Interrupt info        */
519 };
520
521 #define DE4X5_MAX_PHY 8     /* Allow upto 8 attached PHY devices per board */
522
523 struct sia_phy {
524     u_char mc;              /* Media Code                                */
525     u_char ext;             /* csr13-15 valid when set                   */
526     int csr13;              /* SIA Connectivity Register                 */
527     int csr14;              /* SIA TX/RX Register                        */
528     int csr15;              /* SIA General Register                      */
529     int gepc;               /* SIA GEP Control Information               */
530     int gep;                /* SIA GEP Data                              */
531 };
532
533 /*
534 ** Define the know universe of PHY devices that can be
535 ** recognised by this driver.
536 */
537 static struct phy_table phy_info[] = {
538     {0, NATIONAL_TX, 1, {0x19, 0x40, 0x00}},       /* National TX      */
539     {1, BROADCOM_T4, 1, {0x10, 0x02, 0x02}},       /* Broadcom T4      */
540     {0, SEEQ_T4    , 1, {0x12, 0x10, 0x10}},       /* SEEQ T4          */
541     {0, CYPRESS_T4 , 1, {0x05, 0x20, 0x20}},       /* Cypress T4       */
542     {0, 0x7810     , 1, {0x14, 0x0800, 0x0800}}    /* Level One LTX970 */
543 };
544
545 /*
546 ** These GENERIC values assumes that the PHY devices follow 802.3u and
547 ** allow parallel detection to set the link partner ability register.
548 ** Detection of 100Base-TX [H/F Duplex] and 100Base-T4 is supported.
549 */
550 #define GENERIC_REG   0x05      /* Autoneg. Link Partner Advertisement Reg. */
551 #define GENERIC_MASK  MII_ANLPA_100M /* All 100Mb/s Technologies            */
552 #define GENERIC_VALUE MII_ANLPA_100M /* 100B-TX, 100B-TX FDX, 100B-T4       */
553
554 /*
555 ** Define special SROM detection cases
556 */
557 static c_char enet_det[][ETH_ALEN] = {
558     {0x00, 0x00, 0xc0, 0x00, 0x00, 0x00},
559     {0x00, 0x00, 0xe8, 0x00, 0x00, 0x00}
560 };
561
562 #define SMC    1
563 #define ACCTON 2
564
565 /*
566 ** SROM Repair definitions. If a broken SROM is detected a card may
567 ** use this information to help figure out what to do. This is a
568 ** "stab in the dark" and so far for SMC9332's only.
569 */
570 static c_char srom_repair_info[][100] = {
571     {0x00,0x1e,0x00,0x00,0x00,0x08,             /* SMC9332 */
572      0x1f,0x01,0x8f,0x01,0x00,0x01,0x00,0x02,
573      0x01,0x00,0x00,0x78,0xe0,0x01,0x00,0x50,
574      0x00,0x18,}
575 };
576
577
578 #ifdef DE4X5_DEBUG
579 static int de4x5_debug = DE4X5_DEBUG;
580 #else
581 /*static int de4x5_debug = (DEBUG_MII | DEBUG_SROM | DEBUG_PCICFG | DEBUG_MEDIA | DEBUG_VERSION);*/
582 static int de4x5_debug = (DEBUG_MEDIA | DEBUG_VERSION);
583 #endif
584
585 /*
586 ** Allow per adapter set up. For modules this is simply a command line
587 ** parameter, e.g.: 
588 ** insmod de4x5 args='eth1:fdx autosense=BNC eth0:autosense=100Mb'.
589 **
590 ** For a compiled in driver, place e.g.
591 **     #define DE4X5_PARM "eth0:fdx autosense=AUI eth2:autosense=TP"
592 ** here
593 */
594 #ifdef DE4X5_PARM
595 static char *args = DE4X5_PARM;
596 #else
597 static char *args;
598 #endif
599
600 struct parameters {
601     int fdx;
602     int autosense;
603 };
604
605 #define DE4X5_AUTOSENSE_MS 250      /* msec autosense tick (DE500) */
606
607 #define DE4X5_NDA 0xffe0            /* No Device (I/O) Address */
608
609 /*
610 ** Ethernet PROM defines
611 */
612 #define PROBE_LENGTH    32
613 #define ETH_PROM_SIG    0xAA5500FFUL
614
615 /*
616 ** Ethernet Info
617 */
618 #define PKT_BUF_SZ      1536            /* Buffer size for each Tx/Rx buffer */
619 #define IEEE802_3_SZ    1518            /* Packet + CRC */
620 #define MAX_PKT_SZ      1514            /* Maximum ethernet packet length */
621 #define MAX_DAT_SZ      1500            /* Maximum ethernet data length */
622 #define MIN_DAT_SZ      1               /* Minimum ethernet data length */
623 #define PKT_HDR_LEN     14              /* Addresses and data length info */
624 #define FAKE_FRAME_LEN  (MAX_PKT_SZ + 1)
625 #define QUEUE_PKT_TIMEOUT (3*HZ)        /* 3 second timeout */
626
627
628 /*
629 ** EISA bus defines
630 */
631 #define DE4X5_EISA_IO_PORTS   0x0c00    /* I/O port base address, slot 0 */
632 #define DE4X5_EISA_TOTAL_SIZE 0x100     /* I/O address extent */
633
634 #define EISA_ALLOWED_IRQ_LIST  {5, 9, 10, 11}
635
636 #define DE4X5_SIGNATURE {"DE425","DE434","DE435","DE450","DE500"}
637 #define DE4X5_NAME_LENGTH 8
638
639 static c_char *de4x5_signatures[] = DE4X5_SIGNATURE;
640
641 /*
642 ** Ethernet PROM defines for DC21040
643 */
644 #define PROBE_LENGTH    32
645 #define ETH_PROM_SIG    0xAA5500FFUL
646
647 /*
648 ** PCI Bus defines
649 */
650 #define PCI_MAX_BUS_NUM      8
651 #define DE4X5_PCI_TOTAL_SIZE 0x80       /* I/O address extent */
652 #define DE4X5_CLASS_CODE     0x00020000 /* Network controller, Ethernet */
653
654 /*
655 ** Memory Alignment. Each descriptor is 4 longwords long. To force a
656 ** particular alignment on the TX descriptor, adjust DESC_SKIP_LEN and
657 ** DESC_ALIGN. ALIGN aligns the start address of the private memory area
658 ** and hence the RX descriptor ring's first entry. 
659 */
660 #define DE4X5_ALIGN4      ((u_long)4 - 1)     /* 1 longword align */
661 #define DE4X5_ALIGN8      ((u_long)8 - 1)     /* 2 longword align */
662 #define DE4X5_ALIGN16     ((u_long)16 - 1)    /* 4 longword align */
663 #define DE4X5_ALIGN32     ((u_long)32 - 1)    /* 8 longword align */
664 #define DE4X5_ALIGN64     ((u_long)64 - 1)    /* 16 longword align */
665 #define DE4X5_ALIGN128    ((u_long)128 - 1)   /* 32 longword align */
666
667 #define DE4X5_ALIGN         DE4X5_ALIGN32           /* Keep the DC21040 happy... */
668 #define DE4X5_CACHE_ALIGN   CAL_16LONG
669 #define DESC_SKIP_LEN DSL_0             /* Must agree with DESC_ALIGN */
670 /*#define DESC_ALIGN    u32 dummy[4];  / * Must agree with DESC_SKIP_LEN */
671 #define DESC_ALIGN
672
673 #ifndef DEC_ONLY                        /* See README.de4x5 for using this */
674 static int dec_only;
675 #else
676 static int dec_only = 1;
677 #endif
678
679 /*
680 ** DE4X5 IRQ ENABLE/DISABLE
681 */
682 #define ENABLE_IRQs { \
683     imr |= lp->irq_en;\
684     outl(imr, DE4X5_IMR);               /* Enable the IRQs */\
685 }
686
687 #define DISABLE_IRQs {\
688     imr = inl(DE4X5_IMR);\
689     imr &= ~lp->irq_en;\
690     outl(imr, DE4X5_IMR);               /* Disable the IRQs */\
691 }
692
693 #define UNMASK_IRQs {\
694     imr |= lp->irq_mask;\
695     outl(imr, DE4X5_IMR);               /* Unmask the IRQs */\
696 }
697
698 #define MASK_IRQs {\
699     imr = inl(DE4X5_IMR);\
700     imr &= ~lp->irq_mask;\
701     outl(imr, DE4X5_IMR);               /* Mask the IRQs */\
702 }
703
704 /*
705 ** DE4X5 START/STOP
706 */
707 #define START_DE4X5 {\
708     omr = inl(DE4X5_OMR);\
709     omr |= OMR_ST | OMR_SR;\
710     outl(omr, DE4X5_OMR);               /* Enable the TX and/or RX */\
711 }
712
713 #define STOP_DE4X5 {\
714     omr = inl(DE4X5_OMR);\
715     omr &= ~(OMR_ST|OMR_SR);\
716     outl(omr, DE4X5_OMR);               /* Disable the TX and/or RX */ \
717 }
718
719 /*
720 ** DE4X5 SIA RESET
721 */
722 #define RESET_SIA outl(0, DE4X5_SICR);  /* Reset SIA connectivity regs */
723
724 /*
725 ** DE500 AUTOSENSE TIMER INTERVAL (MILLISECS)
726 */
727 #define DE4X5_AUTOSENSE_MS  250
728
729 /*
730 ** SROM Structure
731 */
732 struct de4x5_srom {
733     char sub_vendor_id[2];
734     char sub_system_id[2];
735     char reserved[12];
736     char id_block_crc;
737     char reserved2;
738     char version;
739     char num_controllers;
740     char ieee_addr[6];
741     char info[100];
742     short chksum;
743 };
744 #define SUB_VENDOR_ID 0x500a
745
746 /*
747 ** DE4X5 Descriptors. Make sure that all the RX buffers are contiguous
748 ** and have sizes of both a power of 2 and a multiple of 4.
749 ** A size of 256 bytes for each buffer could be chosen because over 90% of
750 ** all packets in our network are <256 bytes long and 64 longword alignment
751 ** is possible. 1536 showed better 'ttcp' performance. Take your pick. 32 TX
752 ** descriptors are needed for machines with an ALPHA CPU.
753 */
754 #define NUM_RX_DESC 8                   /* Number of RX descriptors   */
755 #define NUM_TX_DESC 32                  /* Number of TX descriptors   */
756 #define RX_BUFF_SZ  1536                /* Power of 2 for kmalloc and */
757                                         /* Multiple of 4 for DC21040  */
758                                         /* Allows 512 byte alignment  */
759 struct de4x5_desc {
760     volatile s32 status;
761     u32 des1;
762     u32 buf;
763     u32 next;
764     DESC_ALIGN
765 };
766
767 /*
768 ** The DE4X5 private structure
769 */
770 #define DE4X5_PKT_STAT_SZ 16
771 #define DE4X5_PKT_BIN_SZ  128            /* Should be >=100 unless you
772                                             increase DE4X5_PKT_STAT_SZ */
773
774 struct pkt_stats {
775         u_int bins[DE4X5_PKT_STAT_SZ];      /* Private stats counters       */
776         u_int unicast;
777         u_int multicast;
778         u_int broadcast;
779         u_int excessive_collisions;
780         u_int tx_underruns;
781         u_int excessive_underruns;
782         u_int rx_runt_frames;
783         u_int rx_collision;
784         u_int rx_dribble;
785         u_int rx_overflow;
786 };
787
788 struct de4x5_private {
789     char adapter_name[80];                  /* Adapter name                 */
790     u_long interrupt;                       /* Aligned ISR flag             */
791     struct de4x5_desc *rx_ring;             /* RX descriptor ring           */
792     struct de4x5_desc *tx_ring;             /* TX descriptor ring           */
793     struct sk_buff *tx_skb[NUM_TX_DESC];    /* TX skb for freeing when sent */
794     struct sk_buff *rx_skb[NUM_RX_DESC];    /* RX skb's                     */
795     int rx_new, rx_old;                     /* RX descriptor ring pointers  */
796     int tx_new, tx_old;                     /* TX descriptor ring pointers  */
797     char setup_frame[SETUP_FRAME_LEN];      /* Holds MCA and PA info.       */
798     char frame[64];                         /* Min sized packet for loopback*/
799     spinlock_t lock;                        /* Adapter specific spinlock    */
800     struct net_device_stats stats;          /* Public stats                 */
801     struct pkt_stats pktStats;              /* Private stats counters       */
802     char rxRingSize;
803     char txRingSize;
804     int  bus;                               /* EISA or PCI                  */
805     int  bus_num;                           /* PCI Bus number               */
806     int  device;                            /* Device number on PCI bus     */
807     int  state;                             /* Adapter OPENED or CLOSED     */
808     int  chipset;                           /* DC21040, DC21041 or DC21140  */
809     s32  irq_mask;                          /* Interrupt Mask (Enable) bits */
810     s32  irq_en;                            /* Summary interrupt bits       */
811     int  media;                             /* Media (eg TP), mode (eg 100B)*/
812     int  c_media;                           /* Remember the last media conn */
813     int  fdx;                               /* media full duplex flag       */
814     int  linkOK;                            /* Link is OK                   */
815     int  autosense;                         /* Allow/disallow autosensing   */
816     int  tx_enable;                         /* Enable descriptor polling    */
817     int  setup_f;                           /* Setup frame filtering type   */
818     int  local_state;                       /* State within a 'media' state */
819     struct mii_phy phy[DE4X5_MAX_PHY];      /* List of attached PHY devices */
820     struct sia_phy sia;                     /* SIA PHY Information          */
821     int  active;                            /* Index to active PHY device   */
822     int  mii_cnt;                           /* Number of attached PHY's     */
823     int  timeout;                           /* Scheduling counter           */
824     struct timer_list timer;                /* Timer info for kernel        */
825     int tmp;                                /* Temporary global per card    */
826     struct {
827         u_long lock;                        /* Lock the cache accesses      */
828         s32 csr0;                           /* Saved Bus Mode Register      */
829         s32 csr6;                           /* Saved Operating Mode Reg.    */
830         s32 csr7;                           /* Saved IRQ Mask Register      */
831         s32 gep;                            /* Saved General Purpose Reg.   */
832         s32 gepc;                           /* Control info for GEP         */
833         s32 csr13;                          /* Saved SIA Connectivity Reg.  */
834         s32 csr14;                          /* Saved SIA TX/RX Register     */
835         s32 csr15;                          /* Saved SIA General Register   */
836         int save_cnt;                       /* Flag if state already saved  */
837         struct sk_buff *skb;                /* Save the (re-ordered) skb's  */
838     } cache;
839     struct de4x5_srom srom;                 /* A copy of the SROM           */
840     int cfrv;                               /* Card CFRV copy */
841     int rx_ovf;                             /* Check for 'RX overflow' tag  */
842     int useSROM;                            /* For non-DEC card use SROM    */
843     int useMII;                             /* Infoblock using the MII      */
844     int asBitValid;                         /* Autosense bits in GEP?       */
845     int asPolarity;                         /* 0 => asserted high           */
846     int asBit;                              /* Autosense bit number in GEP  */
847     int defMedium;                          /* SROM default medium          */
848     int tcount;                             /* Last infoblock number        */
849     int infoblock_init;                     /* Initialised this infoblock?  */
850     int infoleaf_offset;                    /* SROM infoleaf for controller */
851     s32 infoblock_csr6;                     /* csr6 value in SROM infoblock */
852     int infoblock_media;                    /* infoblock media              */
853     int (*infoleaf_fn)(struct net_device *);    /* Pointer to infoleaf function */
854     u_char *rst;                            /* Pointer to Type 5 reset info */
855     u_char  ibn;                            /* Infoblock number             */
856     struct parameters params;               /* Command line/ #defined params */
857     struct device *gendev;                  /* Generic device */
858     dma_addr_t dma_rings;                   /* DMA handle for rings         */
859     int dma_size;                           /* Size of the DMA area         */
860     char *rx_bufs;                          /* rx bufs on alpha, sparc, ... */
861 };
862
863 /*
864 ** To get around certain poxy cards that don't provide an SROM
865 ** for the second and more DECchip, I have to key off the first
866 ** chip's address. I'll assume there's not a bad SROM iff:
867 **
868 **      o the chipset is the same
869 **      o the bus number is the same and > 0
870 **      o the sum of all the returned hw address bytes is 0 or 0x5fa
871 **
872 ** Also have to save the irq for those cards whose hardware designers
873 ** can't follow the PCI to PCI Bridge Architecture spec.
874 */
875 static struct {
876     int chipset;
877     int bus;
878     int irq;
879     u_char addr[ETH_ALEN];
880 } last = {0,};
881
882 /*
883 ** The transmit ring full condition is described by the tx_old and tx_new
884 ** pointers by:
885 **    tx_old            = tx_new    Empty ring
886 **    tx_old            = tx_new+1  Full ring
887 **    tx_old+txRingSize = tx_new+1  Full ring  (wrapped condition)
888 */
889 #define TX_BUFFS_AVAIL ((lp->tx_old<=lp->tx_new)?\
890                         lp->tx_old+lp->txRingSize-lp->tx_new-1:\
891                         lp->tx_old               -lp->tx_new-1)
892
893 #define TX_PKT_PENDING (lp->tx_old != lp->tx_new)
894
895 /*
896 ** Public Functions
897 */
898 static int     de4x5_open(struct net_device *dev);
899 static int     de4x5_queue_pkt(struct sk_buff *skb, struct net_device *dev);
900 static irqreturn_t de4x5_interrupt(int irq, void *dev_id, struct pt_regs *regs);
901 static int     de4x5_close(struct net_device *dev);
902 static struct  net_device_stats *de4x5_get_stats(struct net_device *dev);
903 static void    de4x5_local_stats(struct net_device *dev, char *buf, int pkt_len);
904 static void    set_multicast_list(struct net_device *dev);
905 static int     de4x5_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
906
907 /*
908 ** Private functions
909 */
910 static int     de4x5_hw_init(struct net_device *dev, u_long iobase, struct device *gendev);
911 static int     de4x5_init(struct net_device *dev);
912 static int     de4x5_sw_reset(struct net_device *dev);
913 static int     de4x5_rx(struct net_device *dev);
914 static int     de4x5_tx(struct net_device *dev);
915 static int     de4x5_ast(struct net_device *dev);
916 static int     de4x5_txur(struct net_device *dev);
917 static int     de4x5_rx_ovfc(struct net_device *dev);
918
919 static int     autoconf_media(struct net_device *dev);
920 static void    create_packet(struct net_device *dev, char *frame, int len);
921 static void    load_packet(struct net_device *dev, char *buf, u32 flags, struct sk_buff *skb);
922 static int     dc21040_autoconf(struct net_device *dev);
923 static int     dc21041_autoconf(struct net_device *dev);
924 static int     dc21140m_autoconf(struct net_device *dev);
925 static int     dc2114x_autoconf(struct net_device *dev);
926 static int     srom_autoconf(struct net_device *dev);
927 static int     de4x5_suspect_state(struct net_device *dev, int timeout, int prev_state, int (*fn)(struct net_device *, int), int (*asfn)(struct net_device *));
928 static int     dc21040_state(struct net_device *dev, int csr13, int csr14, int csr15, int timeout, int next_state, int suspect_state, int (*fn)(struct net_device *, int));
929 static int     test_media(struct net_device *dev, s32 irqs, s32 irq_mask, s32 csr13, s32 csr14, s32 csr15, s32 msec);
930 static int     test_for_100Mb(struct net_device *dev, int msec);
931 static int     wait_for_link(struct net_device *dev);
932 static int     test_mii_reg(struct net_device *dev, int reg, int mask, int pol, long msec);
933 static int     is_spd_100(struct net_device *dev);
934 static int     is_100_up(struct net_device *dev);
935 static int     is_10_up(struct net_device *dev);
936 static int     is_anc_capable(struct net_device *dev);
937 static int     ping_media(struct net_device *dev, int msec);
938 static struct sk_buff *de4x5_alloc_rx_buff(struct net_device *dev, int index, int len);
939 static void    de4x5_free_rx_buffs(struct net_device *dev);
940 static void    de4x5_free_tx_buffs(struct net_device *dev);
941 static void    de4x5_save_skbs(struct net_device *dev);
942 static void    de4x5_rst_desc_ring(struct net_device *dev);
943 static void    de4x5_cache_state(struct net_device *dev, int flag);
944 static void    de4x5_put_cache(struct net_device *dev, struct sk_buff *skb);
945 static void    de4x5_putb_cache(struct net_device *dev, struct sk_buff *skb);
946 static struct  sk_buff *de4x5_get_cache(struct net_device *dev);
947 static void    de4x5_setup_intr(struct net_device *dev);
948 static void    de4x5_init_connection(struct net_device *dev);
949 static int     de4x5_reset_phy(struct net_device *dev);
950 static void    reset_init_sia(struct net_device *dev, s32 sicr, s32 strr, s32 sigr);
951 static int     test_ans(struct net_device *dev, s32 irqs, s32 irq_mask, s32 msec);
952 static int     test_tp(struct net_device *dev, s32 msec);
953 static int     EISA_signature(char *name, struct device *device);
954 static int     PCI_signature(char *name, struct de4x5_private *lp);
955 static void    DevicePresent(struct net_device *dev, u_long iobase);
956 static void    enet_addr_rst(u_long aprom_addr);
957 static int     de4x5_bad_srom(struct de4x5_private *lp);
958 static short   srom_rd(u_long address, u_char offset);
959 static void    srom_latch(u_int command, u_long address);
960 static void    srom_command(u_int command, u_long address);
961 static void    srom_address(u_int command, u_long address, u_char offset);
962 static short   srom_data(u_int command, u_long address);
963 /*static void    srom_busy(u_int command, u_long address);*/
964 static void    sendto_srom(u_int command, u_long addr);
965 static int     getfrom_srom(u_long addr);
966 static int     srom_map_media(struct net_device *dev);
967 static int     srom_infoleaf_info(struct net_device *dev);
968 static void    srom_init(struct net_device *dev);
969 static void    srom_exec(struct net_device *dev, u_char *p);
970 static int     mii_rd(u_char phyreg, u_char phyaddr, u_long ioaddr);
971 static void    mii_wr(int data, u_char phyreg, u_char phyaddr, u_long ioaddr);
972 static int     mii_rdata(u_long ioaddr);
973 static void    mii_wdata(int data, int len, u_long ioaddr);
974 static void    mii_ta(u_long rw, u_long ioaddr);
975 static int     mii_swap(int data, int len);
976 static void    mii_address(u_char addr, u_long ioaddr);
977 static void    sendto_mii(u32 command, int data, u_long ioaddr);
978 static int     getfrom_mii(u32 command, u_long ioaddr);
979 static int     mii_get_oui(u_char phyaddr, u_long ioaddr);
980 static int     mii_get_phy(struct net_device *dev);
981 static void    SetMulticastFilter(struct net_device *dev);
982 static int     get_hw_addr(struct net_device *dev);
983 static void    srom_repair(struct net_device *dev, int card);
984 static int     test_bad_enet(struct net_device *dev, int status);
985 static int     an_exception(struct de4x5_private *lp);
986 static char    *build_setup_frame(struct net_device *dev, int mode);
987 static void    disable_ast(struct net_device *dev);
988 static void    enable_ast(struct net_device *dev, u32 time_out);
989 static long    de4x5_switch_mac_port(struct net_device *dev);
990 static int     gep_rd(struct net_device *dev);
991 static void    gep_wr(s32 data, struct net_device *dev);
992 static void    timeout(struct net_device *dev, void (*fn)(u_long data), u_long data, u_long msec);
993 static void    yawn(struct net_device *dev, int state);
994 static void    de4x5_parse_params(struct net_device *dev);
995 static void    de4x5_dbg_open(struct net_device *dev);
996 static void    de4x5_dbg_mii(struct net_device *dev, int k);
997 static void    de4x5_dbg_media(struct net_device *dev);
998 static void    de4x5_dbg_srom(struct de4x5_srom *p);
999 static void    de4x5_dbg_rx(struct sk_buff *skb, int len);
1000 static int     de4x5_strncmp(char *a, char *b, int n);
1001 static int     dc21041_infoleaf(struct net_device *dev);
1002 static int     dc21140_infoleaf(struct net_device *dev);
1003 static int     dc21142_infoleaf(struct net_device *dev);
1004 static int     dc21143_infoleaf(struct net_device *dev);
1005 static int     type0_infoblock(struct net_device *dev, u_char count, u_char *p);
1006 static int     type1_infoblock(struct net_device *dev, u_char count, u_char *p);
1007 static int     type2_infoblock(struct net_device *dev, u_char count, u_char *p);
1008 static int     type3_infoblock(struct net_device *dev, u_char count, u_char *p);
1009 static int     type4_infoblock(struct net_device *dev, u_char count, u_char *p);
1010 static int     type5_infoblock(struct net_device *dev, u_char count, u_char *p);
1011 static int     compact_infoblock(struct net_device *dev, u_char count, u_char *p);
1012
1013 /*
1014 ** Note now that module autoprobing is allowed under EISA and PCI. The
1015 ** IRQ lines will not be auto-detected; instead I'll rely on the BIOSes
1016 ** to "do the right thing".
1017 */
1018
1019 static int io=0x0;/* EDIT THIS LINE FOR YOUR CONFIGURATION IF NEEDED        */
1020
1021 module_param(io, int, 0);
1022 module_param(de4x5_debug, int, 0);
1023 module_param(dec_only, int, 0);
1024 module_param(args, charp, 0);
1025
1026 MODULE_PARM_DESC(io, "de4x5 I/O base address");
1027 MODULE_PARM_DESC(de4x5_debug, "de4x5 debug mask");
1028 MODULE_PARM_DESC(dec_only, "de4x5 probe only for Digital boards (0-1)");
1029 MODULE_PARM_DESC(args, "de4x5 full duplex and media type settings; see de4x5.c for details");
1030 MODULE_LICENSE("GPL");
1031
1032 /*
1033 ** List the SROM infoleaf functions and chipsets
1034 */
1035 struct InfoLeaf {
1036     int chipset;
1037     int (*fn)(struct net_device *);
1038 };
1039 static struct InfoLeaf infoleaf_array[] = {
1040     {DC21041, dc21041_infoleaf},
1041     {DC21140, dc21140_infoleaf},
1042     {DC21142, dc21142_infoleaf},
1043     {DC21143, dc21143_infoleaf}
1044 };
1045 #define INFOLEAF_SIZE (sizeof(infoleaf_array)/(sizeof(int)+sizeof(int *)))
1046
1047 /*
1048 ** List the SROM info block functions
1049 */
1050 static int (*dc_infoblock[])(struct net_device *dev, u_char, u_char *) = {
1051     type0_infoblock,
1052     type1_infoblock,
1053     type2_infoblock,
1054     type3_infoblock,
1055     type4_infoblock,
1056     type5_infoblock,
1057     compact_infoblock
1058 };
1059
1060 #define COMPACT (sizeof(dc_infoblock)/sizeof(int *) - 1)
1061
1062 /*
1063 ** Miscellaneous defines...
1064 */
1065 #define RESET_DE4X5 {\
1066     int i;\
1067     i=inl(DE4X5_BMR);\
1068     mdelay(1);\
1069     outl(i | BMR_SWR, DE4X5_BMR);\
1070     mdelay(1);\
1071     outl(i, DE4X5_BMR);\
1072     mdelay(1);\
1073     for (i=0;i<5;i++) {inl(DE4X5_BMR); mdelay(1);}\
1074     mdelay(1);\
1075 }
1076
1077 #define PHY_HARD_RESET {\
1078     outl(GEP_HRST, DE4X5_GEP);           /* Hard RESET the PHY dev. */\
1079     mdelay(1);                           /* Assert for 1ms */\
1080     outl(0x00, DE4X5_GEP);\
1081     mdelay(2);                           /* Wait for 2ms */\
1082 }
1083
1084 \f
1085 static int __devinit 
1086 de4x5_hw_init(struct net_device *dev, u_long iobase, struct device *gendev)
1087 {
1088     char name[DE4X5_NAME_LENGTH + 1];
1089     struct de4x5_private *lp = netdev_priv(dev);
1090     struct pci_dev *pdev = NULL;
1091     int i, status=0;
1092
1093     gendev->driver_data = dev;
1094
1095     /* Ensure we're not sleeping */
1096     if (lp->bus == EISA) {
1097         outb(WAKEUP, PCI_CFPM);
1098     } else {
1099         pdev = to_pci_dev (gendev);
1100         pci_write_config_byte(pdev, PCI_CFDA_PSM, WAKEUP);
1101     }
1102     mdelay(10);
1103
1104     RESET_DE4X5;
1105     
1106     if ((inl(DE4X5_STS) & (STS_TS | STS_RS)) != 0) {
1107         return -ENXIO;                       /* Hardware could not reset */
1108     }
1109     
1110     /* 
1111     ** Now find out what kind of DC21040/DC21041/DC21140 board we have.
1112     */
1113     lp->useSROM = FALSE;
1114     if (lp->bus == PCI) {
1115         PCI_signature(name, lp);
1116     } else {
1117         EISA_signature(name, gendev);
1118     }
1119     
1120     if (*name == '\0') {                     /* Not found a board signature */
1121         return -ENXIO;
1122     }
1123     
1124     dev->base_addr = iobase;
1125     printk ("%s: %s at 0x%04lx", gendev->bus_id, name, iobase);
1126     
1127     printk(", h/w address ");
1128     status = get_hw_addr(dev);
1129     for (i = 0; i < ETH_ALEN - 1; i++) {     /* get the ethernet addr. */
1130         printk("%2.2x:", dev->dev_addr[i]);
1131     }
1132     printk("%2.2x,\n", dev->dev_addr[i]);
1133     
1134     if (status != 0) {
1135         printk("      which has an Ethernet PROM CRC error.\n");
1136         return -ENXIO;
1137     } else {
1138         lp->cache.gepc = GEP_INIT;
1139         lp->asBit = GEP_SLNK;
1140         lp->asPolarity = GEP_SLNK;
1141         lp->asBitValid = TRUE;
1142         lp->timeout = -1;
1143         lp->gendev = gendev;
1144         lp->lock = (spinlock_t) SPIN_LOCK_UNLOCKED;
1145         init_timer(&lp->timer);
1146         de4x5_parse_params(dev);
1147
1148         /*
1149         ** Choose correct autosensing in case someone messed up
1150         */
1151         lp->autosense = lp->params.autosense;
1152         if (lp->chipset != DC21140) {
1153             if ((lp->chipset==DC21040) && (lp->params.autosense&TP_NW)) {
1154                 lp->params.autosense = TP;
1155             }
1156             if ((lp->chipset==DC21041) && (lp->params.autosense&BNC_AUI)) {
1157                 lp->params.autosense = BNC;
1158             }
1159         }
1160         lp->fdx = lp->params.fdx;
1161         sprintf(lp->adapter_name,"%s (%s)", name, gendev->bus_id);
1162
1163         lp->dma_size = (NUM_RX_DESC + NUM_TX_DESC) * sizeof(struct de4x5_desc);
1164 #if defined(__alpha__) || defined(__powerpc__) || defined(__sparc_v9__) || defined(DE4X5_DO_MEMCPY)
1165         lp->dma_size += RX_BUFF_SZ * NUM_RX_DESC + DE4X5_ALIGN;
1166 #endif
1167         lp->rx_ring = dma_alloc_coherent(gendev, lp->dma_size,
1168                                          &lp->dma_rings, GFP_ATOMIC);
1169         if (lp->rx_ring == NULL) {
1170             return -ENOMEM;
1171         }
1172
1173         lp->tx_ring = lp->rx_ring + NUM_RX_DESC;
1174             
1175         /*
1176         ** Set up the RX descriptor ring (Intels)
1177         ** Allocate contiguous receive buffers, long word aligned (Alphas) 
1178         */
1179 #if !defined(__alpha__) && !defined(__powerpc__) && !defined(__sparc_v9__) && !defined(DE4X5_DO_MEMCPY)
1180         for (i=0; i<NUM_RX_DESC; i++) {
1181             lp->rx_ring[i].status = 0;
1182             lp->rx_ring[i].des1 = cpu_to_le32(RX_BUFF_SZ);
1183             lp->rx_ring[i].buf = 0;
1184             lp->rx_ring[i].next = 0;
1185             lp->rx_skb[i] = (struct sk_buff *) 1;     /* Dummy entry */
1186         }
1187
1188 #else
1189         {
1190                 dma_addr_t dma_rx_bufs;
1191
1192                 dma_rx_bufs = lp->dma_rings + (NUM_RX_DESC + NUM_TX_DESC)
1193                         * sizeof(struct de4x5_desc);
1194                 dma_rx_bufs = (dma_rx_bufs + DE4X5_ALIGN) & ~DE4X5_ALIGN;
1195                 lp->rx_bufs = (char *)(((long)(lp->rx_ring + NUM_RX_DESC
1196                         + NUM_TX_DESC) + DE4X5_ALIGN) & ~DE4X5_ALIGN);
1197                 for (i=0; i<NUM_RX_DESC; i++) {
1198                         lp->rx_ring[i].status = 0;
1199                         lp->rx_ring[i].des1 = cpu_to_le32(RX_BUFF_SZ);
1200                         lp->rx_ring[i].buf =
1201                                 cpu_to_le32(dma_rx_bufs+i*RX_BUFF_SZ);
1202                         lp->rx_ring[i].next = 0;
1203                         lp->rx_skb[i] = (struct sk_buff *) 1; /* Dummy entry */
1204                 }
1205
1206         }
1207 #endif
1208
1209         barrier();
1210
1211         lp->rxRingSize = NUM_RX_DESC;
1212         lp->txRingSize = NUM_TX_DESC;
1213             
1214         /* Write the end of list marker to the descriptor lists */
1215         lp->rx_ring[lp->rxRingSize - 1].des1 |= cpu_to_le32(RD_RER);
1216         lp->tx_ring[lp->txRingSize - 1].des1 |= cpu_to_le32(TD_TER);
1217
1218         /* Tell the adapter where the TX/RX rings are located. */
1219         outl(lp->dma_rings, DE4X5_RRBA);
1220         outl(lp->dma_rings + NUM_RX_DESC * sizeof(struct de4x5_desc),
1221              DE4X5_TRBA);
1222             
1223         /* Initialise the IRQ mask and Enable/Disable */
1224         lp->irq_mask = IMR_RIM | IMR_TIM | IMR_TUM | IMR_UNM;
1225         lp->irq_en   = IMR_NIM | IMR_AIM;
1226
1227         /* Create a loopback packet frame for later media probing */
1228         create_packet(dev, lp->frame, sizeof(lp->frame));
1229
1230         /* Check if the RX overflow bug needs testing for */
1231         i = lp->cfrv & 0x000000fe;
1232         if ((lp->chipset == DC21140) && (i == 0x20)) {
1233             lp->rx_ovf = 1;
1234         }
1235
1236         /* Initialise the SROM pointers if possible */
1237         if (lp->useSROM) {
1238             lp->state = INITIALISED;
1239             if (srom_infoleaf_info(dev)) {
1240                 dma_free_coherent (gendev, lp->dma_size,
1241                                lp->rx_ring, lp->dma_rings);
1242                 return -ENXIO;
1243             }
1244             srom_init(dev);
1245         }
1246
1247         lp->state = CLOSED;
1248
1249         /*
1250         ** Check for an MII interface
1251         */
1252         if ((lp->chipset != DC21040) && (lp->chipset != DC21041)) {
1253             mii_get_phy(dev);
1254         }
1255         
1256 #ifndef __sparc_v9__
1257         printk("      and requires IRQ%d (provided by %s).\n", dev->irq,
1258 #else
1259         printk("      and requires IRQ%x (provided by %s).\n", dev->irq,
1260 #endif
1261                ((lp->bus == PCI) ? "PCI BIOS" : "EISA CNFG"));
1262     }
1263     
1264     if (de4x5_debug & DEBUG_VERSION) {
1265         printk(version);
1266     }
1267     
1268     /* The DE4X5-specific entries in the device structure. */
1269     SET_MODULE_OWNER(dev);
1270     SET_NETDEV_DEV(dev, gendev);
1271     dev->open = &de4x5_open;
1272     dev->hard_start_xmit = &de4x5_queue_pkt;
1273     dev->stop = &de4x5_close;
1274     dev->get_stats = &de4x5_get_stats;
1275     dev->set_multicast_list = &set_multicast_list;
1276     dev->do_ioctl = &de4x5_ioctl;
1277     
1278     dev->mem_start = 0;
1279     
1280     /* Fill in the generic fields of the device structure. */
1281     if ((status = register_netdev (dev))) {
1282             dma_free_coherent (gendev, lp->dma_size,
1283                                lp->rx_ring, lp->dma_rings);
1284             return status;
1285     }
1286     
1287     /* Let the adapter sleep to save power */
1288     yawn(dev, SLEEP);
1289     
1290     return status;
1291 }
1292
1293 \f
1294 static int
1295 de4x5_open(struct net_device *dev)
1296 {
1297     struct de4x5_private *lp = netdev_priv(dev);
1298     u_long iobase = dev->base_addr;
1299     int i, status = 0;
1300     s32 omr;
1301
1302     /* Allocate the RX buffers */
1303     for (i=0; i<lp->rxRingSize; i++) {
1304         if (de4x5_alloc_rx_buff(dev, i, 0) == NULL) {
1305             de4x5_free_rx_buffs(dev);
1306             return -EAGAIN;
1307         }
1308     }
1309
1310     /*
1311     ** Wake up the adapter
1312     */
1313     yawn(dev, WAKEUP);
1314
1315     /* 
1316     ** Re-initialize the DE4X5... 
1317     */
1318     status = de4x5_init(dev);
1319     lp->lock = (spinlock_t) SPIN_LOCK_UNLOCKED;
1320     lp->state = OPEN;
1321     de4x5_dbg_open(dev);
1322     
1323     if (request_irq(dev->irq, (void *)de4x5_interrupt, SA_SHIRQ, 
1324                                                      lp->adapter_name, dev)) {
1325         printk("de4x5_open(): Requested IRQ%d is busy - attemping FAST/SHARE...", dev->irq);
1326         if (request_irq(dev->irq, de4x5_interrupt, SA_INTERRUPT | SA_SHIRQ,
1327                                                      lp->adapter_name, dev)) {
1328             printk("\n              Cannot get IRQ- reconfigure your hardware.\n");
1329             disable_ast(dev);
1330             de4x5_free_rx_buffs(dev);
1331             de4x5_free_tx_buffs(dev);
1332             yawn(dev, SLEEP);
1333             lp->state = CLOSED;
1334             return -EAGAIN;
1335         } else {
1336             printk("\n              Succeeded, but you should reconfigure your hardware to avoid this.\n");
1337             printk("WARNING: there may be IRQ related problems in heavily loaded systems.\n");
1338         }
1339     }
1340
1341     lp->interrupt = UNMASK_INTERRUPTS;
1342     dev->trans_start = jiffies;
1343     
1344     START_DE4X5;
1345         
1346     de4x5_setup_intr(dev);
1347     
1348     if (de4x5_debug & DEBUG_OPEN) {
1349         printk("\tsts:  0x%08x\n", inl(DE4X5_STS));
1350         printk("\tbmr:  0x%08x\n", inl(DE4X5_BMR));
1351         printk("\timr:  0x%08x\n", inl(DE4X5_IMR));
1352         printk("\tomr:  0x%08x\n", inl(DE4X5_OMR));
1353         printk("\tsisr: 0x%08x\n", inl(DE4X5_SISR));
1354         printk("\tsicr: 0x%08x\n", inl(DE4X5_SICR));
1355         printk("\tstrr: 0x%08x\n", inl(DE4X5_STRR));
1356         printk("\tsigr: 0x%08x\n", inl(DE4X5_SIGR));
1357     }
1358     
1359     return status;
1360 }
1361
1362 /*
1363 ** Initialize the DE4X5 operating conditions. NB: a chip problem with the
1364 ** DC21140 requires using perfect filtering mode for that chip. Since I can't
1365 ** see why I'd want > 14 multicast addresses, I have changed all chips to use
1366 ** the perfect filtering mode. Keep the DMA burst length at 8: there seems
1367 ** to be data corruption problems if it is larger (UDP errors seen from a
1368 ** ttcp source).
1369 */
1370 static int
1371 de4x5_init(struct net_device *dev)
1372 {  
1373     /* Lock out other processes whilst setting up the hardware */
1374     netif_stop_queue(dev);
1375     
1376     de4x5_sw_reset(dev);
1377     
1378     /* Autoconfigure the connected port */
1379     autoconf_media(dev);
1380     
1381     return 0;
1382 }
1383
1384 static int
1385 de4x5_sw_reset(struct net_device *dev)
1386 {
1387     struct de4x5_private *lp = netdev_priv(dev);
1388     u_long iobase = dev->base_addr;
1389     int i, j, status = 0;
1390     s32 bmr, omr;
1391     
1392     /* Select the MII or SRL port now and RESET the MAC */
1393     if (!lp->useSROM) {
1394         if (lp->phy[lp->active].id != 0) {
1395             lp->infoblock_csr6 = OMR_SDP | OMR_PS | OMR_HBD;
1396         } else {
1397             lp->infoblock_csr6 = OMR_SDP | OMR_TTM;
1398         }
1399         de4x5_switch_mac_port(dev);
1400     }
1401
1402     /* 
1403     ** Set the programmable burst length to 8 longwords for all the DC21140
1404     ** Fasternet chips and 4 longwords for all others: DMA errors result
1405     ** without these values. Cache align 16 long.
1406     */
1407     bmr = (lp->chipset==DC21140 ? PBL_8 : PBL_4) | DESC_SKIP_LEN | DE4X5_CACHE_ALIGN;
1408     bmr |= ((lp->chipset & ~0x00ff)==DC2114x ? BMR_RML : 0);
1409     outl(bmr, DE4X5_BMR);
1410
1411     omr = inl(DE4X5_OMR) & ~OMR_PR;             /* Turn off promiscuous mode */
1412     if (lp->chipset == DC21140) {
1413         omr |= (OMR_SDP | OMR_SB);
1414     }
1415     lp->setup_f = PERFECT;
1416     outl(lp->dma_rings, DE4X5_RRBA);
1417     outl(lp->dma_rings + NUM_RX_DESC * sizeof(struct de4x5_desc),
1418          DE4X5_TRBA);
1419     
1420     lp->rx_new = lp->rx_old = 0;
1421     lp->tx_new = lp->tx_old = 0;
1422     
1423     for (i = 0; i < lp->rxRingSize; i++) {
1424         lp->rx_ring[i].status = cpu_to_le32(R_OWN);
1425     }
1426     
1427     for (i = 0; i < lp->txRingSize; i++) {
1428         lp->tx_ring[i].status = cpu_to_le32(0);
1429     }
1430     
1431     barrier();
1432
1433     /* Build the setup frame depending on filtering mode */
1434     SetMulticastFilter(dev);
1435     
1436     load_packet(dev, lp->setup_frame, PERFECT_F|TD_SET|SETUP_FRAME_LEN, (struct sk_buff *)1);
1437     outl(omr|OMR_ST, DE4X5_OMR);
1438
1439     /* Poll for setup frame completion (adapter interrupts are disabled now) */
1440
1441     for (j=0, i=0;(i<500) && (j==0);i++) {       /* Upto 500ms delay */
1442         mdelay(1);
1443         if ((s32)le32_to_cpu(lp->tx_ring[lp->tx_new].status) >= 0) j=1;
1444     }
1445     outl(omr, DE4X5_OMR);                        /* Stop everything! */
1446
1447     if (j == 0) {
1448         printk("%s: Setup frame timed out, status %08x\n", dev->name, 
1449                inl(DE4X5_STS));
1450         status = -EIO;
1451     }
1452     
1453     lp->tx_new = (++lp->tx_new) % lp->txRingSize;
1454     lp->tx_old = lp->tx_new;
1455
1456     return status;
1457 }
1458
1459 /* 
1460 ** Writes a socket buffer address to the next available transmit descriptor.
1461 */
1462 static int
1463 de4x5_queue_pkt(struct sk_buff *skb, struct net_device *dev)
1464 {
1465     struct de4x5_private *lp = netdev_priv(dev);
1466     u_long iobase = dev->base_addr;
1467     int status = 0;
1468     u_long flags = 0;
1469
1470     netif_stop_queue(dev);
1471     if (lp->tx_enable == NO) {                   /* Cannot send for now */
1472         return -1;                                
1473     }
1474     
1475     /*
1476     ** Clean out the TX ring asynchronously to interrupts - sometimes the
1477     ** interrupts are lost by delayed descriptor status updates relative to
1478     ** the irq assertion, especially with a busy PCI bus.
1479     */
1480     spin_lock_irqsave(&lp->lock, flags);
1481     de4x5_tx(dev);
1482     spin_unlock_irqrestore(&lp->lock, flags);
1483
1484     /* Test if cache is already locked - requeue skb if so */
1485     if (test_and_set_bit(0, (void *)&lp->cache.lock) && !lp->interrupt) 
1486         return -1;
1487
1488     /* Transmit descriptor ring full or stale skb */
1489     if (netif_queue_stopped(dev) || (u_long) lp->tx_skb[lp->tx_new] > 1) {
1490         if (lp->interrupt) {
1491             de4x5_putb_cache(dev, skb);          /* Requeue the buffer */
1492         } else {
1493             de4x5_put_cache(dev, skb);
1494         }
1495         if (de4x5_debug & DEBUG_TX) {
1496             printk("%s: transmit busy, lost media or stale skb found:\n  STS:%08x\n  tbusy:%d\n  IMR:%08x\n  OMR:%08x\n Stale skb: %s\n",dev->name, inl(DE4X5_STS), netif_queue_stopped(dev), inl(DE4X5_IMR), inl(DE4X5_OMR), ((u_long) lp->tx_skb[lp->tx_new] > 1) ? "YES" : "NO");
1497         }
1498     } else if (skb->len > 0) {
1499         /* If we already have stuff queued locally, use that first */
1500         if (lp->cache.skb && !lp->interrupt) {
1501             de4x5_put_cache(dev, skb);
1502             skb = de4x5_get_cache(dev);
1503         }
1504
1505         while (skb && !netif_queue_stopped(dev) &&
1506                (u_long) lp->tx_skb[lp->tx_new] <= 1) {
1507             spin_lock_irqsave(&lp->lock, flags);
1508             netif_stop_queue(dev);
1509             load_packet(dev, skb->data, TD_IC | TD_LS | TD_FS | skb->len, skb);
1510             lp->stats.tx_bytes += skb->len;
1511             outl(POLL_DEMAND, DE4X5_TPD);/* Start the TX */
1512                 
1513             lp->tx_new = (++lp->tx_new) % lp->txRingSize;
1514             dev->trans_start = jiffies;
1515                     
1516             if (TX_BUFFS_AVAIL) {
1517                 netif_start_queue(dev);         /* Another pkt may be queued */
1518             }
1519             skb = de4x5_get_cache(dev);
1520             spin_unlock_irqrestore(&lp->lock, flags);
1521         }
1522         if (skb) de4x5_putb_cache(dev, skb);
1523     }
1524     
1525     lp->cache.lock = 0;
1526
1527     return status;
1528 }
1529
1530 /*
1531 ** The DE4X5 interrupt handler. 
1532 ** 
1533 ** I/O Read/Writes through intermediate PCI bridges are never 'posted',
1534 ** so that the asserted interrupt always has some real data to work with -
1535 ** if these I/O accesses are ever changed to memory accesses, ensure the
1536 ** STS write is read immediately to complete the transaction if the adapter
1537 ** is not on bus 0. Lost interrupts can still occur when the PCI bus load
1538 ** is high and descriptor status bits cannot be set before the associated
1539 ** interrupt is asserted and this routine entered.
1540 */
1541 static irqreturn_t
1542 de4x5_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1543 {
1544     struct net_device *dev = (struct net_device *)dev_id;
1545     struct de4x5_private *lp;
1546     s32 imr, omr, sts, limit;
1547     u_long iobase;
1548     unsigned int handled = 0;
1549     
1550     if (dev == NULL) {
1551         printk ("de4x5_interrupt(): irq %d for unknown device.\n", irq);
1552         return IRQ_NONE;
1553     }
1554     lp = netdev_priv(dev);
1555     spin_lock(&lp->lock);
1556     iobase = dev->base_addr;
1557         
1558     DISABLE_IRQs;                        /* Ensure non re-entrancy */
1559
1560     if (test_and_set_bit(MASK_INTERRUPTS, (void*) &lp->interrupt))
1561         printk("%s: Re-entering the interrupt handler.\n", dev->name);
1562
1563     synchronize_irq(dev->irq);
1564         
1565     for (limit=0; limit<8; limit++) {
1566         sts = inl(DE4X5_STS);            /* Read IRQ status */
1567         outl(sts, DE4X5_STS);            /* Reset the board interrupts */
1568             
1569         if (!(sts & lp->irq_mask)) break;/* All done */
1570         handled = 1;
1571             
1572         if (sts & (STS_RI | STS_RU))     /* Rx interrupt (packet[s] arrived) */
1573           de4x5_rx(dev);
1574             
1575         if (sts & (STS_TI | STS_TU))     /* Tx interrupt (packet sent) */
1576           de4x5_tx(dev); 
1577             
1578         if (sts & STS_LNF) {             /* TP Link has failed */
1579             lp->irq_mask &= ~IMR_LFM;
1580         }
1581             
1582         if (sts & STS_UNF) {             /* Transmit underrun */
1583             de4x5_txur(dev);
1584         }
1585             
1586         if (sts & STS_SE) {              /* Bus Error */
1587             STOP_DE4X5;
1588             printk("%s: Fatal bus error occurred, sts=%#8x, device stopped.\n",
1589                    dev->name, sts);
1590             spin_unlock(&lp->lock);
1591             return IRQ_HANDLED;
1592         }
1593     }
1594
1595     /* Load the TX ring with any locally stored packets */
1596     if (!test_and_set_bit(0, (void *)&lp->cache.lock)) {
1597         while (lp->cache.skb && !netif_queue_stopped(dev) && lp->tx_enable) {
1598             de4x5_queue_pkt(de4x5_get_cache(dev), dev);
1599         }
1600         lp->cache.lock = 0;
1601     }
1602
1603     lp->interrupt = UNMASK_INTERRUPTS;
1604     ENABLE_IRQs;
1605     spin_unlock(&lp->lock);
1606     
1607     return IRQ_RETVAL(handled);
1608 }
1609
1610 static int
1611 de4x5_rx(struct net_device *dev)
1612 {
1613     struct de4x5_private *lp = netdev_priv(dev);
1614     u_long iobase = dev->base_addr;
1615     int entry;
1616     s32 status;
1617     
1618     for (entry=lp->rx_new; (s32)le32_to_cpu(lp->rx_ring[entry].status)>=0;
1619                                                             entry=lp->rx_new) {
1620         status = (s32)le32_to_cpu(lp->rx_ring[entry].status);
1621         
1622         if (lp->rx_ovf) {
1623             if (inl(DE4X5_MFC) & MFC_FOCM) {
1624                 de4x5_rx_ovfc(dev);
1625                 break;
1626             }
1627         }
1628
1629         if (status & RD_FS) {                 /* Remember the start of frame */
1630             lp->rx_old = entry;
1631         }
1632         
1633         if (status & RD_LS) {                 /* Valid frame status */
1634             if (lp->tx_enable) lp->linkOK++;
1635             if (status & RD_ES) {             /* There was an error. */
1636                 lp->stats.rx_errors++;        /* Update the error stats. */
1637                 if (status & (RD_RF | RD_TL)) lp->stats.rx_frame_errors++;
1638                 if (status & RD_CE)           lp->stats.rx_crc_errors++;
1639                 if (status & RD_OF)           lp->stats.rx_fifo_errors++;
1640                 if (status & RD_TL)           lp->stats.rx_length_errors++;
1641                 if (status & RD_RF)           lp->pktStats.rx_runt_frames++;
1642                 if (status & RD_CS)           lp->pktStats.rx_collision++;
1643                 if (status & RD_DB)           lp->pktStats.rx_dribble++;
1644                 if (status & RD_OF)           lp->pktStats.rx_overflow++;
1645             } else {                          /* A valid frame received */
1646                 struct sk_buff *skb;
1647                 short pkt_len = (short)(le32_to_cpu(lp->rx_ring[entry].status)
1648                                                                     >> 16) - 4;
1649                 
1650                 if ((skb = de4x5_alloc_rx_buff(dev, entry, pkt_len)) == NULL) {
1651                     printk("%s: Insufficient memory; nuking packet.\n", 
1652                                                                     dev->name);
1653                     lp->stats.rx_dropped++;
1654                 } else {
1655                     de4x5_dbg_rx(skb, pkt_len);
1656
1657                     /* Push up the protocol stack */
1658                     skb->protocol=eth_type_trans(skb,dev);
1659                     de4x5_local_stats(dev, skb->data, pkt_len);
1660                     netif_rx(skb);
1661                     
1662                     /* Update stats */
1663                     dev->last_rx = jiffies;
1664                     lp->stats.rx_packets++;
1665                     lp->stats.rx_bytes += pkt_len;
1666                 }
1667             }
1668             
1669             /* Change buffer ownership for this frame, back to the adapter */
1670             for (;lp->rx_old!=entry;lp->rx_old=(++lp->rx_old)%lp->rxRingSize) {
1671                 lp->rx_ring[lp->rx_old].status = cpu_to_le32(R_OWN);
1672                 barrier();
1673             }
1674             lp->rx_ring[entry].status = cpu_to_le32(R_OWN);
1675             barrier();
1676         }
1677         
1678         /*
1679         ** Update entry information
1680         */
1681         lp->rx_new = (++lp->rx_new) % lp->rxRingSize;
1682     }
1683     
1684     return 0;
1685 }
1686
1687 static inline void
1688 de4x5_free_tx_buff(struct de4x5_private *lp, int entry)
1689 {
1690     dma_unmap_single(lp->gendev, le32_to_cpu(lp->tx_ring[entry].buf),
1691                      le32_to_cpu(lp->tx_ring[entry].des1) & TD_TBS1,
1692                      DMA_TO_DEVICE);
1693     if ((u_long) lp->tx_skb[entry] > 1)
1694         dev_kfree_skb_irq(lp->tx_skb[entry]);
1695     lp->tx_skb[entry] = NULL;
1696 }
1697
1698 /*
1699 ** Buffer sent - check for TX buffer errors.
1700 */
1701 static int
1702 de4x5_tx(struct net_device *dev)
1703 {
1704     struct de4x5_private *lp = netdev_priv(dev);
1705     u_long iobase = dev->base_addr;
1706     int entry;
1707     s32 status;
1708     
1709     for (entry = lp->tx_old; entry != lp->tx_new; entry = lp->tx_old) {
1710         status = (s32)le32_to_cpu(lp->tx_ring[entry].status);
1711         if (status < 0) {                     /* Buffer not sent yet */
1712             break;
1713         } else if (status != 0x7fffffff) {    /* Not setup frame */
1714             if (status & TD_ES) {             /* An error happened */
1715                 lp->stats.tx_errors++; 
1716                 if (status & TD_NC) lp->stats.tx_carrier_errors++;
1717                 if (status & TD_LC) lp->stats.tx_window_errors++;
1718                 if (status & TD_UF) lp->stats.tx_fifo_errors++;
1719                 if (status & TD_EC) lp->pktStats.excessive_collisions++;
1720                 if (status & TD_DE) lp->stats.tx_aborted_errors++;
1721             
1722                 if (TX_PKT_PENDING) {
1723                     outl(POLL_DEMAND, DE4X5_TPD);/* Restart a stalled TX */
1724                 }
1725             } else {                      /* Packet sent */
1726                 lp->stats.tx_packets++;
1727                 if (lp->tx_enable) lp->linkOK++;
1728             }
1729             /* Update the collision counter */
1730             lp->stats.collisions += ((status & TD_EC) ? 16 : 
1731                                                       ((status & TD_CC) >> 3));
1732
1733             /* Free the buffer. */
1734             if (lp->tx_skb[entry] != NULL)
1735                 de4x5_free_tx_buff(lp, entry);
1736         }
1737         
1738         /* Update all the pointers */
1739         lp->tx_old = (++lp->tx_old) % lp->txRingSize;
1740     }
1741
1742     /* Any resources available? */
1743     if (TX_BUFFS_AVAIL && netif_queue_stopped(dev)) {
1744         if (lp->interrupt)
1745             netif_wake_queue(dev);
1746         else
1747             netif_start_queue(dev);
1748     }
1749         
1750     return 0;
1751 }
1752
1753 static int
1754 de4x5_ast(struct net_device *dev)
1755 {
1756     struct de4x5_private *lp = netdev_priv(dev);
1757     int next_tick = DE4X5_AUTOSENSE_MS;
1758     
1759     disable_ast(dev);
1760     
1761     if (lp->useSROM) {
1762         next_tick = srom_autoconf(dev);
1763     } else if (lp->chipset == DC21140) {
1764         next_tick = dc21140m_autoconf(dev);
1765     } else if (lp->chipset == DC21041) {
1766         next_tick = dc21041_autoconf(dev);
1767     } else if (lp->chipset == DC21040) {
1768         next_tick = dc21040_autoconf(dev);
1769     }
1770     lp->linkOK = 0;
1771     enable_ast(dev, next_tick);
1772     
1773     return 0;
1774 }
1775
1776 static int
1777 de4x5_txur(struct net_device *dev)
1778 {
1779     struct de4x5_private *lp = netdev_priv(dev);
1780     u_long iobase = dev->base_addr;
1781     int omr;
1782
1783     omr = inl(DE4X5_OMR);
1784     if (!(omr & OMR_SF) || (lp->chipset==DC21041) || (lp->chipset==DC21040)) {
1785         omr &= ~(OMR_ST|OMR_SR);
1786         outl(omr, DE4X5_OMR);
1787         while (inl(DE4X5_STS) & STS_TS);
1788         if ((omr & OMR_TR) < OMR_TR) {
1789             omr += 0x4000;
1790         } else {
1791             omr |= OMR_SF;
1792         }
1793         outl(omr | OMR_ST | OMR_SR, DE4X5_OMR);
1794     }
1795     
1796     return 0;
1797 }
1798
1799 static int 
1800 de4x5_rx_ovfc(struct net_device *dev)
1801 {
1802     struct de4x5_private *lp = netdev_priv(dev);
1803     u_long iobase = dev->base_addr;
1804     int omr;
1805
1806     omr = inl(DE4X5_OMR);
1807     outl(omr & ~OMR_SR, DE4X5_OMR);
1808     while (inl(DE4X5_STS) & STS_RS);
1809
1810     for (; (s32)le32_to_cpu(lp->rx_ring[lp->rx_new].status)>=0;) {
1811         lp->rx_ring[lp->rx_new].status = cpu_to_le32(R_OWN);
1812         lp->rx_new = (++lp->rx_new % lp->rxRingSize);
1813     }
1814
1815     outl(omr, DE4X5_OMR);
1816     
1817     return 0;
1818 }
1819
1820 static int
1821 de4x5_close(struct net_device *dev)
1822 {
1823     struct de4x5_private *lp = netdev_priv(dev);
1824     u_long iobase = dev->base_addr;
1825     s32 imr, omr;
1826     
1827     disable_ast(dev);
1828
1829     netif_stop_queue(dev);
1830     
1831     if (de4x5_debug & DEBUG_CLOSE) {
1832         printk("%s: Shutting down ethercard, status was %8.8x.\n",
1833                dev->name, inl(DE4X5_STS));
1834     }
1835     
1836     /* 
1837     ** We stop the DE4X5 here... mask interrupts and stop TX & RX
1838     */
1839     DISABLE_IRQs;
1840     STOP_DE4X5;
1841     
1842     /* Free the associated irq */
1843     free_irq(dev->irq, dev);
1844     lp->state = CLOSED;
1845
1846     /* Free any socket buffers */
1847     de4x5_free_rx_buffs(dev);
1848     de4x5_free_tx_buffs(dev);
1849     
1850     /* Put the adapter to sleep to save power */
1851     yawn(dev, SLEEP);
1852     
1853     return 0;
1854 }
1855
1856 static struct net_device_stats *
1857 de4x5_get_stats(struct net_device *dev)
1858 {
1859     struct de4x5_private *lp = netdev_priv(dev);
1860     u_long iobase = dev->base_addr;
1861     
1862     lp->stats.rx_missed_errors = (int)(inl(DE4X5_MFC) & (MFC_OVFL | MFC_CNTR));
1863     
1864     return &lp->stats;
1865 }
1866
1867 static void
1868 de4x5_local_stats(struct net_device *dev, char *buf, int pkt_len)
1869 {
1870     struct de4x5_private *lp = netdev_priv(dev);
1871     int i;
1872
1873     for (i=1; i<DE4X5_PKT_STAT_SZ-1; i++) {
1874         if (pkt_len < (i*DE4X5_PKT_BIN_SZ)) {
1875             lp->pktStats.bins[i]++;
1876             i = DE4X5_PKT_STAT_SZ;
1877         }
1878     }
1879     if (buf[0] & 0x01) {          /* Multicast/Broadcast */
1880         if ((*(s32 *)&buf[0] == -1) && (*(s16 *)&buf[4] == -1)) {
1881             lp->pktStats.broadcast++;
1882         } else {
1883             lp->pktStats.multicast++;
1884         }
1885     } else if ((*(s32 *)&buf[0] == *(s32 *)&dev->dev_addr[0]) &&
1886                (*(s16 *)&buf[4] == *(s16 *)&dev->dev_addr[4])) {
1887         lp->pktStats.unicast++;
1888     }
1889                 
1890     lp->pktStats.bins[0]++;       /* Duplicates stats.rx_packets */
1891     if (lp->pktStats.bins[0] == 0) { /* Reset counters */
1892         memset((char *)&lp->pktStats, 0, sizeof(lp->pktStats));
1893     }
1894
1895     return;
1896 }
1897
1898 /*
1899 ** Removes the TD_IC flag from previous descriptor to improve TX performance.
1900 ** If the flag is changed on a descriptor that is being read by the hardware,
1901 ** I assume PCI transaction ordering will mean you are either successful or
1902 ** just miss asserting the change to the hardware. Anyway you're messing with
1903 ** a descriptor you don't own, but this shouldn't kill the chip provided
1904 ** the descriptor register is read only to the hardware.
1905 */
1906 static void
1907 load_packet(struct net_device *dev, char *buf, u32 flags, struct sk_buff *skb)
1908 {
1909     struct de4x5_private *lp = netdev_priv(dev);
1910     int entry = (lp->tx_new ? lp->tx_new-1 : lp->txRingSize-1);
1911     dma_addr_t buf_dma = dma_map_single(lp->gendev, buf, flags & TD_TBS1, DMA_TO_DEVICE);
1912
1913     lp->tx_ring[lp->tx_new].buf = cpu_to_le32(buf_dma);
1914     lp->tx_ring[lp->tx_new].des1 &= cpu_to_le32(TD_TER);
1915     lp->tx_ring[lp->tx_new].des1 |= cpu_to_le32(flags);
1916     lp->tx_skb[lp->tx_new] = skb;
1917     lp->tx_ring[entry].des1 &= cpu_to_le32(~TD_IC);
1918     barrier();
1919
1920     lp->tx_ring[lp->tx_new].status = cpu_to_le32(T_OWN);
1921     barrier();
1922 }
1923
1924 /*
1925 ** Set or clear the multicast filter for this adaptor.
1926 */
1927 static void
1928 set_multicast_list(struct net_device *dev)
1929 {
1930     struct de4x5_private *lp = netdev_priv(dev);
1931     u_long iobase = dev->base_addr;
1932
1933     /* First, double check that the adapter is open */
1934     if (lp->state == OPEN) {
1935         if (dev->flags & IFF_PROMISC) {         /* set promiscuous mode */
1936             u32 omr;
1937             omr = inl(DE4X5_OMR);
1938             omr |= OMR_PR;
1939             outl(omr, DE4X5_OMR);
1940         } else { 
1941             SetMulticastFilter(dev);
1942             load_packet(dev, lp->setup_frame, TD_IC | PERFECT_F | TD_SET | 
1943                                                         SETUP_FRAME_LEN, (struct sk_buff *)1);
1944             
1945             lp->tx_new = (++lp->tx_new) % lp->txRingSize;
1946             outl(POLL_DEMAND, DE4X5_TPD);       /* Start the TX */
1947             dev->trans_start = jiffies;
1948         }
1949     }
1950 }
1951
1952 /*
1953 ** Calculate the hash code and update the logical address filter
1954 ** from a list of ethernet multicast addresses.
1955 ** Little endian crc one liner from Matt Thomas, DEC.
1956 */
1957 static void
1958 SetMulticastFilter(struct net_device *dev)
1959 {
1960     struct de4x5_private *lp = netdev_priv(dev);
1961     struct dev_mc_list *dmi=dev->mc_list;
1962     u_long iobase = dev->base_addr;
1963     int i, j, bit, byte;
1964     u16 hashcode;
1965     u32 omr, crc;
1966     char *pa;
1967     unsigned char *addrs;
1968
1969     omr = inl(DE4X5_OMR);
1970     omr &= ~(OMR_PR | OMR_PM);
1971     pa = build_setup_frame(dev, ALL);        /* Build the basic frame */
1972     
1973     if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 14)) {
1974         omr |= OMR_PM;                       /* Pass all multicasts */
1975     } else if (lp->setup_f == HASH_PERF) {   /* Hash Filtering */
1976         for (i=0;i<dev->mc_count;i++) {      /* for each address in the list */
1977             addrs=dmi->dmi_addr;
1978             dmi=dmi->next;
1979             if ((*addrs & 0x01) == 1) {      /* multicast address? */ 
1980                 crc = ether_crc_le(ETH_ALEN, addrs);
1981                 hashcode = crc & HASH_BITS;  /* hashcode is 9 LSb of CRC */
1982                 
1983                 byte = hashcode >> 3;        /* bit[3-8] -> byte in filter */
1984                 bit = 1 << (hashcode & 0x07);/* bit[0-2] -> bit in byte */
1985                 
1986                 byte <<= 1;                  /* calc offset into setup frame */
1987                 if (byte & 0x02) {
1988                     byte -= 1;
1989                 }
1990                 lp->setup_frame[byte] |= bit;
1991             }
1992         }
1993     } else {                                 /* Perfect filtering */
1994         for (j=0; j<dev->mc_count; j++) {
1995             addrs=dmi->dmi_addr;
1996             dmi=dmi->next;
1997             for (i=0; i<ETH_ALEN; i++) { 
1998                 *(pa + (i&1)) = *addrs++;
1999                 if (i & 0x01) pa += 4;
2000             }
2001         }
2002     }
2003     outl(omr, DE4X5_OMR);
2004     
2005     return;
2006 }
2007
2008 #ifdef CONFIG_EISA
2009
2010 static u_char de4x5_irq[] = EISA_ALLOWED_IRQ_LIST;
2011
2012 static int __init de4x5_eisa_probe (struct device *gendev)
2013 {
2014         struct eisa_device *edev;
2015         u_long iobase;
2016         u_char irq, regval;
2017         u_short vendor;
2018         u32 cfid;
2019         int status, device;
2020         struct net_device *dev;
2021         struct de4x5_private *lp;
2022
2023         edev = to_eisa_device (gendev);
2024         iobase = edev->base_addr;
2025
2026         if (!request_region (iobase, DE4X5_EISA_TOTAL_SIZE, "de4x5"))
2027                 return -EBUSY;
2028
2029         if (!request_region (iobase + DE4X5_EISA_IO_PORTS,
2030                              DE4X5_EISA_TOTAL_SIZE, "de4x5")) {
2031                 status = -EBUSY;
2032                 goto release_reg_1;
2033         }
2034         
2035         if (!(dev = alloc_etherdev (sizeof (struct de4x5_private)))) {
2036                 status = -ENOMEM;
2037                 goto release_reg_2;
2038         }
2039         lp = netdev_priv(dev);
2040         
2041         cfid = (u32) inl(PCI_CFID);
2042         lp->cfrv = (u_short) inl(PCI_CFRV);
2043         device = (cfid >> 8) & 0x00ffff00;
2044         vendor = (u_short) cfid;
2045             
2046         /* Read the EISA Configuration Registers */
2047         regval = inb(EISA_REG0) & (ER0_INTL | ER0_INTT);
2048 #ifdef CONFIG_ALPHA
2049         /* Looks like the Jensen firmware (rev 2.2) doesn't really
2050          * care about the EISA configuration, and thus doesn't
2051          * configure the PLX bridge properly. Oh well... Simply mimic
2052          * the EISA config file to sort it out. */
2053         
2054         /* EISA REG1: Assert DecChip 21040 HW Reset */
2055         outb (ER1_IAM | 1, EISA_REG1);
2056         mdelay (1);
2057
2058         /* EISA REG1: Deassert DecChip 21040 HW Reset */
2059         outb (ER1_IAM, EISA_REG1);
2060         mdelay (1);
2061
2062         /* EISA REG3: R/W Burst Transfer Enable */
2063         outb (ER3_BWE | ER3_BRE, EISA_REG3);
2064         
2065         /* 32_bit slave/master, Preempt Time=23 bclks, Unlatched Interrupt */
2066         outb (ER0_BSW | ER0_BMW | ER0_EPT | regval, EISA_REG0);
2067 #endif
2068         irq = de4x5_irq[(regval >> 1) & 0x03];
2069         
2070         if (is_DC2114x) {
2071             device = ((lp->cfrv & CFRV_RN) < DC2114x_BRK ? DC21142 : DC21143);
2072         }
2073         lp->chipset = device;
2074         lp->bus = EISA;
2075
2076         /* Write the PCI Configuration Registers */
2077         outl(PCI_COMMAND_IO | PCI_COMMAND_MASTER, PCI_CFCS);
2078         outl(0x00006000, PCI_CFLT);
2079         outl(iobase, PCI_CBIO);
2080             
2081         DevicePresent(dev, EISA_APROM);
2082
2083         dev->irq = irq;
2084
2085         if (!(status = de4x5_hw_init (dev, iobase, gendev))) {
2086                 return 0;
2087         }
2088
2089         free_netdev (dev);
2090  release_reg_2:
2091         release_region (iobase + DE4X5_EISA_IO_PORTS, DE4X5_EISA_TOTAL_SIZE);
2092  release_reg_1:
2093         release_region (iobase, DE4X5_EISA_TOTAL_SIZE);
2094
2095         return status;
2096 }
2097
2098 static int __devexit de4x5_eisa_remove (struct device *device)
2099 {
2100         struct net_device *dev;
2101         u_long iobase;
2102
2103         dev = device->driver_data;
2104         iobase = dev->base_addr;
2105         
2106         unregister_netdev (dev);
2107         free_netdev (dev);
2108         release_region (iobase + DE4X5_EISA_IO_PORTS, DE4X5_EISA_TOTAL_SIZE);
2109         release_region (iobase, DE4X5_EISA_TOTAL_SIZE);
2110
2111         return 0;
2112 }
2113
2114 static struct eisa_device_id de4x5_eisa_ids[] = {
2115         { "DEC4250", 0 },       /* 0 is the board name index... */
2116         { "" }
2117 };
2118
2119 static struct eisa_driver de4x5_eisa_driver = {
2120         .id_table = de4x5_eisa_ids,
2121         .driver   = {
2122                 .name    = "de4x5",
2123                 .probe   = de4x5_eisa_probe,
2124                 .remove  = __devexit_p (de4x5_eisa_remove),
2125         }
2126 };
2127 #endif
2128
2129 #ifdef CONFIG_PCI
2130
2131 /*
2132 ** This function searches the current bus (which is >0) for a DECchip with an
2133 ** SROM, so that in multiport cards that have one SROM shared between multiple 
2134 ** DECchips, we can find the base SROM irrespective of the BIOS scan direction.
2135 ** For single port cards this is a time waster...
2136 */
2137 static void __devinit 
2138 srom_search(struct net_device *dev, struct pci_dev *pdev)
2139 {
2140     u_char pb;
2141     u_short vendor, status;
2142     u_int irq = 0, device;
2143     u_long iobase = 0;                     /* Clear upper 32 bits in Alphas */
2144     int i, j, cfrv;
2145     struct de4x5_private *lp = netdev_priv(dev);
2146     struct list_head *walk = &pdev->bus_list;
2147
2148     for (walk = walk->next; walk != &pdev->bus_list; walk = walk->next) {
2149         struct pci_dev *this_dev = pci_dev_b(walk);
2150
2151         /* Skip the pci_bus list entry */
2152         if (list_entry(walk, struct pci_bus, devices) == pdev->bus) continue;
2153
2154         vendor = this_dev->vendor;
2155         device = this_dev->device << 8;
2156         if (!(is_DC21040 || is_DC21041 || is_DC21140 || is_DC2114x)) continue;
2157
2158         /* Get the chip configuration revision register */
2159         pb = this_dev->bus->number;
2160         pci_read_config_dword(this_dev, PCI_REVISION_ID, &cfrv);
2161
2162         /* Set the device number information */
2163         lp->device = PCI_SLOT(this_dev->devfn);
2164         lp->bus_num = pb;
2165             
2166         /* Set the chipset information */
2167         if (is_DC2114x) {
2168             device = ((cfrv & CFRV_RN) < DC2114x_BRK ? DC21142 : DC21143);
2169         }
2170         lp->chipset = device;
2171
2172         /* Get the board I/O address (64 bits on sparc64) */
2173         iobase = pci_resource_start(this_dev, 0);
2174
2175         /* Fetch the IRQ to be used */
2176         irq = this_dev->irq;
2177         if ((irq == 0) || (irq == 0xff) || ((int)irq == -1)) continue;
2178             
2179         /* Check if I/O accesses are enabled */
2180         pci_read_config_word(this_dev, PCI_COMMAND, &status);
2181         if (!(status & PCI_COMMAND_IO)) continue;
2182
2183         /* Search for a valid SROM attached to this DECchip */
2184         DevicePresent(dev, DE4X5_APROM);
2185         for (j=0, i=0; i<ETH_ALEN; i++) {
2186             j += (u_char) *((u_char *)&lp->srom + SROM_HWADD + i);
2187         }
2188         if ((j != 0) && (j != 0x5fa)) {
2189             last.chipset = device;
2190             last.bus = pb;
2191             last.irq = irq;
2192             for (i=0; i<ETH_ALEN; i++) {
2193                 last.addr[i] = (u_char)*((u_char *)&lp->srom + SROM_HWADD + i);
2194             }
2195             return;
2196         }
2197     }
2198
2199     return;
2200 }
2201
2202 /*
2203 ** PCI bus I/O device probe
2204 ** NB: PCI I/O accesses and Bus Mastering are enabled by the PCI BIOS, not
2205 ** the driver. Some PCI BIOS's, pre V2.1, need the slot + features to be
2206 ** enabled by the user first in the set up utility. Hence we just check for
2207 ** enabled features and silently ignore the card if they're not.
2208 **
2209 ** STOP PRESS: Some BIOS's __require__ the driver to enable the bus mastering
2210 ** bit. Here, check for I/O accesses and then set BM. If you put the card in
2211 ** a non BM slot, you're on your own (and complain to the PC vendor that your
2212 ** PC doesn't conform to the PCI standard)!
2213 **
2214 ** This function is only compatible with the *latest* 2.1.x kernels. For 2.0.x
2215 ** kernels use the V0.535[n] drivers.
2216 */
2217
2218 static int __devinit de4x5_pci_probe (struct pci_dev *pdev,
2219                                    const struct pci_device_id *ent)
2220 {
2221         u_char pb, pbus = 0, dev_num, dnum = 0, timer;
2222         u_short vendor, status;
2223         u_int irq = 0, device;
2224         u_long iobase = 0;      /* Clear upper 32 bits in Alphas */
2225         int error;
2226         struct net_device *dev;
2227         struct de4x5_private *lp;
2228
2229         dev_num = PCI_SLOT(pdev->devfn);
2230         pb = pdev->bus->number;
2231
2232         if (io) { /* probe a single PCI device */
2233                 pbus = (u_short)(io >> 8);
2234                 dnum = (u_short)(io & 0xff);
2235                 if ((pbus != pb) || (dnum != dev_num))
2236                         return -ENODEV;
2237         }
2238
2239         vendor = pdev->vendor;
2240         device = pdev->device << 8;
2241         if (!(is_DC21040 || is_DC21041 || is_DC21140 || is_DC2114x))
2242                 return -ENODEV;
2243
2244         /* Ok, the device seems to be for us. */
2245         if (!(dev = alloc_etherdev (sizeof (struct de4x5_private))))
2246                 return -ENOMEM;
2247
2248         lp = netdev_priv(dev);
2249         lp->bus = PCI;
2250         lp->bus_num = 0;
2251         
2252         /* Search for an SROM on this bus */
2253         if (lp->bus_num != pb) {
2254             lp->bus_num = pb;
2255             srom_search(dev, pdev);
2256         }
2257
2258         /* Get the chip configuration revision register */
2259         pci_read_config_dword(pdev, PCI_REVISION_ID, &lp->cfrv);
2260
2261         /* Set the device number information */
2262         lp->device = dev_num;
2263         lp->bus_num = pb;
2264         
2265         /* Set the chipset information */
2266         if (is_DC2114x) {
2267             device = ((lp->cfrv & CFRV_RN) < DC2114x_BRK ? DC21142 : DC21143);
2268         }
2269         lp->chipset = device;
2270
2271         /* Get the board I/O address (64 bits on sparc64) */
2272         iobase = pci_resource_start(pdev, 0);
2273
2274         /* Fetch the IRQ to be used */
2275         irq = pdev->irq;
2276         if ((irq == 0) || (irq == 0xff) || ((int)irq == -1)) {
2277                 error = -ENODEV;
2278                 goto free_dev;
2279         }
2280             
2281         /* Check if I/O accesses and Bus Mastering are enabled */
2282         pci_read_config_word(pdev, PCI_COMMAND, &status);
2283 #ifdef __powerpc__
2284         if (!(status & PCI_COMMAND_IO)) {
2285             status |= PCI_COMMAND_IO;
2286             pci_write_config_word(pdev, PCI_COMMAND, status);
2287             pci_read_config_word(pdev, PCI_COMMAND, &status);
2288         }
2289 #endif /* __powerpc__ */
2290         if (!(status & PCI_COMMAND_IO)) {
2291                 error = -ENODEV;
2292                 goto free_dev;
2293         }
2294
2295         if (!(status & PCI_COMMAND_MASTER)) {
2296             status |= PCI_COMMAND_MASTER;
2297             pci_write_config_word(pdev, PCI_COMMAND, status);
2298             pci_read_config_word(pdev, PCI_COMMAND, &status);
2299         }
2300         if (!(status & PCI_COMMAND_MASTER)) {
2301                 error = -ENODEV;
2302                 goto free_dev;
2303         }
2304
2305         /* Check the latency timer for values >= 0x60 */
2306         pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &timer);
2307         if (timer < 0x60) {
2308             pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x60);
2309         }
2310
2311         DevicePresent(dev, DE4X5_APROM);
2312
2313         if (!request_region (iobase, DE4X5_PCI_TOTAL_SIZE, "de4x5")) {
2314                 error = -EBUSY;
2315                 goto free_dev;
2316         }
2317
2318         dev->irq = irq;
2319         
2320         if ((error = de4x5_hw_init(dev, iobase, &pdev->dev))) {
2321                 goto release;
2322         }
2323
2324         return 0;
2325
2326  release:
2327         release_region (iobase, DE4X5_PCI_TOTAL_SIZE);
2328  free_dev:
2329         free_netdev (dev);
2330         return error;
2331 }
2332
2333 static void __devexit de4x5_pci_remove (struct pci_dev *pdev)
2334 {
2335         struct net_device *dev;
2336         u_long iobase;
2337
2338         dev = pdev->dev.driver_data;
2339         iobase = dev->base_addr;
2340
2341         unregister_netdev (dev);
2342         free_netdev (dev);
2343         release_region (iobase, DE4X5_PCI_TOTAL_SIZE);
2344 }
2345
2346 static struct pci_device_id de4x5_pci_tbl[] = {
2347         { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TULIP,
2348           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
2349         { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TULIP_PLUS,
2350           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
2351         { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TULIP_FAST,
2352           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2 },
2353         { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142,
2354           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3 },
2355         { },
2356 };
2357
2358 static struct pci_driver de4x5_pci_driver = {
2359         .name           = "de4x5",
2360         .id_table       = de4x5_pci_tbl,
2361         .probe          = de4x5_pci_probe,
2362         .remove         = __devexit_p (de4x5_pci_remove),
2363 };
2364
2365 #endif
2366
2367 /*
2368 ** Auto configure the media here rather than setting the port at compile
2369 ** time. This routine is called by de4x5_init() and when a loss of media is
2370 ** detected (excessive collisions, loss of carrier, no carrier or link fail
2371 ** [TP] or no recent receive activity) to check whether the user has been 
2372 ** sneaky and changed the port on us.
2373 */
2374 static int
2375 autoconf_media(struct net_device *dev)
2376 {
2377     struct de4x5_private *lp = netdev_priv(dev);
2378     u_long iobase = dev->base_addr;
2379     int next_tick = DE4X5_AUTOSENSE_MS;
2380
2381     lp->linkOK = 0;
2382     lp->c_media = AUTO;                     /* Bogus last media */
2383     disable_ast(dev);
2384     inl(DE4X5_MFC);                         /* Zero the lost frames counter */
2385     lp->media = INIT;
2386     lp->tcount = 0;
2387
2388     if (lp->useSROM) {
2389         next_tick = srom_autoconf(dev);
2390     } else if (lp->chipset == DC21040) {
2391         next_tick = dc21040_autoconf(dev);
2392     } else if (lp->chipset == DC21041) {
2393         next_tick = dc21041_autoconf(dev);
2394     } else if (lp->chipset == DC21140) {
2395         next_tick = dc21140m_autoconf(dev);
2396     }
2397
2398     enable_ast(dev, next_tick);
2399     
2400     return (lp->media);
2401 }
2402
2403 /*
2404 ** Autoconfigure the media when using the DC21040. AUI cannot be distinguished
2405 ** from BNC as the port has a jumper to set thick or thin wire. When set for
2406 ** BNC, the BNC port will indicate activity if it's not terminated correctly.
2407 ** The only way to test for that is to place a loopback packet onto the
2408 ** network and watch for errors. Since we're messing with the interrupt mask
2409 ** register, disable the board interrupts and do not allow any more packets to
2410 ** be queued to the hardware. Re-enable everything only when the media is
2411 ** found.
2412 ** I may have to "age out" locally queued packets so that the higher layer
2413 ** timeouts don't effectively duplicate packets on the network.
2414 */
2415 static int
2416 dc21040_autoconf(struct net_device *dev)
2417 {
2418     struct de4x5_private *lp = netdev_priv(dev);
2419     u_long iobase = dev->base_addr;
2420     int next_tick = DE4X5_AUTOSENSE_MS;
2421     s32 imr;
2422     
2423     switch (lp->media) {
2424     case INIT:
2425         DISABLE_IRQs;
2426         lp->tx_enable = NO;
2427         lp->timeout = -1;
2428         de4x5_save_skbs(dev);
2429         if ((lp->autosense == AUTO) || (lp->autosense == TP)) {
2430             lp->media = TP;
2431         } else if ((lp->autosense == BNC) || (lp->autosense == AUI) || (lp->autosense == BNC_AUI)) {
2432             lp->media = BNC_AUI;
2433         } else if (lp->autosense == EXT_SIA) {
2434             lp->media = EXT_SIA;
2435         } else {
2436             lp->media = NC;
2437         }
2438         lp->local_state = 0;
2439         next_tick = dc21040_autoconf(dev);
2440         break;
2441         
2442     case TP:
2443         next_tick = dc21040_state(dev, 0x8f01, 0xffff, 0x0000, 3000, BNC_AUI, 
2444                                                          TP_SUSPECT, test_tp);
2445         break;
2446         
2447     case TP_SUSPECT:
2448         next_tick = de4x5_suspect_state(dev, 1000, TP, test_tp, dc21040_autoconf);
2449         break;
2450         
2451     case BNC:
2452     case AUI:
2453     case BNC_AUI:
2454         next_tick = dc21040_state(dev, 0x8f09, 0x0705, 0x0006, 3000, EXT_SIA, 
2455                                                   BNC_AUI_SUSPECT, ping_media);
2456         break;
2457         
2458     case BNC_AUI_SUSPECT:
2459         next_tick = de4x5_suspect_state(dev, 1000, BNC_AUI, ping_media, dc21040_autoconf);
2460         break;
2461         
2462     case EXT_SIA:
2463         next_tick = dc21040_state(dev, 0x3041, 0x0000, 0x0006, 3000, 
2464                                               NC, EXT_SIA_SUSPECT, ping_media);
2465         break;
2466         
2467     case EXT_SIA_SUSPECT:
2468         next_tick = de4x5_suspect_state(dev, 1000, EXT_SIA, ping_media, dc21040_autoconf);
2469         break;
2470         
2471     case NC:
2472         /* default to TP for all */
2473         reset_init_sia(dev, 0x8f01, 0xffff, 0x0000);
2474         if (lp->media != lp->c_media) {
2475             de4x5_dbg_media(dev);
2476             lp->c_media = lp->media;
2477         }
2478         lp->media = INIT;
2479         lp->tx_enable = NO;
2480         break;
2481     }
2482     
2483     return next_tick;
2484 }
2485
2486 static int
2487 dc21040_state(struct net_device *dev, int csr13, int csr14, int csr15, int timeout,
2488               int next_state, int suspect_state, 
2489               int (*fn)(struct net_device *, int))
2490 {
2491     struct de4x5_private *lp = netdev_priv(dev);
2492     int next_tick = DE4X5_AUTOSENSE_MS;
2493     int linkBad;
2494
2495     switch (lp->local_state) {
2496     case 0:
2497         reset_init_sia(dev, csr13, csr14, csr15);
2498         lp->local_state++;
2499         next_tick = 500;
2500         break;
2501             
2502     case 1:
2503         if (!lp->tx_enable) {
2504             linkBad = fn(dev, timeout);
2505             if (linkBad < 0) {
2506                 next_tick = linkBad & ~TIMER_CB;
2507             } else {
2508                 if (linkBad && (lp->autosense == AUTO)) {
2509                     lp->local_state = 0;
2510                     lp->media = next_state;
2511                 } else {
2512                     de4x5_init_connection(dev);
2513                 }
2514             }
2515         } else if (!lp->linkOK && (lp->autosense == AUTO)) {
2516             lp->media = suspect_state;
2517             next_tick = 3000;
2518         }
2519         break;
2520     }
2521     
2522     return next_tick;
2523 }
2524
2525 static int
2526 de4x5_suspect_state(struct net_device *dev, int timeout, int prev_state,
2527                       int (*fn)(struct net_device *, int),
2528                       int (*asfn)(struct net_device *))
2529 {
2530     struct de4x5_private *lp = netdev_priv(dev);
2531     int next_tick = DE4X5_AUTOSENSE_MS;
2532     int linkBad;
2533
2534     switch (lp->local_state) {
2535     case 1:
2536         if (lp->linkOK) {
2537             lp->media = prev_state;
2538         } else {
2539             lp->local_state++;
2540             next_tick = asfn(dev);
2541         }
2542         break;
2543
2544     case 2:
2545         linkBad = fn(dev, timeout);
2546         if (linkBad < 0) {
2547             next_tick = linkBad & ~TIMER_CB;
2548         } else if (!linkBad) {
2549             lp->local_state--;
2550             lp->media = prev_state;
2551         } else {
2552             lp->media = INIT;
2553             lp->tcount++;
2554         }
2555     }
2556
2557     return next_tick;
2558 }
2559
2560 /*
2561 ** Autoconfigure the media when using the DC21041. AUI needs to be tested
2562 ** before BNC, because the BNC port will indicate activity if it's not
2563 ** terminated correctly. The only way to test for that is to place a loopback
2564 ** packet onto the network and watch for errors. Since we're messing with
2565 ** the interrupt mask register, disable the board interrupts and do not allow
2566 ** any more packets to be queued to the hardware. Re-enable everything only
2567 ** when the media is found.
2568 */
2569 static int
2570 dc21041_autoconf(struct net_device *dev)
2571 {
2572     struct de4x5_private *lp = netdev_priv(dev);
2573     u_long iobase = dev->base_addr;
2574     s32 sts, irqs, irq_mask, imr, omr;
2575     int next_tick = DE4X5_AUTOSENSE_MS;
2576     
2577     switch (lp->media) {
2578     case INIT:
2579         DISABLE_IRQs;
2580         lp->tx_enable = NO;
2581         lp->timeout = -1;
2582         de4x5_save_skbs(dev);          /* Save non transmitted skb's */
2583         if ((lp->autosense == AUTO) || (lp->autosense == TP_NW)) {
2584             lp->media = TP;            /* On chip auto negotiation is broken */
2585         } else if (lp->autosense == TP) {
2586             lp->media = TP;
2587         } else if (lp->autosense == BNC) {
2588             lp->media = BNC;
2589         } else if (lp->autosense == AUI) {
2590             lp->media = AUI;
2591         } else {
2592             lp->media = NC;
2593         }
2594         lp->local_state = 0;
2595         next_tick = dc21041_autoconf(dev);
2596         break;
2597         
2598     case TP_NW:
2599         if (lp->timeout < 0) {
2600             omr = inl(DE4X5_OMR);/* Set up full duplex for the autonegotiate */
2601             outl(omr | OMR_FDX, DE4X5_OMR);
2602         }
2603         irqs = STS_LNF | STS_LNP;
2604         irq_mask = IMR_LFM | IMR_LPM;
2605         sts = test_media(dev, irqs, irq_mask, 0xef01, 0xffff, 0x0008, 2400);
2606         if (sts < 0) {
2607             next_tick = sts & ~TIMER_CB;
2608         } else {
2609             if (sts & STS_LNP) {
2610                 lp->media = ANS;
2611             } else {
2612                 lp->media = AUI;
2613             }
2614             next_tick = dc21041_autoconf(dev);
2615         }
2616         break;
2617         
2618     case ANS:
2619         if (!lp->tx_enable) {
2620             irqs = STS_LNP;
2621             irq_mask = IMR_LPM;
2622             sts = test_ans(dev, irqs, irq_mask, 3000);
2623             if (sts < 0) {
2624                 next_tick = sts & ~TIMER_CB;
2625             } else {
2626                 if (!(sts & STS_LNP) && (lp->autosense == AUTO)) {
2627                     lp->media = TP;
2628                     next_tick = dc21041_autoconf(dev);
2629                 } else {
2630                     lp->local_state = 1;
2631                     de4x5_init_connection(dev);
2632                 }
2633             }
2634         } else if (!lp->linkOK && (lp->autosense == AUTO)) {
2635             lp->media = ANS_SUSPECT;
2636             next_tick = 3000;
2637         }
2638         break;
2639         
2640     case ANS_SUSPECT:
2641         next_tick = de4x5_suspect_state(dev, 1000, ANS, test_tp, dc21041_autoconf);
2642         break;
2643         
2644     case TP:
2645         if (!lp->tx_enable) {
2646             if (lp->timeout < 0) {
2647                 omr = inl(DE4X5_OMR);          /* Set up half duplex for TP */
2648                 outl(omr & ~OMR_FDX, DE4X5_OMR);
2649             }
2650             irqs = STS_LNF | STS_LNP;
2651             irq_mask = IMR_LFM | IMR_LPM;
2652             sts = test_media(dev,irqs, irq_mask, 0xef01, 0xff3f, 0x0008, 2400);
2653             if (sts < 0) {
2654                 next_tick = sts & ~TIMER_CB;
2655             } else {
2656                 if (!(sts & STS_LNP) && (lp->autosense == AUTO)) {
2657                     if (inl(DE4X5_SISR) & SISR_NRA) {
2658                         lp->media = AUI;       /* Non selected port activity */
2659                     } else {
2660                         lp->media = BNC;
2661                     }
2662                     next_tick = dc21041_autoconf(dev);
2663                 } else {
2664                     lp->local_state = 1;
2665                     de4x5_init_connection(dev);
2666                 }
2667             }
2668         } else if (!lp->linkOK && (lp->autosense == AUTO)) {
2669             lp->media = TP_SUSPECT;
2670             next_tick = 3000;
2671         }
2672         break;
2673         
2674     case TP_SUSPECT:
2675         next_tick = de4x5_suspect_state(dev, 1000, TP, test_tp, dc21041_autoconf);
2676         break;
2677         
2678     case AUI:
2679         if (!lp->tx_enable) {
2680             if (lp->timeout < 0) {
2681                 omr = inl(DE4X5_OMR);          /* Set up half duplex for AUI */
2682                 outl(omr & ~OMR_FDX, DE4X5_OMR);
2683             }
2684             irqs = 0;
2685             irq_mask = 0;
2686             sts = test_media(dev,irqs, irq_mask, 0xef09, 0xf73d, 0x000e, 1000);
2687             if (sts < 0) {
2688                 next_tick = sts & ~TIMER_CB;
2689             } else {
2690                 if (!(inl(DE4X5_SISR) & SISR_SRA) && (lp->autosense == AUTO)) {
2691                     lp->media = BNC;
2692                     next_tick = dc21041_autoconf(dev);
2693                 } else {
2694                     lp->local_state = 1;
2695                     de4x5_init_connection(dev);
2696                 }
2697             }
2698         } else if (!lp->linkOK && (lp->autosense == AUTO)) {
2699             lp->media = AUI_SUSPECT;
2700             next_tick = 3000;
2701         }
2702         break;
2703         
2704     case AUI_SUSPECT:
2705         next_tick = de4x5_suspect_state(dev, 1000, AUI, ping_media, dc21041_autoconf);
2706         break;
2707         
2708     case BNC:
2709         switch (lp->local_state) {
2710         case 0:
2711             if (lp->timeout < 0) {
2712                 omr = inl(DE4X5_OMR);          /* Set up half duplex for BNC */
2713                 outl(omr & ~OMR_FDX, DE4X5_OMR);
2714             }
2715             irqs = 0;
2716             irq_mask = 0;
2717             sts = test_media(dev,irqs, irq_mask, 0xef09, 0xf73d, 0x0006, 1000);
2718             if (sts < 0) {
2719                 next_tick = sts & ~TIMER_CB;
2720             } else {
2721                 lp->local_state++;             /* Ensure media connected */
2722                 next_tick = dc21041_autoconf(dev);
2723             }
2724             break;
2725             
2726         case 1:
2727             if (!lp->tx_enable) {
2728                 if ((sts = ping_media(dev, 3000)) < 0) {
2729                     next_tick = sts & ~TIMER_CB;
2730                 } else {
2731                     if (sts) {
2732                         lp->local_state = 0;
2733                         lp->media = NC;
2734                     } else {
2735                         de4x5_init_connection(dev);
2736                     }
2737                 }
2738             } else if (!lp->linkOK && (lp->autosense == AUTO)) {
2739                 lp->media = BNC_SUSPECT;
2740                 next_tick = 3000;
2741             }
2742             break;
2743         }
2744         break;
2745         
2746     case BNC_SUSPECT:
2747         next_tick = de4x5_suspect_state(dev, 1000, BNC, ping_media, dc21041_autoconf);
2748         break;
2749         
2750     case NC:
2751         omr = inl(DE4X5_OMR);    /* Set up full duplex for the autonegotiate */
2752         outl(omr | OMR_FDX, DE4X5_OMR);
2753         reset_init_sia(dev, 0xef01, 0xffff, 0x0008);/* Initialise the SIA */
2754         if (lp->media != lp->c_media) {
2755             de4x5_dbg_media(dev);
2756             lp->c_media = lp->media;
2757         }
2758         lp->media = INIT;
2759         lp->tx_enable = NO;
2760         break;
2761     }
2762     
2763     return next_tick;
2764 }
2765
2766 /*
2767 ** Some autonegotiation chips are broken in that they do not return the
2768 ** acknowledge bit (anlpa & MII_ANLPA_ACK) in the link partner advertisement
2769 ** register, except at the first power up negotiation.
2770 */
2771 static int
2772 dc21140m_autoconf(struct net_device *dev)
2773 {
2774     struct de4x5_private *lp = netdev_priv(dev);
2775     int ana, anlpa, cap, cr, slnk, sr;
2776     int next_tick = DE4X5_AUTOSENSE_MS;
2777     u_long imr, omr, iobase = dev->base_addr;
2778     
2779     switch(lp->media) {
2780     case INIT: 
2781         if (lp->timeout < 0) {
2782             DISABLE_IRQs;
2783             lp->tx_enable = FALSE;
2784             lp->linkOK = 0;
2785             de4x5_save_skbs(dev);          /* Save non transmitted skb's */
2786         }
2787         if ((next_tick = de4x5_reset_phy(dev)) < 0) {
2788             next_tick &= ~TIMER_CB;
2789         } else {
2790             if (lp->useSROM) {
2791                 if (srom_map_media(dev) < 0) {
2792                     lp->tcount++;
2793                     return next_tick;
2794                 }
2795                 srom_exec(dev, lp->phy[lp->active].gep);
2796                 if (lp->infoblock_media == ANS) {
2797                     ana = lp->phy[lp->active].ana | MII_ANA_CSMA;
2798                     mii_wr(ana, MII_ANA, lp->phy[lp->active].addr, DE4X5_MII);
2799                 }
2800             } else {
2801                 lp->tmp = MII_SR_ASSC;     /* Fake out the MII speed set */
2802                 SET_10Mb;
2803                 if (lp->autosense == _100Mb) {
2804                     lp->media = _100Mb;
2805                 } else if (lp->autosense == _10Mb) {
2806                     lp->media = _10Mb;
2807                 } else if ((lp->autosense == AUTO) && 
2808                                     ((sr=is_anc_capable(dev)) & MII_SR_ANC)) {
2809                     ana = (((sr >> 6) & MII_ANA_TAF) | MII_ANA_CSMA);
2810                     ana &= (lp->fdx ? ~0 : ~MII_ANA_FDAM);
2811                     mii_wr(ana, MII_ANA, lp->phy[lp->active].addr, DE4X5_MII);
2812                     lp->media = ANS;
2813                 } else if (lp->autosense == AUTO) {
2814                     lp->media = SPD_DET;
2815                 } else if (is_spd_100(dev) && is_100_up(dev)) {
2816                     lp->media = _100Mb;
2817                 } else {
2818                     lp->media = NC;
2819                 }
2820             }
2821             lp->local_state = 0;
2822             next_tick = dc21140m_autoconf(dev);
2823         }
2824         break;
2825         
2826     case ANS:
2827         switch (lp->local_state) {
2828         case 0:
2829             if (lp->timeout < 0) {
2830                 mii_wr(MII_CR_ASSE | MII_CR_RAN, MII_CR, lp->phy[lp->active].addr, DE4X5_MII);
2831             }
2832             cr = test_mii_reg(dev, MII_CR, MII_CR_RAN, FALSE, 500);
2833             if (cr < 0) {
2834                 next_tick = cr & ~TIMER_CB;
2835             } else {
2836                 if (cr) {
2837                     lp->local_state = 0;
2838                     lp->media = SPD_DET;
2839                 } else {
2840                     lp->local_state++;
2841                 }
2842                 next_tick = dc21140m_autoconf(dev);
2843             }
2844             break;
2845             
2846         case 1:
2847             if ((sr=test_mii_reg(dev, MII_SR, MII_SR_ASSC, TRUE, 2000)) < 0) {
2848                 next_tick = sr & ~TIMER_CB;
2849             } else {
2850                 lp->media = SPD_DET;
2851                 lp->local_state = 0;
2852                 if (sr) {                         /* Success! */
2853                     lp->tmp = MII_SR_ASSC;
2854                     anlpa = mii_rd(MII_ANLPA, lp->phy[lp->active].addr, DE4X5_MII);
2855                     ana = mii_rd(MII_ANA, lp->phy[lp->active].addr, DE4X5_MII);
2856                     if (!(anlpa & MII_ANLPA_RF) && 
2857                          (cap = anlpa & MII_ANLPA_TAF & ana)) {
2858                         if (cap & MII_ANA_100M) {
2859                             lp->fdx = ((ana & anlpa & MII_ANA_FDAM & MII_ANA_100M) ? TRUE : FALSE);
2860                             lp->media = _100Mb;
2861                         } else if (cap & MII_ANA_10M) {
2862                             lp->fdx = ((ana & anlpa & MII_ANA_FDAM & MII_ANA_10M) ? TRUE : FALSE);
2863
2864                             lp->media = _10Mb;
2865                         }
2866                     }
2867                 }                       /* Auto Negotiation failed to finish */
2868                 next_tick = dc21140m_autoconf(dev);
2869             }                           /* Auto Negotiation failed to start */
2870             break;
2871         }
2872         break;
2873         
2874     case SPD_DET:                              /* Choose 10Mb/s or 100Mb/s */
2875         if (lp->timeout < 0) {
2876             lp->tmp = (lp->phy[lp->active].id ? MII_SR_LKS : 
2877                                                   (~gep_rd(dev) & GEP_LNP));
2878             SET_100Mb_PDET;
2879         }
2880         if ((slnk = test_for_100Mb(dev, 6500)) < 0) {
2881             next_tick = slnk & ~TIMER_CB;
2882         } else {
2883             if (is_spd_100(dev) && is_100_up(dev)) {
2884                 lp->media = _100Mb;
2885             } else if ((!is_spd_100(dev) && (is_10_up(dev) & lp->tmp))) {
2886                 lp->media = _10Mb;
2887             } else {
2888                 lp->media = NC;
2889             }
2890             next_tick = dc21140m_autoconf(dev);
2891         }
2892         break;
2893         
2894     case _100Mb:                               /* Set 100Mb/s */
2895         next_tick = 3000;
2896         if (!lp->tx_enable) {
2897             SET_100Mb;
2898             de4x5_init_connection(dev);
2899         } else {
2900             if (!lp->linkOK && (lp->autosense == AUTO)) {
2901                 if (!is_100_up(dev) || (!lp->useSROM && !is_spd_100(dev))) {
2902                     lp->media = INIT;
2903                     lp->tcount++;
2904                     next_tick = DE4X5_AUTOSENSE_MS;
2905                 }
2906             }
2907         }
2908         break;
2909
2910     case BNC:
2911     case AUI:
2912     case _10Mb:                                /* Set 10Mb/s */
2913         next_tick = 3000;
2914         if (!lp->tx_enable) {
2915             SET_10Mb;
2916             de4x5_init_connection(dev);
2917         } else {
2918             if (!lp->linkOK && (lp->autosense == AUTO)) {
2919                 if (!is_10_up(dev) || (!lp->useSROM && is_spd_100(dev))) {
2920                     lp->media = INIT;
2921                     lp->tcount++;
2922                     next_tick = DE4X5_AUTOSENSE_MS;
2923                 }
2924             }
2925         }
2926         break;
2927         
2928     case NC:
2929         if (lp->media != lp->c_media) {
2930             de4x5_dbg_media(dev);
2931             lp->c_media = lp->media;
2932         }
2933         lp->media = INIT;
2934         lp->tx_enable = FALSE;
2935         break;
2936     }
2937     
2938     return next_tick;
2939 }
2940
2941 /*
2942 ** This routine may be merged into dc21140m_autoconf() sometime as I'm
2943 ** changing how I figure out the media - but trying to keep it backwards
2944 ** compatible with the de500-xa and de500-aa.
2945 ** Whether it's BNC, AUI, SYM or MII is sorted out in the infoblock
2946 ** functions and set during de4x5_mac_port() and/or de4x5_reset_phy().
2947 ** This routine just has to figure out whether 10Mb/s or 100Mb/s is
2948 ** active.
2949 ** When autonegotiation is working, the ANS part searches the SROM for
2950 ** the highest common speed (TP) link that both can run and if that can
2951 ** be full duplex. That infoblock is executed and then the link speed set.
2952 **
2953 ** Only _10Mb and _100Mb are tested here.
2954 */
2955 static int
2956 dc2114x_autoconf(struct net_device *dev)
2957 {
2958     struct de4x5_private *lp = netdev_priv(dev);
2959     u_long iobase = dev->base_addr;
2960     s32 cr, anlpa, ana, cap, irqs, irq_mask, imr, omr, slnk, sr, sts;
2961     int next_tick = DE4X5_AUTOSENSE_MS;
2962
2963     switch (lp->media) {
2964     case INIT:
2965         if (lp->timeout < 0) {
2966             DISABLE_IRQs;
2967             lp->tx_enable = FALSE;
2968             lp->linkOK = 0;
2969             lp->timeout = -1;
2970             de4x5_save_skbs(dev);            /* Save non transmitted skb's */
2971             if (lp->params.autosense & ~AUTO) {
2972                 srom_map_media(dev);         /* Fixed media requested      */
2973                 if (lp->media != lp->params.autosense) {
2974                     lp->tcount++;
2975                     lp->media = INIT;
2976                     return next_tick;
2977                 }
2978                 lp->media = INIT;
2979             }
2980         }
2981         if ((next_tick = de4x5_reset_phy(dev)) < 0) {
2982             next_tick &= ~TIMER_CB;
2983         } else {
2984             if (lp->autosense == _100Mb) {
2985                 lp->media = _100Mb;
2986             } else if (lp->autosense == _10Mb) {
2987                 lp->media = _10Mb;
2988             } else if (lp->autosense == TP) {
2989                 lp->media = TP;
2990             } else if (lp->autosense == BNC) {
2991                 lp->media = BNC;
2992             } else if (lp->autosense == AUI) {
2993                 lp->media = AUI;
2994             } else {
2995                 lp->media = SPD_DET;
2996                 if ((lp->infoblock_media == ANS) && 
2997                                     ((sr=is_anc_capable(dev)) & MII_SR_ANC)) {
2998                     ana = (((sr >> 6) & MII_ANA_TAF) | MII_ANA_CSMA);
2999                     ana &= (lp->fdx ? ~0 : ~MII_ANA_FDAM);
3000                     mii_wr(ana, MII_ANA, lp->phy[lp->active].addr, DE4X5_MII);
3001                     lp->media = ANS;
3002                 }
3003             }
3004             lp->local_state = 0;
3005             next_tick = dc2114x_autoconf(dev);
3006         }
3007         break;
3008         
3009     case ANS:
3010         switch (lp->local_state) {
3011         case 0:
3012             if (lp->timeout < 0) {
3013                 mii_wr(MII_CR_ASSE | MII_CR_RAN, MII_CR, lp->phy[lp->active].addr, DE4X5_MII);
3014             }
3015             cr = test_mii_reg(dev, MII_CR, MII_CR_RAN, FALSE, 500);
3016             if (cr < 0) {
3017                 next_tick = cr & ~TIMER_CB;
3018             } else {
3019                 if (cr) {
3020                     lp->local_state = 0;
3021                     lp->media = SPD_DET;
3022                 } else {
3023                     lp->local_state++;
3024                 }
3025                 next_tick = dc2114x_autoconf(dev);
3026             }
3027             break;
3028             
3029         case 1:
3030             if ((sr=test_mii_reg(dev, MII_SR, MII_SR_ASSC, TRUE, 2000)) < 0) {
3031                 next_tick = sr & ~TIMER_CB;
3032             } else {
3033                 lp->media = SPD_DET;
3034                 lp->local_state = 0;
3035                 if (sr) {                         /* Success! */
3036                     lp->tmp = MII_SR_ASSC;
3037                     anlpa = mii_rd(MII_ANLPA, lp->phy[lp->active].addr, DE4X5_MII);
3038                     ana = mii_rd(MII_ANA, lp->phy[lp->active].addr, DE4X5_MII);
3039                     if (!(anlpa & MII_ANLPA_RF) && 
3040                          (cap = anlpa & MII_ANLPA_TAF & ana)) {
3041                         if (cap & MII_ANA_100M) {
3042                             lp->fdx = ((ana & anlpa & MII_ANA_FDAM & MII_ANA_100M) ? TRUE : FALSE);
3043                             lp->media = _100Mb;
3044                         } else if (cap & MII_ANA_10M) {
3045                             lp->fdx = ((ana & anlpa & MII_ANA_FDAM & MII_ANA_10M) ? TRUE : FALSE);
3046                             lp->media = _10Mb;
3047                         }
3048                     }
3049                 }                       /* Auto Negotiation failed to finish */
3050                 next_tick = dc2114x_autoconf(dev);
3051             }                           /* Auto Negotiation failed to start  */
3052             break;
3053         }
3054         break;
3055
3056     case AUI:
3057         if (!lp->tx_enable) {
3058             if (lp->timeout < 0) {
3059                 omr = inl(DE4X5_OMR);   /* Set up half duplex for AUI        */
3060                 outl(omr & ~OMR_FDX, DE4X5_OMR);
3061             }
3062             irqs = 0;
3063             irq_mask = 0;
3064             sts = test_media(dev,irqs, irq_mask, 0, 0, 0, 1000);
3065             if (sts < 0) {
3066                 next_tick = sts & ~TIMER_CB;
3067             } else {
3068                 if (!(inl(DE4X5_SISR) & SISR_SRA) && (lp->autosense == AUTO)) {
3069                     lp->media = BNC;
3070                     next_tick = dc2114x_autoconf(dev);
3071                 } else {
3072                     lp->local_state = 1;
3073                     de4x5_init_connection(dev);
3074                 }
3075             }
3076         } else if (!lp->linkOK && (lp->autosense == AUTO)) {
3077             lp->media = AUI_SUSPECT;
3078             next_tick = 3000;
3079         }
3080         break;
3081         
3082     case AUI_SUSPECT:
3083         next_tick = de4x5_suspect_state(dev, 1000, AUI, ping_media, dc2114x_autoconf);
3084         break;
3085         
3086     case BNC:
3087         switch (lp->local_state) {
3088         case 0:
3089             if (lp->timeout < 0) {
3090                 omr = inl(DE4X5_OMR);          /* Set up half duplex for BNC */
3091                 outl(omr & ~OMR_FDX, DE4X5_OMR);
3092             }
3093             irqs = 0;
3094             irq_mask = 0;
3095             sts = test_media(dev,irqs, irq_mask, 0, 0, 0, 1000);
3096             if (sts < 0) {
3097                 next_tick = sts & ~TIMER_CB;
3098             } else {
3099                 lp->local_state++;             /* Ensure media connected */
3100                 next_tick = dc2114x_autoconf(dev);
3101             }
3102             break;
3103             
3104         case 1:
3105             if (!lp->tx_enable) {
3106                 if ((sts = ping_media(dev, 3000)) < 0) {
3107                     next_tick = sts & ~TIMER_CB;
3108                 } else {
3109                     if (sts) {
3110                         lp->local_state = 0;
3111                         lp->tcount++;
3112                         lp->media = INIT;
3113                     } else {
3114                         de4x5_init_connection(dev);
3115                     }
3116                 }
3117             } else if (!lp->linkOK && (lp->autosense == AUTO)) {
3118                 lp->media = BNC_SUSPECT;
3119                 next_tick = 3000;
3120             }
3121             break;
3122         }
3123         break;
3124         
3125     case BNC_SUSPECT:
3126         next_tick = de4x5_suspect_state(dev, 1000, BNC, ping_media, dc2114x_autoconf);
3127         break;
3128         
3129     case SPD_DET:                              /* Choose 10Mb/s or 100Mb/s */
3130           if (srom_map_media(dev) < 0) {
3131               lp->tcount++;
3132               lp->media = INIT;
3133               return next_tick;
3134           }
3135           if (lp->media == _100Mb) {
3136               if ((slnk = test_for_100Mb(dev, 6500)) < 0) {
3137                   lp->media = SPD_DET;
3138                   return  (slnk & ~TIMER_CB);
3139               }
3140           } else {
3141               if (wait_for_link(dev) < 0) {
3142                   lp->media = SPD_DET;
3143                   return PDET_LINK_WAIT;
3144               }
3145           }
3146           if (lp->media == ANS) {           /* Do MII parallel detection */
3147               if (is_spd_100(dev)) {
3148                   lp->media = _100Mb;
3149               } else {
3150                   lp->media = _10Mb;
3151               }
3152               next_tick = dc2114x_autoconf(dev);
3153           } else if (((lp->media == _100Mb) && is_100_up(dev)) ||
3154                      (((lp->media == _10Mb) || (lp->media == TP) ||
3155                        (lp->media == BNC)   || (lp->media == AUI)) && 
3156                       is_10_up(dev))) {
3157               next_tick = dc2114x_autoconf(dev);
3158           } else {
3159               lp->tcount++;
3160               lp->media = INIT;
3161           }
3162           break;
3163         
3164     case _10Mb:
3165         next_tick = 3000;
3166         if (!lp->tx_enable) {
3167             SET_10Mb;
3168             de4x5_init_connection(dev);
3169         } else {
3170             if (!lp->linkOK && (lp->autosense == AUTO)) {
3171                 if (!is_10_up(dev) || (!lp->useSROM && is_spd_100(dev))) {
3172                     lp->media = INIT;
3173                     lp->tcount++;
3174                     next_tick = DE4X5_AUTOSENSE_MS;
3175                 }
3176             }
3177         }
3178         break;
3179
3180     case _100Mb:
3181         next_tick = 3000;
3182         if (!lp->tx_enable) {
3183             SET_100Mb;
3184             de4x5_init_connection(dev);
3185         } else {
3186             if (!lp->linkOK && (lp->autosense == AUTO)) {
3187                 if (!is_100_up(dev) || (!lp->useSROM && !is_spd_100(dev))) {
3188                     lp->media = INIT;
3189                     lp->tcount++;
3190                     next_tick = DE4X5_AUTOSENSE_MS;
3191                 }
3192             }
3193         }
3194         break;
3195
3196     default:
3197         lp->tcount++;
3198 printk("Huh?: media:%02x\n", lp->media);
3199         lp->media = INIT;
3200         break;
3201     }
3202     
3203     return next_tick;
3204 }
3205
3206 static int
3207 srom_autoconf(struct net_device *dev)
3208 {
3209     struct de4x5_private *lp = netdev_priv(dev);
3210
3211     return lp->infoleaf_fn(dev);
3212 }
3213
3214 /*
3215 ** This mapping keeps the original media codes and FDX flag unchanged.
3216 ** While it isn't strictly necessary, it helps me for the moment...
3217 ** The early return avoids a media state / SROM media space clash.
3218 */
3219 static int
3220 srom_map_media(struct net_device *dev)
3221 {
3222     struct de4x5_private *lp = netdev_priv(dev);
3223
3224     lp->fdx = 0;
3225     if (lp->infoblock_media == lp->media) 
3226       return 0;
3227
3228     switch(lp->infoblock_media) {
3229       case SROM_10BASETF:
3230         if (!lp->params.fdx) return -1;
3231         lp->fdx = TRUE;
3232       case SROM_10BASET:
3233         if (lp->params.fdx && !lp->fdx) return -1;
3234         if ((lp->chipset == DC21140) || ((lp->chipset & ~0x00ff) == DC2114x)) {
3235             lp->media = _10Mb;
3236         } else {
3237             lp->media = TP;
3238         }
3239         break;
3240
3241       case SROM_10BASE2:
3242         lp->media = BNC;
3243         break;
3244
3245       case SROM_10BASE5:
3246         lp->media = AUI;
3247         break;
3248
3249       case SROM_100BASETF:
3250         if (!lp->params.fdx) return -1;
3251         lp->fdx = TRUE;
3252       case SROM_100BASET:
3253         if (lp->params.fdx && !lp->fdx) return -1;
3254         lp->media = _100Mb;
3255         break;
3256
3257       case SROM_100BASET4:
3258         lp->media = _100Mb;
3259         break;
3260
3261       case SROM_100BASEFF:
3262         if (!lp->params.fdx) return -1;
3263         lp->fdx = TRUE;
3264       case SROM_100BASEF: 
3265         if (lp->params.fdx && !lp->fdx) return -1;
3266         lp->media = _100Mb;
3267         break;
3268
3269       case ANS:
3270         lp->media = ANS;
3271         lp->fdx = lp->params.fdx;
3272         break;
3273
3274       default: 
3275         printk("%s: Bad media code [%d] detected in SROM!\n", dev->name, 
3276                                                           lp->infoblock_media);
3277         return -1;
3278         break;
3279     }
3280
3281     return 0;
3282 }
3283
3284 static void
3285 de4x5_init_connection(struct net_device *dev)
3286 {
3287     struct de4x5_private *lp = netdev_priv(dev);
3288     u_long iobase = dev->base_addr;
3289     u_long flags = 0;
3290
3291     if (lp->media != lp->c_media) {
3292         de4x5_dbg_media(dev);
3293         lp->c_media = lp->media;          /* Stop scrolling media messages */
3294     }
3295
3296     spin_lock_irqsave(&lp->lock, flags);
3297     de4x5_rst_desc_ring(dev);
3298     de4x5_setup_intr(dev);
3299     lp->tx_enable = YES;
3300     spin_unlock_irqrestore(&lp->lock, flags);
3301     outl(POLL_DEMAND, DE4X5_TPD);
3302
3303     netif_wake_queue(dev);
3304
3305     return;
3306 }
3307
3308 /*
3309 ** General PHY reset function. Some MII devices don't reset correctly
3310 ** since their MII address pins can float at voltages that are dependent
3311 ** on the signal pin use. Do a double reset to ensure a reset.
3312 */
3313 static int
3314 de4x5_reset_phy(struct net_device *dev)
3315 {
3316     struct de4x5_private *lp = netdev_priv(dev);
3317     u_long iobase = dev->base_addr;
3318     int next_tick = 0;
3319
3320     if ((lp->useSROM) || (lp->phy[lp->active].id)) {
3321         if (lp->timeout < 0) {
3322             if (lp->useSROM) {
3323                 if (lp->phy[lp->active].rst) {
3324                     srom_exec(dev, lp->phy[lp->active].rst);
3325                     srom_exec(dev, lp->phy[lp->active].rst);
3326                 } else if (lp->rst) {          /* Type 5 infoblock reset */
3327                     srom_exec(dev, lp->rst);
3328                     srom_exec(dev, lp->rst);
3329                 }
3330             } else {
3331                 PHY_HARD_RESET;
3332             }
3333             if (lp->useMII) {
3334                 mii_wr(MII_CR_RST, MII_CR, lp->phy[lp->active].addr, DE4X5_MII);
3335             }
3336         }
3337         if (lp->useMII) {
3338             next_tick = test_mii_reg(dev, MII_CR, MII_CR_RST, FALSE, 500);
3339         }
3340     } else if (lp->chipset == DC21140) {
3341         PHY_HARD_RESET;
3342     }
3343
3344     return next_tick;
3345 }
3346
3347 static int
3348 test_media(struct net_device *dev, s32 irqs, s32 irq_mask, s32 csr13, s32 csr14, s32 csr15, s32 msec)
3349 {
3350     struct de4x5_private *lp = netdev_priv(dev);
3351     u_long iobase = dev->base_addr;
3352     s32 sts, csr12;
3353     
3354     if (lp->timeout < 0) {
3355         lp->timeout = msec/100;
3356         if (!lp->useSROM) {      /* Already done if by SROM, else dc2104[01] */
3357             reset_init_sia(dev, csr13, csr14, csr15);
3358         }
3359
3360         /* set up the interrupt mask */
3361         outl(irq_mask, DE4X5_IMR);
3362
3363         /* clear all pending interrupts */
3364         sts = inl(DE4X5_STS);
3365         outl(sts, DE4X5_STS);
3366         
3367         /* clear csr12 NRA and SRA bits */
3368         if ((lp->chipset == DC21041) || lp->useSROM) {
3369             csr12 = inl(DE4X5_SISR);
3370             outl(csr12, DE4X5_SISR);
3371         }
3372     }
3373     
3374     sts = inl(DE4X5_STS) & ~TIMER_CB;
3375     
3376     if (!(sts & irqs) && --lp->timeout) {
3377         sts = 100 | TIMER_CB;
3378     } else {
3379         lp->timeout = -1;
3380     }
3381     
3382     return sts;
3383 }
3384
3385 static int
3386 test_tp(struct net_device *dev, s32 msec)
3387 {
3388     struct de4x5_private *lp = netdev_priv(dev);
3389     u_long iobase = dev->base_addr;
3390     int sisr;
3391     
3392     if (lp->timeout < 0) {
3393         lp->timeout = msec/100;
3394     }
3395     
3396     sisr = (inl(DE4X5_SISR) & ~TIMER_CB) & (SISR_LKF | SISR_NCR);
3397
3398     if (sisr && --lp->timeout) {
3399         sisr = 100 | TIMER_CB;
3400     } else {
3401         lp->timeout = -1;
3402     }
3403     
3404     return sisr;
3405 }
3406
3407 /*
3408 ** Samples the 100Mb Link State Signal. The sample interval is important
3409 ** because too fast a rate can give erroneous results and confuse the
3410 ** speed sense algorithm.
3411 */
3412 #define SAMPLE_INTERVAL 500  /* ms */
3413 #define SAMPLE_DELAY    2000 /* ms */
3414 static int
3415 test_for_100Mb(struct net_device *dev, int msec)
3416 {
3417     struct de4x5_private *lp = netdev_priv(dev);
3418     int gep = 0, ret = ((lp->chipset & ~0x00ff)==DC2114x? -1 :GEP_SLNK);
3419
3420     if (lp->timeout < 0) {
3421         if ((msec/SAMPLE_INTERVAL) <= 0) return 0;
3422         if (msec > SAMPLE_DELAY) {
3423             lp->timeout = (msec - SAMPLE_DELAY)/SAMPLE_INTERVAL;
3424             gep = SAMPLE_DELAY | TIMER_CB;
3425             return gep;
3426         } else {
3427             lp->timeout = msec/SAMPLE_INTERVAL;
3428         }
3429     }
3430     
3431     if (lp->phy[lp->active].id || lp->useSROM) {
3432         gep = is_100_up(dev) | is_spd_100(dev);
3433     } else {
3434         gep = (~gep_rd(dev) & (GEP_SLNK | GEP_LNP));
3435     }
3436     if (!(gep & ret) && --lp->timeout) {
3437         gep = SAMPLE_INTERVAL | TIMER_CB;
3438     } else {
3439         lp->timeout = -1;
3440     }
3441     
3442     return gep;
3443 }
3444
3445 static int
3446 wait_for_link(struct net_device *dev)
3447 {
3448     struct de4x5_private *lp = netdev_priv(dev);
3449
3450     if (lp->timeout < 0) {
3451         lp->timeout = 1;
3452     }
3453     
3454     if (lp->timeout--) {
3455         return TIMER_CB;
3456     } else {
3457         lp->timeout = -1;
3458     }
3459     
3460     return 0;
3461 }
3462
3463 /*
3464 **
3465 **
3466 */
3467 static int
3468 test_mii_reg(struct net_device *dev, int reg, int mask, int pol, long msec)
3469 {
3470     struct de4x5_private *lp = netdev_priv(dev);
3471     int test;
3472     u_long iobase = dev->base_addr;
3473     
3474     if (lp->timeout < 0) {
3475         lp->timeout = msec/100;
3476     }
3477     
3478     if (pol) pol = ~0;
3479     reg = mii_rd((u_char)reg, lp->phy[lp->active].addr, DE4X5_MII) & mask;
3480     test = (reg ^ pol) & mask;
3481     
3482     if (test && --lp->timeout) {
3483         reg = 100 | TIMER_CB;
3484     } else {
3485         lp->timeout = -1;
3486     }
3487     
3488     return reg;
3489 }
3490
3491 static int
3492 is_spd_100(struct net_device *dev)
3493 {
3494     struct de4x5_private *lp = netdev_priv(dev);
3495     u_long iobase = dev->base_addr;
3496     int spd;
3497     
3498     if (lp->useMII) {
3499         spd = mii_rd(lp->phy[lp->active].spd.reg, lp->phy[lp->active].addr, DE4X5_MII);
3500         spd = ~(spd ^ lp->phy[lp->active].spd.value);
3501         spd &= lp->phy[lp->active].spd.mask;
3502     } else if (!lp->useSROM) {                      /* de500-xa */
3503         spd = ((~gep_rd(dev)) & GEP_SLNK);
3504     } else {
3505         if ((lp->ibn == 2) || !lp->asBitValid)
3506             return ((lp->chipset == DC21143)?(~inl(DE4X5_SISR)&SISR_LS100):0);
3507
3508         spd = (lp->asBitValid & (lp->asPolarity ^ (gep_rd(dev) & lp->asBit))) |
3509                   (lp->linkOK & ~lp->asBitValid);
3510     }
3511     
3512     return spd;
3513 }
3514
3515 static int
3516 is_100_up(struct net_device *dev)
3517 {
3518     struct de4x5_private *lp = netdev_priv(dev);
3519     u_long iobase = dev->base_addr;
3520     
3521     if (lp->useMII) {
3522         /* Double read for sticky bits & temporary drops */
3523         mii_rd(MII_SR, lp->phy[lp->active].addr, DE4X5_MII);
3524         return (mii_rd(MII_SR, lp->phy[lp->active].addr, DE4X5_MII) & MII_SR_LKS);
3525     } else if (!lp->useSROM) {                       /* de500-xa */
3526         return ((~gep_rd(dev)) & GEP_SLNK);
3527     } else {
3528         if ((lp->ibn == 2) || !lp->asBitValid)
3529             return ((lp->chipset == DC21143)?(~inl(DE4X5_SISR)&SISR_LS100):0);
3530
3531         return ((lp->asBitValid&(lp->asPolarity^(gep_rd(dev)&lp->asBit))) |
3532                 (lp->linkOK & ~lp->asBitValid));
3533     }
3534 }
3535
3536 static int
3537 is_10_up(struct net_device *dev)
3538 {
3539     struct de4x5_private *lp = netdev_priv(dev);
3540     u_long iobase = dev->base_addr;
3541     
3542     if (lp->useMII) {
3543         /* Double read for sticky bits & temporary drops */
3544         mii_rd(MII_SR, lp->phy[lp->active].addr, DE4X5_MII);
3545         return (mii_rd(MII_SR, lp->phy[lp->active].addr, DE4X5_MII) & MII_SR_LKS);
3546     } else if (!lp->useSROM) {                       /* de500-xa */
3547         return ((~gep_rd(dev)) & GEP_LNP);
3548     } else {
3549         if ((lp->ibn == 2) || !lp->asBitValid)
3550             return (((lp->chipset & ~0x00ff) == DC2114x) ?
3551                     (~inl(DE4X5_SISR)&SISR_LS10):
3552                     0);
3553
3554         return ((lp->asBitValid&(lp->asPolarity^(gep_rd(dev)&lp->asBit))) |
3555                 (lp->linkOK & ~lp->asBitValid));
3556     }
3557 }
3558
3559 static int
3560 is_anc_capable(struct net_device *dev)
3561 {
3562     struct de4x5_private *lp = netdev_priv(dev);
3563     u_long iobase = dev->base_addr;
3564     
3565     if (lp->phy[lp->active].id && (!lp->useSROM || lp->useMII)) {
3566         return (mii_rd(MII_SR, lp->phy[lp->active].addr, DE4X5_MII));
3567     } else if ((lp->chipset & ~0x00ff) == DC2114x) {
3568         return (inl(DE4X5_SISR) & SISR_LPN) >> 12;
3569     } else {
3570         return 0;
3571     }
3572 }
3573
3574 /*
3575 ** Send a packet onto the media and watch for send errors that indicate the
3576 ** media is bad or unconnected.
3577 */
3578 static int
3579 ping_media(struct net_device *dev, int msec)
3580 {
3581     struct de4x5_private *lp = netdev_priv(dev);
3582     u_long iobase = dev->base_addr;
3583     int sisr;
3584     
3585     if (lp->timeout < 0) {
3586         lp->timeout = msec/100;
3587         
3588         lp->tmp = lp->tx_new;                /* Remember the ring position */
3589         load_packet(dev, lp->frame, TD_LS | TD_FS | sizeof(lp->frame), (struct sk_buff *)1);
3590         lp->tx_new = (++lp->tx_new) % lp->txRingSize;
3591         outl(POLL_DEMAND, DE4X5_TPD);
3592     }
3593     
3594     sisr = inl(DE4X5_SISR);
3595
3596     if ((!(sisr & SISR_NCR)) && 
3597         ((s32)le32_to_cpu(lp->tx_ring[lp->tmp].status) < 0) && 
3598          (--lp->timeout)) {
3599         sisr = 100 | TIMER_CB;
3600     } else {
3601         if ((!(sisr & SISR_NCR)) && 
3602             !(le32_to_cpu(lp->tx_ring[lp->tmp].status) & (T_OWN | TD_ES)) &&
3603             lp->timeout) {
3604             sisr = 0;
3605         } else {
3606             sisr = 1;
3607         }
3608         lp->timeout = -1;
3609     }
3610     
3611     return sisr;
3612 }
3613
3614 /*
3615 ** This function does 2 things: on Intels it kmalloc's another buffer to
3616 ** replace the one about to be passed up. On Alpha's it kmallocs a buffer
3617 ** into which the packet is copied.
3618 */
3619 static struct sk_buff *
3620 de4x5_alloc_rx_buff(struct net_device *dev, int index, int len)
3621 {
3622     struct de4x5_private *lp = netdev_priv(dev);
3623     struct sk_buff *p;
3624
3625 #if !defined(__alpha__) && !defined(__powerpc__) && !defined(__sparc_v9__) && !defined(DE4X5_DO_MEMCPY)
3626     struct sk_buff *ret;
3627     u_long i=0, tmp;
3628
3629     p = dev_alloc_skb(IEEE802_3_SZ + DE4X5_ALIGN + 2);
3630     if (!p) return NULL;
3631
3632     p->dev = dev;
3633     tmp = virt_to_bus(p->data);
3634     i = ((tmp + DE4X5_ALIGN) & ~DE4X5_ALIGN) - tmp;
3635     skb_reserve(p, i);
3636     lp->rx_ring[index].buf = cpu_to_le32(tmp + i);
3637
3638     ret = lp->rx_skb[index];
3639     lp->rx_skb[index] = p;
3640
3641     if ((u_long) ret > 1) {
3642         skb_put(ret, len);
3643     }
3644
3645     return ret;
3646
3647 #else
3648     if (lp->state != OPEN) return (struct sk_buff *)1; /* Fake out the open */
3649
3650     p = dev_alloc_skb(len + 2);
3651     if (!p) return NULL;
3652
3653     p->dev = dev;
3654     skb_reserve(p, 2);                                 /* Align */
3655     if (index < lp->rx_old) {                          /* Wrapped buffer */
3656         short tlen = (lp->rxRingSize - lp->rx_old) * RX_BUFF_SZ;
3657         memcpy(skb_put(p,tlen),lp->rx_bufs + lp->rx_old * RX_BUFF_SZ,tlen);
3658         memcpy(skb_put(p,len-tlen),lp->rx_bufs,len-tlen);
3659     } else {                                           /* Linear buffer */
3660         memcpy(skb_put(p,len),lp->rx_bufs + lp->rx_old * RX_BUFF_SZ,len);
3661     }
3662                     
3663     return p;
3664 #endif
3665 }
3666
3667 static void
3668 de4x5_free_rx_buffs(struct net_device *dev)
3669 {
3670     struct de4x5_private *lp = netdev_priv(dev);
3671     int i;
3672
3673     for (i=0; i<lp->rxRingSize; i++) {
3674         if ((u_long) lp->rx_skb[i] > 1) {
3675             dev_kfree_skb(lp->rx_skb[i]);
3676         }
3677         lp->rx_ring[i].status = 0;
3678         lp->rx_skb[i] = (struct sk_buff *)1;    /* Dummy entry */
3679     }
3680
3681     return;
3682 }
3683
3684 static void
3685 de4x5_free_tx_buffs(struct net_device *dev)
3686 {
3687     struct de4x5_private *lp = netdev_priv(dev);
3688     int i;
3689
3690     for (i=0; i<lp->txRingSize; i++) {
3691         if (lp->tx_skb[i])
3692             de4x5_free_tx_buff(lp, i);
3693         lp->tx_ring[i].status = 0;
3694     }
3695
3696     /* Unload the locally queued packets */
3697     while (lp->cache.skb) {
3698         dev_kfree_skb(de4x5_get_cache(dev));
3699     }
3700
3701     return;
3702 }
3703
3704 /*
3705 ** When a user pulls a connection, the DECchip can end up in a
3706 ** 'running - waiting for end of transmission' state. This means that we
3707 ** have to perform a chip soft reset to ensure that we can synchronize
3708 ** the hardware and software and make any media probes using a loopback
3709 ** packet meaningful.
3710 */
3711 static void
3712 de4x5_save_skbs(struct net_device *dev)
3713 {
3714     struct de4x5_private *lp = netdev_priv(dev);
3715     u_long iobase = dev->base_addr;
3716     s32 omr;
3717
3718     if (!lp->cache.save_cnt) {
3719         STOP_DE4X5;
3720         de4x5_tx(dev);                          /* Flush any sent skb's */
3721         de4x5_free_tx_buffs(dev);
3722         de4x5_cache_state(dev, DE4X5_SAVE_STATE);
3723         de4x5_sw_reset(dev);
3724         de4x5_cache_state(dev, DE4X5_RESTORE_STATE);
3725         lp->cache.save_cnt++;
3726         START_DE4X5;
3727     }
3728
3729     return;
3730 }
3731
3732 static void
3733 de4x5_rst_desc_ring(struct net_device *dev)
3734 {
3735     struct de4x5_private *lp = netdev_priv(dev);
3736     u_long iobase = dev->base_addr;
3737     int i;
3738     s32 omr;
3739
3740     if (lp->cache.save_cnt) {
3741         STOP_DE4X5;
3742         outl(lp->dma_rings, DE4X5_RRBA);
3743         outl(lp->dma_rings + NUM_RX_DESC * sizeof(struct de4x5_desc),
3744              DE4X5_TRBA);
3745     
3746         lp->rx_new = lp->rx_old = 0;
3747         lp->tx_new = lp->tx_old = 0;
3748     
3749         for (i = 0; i < lp->rxRingSize; i++) {
3750             lp->rx_ring[i].status = cpu_to_le32(R_OWN);
3751         }
3752     
3753         for (i = 0; i < lp->txRingSize; i++) {
3754             lp->tx_ring[i].status = cpu_to_le32(0);
3755         }
3756     
3757         barrier();
3758         lp->cache.save_cnt--;
3759         START_DE4X5;
3760     }
3761         
3762     return;
3763 }
3764
3765 static void
3766 de4x5_cache_state(struct net_device *dev, int flag)
3767 {
3768     struct de4x5_private *lp = netdev_priv(dev);
3769     u_long iobase = dev->base_addr;
3770
3771     switch(flag) {
3772       case DE4X5_SAVE_STATE:
3773         lp->cache.csr0 = inl(DE4X5_BMR);
3774         lp->cache.csr6 = (inl(DE4X5_OMR) & ~(OMR_ST | OMR_SR));
3775         lp->cache.csr7 = inl(DE4X5_IMR);
3776         break;
3777
3778       case DE4X5_RESTORE_STATE:
3779         outl(lp->cache.csr0, DE4X5_BMR);
3780         outl(lp->cache.csr6, DE4X5_OMR);
3781         outl(lp->cache.csr7, DE4X5_IMR);
3782         if (lp->chipset == DC21140) {
3783             gep_wr(lp->cache.gepc, dev);
3784             gep_wr(lp->cache.gep, dev);
3785         } else {
3786             reset_init_sia(dev, lp->cache.csr13, lp->cache.csr14, 
3787                                                               lp->cache.csr15);
3788         }
3789         break;
3790     }
3791
3792     return;
3793 }
3794
3795 static void
3796 de4x5_put_cache(struct net_device *dev, struct sk_buff *skb)
3797 {
3798     struct de4x5_private *lp = netdev_priv(dev);
3799     struct sk_buff *p;
3800
3801     if (lp->cache.skb) {
3802         for (p=lp->cache.skb; p->next; p=p->next);
3803         p->next = skb;
3804     } else {
3805         lp->cache.skb = skb;
3806     }
3807     skb->next = NULL;
3808
3809     return;
3810 }
3811
3812 static void
3813 de4x5_putb_cache(struct net_device *dev, struct sk_buff *skb)
3814 {
3815     struct de4x5_private *lp = netdev_priv(dev);
3816     struct sk_buff *p = lp->cache.skb;
3817
3818     lp->cache.skb = skb;
3819     skb->next = p;
3820
3821     return;
3822 }
3823
3824 static struct sk_buff *
3825 de4x5_get_cache(struct net_device *dev)
3826 {
3827     struct de4x5_private *lp = netdev_priv(dev);
3828     struct sk_buff *p = lp->cache.skb;
3829
3830     if (p) {
3831         lp->cache.skb = p->next;
3832         p->next = NULL;
3833     }
3834
3835     return p;
3836 }
3837
3838 /*
3839 ** Check the Auto Negotiation State. Return OK when a link pass interrupt
3840 ** is received and the auto-negotiation status is NWAY OK.
3841 */
3842 static int
3843 test_ans(struct net_device *dev, s32 irqs, s32 irq_mask, s32 msec)
3844 {
3845     struct de4x5_private *lp = netdev_priv(dev);
3846     u_long iobase = dev->base_addr;
3847     s32 sts, ans;
3848     
3849     if (lp->timeout < 0) {
3850         lp->timeout = msec/100;
3851         outl(irq_mask, DE4X5_IMR);
3852         
3853         /* clear all pending interrupts */
3854         sts = inl(DE4X5_STS);
3855         outl(sts, DE4X5_STS);
3856     }
3857     
3858     ans = inl(DE4X5_SISR) & SISR_ANS;
3859     sts = inl(DE4X5_STS) & ~TIMER_CB;
3860     
3861     if (!(sts & irqs) && (ans ^ ANS_NWOK) && --lp->timeout) {
3862         sts = 100 | TIMER_CB;
3863     } else {
3864         lp->timeout = -1;
3865     }
3866     
3867     return sts;
3868 }
3869
3870 static void
3871 de4x5_setup_intr(struct net_device *dev)
3872 {
3873     struct de4x5_private *lp = netdev_priv(dev);
3874     u_long iobase = dev->base_addr;
3875     s32 imr, sts;
3876     
3877     if (inl(DE4X5_OMR) & OMR_SR) {   /* Only unmask if TX/RX is enabled */
3878         imr = 0;
3879         UNMASK_IRQs;
3880         sts = inl(DE4X5_STS);        /* Reset any pending (stale) interrupts */
3881         outl(sts, DE4X5_STS);
3882         ENABLE_IRQs;
3883     }
3884     
3885     return;
3886 }
3887
3888 /*
3889 **
3890 */
3891 static void
3892 reset_init_sia(struct net_device *dev, s32 csr13, s32 csr14, s32 csr15)
3893 {
3894     struct de4x5_private *lp = netdev_priv(dev);
3895     u_long iobase = dev->base_addr;
3896
3897     RESET_SIA;
3898     if (lp->useSROM) {
3899         if (lp->ibn == 3) {
3900             srom_exec(dev, lp->phy[lp->active].rst);
3901             srom_exec(dev, lp->phy[lp->active].gep);
3902             outl(1, DE4X5_SICR);
3903             return;
3904         } else {
3905             csr15 = lp->cache.csr15;
3906             csr14 = lp->cache.csr14;
3907             csr13 = lp->cache.csr13;
3908             outl(csr15 | lp->cache.gepc, DE4X5_SIGR);
3909             outl(csr15 | lp->cache.gep, DE4X5_SIGR);
3910         }
3911     } else {
3912         outl(csr15, DE4X5_SIGR);
3913     }
3914     outl(csr14, DE4X5_STRR);
3915     outl(csr13, DE4X5_SICR);
3916
3917     mdelay(10);
3918
3919     return;
3920 }
3921
3922 /*
3923 ** Create a loopback ethernet packet
3924 */
3925 static void
3926 create_packet(struct net_device *dev, char *frame, int len)
3927 {
3928     int i;
3929     char *buf = frame;
3930     
3931     for (i=0; i<ETH_ALEN; i++) {             /* Use this source address */
3932         *buf++ = dev->dev_addr[i];
3933     }
3934     for (i=0; i<ETH_ALEN; i++) {             /* Use this destination address */
3935         *buf++ = dev->dev_addr[i];
3936     }
3937     
3938     *buf++ = 0;                              /* Packet length (2 bytes) */
3939     *buf++ = 1;
3940     
3941     return;
3942 }
3943
3944 /*
3945 ** Look for a particular board name in the EISA configuration space
3946 */
3947 static int
3948 EISA_signature(char *name, struct device *device)
3949 {
3950     int i, status = 0, siglen = sizeof(de4x5_signatures)/sizeof(c_char *);
3951     struct eisa_device *edev;
3952
3953     *name = '\0';
3954     edev = to_eisa_device (device);
3955     i = edev->id.driver_data;
3956
3957     if (i >= 0 && i < siglen) {
3958             strcpy (name, de4x5_signatures[i]);
3959             status = 1;
3960     }
3961
3962     return status;                         /* return the device name string */
3963 }
3964
3965 /*
3966 ** Look for a particular board name in the PCI configuration space
3967 */
3968 static int
3969 PCI_signature(char *name, struct de4x5_private *lp)
3970 {
3971     int i, status = 0, siglen = sizeof(de4x5_signatures)/sizeof(c_char *);
3972     
3973     if (lp->chipset == DC21040) {
3974         strcpy(name, "DE434/5");
3975         return status;
3976     } else {                           /* Search for a DEC name in the SROM */
3977         int i = *((char *)&lp->srom + 19) * 3;
3978         strncpy(name, (char *)&lp->srom + 26 + i, 8);
3979     }
3980     name[8] = '\0';
3981     for (i=0; i<siglen; i++) {
3982         if (strstr(name,de4x5_signatures[i])!=NULL) break;
3983     }
3984     if (i == siglen) {
3985         if (dec_only) {
3986             *name = '\0';
3987         } else {                        /* Use chip name to avoid confusion */
3988             strcpy(name, (((lp->chipset == DC21040) ? "DC21040" :
3989                            ((lp->chipset == DC21041) ? "DC21041" :
3990                             ((lp->chipset == DC21140) ? "DC21140" :
3991                              ((lp->chipset == DC21142) ? "DC21142" :
3992                               ((lp->chipset == DC21143) ? "DC21143" : "UNKNOWN"
3993                              )))))));
3994         }
3995         if (lp->chipset != DC21041) {
3996             lp->useSROM = TRUE;             /* card is not recognisably DEC */
3997         }
3998     } else if ((lp->chipset & ~0x00ff) == DC2114x) {
3999         lp->useSROM = TRUE;
4000     }
4001     
4002     return status;
4003 }
4004
4005 /*
4006 ** Set up the Ethernet PROM counter to the start of the Ethernet address on
4007 ** the DC21040, else  read the SROM for the other chips.
4008 ** The SROM may not be present in a multi-MAC card, so first read the
4009 ** MAC address and check for a bad address. If there is a bad one then exit
4010 ** immediately with the prior srom contents intact (the h/w address will
4011 ** be fixed up later).
4012 */
4013 static void
4014 DevicePresent(struct net_device *dev, u_long aprom_addr)
4015 {
4016     int i, j=0;
4017     struct de4x5_private *lp = netdev_priv(dev);
4018     
4019     if (lp->chipset == DC21040) {
4020         if (lp->bus == EISA) {
4021             enet_addr_rst(aprom_addr); /* Reset Ethernet Address ROM Pointer */
4022         } else {
4023             outl(0, aprom_addr);       /* Reset Ethernet Address ROM Pointer */
4024         }
4025     } else {                           /* Read new srom */
4026         u_short tmp, *p = (short *)((char *)&lp->srom + SROM_HWADD);
4027         for (i=0; i<(ETH_ALEN>>1); i++) {
4028             tmp = srom_rd(aprom_addr, (SROM_HWADD>>1) + i);
4029             *p = le16_to_cpu(tmp);
4030             j += *p++;
4031         }
4032         if ((j == 0) || (j == 0x2fffd)) {
4033             return;
4034         }
4035
4036         p=(short *)&lp->srom;
4037         for (i=0; i<(sizeof(struct de4x5_srom)>>1); i++) {
4038             tmp = srom_rd(aprom_addr, i);
4039             *p++ = le16_to_cpu(tmp);
4040         }
4041         de4x5_dbg_srom((struct de4x5_srom *)&lp->srom);
4042     }
4043     
4044     return;
4045 }
4046
4047 /*
4048 ** Since the write on the Enet PROM register doesn't seem to reset the PROM
4049 ** pointer correctly (at least on my DE425 EISA card), this routine should do
4050 ** it...from depca.c.
4051 */
4052 static void
4053 enet_addr_rst(u_long aprom_addr)
4054 {
4055     union {
4056         struct {
4057             u32 a;
4058             u32 b;
4059         } llsig;
4060         char Sig[sizeof(u32) << 1];
4061     } dev;
4062     short sigLength=0;
4063     s8 data;
4064     int i, j;
4065     
4066     dev.llsig.a = ETH_PROM_SIG;
4067     dev.llsig.b = ETH_PROM_SIG;
4068     sigLength = sizeof(u32) << 1;
4069     
4070     for (i=0,j=0;j<sigLength && i<PROBE_LENGTH+sigLength-1;i++) {
4071         data = inb(aprom_addr);
4072         if (dev.Sig[j] == data) {    /* track signature */
4073             j++;
4074         } else {                     /* lost signature; begin search again */
4075             if (data == dev.Sig[0]) {  /* rare case.... */
4076                 j=1;
4077             } else {
4078                 j=0;
4079             }
4080         }
4081     }
4082     
4083     return;
4084 }
4085
4086 /*
4087 ** For the bad status case and no SROM, then add one to the previous
4088 ** address. However, need to add one backwards in case we have 0xff
4089 ** as one or more of the bytes. Only the last 3 bytes should be checked
4090 ** as the first three are invariant - assigned to an organisation.
4091 */
4092 static int
4093 get_hw_addr(struct net_device *dev)
4094 {
4095     u_long iobase = dev->base_addr;
4096     int broken, i, k, tmp, status = 0;
4097     u_short j,chksum;
4098     struct de4x5_private *lp = netdev_priv(dev);
4099
4100     broken = de4x5_bad_srom(lp);
4101
4102     for (i=0,k=0,j=0;j<3;j++) {
4103         k <<= 1;
4104         if (k > 0xffff) k-=0xffff;
4105         
4106         if (lp->bus == PCI) {
4107             if (lp->chipset == DC21040) {
4108                 while ((tmp = inl(DE4X5_APROM)) < 0);
4109                 k += (u_char) tmp;
4110                 dev->dev_addr[i++] = (u_char) tmp;
4111                 while ((tmp = inl(DE4X5_APROM)) < 0);
4112                 k += (u_short) (tmp << 8);
4113                 dev->dev_addr[i++] = (u_char) tmp;
4114             } else if (!broken) {
4115                 dev->dev_addr[i] = (u_char) lp->srom.ieee_addr[i]; i++;
4116                 dev->dev_addr[i] = (u_char) lp->srom.ieee_addr[i]; i++;
4117             } else if ((broken == SMC) || (broken == ACCTON)) {
4118                 dev->dev_addr[i] = *((u_char *)&lp->srom + i); i++;
4119                 dev->dev_addr[i] = *((u_char *)&lp->srom + i); i++;
4120             }
4121         } else {
4122             k += (u_char) (tmp = inb(EISA_APROM));
4123             dev->dev_addr[i++] = (u_char) tmp;
4124             k += (u_short) ((tmp = inb(EISA_APROM)) << 8);
4125             dev->dev_addr[i++] = (u_char) tmp;
4126         }
4127         
4128         if (k > 0xffff) k-=0xffff;
4129     }
4130     if (k == 0xffff) k=0;
4131     
4132     if (lp->bus == PCI) {
4133         if (lp->chipset == DC21040) {
4134             while ((tmp = inl(DE4X5_APROM)) < 0);
4135             chksum = (u_char) tmp;
4136             while ((tmp = inl(DE4X5_APROM)) < 0);
4137             chksum |= (u_short) (tmp << 8);
4138             if ((k != chksum) && (dec_only)) status = -1;
4139         }
4140     } else {
4141         chksum = (u_char) inb(EISA_APROM);
4142         chksum |= (u_short) (inb(EISA_APROM) << 8);
4143         if ((k != chksum) && (dec_only)) status = -1;
4144     }
4145
4146     /* If possible, try to fix a broken card - SMC only so far */
4147     srom_repair(dev, broken);
4148
4149 #ifdef CONFIG_PPC_MULTIPLATFORM
4150     /* 
4151     ** If the address starts with 00 a0, we have to bit-reverse
4152     ** each byte of the address.
4153     */
4154     if ( (_machine & _MACH_Pmac) &&
4155          (dev->dev_addr[0] == 0) &&
4156          (dev->dev_addr[1] == 0xa0) )
4157     {
4158             for (i = 0; i < ETH_ALEN; ++i)
4159             {
4160                     int x = dev->dev_addr[i];
4161                     x = ((x & 0xf) << 4) + ((x & 0xf0) >> 4);
4162                     x = ((x & 0x33) << 2) + ((x & 0xcc) >> 2);
4163                     dev->dev_addr[i] = ((x & 0x55) << 1) + ((x & 0xaa) >> 1);
4164             }
4165     }
4166 #endif /* CONFIG_PPC_MULTIPLATFORM */
4167
4168     /* Test for a bad enet address */
4169     status = test_bad_enet(dev, status);
4170
4171     return status;
4172 }
4173
4174 /*
4175 ** Test for enet addresses in the first 32 bytes. The built-in strncmp
4176 ** didn't seem to work here...?
4177 */
4178 static int
4179 de4x5_bad_srom(struct de4x5_private *lp)
4180 {
4181     int i, status = 0;
4182
4183     for (i=0; i<sizeof(enet_det)/ETH_ALEN; i++) {
4184         if (!de4x5_strncmp((char *)&lp->srom, (char *)&enet_det[i], 3) &&
4185             !de4x5_strncmp((char *)&lp->srom+0x10, (char *)&enet_det[i], 3)) {
4186             if (i == 0) {
4187                 status = SMC;
4188             } else if (i == 1) {
4189                 status = ACCTON;
4190             }
4191             break;
4192         }
4193     }
4194
4195     return status;
4196 }
4197
4198 static int
4199 de4x5_strncmp(char *a, char *b, int n)
4200 {
4201     int ret=0;
4202
4203     for (;n && !ret;n--) {
4204         ret = *a++ - *b++;
4205     }
4206
4207     return ret;
4208 }
4209
4210 static void
4211 srom_repair(struct net_device *dev, int card)
4212 {
4213     struct de4x5_private *lp = netdev_priv(dev);
4214
4215     switch(card) {
4216       case SMC:
4217         memset((char *)&lp->srom, 0, sizeof(struct de4x5_srom));
4218         memcpy(lp->srom.ieee_addr, (char *)dev->dev_addr, ETH_ALEN);
4219         memcpy(lp->srom.info, (char *)&srom_repair_info[SMC-1], 100);
4220         lp->useSROM = TRUE;
4221         break;
4222     }
4223
4224     return;
4225 }
4226
4227 /*
4228 ** Assume that the irq's do not follow the PCI spec - this is seems
4229 ** to be true so far (2 for 2).
4230 */
4231 static int
4232 test_bad_enet(struct net_device *dev, int status)
4233 {
4234     struct de4x5_private *lp = netdev_priv(dev);
4235     int i, tmp;
4236
4237     for (tmp=0,i=0; i<ETH_ALEN; i++) tmp += (u_char)dev->dev_addr[i];
4238     if ((tmp == 0) || (tmp == 0x5fa)) {
4239         if ((lp->chipset == last.chipset) && 
4240             (lp->bus_num == last.bus) && (lp->bus_num > 0)) {
4241             for (i=0; i<ETH_ALEN; i++) dev->dev_addr[i] = last.addr[i];
4242             for (i=ETH_ALEN-1; i>2; --i) {
4243                 dev->dev_addr[i] += 1;
4244                 if (dev->dev_addr[i] != 0) break;
4245             }
4246             for (i=0; i<ETH_ALEN; i++) last.addr[i] = dev->dev_addr[i];
4247             if (!an_exception(lp)) {
4248                 dev->irq = last.irq;
4249             }
4250
4251             status = 0;
4252         }
4253     } else if (!status) {
4254         last.chipset = lp->chipset;
4255         last.bus = lp->bus_num;
4256         last.irq = dev->irq;
4257         for (i=0; i<ETH_ALEN; i++) last.addr[i] = dev->dev_addr[i];
4258     }
4259
4260     return status;
4261 }
4262
4263 /*
4264 ** List of board exceptions with correctly wired IRQs
4265 */
4266 static int
4267 an_exception(struct de4x5_private *lp)
4268 {
4269     if ((*(u_short *)lp->srom.sub_vendor_id == 0x00c0) && 
4270         (*(u_short *)lp->srom.sub_system_id == 0x95e0)) {
4271         return -1;
4272     }
4273
4274     return 0;
4275 }
4276
4277 /*
4278 ** SROM Read
4279 */
4280 static short
4281 srom_rd(u_long addr, u_char offset)
4282 {
4283     sendto_srom(SROM_RD | SROM_SR, addr);
4284     
4285     srom_latch(SROM_RD | SROM_SR | DT_CS, addr);
4286     srom_command(SROM_RD | SROM_SR | DT_IN | DT_CS, addr);
4287     srom_address(SROM_RD | SROM_SR | DT_CS, addr, offset);
4288     
4289     return srom_data(SROM_RD | SROM_SR | DT_CS, addr);
4290 }
4291
4292 static void
4293 srom_latch(u_int command, u_long addr)
4294 {
4295     sendto_srom(command, addr);
4296     sendto_srom(command | DT_CLK, addr);
4297     sendto_srom(command, addr);
4298     
4299     return;
4300 }
4301
4302 static void
4303 srom_command(u_int command, u_long addr)
4304 {
4305     srom_latch(command, addr);
4306     srom_latch(command, addr);
4307     srom_latch((command & 0x0000ff00) | DT_CS, addr);
4308     
4309     return;
4310 }
4311
4312 static void
4313 srom_address(u_int command, u_long addr, u_char offset)
4314 {
4315     int i, a;
4316     
4317     a = offset << 2;
4318     for (i=0; i<6; i++, a <<= 1) {
4319         srom_latch(command | ((a & 0x80) ? DT_IN : 0), addr);
4320     }
4321     udelay(1);
4322     
4323     i = (getfrom_srom(addr) >> 3) & 0x01;
4324     
4325     return;
4326 }
4327
4328 static short
4329 srom_data(u_int command, u_long addr)
4330 {
4331     int i;
4332     short word = 0;
4333     s32 tmp;
4334     
4335     for (i=0; i<16; i++) {
4336         sendto_srom(command  | DT_CLK, addr);
4337         tmp = getfrom_srom(addr);
4338         sendto_srom(command, addr);
4339         
4340         word = (word << 1) | ((tmp >> 3) & 0x01);
4341     }
4342     
4343     sendto_srom(command & 0x0000ff00, addr);
4344     
4345     return word;
4346 }
4347
4348 /*
4349 static void
4350 srom_busy(u_int command, u_long addr)
4351 {
4352    sendto_srom((command & 0x0000ff00) | DT_CS, addr);
4353    
4354    while (!((getfrom_srom(addr) >> 3) & 0x01)) {
4355        mdelay(1);
4356    }
4357    
4358    sendto_srom(command & 0x0000ff00, addr);
4359    
4360    return;
4361 }
4362 */
4363
4364 static void
4365 sendto_srom(u_int command, u_long addr)
4366 {
4367     outl(command, addr);
4368     udelay(1);
4369     
4370     return;
4371 }
4372
4373 static int
4374 getfrom_srom(u_long addr)
4375 {
4376     s32 tmp;
4377     
4378     tmp = inl(addr);
4379     udelay(1);
4380     
4381     return tmp;
4382 }
4383
4384 static int
4385 srom_infoleaf_info(struct net_device *dev)
4386 {
4387     struct de4x5_private *lp = netdev_priv(dev);
4388     int i, count;
4389     u_char *p;
4390
4391     /* Find the infoleaf decoder function that matches this chipset */
4392     for (i=0; i<INFOLEAF_SIZE; i++) {
4393         if (lp->chipset == infoleaf_array[i].chipset) break;
4394     }
4395     if (i == INFOLEAF_SIZE) {
4396         lp->useSROM = FALSE;
4397         printk("%s: Cannot find correct chipset for SROM decoding!\n", 
4398                                                                   dev->name);
4399         return -ENXIO;
4400     }
4401
4402     lp->infoleaf_fn = infoleaf_array[i].fn;
4403
4404     /* Find the information offset that this function should use */
4405     count = *((u_char *)&lp->srom + 19);
4406     p  = (u_char *)&lp->srom + 26;
4407
4408     if (count > 1) {
4409         for (i=count; i; --i, p+=3) {
4410             if (lp->device == *p) break;
4411         }
4412         if (i == 0) {
4413             lp->useSROM = FALSE;
4414             printk("%s: Cannot find correct PCI device [%d] for SROM decoding!\n", 
4415                                                        dev->name, lp->device);
4416             return -ENXIO;
4417         }
4418     }
4419
4420     lp->infoleaf_offset = TWIDDLE(p+1);
4421
4422     return 0;
4423 }
4424
4425 /*
4426 ** This routine loads any type 1 or 3 MII info into the mii device
4427 ** struct and executes any type 5 code to reset PHY devices for this
4428 ** controller.
4429 ** The info for the MII devices will be valid since the index used
4430 ** will follow the discovery process from MII address 1-31 then 0.
4431 */
4432 static void
4433 srom_init(struct net_device *dev)
4434 {
4435     struct de4x5_private *lp = netdev_priv(dev);
4436     u_char *p = (u_char *)&lp->srom + lp->infoleaf_offset;
4437     u_char count;
4438
4439     p+=2;
4440     if (lp->chipset == DC21140) {
4441         lp->cache.gepc = (*p++ | GEP_CTRL);
4442         gep_wr(lp->cache.gepc, dev);
4443     }
4444
4445     /* Block count */
4446     count = *p++;
4447
4448     /* Jump the infoblocks to find types */
4449     for (;count; --count) {
4450         if (*p < 128) {
4451             p += COMPACT_LEN;
4452         } else if (*(p+1) == 5) {
4453             type5_infoblock(dev, 1, p);
4454             p += ((*p & BLOCK_LEN) + 1);
4455         } else if (*(p+1) == 4) {
4456             p += ((*p & BLOCK_LEN) + 1);
4457         } else if (*(p+1) == 3) {
4458             type3_infoblock(dev, 1, p);
4459             p += ((*p & BLOCK_LEN) + 1);
4460         } else if (*(p+1) == 2) {
4461             p += ((*p & BLOCK_LEN) + 1);
4462         } else if (*(p+1) == 1) {
4463             type1_infoblock(dev, 1, p);
4464             p += ((*p & BLOCK_LEN) + 1);
4465         } else {
4466             p += ((*p & BLOCK_LEN) + 1);
4467         }
4468     }
4469
4470     return;
4471 }
4472
4473 /*
4474 ** A generic routine that writes GEP control, data and reset information
4475 ** to the GEP register (21140) or csr15 GEP portion (2114[23]).
4476 */
4477 static void
4478 srom_exec(struct net_device *dev, u_char *p)
4479 {
4480     struct de4x5_private *lp = netdev_priv(dev);
4481     u_long iobase = dev->base_addr;
4482     u_char count = (p ? *p++ : 0);
4483     u_short *w = (u_short *)p;
4484
4485     if (((lp->ibn != 1) && (lp->ibn != 3) && (lp->ibn != 5)) || !count) return;
4486
4487     if (lp->chipset != DC21140) RESET_SIA;
4488  
4489     while (count--) {
4490         gep_wr(((lp->chipset==DC21140) && (lp->ibn!=5) ? 
4491                                                    *p++ : TWIDDLE(w++)), dev);
4492         mdelay(2);                          /* 2ms per action */
4493     }
4494
4495     if (lp->chipset != DC21140) {
4496         outl(lp->cache.csr14, DE4X5_STRR);
4497         outl(lp->cache.csr13, DE4X5_SICR);
4498     }
4499
4500     return;
4501 }
4502
4503 /*
4504 ** Basically this function is a NOP since it will never be called,
4505 ** unless I implement the DC21041 SROM functions. There's no need
4506 ** since the existing code will be satisfactory for all boards.
4507 */
4508 static int 
4509 dc21041_infoleaf(struct net_device *dev)
4510 {
4511     return DE4X5_AUTOSENSE_MS;
4512 }
4513
4514 static int 
4515 dc21140_infoleaf(struct net_device *dev)
4516 {
4517     struct de4x5_private *lp = netdev_priv(dev);
4518     u_char count = 0;
4519     u_char *p = (u_char *)&lp->srom + lp->infoleaf_offset;
4520     int next_tick = DE4X5_AUTOSENSE_MS;
4521
4522     /* Read the connection type */
4523     p+=2;
4524
4525     /* GEP control */
4526     lp->cache.gepc = (*p++ | GEP_CTRL);
4527
4528     /* Block count */
4529     count = *p++;
4530
4531     /* Recursively figure out the info blocks */
4532     if (*p < 128) {
4533         next_tick = dc_infoblock[COMPACT](dev, count, p);
4534     } else {
4535         next_tick = dc_infoblock[*(p+1)](dev, count, p);
4536     }
4537
4538     if (lp->tcount == count) {
4539         lp->media = NC;
4540         if (lp->media != lp->c_media) {
4541             de4x5_dbg_media(dev);
4542             lp->c_media = lp->media;
4543         }
4544         lp->media = INIT;
4545         lp->tcount = 0;
4546         lp->tx_enable = FALSE;
4547     }
4548
4549     return next_tick & ~TIMER_CB;
4550 }
4551
4552 static int 
4553 dc21142_infoleaf(struct net_device *dev)
4554 {
4555     struct de4x5_private *lp = netdev_priv(dev);
4556     u_char count = 0;
4557     u_char *p = (u_char *)&lp->srom + lp->infoleaf_offset;
4558     int next_tick = DE4X5_AUTOSENSE_MS;
4559
4560     /* Read the connection type */
4561     p+=2;
4562
4563     /* Block count */
4564     count = *p++;
4565
4566     /* Recursively figure out the info blocks */
4567     if (*p < 128) {
4568         next_tick = dc_infoblock[COMPACT](dev, count, p);
4569     } else {
4570         next_tick = dc_infoblock[*(p+1)](dev, count, p);
4571     }
4572
4573     if (lp->tcount == count) {
4574         lp->media = NC;
4575         if (lp->media != lp->c_media) {
4576             de4x5_dbg_media(dev);
4577             lp->c_media = lp->media;
4578         }
4579         lp->media = INIT;
4580         lp->tcount = 0;
4581         lp->tx_enable = FALSE;
4582     }
4583
4584     return next_tick & ~TIMER_CB;
4585 }
4586
4587 static int 
4588 dc21143_infoleaf(struct net_device *dev)
4589 {
4590     struct de4x5_private *lp = netdev_priv(dev);
4591     u_char count = 0;
4592     u_char *p = (u_char *)&lp->srom + lp->infoleaf_offset;
4593     int next_tick = DE4X5_AUTOSENSE_MS;
4594
4595     /* Read the connection type */
4596     p+=2;
4597
4598     /* Block count */
4599     count = *p++;
4600
4601     /* Recursively figure out the info blocks */
4602     if (*p < 128) {
4603         next_tick = dc_infoblock[COMPACT](dev, count, p);
4604     } else {
4605         next_tick = dc_infoblock[*(p+1)](dev, count, p);
4606     }
4607     if (lp->tcount == count) {
4608         lp->media = NC;
4609         if (lp->media != lp->c_media) {
4610             de4x5_dbg_media(dev);
4611             lp->c_media = lp->media;
4612         }
4613         lp->media = INIT;
4614         lp->tcount = 0;
4615         lp->tx_enable = FALSE;
4616     }
4617
4618     return next_tick & ~TIMER_CB;
4619 }
4620
4621 /*
4622 ** The compact infoblock is only designed for DC21140[A] chips, so
4623 ** we'll reuse the dc21140m_autoconf function. Non MII media only.
4624 */
4625 static int 
4626 compact_infoblock(struct net_device *dev, u_char count, u_char *p)
4627 {
4628     struct de4x5_private *lp = netdev_priv(dev);
4629     u_char flags, csr6;
4630
4631     /* Recursively figure out the info blocks */
4632     if (--count > lp->tcount) {
4633         if (*(p+COMPACT_LEN) < 128) {
4634             return dc_infoblock[COMPACT](dev, count, p+COMPACT_LEN);
4635         } else {
4636             return dc_infoblock[*(p+COMPACT_LEN+1)](dev, count, p+COMPACT_LEN);
4637         }
4638     }
4639
4640     if ((lp->media == INIT) && (lp->timeout < 0)) {
4641         lp->ibn = COMPACT;
4642         lp->active = 0;
4643         gep_wr(lp->cache.gepc, dev);
4644         lp->infoblock_media = (*p++) & COMPACT_MC;
4645         lp->cache.gep = *p++;
4646         csr6 = *p++;
4647         flags = *p++;
4648
4649         lp->asBitValid = (flags & 0x80) ? 0 : -1;
4650         lp->defMedium = (flags & 0x40) ? -1 : 0;
4651         lp->asBit = 1 << ((csr6 >> 1) & 0x07);
4652         lp->asPolarity = ((csr6 & 0x80) ? -1 : 0) & lp->asBit;
4653         lp->infoblock_csr6 = OMR_DEF | ((csr6 & 0x71) << 18);
4654         lp->useMII = FALSE;
4655
4656         de4x5_switch_mac_port(dev);
4657     }
4658
4659     return dc21140m_autoconf(dev);
4660 }
4661
4662 /*
4663 ** This block describes non MII media for the DC21140[A] only.
4664 */
4665 static int 
4666 type0_infoblock(struct net_device *dev, u_char count, u_char *p)
4667 {
4668     struct de4x5_private *lp = netdev_priv(dev);
4669     u_char flags, csr6, len = (*p & BLOCK_LEN)+1;
4670
4671     /* Recursively figure out the info blocks */
4672     if (--count > lp->tcount) {
4673         if (*(p+len) < 128) {
4674             return dc_infoblock[COMPACT](dev, count, p+len);
4675         } else {
4676             return dc_infoblock[*(p+len+1)](dev, count, p+len);
4677         }
4678     }
4679
4680     if ((lp->media == INIT) && (lp->timeout < 0)) {
4681         lp->ibn = 0;
4682         lp->active = 0;
4683         gep_wr(lp->cache.gepc, dev);
4684         p+=2;
4685         lp->infoblock_media = (*p++) & BLOCK0_MC;
4686         lp->cache.gep = *p++;
4687         csr6 = *p++;
4688         flags = *p++;
4689
4690         lp->asBitValid = (flags & 0x80) ? 0 : -1;
4691         lp->defMedium = (flags & 0x40) ? -1 : 0;
4692         lp->asBit = 1 << ((csr6 >> 1) & 0x07);
4693         lp->asPolarity = ((csr6 & 0x80) ? -1 : 0) & lp->asBit;
4694         lp->infoblock_csr6 = OMR_DEF | ((csr6 & 0x71) << 18);
4695         lp->useMII = FALSE;
4696
4697         de4x5_switch_mac_port(dev);
4698     }
4699
4700     return dc21140m_autoconf(dev);
4701 }
4702
4703 /* These functions are under construction! */
4704
4705 static int 
4706 type1_infoblock(struct net_device *dev, u_char count, u_char *p)
4707 {
4708     struct de4x5_private *lp = netdev_priv(dev);
4709     u_char len = (*p & BLOCK_LEN)+1;
4710
4711     /* Recursively figure out the info blocks */
4712     if (--count > lp->tcount) {
4713         if (*(p+len) < 128) {
4714             return dc_infoblock[COMPACT](dev, count, p+len);
4715         } else {
4716             return dc_infoblock[*(p+len+1)](dev, count, p+len);
4717         }
4718     }
4719
4720     p += 2;
4721     if (lp->state == INITIALISED) {
4722         lp->ibn = 1;
4723         lp->active = *p++;
4724         lp->phy[lp->active].gep = (*p ? p : 0); p += (*p + 1);
4725         lp->phy[lp->active].rst = (*p ? p : 0); p += (*p + 1);
4726         lp->phy[lp->active].mc  = TWIDDLE(p); p += 2;
4727         lp->phy[lp->active].ana = TWIDDLE(p); p += 2;
4728         lp->phy[lp->active].fdx = TWIDDLE(p); p += 2;
4729         lp->phy[lp->active].ttm = TWIDDLE(p);
4730         return 0;
4731     } else if ((lp->media == INIT) && (lp->timeout < 0)) {
4732         lp->ibn = 1;
4733         lp->active = *p;
4734         lp->infoblock_csr6 = OMR_MII_100;
4735         lp->useMII = TRUE;
4736         lp->infoblock_media = ANS;
4737
4738         de4x5_switch_mac_port(dev);
4739     }
4740
4741     return dc21140m_autoconf(dev);
4742 }
4743
4744 static int 
4745 type2_infoblock(struct net_device *dev, u_char count, u_char *p)
4746 {
4747     struct de4x5_private *lp = netdev_priv(dev);
4748     u_char len = (*p & BLOCK_LEN)+1;
4749
4750     /* Recursively figure out the info blocks */
4751     if (--count > lp->tcount) {
4752         if (*(p+len) < 128) {
4753             return dc_infoblock[COMPACT](dev, count, p+len);
4754         } else {
4755             return dc_infoblock[*(p+len+1)](dev, count, p+len);
4756         }
4757     }
4758
4759     if ((lp->media == INIT) && (lp->timeout < 0)) {
4760         lp->ibn = 2;
4761         lp->active = 0;
4762         p += 2;
4763         lp->infoblock_media = (*p) & MEDIA_CODE;
4764
4765         if ((*p++) & EXT_FIELD) {
4766             lp->cache.csr13 = TWIDDLE(p); p += 2;
4767             lp->cache.csr14 = TWIDDLE(p); p += 2;
4768             lp->cache.csr15 = TWIDDLE(p); p += 2;
4769         } else {
4770             lp->cache.csr13 = CSR13;
4771             lp->cache.csr14 = CSR14;
4772             lp->cache.csr15 = CSR15;
4773         }
4774         lp->cache.gepc = ((s32)(TWIDDLE(p)) << 16); p += 2;
4775         lp->cache.gep  = ((s32)(TWIDDLE(p)) << 16);
4776         lp->infoblock_csr6 = OMR_SIA;
4777         lp->useMII = FALSE;
4778
4779         de4x5_switch_mac_port(dev);
4780     }
4781
4782     return dc2114x_autoconf(dev);
4783 }
4784
4785 static int 
4786 type3_infoblock(struct net_device *dev, u_char count, u_char *p)
4787 {
4788     struct de4x5_private *lp = netdev_priv(dev);
4789     u_char len = (*p & BLOCK_LEN)+1;
4790
4791     /* Recursively figure out the info blocks */
4792     if (--count > lp->tcount) {
4793         if (*(p+len) < 128) {
4794             return dc_infoblock[COMPACT](dev, count, p+len);
4795         } else {
4796             return dc_infoblock[*(p+len+1)](dev, count, p+len);
4797         }
4798     }
4799
4800     p += 2;
4801     if (lp->state == INITIALISED) {
4802         lp->ibn = 3;
4803         lp->active = *p++;
4804         if (MOTO_SROM_BUG) lp->active = 0;
4805         lp->phy[lp->active].gep = (*p ? p : 0); p += (2 * (*p) + 1);
4806         lp->phy[lp->active].rst = (*p ? p : 0); p += (2 * (*p) + 1);
4807         lp->phy[lp->active].mc  = TWIDDLE(p); p += 2;
4808         lp->phy[lp->active].ana = TWIDDLE(p); p += 2;
4809         lp->phy[lp->active].fdx = TWIDDLE(p); p += 2;
4810         lp->phy[lp->active].ttm = TWIDDLE(p); p += 2;
4811         lp->phy[lp->active].mci = *p;
4812         return 0;
4813     } else if ((lp->media == INIT) && (lp->timeout < 0)) {
4814         lp->ibn = 3;
4815         lp->active = *p;
4816         if (MOTO_SROM_BUG) lp->active = 0;
4817         lp->infoblock_csr6 = OMR_MII_100;
4818         lp->useMII = TRUE;
4819         lp->infoblock_media = ANS;
4820
4821         de4x5_switch_mac_port(dev);
4822     }
4823
4824     return dc2114x_autoconf(dev);
4825 }
4826
4827 static int 
4828 type4_infoblock(struct net_device *dev, u_char count, u_char *p)
4829 {
4830     struct de4x5_private *lp = netdev_priv(dev);
4831     u_char flags, csr6, len = (*p & BLOCK_LEN)+1;
4832
4833     /* Recursively figure out the info blocks */
4834     if (--count > lp->tcount) {
4835         if (*(p+len) < 128) {
4836             return dc_infoblock[COMPACT](dev, count, p+len);
4837         } else {
4838             return dc_infoblock[*(p+len+1)](dev, count, p+len);
4839         }
4840     }
4841
4842     if ((lp->media == INIT) && (lp->timeout < 0)) {
4843         lp->ibn = 4;
4844         lp->active = 0;
4845         p+=2;
4846         lp->infoblock_media = (*p++) & MEDIA_CODE;
4847         lp->cache.csr13 = CSR13;              /* Hard coded defaults */
4848         lp->cache.csr14 = CSR14;
4849         lp->cache.csr15 = CSR15;
4850         lp->cache.gepc = ((s32)(TWIDDLE(p)) << 16); p += 2;
4851         lp->cache.gep  = ((s32)(TWIDDLE(p)) << 16); p += 2;
4852         csr6 = *p++;
4853         flags = *p++;
4854
4855         lp->asBitValid = (flags & 0x80) ? 0 : -1;
4856         lp->defMedium = (flags & 0x40) ? -1 : 0;
4857         lp->asBit = 1 << ((csr6 >> 1) & 0x07);
4858         lp->asPolarity = ((csr6 & 0x80) ? -1 : 0) & lp->asBit;
4859         lp->infoblock_csr6 = OMR_DEF | ((csr6 & 0x71) << 18);
4860         lp->useMII = FALSE;
4861
4862         de4x5_switch_mac_port(dev);
4863     }
4864
4865     return dc2114x_autoconf(dev);
4866 }
4867
4868 /*
4869 ** This block type provides information for resetting external devices
4870 ** (chips) through the General Purpose Register.
4871 */
4872 static int 
4873 type5_infoblock(struct net_device *dev, u_char count, u_char *p)
4874 {
4875     struct de4x5_private *lp = netdev_priv(dev);
4876     u_char len = (*p & BLOCK_LEN)+1;
4877
4878     /* Recursively figure out the info blocks */
4879     if (--count > lp->tcount) {
4880         if (*(p+len) < 128) {
4881             return dc_infoblock[COMPACT](dev, count, p+len);
4882         } else {
4883             return dc_infoblock[*(p+len+1)](dev, count, p+len);
4884         }
4885     }
4886
4887     /* Must be initializing to run this code */
4888     if ((lp->state == INITIALISED) || (lp->media == INIT)) {
4889         p+=2;
4890         lp->rst = p;
4891         srom_exec(dev, lp->rst);
4892     }
4893
4894     return DE4X5_AUTOSENSE_MS;
4895 }
4896
4897 /*
4898 ** MII Read/Write
4899 */
4900
4901 static int
4902 mii_rd(u_char phyreg, u_char phyaddr, u_long ioaddr)
4903 {
4904     mii_wdata(MII_PREAMBLE,  2, ioaddr);   /* Start of 34 bit preamble...    */
4905     mii_wdata(MII_PREAMBLE, 32, ioaddr);   /* ...continued                   */
4906     mii_wdata(MII_STRD, 4, ioaddr);        /* SFD and Read operation         */
4907     mii_address(phyaddr, ioaddr);          /* PHY address to be accessed     */
4908     mii_address(phyreg, ioaddr);           /* PHY Register to read           */
4909     mii_ta(MII_STRD, ioaddr);              /* Turn around time - 2 MDC       */
4910     
4911     return mii_rdata(ioaddr);              /* Read data                      */
4912 }
4913
4914 static void
4915 mii_wr(int data, u_char phyreg, u_char phyaddr, u_long ioaddr)
4916 {
4917     mii_wdata(MII_PREAMBLE,  2, ioaddr);   /* Start of 34 bit preamble...    */
4918     mii_wdata(MII_PREAMBLE, 32, ioaddr);   /* ...continued                   */
4919     mii_wdata(MII_STWR, 4, ioaddr);        /* SFD and Write operation        */
4920     mii_address(phyaddr, ioaddr);          /* PHY address to be accessed     */
4921     mii_address(phyreg, ioaddr);           /* PHY Register to write          */
4922     mii_ta(MII_STWR, ioaddr);              /* Turn around time - 2 MDC       */
4923     data = mii_swap(data, 16);             /* Swap data bit ordering         */
4924     mii_wdata(data, 16, ioaddr);           /* Write data                     */
4925     
4926     return;
4927 }
4928
4929 static int
4930 mii_rdata(u_long ioaddr)
4931 {
4932     int i;
4933     s32 tmp = 0;
4934     
4935     for (i=0; i<16; i++) {
4936         tmp <<= 1;
4937         tmp |= getfrom_mii(MII_MRD | MII_RD, ioaddr);
4938     }
4939     
4940     return tmp;
4941 }
4942
4943 static void
4944 mii_wdata(int data, int len, u_long ioaddr)
4945 {
4946     int i;
4947     
4948     for (i=0; i<len; i++) {
4949         sendto_mii(MII_MWR | MII_WR, data, ioaddr);
4950         data >>= 1;
4951     }
4952     
4953     return;
4954 }
4955
4956 static void
4957 mii_address(u_char addr, u_long ioaddr)
4958 {
4959     int i;
4960     
4961     addr = mii_swap(addr, 5);
4962     for (i=0; i<5; i++) {
4963         sendto_mii(MII_MWR | MII_WR, addr, ioaddr);
4964         addr >>= 1;
4965     }
4966     
4967     return;
4968 }
4969
4970 static void
4971 mii_ta(u_long rw, u_long ioaddr)
4972 {
4973     if (rw == MII_STWR) {
4974         sendto_mii(MII_MWR | MII_WR, 1, ioaddr);  
4975         sendto_mii(MII_MWR | MII_WR, 0, ioaddr);  
4976     } else {
4977         getfrom_mii(MII_MRD | MII_RD, ioaddr);        /* Tri-state MDIO */
4978     }
4979     
4980     return;
4981 }
4982
4983 static int
4984 mii_swap(int data, int len)
4985 {
4986     int i, tmp = 0;
4987     
4988     for (i=0; i<len; i++) {
4989         tmp <<= 1;
4990         tmp |= (data & 1);
4991         data >>= 1;
4992     }
4993     
4994     return tmp;
4995 }
4996
4997 static void
4998 sendto_mii(u32 command, int data, u_long ioaddr)
4999 {
5000     u32 j;
5001     
5002     j = (data & 1) << 17;
5003     outl(command | j, ioaddr);
5004     udelay(1);
5005     outl(command | MII_MDC | j, ioaddr);
5006     udelay(1);
5007     
5008     return;
5009 }
5010
5011 static int
5012 getfrom_mii(u32 command, u_long ioaddr)
5013 {
5014     outl(command, ioaddr);
5015     udelay(1);
5016     outl(command | MII_MDC, ioaddr);
5017     udelay(1);
5018     
5019     return ((inl(ioaddr) >> 19) & 1);
5020 }
5021
5022 /*
5023 ** Here's 3 ways to calculate the OUI from the ID registers.
5024 */
5025 static int
5026 mii_get_oui(u_char phyaddr, u_long ioaddr)
5027 {
5028 /*
5029     union {
5030         u_short reg;
5031         u_char breg[2];
5032     } a;
5033     int i, r2, r3, ret=0;*/
5034     int r2, r3;
5035
5036     /* Read r2 and r3 */
5037     r2 = mii_rd(MII_ID0, phyaddr, ioaddr);
5038     r3 = mii_rd(MII_ID1, phyaddr, ioaddr);
5039                                                 /* SEEQ and Cypress way * /
5040     / * Shuffle r2 and r3 * /
5041     a.reg=0;
5042     r3 = ((r3>>10)|(r2<<6))&0x0ff;
5043     r2 = ((r2>>2)&0x3fff);
5044
5045     / * Bit reverse r3 * /
5046     for (i=0;i<8;i++) {
5047         ret<<=1;
5048         ret |= (r3&1);
5049         r3>>=1;
5050     }
5051
5052     / * Bit reverse r2 * /
5053     for (i=0;i<16;i++) {
5054         a.reg<<=1;
5055         a.reg |= (r2&1);
5056         r2>>=1;
5057     }
5058
5059     / * Swap r2 bytes * /
5060     i=a.breg[0];
5061     a.breg[0]=a.breg[1];
5062     a.breg[1]=i;
5063
5064     return ((a.reg<<8)|ret); */                 /* SEEQ and Cypress way */
5065 /*    return ((r2<<6)|(u_int)(r3>>10)); */      /* NATIONAL and BROADCOM way */
5066     return r2;                                  /* (I did it) My way */
5067 }
5068
5069 /*
5070 ** The SROM spec forces us to search addresses [1-31 0]. Bummer.
5071 */
5072 static int
5073 mii_get_phy(struct net_device *dev)
5074 {
5075     struct de4x5_private *lp = netdev_priv(dev);
5076     u_long iobase = dev->base_addr;
5077     int i, j, k, n, limit=sizeof(phy_info)/sizeof(struct phy_table);
5078     int id;
5079     
5080     lp->active = 0;
5081     lp->useMII = TRUE;
5082
5083     /* Search the MII address space for possible PHY devices */
5084     for (n=0, lp->mii_cnt=0, i=1; !((i==1) && (n==1)); i=(++i)%DE4X5_MAX_MII) {
5085         lp->phy[lp->active].addr = i;
5086         if (i==0) n++;                             /* Count cycles */
5087         while (de4x5_reset_phy(dev)<0) udelay(100);/* Wait for reset */
5088         id = mii_get_oui(i, DE4X5_MII); 
5089         if ((id == 0) || (id == 65535)) continue;  /* Valid ID? */
5090         for (j=0; j<limit; j++) {                  /* Search PHY table */
5091             if (id != phy_info[j].id) continue;    /* ID match? */
5092             for (k=0; lp->phy[k].id && (k < DE4X5_MAX_PHY); k++);
5093             if (k < DE4X5_MAX_PHY) {
5094                 memcpy((char *)&lp->phy[k],
5095                        (char *)&phy_info[j], sizeof(struct phy_table));
5096                 lp->phy[k].addr = i;
5097                 lp->mii_cnt++;
5098                 lp->active++;
5099             } else {
5100                 goto purgatory;                    /* Stop the search */
5101             }
5102             break;
5103         }
5104         if ((j == limit) && (i < DE4X5_MAX_MII)) {
5105             for (k=0; lp->phy[k].id && (k < DE4X5_MAX_PHY); k++);
5106             lp->phy[k].addr = i;
5107             lp->phy[k].id = id;
5108             lp->phy[k].spd.reg = GENERIC_REG;      /* ANLPA register         */
5109             lp->phy[k].spd.mask = GENERIC_MASK;    /* 100Mb/s technologies   */
5110             lp->phy[k].spd.value = GENERIC_VALUE;  /* TX & T4, H/F Duplex    */
5111             lp->mii_cnt++;
5112             lp->active++;
5113             printk("%s: Using generic MII device control. If the board doesn't operate, \nplease mail the following dump to the author:\n", dev->name);
5114             j = de4x5_debug;
5115             de4x5_debug |= DEBUG_MII;
5116             de4x5_dbg_mii(dev, k);
5117             de4x5_debug = j;
5118             printk("\n");
5119         }
5120     }
5121   purgatory:
5122     lp->active = 0;
5123     if (lp->phy[0].id) {                           /* Reset the PHY devices */
5124         for (k=0; lp->phy[k].id && (k < DE4X5_MAX_PHY); k++) { /*For each PHY*/
5125             mii_wr(MII_CR_RST, MII_CR, lp->phy[k].addr, DE4X5_MII);
5126             while (mii_rd(MII_CR, lp->phy[k].addr, DE4X5_MII) & MII_CR_RST);
5127             
5128             de4x5_dbg_mii(dev, k);
5129         }
5130     }
5131     if (!lp->mii_cnt) lp->useMII = FALSE;
5132
5133     return lp->mii_cnt;
5134 }
5135
5136 static char *
5137 build_setup_frame(struct net_device *dev, int mode)
5138 {
5139     struct de4x5_private *lp = netdev_priv(dev);
5140     int i;
5141     char *pa = lp->setup_frame;
5142     
5143     /* Initialise the setup frame */
5144     if (mode == ALL) {
5145         memset(lp->setup_frame, 0, SETUP_FRAME_LEN);
5146     }
5147     
5148     if (lp->setup_f == HASH_PERF) {
5149         for (pa=lp->setup_frame+IMPERF_PA_OFFSET, i=0; i<ETH_ALEN; i++) {
5150             *(pa + i) = dev->dev_addr[i];                 /* Host address */
5151             if (i & 0x01) pa += 2;
5152         }
5153         *(lp->setup_frame + (HASH_TABLE_LEN >> 3) - 3) = 0x80;
5154     } else {
5155         for (i=0; i<ETH_ALEN; i++) { /* Host address */
5156             *(pa + (i&1)) = dev->dev_addr[i];
5157             if (i & 0x01) pa += 4;
5158         }
5159         for (i=0; i<ETH_ALEN; i++) { /* Broadcast address */
5160             *(pa + (i&1)) = (char) 0xff;
5161             if (i & 0x01) pa += 4;
5162         }
5163     }
5164     
5165     return pa;                     /* Points to the next entry */
5166 }
5167
5168 static void
5169 enable_ast(struct net_device *dev, u32 time_out)
5170 {
5171     timeout(dev, (void *)&de4x5_ast, (u_long)dev, time_out);
5172     
5173     return;
5174 }
5175
5176 static void
5177 disable_ast(struct net_device *dev)
5178 {
5179     struct de4x5_private *lp = netdev_priv(dev);
5180     
5181     del_timer(&lp->timer);
5182     
5183     return;
5184 }
5185
5186 static long
5187 de4x5_switch_mac_port(struct net_device *dev)
5188 {
5189     struct de4x5_private *lp = netdev_priv(dev);
5190     u_long iobase = dev->base_addr;
5191     s32 omr;
5192
5193     STOP_DE4X5;
5194
5195     /* Assert the OMR_PS bit in CSR6 */
5196     omr = (inl(DE4X5_OMR) & ~(OMR_PS | OMR_HBD | OMR_TTM | OMR_PCS | OMR_SCR |
5197                                                                      OMR_FDX));
5198     omr |= lp->infoblock_csr6;
5199     if (omr & OMR_PS) omr |= OMR_HBD;
5200     outl(omr, DE4X5_OMR);
5201     
5202     /* Soft Reset */
5203     RESET_DE4X5;
5204     
5205     /* Restore the GEP - especially for COMPACT and Type 0 Infoblocks */
5206     if (lp->chipset == DC21140) {
5207         gep_wr(lp->cache.gepc, dev);
5208         gep_wr(lp->cache.gep, dev);
5209     } else if ((lp->chipset & ~0x0ff) == DC2114x) {
5210         reset_init_sia(dev, lp->cache.csr13, lp->cache.csr14, lp->cache.csr15);
5211     }
5212
5213     /* Restore CSR6 */
5214     outl(omr, DE4X5_OMR);
5215
5216     /* Reset CSR8 */
5217     inl(DE4X5_MFC);
5218
5219     return omr;
5220 }
5221
5222 static void
5223 gep_wr(s32 data, struct net_device *dev)
5224 {
5225     struct de4x5_private *lp = netdev_priv(dev);
5226     u_long iobase = dev->base_addr;
5227
5228     if (lp->chipset == DC21140) {
5229         outl(data, DE4X5_GEP);
5230     } else if ((lp->chipset & ~0x00ff) == DC2114x) {
5231         outl((data<<16) | lp->cache.csr15, DE4X5_SIGR);
5232     }
5233
5234     return;
5235 }
5236
5237 static int
5238 gep_rd(struct net_device *dev)
5239 {
5240     struct de4x5_private *lp = netdev_priv(dev);
5241     u_long iobase = dev->base_addr;
5242
5243     if (lp->chipset == DC21140) {
5244         return inl(DE4X5_GEP);
5245     } else if ((lp->chipset & ~0x00ff) == DC2114x) {
5246         return (inl(DE4X5_SIGR) & 0x000fffff);
5247     }
5248
5249     return 0;
5250 }
5251
5252 static void
5253 timeout(struct net_device *dev, void (*fn)(u_long data), u_long data, u_long msec)
5254 {
5255     struct de4x5_private *lp = netdev_priv(dev);
5256     int dt;
5257     
5258     /* First, cancel any pending timer events */
5259     del_timer(&lp->timer);
5260     
5261     /* Convert msec to ticks */
5262     dt = (msec * HZ) / 1000;
5263     if (dt==0) dt=1;
5264     
5265     /* Set up timer */
5266     init_timer(&lp->timer);
5267     lp->timer.expires = jiffies + dt;
5268     lp->timer.function = fn;
5269     lp->timer.data = data;
5270     add_timer(&lp->timer);
5271     
5272     return;
5273 }
5274
5275 static void
5276 yawn(struct net_device *dev, int state)
5277 {
5278     struct de4x5_private *lp = netdev_priv(dev);
5279     u_long iobase = dev->base_addr;
5280
5281     if ((lp->chipset == DC21040) || (lp->chipset == DC21140)) return;
5282
5283     if(lp->bus == EISA) {
5284         switch(state) {
5285           case WAKEUP:
5286             outb(WAKEUP, PCI_CFPM);
5287             mdelay(10);
5288             break;
5289
5290           case SNOOZE:
5291             outb(SNOOZE, PCI_CFPM);
5292             break;
5293
5294           case SLEEP:
5295             outl(0, DE4X5_SICR);
5296             outb(SLEEP, PCI_CFPM);
5297             break;
5298         }
5299     } else {
5300         struct pci_dev *pdev = to_pci_dev (lp->gendev);
5301         switch(state) {
5302           case WAKEUP:
5303             pci_write_config_byte(pdev, PCI_CFDA_PSM, WAKEUP);
5304             mdelay(10);
5305             break;
5306
5307           case SNOOZE:
5308             pci_write_config_byte(pdev, PCI_CFDA_PSM, SNOOZE);
5309             break;
5310
5311           case SLEEP:
5312             outl(0, DE4X5_SICR);
5313             pci_write_config_byte(pdev, PCI_CFDA_PSM, SLEEP);
5314             break;
5315         }
5316     }
5317
5318     return;
5319 }
5320
5321 static void
5322 de4x5_parse_params(struct net_device *dev)
5323 {
5324     struct de4x5_private *lp = netdev_priv(dev);
5325     char *p, *q, t;
5326
5327     lp->params.fdx = 0;
5328     lp->params.autosense = AUTO;
5329
5330     if (args == NULL) return;
5331
5332     if ((p = strstr(args, dev->name))) {
5333         if (!(q = strstr(p+strlen(dev->name), "eth"))) q = p + strlen(p);
5334         t = *q;
5335         *q = '\0';
5336
5337         if (strstr(p, "fdx") || strstr(p, "FDX")) lp->params.fdx = 1;
5338
5339         if (strstr(p, "autosense") || strstr(p, "AUTOSENSE")) {
5340             if (strstr(p, "TP")) {
5341                 lp->params.autosense = TP;
5342             } else if (strstr(p, "TP_NW")) {
5343                 lp->params.autosense = TP_NW;
5344             } else if (strstr(p, "BNC")) {
5345                 lp->params.autosense = BNC;
5346             } else if (strstr(p, "AUI")) {
5347                 lp->params.autosense = AUI;
5348             } else if (strstr(p, "BNC_AUI")) {
5349                 lp->params.autosense = BNC;
5350             } else if (strstr(p, "10Mb")) {
5351                 lp->params.autosense = _10Mb;
5352             } else if (strstr(p, "100Mb")) {
5353                 lp->params.autosense = _100Mb;
5354             } else if (strstr(p, "AUTO")) {
5355                 lp->params.autosense = AUTO;
5356             }
5357         }
5358         *q = t;
5359     }
5360
5361     return;
5362 }
5363
5364 static void
5365 de4x5_dbg_open(struct net_device *dev)
5366 {
5367     struct de4x5_private *lp = netdev_priv(dev);
5368     int i;
5369     
5370     if (de4x5_debug & DEBUG_OPEN) {
5371         printk("%s: de4x5 opening with irq %d\n",dev->name,dev->irq);
5372         printk("\tphysical address: ");
5373         for (i=0;i<6;i++) {
5374             printk("%2.2x:",(short)dev->dev_addr[i]);
5375         }
5376         printk("\n");
5377         printk("Descriptor head addresses:\n");
5378         printk("\t0x%8.8lx  0x%8.8lx\n",(u_long)lp->rx_ring,(u_long)lp->tx_ring);
5379         printk("Descriptor addresses:\nRX: ");
5380         for (i=0;i<lp->rxRingSize-1;i++){
5381             if (i < 3) {
5382                 printk("0x%8.8lx  ",(u_long)&lp->rx_ring[i].status);
5383             }
5384         }
5385         printk("...0x%8.8lx\n",(u_long)&lp->rx_ring[i].status);
5386         printk("TX: ");
5387         for (i=0;i<lp->txRingSize-1;i++){
5388             if (i < 3) {
5389                 printk("0x%8.8lx  ", (u_long)&lp->tx_ring[i].status);
5390             }
5391         }
5392         printk("...0x%8.8lx\n", (u_long)&lp->tx_ring[i].status);
5393         printk("Descriptor buffers:\nRX: ");
5394         for (i=0;i<lp->rxRingSize-1;i++){
5395             if (i < 3) {
5396                 printk("0x%8.8x  ",le32_to_cpu(lp->rx_ring[i].buf));
5397             }
5398         }
5399         printk("...0x%8.8x\n",le32_to_cpu(lp->rx_ring[i].buf));
5400         printk("TX: ");
5401         for (i=0;i<lp->txRingSize-1;i++){
5402             if (i < 3) {
5403                 printk("0x%8.8x  ", le32_to_cpu(lp->tx_ring[i].buf));
5404             }
5405         }
5406         printk("...0x%8.8x\n", le32_to_cpu(lp->tx_ring[i].buf));
5407         printk("Ring size: \nRX: %d\nTX: %d\n", 
5408                (short)lp->rxRingSize, 
5409                (short)lp->txRingSize); 
5410     }
5411     
5412     return;
5413 }
5414
5415 static void
5416 de4x5_dbg_mii(struct net_device *dev, int k)
5417 {
5418     struct de4x5_private *lp = netdev_priv(dev);
5419     u_long iobase = dev->base_addr;
5420     
5421     if (de4x5_debug & DEBUG_MII) {
5422         printk("\nMII device address: %d\n", lp->phy[k].addr);
5423         printk("MII CR:  %x\n",mii_rd(MII_CR,lp->phy[k].addr,DE4X5_MII));
5424         printk("MII SR:  %x\n",mii_rd(MII_SR,lp->phy[k].addr,DE4X5_MII));
5425         printk("MII ID0: %x\n",mii_rd(MII_ID0,lp->phy[k].addr,DE4X5_MII));
5426         printk("MII ID1: %x\n",mii_rd(MII_ID1,lp->phy[k].addr,DE4X5_MII));
5427         if (lp->phy[k].id != BROADCOM_T4) {
5428             printk("MII ANA: %x\n",mii_rd(0x04,lp->phy[k].addr,DE4X5_MII));
5429             printk("MII ANC: %x\n",mii_rd(0x05,lp->phy[k].addr,DE4X5_MII));
5430         }
5431         printk("MII 16:  %x\n",mii_rd(0x10,lp->phy[k].addr,DE4X5_MII));
5432         if (lp->phy[k].id != BROADCOM_T4) {
5433             printk("MII 17:  %x\n",mii_rd(0x11,lp->phy[k].addr,DE4X5_MII));
5434             printk("MII 18:  %x\n",mii_rd(0x12,lp->phy[k].addr,DE4X5_MII));
5435         } else {
5436             printk("MII 20:  %x\n",mii_rd(0x14,lp->phy[k].addr,DE4X5_MII));
5437         }
5438     }
5439     
5440     return;
5441 }
5442
5443 static void
5444 de4x5_dbg_media(struct net_device *dev)
5445 {
5446     struct de4x5_private *lp = netdev_priv(dev);
5447     
5448     if (lp->media != lp->c_media) {
5449         if (de4x5_debug & DEBUG_MEDIA) {
5450             printk("%s: media is %s%s\n", dev->name,
5451                    (lp->media == NC  ? "unconnected, link down or incompatible connection" :
5452                     (lp->media == TP  ? "TP" :
5453                      (lp->media == ANS ? "TP/Nway" :
5454                       (lp->media == BNC ? "BNC" : 
5455                        (lp->media == AUI ? "AUI" : 
5456                         (lp->media == BNC_AUI ? "BNC/AUI" : 
5457                          (lp->media == EXT_SIA ? "EXT SIA" : 
5458                           (lp->media == _100Mb  ? "100Mb/s" :
5459                            (lp->media == _10Mb   ? "10Mb/s" :
5460                             "???"
5461                             ))))))))), (lp->fdx?" full duplex.":"."));
5462         }
5463         lp->c_media = lp->media;
5464     }
5465     
5466     return;
5467 }
5468
5469 static void
5470 de4x5_dbg_srom(struct de4x5_srom *p)
5471 {
5472     int i;
5473
5474     if (de4x5_debug & DEBUG_SROM) {
5475         printk("Sub-system Vendor ID: %04x\n", *((u_short *)p->sub_vendor_id));
5476         printk("Sub-system ID:        %04x\n", *((u_short *)p->sub_system_id));
5477         printk("ID Block CRC:         %02x\n", (u_char)(p->id_block_crc));
5478         printk("SROM version:         %02x\n", (u_char)(p->version));
5479         printk("# controllers:         %02x\n", (u_char)(p->num_controllers));
5480
5481         printk("Hardware Address:     ");
5482         for (i=0;i<ETH_ALEN-1;i++) {
5483             printk("%02x:", (u_char)*(p->ieee_addr+i));
5484         }
5485         printk("%02x\n", (u_char)*(p->ieee_addr+i));
5486         printk("CRC checksum:         %04x\n", (u_short)(p->chksum));
5487         for (i=0; i<64; i++) {
5488             printk("%3d %04x\n", i<<1, (u_short)*((u_short *)p+i));
5489         }
5490     }
5491
5492     return;
5493 }
5494
5495 static void
5496 de4x5_dbg_rx(struct sk_buff *skb, int len)
5497 {
5498     int i, j;
5499
5500     if (de4x5_debug & DEBUG_RX) {
5501         printk("R: %02x:%02x:%02x:%02x:%02x:%02x <- %02x:%02x:%02x:%02x:%02x:%02x len/SAP:%02x%02x [%d]\n",
5502                (u_char)skb->data[0],
5503                (u_char)skb->data[1],
5504                (u_char)skb->data[2],
5505                (u_char)skb->data[3],
5506                (u_char)skb->data[4],
5507                (u_char)skb->data[5],
5508                (u_char)skb->data[6],
5509                (u_char)skb->data[7],
5510                (u_char)skb->data[8],
5511                (u_char)skb->data[9],
5512                (u_char)skb->data[10],
5513                (u_char)skb->data[11],
5514                (u_char)skb->data[12],
5515                (u_char)skb->data[13],
5516                len);
5517         for (j=0; len>0;j+=16, len-=16) {
5518           printk("    %03x: ",j);
5519           for (i=0; i<16 && i<len; i++) {
5520             printk("%02x ",(u_char)skb->data[i+j]);
5521           }
5522           printk("\n");
5523         }
5524     }
5525
5526     return;
5527 }
5528
5529 /*
5530 ** Perform IOCTL call functions here. Some are privileged operations and the
5531 ** effective uid is checked in those cases. In the normal course of events
5532 ** this function is only used for my testing.
5533 */
5534 static int
5535 de4x5_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
5536 {
5537     struct de4x5_private *lp = netdev_priv(dev);
5538     struct de4x5_ioctl *ioc = (struct de4x5_ioctl *) &rq->ifr_ifru;
5539     u_long iobase = dev->base_addr;
5540     int i, j, status = 0;
5541     s32 omr;
5542     union {
5543         u8  addr[144];
5544         u16 sval[72];
5545         u32 lval[36];
5546     } tmp;
5547     u_long flags = 0;
5548     
5549     switch(ioc->cmd) {
5550     case DE4X5_GET_HWADDR:           /* Get the hardware address */
5551         ioc->len = ETH_ALEN;
5552         for (i=0; i<ETH_ALEN; i++) {
5553             tmp.addr[i] = dev->dev_addr[i];
5554         }
5555         if (copy_to_user(ioc->data, tmp.addr, ioc->len)) return -EFAULT;
5556         break;
5557
5558     case DE4X5_SET_HWADDR:           /* Set the hardware address */
5559         if (!capable(CAP_NET_ADMIN)) return -EPERM;
5560         if (copy_from_user(tmp.addr, ioc->data, ETH_ALEN)) return -EFAULT;
5561         if (netif_queue_stopped(dev))
5562                 return -EBUSY;
5563         netif_stop_queue(dev);
5564         for (i=0; i<ETH_ALEN; i++) {
5565             dev->dev_addr[i] = tmp.addr[i];
5566         }
5567         build_setup_frame(dev, PHYS_ADDR_ONLY);
5568         /* Set up the descriptor and give ownership to the card */
5569         load_packet(dev, lp->setup_frame, TD_IC | PERFECT_F | TD_SET | 
5570                                                        SETUP_FRAME_LEN, (struct sk_buff *)1);
5571         lp->tx_new = (++lp->tx_new) % lp->txRingSize;
5572         outl(POLL_DEMAND, DE4X5_TPD);                /* Start the TX */
5573         netif_wake_queue(dev);                      /* Unlock the TX ring */
5574         break;
5575
5576     case DE4X5_SET_PROM:             /* Set Promiscuous Mode */
5577         if (!capable(CAP_NET_ADMIN)) return -EPERM;
5578         omr = inl(DE4X5_OMR);
5579         omr |= OMR_PR;
5580         outl(omr, DE4X5_OMR);
5581         dev->flags |= IFF_PROMISC;
5582         break;
5583
5584     case DE4X5_CLR_PROM:             /* Clear Promiscuous Mode */
5585         if (!capable(CAP_NET_ADMIN)) return -EPERM;
5586         omr = inl(DE4X5_OMR);
5587         omr &= ~OMR_PR;
5588         outl(omr, DE4X5_OMR);
5589         dev->flags &= ~IFF_PROMISC;
5590         break;
5591
5592     case DE4X5_SAY_BOO:              /* Say "Boo!" to the kernel log file */
5593         if (!capable(CAP_NET_ADMIN)) return -EPERM;
5594         printk("%s: Boo!\n", dev->name);
5595         break;
5596
5597     case DE4X5_MCA_EN:               /* Enable pass all multicast addressing */
5598         if (!capable(CAP_NET_ADMIN)) return -EPERM;
5599         omr = inl(DE4X5_OMR);
5600         omr |= OMR_PM;
5601         outl(omr, DE4X5_OMR);
5602         break;
5603
5604     case DE4X5_GET_STATS:            /* Get the driver statistics */
5605     {
5606         struct pkt_stats statbuf;
5607         ioc->len = sizeof(statbuf);
5608         spin_lock_irqsave(&lp->lock, flags);
5609         memcpy(&statbuf, &lp->pktStats, ioc->len);
5610         spin_unlock_irqrestore(&lp->lock, flags);
5611         if (copy_to_user(ioc->data, &statbuf, ioc->len)) 
5612                 return -EFAULT; 
5613         break;
5614     }
5615     case DE4X5_CLR_STATS:            /* Zero out the driver statistics */
5616         if (!capable(CAP_NET_ADMIN)) return -EPERM;
5617         spin_lock_irqsave(&lp->lock, flags);
5618         memset(&lp->pktStats, 0, sizeof(lp->pktStats));
5619         spin_unlock_irqrestore(&lp->lock, flags);
5620         break;
5621
5622     case DE4X5_GET_OMR:              /* Get the OMR Register contents */
5623         tmp.addr[0] = inl(DE4X5_OMR);
5624         if (copy_to_user(ioc->data, tmp.addr, 1)) return -EFAULT;
5625         break;
5626
5627     case DE4X5_SET_OMR:              /* Set the OMR Register contents */
5628         if (!capable(CAP_NET_ADMIN)) return -EPERM;
5629         if (copy_from_user(tmp.addr, ioc->data, 1)) return -EFAULT;
5630         outl(tmp.addr[0], DE4X5_OMR);
5631         break;
5632
5633     case DE4X5_GET_REG:              /* Get the DE4X5 Registers */
5634         j = 0;
5635         tmp.lval[0] = inl(DE4X5_STS); j+=4;
5636         tmp.lval[1] = inl(DE4X5_BMR); j+=4;
5637         tmp.lval[2] = inl(DE4X5_IMR); j+=4;
5638         tmp.lval[3] = inl(DE4X5_OMR); j+=4;
5639         tmp.lval[4] = inl(DE4X5_SISR); j+=4;
5640         tmp.lval[5] = inl(DE4X5_SICR); j+=4;
5641         tmp.lval[6] = inl(DE4X5_STRR); j+=4;
5642         tmp.lval[7] = inl(DE4X5_SIGR); j+=4;
5643         ioc->len = j;
5644         if (copy_to_user(ioc->data, tmp.addr, ioc->len)) return -EFAULT;
5645         break;
5646         
5647 #define DE4X5_DUMP              0x0f /* Dump the DE4X5 Status */
5648 /*      
5649       case DE4X5_DUMP:
5650         j = 0;
5651         tmp.addr[j++] = dev->irq;
5652         for (i=0; i<ETH_ALEN; i++) {
5653             tmp.addr[j++] = dev->dev_addr[i];
5654         }
5655         tmp.addr[j++] = lp->rxRingSize;
5656         tmp.lval[j>>2] = (long)lp->rx_ring; j+=4;
5657         tmp.lval[j>>2] = (long)lp->tx_ring; j+=4;
5658         
5659         for (i=0;i<lp->rxRingSize-1;i++){
5660             if (i < 3) {
5661                 tmp.lval[j>>2] = (long)&lp->rx_ring[i].status; j+=4;
5662             }
5663         }
5664         tmp.lval[j>>2] = (long)&lp->rx_ring[i].status; j+=4;
5665         for (i=0;i<lp->txRingSize-1;i++){
5666             if (i < 3) {
5667                 tmp.lval[j>>2] = (long)&lp->tx_ring[i].status; j+=4;
5668             }
5669         }
5670         tmp.lval[j>>2] = (long)&lp->tx_ring[i].status; j+=4;
5671         
5672         for (i=0;i<lp->rxRingSize-1;i++){
5673             if (i < 3) {
5674                 tmp.lval[j>>2] = (s32)le32_to_cpu(lp->rx_ring[i].buf); j+=4;
5675             }
5676         }
5677         tmp.lval[j>>2] = (s32)le32_to_cpu(lp->rx_ring[i].buf); j+=4;
5678         for (i=0;i<lp->txRingSize-1;i++){
5679             if (i < 3) {
5680                 tmp.lval[j>>2] = (s32)le32_to_cpu(lp->tx_ring[i].buf); j+=4;
5681             }
5682         }
5683         tmp.lval[j>>2] = (s32)le32_to_cpu(lp->tx_ring[i].buf); j+=4;
5684         
5685         for (i=0;i<lp->rxRingSize;i++){
5686             tmp.lval[j>>2] = le32_to_cpu(lp->rx_ring[i].status); j+=4;
5687         }
5688         for (i=0;i<lp->txRingSize;i++){
5689             tmp.lval[j>>2] = le32_to_cpu(lp->tx_ring[i].status); j+=4;
5690         }
5691         
5692         tmp.lval[j>>2] = inl(DE4X5_BMR);  j+=4;
5693         tmp.lval[j>>2] = inl(DE4X5_TPD);  j+=4;
5694         tmp.lval[j>>2] = inl(DE4X5_RPD);  j+=4;
5695         tmp.lval[j>>2] = inl(DE4X5_RRBA); j+=4;
5696         tmp.lval[j>>2] = inl(DE4X5_TRBA); j+=4;
5697         tmp.lval[j>>2] = inl(DE4X5_STS);  j+=4;
5698         tmp.lval[j>>2] = inl(DE4X5_OMR);  j+=4;
5699         tmp.lval[j>>2] = inl(DE4X5_IMR);  j+=4;
5700         tmp.lval[j>>2] = lp->chipset; j+=4; 
5701         if (lp->chipset == DC21140) {
5702             tmp.lval[j>>2] = gep_rd(dev);  j+=4;
5703         } else {
5704             tmp.lval[j>>2] = inl(DE4X5_SISR); j+=4;
5705             tmp.lval[j>>2] = inl(DE4X5_SICR); j+=4;
5706             tmp.lval[j>>2] = inl(DE4X5_STRR); j+=4;
5707             tmp.lval[j>>2] = inl(DE4X5_SIGR); j+=4; 
5708         }
5709         tmp.lval[j>>2] = lp->phy[lp->active].id; j+=4; 
5710         if (lp->phy[lp->active].id && (!lp->useSROM || lp->useMII)) {
5711             tmp.lval[j>>2] = lp->active; j+=4; 
5712             tmp.lval[j>>2]=mii_rd(MII_CR,lp->phy[lp->active].addr,DE4X5_MII); j+=4;
5713             tmp.lval[j>>2]=mii_rd(MII_SR,lp->phy[lp->active].addr,DE4X5_MII); j+=4;
5714             tmp.lval[j>>2]=mii_rd(MII_ID0,lp->phy[lp->active].addr,DE4X5_MII); j+=4;
5715             tmp.lval[j>>2]=mii_rd(MII_ID1,lp->phy[lp->active].addr,DE4X5_MII); j+=4;
5716             if (lp->phy[lp->active].id != BROADCOM_T4) {
5717                 tmp.lval[j>>2]=mii_rd(MII_ANA,lp->phy[lp->active].addr,DE4X5_MII); j+=4;
5718                 tmp.lval[j>>2]=mii_rd(MII_ANLPA,lp->phy[lp->active].addr,DE4X5_MII); j+=4;
5719             }
5720             tmp.lval[j>>2]=mii_rd(0x10,lp->phy[lp->active].addr,DE4X5_MII); j+=4;
5721             if (lp->phy[lp->active].id != BROADCOM_T4) {
5722                 tmp.lval[j>>2]=mii_rd(0x11,lp->phy[lp->active].addr,DE4X5_MII); j+=4;
5723                 tmp.lval[j>>2]=mii_rd(0x12,lp->phy[lp->active].addr,DE4X5_MII); j+=4;
5724             } else {
5725                 tmp.lval[j>>2]=mii_rd(0x14,lp->phy[lp->active].addr,DE4X5_MII); j+=4;
5726             }
5727         }
5728         
5729         tmp.addr[j++] = lp->txRingSize;
5730         tmp.addr[j++] = netif_queue_stopped(dev);
5731         
5732         ioc->len = j;
5733         if (copy_to_user(ioc->data, tmp.addr, ioc->len)) return -EFAULT;
5734         break;
5735
5736 */
5737     default:
5738         return -EOPNOTSUPP;
5739     }
5740     
5741     return status;
5742 }
5743
5744 static int __init de4x5_module_init (void)
5745 {
5746         int err = 0;
5747
5748 #ifdef CONFIG_PCI
5749         err = pci_module_init (&de4x5_pci_driver);
5750 #endif
5751 #ifdef CONFIG_EISA
5752         err |= eisa_driver_register (&de4x5_eisa_driver);
5753 #endif
5754
5755         return err;
5756 }
5757
5758 static void __exit de4x5_module_exit (void)
5759 {
5760 #ifdef CONFIG_PCI
5761         pci_unregister_driver (&de4x5_pci_driver);
5762 #endif
5763 #ifdef CONFIG_EISA
5764         eisa_driver_unregister (&de4x5_eisa_driver);
5765 #endif
5766 }
5767
5768 module_init (de4x5_module_init);
5769 module_exit (de4x5_module_exit);