setup enviroment for compilation
[linux-2.4.21-pre4.git] / drivers / net / smc91111.c
1 /*------------------------------------------------------------------------
2  . smc91111.c
3  . This is a driver for SMSC's 91C111 single-chip Ethernet device.
4  .
5  . Copyright (C) 2001 Standard Microsystems Corporation (SMSC)
6  .       Developed by Simple Network Magic Corporation (SNMC)
7  . Copyright (C) 1996 by Erik Stahlman (ES)
8  .
9  . This program is free software; you can redistribute it and/or modify
10  . it under the terms of the GNU General Public License as published by
11  . the Free Software Foundation; either version 2 of the License, or
12  . (at your option) any later version.
13  .
14  . This program is distributed in the hope that it will be useful,
15  . but WITHOUT ANY WARRANTY; without even the implied warranty of
16  . MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  . GNU General Public License for more details.
18  .
19  . You should have received a copy of the GNU General Public License
20  . along with this program; if not, write to the Free Software
21  . Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  .
23  . Information contained in this file was obtained from the LAN91C111
24  . manual from SMC.  To get a copy, if you really want one, you can find
25  . information under www.smsc.com.
26  .
27  .
28  . "Features" of the SMC chip:
29  .   Integrated PHY/MAC for 10/100BaseT Operation
30  .   Supports internal and external MII
31  .   Integrated 8K packet memory
32  .   EEPROM interface for configuration
33  .
34  . Arguments:
35  .      io      = for the base address
36  .      irq     = for the IRQ
37  .      nowait  = 0 for normal wait states, 1 eliminates additional wait states
38  .
39  . author:
40  .      Erik Stahlman                           ( erik@vt.edu )
41  .      Daris A Nevil                           ( dnevil@snmc.com )
42  . contributors:
43  .      Arnaldo Carvalho de Melo <acme@conectiva.com.br>
44  .
45  . Hardware multicast code from Peter Cammaert ( pc@denkart.be )
46  .
47  . Sources:
48  .    o   SMSC LAN91C111 databook (www.smsc.com)
49  .    o   smc9194.c by Erik Stahlman
50  .    o   skeleton.c by Donald Becker ( becker@scyld.com )
51  .
52  . History:
53  .      08/20/00  Arnaldo Melo   fix kfree(skb) in smc_hardware_send_packet
54  .      12/15/00  Christian Jullien fix "Warning: kfree_skb on hard IRQ"
55  .      04/25/01  Daris A Nevil  Initial public release through SMSC
56  .      03/16/01  Daris A Nevil  Modified smc9194.c for use with LAN91C111
57  .      08/22/01  Scott Anderson Merge changes from smc9194 to smc91111
58  ----------------------------------------------------------------------------*/
59
60 // Use power-down feature of the chip
61 #define POWER_DOWN      1
62
63 /*
64  . DEBUGGING LEVELS
65  .
66  . 0 for normal operation
67  . 1 for slightly more details
68  . >2 for various levels of increasingly useless information
69  .    2 for interrupt tracking, status flags
70  .    3 for packet info
71  .    4 for complete packet dumps
72 */
73 #ifndef SMC_DEBUG
74 #define SMC_DEBUG 0
75 #endif
76
77 static const char version[] =
78         "smc91111.c:v1.0saa 08/22/01 by Daris A Nevil (dnevil@snmc.com)\n";
79
80 #include <linux/module.h>
81 #include <linux/version.h>
82 #include <linux/kernel.h>
83 #include <linux/sched.h>
84 #include <linux/types.h>
85 #include <linux/fcntl.h>
86 #include <linux/interrupt.h>
87 #include <linux/ptrace.h>
88 #include <linux/ioport.h>
89 #include <linux/in.h>
90 #include <linux/slab.h>
91 #include <linux/string.h>
92 #include <linux/init.h>
93 #include <asm/bitops.h>
94 #include <asm/io.h>
95 #include <linux/errno.h>
96 #include <linux/delay.h>
97
98 #include <linux/netdevice.h>
99 #include <linux/etherdevice.h>
100 #include <linux/skbuff.h>
101
102 #ifdef CONFIG_SYSCTL
103 #include <linux/proc_fs.h>
104 #include <linux/sysctl.h>
105 #endif
106
107 #include "smc91111.h"
108
109 #ifdef CONFIG_ISA
110
111 /*
112  .the LAN91C111 can be at any of the following port addresses.  To change,
113  .for a slightly different card, you can add it to the array.  Keep in
114  .mind that the array must end in zero.
115 */
116 static unsigned int smc_portlist[] __initdata = {
117         0x200, 0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x2E0,
118         0x300, 0x320, 0x340, 0x360, 0x380, 0x3A0, 0x3C0, 0x3E0, 0
119 };
120
121 #endif  /* CONFIG_ISA */
122
123 static struct net_device *global_dev = NULL;
124 #ifndef SMC91111_BASE_ADDR
125 # define SMC91111_BASE_ADDR -1
126 #endif
127 static int io = SMC91111_BASE_ADDR;
128 #ifndef SMC91111_IRQ
129 # define SMC91111_IRQ -1
130 #endif
131 static int irq = SMC91111_IRQ;
132 static int nowait = 0;
133
134 MODULE_PARM(io, "i");
135 MODULE_PARM(irq, "i");
136 MODULE_PARM(nowait, "i");
137 MODULE_PARM_DESC(io, "SMC 91111 I/O base address");
138 MODULE_PARM_DESC(irq, "SMC 91111 IRQ number");
139
140 /*
141  . Wait time for memory to be free.  This probably shouldn't be
142  . tuned that much, as waiting for this means nothing else happens
143  . in the system
144 */
145 #define MEMORY_WAIT_TIME 16
146
147 #if SMC_DEBUG > 2
148 #define PRINTK3(args...) printk(args)
149 #else
150 #define PRINTK3(args...)
151 #endif
152
153 #if SMC_DEBUG > 1
154 #define PRINTK2(args...) printk(args)
155 #else
156 #define PRINTK2(args...)
157 #endif
158
159 #if SMC_DEBUG > 0
160 #define PRINTK(args...) printk(args)
161 #else
162 #define PRINTK(args...)
163 #endif
164
165
166 /*------------------------------------------------------------------------
167  .
168  . The internal workings of the driver.  If you are changing anything
169  . here with the SMC stuff, you should have the datasheet and know
170  . what you are doing.
171  .
172  -------------------------------------------------------------------------*/
173 #define CARDNAME "LAN91C111"
174
175 // Memory sizing constant
176 #define LAN91C111_MEMORY_MULTIPLIER     (1024*2)
177
178 /* store this information for the driver.. */
179 struct smc_local {
180
181         // these are things that the kernel wants me to keep, so users
182         // can find out semi-useless statistics of how well the card is
183         // performing
184         struct net_device_stats stats;
185
186         // If I have to wait until memory is available to send
187         // a packet, I will store the skbuff here, until I get the
188         // desired memory.  Then, I'll send it out and free it.
189         struct sk_buff * saved_skb;
190
191         // This keeps track of how many packets that I have
192         // sent out.  When an TX_EMPTY interrupt comes, I know
193         // that all of these have been sent.
194         int     packets_waiting;
195
196         // Set to true during the auto-negotiation sequence
197         int     autoneg_active;
198
199         // Address of our PHY port
200         word    phyaddr;
201
202         // Type of PHY
203         word    phytype;
204
205         // Last contents of PHY Register 18
206         word    lastPhy18;
207
208         // Contains the current active transmission mode
209         word    tcr_cur_mode;
210
211         // Contains the current active receive mode
212         word    rcr_cur_mode;
213
214         // Contains the current active receive/phy mode
215         word    rpc_cur_mode;
216
217
218 #ifdef CONFIG_SYSCTL
219
220         // Root directory /proc/sys/dev
221         // Second entry must be null to terminate the table
222         ctl_table root_table[2];
223
224         // Directory for this device /proc/sys/dev/ethX
225         // Again the second entry must be zero to terminate
226         ctl_table eth_table[2];
227
228         // This is the parameters (file) table
229         ctl_table param_table[CTL_SMC_LAST_ENTRY];
230
231         // Saves the sysctl header returned by register_sysctl_table()
232         // we send this to unregister_sysctl_table()
233         struct ctl_table_header *sysctl_header;
234
235         // Parameter variables (files) go here
236         char ctl_info[1024];
237         int ctl_swfdup;
238         int ctl_ephloop;
239         int ctl_miiop;
240         int ctl_autoneg;
241         int ctl_rfduplx;
242         int ctl_rspeed;
243         int ctl_afduplx;
244         int ctl_aspeed;
245         int ctl_lnkfail;
246         int ctl_forcol;
247         int ctl_filtcar;
248         int ctl_freemem;
249         int ctl_totmem;
250         int ctl_leda;
251         int ctl_ledb;
252         int ctl_chiprev;
253 #if SMC_DEBUG > 0
254         int ctl_reg_bsr;
255         int ctl_reg_tcr;
256         int ctl_reg_esr;
257         int ctl_reg_rcr;
258         int ctl_reg_ctrr;
259         int ctl_reg_mir;
260         int ctl_reg_rpcr;
261         int ctl_reg_cfgr;
262         int ctl_reg_bar;
263         int ctl_reg_iar0;
264         int ctl_reg_iar1;
265         int ctl_reg_iar2;
266         int ctl_reg_gpr;
267         int ctl_reg_ctlr;
268         int ctl_reg_mcr;
269         int ctl_reg_pnr;
270         int ctl_reg_fpr;
271         int ctl_reg_ptr;
272         int ctl_reg_dr;
273         int ctl_reg_isr;
274         int ctl_reg_mtr1;
275         int ctl_reg_mtr2;
276         int ctl_reg_mtr3;
277         int ctl_reg_mtr4;
278         int ctl_reg_miir;
279         int ctl_reg_revr;
280         int ctl_reg_ercvr;
281         int ctl_reg_extr;
282         int ctl_phy_ctrl;
283         int ctl_phy_stat;
284         int ctl_phy_id1;
285         int ctl_phy_id2;
286         int ctl_phy_adc;
287         int ctl_phy_remc;
288         int ctl_phy_cfg1;
289         int ctl_phy_cfg2;
290         int ctl_phy_int;
291         int ctl_phy_mask;
292 #endif // SMC_DEBUG > 0
293
294
295 #endif // CONFIG_SYSCTL
296
297 };
298
299
300 /*-----------------------------------------------------------------
301  .
302  .  The driver can be entered at any of the following entry points.
303  .
304  .------------------------------------------------------------------  */
305
306 /*
307  . The kernel calls this function when someone wants to use the device,
308  . typically 'ifconfig ethX up'.
309 */
310 static int smc_open(struct net_device *dev);
311
312 /*
313  . Our watchdog timed out. Called by the networking layer
314 */
315 static void smc_timeout(struct net_device *dev);
316
317 /*
318  . This is called by the kernel in response to 'ifconfig ethX down'.  It
319  . is responsible for cleaning up everything that the open routine
320  . does, and maybe putting the card into a powerdown state.
321 */
322 static int smc_close(struct net_device *dev);
323
324 /*
325  . This routine allows the proc file system to query the driver's
326  . statistics.
327 */
328 static struct net_device_stats * smc_query_statistics( struct net_device *dev);
329
330 /*
331  . Finally, a call to set promiscuous mode ( for TCPDUMP and related
332  . programs ) and multicast modes.
333 */
334 static void smc_set_multicast_list(struct net_device *dev);
335
336 /*
337  . CRC compute
338  */
339 static int crc32( char * s, int length );
340
341 /*
342  . Configures the PHY through the MII Management interface
343 */
344 static void smc_phy_configure(struct net_device* dev);
345
346 /*---------------------------------------------------------------
347  .
348  . Interrupt level calls..
349  .
350  ----------------------------------------------------------------*/
351
352 /*
353  . Handles the actual interrupt
354 */
355 static void smc_interrupt(int irq, void *, struct pt_regs *regs);
356 /*
357  . This is a separate procedure to handle the receipt of a packet, to
358  . leave the interrupt code looking slightly cleaner
359 */
360 inline static void smc_rcv( struct net_device *dev );
361 /*
362  . This handles a TX interrupt, which is only called when an error
363  . relating to a packet is sent.
364 */
365 inline static void smc_tx( struct net_device * dev );
366
367 /*
368  . This handles interrupts generated from PHY register 18
369 */
370 static void smc_phy_interrupt(struct net_device* dev);
371
372 /*
373  ------------------------------------------------------------
374  .
375  . Internal routines
376  .
377  ------------------------------------------------------------
378 */
379
380 /*
381  . Test if a given location contains a chip, trying to cause as
382  . little damage as possible if it's not a SMC chip.
383 */
384 static int smc_probe(struct net_device *dev, unsigned long ioaddr);
385
386 /*
387  . A rather simple routine to print out a packet for debugging purposes.
388 */
389 #if SMC_DEBUG > 2
390 static void print_packet( byte *, int );
391 #endif
392
393 #define tx_done(dev) 1
394
395 /* this is called to actually send the packet to the chip */
396 static void smc_hardware_send_packet( struct net_device * dev );
397
398 /* Since I am not sure if I will have enough room in the chip's ram
399  . to store the packet, I call this routine, which either sends it
400  . now, or generates an interrupt when the card is ready for the
401  . packet */
402 static int  smc_wait_to_send_packet( struct sk_buff * skb, struct net_device *dev );
403
404 /* this does a soft reset on the device */
405 static void smc_reset( struct net_device* dev );
406
407 /* Enable Interrupts, Receive, and Transmit */
408 static void smc_enable( struct net_device *dev );
409
410 /* this puts the device in an inactive state */
411 static void smc_shutdown( unsigned long ioaddr );
412
413 /* This routine will find the IRQ of the driver if one is not
414  . specified in the input to the device.  */
415 static int smc_findirq( unsigned long ioaddr );
416
417 /* Routines to Read and Write the PHY Registers across the
418    MII Management Interface
419 */
420
421 static word smc_read_phy_register(unsigned long ioaddr,
422                                   byte phyaddr, byte phyreg);
423 static void smc_write_phy_register(unsigned long ioaddr,
424         byte phyaddr, byte phyreg, word phydata);
425
426 /*
427   Initilizes our device's sysctl proc filesystem
428 */
429
430 #ifdef CONFIG_SYSCTL
431 static void smc_sysctl_register(struct net_device *dev);
432 static void smc_sysctl_unregister(struct net_device *dev);
433 #endif /* CONFIG_SYSCTL */
434
435 /*
436  . Function: smc_reset( struct net_device* dev )
437  . Purpose:
438  .      This sets the SMC91111 chip to its normal state, hopefully from whatever
439  .      mess that any other DOS driver has put it in.
440  .
441  . Maybe I should reset more registers to defaults in here?  SOFTRST  should
442  . do that for me.
443  .
444  . Method:
445  .      1.  send a SOFT RESET
446  .      2.  wait for it to finish
447  .      3.  enable autorelease mode
448  .      4.  reset the memory management unit
449  .      5.  clear all interrupts
450  .
451 */
452 static void smc_reset( struct net_device* dev )
453 {
454         //struct smc_local *lp  = (struct smc_local *)dev->priv;
455         unsigned long ioaddr = dev->base_addr;
456
457         PRINTK2("%s:smc_reset\n", dev->name);
458
459         /* This resets the registers mostly to defaults, but doesn't
460            affect EEPROM.  That seems unnecessary */
461         SMC_SELECT_BANK( 0 );
462         SMC_SET_RCR( RCR_SOFTRST );
463
464         /* Setup the Configuration Register */
465         /* This is necessary because the CONFIG_REG is not affected */
466         /* by a soft reset */
467
468         SMC_SELECT_BANK( 1 );
469         SMC_SET_CONFIG( CONFIG_DEFAULT );
470
471         /* Setup for fast accesses if requested */
472         /* If the card/system can't handle it then there will */
473         /* be no recovery except for a hard reset or power cycle */
474
475         if (dev->dma)
476                 SMC_SET_CONFIG( SMC_GET_CONFIG() | CONFIG_NO_WAIT );
477
478 #ifdef POWER_DOWN
479         /* Release from possible power-down state */
480         /* Configuration register is not affected by Soft Reset */
481         SMC_SELECT_BANK( 1 );
482         SMC_SET_CONFIG( SMC_GET_CONFIG() | CONFIG_EPH_POWER_EN );
483 #endif
484
485         SMC_SELECT_BANK( 0 );
486
487         /* this should pause enough for the chip to be happy */
488         mdelay(10);
489
490         /* Disable transmit and receive functionality */
491         SMC_SET_RCR( RCR_CLEAR );
492         SMC_SET_TCR( TCR_CLEAR );
493
494         /* set the control register to automatically
495            release successfully transmitted packets, to make the best
496            use out of our limited memory */
497         SMC_SELECT_BANK( 1 );
498         SMC_SET_CTL( SMC_GET_CTL() | CTL_AUTO_RELEASE );
499
500         /* Reset the MMU */
501         SMC_SELECT_BANK( 2 );
502         SMC_SET_MMU_CMD( MC_RESET );
503
504         /* Note:  It doesn't seem that waiting for the MMU busy is needed here,
505            but this is a place where future chipsets _COULD_ break.  Be wary
506            of issuing another MMU command right after this */
507
508         /* Disable all interrupts */
509         SMC_SET_INT_MASK( 0 );
510 }
511
512 /*
513  . Function: smc_enable
514  . Purpose: let the chip talk to the outside work
515  . Method:
516  .      1.  Enable the transmitter
517  .      2.  Enable the receiver
518  .      3.  Enable interrupts
519 */
520 static void smc_enable( struct net_device *dev )
521 {
522         unsigned long ioaddr    = dev->base_addr;
523         struct smc_local *lp    = (struct smc_local *)dev->priv;
524
525         PRINTK2("%s:smc_enable\n", dev->name);
526
527         SMC_SELECT_BANK( 0 );
528         /* see the header file for options in TCR/RCR DEFAULT*/
529         SMC_SET_TCR( lp->tcr_cur_mode );
530         SMC_SET_RCR( lp->rcr_cur_mode );
531
532         /* now, enable interrupts */
533         SMC_SELECT_BANK( 2 );
534         SMC_SET_INT_MASK( SMC_INTERRUPT_MASK );
535 }
536
537 /*
538  . Function: smc_shutdown
539  . Purpose:  closes down the SMC91xxx chip.
540  . Method:
541  .      1. zero the interrupt mask
542  .      2. clear the enable receive flag
543  .      3. clear the enable xmit flags
544  .
545  . TODO:
546  .   (1) maybe utilize power down mode.
547  .      Why not yet?  Because while the chip will go into power down mode,
548  .      the manual says that it will wake up in response to any I/O requests
549  .      in the register space.   Empirical results do not show this working.
550 */
551 static void smc_shutdown( unsigned long ioaddr )
552 {
553         PRINTK2(CARDNAME ":smc_shutdown\n");
554
555         /* no more interrupts for me */
556         SMC_SELECT_BANK( 2 );
557         SMC_SET_INT_MASK( 0 );
558
559         /* and tell the card to stay away from that nasty outside world */
560         SMC_SELECT_BANK( 0 );
561         SMC_SET_RCR( RCR_CLEAR );
562         SMC_SET_TCR( TCR_CLEAR );
563
564 #ifdef POWER_DOWN
565         /* finally, shut the chip down */
566         SMC_SELECT_BANK( 1 );
567         SMC_SET_CONFIG( SMC_GET_CONFIG() & ~CONFIG_EPH_POWER_EN );
568 #endif
569 }
570
571
572 /*
573  . Function: smc_setmulticast( int ioaddr, int count, dev_mc_list * adds )
574  . Purpose:
575  .    This sets the internal hardware table to filter out unwanted multicast
576  .    packets before they take up memory.
577  .
578  .    The SMC chip uses a hash table where the high 6 bits of the CRC of
579  .    address are the offset into the table.  If that bit is 1, then the
580  .    multicast packet is accepted.  Otherwise, it's dropped silently.
581  .
582  .    To use the 6 bits as an offset into the table, the high 3 bits are the
583  .    number of the 8 bit register, while the low 3 bits are the bit within
584  .    that register.
585  .
586  . This routine is based very heavily on the one provided by Peter Cammaert.
587 */
588
589
590 static void smc_setmulticast( unsigned long ioaddr, int count,
591                               struct dev_mc_list * addrs ) {
592         int                     i;
593         unsigned char           multicast_table[ 8 ];
594         struct dev_mc_list      * cur_addr;
595         /* table for flipping the order of 3 bits */
596         unsigned char invert3[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
597
598         PRINTK2(CARDNAME ":smc_setmulticast\n");
599
600         /* start with a table of all zeros: reject all */
601         memset( multicast_table, 0, sizeof( multicast_table ) );
602
603         cur_addr = addrs;
604         for ( i = 0; i < count ; i ++, cur_addr = cur_addr->next  ) {
605                 int position;
606
607                 /* do we have a pointer here? */
608                 if ( !cur_addr )
609                         break;
610                 /* make sure this is a multicast address - shouldn't this
611                    be a given if we have it here ? */
612                 if ( !( *cur_addr->dmi_addr & 1 ) )
613                         continue;
614
615                 /* only use the low order bits */
616                 position = crc32( cur_addr->dmi_addr, 6 ) & 0x3f;
617
618                 /* do some messy swapping to put the bit in the right spot */
619                 multicast_table[invert3[position&7]] |=
620                                         (1<<invert3[(position>>3)&7]);
621
622         }
623         /* now, the table can be loaded into the chipset */
624         SMC_SELECT_BANK( 3 );
625         SMC_SET_MCAST( multicast_table );
626 }
627
628 /*
629   Finds the CRC32 of a set of bytes.
630   Again, from Peter Cammaert's code.
631 */
632 static int crc32( char * s, int length ) {
633         /* indices */
634         int perByte;
635         int perBit;
636         /* crc polynomial for Ethernet */
637         const unsigned long poly = 0xedb88320;
638         /* crc value - preinitialized to all 1's */
639         unsigned long crc_value = 0xffffffff;
640
641         for ( perByte = 0; perByte < length; perByte ++ ) {
642                 unsigned char   c;
643
644                 c = *(s++);
645                 for ( perBit = 0; perBit < 8; perBit++ ) {
646                         crc_value = (crc_value>>1)^
647                                 (((crc_value^c)&0x01)?poly:0);
648                         c >>= 1;
649                 }
650         }
651         return  crc_value;
652 }
653
654
655 /*
656  . Function: smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * )
657  . Purpose:
658  .    Attempt to allocate memory for a packet, if chip-memory is not
659  .    available, then tell the card to generate an interrupt when it
660  .    is available.
661  .
662  . Algorithm:
663  .
664  . o    if the saved_skb is not currently null, then drop this packet
665  .      on the floor.  This should never happen, because of TBUSY.
666  . o    if the saved_skb is null, then replace it with the current packet,
667  . o    See if I can sending it now.
668  . o    (NO): Enable interrupts and let the interrupt handler deal with it.
669  . o    (YES):Send it now.
670 */
671 static int smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * dev )
672 {
673         struct smc_local *lp    = (struct smc_local *)dev->priv;
674         unsigned long ioaddr    = dev->base_addr;
675         word                    length;
676         unsigned short          numPages;
677         word                    time_out;
678         word                    status;
679
680         PRINTK3("%s:smc_wait_to_send_packet\n", dev->name);
681
682         netif_stop_queue(dev);
683         /* Well, I want to send the packet.. but I don't know
684            if I can send it right now...  */
685
686         if ( lp->saved_skb) {
687                 /* THIS SHOULD NEVER HAPPEN. */
688                 lp->stats.tx_aborted_errors++;
689                 printk("%s: Bad Craziness - sent packet while busy.\n",
690                         dev->name);
691                 return 1;
692         }
693         lp->saved_skb = skb;
694
695         length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
696
697
698         /*
699         ** The MMU wants the number of pages to be the number of 256 bytes
700         ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
701         **
702         ** The 91C111 ignores the size bits, but the code is left intact
703         ** for backwards and future compatibility.
704         **
705         ** Pkt size for allocating is data length +6 (for additional status
706         ** words, length and ctl!)
707         **
708         ** If odd size then last byte is included in this header.
709         */
710         numPages =   ((length & 0xfffe) + 6);
711         numPages >>= 8; // Divide by 256
712
713         if (numPages > 7 ) {
714                 printk("%s: Far too big packet error. \n", dev->name);
715                 /* freeing the packet is a good thing here... but should
716                  . any packets of this size get down here?   */
717                 dev_kfree_skb (skb);
718                 lp->saved_skb = NULL;
719                 /* this IS an error, but, i don't want the skb saved */
720                 netif_wake_queue(dev);
721                 return 0;
722         }
723         /* either way, a packet is waiting now */
724         lp->packets_waiting++;
725
726         /* now, try to allocate the memory */
727         SMC_SELECT_BANK( 2 );
728         SMC_SET_MMU_CMD( MC_ALLOC | numPages );
729         /*
730         . Performance Hack
731         .
732         . wait a short amount of time.. if I can send a packet now, I send
733         . it now.  Otherwise, I enable an interrupt and wait for one to be
734         . available.
735         .
736         . I could have handled this a slightly different way, by checking to
737         . see if any memory was available in the FREE MEMORY register.  However,
738         . either way, I need to generate an allocation, and the allocation works
739         . no matter what, so I saw no point in checking free memory.
740         */
741         time_out = MEMORY_WAIT_TIME;
742         do {
743                 status = SMC_GET_INT();
744                 if ( status & IM_ALLOC_INT ) {
745                         /* acknowledge the interrupt */
746                         SMC_ACK_INT( IM_ALLOC_INT );
747                         break;
748                 }
749         } while ( -- time_out );
750
751         if ( !time_out ) {
752                 /* oh well, wait until the chip finds memory later */
753                 SMC_ENABLE_INT( IM_ALLOC_INT );
754
755                 /* Check the status bit one more time just in case */
756                 /* it snuk in between the time we last checked it */
757                 /* and when we set the interrupt bit */
758                 status = SMC_GET_INT();
759                 if ( !(status & IM_ALLOC_INT) ) {
760                         PRINTK2("%s: memory allocation deferred. \n",
761                                 dev->name);
762                         /* it's deferred, but I'll handle it later */
763                         return 0;
764                         }
765
766                 /* Looks like it did sneak in, so disable */
767                 /* the interrupt */
768                 SMC_DISABLE_INT( IM_ALLOC_INT );
769         }
770         /* or YES! I can send the packet now.. */
771         smc_hardware_send_packet(dev);
772         netif_wake_queue(dev);
773         return 0;
774 }
775
776 /*
777  . Function:  smc_hardware_send_packet(struct net_device * )
778  . Purpose:
779  .      This sends the actual packet to the SMC9xxx chip.
780  .
781  . Algorithm:
782  .      First, see if a saved_skb is available.
783  .              ( this should NOT be called if there is no 'saved_skb'
784  .      Now, find the packet number that the chip allocated
785  .      Point the data pointers at it in memory
786  .      Set the length word in the chip's memory
787  .      Dump the packet to chip memory
788  .      Check if a last byte is needed ( odd length packet )
789  .              if so, set the control flag right
790  .      Tell the card to send it
791  .      Enable the transmit interrupt, so I know if it failed
792  .      Free the kernel data if I actually sent it.
793 */
794 static void smc_hardware_send_packet( struct net_device * dev )
795 {
796         struct smc_local *lp = (struct smc_local *)dev->priv;
797         byte                    packet_no;
798         struct sk_buff *        skb = lp->saved_skb;
799         word                    length;
800         unsigned long           ioaddr;
801         byte                    * buf;
802
803         PRINTK3("%s:smc_hardware_send_packet\n", dev->name);
804
805         ioaddr = dev->base_addr;
806
807         if ( !skb ) {
808                 PRINTK("%s: In XMIT with no packet to send \n", dev->name);
809                 return;
810         }
811         length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
812         buf = skb->data;
813
814         /* If I get here, I _know_ there is a packet slot waiting for me */
815         packet_no = SMC_GET_AR();
816         if ( packet_no & AR_FAILED ) {
817                 /* or isn't there?  BAD CHIP! */
818                 printk(KERN_DEBUG "%s: Memory allocation failed. \n",
819                         dev->name);
820                 dev_kfree_skb_any(skb);
821                 lp->saved_skb = NULL;
822                 netif_wake_queue(dev);
823                 return;
824         }
825
826         /* we have a packet address, so tell the card to use it */
827         SMC_SET_PN( packet_no );
828
829         /* point to the beginning of the packet */
830         SMC_SET_PTR( PTR_AUTOINC );
831
832         PRINTK3("%s: Trying to xmit packet of length %x\n",
833                 dev->name, length);
834
835 #if SMC_DEBUG > 2
836         printk("Transmitting Packet\n");
837         print_packet( buf, length );
838 #endif
839
840         /* send the packet length ( +6 for status, length and ctl byte )
841            and the status word ( set to zeros ) */
842 #ifdef CONFIG_SMC91111_USE_32_BIT
843         SMC_outl(  (length +6 ) << 16 , ioaddr + DATA_REG );
844 #else
845         SMC_outw( 0, ioaddr + DATA_REG );
846         /* send the packet length ( +6 for status words, length, and ctl*/
847         SMC_outw( (length+6), ioaddr + DATA_REG );
848 #endif
849
850         /* send the actual data
851          . I _think_ it's faster to send the longs first, and then
852          . mop up by sending the last word.  It depends heavily
853          . on alignment, at least on the 486.  Maybe it would be
854          . a good idea to check which is optimal?  But that could take
855          . almost as much time as is saved?
856         */
857 #ifdef CONFIG_SMC91111_USE_32_BIT
858         SMC_outsl(ioaddr + DATA_REG, buf,  length >> 2 );
859         if ( length & 0x2  )
860                 SMC_outw(*((word *)(buf + (length & 0xFFFFFFFC))),ioaddr +
861                                 DATA_REG);
862 #else
863         SMC_outsw(ioaddr + DATA_REG , buf, (length ) >> 1);
864 #endif // CONFIG_SMC91111_USE_32_BIT
865
866         /* Send the last byte, if there is one.   */
867         if ( (length & 1) == 0 ) {
868                 SMC_outw( 0, ioaddr + DATA_REG );
869         } else {
870                 SMC_outw( 0x2000 | buf[length -1 ], ioaddr + DATA_REG );
871         }
872
873         /* enable the interrupts */
874         SMC_ENABLE_INT( (IM_TX_INT | IM_TX_EMPTY_INT) );
875
876         /* and let the chipset deal with it */
877         SMC_SET_MMU_CMD( MC_ENQUEUE );
878
879         PRINTK2("%s: Sent packet of length %d \n", dev->name, length);
880
881         lp->saved_skb = NULL;
882         dev_kfree_skb_any (skb);
883
884         dev->trans_start = jiffies;
885
886         /* we can send another packet */
887         netif_wake_queue(dev);
888
889         return;
890 }
891
892 /*-------------------------------------------------------------------------
893  |
894  | smc_init( void )
895  |   Input parameters:
896  |      dev->base_addr == 0, try to find all possible locations
897  |      dev->base_addr > 0x1ff, this is the address to check
898  |      dev->base_addr == <anything else>, return failure code
899  |
900  |   Output:
901  |      0 --> there is a device
902  |      anything else, error
903  |
904  ---------------------------------------------------------------------------
905 */
906 static int __init smc_init( void )
907 {
908         int rtn;
909
910         PRINTK2(CARDNAME ":smc_init\n");
911
912 #ifdef MODULE
913         if (io == -1)
914                 printk(KERN_WARNING
915                 CARDNAME": You shouldn't use auto-probing with insmod!\n" );
916 #endif
917
918         if (global_dev) {
919                 printk(CARDNAME ": already initialized.\n");
920                 return -EBUSY;
921         }
922
923         global_dev = init_etherdev(0, sizeof(struct smc_local));
924         if (!global_dev) {
925                 printk(CARDNAME ": could not allocate device.\n");
926                 return -ENODEV;
927         }
928         SET_MODULE_OWNER(global_dev);
929
930         /* copy the parameters from insmod into the device structure */
931         if (io != -1)
932                 global_dev->base_addr   = io;
933         if (irq != -1)
934                 global_dev->irq = irq;
935         global_dev->dma         = nowait; // Use DMA field for nowait
936
937 #ifdef CONFIG_ISA
938         /*  try a specific location */
939         if (global_dev->base_addr > 0x1ff)
940                 rtn = smc_probe(global_dev, global_dev->base_addr);
941         else if (global_dev->base_addr != 0)
942                 rtn = -ENXIO;
943         else {
944                 int i;
945
946                 /* check every ethernet address */
947                 for (i = 0; smc_portlist[i]; i++) {
948                         rtn = smc_probe(global_dev, smc_portlist[i]);
949                         if (rtn == 0)
950                                 break;
951                 }
952         }
953 #else
954         if (global_dev->base_addr == -1) {
955                 printk(KERN_WARNING
956                 CARDNAME": SMC91111_BASE_ADDR not set!\n" );
957                 rtn = -ENXIO;
958         }
959         else
960                 rtn = smc_probe(global_dev,
961                                 (int)ioremap(global_dev->base_addr,
962                                              SMC_IO_EXTENT));
963 #endif
964
965         if (rtn != 0) {
966                 printk(CARDNAME ": not found.\n");
967                 /* couldn't find anything */
968 #ifndef CONFIG_ISA
969                 iounmap((void *)global_dev->base_addr);
970 #endif
971                 kfree(global_dev->priv);
972                 unregister_netdev(global_dev);
973                 kfree(global_dev);
974         }
975
976         return rtn;
977 }
978
979 /*----------------------------------------------------------------------
980  . smc_findirq
981  .
982  . This routine has a simple purpose -- make the SMC chip generate an
983  . interrupt, so an auto-detect routine can detect it, and find the IRQ,
984  ------------------------------------------------------------------------
985 */
986 int __init smc_findirq( unsigned long ioaddr )
987 {
988         int     timeout = 20;
989         unsigned long cookie;
990
991         PRINTK2(CARDNAME ":smc_findirq\n");
992
993         /* I have to do a STI() here, because this is called from
994            a routine that does an CLI during this process, making it
995            rather difficult to get interrupts for auto detection */
996         sti();
997
998         cookie = probe_irq_on();
999
1000         /*
1001          * What I try to do here is trigger an ALLOC_INT. This is done
1002          * by allocating a small chunk of memory, which will give an interrupt
1003          * when done.
1004          */
1005
1006
1007         SMC_SELECT_BANK(2);
1008         /* enable ALLOCation interrupts ONLY */
1009         SMC_SET_INT_MASK( IM_ALLOC_INT );
1010
1011         /*
1012          . Allocate 512 bytes of memory.  Note that the chip was just
1013          . reset so all the memory is available
1014         */
1015         SMC_SET_MMU_CMD( MC_ALLOC | 1 );
1016
1017         /*
1018          . Wait until positive that the interrupt has been generated
1019         */
1020         while ( timeout ) {
1021                 byte    int_status;
1022
1023                 int_status = SMC_GET_INT();
1024
1025                 if ( int_status & IM_ALLOC_INT )
1026                         break;          /* got the interrupt */
1027                 timeout--;
1028         }
1029
1030         /* there is really nothing that I can do here if timeout fails,
1031            as autoirq_report will return a 0 anyway, which is what I
1032            want in this case.   Plus, the clean up is needed in both
1033            cases.  */
1034
1035         /* DELAY HERE!
1036            On a fast machine, the status might change before the interrupt
1037            is given to the processor.  This means that the interrupt was
1038            never detected, and autoirq_report fails to report anything.
1039            This should fix autoirq_* problems.
1040         */
1041         mdelay(10);
1042
1043         /* and disable all interrupts again */
1044         SMC_SET_INT_MASK( 0 );
1045
1046         /* clear hardware interrupts again, because that's how it
1047            was when I was called... */
1048         cli();
1049
1050         /* and return what I found */
1051         return probe_irq_off(cookie);
1052 }
1053
1054 /*----------------------------------------------------------------------
1055  . Function: smc_probe( unsigned long ioaddr )
1056  .
1057  . Purpose:
1058  .      Tests to see if a given ioaddr points to an SMC91111 chip.
1059  .      Returns a 0 on success
1060  .
1061  . Algorithm:
1062  .      (1) see if the high byte of BANK_SELECT is 0x33
1063  .      (2) compare the ioaddr with the base register's address
1064  .      (3) see if I recognize the chip ID in the appropriate register
1065  .
1066  .---------------------------------------------------------------------
1067  */
1068 /*---------------------------------------------------------------
1069  . Here I do typical initialization tasks.
1070  .
1071  . o  Initialize the structure if needed
1072  . o  print out my vanity message if not done so already
1073  . o  print out what type of hardware is detected
1074  . o  print out the ethernet address
1075  . o  find the IRQ
1076  . o  set up my private data
1077  . o  configure the dev structure with my subroutines
1078  . o  actually GRAB the irq.
1079  . o  GRAB the region
1080  .-----------------------------------------------------------------
1081 */
1082 static int __init smc_probe(struct net_device *dev, unsigned long ioaddr)
1083 {
1084         int i, memory, retval;
1085         static unsigned version_printed;
1086         unsigned int    bank;
1087
1088         const char *version_string;
1089         const char *if_string;
1090
1091         /* registers */
1092         word    revision_register;
1093         word  base_address_register;
1094         word memory_info_register;
1095
1096         PRINTK2(CARDNAME ":smc_probe\n");
1097
1098         /* Grab the region so that no one else tries to probe our ioports. */
1099         if (!request_region(ioaddr, SMC_IO_EXTENT, dev->name))
1100                 return -EBUSY;
1101
1102         /* First, see if the high byte is 0x33 */
1103         bank = SMC_CURRENT_BANK();
1104         if ( (bank & 0xFF00) != 0x3300 ) {
1105                 if ( (bank & 0xFF) == 0x33 ) {
1106                         printk(CARDNAME
1107                                ": Detected possible byte-swapped interface"
1108                                " at IOADDR %lx\n", ioaddr);
1109                 }
1110                 retval = -ENODEV;
1111                 goto err_out;
1112         }
1113         /* The above MIGHT indicate a device, but I need to write to further
1114                 test this.  */
1115         SMC_SELECT_BANK(0);
1116         bank = SMC_CURRENT_BANK();
1117         if ( (bank & 0xFF00 ) != 0x3300 ) {
1118                 retval = -ENODEV;
1119                 goto err_out;
1120         }
1121         /* well, we've already written once, so hopefully another time won't
1122            hurt.  This time, I need to switch the bank register to bank 1,
1123            so I can access the base address register */
1124         SMC_SELECT_BANK(1);
1125         base_address_register = SMC_GET_BASE();
1126         base_address_register = ((base_address_register & 0xE000)
1127                                  | ((base_address_register & 0x1F00) >> 3));
1128         if ( (ioaddr & (PAGE_SIZE-1)) != base_address_register )  {
1129                 printk(CARDNAME ": IOADDR %lx doesn't match configuration (%x)."
1130                         "Probably not a SMC chip\n",
1131                         ioaddr, base_address_register );
1132                 /* well, the base address register didn't match.  Must not have
1133                    been a SMC chip after all. */
1134                 retval = -ENODEV;
1135                 goto err_out;
1136         }
1137
1138         /*  check if the revision register is something that I recognize.
1139             These might need to be added to later, as future revisions
1140             could be added.  */
1141         SMC_SELECT_BANK(3);
1142         revision_register  = SMC_GET_REV();
1143         if ( !chip_ids[ ( revision_register  >> 4 ) & 0xF  ] ) {
1144                 /* I don't recognize this chip, so... */
1145                 printk(CARDNAME ": IO %lx: Unrecognized revision register:"
1146                         " %x, Contact author. \n",
1147                         ioaddr, revision_register );
1148
1149                 retval = -ENODEV;
1150                 goto err_out;
1151         }
1152
1153         /* at this point I'll assume that the chip is an SMC9xxx.
1154            It might be prudent to check a listing of MAC addresses
1155            against the hardware address, or do some other tests. */
1156
1157         if (version_printed++ == 0)
1158                 printk("%s", version);
1159
1160         /* fill in some of the fields */
1161         dev->base_addr = ioaddr;
1162
1163         /*
1164          . Get the MAC address ( bank 1, regs 4 - 9 )
1165         */
1166         SMC_SELECT_BANK( 1 );
1167         for ( i = 0; i < 6; i += 2 ) {
1168                 word    address;
1169
1170                 address = SMC_inw( ioaddr + ADDR0_REG + i  );
1171                 dev->dev_addr[ i + 1] = address >> 8;
1172                 dev->dev_addr[ i ] = address & 0xFF;
1173         }
1174
1175         /* get the memory information */
1176
1177         SMC_SELECT_BANK( 0 );
1178         memory_info_register = SMC_GET_MIR();
1179         memory = memory_info_register & (word)0x00ff;
1180         memory *= LAN91C111_MEMORY_MULTIPLIER;
1181
1182         /*
1183          Now, I want to find out more about the chip.  This is sort of
1184          redundant, but it's cleaner to have it in both, rather than having
1185          one VERY long probe procedure.
1186         */
1187         SMC_SELECT_BANK(3);
1188         revision_register  = SMC_GET_REV();
1189         version_string = chip_ids[ ( revision_register  >> 4 ) & 0xF  ];
1190         if ( !version_string ) {
1191                 /* I shouldn't get here because this call was done before.... */
1192                 retval = -ENODEV;
1193                 goto err_out;
1194         }
1195
1196
1197         /* now, reset the chip, and put it into a known state */
1198         smc_reset( dev );
1199
1200         /*
1201          . If dev->irq is 0, then the device has to be banged on to see
1202          . what the IRQ is.
1203          .
1204          . This banging doesn't always detect the IRQ, for unknown reasons.
1205          . a workaround is to reset the chip and try again.
1206          .
1207          . Interestingly, the DOS packet driver *SETS* the IRQ on the card to
1208          . be what is requested on the command line.   I don't do that, mostly
1209          . because the card that I have uses a non-standard method of accessing
1210          . the IRQs, and because this _should_ work in most configurations.
1211          .
1212          . Specifying an IRQ is done with the assumption that the user knows
1213          . what (s)he is doing.  No checking is done!!!!
1214          .
1215         */
1216         if ( dev->irq < 2 ) {
1217                 int     trials;
1218
1219                 trials = 3;
1220                 while ( trials-- ) {
1221                         dev->irq = smc_findirq( ioaddr );
1222                         if ( dev->irq )
1223                                 break;
1224                         /* kick the card and try again */
1225                         smc_reset( dev );
1226                 }
1227         }
1228         if (dev->irq == 0 ) {
1229                 printk("%s: Couldn't autodetect your IRQ. Use irq=xx.\n",
1230                         dev->name);
1231                 retval = -ENODEV;
1232                 goto err_out;
1233         }
1234         if (dev->irq == 2) {
1235                 /* Fixup for users that don't know that IRQ 2 is really IRQ 9,
1236                  * or don't know which one to set.
1237                  */
1238                 dev->irq = 9;
1239         }
1240
1241         /* now, print out the card info, in a short format.. */
1242
1243         printk("%s: %s(r:%d) at %#lx IRQ:%d\n", dev->name,
1244                version_string, revision_register & 0xF, ioaddr, dev->irq );
1245         printk(" INTF:%s MEM:%db NOWAIT:%d", if_string, memory, dev->dma );
1246         /*
1247          . Print the Ethernet address
1248         */
1249         printk("  ADDR ");
1250         for (i = 0; i < 5; i++)
1251                 printk("%2.2x:", dev->dev_addr[i] );
1252         printk("%2.2x \n", dev->dev_addr[5] );
1253
1254         /* set the private data to zero by default */
1255         memset(dev->priv, 0, sizeof(struct smc_local));
1256
1257         /* Fill in the fields of the device structure with ethernet values. */
1258         ether_setup(dev);
1259
1260         /* Grab the IRQ */
1261         retval = request_irq(dev->irq, &smc_interrupt, 0, dev->name, dev);
1262         if (retval) {
1263                 goto err_out;
1264         }
1265
1266         dev->open                       = smc_open;
1267         dev->stop                       = smc_close;
1268         dev->hard_start_xmit            = smc_wait_to_send_packet;
1269         dev->tx_timeout                 = smc_timeout;
1270         dev->watchdog_timeo             = HZ/20;
1271         dev->get_stats                  = smc_query_statistics;
1272         dev->set_multicast_list         = smc_set_multicast_list;
1273
1274         return 0;
1275
1276 err_out:
1277         release_region(ioaddr, SMC_IO_EXTENT);
1278         return retval;
1279 }
1280
1281 #if SMC_DEBUG > 2
1282 static void print_packet( byte * buf, int length )
1283 {
1284 #if 1
1285 #if SMC_DEBUG > 3
1286         int i;
1287         int remainder;
1288         int lines;
1289 #endif
1290
1291         printk("Packet of length %d \n", length );
1292
1293 #if SMC_DEBUG > 3
1294         lines = length / 16;
1295         remainder = length % 16;
1296
1297         for ( i = 0; i < lines ; i ++ ) {
1298                 int cur;
1299
1300                 for ( cur = 0; cur < 8; cur ++ ) {
1301                         byte a, b;
1302
1303                         a = *(buf ++ );
1304                         b = *(buf ++ );
1305                         printk("%02x%02x ", a, b );
1306                 }
1307                 printk("\n");
1308         }
1309         for ( i = 0; i < remainder/2 ; i++ ) {
1310                 byte a, b;
1311
1312                 a = *(buf ++ );
1313                 b = *(buf ++ );
1314                 printk("%02x%02x ", a, b );
1315         }
1316         printk("\n");
1317 #endif
1318 #endif
1319 }
1320 #endif
1321
1322
1323 /*
1324  * Open and Initialize the board
1325  *
1326  * Set up everything, reset the card, etc ..
1327  *
1328  */
1329 static int smc_open(struct net_device *dev)
1330 {
1331         struct smc_local *lp    = (struct smc_local *)dev->priv;
1332         unsigned long ioaddr    = dev->base_addr;
1333         int     i;      /* used to set hw ethernet address */
1334
1335         PRINTK2("%s:smc_open\n", dev->name);
1336
1337         /* clear out all the junk that was put here before... */
1338         memset(dev->priv, 0, sizeof(struct smc_local));
1339
1340         // Setup the default Register Modes
1341         lp->tcr_cur_mode = TCR_DEFAULT;
1342         lp->rcr_cur_mode = RCR_DEFAULT;
1343         lp->rpc_cur_mode = RPC_DEFAULT;
1344
1345         // Set default parameters (files)
1346         lp->ctl_swfdup = 0;
1347         lp->ctl_ephloop = 0;
1348         lp->ctl_miiop = 0;
1349         lp->ctl_autoneg = 1;
1350         lp->ctl_rfduplx = 1;
1351         lp->ctl_rspeed = 100;
1352         lp->ctl_afduplx = 1;
1353         lp->ctl_aspeed = 100;
1354         lp->ctl_lnkfail = 1;
1355         lp->ctl_forcol = 0;
1356         lp->ctl_filtcar = 0;
1357
1358         /* reset the hardware */
1359
1360         smc_reset( dev );
1361         smc_enable( dev );
1362
1363         /* Configure the PHY */
1364         smc_phy_configure(dev);
1365
1366         /*
1367                 According to Becker, I have to set the hardware address
1368                 at this point, because the (l)user can set it with an
1369                 ioctl.  Easily done...
1370         */
1371         SMC_SELECT_BANK( 1 );
1372         for ( i = 0; i < 6; i += 2 ) {
1373                 word    address;
1374
1375                 address = dev->dev_addr[ i + 1 ] << 8 ;
1376                 address  |= dev->dev_addr[ i ];
1377                 SMC_outw( address, ioaddr + ADDR0_REG + i );
1378         }
1379
1380 #ifdef CONFIG_SYSCTL
1381         smc_sysctl_register(dev);
1382 #endif /* CONFIG_SYSCTL */
1383
1384         netif_start_queue(dev);
1385         return 0;
1386 }
1387
1388 /*--------------------------------------------------------
1389  . Called by the kernel to send a packet out into the void
1390  . of the net.  This routine is largely based on
1391  . skeleton.c, from Becker.
1392  .--------------------------------------------------------
1393 */
1394 static void smc_timeout(struct net_device *dev)
1395 {
1396         struct smc_local *lp    = (struct smc_local *)dev->priv;
1397
1398         PRINTK3("%s:smc_timeout\n", dev->name);
1399
1400         /* If we get here, some higher level has decided we are broken.
1401            There should really be a "kick me" function call instead. */
1402         printk(KERN_WARNING "%s: transmit timed out, %s?\n",
1403                 dev->name, tx_done(dev) ? "IRQ conflict" :
1404                 "network cable problem");
1405         /* "kick" the adaptor */
1406         smc_reset( dev );
1407         smc_enable( dev );
1408
1409 #if 0
1410         /* Reconfiguring the PHY doesn't seem like a bad idea here, but
1411          * it introduced a problem.  Now that this is a timeout routine,
1412          * we are getting called from within an interrupt context.
1413          * smc_phy_configure() calls smc_wait_ms() which calls
1414          * schedule_timeout() which calls schedule().  When schedule()
1415          * is called from an interrupt context, it prints out
1416          * "Scheduling in interrupt" and then calls BUG().  This is
1417          * obviously not desirable.  This was worked around by removing
1418          * the call to smc_phy_configure() here because it didn't seem
1419          * absolutely necessary.  Ultimately, if smc_wait_ms() is
1420          * supposed to be usable from an interrupt context (which it
1421          * looks like it thinks it should handle), it should be fixed.
1422          */
1423         /* Reconfigure the PHY */
1424         smc_phy_configure(dev);
1425 #endif
1426         dev->trans_start = jiffies;
1427         /* clear anything saved */
1428         if (lp->saved_skb != NULL) {
1429                 dev_kfree_skb (lp->saved_skb);
1430                 lp->saved_skb = NULL;
1431         }
1432         ((struct smc_local *)dev->priv)->saved_skb = NULL;
1433         netif_wake_queue(dev);
1434 }
1435
1436 /*--------------------------------------------------------------------
1437  .
1438  . This is the main routine of the driver, to handle the device when
1439  . it needs some attention.
1440  .
1441  . So:
1442  .   first, save state of the chipset
1443  .   branch off into routines to handle each case, and acknowledge
1444  .          each to the interrupt register
1445  .   and finally restore state.
1446  .
1447  ---------------------------------------------------------------------*/
1448 static void smc_interrupt(int irq, void * dev_id,  struct pt_regs * regs)
1449 {
1450         struct net_device *dev  = dev_id;
1451         unsigned long ioaddr    = dev->base_addr;
1452         struct smc_local *lp    = (struct smc_local *)dev->priv;
1453
1454         byte    status;
1455         word    card_stats;
1456         byte    mask;
1457         int     timeout;
1458         /* state registers */
1459         word    saved_bank;
1460         word    saved_pointer;
1461
1462         PRINTK3("%s: SMC interrupt started \n", dev->name);
1463
1464         if (dev == NULL) {
1465                 printk(KERN_WARNING "%s: irq %d for unknown device.\n",
1466                         dev->name, irq);
1467                 return;
1468         }
1469
1470         saved_bank = SMC_CURRENT_BANK();
1471
1472         SMC_SELECT_BANK(2);
1473         saved_pointer = SMC_GET_PTR();
1474
1475         /* read the interrupt mask register */
1476         mask = SMC_GET_INT_MASK();
1477
1478         /* disable all interrupts */
1479         SMC_SET_INT_MASK( 0 );
1480
1481         /* set a timeout value, so I don't stay here forever */
1482         timeout = 4;
1483
1484         PRINTK2(KERN_WARNING "%s: MASK IS %x \n", dev->name, mask);
1485         do {
1486                 /* read the status flag, and mask it */
1487                 status = SMC_GET_INT() & mask;
1488                 if (!status )
1489                         break;
1490
1491                 PRINTK3(KERN_WARNING "%s: Handling interrupt status %x \n",
1492                         dev->name, status);
1493
1494                 if (status & IM_RCV_INT) {
1495                         /* Got a packet(s). */
1496                         PRINTK2(KERN_WARNING
1497                                 "%s: Receive Interrupt\n", dev->name);
1498                         smc_rcv(dev);
1499                 } else if (status & IM_TX_INT ) {
1500                         PRINTK2(KERN_WARNING "%s: TX ERROR handled\n",
1501                                 dev->name);
1502                         smc_tx(dev);
1503                         // Acknowledge the interrupt
1504                         SMC_ACK_INT( IM_TX_INT );
1505                 } else if (status & IM_TX_EMPTY_INT ) {
1506                         /* update stats */
1507                         SMC_SELECT_BANK( 0 );
1508                         card_stats = SMC_GET_COUNTER();
1509                         /* single collisions */
1510                         lp->stats.collisions += card_stats & 0xF;
1511                         card_stats >>= 4;
1512                         /* multiple collisions */
1513                         lp->stats.collisions += card_stats & 0xF;
1514
1515                         /* these are for when linux supports these statistics */
1516
1517                         SMC_SELECT_BANK( 2 );
1518                         PRINTK2(KERN_WARNING "%s: TX_BUFFER_EMPTY handled\n",
1519                                 dev->name);
1520                         // Acknowledge the interrupt
1521                         SMC_ACK_INT( IM_TX_EMPTY_INT );
1522                         mask &= ~IM_TX_EMPTY_INT;
1523                         lp->stats.tx_packets += lp->packets_waiting;
1524                         lp->packets_waiting = 0;
1525
1526                 } else if (status & IM_ALLOC_INT ) {
1527                         PRINTK2(KERN_DEBUG "%s: Allocation interrupt \n",
1528                                 dev->name);
1529                         /* clear this interrupt so it doesn't happen again */
1530                         mask &= ~IM_ALLOC_INT;
1531
1532                         smc_hardware_send_packet( dev );
1533
1534                         /* enable xmit interrupts based on this */
1535                         mask |= ( IM_TX_EMPTY_INT | IM_TX_INT );
1536
1537                         /* and let the card send more packets to me */
1538                         netif_wake_queue(dev);
1539
1540                         PRINTK2("%s: Handoff done successfully.\n",
1541                                 dev->name);
1542                 } else if (status & IM_RX_OVRN_INT ) {
1543                         lp->stats.rx_errors++;
1544                         lp->stats.rx_fifo_errors++;
1545                         // Acknowledge the interrupt
1546                         SMC_ACK_INT( IM_RX_OVRN_INT );
1547                 } else if (status & IM_EPH_INT ) {
1548                         PRINTK("%s: UNSUPPORTED: EPH INTERRUPT \n",
1549                                 dev->name);
1550                 } else if (status & IM_MDINT ) {
1551                         smc_phy_interrupt(dev);
1552                         // Acknowledge the interrupt
1553                         SMC_ACK_INT( IM_MDINT );
1554                 } else if (status & IM_ERCV_INT ) {
1555                         PRINTK("%s: UNSUPPORTED: ERCV INTERRUPT \n",
1556                                 dev->name);
1557                         // Acknowledge the interrupt
1558                         SMC_ACK_INT( IM_ERCV_INT );
1559                 }
1560         } while ( timeout -- );
1561
1562
1563         /* restore register states */
1564
1565         SMC_SELECT_BANK( 2 );
1566
1567         SMC_SET_INT_MASK( mask );
1568
1569         PRINTK3( KERN_WARNING "%s: MASK is now %x \n", dev->name, mask);
1570         SMC_SET_PTR( saved_pointer );
1571
1572         SMC_SELECT_BANK( saved_bank );
1573
1574         PRINTK3("%s: Interrupt done\n", dev->name);
1575         return;
1576 }
1577
1578 /*-------------------------------------------------------------
1579  .
1580  . smc_rcv -  receive a packet from the card
1581  .
1582  . There is ( at least ) a packet waiting to be read from
1583  . chip-memory.
1584  .
1585  . o Read the status
1586  . o If an error, record it
1587  . o otherwise, read in the packet
1588  --------------------------------------------------------------
1589 */
1590 static void smc_rcv(struct net_device *dev)
1591 {
1592         struct smc_local *lp = (struct smc_local *)dev->priv;
1593         unsigned long ioaddr  = dev->base_addr;
1594         int     packet_number;
1595         word    status;
1596         word    packet_length;
1597
1598         PRINTK3("%s:smc_rcv\n", dev->name);
1599
1600         /* assume bank 2 */
1601
1602         packet_number = SMC_GET_RXFIFO();
1603
1604         if ( packet_number & RXFIFO_REMPTY ) {
1605
1606                 /* we got called , but nothing was on the FIFO */
1607                 PRINTK("%s: WARNING: smc_rcv with nothing on FIFO. \n",
1608                         dev->name);
1609                 /* don't need to restore anything */
1610                 return;
1611         }
1612
1613         /*  start reading from the start of the packet */
1614         SMC_SET_PTR( PTR_READ | PTR_RCV | PTR_AUTOINC );
1615
1616         /* First two words are status and packet_length */
1617         status          = SMC_inw( ioaddr + DATA_REG );
1618         packet_length   = SMC_inw( ioaddr + DATA_REG );
1619
1620         packet_length &= 0x07ff;  /* mask off top bits */
1621
1622         PRINTK2("RCV: STATUS %4x LENGTH %4x\n", status, packet_length );
1623
1624         if ( !(status & RS_ERRORS ) ){
1625                 /* do stuff to make a new packet */
1626                 struct sk_buff  * skb;
1627                 byte            * data;
1628
1629                 /* set multicast stats */
1630                 if ( status & RS_MULTICAST )
1631                         lp->stats.multicast++;
1632
1633                 // Allocate enough memory for entire receive frame, to be safe
1634                 skb = dev_alloc_skb( packet_length );
1635
1636                 /* Adjust for having already read the first two words */
1637                 packet_length -= 4;
1638
1639                 if ( skb == NULL ) {
1640                         printk(KERN_NOTICE "%s: Low memory, packet dropped.\n",
1641                                 dev->name);
1642                         lp->stats.rx_dropped++;
1643                         goto done;
1644                 }
1645
1646                 /*
1647                  ! This should work without alignment, but it could be
1648                  ! in the worse case
1649                 */
1650
1651                 skb_reserve( skb, 2 );   /* 16 bit alignment */
1652
1653                 skb->dev = dev;
1654
1655                 // set odd length for bug in LAN91C111,
1656                 // which never sets RS_ODDFRAME
1657                 data = skb_put( skb, packet_length + 1 );
1658
1659 #ifdef CONFIG_SMC91111_USE_32_BIT
1660                 PRINTK3(" Reading %d dwords (and %d bytes) \n",
1661                         packet_length >> 2, packet_length & 3 );
1662                 /* QUESTION:  Like in the TX routine, do I want
1663                    to send the DWORDs or the bytes first, or some
1664                    mixture.  A mixture might improve already slow PIO
1665                    performance  */
1666                 SMC_insl(ioaddr + DATA_REG , data, packet_length >> 2 );
1667                 /* read the left over bytes */
1668 #ifdef CONFIG_SMC91111_USE_8_BIT
1669                 SMC_insb( ioaddr + DATA_REG, data + (packet_length & 0xFFFFFC),
1670                           packet_length & 0x3  );
1671 #else
1672                 if (packet_length & 0x3)
1673                 {
1674                         unsigned long remaining_data;
1675                         insl(ioaddr + DATA_REG , &remaining_data, 1);
1676                         memcpy(data + (packet_length & 0xFFFFFC),
1677                                &remaining_data, packet_length & 0x3  );
1678                 }
1679 #endif // CONFIG_SMC91111_USE_8_BIT
1680 #else
1681                 PRINTK3(" Reading %d words and %d byte(s) \n",
1682                         (packet_length >> 1 ), packet_length & 1 );
1683                 SMC_insw(ioaddr + DATA_REG , data, packet_length >> 1);
1684
1685 #endif // CONFIG_SMC91111_USE_32_BIT
1686
1687 #if     SMC_DEBUG > 2
1688                 printk("Receiving Packet\n");
1689                 print_packet( data, packet_length );
1690 #endif
1691
1692                 skb->protocol = eth_type_trans(skb, dev );
1693                 netif_rx(skb);
1694                 dev->last_rx = jiffies;
1695                 lp->stats.rx_packets++;
1696                 lp->stats.rx_bytes += packet_length;
1697         } else {
1698                 /* error ... */
1699                 lp->stats.rx_errors++;
1700
1701                 if ( status & RS_ALGNERR )  lp->stats.rx_frame_errors++;
1702                 if ( status & (RS_TOOSHORT | RS_TOOLONG ) )
1703                         lp->stats.rx_length_errors++;
1704                 if ( status & RS_BADCRC)        lp->stats.rx_crc_errors++;
1705         }
1706
1707         while ( SMC_GET_MMU_CMD() & MC_BUSY )
1708                 udelay(1); // Wait until not busy
1709
1710 done:
1711         /*  error or good, tell the card to get rid of this packet */
1712         SMC_SET_MMU_CMD( MC_RELEASE );
1713 }
1714
1715
1716 /*************************************************************************
1717  . smc_tx
1718  .
1719  . Purpose:  Handle a transmit error message.   This will only be called
1720  .   when an error, because of the AUTO_RELEASE mode.
1721  .
1722  . Algorithm:
1723  .      Save pointer and packet no
1724  .      Get the packet no from the top of the queue
1725  .      check if it's valid ( if not, is this an error??? )
1726  .      read the status word
1727  .      record the error
1728  .      ( resend?  Not really, since we don't want old packets around )
1729  .      Restore saved values
1730  ************************************************************************/
1731 static void smc_tx( struct net_device * dev )
1732 {
1733         unsigned long  ioaddr = dev->base_addr;
1734         struct smc_local *lp = (struct smc_local *)dev->priv;
1735         byte saved_packet;
1736         byte packet_no;
1737         word tx_status;
1738
1739
1740         PRINTK3("%s:smc_tx\n", dev->name);
1741
1742         /* assume bank 2  */
1743
1744         saved_packet = SMC_GET_PN();
1745         packet_no = SMC_GET_RXFIFO();
1746         packet_no &= 0x7F;
1747
1748         /* If the TX FIFO is empty then nothing to do */
1749         if ( packet_no & TXFIFO_TEMPTY )
1750                 return;
1751
1752         /* select this as the packet to read from */
1753         SMC_SET_PN( packet_no );
1754
1755         /* read the first word (status word) from this packet */
1756         SMC_SET_PTR( PTR_AUTOINC | PTR_READ );
1757
1758         tx_status = SMC_inw( ioaddr + DATA_REG );
1759         PRINTK3("%s: TX DONE STATUS: %4x \n", dev->name, tx_status);
1760
1761         lp->stats.tx_errors++;
1762         if ( tx_status & TS_LOSTCAR ) lp->stats.tx_carrier_errors++;
1763         if ( tx_status & TS_LATCOL  ) {
1764                 printk(KERN_DEBUG
1765                         "%s: Late collision occurred on last xmit.\n",
1766                         dev->name);
1767                 lp->stats.tx_window_errors++;
1768                 lp->ctl_forcol = 0; // Reset forced collsion
1769         }
1770 #if 0
1771         if ( tx_status & TS_16COL ) { ... }
1772 #endif
1773
1774         if ( tx_status & TS_SUCCESS ) {
1775                 printk("%s: Successful packet caused interrupt \n", dev->name);
1776         }
1777         /* re-enable transmit */
1778         SMC_SELECT_BANK( 0 );
1779         SMC_SET_TCR( SMC_GET_TCR() | TCR_ENABLE );
1780
1781         /* kill the packet */
1782         SMC_SELECT_BANK( 2 );
1783         SMC_SET_MMU_CMD( MC_FREEPKT );
1784
1785         /* one less packet waiting for me */
1786         lp->packets_waiting--;
1787
1788         /* Don't change Packet Number Reg until busy bit is cleared */
1789         /* Per LAN91C111 Spec, Page 50 */
1790         while ( SMC_GET_MMU_CMD() & MC_BUSY );
1791
1792         SMC_SET_PN( saved_packet );
1793         return;
1794 }
1795
1796
1797 /*----------------------------------------------------
1798  . smc_close
1799  .
1800  . this makes the board clean up everything that it can
1801  . and not talk to the outside world.   Caused by
1802  . an 'ifconfig ethX down'
1803  .
1804  -----------------------------------------------------*/
1805 static int smc_close(struct net_device *dev)
1806 {
1807         PRINTK2("%s:smc_close\n", dev->name);
1808
1809         netif_stop_queue(dev);
1810
1811 #ifdef CONFIG_SYSCTL
1812         smc_sysctl_unregister(dev);
1813 #endif /* CONFIG_SYSCTL */
1814
1815         /* clear everything */
1816         smc_shutdown( dev->base_addr );
1817
1818         /* Update the statistics here. */
1819         return 0;
1820 }
1821
1822 /*------------------------------------------------------------
1823  . Get the current statistics.
1824  . This may be called with the card open or closed.
1825  .-------------------------------------------------------------*/
1826 static struct net_device_stats* smc_query_statistics(struct net_device *dev) {
1827         struct smc_local *lp = (struct smc_local *)dev->priv;
1828
1829         PRINTK2("%s:smc_query_statistics\n", dev->name);
1830
1831         return &lp->stats;
1832 }
1833
1834 /*-----------------------------------------------------------
1835  . smc_set_multicast_list
1836  .
1837  . This routine will, depending on the values passed to it,
1838  . either make it accept multicast packets, go into
1839  . promiscuous mode ( for TCPDUMP and cousins ) or accept
1840  . a select set of multicast packets
1841 */
1842 static void smc_set_multicast_list(struct net_device *dev)
1843 {
1844         unsigned long ioaddr = dev->base_addr;
1845
1846         PRINTK2("%s:smc_set_multicast_list\n", dev->name);
1847
1848         SMC_SELECT_BANK(0);
1849         if ( dev->flags & IFF_PROMISC ) {
1850                 PRINTK2("%s:smc_set_multicast_list:RCR_PRMS\n", dev->name);
1851                 SMC_SET_RCR( SMC_GET_RCR() | RCR_PRMS );
1852         }
1853
1854 /* BUG?  I never disable promiscuous mode if multicasting was turned on.
1855    Now, I turn off promiscuous mode, but I don't do anything to multicasting
1856    when promiscuous mode is turned on.
1857 */
1858
1859         /* Here, I am setting this to accept all multicast packets.
1860            I don't need to zero the multicast table, because the flag is
1861            checked before the table is
1862         */
1863         else if (dev->flags & IFF_ALLMULTI) {
1864                 SMC_SET_RCR( SMC_GET_RCR() | RCR_ALMUL );
1865                 PRINTK2("%s:smc_set_multicast_list:RCR_ALMUL\n", dev->name);
1866         }
1867
1868         /* We just get all multicast packets even if we only want them
1869          . from one source.  This will be changed at some future
1870          . point. */
1871         else if (dev->mc_count )  {
1872                 /* support hardware multicasting */
1873
1874                 /* be sure I get rid of flags I might have set */
1875                 SMC_SET_RCR( SMC_GET_RCR() & ~(RCR_PRMS | RCR_ALMUL) );
1876                 /* NOTE: this has to set the bank, so make sure it is the
1877                    last thing called.  The bank is set to zero at the top */
1878                 smc_setmulticast( ioaddr, dev->mc_count, dev->mc_list );
1879         } else  {
1880                 PRINTK2("%s:smc_set_multicast_list:~(RCR_PRMS|RCR_ALMUL)\n",
1881                         dev->name);
1882                 SMC_SET_RCR( SMC_GET_RCR() & ~(RCR_PRMS | RCR_ALMUL) );
1883
1884                 /*
1885                   since I'm disabling all multicast entirely, I need to
1886                   clear the multicast list
1887                 */
1888                 SMC_SELECT_BANK( 3 );
1889                 SMC_CLEAR_MCAST();
1890         }
1891 }
1892
1893 /*------------------------------------------------------------
1894  . Cleanup when module is removed with rmmod
1895  .-------------------------------------------------------------*/
1896 static void __exit smc_cleanup(void)
1897 {
1898         /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1899         unregister_netdev(global_dev);
1900
1901         free_irq(global_dev->irq, global_dev);
1902         release_region(global_dev->base_addr, SMC_IO_EXTENT);
1903
1904 #ifndef CONFIG_ISA
1905         iounmap((void *)global_dev->base_addr);
1906 #endif
1907
1908         kfree(global_dev);
1909         global_dev = NULL;
1910 }
1911
1912 module_init(smc_init);
1913 module_exit(smc_cleanup);
1914
1915
1916 #ifdef CONFIG_SYSCTL
1917 /*------------------------------------------------------------
1918  . Modify a bit in the LAN91C111 register set
1919  .-------------------------------------------------------------*/
1920 static word smc_modify_regbit(int bank, unsigned long ioaddr, int reg,
1921         unsigned int bit, int val)
1922 {
1923         word regval;
1924
1925         SMC_SELECT_BANK( bank );
1926
1927         regval = SMC_inw( ioaddr+reg );
1928         if (val)
1929                 regval |= bit;
1930         else
1931                 regval &= ~bit;
1932
1933         SMC_outw( regval, ioaddr );
1934         return(regval);
1935 }
1936
1937
1938 /*------------------------------------------------------------
1939  . Retrieve a bit in the LAN91C111 register set
1940  .-------------------------------------------------------------*/
1941 static int smc_get_regbit(int bank, unsigned long ioaddr,
1942                           int reg, unsigned int bit)
1943 {
1944         SMC_SELECT_BANK( bank );
1945         if ( SMC_inw( ioaddr+reg ) & bit)
1946                 return(1);
1947         else
1948                 return(0);
1949 }
1950
1951
1952 /*------------------------------------------------------------
1953  . Modify a LAN91C111 register (word access only)
1954  .-------------------------------------------------------------*/
1955 static void smc_modify_reg(int bank, unsigned long ioaddr,
1956                            int reg, word val)
1957 {
1958         SMC_SELECT_BANK( bank );
1959         SMC_outw( val, ioaddr+reg );
1960 }
1961
1962
1963 /*------------------------------------------------------------
1964  . Retrieve a LAN91C111 register (word access only)
1965  .-------------------------------------------------------------*/
1966 static int smc_get_reg(int bank, unsigned long ioaddr,
1967                        int reg)
1968 {
1969         SMC_SELECT_BANK( bank );
1970         return(SMC_inw( ioaddr+reg ));
1971 }
1972
1973
1974 static const char smc_info_string[] =
1975 "\n"
1976 "info           Provides this information blurb\n"
1977 "swver          Prints the software version information of this driver\n"
1978 "autoneg        Auto-negotiate Mode = 1\n"
1979 "rspeed         Requested Speed, 100=100Mbps, 10=10Mpbs\n"
1980 "rfduplx        Requested Full Duplex Operation\n"
1981 "aspeed         Actual Speed, 100=100Mbps, 10=10Mpbs\n"
1982 "afduplx        Actual Full Duplex Operation\n"
1983 "lnkfail        PHY Link Failure when 1\n"
1984 "miiop          External MII when 1, Internal PHY when 0\n"
1985 "swfdup         Switched Full Duplex Mode (allowed only in MII operation)\n"
1986 "ephloop        EPH Block Loopback\n"
1987 "forcol         Force a collision\n"
1988 "filtcar        Filter leading edge of carrier sense for 12 bit times\n"
1989 "freemem        Free buffer memory in bytes\n"
1990 "totmem         Total buffer memory in bytes\n"
1991 "leda           Output of LED-A (green)\n"
1992 "ledb           Output of LED-B (yellow)\n"
1993 "chiprev        Revision ID of the LAN91C111 chip\n"
1994 "";
1995
1996 /*------------------------------------------------------------
1997  . Sysctl handler for all integer parameters
1998  .-------------------------------------------------------------*/
1999 static int smc_sysctl_handler(ctl_table *ctl, int write, struct file * filp,
2000                                 void *buffer, size_t *lenp)
2001 {
2002         struct net_device *dev = (struct net_device*)ctl->extra1;
2003         struct smc_local *lp = (struct smc_local *)ctl->extra2;
2004         unsigned long ioaddr = dev->base_addr;
2005         int *valp = ctl->data;
2006         int val;
2007         int ret;
2008
2009         // Update parameters from the real registers
2010         switch (ctl->ctl_name)
2011         {
2012         case CTL_SMC_FORCOL:
2013                 *valp = smc_get_regbit(0, ioaddr, TCR_REG, TCR_FORCOL);
2014                 break;
2015
2016         case CTL_SMC_FREEMEM:
2017                 *valp = ( (word)smc_get_reg(0, ioaddr, MIR_REG) >> 8 )
2018                         * LAN91C111_MEMORY_MULTIPLIER;
2019                 break;
2020
2021
2022         case CTL_SMC_TOTMEM:
2023                 *valp = ( smc_get_reg(0, ioaddr, MIR_REG) & (word)0x00ff )
2024                         * LAN91C111_MEMORY_MULTIPLIER;
2025                 break;
2026
2027         case CTL_SMC_CHIPREV:
2028                 *valp = smc_get_reg(3, ioaddr, REV_REG);
2029                 break;
2030
2031         case CTL_SMC_AFDUPLX:
2032                 *valp = (lp->lastPhy18 & PHY_INT_DPLXDET) ? 1 : 0;
2033                 break;
2034
2035         case CTL_SMC_ASPEED:
2036                 *valp = (lp->lastPhy18 & PHY_INT_SPDDET) ? 100 : 10;
2037                 break;
2038
2039         case CTL_SMC_LNKFAIL:
2040                 *valp = (lp->lastPhy18 & PHY_INT_LNKFAIL) ? 1 : 0;
2041                 break;
2042
2043         case CTL_SMC_LEDA:
2044                 *valp = (lp->rpc_cur_mode >> RPC_LSXA_SHFT) & (word)0x0007;
2045                 break;
2046
2047         case CTL_SMC_LEDB:
2048                 *valp = (lp->rpc_cur_mode >> RPC_LSXB_SHFT) & (word)0x0007;
2049                 break;
2050
2051         case CTL_SMC_MIIOP:
2052                 *valp = smc_get_regbit(1, ioaddr, CONFIG_REG, CONFIG_EXT_PHY);
2053                 break;
2054
2055 #if SMC_DEBUG > 1
2056         case CTL_SMC_REG_BSR:   // Bank Select
2057                 *valp = smc_get_reg(0, ioaddr, BSR_REG);
2058                 break;
2059
2060         case CTL_SMC_REG_TCR:   // Transmit Control
2061                 *valp = smc_get_reg(0, ioaddr, TCR_REG);
2062                 break;
2063
2064         case CTL_SMC_REG_ESR:   // EPH Status
2065                 *valp = smc_get_reg(0, ioaddr, EPH_STATUS_REG);
2066                 break;
2067
2068         case CTL_SMC_REG_RCR:   // Receive Control
2069                 *valp = smc_get_reg(0, ioaddr, RCR_REG);
2070                 break;
2071
2072         case CTL_SMC_REG_CTRR:  // Counter
2073                 *valp = smc_get_reg(0, ioaddr, COUNTER_REG);
2074                 break;
2075
2076         case CTL_SMC_REG_MIR:   // Memory Information
2077                 *valp = smc_get_reg(0, ioaddr, MIR_REG);
2078                 break;
2079
2080         case CTL_SMC_REG_RPCR:  // Receive/Phy Control
2081                 *valp = smc_get_reg(0, ioaddr, RPC_REG);
2082                 break;
2083
2084         case CTL_SMC_REG_CFGR:  // Configuration
2085                 *valp = smc_get_reg(1, ioaddr, CONFIG_REG);
2086                 break;
2087
2088         case CTL_SMC_REG_BAR:   // Base Address
2089                 *valp = smc_get_reg(1, ioaddr, BASE_REG);
2090                 break;
2091
2092         case CTL_SMC_REG_IAR0:  // Individual Address
2093                 *valp = smc_get_reg(1, ioaddr, ADDR0_REG);
2094                 break;
2095
2096         case CTL_SMC_REG_IAR1:  // Individual Address
2097                 *valp = smc_get_reg(1, ioaddr, ADDR1_REG);
2098                 break;
2099
2100         case CTL_SMC_REG_IAR2:  // Individual Address
2101                 *valp = smc_get_reg(1, ioaddr, ADDR2_REG);
2102                 break;
2103
2104         case CTL_SMC_REG_GPR:   // General Purpose
2105                 *valp = smc_get_reg(1, ioaddr, GP_REG);
2106                 break;
2107
2108         case CTL_SMC_REG_CTLR:  // Control
2109                 *valp = smc_get_reg(1, ioaddr, CTL_REG);
2110                 break;
2111
2112         case CTL_SMC_REG_MCR:   // MMU Command
2113                 *valp = smc_get_reg(2, ioaddr, MMU_CMD_REG);
2114                 break;
2115
2116         case CTL_SMC_REG_PNR:   // Packet Number
2117                 *valp = smc_get_reg(2, ioaddr, PN_REG);
2118                 break;
2119
2120         case CTL_SMC_REG_FPR:   // Allocation Result/FIFO Ports
2121                 *valp = smc_get_reg(2, ioaddr, RXFIFO_REG);
2122                 break;
2123
2124         case CTL_SMC_REG_PTR:   // Pointer
2125                 *valp = smc_get_reg(2, ioaddr, PTR_REG);
2126                 break;
2127
2128         case CTL_SMC_REG_DR:    // Data
2129                 *valp = smc_get_reg(2, ioaddr, DATA_REG);
2130                 break;
2131
2132         case CTL_SMC_REG_ISR:   // Interrupt Status/Mask
2133                 *valp = smc_get_reg(2, ioaddr, INT_REG);
2134                 break;
2135
2136         case CTL_SMC_REG_MTR1:  // Multicast Table Entry 1
2137                 *valp = smc_get_reg(3, ioaddr, MCAST_REG1);
2138                 break;
2139
2140         case CTL_SMC_REG_MTR2:  // Multicast Table Entry 2
2141                 *valp = smc_get_reg(3, ioaddr, MCAST_REG2);
2142                 break;
2143
2144         case CTL_SMC_REG_MTR3:  // Multicast Table Entry 3
2145                 *valp = smc_get_reg(3, ioaddr, MCAST_REG3);
2146                 break;
2147
2148         case CTL_SMC_REG_MTR4:  // Multicast Table Entry 4
2149                 *valp = smc_get_reg(3, ioaddr, MCAST_REG4);
2150                 break;
2151
2152         case CTL_SMC_REG_MIIR:  // Management Interface
2153                 *valp = smc_get_reg(3, ioaddr, MII_REG);
2154                 break;
2155
2156         case CTL_SMC_REG_REVR:  // Revision
2157                 *valp = smc_get_reg(3, ioaddr, REV_REG);
2158                 break;
2159
2160         case CTL_SMC_REG_ERCVR: // Early RCV
2161                 *valp = smc_get_reg(3, ioaddr, ERCV_REG);
2162                 break;
2163
2164         case CTL_SMC_REG_EXTR:  // External
2165                 *valp = smc_get_reg(7, ioaddr, EXT_REG);
2166                 break;
2167
2168         case CTL_SMC_PHY_CTRL:
2169                 *valp = smc_read_phy_register(ioaddr, lp->phyaddr,
2170                         PHY_CNTL_REG);
2171                 break;
2172
2173         case CTL_SMC_PHY_STAT:
2174                 *valp = smc_read_phy_register(ioaddr, lp->phyaddr,
2175                         PHY_STAT_REG);
2176                 break;
2177
2178         case CTL_SMC_PHY_ID1:
2179                 *valp = smc_read_phy_register(ioaddr, lp->phyaddr,
2180                         PHY_ID1_REG);
2181                 break;
2182
2183         case CTL_SMC_PHY_ID2:
2184                 *valp = smc_read_phy_register(ioaddr, lp->phyaddr,
2185                         PHY_ID2_REG);
2186                 break;
2187
2188         case CTL_SMC_PHY_ADC:
2189                 *valp = smc_read_phy_register(ioaddr, lp->phyaddr,
2190                         PHY_AD_REG);
2191                 break;
2192
2193         case CTL_SMC_PHY_REMC:
2194                 *valp = smc_read_phy_register(ioaddr, lp->phyaddr,
2195                         PHY_RMT_REG);
2196                 break;
2197
2198         case CTL_SMC_PHY_CFG1:
2199                 *valp = smc_read_phy_register(ioaddr, lp->phyaddr,
2200                         PHY_CFG1_REG);
2201                 break;
2202
2203         case CTL_SMC_PHY_CFG2:
2204                 *valp = smc_read_phy_register(ioaddr, lp->phyaddr,
2205                         PHY_CFG2_REG);
2206                 break;
2207
2208         case CTL_SMC_PHY_INT:
2209                 *valp = smc_read_phy_register(ioaddr, lp->phyaddr,
2210                         PHY_INT_REG);
2211                 break;
2212
2213         case CTL_SMC_PHY_MASK:
2214                 *valp = smc_read_phy_register(ioaddr, lp->phyaddr,
2215                         PHY_MASK_REG);
2216                 break;
2217
2218 #endif // SMC_DEBUG > 1
2219
2220         default:
2221                 // Just ignore unsupported parameters
2222                 break;
2223         }
2224
2225         // Save old state
2226         val = *valp;
2227
2228         // Perform the generic integer operation
2229         if ((ret = proc_dointvec(ctl, write, filp, buffer, lenp)) != 0)
2230                 return(ret);
2231
2232         // Write changes out to the registers
2233         if (write && *valp != val) {
2234
2235                 val = *valp;
2236                 switch (ctl->ctl_name) {
2237
2238                 case CTL_SMC_SWFDUP:
2239                         if (val)
2240                                 lp->tcr_cur_mode |= TCR_SWFDUP;
2241                         else
2242                                 lp->tcr_cur_mode &= ~TCR_SWFDUP;
2243
2244                         smc_modify_regbit(0, ioaddr, TCR_REG, TCR_SWFDUP, val);
2245                         break;
2246
2247                 case CTL_SMC_EPHLOOP:
2248                         if (val)
2249                                 lp->tcr_cur_mode |= TCR_EPH_LOOP;
2250                         else
2251                                 lp->tcr_cur_mode &= ~TCR_EPH_LOOP;
2252
2253                         smc_modify_regbit(0, ioaddr, TCR_REG, TCR_EPH_LOOP, val);
2254                         break;
2255
2256                 case CTL_SMC_FORCOL:
2257                         if (val)
2258                                 lp->tcr_cur_mode |= TCR_FORCOL;
2259                         else
2260                                 lp->tcr_cur_mode &= ~TCR_FORCOL;
2261
2262                         // Update the EPH block
2263                         smc_modify_regbit(0, ioaddr, TCR_REG, TCR_FORCOL, val);
2264                         break;
2265
2266                 case CTL_SMC_FILTCAR:
2267                         if (val)
2268                                 lp->rcr_cur_mode |= RCR_FILT_CAR;
2269                         else
2270                                 lp->rcr_cur_mode &= ~RCR_FILT_CAR;
2271
2272                         // Update the EPH block
2273                         smc_modify_regbit(0, ioaddr, RCR_REG, RCR_FILT_CAR, val);
2274                         break;
2275
2276                 case CTL_SMC_RFDUPLX:
2277                         // Disallow changes if in auto-negotiation mode
2278                         if (lp->ctl_autoneg)
2279                                 break;
2280
2281                         if (val)
2282                                 lp->rpc_cur_mode |= RPC_DPLX;
2283                         else
2284                                 lp->rpc_cur_mode &= ~RPC_DPLX;
2285
2286                         // Reconfigure the PHY
2287                         smc_phy_configure(dev);
2288
2289                         break;
2290
2291                 case CTL_SMC_RSPEED:
2292                         // Disallow changes if in auto-negotiation mode
2293                         if (lp->ctl_autoneg)
2294                                 break;
2295
2296                         if (val > 10)
2297                                 lp->rpc_cur_mode |= RPC_SPEED;
2298                         else
2299                                 lp->rpc_cur_mode &= ~RPC_SPEED;
2300
2301                         // Reconfigure the PHY
2302                         smc_phy_configure(dev);
2303
2304                         break;
2305
2306                 case CTL_SMC_AUTONEG:
2307                         if (val)
2308                                 lp->rpc_cur_mode |= RPC_ANEG;
2309                         else
2310                                 lp->rpc_cur_mode &= ~RPC_ANEG;
2311
2312                         // Reconfigure the PHY
2313                         smc_phy_configure(dev);
2314
2315                         break;
2316
2317                 case CTL_SMC_LEDA:
2318                         val &= 0x07; // Restrict to 3 ls bits
2319                         lp->rpc_cur_mode &= ~(word)(0x07<<RPC_LSXA_SHFT);
2320                         lp->rpc_cur_mode |= (word)(val<<RPC_LSXA_SHFT);
2321
2322                         // Update the Internal PHY block
2323                         smc_modify_reg(0, ioaddr, RPC_REG, lp->rpc_cur_mode);
2324                         break;
2325
2326                 case CTL_SMC_LEDB:
2327                         val &= 0x07; // Restrict to 3 ls bits
2328                         lp->rpc_cur_mode &= ~(word)(0x07<<RPC_LSXB_SHFT);
2329                         lp->rpc_cur_mode |= (word)(val<<RPC_LSXB_SHFT);
2330
2331                         // Update the Internal PHY block
2332                         smc_modify_reg(0, ioaddr, RPC_REG, lp->rpc_cur_mode);
2333                         break;
2334
2335                 case CTL_SMC_MIIOP:
2336                         // Update the Internal PHY block
2337                         smc_modify_regbit(1, ioaddr, CONFIG_REG,
2338                                 CONFIG_EXT_PHY, val);
2339                         break;
2340
2341 #if SMC_DEBUG > 1
2342                 case CTL_SMC_REG_BSR:   // Bank Select
2343                         smc_modify_reg(0, ioaddr, BSR_REG, val);
2344                         break;
2345
2346                 case CTL_SMC_REG_TCR:   // Transmit Control
2347                         smc_modify_reg(0, ioaddr, TCR_REG, val);
2348                         break;
2349
2350                 case CTL_SMC_REG_ESR:   // EPH Status
2351                         smc_modify_reg(0, ioaddr, EPH_STATUS_REG, val);
2352                         break;
2353
2354                 case CTL_SMC_REG_RCR:   // Receive Control
2355                         smc_modify_reg(0, ioaddr, RCR_REG, val);
2356                         break;
2357
2358                 case CTL_SMC_REG_CTRR:  // Counter
2359                         smc_modify_reg(0, ioaddr, COUNTER_REG, val);
2360                         break;
2361
2362                 case CTL_SMC_REG_MIR:   // Memory Information
2363                         smc_modify_reg(0, ioaddr, MIR_REG, val);
2364                         break;
2365
2366                 case CTL_SMC_REG_RPCR:  // Receive/Phy Control
2367                         smc_modify_reg(0, ioaddr, RPC_REG, val);
2368                         break;
2369
2370                 case CTL_SMC_REG_CFGR:  // Configuration
2371                         smc_modify_reg(1, ioaddr, CONFIG_REG, val);
2372                         break;
2373
2374                 case CTL_SMC_REG_BAR:   // Base Address
2375                         smc_modify_reg(1, ioaddr, BASE_REG, val);
2376                         break;
2377
2378                 case CTL_SMC_REG_IAR0:  // Individual Address
2379                         smc_modify_reg(1, ioaddr, ADDR0_REG, val);
2380                         break;
2381
2382                 case CTL_SMC_REG_IAR1:  // Individual Address
2383                         smc_modify_reg(1, ioaddr, ADDR1_REG, val);
2384                         break;
2385
2386                 case CTL_SMC_REG_IAR2:  // Individual Address
2387                         smc_modify_reg(1, ioaddr, ADDR2_REG, val);
2388                         break;
2389
2390                 case CTL_SMC_REG_GPR:   // General Purpose
2391                         smc_modify_reg(1, ioaddr, GP_REG, val);
2392                         break;
2393
2394                 case CTL_SMC_REG_CTLR:  // Control
2395                         smc_modify_reg(1, ioaddr, CTL_REG, val);
2396                         break;
2397
2398                 case CTL_SMC_REG_MCR:   // MMU Command
2399                         smc_modify_reg(2, ioaddr, MMU_CMD_REG, val);
2400                         break;
2401
2402                 case CTL_SMC_REG_PNR:   // Packet Number
2403                         smc_modify_reg(2, ioaddr, PN_REG, val);
2404                         break;
2405
2406                 case CTL_SMC_REG_FPR:   // Allocation Result/FIFO Ports
2407                         smc_modify_reg(2, ioaddr, RXFIFO_REG, val);
2408                         break;
2409
2410                 case CTL_SMC_REG_PTR:   // Pointer
2411                         smc_modify_reg(2, ioaddr, PTR_REG, val);
2412                         break;
2413
2414                 case CTL_SMC_REG_DR:    // Data
2415                         smc_modify_reg(2, ioaddr, DATA_REG, val);
2416                         break;
2417
2418                 case CTL_SMC_REG_ISR:   // Interrupt Status/Mask
2419                         smc_modify_reg(2, ioaddr, INT_REG, val);
2420                         break;
2421
2422                 case CTL_SMC_REG_MTR1:  // Multicast Table Entry 1
2423                         smc_modify_reg(3, ioaddr, MCAST_REG1, val);
2424                         break;
2425
2426                 case CTL_SMC_REG_MTR2:  // Multicast Table Entry 2
2427                         smc_modify_reg(3, ioaddr, MCAST_REG2, val);
2428                         break;
2429
2430                 case CTL_SMC_REG_MTR3:  // Multicast Table Entry 3
2431                         smc_modify_reg(3, ioaddr, MCAST_REG3, val);
2432                         break;
2433
2434                 case CTL_SMC_REG_MTR4:  // Multicast Table Entry 4
2435                         smc_modify_reg(3, ioaddr, MCAST_REG4, val);
2436                         break;
2437
2438                 case CTL_SMC_REG_MIIR:  // Management Interface
2439                         smc_modify_reg(3, ioaddr, MII_REG, val);
2440                         break;
2441
2442                 case CTL_SMC_REG_REVR:  // Revision
2443                         smc_modify_reg(3, ioaddr, REV_REG, val);
2444                         break;
2445
2446                 case CTL_SMC_REG_ERCVR: // Early RCV
2447                         smc_modify_reg(3, ioaddr, ERCV_REG, val);
2448                         break;
2449
2450                 case CTL_SMC_REG_EXTR:  // External
2451                         smc_modify_reg(7, ioaddr, EXT_REG, val);
2452                         break;
2453
2454                 case CTL_SMC_PHY_CTRL:
2455                         smc_write_phy_register(ioaddr, lp->phyaddr,
2456                                 PHY_CNTL_REG, val);
2457                         break;
2458
2459                 case CTL_SMC_PHY_STAT:
2460                         smc_write_phy_register(ioaddr, lp->phyaddr,
2461                                 PHY_STAT_REG, val);
2462                         break;
2463
2464                 case CTL_SMC_PHY_ID1:
2465                         smc_write_phy_register(ioaddr, lp->phyaddr,
2466                                 PHY_ID1_REG, val);
2467                         break;
2468
2469                 case CTL_SMC_PHY_ID2:
2470                         smc_write_phy_register(ioaddr, lp->phyaddr,
2471                                 PHY_ID2_REG, val);
2472                         break;
2473
2474                 case CTL_SMC_PHY_ADC:
2475                         smc_write_phy_register(ioaddr, lp->phyaddr,
2476                                 PHY_AD_REG, val);
2477                         break;
2478
2479                 case CTL_SMC_PHY_REMC:
2480                         smc_write_phy_register(ioaddr, lp->phyaddr,
2481                                 PHY_RMT_REG, val);
2482                         break;
2483
2484                 case CTL_SMC_PHY_CFG1:
2485                         smc_write_phy_register(ioaddr, lp->phyaddr,
2486                                 PHY_CFG1_REG, val);
2487                         break;
2488
2489                 case CTL_SMC_PHY_CFG2:
2490                         smc_write_phy_register(ioaddr, lp->phyaddr,
2491                                 PHY_CFG2_REG, val);
2492                         break;
2493
2494                 case CTL_SMC_PHY_INT:
2495                         smc_write_phy_register(ioaddr, lp->phyaddr,
2496                                 PHY_INT_REG, val);
2497                         break;
2498
2499                 case CTL_SMC_PHY_MASK:
2500                         smc_write_phy_register(ioaddr, lp->phyaddr,
2501                                 PHY_MASK_REG, val);
2502                         break;
2503
2504 #endif // SMC_DEBUG > 1
2505
2506                 default:
2507                         // Just ignore unsupported parameters
2508                         break;
2509                 } // end switch
2510
2511         } // end if
2512
2513         return ret;
2514 }
2515
2516
2517 #ifdef MODULE
2518 /*
2519  * This is called as the fill_inode function when an inode
2520  * is going into (fill = 1) or out of service (fill = 0).
2521  * We use it here to manage the module use counts.
2522  *
2523  * Note: only the top-level directory needs to do this; if
2524  * a lower level is referenced, the parent will be as well.
2525  */
2526 static void smc_procfs_modcount(struct inode *inode, int fill)
2527 {
2528         if (fill)
2529                 MOD_INC_USE_COUNT;
2530         else
2531                 MOD_DEC_USE_COUNT;
2532 }
2533 #endif // MODULE
2534
2535 /*------------------------------------------------------------
2536  . Sysctl registration function for all parameters (files)
2537  .-------------------------------------------------------------*/
2538 static void smc_sysctl_register(struct net_device *dev)
2539 {
2540         struct smc_local *lp = (struct smc_local *)dev->priv;
2541         static int ctl_name = CTL_SMC;
2542         ctl_table* ct;
2543         int i;
2544
2545         // Make sure the ctl_tables start out as all zeros
2546         memset(lp->root_table, 0, sizeof lp->root_table);
2547         memset(lp->eth_table, 0, sizeof lp->eth_table);
2548         memset(lp->param_table, 0, sizeof lp->param_table);
2549
2550         // Initialize the root table
2551         ct = lp->root_table;
2552         ct->ctl_name = CTL_DEV;
2553         ct->procname = "dev";
2554         ct->maxlen = 0;
2555         ct->mode = 0555;
2556         ct->child = lp->eth_table;
2557         // remaining fields are zero
2558
2559         // Initialize the ethX table (this device's table)
2560         ct = lp->eth_table;
2561         ct->ctl_name = ctl_name++; // Must be unique
2562         ct->procname = dev->name;
2563         ct->maxlen = 0;
2564         ct->mode = 0555;
2565         ct->child = lp->param_table;
2566         // remaining fields are zero
2567
2568         // Initialize the parameter (files) table
2569         // Make sure the last entry remains null
2570         ct = lp->param_table;
2571         for (i = 0; i < (CTL_SMC_LAST_ENTRY-1); ++i) {
2572                 // Initialize fields common to all table entries
2573                 ct[i].proc_handler = smc_sysctl_handler;
2574                 ct[i].extra1 = (void*)dev; // Save our device pointer
2575                 ct[i].extra2 = (void*)lp;  // Save our smc_local data pointer
2576         }
2577
2578         // INFO - this is our only string parameter
2579         i = 0;
2580         ct[i].proc_handler = proc_dostring; // use default handler
2581         ct[i].ctl_name = CTL_SMC_INFO;
2582         ct[i].procname = "info";
2583         ct[i].data = (void*)smc_info_string;
2584         ct[i].maxlen = sizeof smc_info_string;
2585         ct[i].mode = 0444; // Read only
2586
2587         // SWVER
2588         ++i;
2589         ct[i].proc_handler = proc_dostring; // use default handler
2590         ct[i].ctl_name = CTL_SMC_SWVER;
2591         ct[i].procname = "swver";
2592         ct[i].data = (void*)version;
2593         ct[i].maxlen = sizeof version;
2594         ct[i].mode = 0444; // Read only
2595
2596         // SWFDUP
2597         ++i;
2598         ct[i].ctl_name = CTL_SMC_SWFDUP;
2599         ct[i].procname = "swfdup";
2600         ct[i].data = (void*)&(lp->ctl_swfdup);
2601         ct[i].maxlen = sizeof lp->ctl_swfdup;
2602         ct[i].mode = 0644; // Read by all, write by root
2603
2604         // EPHLOOP
2605         ++i;
2606         ct[i].ctl_name = CTL_SMC_EPHLOOP;
2607         ct[i].procname = "ephloop";
2608         ct[i].data = (void*)&(lp->ctl_ephloop);
2609         ct[i].maxlen = sizeof lp->ctl_ephloop;
2610         ct[i].mode = 0644; // Read by all, write by root
2611
2612         // MIIOP
2613         ++i;
2614         ct[i].ctl_name = CTL_SMC_MIIOP;
2615         ct[i].procname = "miiop";
2616         ct[i].data = (void*)&(lp->ctl_miiop);
2617         ct[i].maxlen = sizeof lp->ctl_miiop;
2618         ct[i].mode = 0644; // Read by all, write by root
2619
2620         // AUTONEG
2621         ++i;
2622         ct[i].ctl_name = CTL_SMC_AUTONEG;
2623         ct[i].procname = "autoneg";
2624         ct[i].data = (void*)&(lp->ctl_autoneg);
2625         ct[i].maxlen = sizeof lp->ctl_autoneg;
2626         ct[i].mode = 0644; // Read by all, write by root
2627
2628         // RFDUPLX
2629         ++i;
2630         ct[i].ctl_name = CTL_SMC_RFDUPLX;
2631         ct[i].procname = "rfduplx";
2632         ct[i].data = (void*)&(lp->ctl_rfduplx);
2633         ct[i].maxlen = sizeof lp->ctl_rfduplx;
2634         ct[i].mode = 0644; // Read by all, write by root
2635
2636         // RSPEED
2637         ++i;
2638         ct[i].ctl_name = CTL_SMC_RSPEED;
2639         ct[i].procname = "rspeed";
2640         ct[i].data = (void*)&(lp->ctl_rspeed);
2641         ct[i].maxlen = sizeof lp->ctl_rspeed;
2642         ct[i].mode = 0644; // Read by all, write by root
2643
2644         // AFDUPLX
2645         ++i;
2646         ct[i].ctl_name = CTL_SMC_AFDUPLX;
2647         ct[i].procname = "afduplx";
2648         ct[i].data = (void*)&(lp->ctl_afduplx);
2649         ct[i].maxlen = sizeof lp->ctl_afduplx;
2650         ct[i].mode = 0444; // Read only
2651
2652         // ASPEED
2653         ++i;
2654         ct[i].ctl_name = CTL_SMC_ASPEED;
2655         ct[i].procname = "aspeed";
2656         ct[i].data = (void*)&(lp->ctl_aspeed);
2657         ct[i].maxlen = sizeof lp->ctl_aspeed;
2658         ct[i].mode = 0444; // Read only
2659
2660         // LNKFAIL
2661         ++i;
2662         ct[i].ctl_name = CTL_SMC_LNKFAIL;
2663         ct[i].procname = "lnkfail";
2664         ct[i].data = (void*)&(lp->ctl_lnkfail);
2665         ct[i].maxlen = sizeof lp->ctl_lnkfail;
2666         ct[i].mode = 0444; // Read only
2667
2668         // FORCOL
2669         ++i;
2670         ct[i].ctl_name = CTL_SMC_FORCOL;
2671         ct[i].procname = "forcol";
2672         ct[i].data = (void*)&(lp->ctl_forcol);
2673         ct[i].maxlen = sizeof lp->ctl_forcol;
2674         ct[i].mode = 0644; // Read by all, write by root
2675
2676         // FILTCAR
2677         ++i;
2678         ct[i].ctl_name = CTL_SMC_FILTCAR;
2679         ct[i].procname = "filtcar";
2680         ct[i].data = (void*)&(lp->ctl_filtcar);
2681         ct[i].maxlen = sizeof lp->ctl_filtcar;
2682         ct[i].mode = 0644; // Read by all, write by root
2683
2684         // FREEMEM
2685         ++i;
2686         ct[i].ctl_name = CTL_SMC_FREEMEM;
2687         ct[i].procname = "freemem";
2688         ct[i].data = (void*)&(lp->ctl_freemem);
2689         ct[i].maxlen = sizeof lp->ctl_freemem;
2690         ct[i].mode = 0444; // Read only
2691
2692         // TOTMEM
2693         ++i;
2694         ct[i].ctl_name = CTL_SMC_TOTMEM;
2695         ct[i].procname = "totmem";
2696         ct[i].data = (void*)&(lp->ctl_totmem);
2697         ct[i].maxlen = sizeof lp->ctl_totmem;
2698         ct[i].mode = 0444; // Read only
2699
2700         // LEDA
2701         ++i;
2702         ct[i].ctl_name = CTL_SMC_LEDA;
2703         ct[i].procname = "leda";
2704         ct[i].data = (void*)&(lp->ctl_leda);
2705         ct[i].maxlen = sizeof lp->ctl_leda;
2706         ct[i].mode = 0644; // Read by all, write by root
2707
2708         // LEDB
2709         ++i;
2710         ct[i].ctl_name = CTL_SMC_LEDB;
2711         ct[i].procname = "ledb";
2712         ct[i].data = (void*)&(lp->ctl_ledb);
2713         ct[i].maxlen = sizeof lp->ctl_ledb;
2714         ct[i].mode = 0644; // Read by all, write by root
2715
2716         // CHIPREV
2717         ++i;
2718         ct[i].ctl_name = CTL_SMC_CHIPREV;
2719         ct[i].procname = "chiprev";
2720         ct[i].data = (void*)&(lp->ctl_chiprev);
2721         ct[i].maxlen = sizeof lp->ctl_chiprev;
2722         ct[i].mode = 0444; // Read only
2723
2724 #if SMC_DEBUG > 1
2725         // REG_BSR
2726         ++i;
2727         ct[i].ctl_name = CTL_SMC_REG_BSR;
2728         ct[i].procname = "reg_bsr";
2729         ct[i].data = (void*)&(lp->ctl_reg_bsr);
2730         ct[i].maxlen = sizeof lp->ctl_reg_bsr;
2731         ct[i].mode = 0644; // Read by all, write by root
2732
2733         // REG_TCR
2734         ++i;
2735         ct[i].ctl_name = CTL_SMC_REG_TCR;
2736         ct[i].procname = "reg_tcr";
2737         ct[i].data = (void*)&(lp->ctl_reg_tcr);
2738         ct[i].maxlen = sizeof lp->ctl_reg_tcr;
2739         ct[i].mode = 0644; // Read by all, write by root
2740
2741         // REG_ESR
2742         ++i;
2743         ct[i].ctl_name = CTL_SMC_REG_ESR;
2744         ct[i].procname = "reg_esr";
2745         ct[i].data = (void*)&(lp->ctl_reg_esr);
2746         ct[i].maxlen = sizeof lp->ctl_reg_esr;
2747         ct[i].mode = 0644; // Read by all, write by root
2748
2749         // REG_RCR
2750         ++i;
2751         ct[i].ctl_name = CTL_SMC_REG_RCR;
2752         ct[i].procname = "reg_rcr";
2753         ct[i].data = (void*)&(lp->ctl_reg_rcr);
2754         ct[i].maxlen = sizeof lp->ctl_reg_rcr;
2755         ct[i].mode = 0644; // Read by all, write by root
2756
2757         // REG_CTRR
2758         ++i;
2759         ct[i].ctl_name = CTL_SMC_REG_CTRR;
2760         ct[i].procname = "reg_ctrr";
2761         ct[i].data = (void*)&(lp->ctl_reg_ctrr);
2762         ct[i].maxlen = sizeof lp->ctl_reg_ctrr;
2763         ct[i].mode = 0644; // Read by all, write by root
2764
2765         // REG_MIR
2766         ++i;
2767         ct[i].ctl_name = CTL_SMC_REG_MIR;
2768         ct[i].procname = "reg_mir";
2769         ct[i].data = (void*)&(lp->ctl_reg_mir);
2770         ct[i].maxlen = sizeof lp->ctl_reg_mir;
2771         ct[i].mode = 0644; // Read by all, write by root
2772
2773         // REG_RPCR
2774         ++i;
2775         ct[i].ctl_name = CTL_SMC_REG_RPCR;
2776         ct[i].procname = "reg_rpcr";
2777         ct[i].data = (void*)&(lp->ctl_reg_rpcr);
2778         ct[i].maxlen = sizeof lp->ctl_reg_rpcr;
2779         ct[i].mode = 0644; // Read by all, write by root
2780
2781         // REG_CFGR
2782         ++i;
2783         ct[i].ctl_name = CTL_SMC_REG_CFGR;
2784         ct[i].procname = "reg_cfgr";
2785         ct[i].data = (void*)&(lp->ctl_reg_cfgr);
2786         ct[i].maxlen = sizeof lp->ctl_reg_cfgr;
2787         ct[i].mode = 0644; // Read by all, write by root
2788
2789         // REG_BAR
2790         ++i;
2791         ct[i].ctl_name = CTL_SMC_REG_BAR;
2792         ct[i].procname = "reg_bar";
2793         ct[i].data = (void*)&(lp->ctl_reg_bar);
2794         ct[i].maxlen = sizeof lp->ctl_reg_bar;
2795         ct[i].mode = 0644; // Read by all, write by root
2796
2797         // REG_IAR0
2798         ++i;
2799         ct[i].ctl_name = CTL_SMC_REG_IAR0;
2800         ct[i].procname = "reg_iar0";
2801         ct[i].data = (void*)&(lp->ctl_reg_iar0);
2802         ct[i].maxlen = sizeof lp->ctl_reg_iar0;
2803         ct[i].mode = 0644; // Read by all, write by root
2804
2805         // REG_IAR1
2806         ++i;
2807         ct[i].ctl_name = CTL_SMC_REG_IAR1;
2808         ct[i].procname = "reg_iar1";
2809         ct[i].data = (void*)&(lp->ctl_reg_iar1);
2810         ct[i].maxlen = sizeof lp->ctl_reg_iar1;
2811         ct[i].mode = 0644; // Read by all, write by root
2812
2813         // REG_IAR2
2814         ++i;
2815         ct[i].ctl_name = CTL_SMC_REG_IAR2;
2816         ct[i].procname = "reg_iar2";
2817         ct[i].data = (void*)&(lp->ctl_reg_iar2);
2818         ct[i].maxlen = sizeof lp->ctl_reg_iar2;
2819         ct[i].mode = 0644; // Read by all, write by root
2820
2821         // REG_GPR
2822         ++i;
2823         ct[i].ctl_name = CTL_SMC_REG_GPR;
2824         ct[i].procname = "reg_gpr";
2825         ct[i].data = (void*)&(lp->ctl_reg_gpr);
2826         ct[i].maxlen = sizeof lp->ctl_reg_gpr;
2827         ct[i].mode = 0644; // Read by all, write by root
2828
2829         // REG_CTLR
2830         ++i;
2831         ct[i].ctl_name = CTL_SMC_REG_CTLR;
2832         ct[i].procname = "reg_ctlr";
2833         ct[i].data = (void*)&(lp->ctl_reg_ctlr);
2834         ct[i].maxlen = sizeof lp->ctl_reg_ctlr;
2835         ct[i].mode = 0644; // Read by all, write by root
2836
2837         // REG_MCR
2838         ++i;
2839         ct[i].ctl_name = CTL_SMC_REG_MCR;
2840         ct[i].procname = "reg_mcr";
2841         ct[i].data = (void*)&(lp->ctl_reg_mcr);
2842         ct[i].maxlen = sizeof lp->ctl_reg_mcr;
2843         ct[i].mode = 0644; // Read by all, write by root
2844
2845         // REG_PNR
2846         ++i;
2847         ct[i].ctl_name = CTL_SMC_REG_PNR;
2848         ct[i].procname = "reg_pnr";
2849         ct[i].data = (void*)&(lp->ctl_reg_pnr);
2850         ct[i].maxlen = sizeof lp->ctl_reg_pnr;
2851         ct[i].mode = 0644; // Read by all, write by root
2852
2853         // REG_FPR
2854         ++i;
2855         ct[i].ctl_name = CTL_SMC_REG_FPR;
2856         ct[i].procname = "reg_fpr";
2857         ct[i].data = (void*)&(lp->ctl_reg_fpr);
2858         ct[i].maxlen = sizeof lp->ctl_reg_fpr;
2859         ct[i].mode = 0644; // Read by all, write by root
2860
2861         // REG_PTR
2862         ++i;
2863         ct[i].ctl_name = CTL_SMC_REG_PTR;
2864         ct[i].procname = "reg_ptr";
2865         ct[i].data = (void*)&(lp->ctl_reg_ptr);
2866         ct[i].maxlen = sizeof lp->ctl_reg_ptr;
2867         ct[i].mode = 0644; // Read by all, write by root
2868
2869         // REG_DR
2870         ++i;
2871         ct[i].ctl_name = CTL_SMC_REG_DR;
2872         ct[i].procname = "reg_dr";
2873         ct[i].data = (void*)&(lp->ctl_reg_dr);
2874         ct[i].maxlen = sizeof lp->ctl_reg_dr;
2875         ct[i].mode = 0644; // Read by all, write by root
2876
2877         // REG_ISR
2878         ++i;
2879         ct[i].ctl_name = CTL_SMC_REG_ISR;
2880         ct[i].procname = "reg_isr";
2881         ct[i].data = (void*)&(lp->ctl_reg_isr);
2882         ct[i].maxlen = sizeof lp->ctl_reg_isr;
2883         ct[i].mode = 0644; // Read by all, write by root
2884
2885         // REG_MTR1
2886         ++i;
2887         ct[i].ctl_name = CTL_SMC_REG_MTR1;
2888         ct[i].procname = "reg_mtr1";
2889         ct[i].data = (void*)&(lp->ctl_reg_mtr1);
2890         ct[i].maxlen = sizeof lp->ctl_reg_mtr1;
2891         ct[i].mode = 0644; // Read by all, write by root
2892
2893         // REG_MTR2
2894         ++i;
2895         ct[i].ctl_name = CTL_SMC_REG_MTR2;
2896         ct[i].procname = "reg_mtr2";
2897         ct[i].data = (void*)&(lp->ctl_reg_mtr2);
2898         ct[i].maxlen = sizeof lp->ctl_reg_mtr2;
2899         ct[i].mode = 0644; // Read by all, write by root
2900
2901         // REG_MTR3
2902         ++i;
2903         ct[i].ctl_name = CTL_SMC_REG_MTR3;
2904         ct[i].procname = "reg_mtr3";
2905         ct[i].data = (void*)&(lp->ctl_reg_mtr3);
2906         ct[i].maxlen = sizeof lp->ctl_reg_mtr3;
2907         ct[i].mode = 0644; // Read by all, write by root
2908
2909         // REG_MTR4
2910         ++i;
2911         ct[i].ctl_name = CTL_SMC_REG_MTR4;
2912         ct[i].procname = "reg_mtr4";
2913         ct[i].data = (void*)&(lp->ctl_reg_mtr4);
2914         ct[i].maxlen = sizeof lp->ctl_reg_mtr4;
2915         ct[i].mode = 0644; // Read by all, write by root
2916
2917         // REG_MIIR
2918         ++i;
2919         ct[i].ctl_name = CTL_SMC_REG_MIIR;
2920         ct[i].procname = "reg_miir";
2921         ct[i].data = (void*)&(lp->ctl_reg_miir);
2922         ct[i].maxlen = sizeof lp->ctl_reg_miir;
2923         ct[i].mode = 0644; // Read by all, write by root
2924
2925         // REG_REVR
2926         ++i;
2927         ct[i].ctl_name = CTL_SMC_REG_REVR;
2928         ct[i].procname = "reg_revr";
2929         ct[i].data = (void*)&(lp->ctl_reg_revr);
2930         ct[i].maxlen = sizeof lp->ctl_reg_revr;
2931         ct[i].mode = 0644; // Read by all, write by root
2932
2933         // REG_ERCVR
2934         ++i;
2935         ct[i].ctl_name = CTL_SMC_REG_ERCVR;
2936         ct[i].procname = "reg_ercvr";
2937         ct[i].data = (void*)&(lp->ctl_reg_ercvr);
2938         ct[i].maxlen = sizeof lp->ctl_reg_ercvr;
2939         ct[i].mode = 0644; // Read by all, write by root
2940
2941         // REG_EXTR
2942         ++i;
2943         ct[i].ctl_name = CTL_SMC_REG_EXTR;
2944         ct[i].procname = "reg_extr";
2945         ct[i].data = (void*)&(lp->ctl_reg_extr);
2946         ct[i].maxlen = sizeof lp->ctl_reg_extr;
2947         ct[i].mode = 0644; // Read by all, write by root
2948
2949         // PHY Control
2950         ++i;
2951         ct[i].ctl_name = CTL_SMC_PHY_CTRL;
2952         ct[i].procname = "phy_ctrl";
2953         ct[i].data = (void*)&(lp->ctl_phy_ctrl);
2954         ct[i].maxlen = sizeof lp->ctl_phy_ctrl;
2955         ct[i].mode = 0644; // Read by all, write by root
2956
2957         // PHY Status
2958         ++i;
2959         ct[i].ctl_name = CTL_SMC_PHY_STAT;
2960         ct[i].procname = "phy_stat";
2961         ct[i].data = (void*)&(lp->ctl_phy_stat);
2962         ct[i].maxlen = sizeof lp->ctl_phy_stat;
2963         ct[i].mode = 0644; // Read by all, write by root
2964
2965         // PHY ID1
2966         ++i;
2967         ct[i].ctl_name = CTL_SMC_PHY_ID1;
2968         ct[i].procname = "phy_id1";
2969         ct[i].data = (void*)&(lp->ctl_phy_id1);
2970         ct[i].maxlen = sizeof lp->ctl_phy_id1;
2971         ct[i].mode = 0644; // Read by all, write by root
2972
2973         // PHY ID2
2974         ++i;
2975         ct[i].ctl_name = CTL_SMC_PHY_ID2;
2976         ct[i].procname = "phy_id2";
2977         ct[i].data = (void*)&(lp->ctl_phy_id2);
2978         ct[i].maxlen = sizeof lp->ctl_phy_id2;
2979         ct[i].mode = 0644; // Read by all, write by root
2980
2981         // PHY Advertise Capabilities
2982         ++i;
2983         ct[i].ctl_name = CTL_SMC_PHY_ADC;
2984         ct[i].procname = "phy_adc";
2985         ct[i].data = (void*)&(lp->ctl_phy_adc);
2986         ct[i].maxlen = sizeof lp->ctl_phy_adc;
2987         ct[i].mode = 0644; // Read by all, write by root
2988
2989         // PHY Remote Capabilities
2990         ++i;
2991         ct[i].ctl_name = CTL_SMC_PHY_REMC;
2992         ct[i].procname = "phy_remc";
2993         ct[i].data = (void*)&(lp->ctl_phy_remc);
2994         ct[i].maxlen = sizeof lp->ctl_phy_remc;
2995         ct[i].mode = 0644; // Read by all, write by root
2996
2997         // PHY Configuration 1
2998         ++i;
2999         ct[i].ctl_name = CTL_SMC_PHY_CFG1;
3000         ct[i].procname = "phy_cfg1";
3001         ct[i].data = (void*)&(lp->ctl_phy_cfg1);
3002         ct[i].maxlen = sizeof lp->ctl_phy_cfg1;
3003         ct[i].mode = 0644; // Read by all, write by root
3004
3005         // PHY Configuration 2
3006         ++i;
3007         ct[i].ctl_name = CTL_SMC_PHY_CFG2;
3008         ct[i].procname = "phy_cfg2";
3009         ct[i].data = (void*)&(lp->ctl_phy_cfg2);
3010         ct[i].maxlen = sizeof lp->ctl_phy_cfg2;
3011         ct[i].mode = 0644; // Read by all, write by root
3012
3013         // PHY Interrupt/Status Output
3014         ++i;
3015         ct[i].ctl_name = CTL_SMC_PHY_INT;
3016         ct[i].procname = "phy_int";
3017         ct[i].data = (void*)&(lp->ctl_phy_int);
3018         ct[i].maxlen = sizeof lp->ctl_phy_int;
3019         ct[i].mode = 0644; // Read by all, write by root
3020
3021         // PHY Interrupt/Status Mask
3022         ++i;
3023         ct[i].ctl_name = CTL_SMC_PHY_MASK;
3024         ct[i].procname = "phy_mask";
3025         ct[i].data = (void*)&(lp->ctl_phy_mask);
3026         ct[i].maxlen = sizeof lp->ctl_phy_mask;
3027         ct[i].mode = 0644; // Read by all, write by root
3028
3029 #endif // SMC_DEBUG > 1
3030
3031         // Register /proc/sys/dev/ethX
3032         lp->sysctl_header = register_sysctl_table(lp->root_table, 1);
3033
3034 #ifdef MODULE
3035         // Register our modcount function which adjusts the module count
3036         lp->root_table->child->de->fill_inode = smc_procfs_modcount;
3037 #endif // MODULE
3038
3039 }
3040
3041
3042 /*------------------------------------------------------------
3043  . Sysctl unregistration when driver is closed
3044  .-------------------------------------------------------------*/
3045 static void smc_sysctl_unregister(struct net_device *dev)
3046 {
3047         struct smc_local *lp = (struct smc_local *)dev->priv;
3048
3049         unregister_sysctl_table(lp->sysctl_header);
3050 }
3051
3052 #endif /* endif CONFIG_SYSCTL */
3053
3054
3055 //---PHY CONTROL AND CONFIGURATION-----------------------------------------
3056
3057 #if (SMC_DEBUG > 2 )
3058
3059 /*------------------------------------------------------------
3060  . Debugging function for viewing MII Management serial bitstream
3061  .-------------------------------------------------------------*/
3062 static void smc_dump_mii_stream(byte* bits, int size)
3063 {
3064         int i;
3065
3066         printk("BIT#:");
3067         for (i = 0; i < size; ++i)
3068                 printk("%d", i%10);
3069
3070         printk("\nMDOE:");
3071         for (i = 0; i < size; ++i) {
3072                 if (bits[i] & MII_MDOE)
3073                         printk("1");
3074                 else
3075                         printk("0");
3076         }
3077
3078         printk("\nMDO :");
3079         for (i = 0; i < size; ++i) {
3080                 if (bits[i] & MII_MDO)
3081                         printk("1");
3082                 else
3083                         printk("0");
3084         }
3085
3086         printk("\nMDI :");
3087         for (i = 0; i < size; ++i) {
3088                 if (bits[i] & MII_MDI)
3089                         printk("1");
3090                 else
3091                         printk("0");
3092         }
3093
3094         printk("\n");
3095 }
3096 #endif
3097
3098 /*------------------------------------------------------------
3099  . Reads a register from the MII Management serial interface
3100  .-------------------------------------------------------------*/
3101 static word smc_read_phy_register(unsigned long ioaddr,
3102                                   byte phyaddr, byte phyreg)
3103 {
3104         int oldBank;
3105         int i;
3106         byte mask;
3107         word mii_reg;
3108         byte bits[64];
3109         int clk_idx = 0;
3110         int input_idx;
3111         word phydata;
3112
3113         // 32 consecutive ones on MDO to establish sync
3114         for (i = 0; i < 32; ++i)
3115                 bits[clk_idx++] = MII_MDOE | MII_MDO;
3116
3117         // Start code <01>
3118         bits[clk_idx++] = MII_MDOE;
3119         bits[clk_idx++] = MII_MDOE | MII_MDO;
3120
3121         // Read command <10>
3122         bits[clk_idx++] = MII_MDOE | MII_MDO;
3123         bits[clk_idx++] = MII_MDOE;
3124
3125         // Output the PHY address, msb first
3126         mask = (byte)0x10;
3127         for (i = 0; i < 5; ++i) {
3128                 if (phyaddr & mask)
3129                         bits[clk_idx++] = MII_MDOE | MII_MDO;
3130                 else
3131                         bits[clk_idx++] = MII_MDOE;
3132
3133                 // Shift to next lowest bit
3134                 mask >>= 1;
3135         }
3136
3137         // Output the phy register number, msb first
3138         mask = (byte)0x10;
3139         for (i = 0; i < 5; ++i) {
3140                 if (phyreg & mask)
3141                         bits[clk_idx++] = MII_MDOE | MII_MDO;
3142                 else
3143                         bits[clk_idx++] = MII_MDOE;
3144
3145                 // Shift to next lowest bit
3146                 mask >>= 1;
3147         }
3148
3149         // Tristate and turnaround (2 bit times)
3150         bits[clk_idx++] = 0;
3151         //bits[clk_idx++] = 0;
3152
3153         // Input starts at this bit time
3154         input_idx = clk_idx;
3155
3156         // Will input 16 bits
3157         for (i = 0; i < 16; ++i)
3158                 bits[clk_idx++] = 0;
3159
3160         // Final clock bit
3161         bits[clk_idx++] = 0;
3162
3163         // Save the current bank
3164         oldBank = SMC_CURRENT_BANK();
3165
3166         // Select bank 3
3167         SMC_SELECT_BANK( 3 );
3168
3169         // Get the current MII register value
3170         mii_reg = SMC_GET_MII();
3171
3172         // Turn off all MII Interface bits
3173         mii_reg &= ~(MII_MDOE|MII_MCLK|MII_MDI|MII_MDO);
3174
3175         // Clock all 64 cycles
3176         for (i = 0; i < sizeof bits; ++i) {
3177                 // Clock Low - output data
3178                 SMC_SET_MII( mii_reg | bits[i] );
3179                 udelay(50);
3180
3181
3182                 // Clock Hi - input data
3183                 SMC_SET_MII( mii_reg | bits[i] | MII_MCLK );
3184                 udelay(50);
3185                 bits[i] |= SMC_GET_MII() & MII_MDI;
3186         }
3187
3188         // Return to idle state
3189         // Set clock to low, data to low, and output tristated
3190         SMC_SET_MII( mii_reg );
3191         udelay(50);
3192
3193         // Restore original bank select
3194         SMC_SELECT_BANK( oldBank );
3195
3196         // Recover input data
3197         phydata = 0;
3198         for (i = 0; i < 16; ++i) {
3199                 phydata <<= 1;
3200
3201                 if (bits[input_idx++] & MII_MDI)
3202                         phydata |= 0x0001;
3203         }
3204
3205 #if (SMC_DEBUG > 2 )
3206         printk("smc_read_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
3207                 phyaddr, phyreg, phydata);
3208         smc_dump_mii_stream(bits, sizeof bits);
3209 #endif
3210
3211         return(phydata);
3212 }
3213
3214
3215 /*------------------------------------------------------------
3216  . Writes a register to the MII Management serial interface
3217  .-------------------------------------------------------------*/
3218 static void smc_write_phy_register(unsigned long ioaddr,
3219         byte phyaddr, byte phyreg, word phydata)
3220 {
3221         int oldBank;
3222         int i;
3223         word mask;
3224         word mii_reg;
3225         byte bits[65];
3226         int clk_idx = 0;
3227
3228         // 32 consecutive ones on MDO to establish sync
3229         for (i = 0; i < 32; ++i)
3230                 bits[clk_idx++] = MII_MDOE | MII_MDO;
3231
3232         // Start code <01>
3233         bits[clk_idx++] = MII_MDOE;
3234         bits[clk_idx++] = MII_MDOE | MII_MDO;
3235
3236         // Write command <01>
3237         bits[clk_idx++] = MII_MDOE;
3238         bits[clk_idx++] = MII_MDOE | MII_MDO;
3239
3240         // Output the PHY address, msb first
3241         mask = (byte)0x10;
3242         for (i = 0; i < 5; ++i) {
3243                 if (phyaddr & mask)
3244                         bits[clk_idx++] = MII_MDOE | MII_MDO;
3245                 else
3246                         bits[clk_idx++] = MII_MDOE;
3247
3248                 // Shift to next lowest bit
3249                 mask >>= 1;
3250         }
3251
3252         // Output the phy register number, msb first
3253         mask = (byte)0x10;
3254         for (i = 0; i < 5; ++i) {
3255                 if (phyreg & mask)
3256                         bits[clk_idx++] = MII_MDOE | MII_MDO;
3257                 else
3258                         bits[clk_idx++] = MII_MDOE;
3259
3260                 // Shift to next lowest bit
3261                 mask >>= 1;
3262         }
3263
3264         // Tristate and turnaround (2 bit times)
3265         bits[clk_idx++] = 0;
3266         bits[clk_idx++] = 0;
3267
3268         // Write out 16 bits of data, msb first
3269         mask = 0x8000;
3270         for (i = 0; i < 16; ++i) {
3271                 if (phydata & mask)
3272                         bits[clk_idx++] = MII_MDOE | MII_MDO;
3273                 else
3274                         bits[clk_idx++] = MII_MDOE;
3275
3276                 // Shift to next lowest bit
3277                 mask >>= 1;
3278         }
3279
3280         // Final clock bit (tristate)
3281         bits[clk_idx++] = 0;
3282
3283         // Save the current bank
3284         oldBank = SMC_CURRENT_BANK();
3285
3286         // Select bank 3
3287         SMC_SELECT_BANK( 3 );
3288
3289         // Get the current MII register value
3290         mii_reg = SMC_GET_MII();
3291
3292         // Turn off all MII Interface bits
3293         mii_reg &= ~(MII_MDOE|MII_MCLK|MII_MDI|MII_MDO);
3294
3295         // Clock all cycles
3296         for (i = 0; i < sizeof bits; ++i) {
3297                 // Clock Low - output data
3298                 SMC_SET_MII( mii_reg | bits[i] );
3299                 udelay(50);
3300
3301
3302                 // Clock Hi - input data
3303                 SMC_SET_MII( mii_reg | bits[i] | MII_MCLK );
3304                 udelay(50);
3305                 bits[i] |= SMC_GET_MII() & MII_MDI;
3306         }
3307
3308         // Return to idle state
3309         // Set clock to low, data to low, and output tristated
3310         SMC_SET_MII( mii_reg );
3311         udelay(50);
3312
3313         // Restore original bank select
3314         SMC_SELECT_BANK( oldBank );
3315
3316 #if (SMC_DEBUG > 2 )
3317         printk("smc_write_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
3318                 phyaddr, phyreg, phydata);
3319         smc_dump_mii_stream(bits, sizeof bits);
3320 #endif
3321 }
3322
3323
3324 /*------------------------------------------------------------
3325  . Finds and reports the PHY address
3326  .-------------------------------------------------------------*/
3327 static int smc_detect_phy(struct net_device* dev)
3328 {
3329         struct smc_local *lp = (struct smc_local *)dev->priv;
3330         unsigned long ioaddr = dev->base_addr;
3331         word phy_id1;
3332         word phy_id2;
3333         int phyaddr;
3334         int found = 0;
3335
3336         PRINTK3("%s:smc_detect_phy()\n", dev->name);
3337
3338         // Scan all 32 PHY addresses if necessary
3339         for (phyaddr = 0; phyaddr < 32; ++phyaddr) {
3340                 // Read the PHY identifiers
3341                 phy_id1  = smc_read_phy_register(ioaddr, phyaddr, PHY_ID1_REG);
3342                 phy_id2  = smc_read_phy_register(ioaddr, phyaddr, PHY_ID2_REG);
3343
3344                 PRINTK3("%s: phy_id1=%x, phy_id2=%x\n",
3345                         dev->name, phy_id1, phy_id2);
3346
3347                 // Make sure it is a valid identifier
3348                 if ((phy_id2 > 0x0000) && (phy_id2 < 0xffff) &&
3349                     (phy_id1 > 0x0000) && (phy_id1 < 0xffff)) {
3350                         if ((phy_id1 != 0x8000) && (phy_id2 != 0x8000)) {
3351                                 // Save the PHY's address
3352                                 lp->phyaddr = phyaddr;
3353                                 found = 1;
3354                                 break;
3355                         }
3356                 }
3357         }
3358
3359         if (!found) {
3360                 PRINTK("%s: No PHY found\n", dev->name);
3361                 return(0);
3362         }
3363
3364         // Set the PHY type
3365         if ( (phy_id1 == 0x0016) && ((phy_id2 & 0xFFF0) == 0xF840 ) ) {
3366                 lp->phytype = PHY_LAN83C183;
3367                 PRINTK("%s: PHY=LAN83C183 (LAN91C111 Internal)\n", dev->name);
3368         }
3369
3370         if ( (phy_id1 == 0x0282) && ((phy_id2 & 0xFFF0) == 0x1C50) ) {
3371                 lp->phytype = PHY_LAN83C180;
3372                 PRINTK("%s: PHY=LAN83C180\n", dev->name);
3373         }
3374
3375         return(1);
3376 }
3377
3378 /*------------------------------------------------------------
3379  . Waits the specified number of milliseconds - kernel friendly
3380  .-------------------------------------------------------------*/
3381 static void smc_wait_ms(unsigned int ms)
3382 {
3383
3384         if (!in_interrupt()) {
3385                 set_current_state(TASK_UNINTERRUPTIBLE);
3386                 schedule_timeout(1 + ms * HZ / 1000);
3387         } else {
3388                 set_current_state(TASK_INTERRUPTIBLE);
3389                 schedule_timeout(1 + ms * HZ / 1000);
3390                 set_current_state(TASK_RUNNING);
3391         }
3392 }
3393
3394 /*------------------------------------------------------------
3395  . Sets the PHY to a configuration as determined by the user
3396  .-------------------------------------------------------------*/
3397 static int smc_phy_fixed(struct net_device* dev)
3398 {
3399         unsigned long ioaddr = dev->base_addr;
3400         struct smc_local *lp = (struct smc_local *)dev->priv;
3401         byte phyaddr = lp->phyaddr;
3402         word my_fixed_caps;
3403         word cfg1;
3404
3405         PRINTK3("%s:smc_phy_fixed()\n", dev->name);
3406
3407         // Enter Link Disable state
3408         cfg1 = smc_read_phy_register(ioaddr, phyaddr, PHY_CFG1_REG);
3409         cfg1 |= PHY_CFG1_LNKDIS;
3410         smc_write_phy_register(ioaddr, phyaddr, PHY_CFG1_REG, cfg1);
3411
3412         // Set our fixed capabilities
3413         // Disable auto-negotiation
3414         my_fixed_caps = 0;
3415
3416         if (lp->ctl_rfduplx)
3417                 my_fixed_caps |= PHY_CNTL_DPLX;
3418
3419         if (lp->ctl_rspeed == 100)
3420                 my_fixed_caps |= PHY_CNTL_SPEED;
3421
3422         // Write our capabilities to the phy control register
3423         smc_write_phy_register(ioaddr, phyaddr, PHY_CNTL_REG, my_fixed_caps);
3424
3425         // Re-Configure the Receive/Phy Control register
3426         SMC_SET_RPC( lp->rpc_cur_mode );
3427
3428         // Success
3429         return(1);
3430 }
3431
3432
3433 /*------------------------------------------------------------
3434  . Configures the specified PHY using Autonegotiation. Calls
3435  . smc_phy_fixed() if the user has requested a certain config.
3436  .-------------------------------------------------------------*/
3437 static void smc_phy_configure(struct net_device* dev)
3438 {
3439         unsigned long ioaddr = dev->base_addr;
3440         struct smc_local *lp = (struct smc_local *)dev->priv;
3441         int timeout;
3442         byte phyaddr;
3443         word my_phy_caps; // My PHY capabilities
3444         word my_ad_caps; // My Advertised capabilities
3445         word status;
3446         int failed = 0;
3447
3448         PRINTK3("%s:smc_program_phy()\n", dev->name);
3449
3450         // Set the blocking flag
3451         lp->autoneg_active = 1;
3452
3453         // Find the address and type of our phy
3454         if (!smc_detect_phy(dev))
3455                 goto smc_phy_configure_exit;
3456
3457         // Get the detected phy address
3458         phyaddr = lp->phyaddr;
3459
3460         // Reset the PHY, setting all other bits to zero
3461         smc_write_phy_register(ioaddr, phyaddr, PHY_CNTL_REG, PHY_CNTL_RST);
3462
3463         // Wait for the reset to complete, or time out
3464         timeout = 6; // Wait up to 3 seconds
3465         while (timeout--) {
3466                 if (!(smc_read_phy_register(ioaddr, phyaddr, PHY_CNTL_REG)
3467                     & PHY_CNTL_RST))
3468                         // reset complete
3469                         break;
3470                 smc_wait_ms(500); // wait 500 millisecs
3471                 if (signal_pending(current)) { // Exit anyway if signaled
3472                         PRINTK2("%s:PHY reset interrupted by signal\n",
3473                                 dev->name);
3474                         timeout = 0;
3475                         break;
3476                 }
3477         }
3478
3479         if (timeout < 1) {
3480                 printk("%s:PHY reset timed out\n", dev->name);
3481                 goto smc_phy_configure_exit;
3482         }
3483
3484         // Read PHY Register 18, Status Output
3485         lp->lastPhy18 = smc_read_phy_register(ioaddr, phyaddr, PHY_INT_REG);
3486
3487         // Enable PHY Interrupts (for register 18)
3488         // Interrupts listed here are disabled
3489         smc_write_phy_register(ioaddr, phyaddr, PHY_MASK_REG,
3490                 PHY_INT_LOSSSYNC | PHY_INT_CWRD | PHY_INT_SSD |
3491                 PHY_INT_ESD | PHY_INT_RPOL | PHY_INT_JAB |
3492                 PHY_INT_SPDDET | PHY_INT_DPLXDET);
3493
3494         /* Configure the Receive/Phy Control register */
3495         SMC_SELECT_BANK( 0 );
3496         SMC_SET_RPC( lp->rpc_cur_mode );
3497
3498         // Copy our capabilities from PHY_STAT_REG to PHY_AD_REG
3499         my_phy_caps = smc_read_phy_register(ioaddr, phyaddr, PHY_STAT_REG);
3500         my_ad_caps  = PHY_AD_CSMA; // I am CSMA capable
3501
3502         if (my_phy_caps & PHY_STAT_CAP_T4)
3503                 my_ad_caps |= PHY_AD_T4;
3504
3505         if (my_phy_caps & PHY_STAT_CAP_TXF)
3506                 my_ad_caps |= PHY_AD_TX_FDX;
3507
3508         if (my_phy_caps & PHY_STAT_CAP_TXH)
3509                 my_ad_caps |= PHY_AD_TX_HDX;
3510
3511         if (my_phy_caps & PHY_STAT_CAP_TF)
3512                 my_ad_caps |= PHY_AD_10_FDX;
3513
3514         if (my_phy_caps & PHY_STAT_CAP_TH)
3515                 my_ad_caps |= PHY_AD_10_HDX;
3516
3517         // Disable capabilities not selected by our user
3518         if (lp->ctl_rspeed != 100)
3519                 my_ad_caps &= ~(PHY_AD_T4|PHY_AD_TX_FDX|PHY_AD_TX_HDX);
3520
3521         if (!lp->ctl_rfduplx)
3522                 my_ad_caps &= ~(PHY_AD_TX_FDX|PHY_AD_10_FDX);
3523
3524         // Update our Auto-Neg Advertisement Register
3525         smc_write_phy_register(ioaddr, phyaddr, PHY_AD_REG, my_ad_caps);
3526
3527         // Read the register back.  Without this, it appears that when
3528         // auto-negotiation is restarted, sometimes it isn't ready and
3529         // the link does not come up.
3530         status = smc_read_phy_register(ioaddr, phyaddr, PHY_AD_REG);
3531
3532         PRINTK2("%s:phy caps=%x\n", dev->name, my_phy_caps);
3533         PRINTK2("%s:phy advertised caps=%x\n", dev->name, my_ad_caps);
3534
3535         // If the user requested no auto neg, then go set his request
3536         if (!(lp->ctl_autoneg)) {
3537                 smc_phy_fixed(dev);
3538                 goto smc_phy_configure_exit;
3539         }
3540
3541         // Restart auto-negotiation process in order to advertise my caps
3542         smc_write_phy_register( ioaddr, phyaddr, PHY_CNTL_REG,
3543                 PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST );
3544
3545         // Wait for the auto-negotiation to complete.  This may take from
3546         // 2 to 3 seconds.
3547         // Wait for the reset to complete, or time out
3548         timeout = 20; // Wait up to 10 seconds
3549         while (timeout--) {
3550                 status = smc_read_phy_register(ioaddr, phyaddr, PHY_STAT_REG);
3551                 if (status & PHY_STAT_ANEG_ACK)
3552                         // auto-negotiate complete
3553                         break;
3554
3555                 smc_wait_ms(500); // wait 500 millisecs
3556                 if (signal_pending(current)) { // Exit anyway if signaled
3557                         printk(KERN_DEBUG
3558                                 "%s:PHY auto-negotiate interrupted by signal\n",
3559                                 dev->name);
3560                         timeout = 0;
3561                         break;
3562                 }
3563
3564                 // Restart auto-negotiation if remote fault
3565                 if (status & PHY_STAT_REM_FLT) {
3566                         PRINTK2("%s:PHY remote fault detected\n", dev->name);
3567
3568                         // Restart auto-negotiation
3569                         PRINTK2("%s:PHY restarting auto-negotiation\n",
3570                                 dev->name);
3571                         smc_write_phy_register( ioaddr, phyaddr, PHY_CNTL_REG,
3572                                 PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST |
3573                                 PHY_CNTL_SPEED | PHY_CNTL_DPLX);
3574                 }
3575         }
3576
3577         if (timeout < 1) {
3578                 printk(KERN_DEBUG "%s:PHY auto-negotiate timed out\n",
3579                         dev->name);
3580                 PRINTK2("%s:PHY auto-negotiate timed out\n", dev->name);
3581                 failed = 1;
3582         }
3583
3584         // Fail if we detected an auto-negotiate remote fault
3585         if (status & PHY_STAT_REM_FLT) {
3586                 printk(KERN_DEBUG "%s:PHY remote fault detected\n", dev->name);
3587                 PRINTK2("%s:PHY remote fault detected\n", dev->name);
3588                 failed = 1;
3589         }
3590
3591         // The smc_phy_interrupt() routine will be called to update lastPhy18
3592
3593         // Set our sysctl parameters to match auto-negotiation results
3594         if ( lp->lastPhy18 & PHY_INT_SPDDET ) {
3595                 PRINTK2("%s:PHY 100BaseT\n", dev->name);
3596                 lp->rpc_cur_mode |= RPC_SPEED;
3597         } else {
3598                 PRINTK2("%s:PHY 10BaseT\n", dev->name);
3599                 lp->rpc_cur_mode &= ~RPC_SPEED;
3600         }
3601
3602         if ( lp->lastPhy18 & PHY_INT_DPLXDET ) {
3603                 PRINTK2("%s:PHY Full Duplex\n", dev->name);
3604                 lp->rpc_cur_mode |= RPC_DPLX;
3605         } else {
3606                 PRINTK2("%s:PHY Half Duplex\n", dev->name);
3607                 lp->rpc_cur_mode &= ~RPC_DPLX;
3608         }
3609
3610         // Re-Configure the Receive/Phy Control register
3611         SMC_SET_RPC( lp->rpc_cur_mode );
3612
3613 smc_phy_configure_exit:
3614         // Exit auto-negotiation
3615         lp->autoneg_active = 0;
3616 }
3617
3618
3619
3620 /*************************************************************************
3621  . smc_phy_interrupt
3622  .
3623  . Purpose:  Handle interrupts relating to PHY register 18. This is
3624  .  called from the "hard" interrupt handler.
3625  .
3626  ************************************************************************/
3627 static void smc_phy_interrupt(struct net_device* dev)
3628 {
3629         unsigned long ioaddr    = dev->base_addr;
3630         struct smc_local *lp    = (struct smc_local *)dev->priv;
3631         byte phyaddr = lp->phyaddr;
3632         word phy18;
3633
3634         PRINTK2("%s: smc_phy_interrupt\n", dev->name);
3635
3636         for(;;) {
3637                 // Read PHY Register 18, Status Output
3638                 phy18 = smc_read_phy_register(ioaddr, phyaddr, PHY_INT_REG);
3639
3640                 // Exit if not more changes
3641                 if (phy18 == lp->lastPhy18)
3642                         break;
3643
3644 #if (SMC_DEBUG > 1 )
3645                 PRINTK2("%s:     phy18=0x%x\n", dev->name, phy18);
3646                 PRINTK2("%s: lastPhy18=0x%x\n", dev->name, lp->lastPhy18);
3647
3648                 // Handle events
3649                 if ((phy18 & PHY_INT_LNKFAIL) !=
3650                                 (lp->lastPhy18 & PHY_INT_LNKFAIL))
3651                         PRINTK2("%s: PHY Link Fail=%x\n", dev->name,
3652                                         phy18 & PHY_INT_LNKFAIL);
3653
3654                 if ((phy18 & PHY_INT_LOSSSYNC) !=
3655                                 (lp->lastPhy18 & PHY_INT_LOSSSYNC))
3656                         PRINTK2("%s: PHY LOSS SYNC=%x\n", dev->name,
3657                                         phy18 & PHY_INT_LOSSSYNC);
3658
3659                 if ((phy18 & PHY_INT_CWRD) != (lp->lastPhy18 & PHY_INT_CWRD))
3660                         PRINTK2("%s: PHY INVALID 4B5B code=%x\n", dev->name,
3661                                         phy18 & PHY_INT_CWRD);
3662
3663                 if ((phy18 & PHY_INT_SSD) != (lp->lastPhy18 & PHY_INT_SSD))
3664                         PRINTK2("%s: PHY No Start Of Stream=%x\n", dev->name,
3665                                         phy18 & PHY_INT_SSD);
3666
3667                 if ((phy18 & PHY_INT_ESD) != (lp->lastPhy18 & PHY_INT_ESD))
3668
3669                         PRINTK2("%s: PHY No End Of Stream=%x\n", dev->name,
3670                                         phy18 & PHY_INT_ESD);
3671
3672                 if ((phy18 & PHY_INT_RPOL) != (lp->lastPhy18 & PHY_INT_RPOL))
3673                         PRINTK2("%s: PHY Reverse Polarity Detected=%x\n",
3674                                         dev->name, phy18 & PHY_INT_RPOL);
3675
3676                 if ((phy18 & PHY_INT_JAB) != (lp->lastPhy18 & PHY_INT_JAB))
3677                         PRINTK2("%s: PHY Jabber Detected=%x\n", dev->name,
3678                                         phy18 & PHY_INT_JAB);
3679
3680                 if ((phy18 & PHY_INT_SPDDET) !=
3681                                 (lp->lastPhy18 & PHY_INT_SPDDET))
3682                         PRINTK2("%s: PHY Speed Detect=%x\n", dev->name,
3683                                         phy18 & PHY_INT_SPDDET);
3684
3685                 if ((phy18 & PHY_INT_DPLXDET) !=
3686                                 (lp->lastPhy18 & PHY_INT_DPLXDET))
3687                         PRINTK2("%s: PHY Duplex Detect=%x\n", dev->name,
3688                                         phy18 & PHY_INT_DPLXDET);
3689 #endif
3690                 // Update the last phy 18 variable
3691                 lp->lastPhy18 = phy18;
3692         }
3693 }