root/drivers/net/arcnet.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. arcnet_probe
  2. arcnet_found
  3. arcnet_reset
  4. arcnet_setup
  5. arcnet_open
  6. arcnet_close
  7. arcnet_send_packet_bad
  8. arcnetA_send_packet
  9. arcnetA_continue_tx
  10. arcnetAS_prepare_tx
  11. arcnet_go_tx
  12. arcnet_interrupt
  13. arcnet_inthandler
  14. arcnet_rx
  15. arcnetA_rx
  16. arcnet_get_stats
  17. set_multicast_list
  18. arcnetA_header
  19. arcnetA_rebuild_header
  20. arcnetA_type_trans
  21. arcnetE_init
  22. arcnetE_send_packet
  23. arcnetE_rx
  24. arcnetS_init
  25. arcnetS_send_packet
  26. arcnetS_rx
  27. arcnetS_header
  28. arcnetS_rebuild_header
  29. arcnetS_type_trans
  30. init_module
  31. cleanup_module

   1 /* arcnet.c:
   2         Written 1994-1996 by Avery Pennarun,
   3         derived from skeleton.c by Donald Becker.
   4 
   5         Contact Avery at: apenwarr@foxnet.net or
   6         RR #5 Pole Line Road, Thunder Bay, ON, Canada P7C 5M9
   7         
   8         **********************
   9         
  10         The original copyright was as follows:
  11 
  12         skeleton.c Written 1993 by Donald Becker.
  13         Copyright 1993 United States Government as represented by the
  14         Director, National Security Agency.  This software may only be used
  15         and distributed according to the terms of the GNU Public License as
  16         modified by SRC, incorporated herein by reference.
  17          
  18         **********************
  19         
  20         This is ONLY A SUMMARY.  The complete ChangeLog is available in
  21         the full Linux-ARCnet package.
  22         
  23         v2.41 ALPHA (96/02/10)
  24           - Incorporated changes someone made to arcnet_setup in 1.3.60.
  25           - Increased reset timeout to 3/10 of a second because some cards
  26             are too slow.
  27           - Removed a useless delay from autoprobe stage 4; I wonder
  28             how that got there!  (oops)
  29           - If FAST_IFCONFIG is defined, don't reset the card during
  30             arcnet_open; rather, do it in arcnet_close but don't delay. 
  31             This speeds up calls to ifconfig up/down.  (Thanks to Vojtech
  32             for the idea to speed this up.)
  33           - If FAST_PROBE is defined, don't bother to reset the card in
  34             autoprobe Stage 5 when there is only one io port and one shmem
  35             left in the list.  They "obviously" correspond to each other. 
  36             Another good idea from Vojtech.
  37 
  38         v2.40 ALPHA (96/02/03)
  39           - Checked the RECON flag last in the interrupt handler (when
  40             enabled) so the flag will show up in "status" when reporting
  41             other errors.  (I had a cabling problem that was hard to notice
  42             until I saw the huge RECON count in /proc/net/dev.)
  43           - Moved "IRQ for unknown device" message to D_DURING.
  44           - "transmit timed out" and "not acknowledged" messages are
  45             now D_EXTRA, because they very commonly happen when things
  46             are working fine.  "transmit timed out" due to missed IRQ's
  47             is still D_NORMAL, because it's probably a bug.
  48           - "Transmit timed out" messages now include destination station id.
  49           - The virtual arc0e and arc0s devices can now be disabled. 
  50             Massive (but simple) code rearrangement to support this with
  51             fewer ifdef's.
  52           - SLOW_XMIT_COPY option so fast computers don't hog the bus.  It's
  53             weird, but it works.
  54           - Finally redesigned autoprobe after some discussion with Vojtech
  55             Pavlik <Vojtech.Pavlik@st.mff.cuni.cz>.  It should be much safer
  56             and more reliable now, I think.  We now probe several more io
  57             ports and many more shmem addresses.
  58           - Rearranged D_* debugging flags slightly.  Watch out!  There is
  59             now a new flag, disabled by default, that will tell you the
  60             reason a particular port or shmem is discarded from the list.
  61 
  62         v2.30 ALPHA (96/01/10)
  63           - Abandoned support for Linux 1.2 to simplify coding and allow me
  64             to take advantage of new features in 1.3.
  65           - Updated cabling/jumpers documentation with MUCH information that
  66             people have recently (and in some cases, not so recently) sent
  67             me.  I don't mind writing documentation, but the jumpers
  68             database is REALLY starting to get dull.
  69           - Autoprobing works as a module now - but ONLY with my fix to
  70             register_netdev and unregister_netdev.  It's also much faster,
  71             and uses the "new-style" IRQ autoprobe routines.  Autoprobe is
  72             still a horrible mess and will be cleaned up further as time
  73             passes.
  74             
  75         v2.22 (95/12/08)
  76           - Major cleanups, speedups, and better code-sharing.
  77           - Eliminated/changed many useless/meaningless/scary debug messages
  78             (and, in most cases, the bugs that caused them).
  79           - Better IPX support.
  80           - lp->stats updated properly.
  81           - RECON checking now by default only bugs you if there are an
  82             excessive number (ie. your cable is probably broken).
  83           - New RFC1051-compliant "arc0s" virtual device by Tomasz
  84             Motylewski.
  85           - Excess debug messages can be compiled out for maximum
  86             efficiency.
  87 
  88         v2.00 (95/09/06)
  89           - ARCnet RECON messages are now detected and logged.  These occur
  90             when a new computer is powered up on the network, or in a
  91             constant stream when the network cable is broken.  Thanks to
  92             Tomasz Motylewski for this.  You must have D_EXTRA enabled
  93             if you want these messages sent to syslog, otherwise they will
  94             only show up in the network statistics (/proc/net/dev).
  95           - The TX Acknowledge flag is now checked, and a log message is sent
  96             if a completed transmit is not ACK'd.  (I have yet to have this
  97             happen to me.)
  98           - Debug levels are now completely different.  See the README.
  99           - Many code cleanups, with several no-longer-necessary and some
 100             completely useless options removed.
 101           - Multiprotocol support.  You can now use the "arc0e" device to
 102             send "Ethernet-Encapsulation" packets, which are compatible with
 103             Windows for Workgroups and LAN Manager, and possibly other
 104             software.  See the README for more information.
 105           - Documentation updates and improvements.
 106           
 107         v1.02 (95/06/21)
 108           - A fix to make "exception" packets sent from Linux receivable
 109             on other systems.  (The protocol_id byte was sometimes being set
 110             incorrectly, and Linux wasn't checking it on receive so it
 111             didn't show up)
 112 
 113         v1.01 (95/03/24)
 114           - Fixed some IPX-related bugs. (Thanks to Tomasz Motylewski
 115             <motyl@tichy.ch.uj.edu.pl> for the patches to make arcnet work
 116             with dosemu!)
 117             
 118         v1.00 (95/02/15)
 119           - Initial non-alpha release.
 120         
 121          
 122         TO DO: (it it just me, or does this list only get longer?)
 123         
 124          - Make sure autoprobe puts TESTvalue back into shmem locations
 125            that it determines aren't ARCnet cards.
 126          - Probe for multiple devices in one shot (there's supposed to
 127            be a way to do that now, but I can't remember what it is!)
 128          - Support printk's priority levels.
 129          - Have people test the new autoprobe a bit more - then remove
 130            D_INIT from the default debug level.
 131          - Move the SKB and memory dump code into separate functions.
 132          - Debug level should be changed with a system call, not a hack to
 133            the "metric" flag.
 134          - What about cards with shared memory that can be "turned off?"
 135            (or that have none at all, like the SMC PC500longboard)
 136          - Autoconfigure PDI5xxPlus cards. (I now have a PDI508Plus to play
 137            with temporarily.)  Update: yes, the Pure Data config program
 138            for DOS works fine, but the PDI508Plus I have doesn't! :)
 139          - Try to implement promiscuous (receive-all-packets) mode available
 140            on some newer cards with COM20020 and similar chips.  I don't have
 141            one, but SMC sent me the specs.
 142          - Add support for the new 1.3.x IP header cache features.
 143          - Make arcnetE_send_packet use arcnet_prepare_tx for loading the
 144            packet into ARCnet memory.
 145          - ATA protocol support?? 
 146          - VINES TCP/IP encapsulation?? (info needed)
 147 
 148            
 149         Sources:
 150          - Crynwr arcnet.com/arcether.com packet drivers.
 151          - arcnet.c v0.00 dated 1/1/94 and apparently by
 152                 Donald Becker - it didn't work :)
 153          - skeleton.c v0.05 dated 11/16/93 by Donald Becker
 154                 (from Linux Kernel 1.1.45)
 155          - RFC's 1201 and 1051 - re: TCP/IP over ARCnet
 156          - The official ARCnet COM9026 data sheets (!) thanks to Ken
 157                 Cornetet <kcornete@nyx10.cs.du.edu>
 158          - Information on some more obscure ARCnet controller chips, thanks
 159            to the nice people at SMC.
 160          - net/inet/eth.c (from kernel 1.1.50) for header-building info.
 161          - Alternate Linux ARCnet source by V.Shergin <vsher@sao.stavropol.su>
 162          - Textual information and more alternate source from Joachim Koenig
 163                 <jojo@repas.de>
 164 */
 165 
 166 static const char *version =
 167  "arcnet.c: v2.41 ALPHA 96/02/10 Avery Pennarun <apenwarr@foxnet.net>\n";
 168 
 169  
 170 
 171 #include <linux/module.h>
 172 #include <linux/config.h>
 173 #include <linux/version.h>
 174 
 175 #include <linux/kernel.h>
 176 #include <linux/sched.h>
 177 #include <linux/types.h>
 178 #include <linux/fcntl.h>
 179 #include <linux/interrupt.h>
 180 #include <linux/ptrace.h>
 181 #include <linux/ioport.h>
 182 #include <linux/in.h>
 183 #include <linux/malloc.h>
 184 #include <linux/string.h>
 185 #include <linux/timer.h>
 186 #include <linux/errno.h>
 187 #include <linux/delay.h>
 188 #include <linux/if_arp.h>
 189 #include <linux/netdevice.h>
 190 #include <linux/etherdevice.h>
 191 #include <linux/skbuff.h>
 192 
 193 #include <asm/system.h>
 194 #include <asm/bitops.h>
 195 #include <asm/io.h>
 196 #include <asm/dma.h>
 197 
 198 #include <net/arp.h>
 199 
 200 /**************************************************************************/
 201 
 202 /* This driver supports three different "virtual ARCnet devices" running
 203  * on the same adapter, in order to communicate with various different
 204  * TCP/IP-over-ARCnet implementations.  They are:
 205  *      arc0    - RFC1201 Internet-standard protocol
 206  *      arc0e   - Ethernet-encapsulation protocol (as used by Windows)
 207  *      arc0s   - "Simpler" (but outdated) RFC1051 internet standard.
 208  *
 209  * arc0e and arc0s are created when arc0 is ifconfig'ed up.  You can disable
 210  * either or both of them by undefining CONFIG_ARCNET_ETH and/or
 211  * CONFIG_ARCNET_1051.
 212  */
 213 #define CONFIG_ARCNET_ETH
 214 #define CONFIG_ARCNET_1051
 215 
 216 /* On a fast computer, the buffer copy from memory to the ARCnet card during
 217  * a transmit can hog the bus just a little too long.  SLOW_XMIT_COPY
 218  * replaces the fast memcpy() with a slower for() loop that seems to solve
 219  * my problems with ftape.
 220  *
 221  * Probably a better solution would be to use memcpy_toio (more portable
 222  * anyway) and modify that routine to support REALLY_SLOW_IO-style
 223  * defines; ARCnet probably is not the only driver that can screw up an
 224  * ftape DMA transfer.
 225  *
 226  * Turn this off if you don't have timing-sensitive DMA (ie. a tape drive)
 227  * and would like the little bit of speed back.  It's on by default because
 228  * - trust me - it's very difficult to figure out that you need it!
 229  */
 230 #define SLOW_XMIT_COPY
 231 
 232 /* The card sends the reconfiguration signal when it loses the connection to
 233  * the rest of its network. It is a 'Hello, is anybody there?' cry.  This
 234  * usually happens when a new computer on the network is powered on or when
 235  * the cable is broken.
 236  *
 237  * Define DETECT_RECONFIGS if you want to detect network reconfigurations. 
 238  * Recons may be a real nuisance on a larger ARCnet network; if you are a
 239  * network administrator you probably would like to count them. 
 240  * Reconfigurations will be recorded in stats.tx_carrier_errors (the last
 241  * field of the /proc/net/dev file).
 242  *
 243  * Define SHOW_RECONFIGS if you really want to see a log message whenever
 244  * a RECON occurs.
 245  */
 246 #define DETECT_RECONFIGS
 247 #undef SHOW_RECONFIGS
 248 
 249 /* RECON_THRESHOLD is the maximum number of RECON messages to receive within
 250  * one minute before printing a "cabling problem" warning.  You must have
 251  * DETECT_RECONFIGS enabled if you want to use this.  The default value
 252  * should be fine.
 253  *
 254  * After that, a "cabling restored" message will be printed on the next IRQ
 255  * if no RECON messages have been received for 10 seconds.
 256  *
 257  * Do not define RECON_THRESHOLD at all if you want to disable this feature.
 258  */
 259 #define RECON_THRESHOLD 30
 260 
 261 /* Define this to the minimum "timeout" value.  If a transmit takes longer
 262  * than TX_TIMEOUT jiffies, Linux will abort the TX and retry.  On a large
 263  * network, or one with heavy network traffic, this timeout may need to be
 264  * increased.  The larger it is, though, the longer it will be between
 265  * necessary transmits - don't set this too large.
 266  */
 267 #define TX_TIMEOUT 20
 268 
 269 /* Define this to speed up the autoprobe by assuming if only one io port and
 270  * shmem are left in the list at Stage 5, they must correspond to each
 271  * other.
 272  *
 273  * This is undefined by default because it might not always be true.  Speed
 274  * demons can turn it on - I think it should be fine if you only have one
 275  * ARCnet card installed.
 276  *
 277  * If no ARCnet cards are installed, this delay never happens anyway and thus
 278  * the option has no effect.
 279  */
 280 #undef FAST_PROBE
 281 
 282 /* Define this to speed up "ifconfig up" by moving the card reset command
 283  * around.  This is a new option in 2.41 ALPHA.  If it causes problems,
 284  * undefine this to get the old behaviour; then send me email, because if
 285  * there are no problems, this option will go away very soon.
 286  */
 287 #define FAST_IFCONFIG
 288 
 289 /* Define this if you want to make it easier to use the "call trace" when
 290  * a kernel NULL pointer assignment occurs.  Hopefully unnecessary, most of
 291  * the time.  It will make all the function names (and other things) show
 292  * up as kernel symbols.  (especially handy when using arcnet as a module)
 293  */
 294 #undef static
 295 
 296 /**************************************************************************/
 297 
 298 /* New debugging bitflags: each option can be enabled individually.
 299  *
 300  * These can be set while the driver is running by typing:
 301  *      ifconfig arc0 down metric 1xxx HOSTNAME
 302  *              where 1xxx is 1000 + the debug level you want
 303  *              and HOSTNAME is your hostname/ip address
 304  * and then resetting your routes.
 305  *
 306  * An ioctl() should be used for this instead, someday.
 307  *
 308  * Note: only debug flags included in the ARCNET_DEBUG_MAX define will
 309  *   actually be available.  GCC will (at least, GCC 2.7.0 will) notice
 310  *   lines using a BUGLVL not in ARCNET_DEBUG_MAX and automatically optimize
 311  *   them out.
 312  */
 313 #define D_NORMAL        1       /* important operational info           */
 314 #define D_EXTRA         2       /* useful, but non-vital information    */
 315 #define D_INIT          4       /* show init/probe messages             */
 316 #define D_INIT_REASONS  8       /* show reasons for discarding probes   */
 317 /* debug levels below give LOTS of output during normal operation! */
 318 #define D_DURING        16      /* trace operations (including irq's)   */
 319 #define D_TX            32      /* show tx packets                      */
 320 #define D_RX            64      /* show rx packets                      */
 321 #define D_SKB           128     /* show skb's                           */
 322 
 323 #ifndef ARCNET_DEBUG_MAX
 324 #define ARCNET_DEBUG_MAX (~0)           /* enable ALL debug messages */
 325 /*#define ARCNET_DEBUG_MAX (D_NORMAL|D_EXTRA|D_INIT|D_INIT_REASONS) */
 326 /*#define ARCNET_DEBUG_MAX 0    */      /* enable NO debug messages */
 327 #endif
 328 
 329 #ifndef ARCNET_DEBUG
 330 #define ARCNET_DEBUG (D_NORMAL|D_EXTRA|D_INIT)
 331 /*#define ARCNET_DEBUG (D_NORMAL)*/
 332 #endif
 333 int arcnet_debug = ARCNET_DEBUG;
 334 
 335 /* macros to simplify debug checking */
 336 #define BUGLVL(x) if ((ARCNET_DEBUG_MAX)&arcnet_debug&(x))
 337 #define BUGMSG2(x,msg,args...) BUGLVL(x) printk(msg , ## args)
 338 #define BUGMSG(x,msg,args...) BUGMSG2(x,"%6s: " msg, dev->name , ## args)
 339 
 340 /* Some useful multiprotocol macros.  The idea here is that GCC will
 341  * optimize away multiple tests or assignments to lp->adev.  Relying on this
 342  * results in the cleanest mess possible.
 343  */
 344 #define ADEV lp->adev
 345  
 346 #ifdef CONFIG_ARCNET_ETH
 347  #define EDEV lp->edev
 348 #else
 349  #define EDEV lp->adev
 350 #endif
 351 
 352 #ifdef CONFIG_ARCNET_1051
 353  #define SDEV lp->sdev
 354 #else
 355  #define SDEV lp->adev
 356 #endif
 357 
 358 #define TBUSY ADEV->tbusy=EDEV->tbusy=SDEV->tbusy
 359 #define IF_TBUSY (ADEV->tbusy||EDEV->tbusy||SDEV->tbusy)
 360 
 361 #define INTERRUPT ADEV->interrupt=EDEV->interrupt=SDEV->interrupt
 362 #define IF_INTERRUPT (ADEV->interrupt||EDEV->interrupt||SDEV->interrupt)
 363 
 364 #define START ADEV->start=EDEV->start=SDEV->start
 365 
 366 
 367 /* The number of low I/O ports used by the ethercard. */
 368 #define ARCNET_TOTAL_SIZE       16
 369 
 370 
 371 /* Handy defines for ARCnet specific stuff */
 372         /* COM 9026 controller chip --> ARCnet register addresses */
 373 #define INTMASK (ioaddr+0)              /* writable */
 374 #define STATUS  (ioaddr+0)              /* readable */
 375 #define COMMAND (ioaddr+1)      /* writable, returns random vals on read (?) */
 376 #define RESET  (ioaddr+8)               /* software reset writable */
 377 
 378 #define SETMASK outb(lp->intmask,INTMASK);
 379 
 380         /* Time needed to reset the card - in jiffies.  This works on my SMC
 381          * PC100.  I can't find a reference that tells me just how long I
 382          * should wait.
 383          */
 384 #define RESETtime (HZ * 3 / 10)         /* reset */
 385 
 386         /* these are the max/min lengths of packet data. (including
 387          * ClientData header)
 388          * note: packet sizes 250, 251, 252 are impossible (God knows why)
 389          *  so exception packets become necessary.
 390          *
 391          * These numbers are compared with the length of the full packet,
 392          * including ClientData header.
 393          */
 394 #define MTU     253     /* normal packet max size */
 395 #define MinTU   257     /* extended packet min size */
 396 #define XMTU    508     /* extended packet max size */
 397 
 398         /* status/interrupt mask bit fields */
 399 #define TXFREEflag      0x01            /* transmitter available */
 400 #define TXACKflag       0x02            /* transmitted msg. ackd */
 401 #define RECONflag       0x04            /* system reconfigured */
 402 #define TESTflag        0x08            /* test flag */
 403 #define RESETflag       0x10            /* power-on-reset */
 404 #define RES1flag        0x20            /* reserved - usually set by jumper */
 405 #define RES2flag        0x40            /* reserved - usually set by jumper */
 406 #define NORXflag        0x80            /* receiver inhibited */
 407 
 408        /* in the command register, the following bits have these meanings:
 409         *                0-2     command
 410         *                3-4     page number (for enable rcv/xmt command)
 411         *                 7      receive broadcasts
 412         */
 413 #define NOTXcmd         0x01            /* disable transmitter */
 414 #define NORXcmd         0x02            /* disable receiver */
 415 #define TXcmd           0x03            /* enable transmitter */
 416 #define RXcmd           0x04            /* enable receiver */
 417 #define CONFIGcmd       0x05            /* define configuration */
 418 #define CFLAGScmd       0x06            /* clear flags */
 419 #define TESTcmd         0x07            /* load test flags */
 420 
 421        /* flags for "clear flags" command */
 422 #define RESETclear      0x08            /* power-on-reset */
 423 #define CONFIGclear     0x10            /* system reconfigured */
 424 
 425         /* flags for "load test flags" command */
 426 #define TESTload        0x08            /* test flag (diagnostic) */
 427 
 428         /* byte deposited into first address of buffers on reset */
 429 #define TESTvalue       0321             /* that's octal for 0xD1 :) */
 430 
 431         /* for "enable receiver" command */
 432 #define RXbcasts        0x80            /* receive broadcasts */
 433 
 434         /* flags for "define configuration" command */
 435 #define NORMALconf      0x00            /* 1-249 byte packets */
 436 #define EXTconf         0x08            /* 250-504 byte packets */
 437 
 438         /* Starts receiving packets into recbuf.
 439          */
 440 #define EnableReceiver()        outb(RXcmd|(recbuf<<3)|RXbcasts,COMMAND)
 441 
 442         /* RFC1201 Protocol ID's */
 443 #define ARC_P_IP        212             /* 0xD4 */
 444 #define ARC_P_ARP       213             /* 0xD5 */
 445 #define ARC_P_RARP      214             /* 0xD6 */
 446 #define ARC_P_IPX       250             /* 0xFA */
 447 #define ARC_P_NOVELL_EC 236             /* 0xEC */
 448 
 449         /* Old RFC1051 Protocol ID's */
 450 #define ARC_P_IP_RFC1051 240            /* 0xF0 */
 451 #define ARC_P_ARP_RFC1051 241           /* 0xF1 */
 452 
 453         /* MS LanMan/WfWg protocol */
 454 #define ARC_P_ETHER     0xE8
 455 
 456         /* Unsupported/indirectly supported protocols */
 457 #define ARC_P_LANSOFT   251             /* 0xFB - what is this? */
 458 #define ARC_P_ATALK     0xDD
 459 
 460         /* the header required by the card itself */
 461 struct HardHeader
 462 {
 463         u_char  source,         /* source ARCnet - filled in automagically */
 464                 destination,    /* destination ARCnet - 0 for broadcast */
 465                 offset1,        /* offset of ClientData (256-byte packets) */
 466                 offset2;        /* offset of ClientData (512-byte packets) */
 467 };
 468 
 469         /* a complete ARCnet packet */
 470 union ArcPacket
 471 {
 472         struct HardHeader hardheader;   /* the hardware header */
 473         u_char raw[512];                /* raw packet info, incl ClientData */
 474 };
 475 
 476         /* the "client data" header - RFC1201 information
 477          * notice that this screws up if it's not an even number of bytes
 478          * <sigh>
 479          */
 480 struct ClientData
 481 {
 482         /* data that's NOT part of real packet - we MUST get rid of it before
 483          * actually sending!!
 484          */
 485         u_char  saddr,          /* Source address - needed for IPX */
 486                 daddr;          /* Destination address */
 487         
 488         /* data that IS part of real packet */
 489         u_char  protocol_id,    /* ARC_P_IP, ARC_P_ARP, etc */
 490                 split_flag;     /* for use with split packets */
 491         u_short sequence;       /* sequence number */
 492 };
 493 #define EXTRA_CLIENTDATA (sizeof(struct ClientData)-4)
 494 
 495 
 496         /* the "client data" header - RFC1051 information
 497          * this also screws up if it's not an even number of bytes
 498          * <sigh again>
 499          */
 500 struct S_ClientData
 501 {
 502         /* data that's NOT part of real packet - we MUST get rid of it before
 503          * actually sending!!
 504          */
 505         u_char  saddr,          /* Source address - needed for IPX */
 506                 daddr,          /* Destination address */
 507                 junk;           /* padding to make an even length */
 508         
 509         /* data that IS part of real packet */
 510         u_char  protocol_id;    /* ARC_P_IP, ARC_P_ARP, etc */
 511 };
 512 #define S_EXTRA_CLIENTDATA (sizeof(struct S_ClientData)-1)
 513 
 514 
 515 /* "Incoming" is information needed for each address that could be sending
 516  * to us.  Mostly for partially-received split packets.
 517  */
 518 struct Incoming
 519 {
 520         struct sk_buff *skb;            /* packet data buffer             */
 521         unsigned char lastpacket,       /* number of last packet (from 1) */
 522                       numpackets;       /* number of packets in split     */
 523         u_short sequence;               /* sequence number of assembly    */
 524 };
 525 
 526 struct Outgoing
 527 {
 528         struct sk_buff *skb;            /* buffer from upper levels */
 529         struct ClientData *hdr;         /* clientdata of last packet */
 530         u_char *data;                   /* pointer to data in packet */
 531         short length,                   /* bytes total */
 532               dataleft,                 /* bytes left */
 533               segnum,                   /* segment being sent */
 534               numsegs,                  /* number of segments */
 535               seglen;                   /* length of segment */
 536 };
 537 
 538 
 539 /* Information that needs to be kept for each board. */
 540 struct arcnet_local {
 541         struct enet_statistics stats;
 542         u_short sequence;       /* sequence number (incs with each packet) */
 543         u_char stationid,       /* our 8-bit station address */
 544                 recbuf,         /* receive buffer # (0 or 1) */
 545                 txbuf,          /* transmit buffer # (2 or 3) */
 546                 txready,        /* buffer where a packet is ready to send */
 547                 intmask;        /* current value of INTMASK register */
 548         short intx,             /* in TX routine? */
 549               in_txhandler,     /* in TX_IRQ handler? */
 550               sending,          /* transmit in progress? */
 551               lastload_dest,    /* can last loaded packet be acked? */
 552               lasttrans_dest;   /* can last TX'd packet be acked? */
 553 
 554 #if defined(DETECT_RECONFIGS) && defined(RECON_THRESHOLD)
 555         time_t first_recon,     /* time of "first" RECON message to count */
 556                 last_recon;     /* time of most recent RECON */
 557         int num_recons,         /* number of RECONs between first and last. */
 558             network_down;       /* do we think the network is down? */
 559 #endif
 560         
 561         struct timer_list timer; /* the timer interrupt struct */
 562         struct Incoming incoming[256];  /* one from each address */
 563         struct Outgoing outgoing; /* packet currently being sent */
 564         
 565         struct device *adev;    /* RFC1201 protocol device */
 566 
 567 #ifdef CONFIG_ARCNET_ETH
 568         struct device *edev;    /* Ethernet-Encap device */
 569 #endif
 570 
 571 #ifdef CONFIG_ARCNET_1051
 572         struct device *sdev;    /* RFC1051 protocol device */
 573 #endif
 574 };
 575 
 576 
 577 /* Index to functions, as function prototypes. */
 578 extern int arcnet_probe(struct device *dev);
 579 static int arcnet_found(struct device *dev,int port,int airq,u_long shmem);
 580 
 581 static void arcnet_setup(struct device *dev);
 582 static int arcnet_open(struct device *dev);
 583 static int arcnet_close(struct device *dev);
 584 static int arcnet_reset(struct device *dev,int reset_delay);
 585 
 586 static int arcnet_send_packet_bad(struct sk_buff *skb,struct device *dev);
 587 static int arcnetA_send_packet(struct sk_buff *skb, struct device *dev);
 588 static void arcnetA_continue_tx(struct device *dev);
 589 static void arcnetAS_prepare_tx(struct device *dev,u_char *hdr,int hdrlen,
 590                 char *data,int length,int daddr,int exceptA);
 591 static int arcnet_go_tx(struct device *dev,int enable_irq);
 592 
 593 static void arcnet_interrupt(int irq,struct pt_regs *regs);
 594 static void arcnet_inthandler(struct device *dev);
 595 
 596 static void arcnet_rx(struct device *dev,int recbuf);
 597 static void arcnetA_rx(struct device *dev,u_char *buf,
 598         int length,u_char saddr, u_char daddr);
 599 
 600 static struct enet_statistics *arcnet_get_stats(struct device *dev);
 601 
 602 int arcnetA_header(struct sk_buff *skb,struct device *dev,
 603                 unsigned short type,void *daddr,void *saddr,unsigned len);
 604 int arcnetA_rebuild_header(void *eth,struct device *dev,unsigned long raddr,
 605                 struct sk_buff *skb);
 606 unsigned short arcnetA_type_trans(struct sk_buff *skb,struct device *dev);
 607 
 608 #ifdef CONFIG_ARCNET_ETH
 609         /* functions specific to Ethernet-Encap */
 610 static int arcnetE_init(struct device *dev);
 611 static int arcnetE_send_packet(struct sk_buff *skb, struct device *dev);
 612 static void arcnetE_rx(struct device *dev,u_char *arcsoft,
 613         int length,u_char saddr, u_char daddr);
 614 #endif
 615 
 616 #ifdef CONFIG_ARCNET_1051
 617         /* functions specific to RFC1051 */
 618 static int arcnetS_init(struct device *dev);
 619 static int arcnetS_send_packet(struct sk_buff *skb, struct device *dev);
 620 static void arcnetS_rx(struct device *dev,u_char *buf,
 621         int length,u_char saddr, u_char daddr);
 622 int arcnetS_header(struct sk_buff *skb,struct device *dev,
 623                 unsigned short type,void *daddr,void *saddr,unsigned len);
 624 int arcnetS_rebuild_header(void *eth,struct device *dev,unsigned long raddr,
 625                 struct sk_buff *skb);
 626 unsigned short arcnetS_type_trans(struct sk_buff *skb,struct device *dev);
 627 #endif
 628 
 629 #ifdef MODULE
 630 int  init_module(void);
 631 void cleanup_module(void);
 632 #endif
 633 
 634 #define tx_done(dev) 1
 635 
 636 #define JIFFER(time) for (delayval=jiffies+time; jiffies<delayval;) ;
 637                 
 638 
 639 /****************************************************************************
 640  *                                                                          *
 641  * Probe and initialization                                                 *
 642  *                                                                          *
 643  ****************************************************************************/
 644  
 645 /* Check for an ARCnet network adaptor, and return '0' if one exists.
 646  *  If dev->base_addr == 0, probe all likely locations.
 647  *  If dev->base_addr == 1, always return failure.
 648  *  If dev->base_addr == 2, allocate space for the device and return success
 649  *                          (detachable devices only).
 650  *
 651  * NOTE: the list of possible ports/shmems is static, so it is retained
 652  * across calls to arcnet_probe.  So, if more than one ARCnet probe is made,
 653  * values that were discarded once will not even be tried again.
 654  */
 655 int arcnet_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 656 {
 657         static int init_once = 0;
 658         static int ports[(0x3f0 - 0x200) / 16 + 1];
 659         static u_long shmems[(0xFF800 - 0xA0000) / 2048 + 1];
 660         static int numports=sizeof(ports)/sizeof(ports[0]),
 661                    numshmems=sizeof(shmems)/sizeof(shmems[0]);
 662 
 663         int count,status,delayval,ioaddr,numprint,airq;
 664         unsigned long airqmask;
 665         int *port;
 666         u_long *shmem;
 667         
 668         if (!init_once)
 669         {
 670                 for (count=0x200; count<=0x3f0; count+=16)
 671                         ports[(count-0x200)/16] = count;
 672                 for (count=0xA0000; count<=0xFF800; count+=2048)
 673                         shmems[(count-0xA0000)/2048] = count;
 674         }
 675         else
 676                 init_once=1;
 677 
 678         BUGLVL(D_NORMAL) printk(version);
 679 
 680         BUGMSG(D_DURING,"space used for probe buffers: %d+%d=%d bytes\n",
 681                 sizeof(ports),sizeof(shmems),
 682                 sizeof(ports)+sizeof(shmems));
 683 
 684         
 685 #if 1
 686         BUGLVL(D_EXTRA)
 687         {
 688                 printk("arcnet: ***\n");
 689                 printk("arcnet: * Read arcnet.txt for important release notes!\n");
 690                 printk("arcnet: *\n");
 691                 printk("arcnet: * This is an ALPHA version!  (Last stable release: v2.22)  E-mail me if\n");
 692                 printk("arcnet: * you have any questions, comments, or bug reports.\n");
 693                 printk("arcnet: ***\n");
 694         }
 695 #endif
 696 
 697         BUGMSG(D_INIT,"given: base %lXh, IRQ %d, shmem %lXh\n",
 698                         dev->base_addr,dev->irq,dev->mem_start);
 699 
 700         if (dev->base_addr > 0x1ff)     /* Check a single specified port */
 701         {
 702                 ports[0]=dev->base_addr;
 703                 numports=1;
 704         }
 705         else if (dev->base_addr > 0)    /* Don't probe at all. */
 706                 return -ENXIO;
 707                 
 708         if (dev->mem_start)
 709         {
 710                 shmems[0]=dev->mem_start;
 711                 numshmems=1;
 712         }
 713         
 714 
 715         /* Stage 1: abandon any reserved ports, or ones with status==0xFF
 716          * (empty), and reset any others by reading the reset port.
 717          */
 718         BUGMSG(D_INIT,"Stage 1: ");
 719         numprint=0;
 720         for (port = &ports[0]; port-ports<numports; port++)
 721         {
 722                 numprint++;
 723                 if (numprint>8)
 724                 {
 725                         BUGMSG2(D_INIT,"\n");
 726                         BUGMSG(D_INIT,"Stage 1: ");
 727                         numprint=1;
 728                 }
 729                 BUGMSG2(D_INIT,"%Xh ",*port);
 730                 
 731                 ioaddr=*port;
 732                 
 733                 if (check_region(*port, ARCNET_TOTAL_SIZE))
 734                 {
 735                         BUGMSG2(D_INIT_REASONS,"(check_region)\n");
 736                         BUGMSG(D_INIT_REASONS,"Stage 1: ");
 737                         BUGLVL(D_INIT_REASONS) numprint=0;
 738                         *port=ports[numports-1];
 739                         numports--;
 740                         port--;
 741                         continue;
 742                 }
 743                 
 744                 if (inb(STATUS) == 0xFF)
 745                 {
 746                         BUGMSG2(D_INIT_REASONS,"(empty)\n");
 747                         BUGMSG(D_INIT_REASONS,"Stage 1: ");
 748                         BUGLVL(D_INIT_REASONS) numprint=0;
 749                         *port=ports[numports-1];
 750                         numports--;
 751                         port--;
 752                         continue;
 753                 }
 754                 
 755                 inb(RESET);     /* begin resetting card */
 756 
 757                 BUGMSG2(D_INIT_REASONS,"\n");
 758                 BUGMSG(D_INIT_REASONS,"Stage 1: ");
 759                 BUGLVL(D_INIT_REASONS) numprint=0;
 760         }
 761         BUGMSG2(D_INIT,"\n");
 762         
 763         if (!numports)
 764         {
 765                 BUGMSG(D_INIT,"Stage 1: failed.  No ARCnet cards found.\n");
 766                 return -ENODEV;
 767         }
 768         
 769 
 770         /* Stage 2: we have now reset any possible ARCnet cards, so we can't
 771          * do anything until they finish.  If D_INIT, print the list of
 772          * cards that are left.
 773          */
 774         BUGMSG(D_INIT,"Stage 2: ");
 775         numprint=0;
 776         for (port = &ports[0]; port-ports<numports; port++)
 777         {
 778                 numprint++;
 779                 if (numprint>8)
 780                 {
 781                         BUGMSG2(D_INIT,"\n");
 782                         BUGMSG(D_INIT,"Stage 2: ");
 783                         numprint=1;
 784                 }
 785                 BUGMSG2(D_INIT,"%Xh ",*port);
 786         }
 787         BUGMSG2(D_INIT,"\n");
 788         JIFFER(RESETtime);
 789         
 790 
 791         /* Stage 3: abandon any shmem addresses that don't have the signature
 792          * 0xD1 byte in the right place, or are read-only.
 793          */
 794         BUGMSG(D_INIT,"Stage 3: ");
 795         numprint=0;
 796         for (shmem = &shmems[0]; shmem-shmems<numshmems; shmem++)
 797         {
 798                 u_char *cptr;
 799         
 800                 numprint++;
 801                 if (numprint>8)
 802                 {
 803                         BUGMSG2(D_INIT,"\n");
 804                         BUGMSG(D_INIT,"Stage 3: ");
 805                         numprint=1;
 806                 }
 807                 BUGMSG2(D_INIT,"%lXh ",*shmem);
 808                 
 809                 cptr=(u_char *)(*shmem);
 810                 
 811                 if (*cptr != TESTvalue)
 812                 {
 813                         BUGMSG2(D_INIT_REASONS,"(mem=%Xh, not %Xh)\n",
 814                                 *cptr,TESTvalue);
 815                         BUGMSG(D_INIT_REASONS,"Stage 3: ");
 816                         BUGLVL(D_INIT_REASONS) numprint=0;
 817                         *shmem=shmems[numshmems-1];
 818                         numshmems--;
 819                         shmem--;
 820                         continue;
 821                 }
 822                 
 823                 /* By writing 0x42 to the TESTvalue location, we also make
 824                  * sure no "mirror" shmem areas show up - if they occur
 825                  * in another pass through this loop, they will be discarded
 826                  * because *cptr != TESTvalue.
 827                  */
 828                 *cptr=0x42;
 829                 if (*cptr != 0x42)
 830                 {
 831                         BUGMSG2(D_INIT_REASONS,"(read only)\n");
 832                         BUGMSG(D_INIT_REASONS,"Stage 3: ");
 833                         *shmem=shmems[numshmems-1];
 834                         numshmems--;
 835                         shmem--;
 836                         continue;
 837                 }
 838                 
 839                 BUGMSG2(D_INIT_REASONS,"\n");
 840                 BUGMSG(D_INIT_REASONS,"Stage 3: ");
 841                 BUGLVL(D_INIT_REASONS) numprint=0;
 842         }
 843         BUGMSG2(D_INIT,"\n");
 844 
 845         if (!numshmems)
 846         {
 847                 BUGMSG(D_INIT,"Stage 3: failed.  No ARCnet cards found.\n");
 848                 return -ENODEV;
 849         }
 850 
 851         /* Stage 4: something of a dummy, to report the shmems that are
 852          * still possible after stage 3.
 853          */
 854         BUGMSG(D_INIT,"Stage 4: ");
 855         numprint=0;
 856         for (shmem = &shmems[0]; shmem-shmems<numshmems; shmem++)
 857         {
 858                 numprint++;
 859                 if (numprint>8)
 860                 {
 861                         BUGMSG2(D_INIT,"\n");
 862                         BUGMSG(D_INIT,"Stage 4: ");
 863                         numprint=1;
 864                 }
 865                 BUGMSG2(D_INIT,"%lXh ",*shmem);
 866         }
 867         BUGMSG2(D_INIT,"\n");
 868         
 869 
 870         /* Stage 5: for any ports that have the correct status, can disable
 871          * the RESET flag, and (if no irq is given) generate an autoirq,
 872          * register an ARCnet device.
 873          *
 874          * Currently, we can only register one device per probe, so quit
 875          * after the first one is found.
 876          */
 877         BUGMSG(D_INIT,"Stage 5: ");
 878         numprint=0;
 879         for (port = &ports[0]; port-ports<numports; port++)
 880         {
 881                 numprint++;
 882                 if (numprint>8)
 883                 {
 884                         BUGMSG2(D_INIT,"\n");
 885                         BUGMSG(D_INIT,"Stage 5: ");
 886                         numprint=1;
 887                 }
 888                 BUGMSG2(D_INIT,"%Xh ",*port);
 889                 
 890                 ioaddr=*port;
 891                 status=inb(STATUS);
 892                 
 893                 if ((status & 0x9F)
 894                         != (NORXflag|RECONflag|TXFREEflag|RESETflag))
 895                 {
 896                         BUGMSG2(D_INIT_REASONS,"(status=%Xh)\n",status);
 897                         BUGMSG(D_INIT_REASONS,"Stage 5: ");
 898                         BUGLVL(D_INIT_REASONS) numprint=0;
 899                         *port=ports[numports-1];
 900                         numports--;
 901                         port--;
 902                         continue;
 903                 }
 904 
 905                 outb(CFLAGScmd|RESETclear|CONFIGclear,COMMAND);
 906                 status=inb(STATUS);
 907                 if (status & RESETflag)
 908                 {
 909                         BUGMSG2(D_INIT_REASONS," (eternal reset, status=%Xh)\n",
 910                                         status);
 911                         BUGMSG(D_INIT_REASONS,"Stage 5: ");
 912                         BUGLVL(D_INIT_REASONS) numprint=0;
 913                         *port=ports[numports-1];
 914                         numports--;
 915                         port--;
 916                         continue;
 917                 }
 918 
 919                 /* skip this if an IRQ was given, because maybe we're on a
 920                  * machine that locks during autoirq!
 921                  */
 922                 if (!dev->irq)
 923                 {
 924                         /* if we do this, we're sure to get an IRQ since the
 925                          * card has just reset and the NORXflag is on until
 926                          * we tell it to start receiving.
 927                          */
 928                         airqmask = probe_irq_on();
 929                         outb(NORXflag,INTMASK);
 930                         /*udelay(1);*/
 931                         outb(0,INTMASK);
 932                         airq = probe_irq_off(airqmask);
 933         
 934                         if (airq<=0)
 935                         {
 936                                 BUGMSG2(D_INIT_REASONS,"(airq=%d)\n",airq);
 937                                 BUGMSG(D_INIT_REASONS,"Stage 5: ");
 938                                 BUGLVL(D_INIT_REASONS) numprint=0;
 939                                 *port=ports[numports-1];
 940                                 numports--;
 941                                 port--;
 942                                 continue;
 943                         }
 944                 }
 945                 else
 946                 {
 947                         airq=dev->irq;
 948                 }
 949                 
 950                 BUGMSG2(D_INIT,"(%d,", airq);
 951                 
 952                 /* Everything seems okay.  But which shmem, if any, puts
 953                  * back its signature byte when the card is reset?
 954                  *
 955                  * If there are multiple cards installed, there might be
 956                  * multiple shmems still in the list.
 957                  */
 958 #ifdef FAST_PROBE
 959                 if (numports>1 || numshmems>1)
 960                 {
 961                         inb(RESET);
 962                         JIFFER(RESETtime);
 963                 }
 964                 else
 965                 {
 966                         *(u_char *)(shmems[0]) = TESTvalue;
 967                 }
 968 #else
 969                 inb(RESET);
 970                 JIFFER(RESETtime);
 971 #endif
 972 
 973 
 974                 for (shmem = &shmems[0]; shmem-shmems<numshmems; shmem++)
 975                 {
 976                         u_char *cptr;
 977                         cptr=(u_char *)(*shmem);
 978                         
 979                         if (*cptr == TESTvalue) /* found one */
 980                         {
 981                                 BUGMSG2(D_INIT,"%lXh)\n", *shmem);
 982 
 983                                 /* register the card */
 984                                 status=arcnet_found(dev,*port,airq,*shmem);
 985 
 986                                 /* remove port and shmem from the lists */
 987                                 *port=ports[numports-1];
 988                                 numports--;
 989                                 *shmem=shmems[numshmems-1];
 990                                 numshmems--;
 991                                 
 992                                 return status;
 993                         }
 994                         else
 995                         {
 996                                 BUGMSG2(D_INIT_REASONS,"%Xh-", *cptr);
 997                         }
 998                 }
 999 
1000                 BUGMSG2(D_INIT,"no matching shmem)\n");
1001                 BUGMSG(D_INIT_REASONS,"Stage 5: ");
1002                 BUGLVL(D_INIT_REASONS) numprint=0;
1003                 *port=ports[numports-1];
1004                 numports--;
1005                 port--;
1006         }
1007         BUGMSG(D_INIT_REASONS,"\n");
1008 
1009         return -ENODEV;
1010 }
1011 
1012 /* Set up the struct device associated with this card.  Called after
1013  * probing succeeds.
1014  */
1015 int arcnet_found(struct device *dev,int port,int airq, u_long shmem)
     /* [previous][next][first][last][top][bottom][index][help] */
1016 {
1017         int irqval;
1018         u_char *first_mirror,*last_mirror;
1019         struct arcnet_local *lp;
1020         
1021         /* reserve the I/O region */
1022         request_region(port,ARCNET_TOTAL_SIZE,"arcnet");
1023         dev->base_addr=port;
1024 
1025         /* reserve the irq */
1026         irqval = request_irq(airq,&arcnet_interrupt,0,"arcnet");
1027         if (irqval)
1028         {
1029                 BUGMSG(D_NORMAL,"unable to get IRQ %d (irqval=%d).\n",
1030                         airq, irqval);
1031                 return -ENODEV;
1032         }
1033         irq2dev_map[airq]=dev;
1034         dev->irq=airq;
1035         
1036         /* find the real shared memory start/end points, including mirrors */
1037         
1038         #define BUFFER_SIZE (512)
1039         #define MIRROR_SIZE (BUFFER_SIZE*4)
1040         
1041         first_mirror=last_mirror=(u_char *)shmem;
1042         while (*first_mirror==TESTvalue) first_mirror-=MIRROR_SIZE;
1043         first_mirror+=MIRROR_SIZE;
1044 
1045         while (*last_mirror==TESTvalue) last_mirror+=MIRROR_SIZE;
1046         last_mirror-=MIRROR_SIZE;
1047 
1048         dev->mem_start=(u_long)first_mirror;
1049         dev->mem_end=(u_long)last_mirror+MIRROR_SIZE-1;
1050         dev->rmem_start=dev->mem_start+BUFFER_SIZE*0;
1051         dev->rmem_end=dev->mem_start+BUFFER_SIZE*2-1;
1052          
1053         /* Initialize the rest of the device structure. */
1054         
1055         dev->priv = kmalloc(sizeof(struct arcnet_local), GFP_KERNEL);
1056         if (dev->priv == NULL)
1057                 return -ENOMEM;
1058         memset(dev->priv,0,sizeof(struct arcnet_local));
1059         lp=(struct arcnet_local *)(dev->priv);
1060         
1061         dev->open=arcnet_open;
1062         dev->stop=arcnet_close;
1063         dev->hard_start_xmit=arcnetA_send_packet;
1064         dev->get_stats=arcnet_get_stats;
1065         /*dev->set_multicast_list = &set_multicast_list;*/
1066 
1067         /* Fill in the fields of the device structure with generic
1068          * values.
1069          */
1070         arcnet_setup(dev);
1071         
1072         /* And now fill particular fields with arcnet values */
1073         dev->mtu=1500; /* completely arbitrary - agrees with ether, though */
1074         dev->hard_header_len=sizeof(struct ClientData);
1075         dev->hard_header=arcnetA_header;
1076         dev->rebuild_header=arcnetA_rebuild_header;
1077         lp->sequence=1;
1078         lp->recbuf=0;
1079 
1080         BUGMSG(D_DURING,"ClientData header size is %d.\n",
1081                 sizeof(struct ClientData));
1082         BUGMSG(D_DURING,"HardHeader size is %d.\n",
1083                 sizeof(struct HardHeader));
1084 
1085         /* get and check the station ID from offset 1 in shmem */
1086         lp->stationid = first_mirror[1];
1087         if (lp->stationid==0)
1088                 BUGMSG(D_NORMAL,"WARNING!  Station address 0 is reserved "
1089                         "for broadcasts!\n");
1090         else if (lp->stationid==255)
1091                 BUGMSG(D_NORMAL,"WARNING!  Station address FF may confuse "
1092                         "DOS networking programs!\n");
1093         dev->dev_addr[0]=lp->stationid;
1094 
1095         BUGMSG(D_NORMAL,"ARCnet station %02Xh found at %03lXh, IRQ %d, "
1096                 "ShMem %lXh (%ld bytes).\n",
1097                 lp->stationid,
1098                 dev->base_addr,dev->irq,dev->mem_start,
1099                 dev->mem_end-dev->mem_start+1);
1100                 
1101         return 0;
1102 }
1103 
1104 
1105 /* Do a hardware reset on the card, and set up necessary registers.
1106  *
1107  * This should be called as little as possible, because it disrupts the
1108  * token on the network (causes a RECON) and requires a significant delay.
1109  *
1110  * However, it does make sure the card is in a defined state.
1111  */
1112 int arcnet_reset(struct device *dev,int reset_delay)
     /* [previous][next][first][last][top][bottom][index][help] */
1113 {
1114         struct arcnet_local *lp=(struct arcnet_local *)dev->priv;
1115         short ioaddr=dev->base_addr;
1116         int delayval,recbuf=lp->recbuf;
1117         u_char *cardmem;
1118         
1119         /* no IRQ's, please! */
1120         lp->intmask=0;
1121         SETMASK;
1122         
1123         BUGMSG(D_INIT,"Resetting %s (status=%Xh)\n",
1124                         dev->name,inb(STATUS));
1125 
1126         if (reset_delay)
1127         {
1128                 /* reset the card */
1129                 inb(RESET);
1130                 JIFFER(RESETtime);
1131         }
1132 
1133         outb(CFLAGScmd|RESETclear, COMMAND); /* clear flags & end reset */
1134         outb(CFLAGScmd|CONFIGclear,COMMAND);
1135 
1136         /* verify that the ARCnet signature byte is present */
1137         cardmem = (u_char *) dev->mem_start;
1138         if (cardmem[0] != TESTvalue)
1139         {
1140                 BUGMSG(D_NORMAL,"reset failed: TESTvalue not present.\n");
1141                 return 1;
1142         }
1143         
1144         /* clear out status variables */
1145         recbuf=lp->recbuf=0;
1146         lp->txbuf=2;
1147 
1148         /* enable extended (512-byte) packets */
1149         outb(CONFIGcmd|EXTconf,COMMAND);
1150         
1151 #ifndef SLOW_XMIT_COPY
1152         /* clean out all the memory to make debugging make more sense :) */
1153         BUGLVL(D_DURING)
1154                 memset((void *)dev->mem_start,0x42,2048);
1155 #endif
1156         
1157         /* and enable receive of our first packet to the first buffer */
1158         EnableReceiver();
1159 
1160         /* re-enable interrupts */
1161         lp->intmask|=NORXflag;
1162 #ifdef DETECT_RECONFIGS
1163         lp->intmask|=RECONflag;
1164 #endif
1165         SETMASK;
1166         
1167         /* done!  return success. */
1168         return 0;
1169 }
1170 
1171 
1172 /* Setup a struct device for ARCnet.  This should really be in net_init.c
1173  * but since there are three different ARCnet devices ANYWAY... <gargle>
1174  *
1175  * Actually, the whole idea of having all this kernel-dependent stuff (ie.
1176  * "new-style flags") setup per-net-device is kind of weird anyway.
1177  *
1178  * Intelligent defaults?!  Nah.
1179  */
1180 void arcnet_setup(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1181 {
1182         int i;
1183         for (i=0; i<DEV_NUMBUFFS; i++)
1184                 skb_queue_head_init(&dev->buffs[i]);
1185 
1186         dev->broadcast[0]       = 0x00; /* for us, broadcasts are address 0 */
1187         dev->addr_len           = 1;
1188         dev->type               = ARPHRD_ARCNET;
1189         dev->tx_queue_len       = 30;   /* fairly long queue - arcnet is
1190                                          * quite speedy.
1191                                          */
1192 
1193         /* New-style flags. */
1194         dev->flags              = IFF_BROADCAST;
1195         dev->family             = AF_INET;
1196         dev->pa_addr            = 0;
1197         dev->pa_brdaddr         = 0;
1198         dev->pa_mask            = 0;
1199         dev->pa_alen            = 4;
1200 }
1201 
1202 
1203 /****************************************************************************
1204  *                                                                          *
1205  * Open and close the driver                                                *
1206  *                                                                          *
1207  ****************************************************************************/
1208  
1209 
1210 /* Open/initialize the board.  This is called sometime after booting when
1211  * the 'ifconfig' program is run.
1212  *
1213  * This routine should set everything up anew at each open, even
1214  * registers that "should" only need to be set once at boot, so that
1215  * there is non-reboot way to recover if something goes wrong.
1216  */
1217 static int
1218 arcnet_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1219 {
1220         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1221         int ioaddr=dev->base_addr;
1222         
1223         if (dev->metric>=1000)
1224         {
1225                 arcnet_debug=dev->metric-1000;
1226                 printk("%6s: debug level set to %d\n",dev->name,arcnet_debug);
1227                 dev->metric=1;
1228         }
1229 
1230         BUGMSG(D_INIT,"arcnet_open: resetting card.\n");
1231         
1232 #ifdef FAST_IFCONFIG
1233         /* try to put the card in a defined state - if it fails the first
1234          * time, actually reset it.
1235          */
1236         if (arcnet_reset(dev,0) && arcnet_reset(dev,1))
1237                 return -ENODEV;
1238 #else
1239         /* reset the card twice in case something goes wrong the first time.
1240          */
1241         if (arcnet_reset(dev,1) && arcnet_reset(dev,1))
1242                 return -ENODEV;
1243 #endif
1244         
1245         dev->tbusy=0;
1246         dev->interrupt=0;
1247         lp->intx=0;
1248         lp->in_txhandler=0;
1249         
1250         /* The RFC1201 driver is the default - just store */
1251         lp->adev=dev;
1252         BUGMSG(D_EXTRA,"ARCnet RFC1201 protocol initialized.\n");
1253 
1254 #ifdef CONFIG_ARCNET_ETH        
1255         /* Initialize the ethernet-encap protocol driver */
1256         lp->edev=(struct device *)kmalloc(sizeof(struct device),GFP_KERNEL);
1257         if (lp->edev == NULL)
1258                 return -ENOMEM;
1259         memcpy(lp->edev,dev,sizeof(struct device));
1260         lp->edev->name=(char *)kmalloc(10,GFP_KERNEL);
1261         if (lp->edev->name == NULL) {
1262                 kfree(lp->edev);
1263                 lp->edev = NULL;
1264                 return -ENOMEM;
1265         }
1266         sprintf(lp->edev->name,"%se",dev->name);
1267         lp->edev->init=arcnetE_init;
1268         register_netdev(lp->edev);
1269 #else
1270         BUGMSG(D_EXTRA,"Ethernet-Encap protocol not available (disabled).\n");
1271 #endif
1272 
1273 #ifdef CONFIG_ARCNET_1051
1274         /* Initialize the RFC1051-encap protocol driver */
1275         lp->sdev=(struct device *)kmalloc(sizeof(struct device),GFP_KERNEL);
1276         memcpy(lp->sdev,dev,sizeof(struct device));
1277         lp->sdev->name=(char *)kmalloc(10,GFP_KERNEL);
1278         sprintf(lp->sdev->name,"%ss",dev->name);
1279         lp->sdev->init=arcnetS_init;
1280         register_netdev(lp->sdev);
1281 #else
1282         BUGMSG(D_EXTRA,"RFC1051 protocol not available (disabled).\n");
1283 #endif
1284 
1285         /* we're started */
1286         START=1;
1287         
1288         /* make sure we're ready to receive IRQ's.
1289          * arcnet_reset sets this for us, but if we receive one before
1290          * START is set to 1, it could be ignored.  So, we turn IRQ's
1291          * off, then on again to clean out the IRQ controller.
1292          */
1293         outb(0,INTMASK);
1294         udelay(1);      /* give it time to set the mask before
1295                          * we reset it again. (may not even be
1296                          * necessary)
1297                          */
1298         SETMASK;
1299         
1300         MOD_INC_USE_COUNT;
1301         return 0;
1302 }
1303 
1304 
1305 /* The inverse routine to arcnet_open - shuts down the card.
1306  */
1307 static int
1308 arcnet_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1309 {
1310         int ioaddr = dev->base_addr;
1311         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1312         
1313         TBUSY=1;
1314         START=0;
1315 
1316         /* Shut down the card */
1317 #ifdef FAST_IFCONFIG
1318         inb(RESET);     /* reset IRQ won't run if START=0 */
1319 #else
1320         lp->intmask=0;
1321         SETMASK;        /* no IRQ's (except RESET, of course) */
1322         outb(NOTXcmd,COMMAND);  /* stop transmit */
1323         outb(NORXcmd,COMMAND);  /* disable receive */
1324 #endif
1325 
1326         /* reset more flags */
1327         INTERRUPT=0;
1328         
1329         /* do NOT free lp->adev!!  It's static! */
1330         lp->adev=NULL;
1331         
1332 #ifdef CONFIG_ARCNET_ETH
1333         /* free the ethernet-encap protocol device */
1334         lp->edev->priv=NULL;
1335         dev_close(lp->edev);
1336         unregister_netdev(lp->edev);
1337         kfree(lp->edev->name);
1338         kfree(lp->edev);
1339         lp->edev=NULL;
1340 #endif
1341 
1342 #ifdef CONFIG_ARCNET_1051
1343         /* free the RFC1051-encap protocol device */
1344         lp->sdev->priv=NULL;
1345         dev_close(lp->sdev);
1346         unregister_netdev(lp->sdev);
1347         kfree(lp->sdev->name);
1348         kfree(lp->sdev);
1349         lp->sdev=NULL;
1350 #endif
1351 
1352         /* Update the statistics here. (not necessary in ARCnet) */
1353 
1354         MOD_DEC_USE_COUNT;
1355         return 0;
1356 }
1357 
1358 
1359 
1360 /****************************************************************************
1361  *                                                                          *
1362  * Transmitter routines                                                     *
1363  *                                                                          *
1364  ****************************************************************************/
1365 
1366 /* Generic error checking routine for arcnet??_send_packet
1367  */
1368 static int
1369 arcnet_send_packet_bad(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1370 {
1371         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1372         int ioaddr=dev->base_addr;
1373         
1374         BUGMSG(D_DURING,"transmit requested (status=%Xh, inTX=%d)\n",
1375                         inb(STATUS),lp->intx);
1376                         
1377         if (lp->in_txhandler)
1378         {
1379                 BUGMSG(D_NORMAL,"send_packet called while in txhandler!\n");
1380                 lp->stats.tx_dropped++;
1381                 return 1;
1382         }
1383 
1384         if (lp->intx>1)
1385         {
1386                 BUGMSG(D_NORMAL,"send_packet called while intx!\n");
1387                 lp->stats.tx_dropped++;
1388                 return 1;
1389         }
1390 
1391         if (IF_TBUSY)
1392         {
1393                 /* If we get here, some higher level has decided we are broken.
1394                    There should really be a "kick me" function call instead. */
1395                 int tickssofar = jiffies - dev->trans_start;
1396                 /*int recbuf=lp->recbuf;*/
1397                 int status=inb(STATUS);
1398                 
1399                 if (tickssofar < TX_TIMEOUT) 
1400                 {
1401                         BUGMSG(D_DURING,"premature kickme! (status=%Xh ticks=%d o.skb=%ph numsegs=%d segnum=%d\n",
1402                                         status,tickssofar,lp->outgoing.skb,
1403                                         lp->outgoing.numsegs,
1404                                         lp->outgoing.segnum);
1405                         return 1;
1406                 }
1407 
1408                 lp->intmask &= ~TXFREEflag;
1409                 SETMASK;
1410                 
1411                 if (status&TXFREEflag)  /* transmit _DID_ finish */
1412                 {
1413                         BUGMSG(D_NORMAL,"tx timeout - missed IRQ? (status=%Xh, ticks=%d, mask=%Xh, dest=%d)\n",
1414                                         status,tickssofar,lp->intmask,lp->lasttrans_dest);
1415                         lp->stats.tx_errors++;
1416                 }
1417                 else
1418                 {
1419                         BUGMSG(D_EXTRA,"tx timed out (status=%Xh, tickssofar=%d, intmask=%Xh, dest=%d)\n",
1420                                         status,tickssofar,lp->intmask,lp->lasttrans_dest);
1421                         lp->stats.tx_errors++;
1422                         lp->stats.tx_aborted_errors++;
1423 
1424                         outb(NOTXcmd,COMMAND);
1425                 }
1426 
1427                 if (lp->outgoing.skb)
1428                 {
1429                         dev_kfree_skb(lp->outgoing.skb,FREE_WRITE);
1430                         lp->stats.tx_dropped++;
1431                 }
1432                 lp->outgoing.skb=NULL;
1433                 
1434                 TBUSY=0;
1435                 lp->txready=0;
1436                 lp->sending=0;
1437 
1438                 return 1;
1439         }
1440 
1441         /* If some higher layer thinks we've missed a tx-done interrupt
1442            we are passed NULL. Caution: dev_tint() handles the cli()/sti()
1443            itself. */
1444         if (skb == NULL) {
1445                 BUGMSG(D_NORMAL,"tx passed null skb (status=%Xh, inTX=%d, tickssofar=%ld)\n",
1446                         inb(STATUS),lp->intx,jiffies-dev->trans_start);
1447                 lp->stats.tx_errors++;
1448                 dev_tint(dev);
1449                 return 0;
1450         }
1451         
1452         if (lp->txready)        /* transmit already in progress! */
1453         {
1454                 BUGMSG(D_NORMAL,"trying to start new packet while busy! (status=%Xh)\n",
1455                         inb(STATUS));
1456                 lp->intmask &= ~TXFREEflag;
1457                 SETMASK;
1458                 outb(NOTXcmd,COMMAND); /* abort current send */
1459                 arcnet_inthandler(dev); /* fake an interrupt */
1460                 lp->stats.tx_errors++;
1461                 lp->stats.tx_fifo_errors++;
1462                 lp->txready=0;  /* we definitely need this line! */
1463                 
1464                 return 1;
1465         }
1466 
1467         /* Block a timer-based transmit from overlapping.  This could better be
1468            done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
1469         if (set_bit(0, (void*)&dev->tbusy) != 0)
1470         {
1471             BUGMSG(D_NORMAL,"transmitter called with busy bit set! (status=%Xh, inTX=%d, tickssofar=%ld)\n",
1472                         inb(STATUS),lp->intx,jiffies-dev->trans_start);
1473             lp->stats.tx_errors++;
1474             lp->stats.tx_fifo_errors++;
1475             return -EBUSY;
1476         }
1477 
1478         return 0;
1479 }
1480 
1481 
1482 /* Called by the kernel in order to transmit a packet.
1483  */
1484 static int
1485 arcnetA_send_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1486 {
1487         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1488         int ioaddr=dev->base_addr,bad;
1489         struct Outgoing *out=&(lp->outgoing);
1490 
1491         lp->intx++;
1492         
1493         bad=arcnet_send_packet_bad(skb,dev);
1494         if (bad)
1495         {
1496                 lp->intx--;
1497                 return bad;
1498         }
1499         
1500         TBUSY=1;
1501         
1502         out->length = 1 < skb->len ? skb->len : 1;
1503         out->hdr=(struct ClientData*)skb->data;
1504         out->skb=skb;
1505                 
1506         BUGLVL(D_SKB)
1507         {
1508                 short i;
1509                 for(i=0; i<skb->len; i++)
1510                 {
1511                         if(i%16 == 0) printk("\n[%04hX] ",i);
1512                         printk("%02hX ",((unsigned char*)skb->data)[i]);
1513                 }
1514                 printk("\n");
1515         }
1516 
1517         out->hdr->sequence=(lp->sequence++);
1518         
1519         /* fits in one packet? */
1520         if (out->length-EXTRA_CLIENTDATA<=XMTU)
1521         {
1522                 BUGMSG(D_DURING,"not splitting %d-byte packet. (split_flag=%d)\n",
1523                                 out->length,out->hdr->split_flag);
1524                 if (out->hdr->split_flag)
1525                         BUGMSG(D_NORMAL,"short packet has split_flag set?! (split_flag=%d)\n",
1526                                 out->hdr->split_flag);
1527                 out->numsegs=1;
1528                 out->segnum=1;
1529                 arcnetAS_prepare_tx(dev,
1530                         ((char *)out->hdr)+EXTRA_CLIENTDATA,
1531                         sizeof(struct ClientData)-EXTRA_CLIENTDATA,
1532                         ((char *)skb->data)+sizeof(struct ClientData),
1533                         out->length-sizeof(struct ClientData),
1534                         out->hdr->daddr,1);
1535 
1536                 /* done right away */
1537                 dev_kfree_skb(out->skb,FREE_WRITE);
1538                 out->skb=NULL;
1539                                         
1540                 if (arcnet_go_tx(dev,1))
1541                 {
1542                         /* inform upper layers */
1543                         TBUSY=0;
1544                         mark_bh(NET_BH);
1545                 }
1546         }
1547         else                    /* too big for one - split it */
1548         {
1549                 int maxsegsize=XMTU-4;
1550 
1551                 out->data=(u_char *)skb->data
1552                                 + sizeof(struct ClientData);
1553                 out->dataleft=out->length-sizeof(struct ClientData);
1554                 out->numsegs=(out->dataleft+maxsegsize-1)/maxsegsize;
1555                 out->segnum=0;
1556                 
1557                 BUGMSG(D_TX,"packet (%d bytes) split into %d fragments:\n",
1558                         out->length,out->numsegs);
1559 
1560                 /* if a packet waiting, launch it */
1561                 arcnet_go_tx(dev,1);
1562 
1563                 if (!lp->txready)
1564                 {
1565                         /* prepare a packet, launch it and prepare
1566                          * another.
1567                          */
1568                         arcnetA_continue_tx(dev);
1569                         if (arcnet_go_tx(dev,1))
1570                         {
1571                                 arcnetA_continue_tx(dev);
1572                                 arcnet_go_tx(dev,1);
1573                         }
1574                 }
1575                 
1576                 /* if segnum==numsegs, the transmission is finished;
1577                  * free the skb right away.
1578                  */
1579                 if (out->segnum==out->numsegs)
1580                 {
1581                         /* transmit completed */
1582                         out->segnum++;
1583                         if (out->skb)
1584                                 dev_kfree_skb(out->skb,FREE_WRITE);
1585                         out->skb=NULL;
1586                 }
1587         }
1588 
1589         dev->trans_start=jiffies;
1590         lp->intx--;
1591         
1592         /* make sure we didn't ignore a TX IRQ while we were in here */
1593         lp->intmask |= TXFREEflag;
1594         SETMASK;
1595 
1596         return 0;
1597 }
1598 
1599 
1600 /* After an RFC1201 split packet has been set up, this function calls
1601  * arcnetAS_prepare_tx to load the next segment into the card.  This function
1602  * does NOT automatically call arcnet_go_tx.
1603  */
1604 static void arcnetA_continue_tx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1605 {
1606         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1607         int ioaddr=dev->base_addr,maxsegsize=XMTU-4;
1608         struct Outgoing *out=&(lp->outgoing);
1609         
1610         BUGMSG(D_DURING,"continue_tx called (status=%Xh, intx=%d, intxh=%d, intmask=%Xh\n",
1611                 inb(STATUS),lp->intx,lp->in_txhandler,lp->intmask);
1612         
1613         if (lp->txready)
1614         {
1615                 BUGMSG(D_NORMAL,"continue_tx: called with packet in buffer!\n");
1616                 return;
1617         }
1618         
1619         if (out->segnum>=out->numsegs)
1620         {
1621                 BUGMSG(D_NORMAL,"continue_tx: building segment %d of %d!\n",
1622                         out->segnum+1,out->numsegs);
1623         }
1624 
1625         if (!out->segnum)       /* first packet */
1626                 out->hdr->split_flag=((out->numsegs-2)<<1)+1;
1627         else
1628                 out->hdr->split_flag=out->segnum<<1;
1629 
1630         out->seglen=maxsegsize;
1631         if (out->seglen>out->dataleft) out->seglen=out->dataleft;
1632                         
1633         BUGMSG(D_TX,"building packet #%d (%d bytes) of %d (%d total), splitflag=%d\n",
1634                 out->segnum+1,out->seglen,out->numsegs,
1635                 out->length,out->hdr->split_flag);
1636 
1637         arcnetAS_prepare_tx(dev,((char *)out->hdr)+EXTRA_CLIENTDATA,
1638                 sizeof(struct ClientData)-EXTRA_CLIENTDATA,
1639                 out->data,out->seglen,out->hdr->daddr,1);
1640                 
1641         out->dataleft-=out->seglen;
1642         out->data+=out->seglen;
1643         out->segnum++;
1644 }
1645 
1646 
1647 /* Given an skb, copy a packet into the ARCnet buffers for later transmission
1648  * by arcnet_go_tx.
1649  */
1650 static void
1651 arcnetAS_prepare_tx(struct device *dev,u_char *hdr,int hdrlen,
     /* [previous][next][first][last][top][bottom][index][help] */
1652                 char *data,int length,int daddr,int exceptA)
1653 {
1654         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1655         struct ClientData *arcsoft;
1656         union ArcPacket *arcpacket = 
1657                 (union ArcPacket *)(dev->mem_start+512*(lp->txbuf^1));
1658         int offset;
1659         
1660 #ifdef SLOW_XMIT_COPY
1661         char *iptr,*iend,*optr;
1662 #endif
1663         
1664         lp->txbuf=lp->txbuf^1;  /* XOR with 1 to alternate between 2 and 3 */
1665         
1666         length+=hdrlen;
1667 
1668         BUGMSG(D_TX,"arcnetAS_prep_tx: hdr:%ph, length:%d, data:%ph\n",
1669                         hdr,length,data);
1670 
1671 #ifndef SLOW_XMIT_COPY
1672         /* clean out the page to make debugging make more sense :) */
1673         BUGLVL(D_DURING)
1674                 memset((void *)dev->mem_start+lp->txbuf*512,0x42,512);
1675 #endif
1676 
1677         arcpacket->hardheader.destination=daddr;
1678 
1679         /* load packet into shared memory */
1680         if (length<=MTU)        /* Normal (256-byte) Packet */
1681         {
1682                 arcpacket->hardheader.offset1=offset=256-length;
1683                 arcsoft=(struct ClientData *)
1684                         (&arcpacket->raw[offset]);
1685         }
1686         else if (length>=MinTU) /* Extended (512-byte) Packet */
1687         {
1688                 arcpacket->hardheader.offset1=0;
1689                 arcpacket->hardheader.offset2=offset=512-length;
1690                 
1691                 arcsoft=(struct ClientData *)
1692                         (&arcpacket->raw[offset]);
1693         }
1694         else if (exceptA)               /* RFC1201 Exception Packet */
1695         {
1696                 arcpacket->hardheader.offset1=0;
1697                 arcpacket->hardheader.offset2=offset=512-length-4;
1698                 arcsoft=(struct ClientData *)
1699                         (&arcpacket->raw[offset+4]);
1700                 
1701                 /* exception-specific stuff - these four bytes
1702                  * make the packet long enough to fit in a 512-byte
1703                  * frame.
1704                  */
1705                 arcpacket->raw[offset+0]=hdr[0];
1706                 arcpacket->raw[offset+1]=0xFF; /* FF flag */
1707                         arcpacket->raw[offset+2]=0xFF; /* FF padding */
1708                         arcpacket->raw[offset+3]=0xFF; /* FF padding */
1709         }
1710         else                            /* "other" Exception packet */
1711         {
1712                 /* RFC1051 - set 4 trailing bytes to 0 */
1713                 memset(&arcpacket->raw[508],0,4);
1714 
1715                 /* now round up to MinTU */
1716                 arcpacket->hardheader.offset1=0;
1717                 arcpacket->hardheader.offset2=offset=512-MinTU;
1718                 arcsoft=(struct ClientData *)(&arcpacket->raw[offset]);
1719         }
1720 
1721 
1722         /* copy the packet into ARCnet shmem
1723          *  - the first bytes of ClientData header are skipped
1724          */
1725         memcpy((u_char*)arcsoft,
1726                 (u_char*)hdr,hdrlen);
1727 #ifdef SLOW_XMIT_COPY
1728         for (iptr=data,iend=iptr+length-hdrlen,optr=(char *)arcsoft+hdrlen;
1729                 iptr<iend; iptr++,optr++)
1730         {
1731                 *optr=*iptr;
1732                 /*udelay(5);*/
1733         }
1734 #else
1735         memcpy((u_char*)arcsoft+hdrlen,
1736                 data,length-hdrlen);
1737 #endif
1738                 
1739         BUGMSG(D_DURING,"transmitting packet to station %02Xh (%d bytes)\n",
1740                         daddr,length);
1741                         
1742         BUGLVL(D_TX)
1743         {
1744                 int countx,county;
1745                 
1746                 printk("%6s: packet dump [tx] follows:",dev->name);
1747 
1748                 for (county=0; county<16+(length>MTU)*16; county++)
1749                 {
1750                         printk("\n[%04X] ",county*16);
1751                         for (countx=0; countx<16; countx++)
1752                                 printk("%02X ",
1753                                         arcpacket->raw[county*16+countx]);
1754                 }
1755                 
1756                 printk("\n");
1757         }
1758 
1759         lp->lastload_dest=daddr;
1760         lp->txready=lp->txbuf;  /* packet is ready for sending */
1761 }
1762 
1763 /* Actually start transmitting a packet that was placed in the card's
1764  * buffer by arcnetAS_prepare_tx.  Returns 1 if a Tx is really started.
1765  *
1766  * This should probably always be called with the INTMASK register set to 0,
1767  * so go_tx is not called recursively.
1768  *
1769  * The enable_irq flag determines whether to actually write INTMASK value
1770  * to the card; TXFREEflag is always OR'ed into the memory variable either
1771  * way.
1772  */
1773 static int
1774 arcnet_go_tx(struct device *dev,int enable_irq)
     /* [previous][next][first][last][top][bottom][index][help] */
1775 {
1776         struct arcnet_local *lp=(struct arcnet_local *)dev->priv;
1777         int ioaddr=dev->base_addr;
1778 
1779         BUGMSG(D_DURING,"go_tx: status=%Xh, intmask=%Xh, txready=%d, sending=%d\n",
1780                 inb(STATUS),lp->intmask,lp->txready,lp->sending);
1781 
1782         if (lp->sending || !lp->txready)
1783         {
1784                 if (enable_irq && lp->sending)
1785                 {
1786                         lp->intmask |= TXFREEflag;
1787                         SETMASK;
1788                 }
1789                 return 0;
1790         }
1791         
1792         /* start sending */
1793         outb(TXcmd|(lp->txready<<3),COMMAND);
1794 
1795         lp->stats.tx_packets++;
1796         lp->txready=0;
1797         lp->sending++;
1798 
1799         lp->lasttrans_dest=lp->lastload_dest;
1800         lp->lastload_dest=0;
1801 
1802         lp->intmask |= TXFREEflag;
1803 
1804         if (enable_irq) SETMASK;
1805 
1806         return 1;
1807 }
1808 
1809 
1810 /****************************************************************************
1811  *                                                                          *
1812  * Interrupt handler                                                        *
1813  *                                                                          *
1814  ****************************************************************************/
1815 
1816 
1817 /* The typical workload of the driver:  Handle the network interface
1818  * interrupts.  This doesn't do much right now except call arcnet_inthandler,
1819  * which takes different parameters but may be called from other places
1820  * as well.
1821  */
1822 static void
1823 arcnet_interrupt(int irq,struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
1824 {
1825         struct device *dev = (struct device *)(irq2dev_map[irq]);
1826         int ioaddr;
1827         
1828         if (dev==NULL)
1829         {
1830                 BUGLVL(D_DURING)
1831                         printk("arcnet: irq %d for unknown device.\n", irq);
1832                 return;
1833         }
1834         
1835         BUGMSG(D_DURING,"in arcnet_interrupt\n");
1836 
1837         /* RESET flag was enabled - if !dev->start, we must clear it right
1838          * away (but nothing else) since inthandler() is never called.
1839          */
1840         ioaddr=dev->base_addr;
1841         if (!dev->start)
1842         {
1843                 if (inb(STATUS) & RESETflag)
1844                         outb(CFLAGScmd|RESETclear, COMMAND);
1845                 return;
1846         }
1847 
1848         /* Call the "real" interrupt handler. */
1849         arcnet_inthandler(dev);
1850 }
1851 
1852 
1853 /* The actual interrupt handler routine - handle various IRQ's generated
1854  * by the card.
1855  */
1856 static void
1857 arcnet_inthandler(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1858 {       
1859         struct arcnet_local *lp=(struct arcnet_local *)dev->priv;
1860         int ioaddr=dev->base_addr, status, boguscount = 3, didsomething;
1861 
1862         if (IF_INTERRUPT)
1863         {
1864                 BUGMSG(D_NORMAL,"DRIVER PROBLEM!  Nested arcnet interrupts!\n");
1865                 return; /* don't even try. */
1866         }
1867         
1868         outb(0,INTMASK);
1869         INTERRUPT = 1;
1870 
1871         BUGMSG(D_DURING,"in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
1872                 inb(STATUS),lp->intmask);
1873 
1874         do
1875         {
1876                 status = inb(STATUS);
1877                 didsomething=0;
1878         
1879 
1880                 /* RESET flag was enabled - card is resetting and if RX
1881                  * is disabled, it's NOT because we just got a packet.
1882                  */
1883                 if (status & RESETflag)
1884                 {
1885                         BUGMSG(D_NORMAL,"spurious reset (status=%Xh)\n",
1886                                         status);
1887                         arcnet_reset(dev,0);
1888                         
1889                         /* all other flag values are just garbage */
1890                         break;
1891                 }
1892                 
1893                 
1894                 /* RX is inhibited - we must have received something. */
1895                 if (status & lp->intmask & NORXflag)
1896                 {
1897                         int recbuf=lp->recbuf=!lp->recbuf;
1898 
1899                         BUGMSG(D_DURING,"receive irq (status=%Xh)\n",
1900                                         status);
1901 
1902                         /* enable receive of our next packet */
1903                         EnableReceiver();
1904 
1905                         /* Got a packet. */
1906                         arcnet_rx(dev,!recbuf);
1907                         didsomething++;
1908                 }
1909                 
1910                 /* it can only be an xmit-done irq if we're xmitting :) */
1911                 /*if (status&TXFREEflag && !lp->in_txhandler && lp->sending)*/
1912                 if (status & lp->intmask & TXFREEflag)
1913                 {
1914                         struct Outgoing *out=&(lp->outgoing);
1915                         int was_sending=lp->sending;
1916                         
1917                         lp->intmask &= ~TXFREEflag;
1918 
1919                         lp->in_txhandler++;
1920                         if (was_sending) lp->sending--;
1921 
1922                         BUGMSG(D_DURING,"TX IRQ (stat=%Xh, numsegs=%d, segnum=%d, skb=%ph)\n",
1923                                         status,out->numsegs,out->segnum,out->skb);
1924                                         
1925                         if (was_sending && !(status&TXACKflag))
1926                         {
1927                                 if (lp->lasttrans_dest != 0)
1928                                 {
1929                                         BUGMSG(D_EXTRA,"transmit was not acknowledged! (status=%Xh, dest=%d)\n",
1930                                                 status,lp->lasttrans_dest);
1931                                         lp->stats.tx_errors++;
1932                                         lp->stats.tx_carrier_errors++;
1933                                 }
1934                                 else
1935                                 {
1936                                         BUGMSG(D_DURING,"broadcast was not acknowledged; that's normal (status=%Xh, dest=%d)\n",
1937                                                         status,
1938                                                         lp->lasttrans_dest);
1939                                 }
1940                         }
1941 
1942                         /* send packet if there is one */
1943                         arcnet_go_tx(dev,0);
1944                         didsomething++;
1945                         
1946                         if (lp->intx)
1947                         {
1948                                 BUGMSG(D_DURING,"TXDONE while intx! (status=%Xh, intx=%d)\n",
1949                                         inb(STATUS),lp->intx);
1950                                 lp->in_txhandler--;
1951                                 continue;
1952                         }
1953 
1954                         if (!lp->outgoing.skb)
1955                         {
1956                                 BUGMSG(D_DURING,"TX IRQ done: no split to continue.\n");
1957                                 
1958                                 /* inform upper layers */
1959                                 if (!lp->txready && IF_TBUSY)
1960                                 {
1961                                         TBUSY=0;
1962                                         mark_bh(NET_BH);
1963                                 }
1964                                 lp->in_txhandler--;
1965                                 continue;
1966                         }
1967                         
1968                         /* if more than one segment, and not all segments
1969                          * are done, then continue xmit.
1970                          */
1971                         if (out->segnum<out->numsegs)
1972                                 arcnetA_continue_tx(dev);
1973                         arcnet_go_tx(dev,0);
1974 
1975                         /* if segnum==numsegs, the transmission is finished;
1976                          * free the skb.
1977                          */
1978                         if (out->segnum>=out->numsegs)
1979                         {
1980                                 /* transmit completed */
1981                                 out->segnum++;
1982                                 if (out->skb)
1983                                         dev_kfree_skb(out->skb,FREE_WRITE);
1984                                 out->skb=NULL;
1985 
1986                                 /* inform upper layers */
1987                                 if (!lp->txready && IF_TBUSY)
1988                                 {
1989                                         TBUSY=0;
1990                                         mark_bh(NET_BH);
1991                                 }
1992                         }
1993                         didsomething++;
1994 
1995                         lp->in_txhandler--;
1996                 }
1997                 else if (lp->txready && !lp->sending && !lp->intx)
1998                 {
1999                         BUGMSG(D_NORMAL,"recovery from silent TX (status=%Xh)\n",
2000                                                 status);
2001                         arcnet_go_tx(dev,0);
2002                         didsomething++;
2003                 }
2004 
2005 #ifdef DETECT_RECONFIGS
2006                 if (status & (lp->intmask) & RECONflag)
2007                 {
2008                         outb(CFLAGScmd|CONFIGclear,COMMAND);
2009                         lp->stats.tx_carrier_errors++;
2010                         
2011                         #ifdef SHOW_RECONFIGS
2012                         BUGMSG(D_NORMAL,"Network reconfiguration detected (status=%Xh)\n",
2013                                         status);
2014                         #endif /* SHOW_RECONFIGS */
2015                         
2016                         #ifdef RECON_THRESHOLD
2017                         /* is the RECON info empty or old? */
2018                         if (!lp->first_recon || !lp->last_recon || 
2019                                 jiffies-lp->last_recon > HZ*10)
2020                         {
2021                                 if (lp->network_down)
2022                                         BUGMSG(D_NORMAL,"reconfiguration detected: cabling restored?\n");
2023                                 lp->first_recon=lp->last_recon=jiffies;
2024                                 lp->num_recons=lp->network_down=0;
2025                                 
2026                                 BUGMSG(D_DURING,"recon: clearing counters.\n");
2027                         }
2028                         else /* add to current RECON counter */
2029                         {
2030                                 lp->last_recon=jiffies;
2031                                 lp->num_recons++;
2032                                 
2033                                 BUGMSG(D_DURING,"recon: counter=%d, time=%lds, net=%d\n",
2034                                         lp->num_recons,
2035                                         (lp->last_recon-lp->first_recon)/HZ,
2036                                         lp->network_down);
2037 
2038                                 /* if network is marked up;
2039                                  * and first_recon and last_recon are 60+ sec
2040                                  *   apart;
2041                                  * and the average no. of recons counted is
2042                                  *   > RECON_THRESHOLD/min;
2043                                  * then print a warning message.
2044                                  */
2045                                 if (!lp->network_down
2046                                     && (lp->last_recon-lp->first_recon)<=HZ*60
2047                                     && lp->num_recons >= RECON_THRESHOLD)
2048                                 {
2049                                         lp->network_down=1;
2050                                         BUGMSG(D_NORMAL,"many reconfigurations detected: cabling problem?\n");
2051                                 }
2052                                 else if (!lp->network_down
2053                                     && lp->last_recon-lp->first_recon > HZ*60)
2054                                 {
2055                                         /* reset counters if we've gone for
2056                                          * over a minute.
2057                                          */
2058                                         lp->first_recon=lp->last_recon;
2059                                         lp->num_recons=1;
2060                                 }
2061                         }
2062                         #endif
2063                 }
2064                 #ifdef RECON_THRESHOLD
2065                 else if (lp->network_down && jiffies-lp->last_recon > HZ*10)
2066                 {
2067                         if (lp->network_down)
2068                                 BUGMSG(D_NORMAL,"cabling restored?\n");
2069                         lp->first_recon=lp->last_recon=0;
2070                         lp->num_recons=lp->network_down=0;
2071                         
2072                         BUGMSG(D_DURING,"not recon: clearing counters anyway.\n");
2073                 }
2074                 #endif
2075 #endif /* DETECT_RECONFIGS */
2076         } while (--boguscount && didsomething);
2077 
2078         BUGMSG(D_DURING,"net_interrupt complete (status=%Xh, count=%d)\n\n",
2079                         inb(STATUS),boguscount);
2080 
2081         SETMASK;        /* put back interrupt mask */
2082 
2083         INTERRUPT=0;
2084 }
2085 
2086 
2087 /****************************************************************************
2088  *                                                                          *
2089  * Receiver routines                                                        *
2090  *                                                                          *
2091  ****************************************************************************/
2092 
2093 
2094 /* A packet has arrived; grab it from the buffers and possibly unsplit it.
2095  * This is a generic packet receiver that calls arcnet??_rx depending on the
2096  * protocol ID found.
2097  */
2098 static void
2099 arcnet_rx(struct device *dev,int recbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
2100 {
2101         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
2102         int ioaddr = dev->base_addr;
2103         union ArcPacket *arcpacket=
2104                 (union ArcPacket *)(dev->mem_start+recbuf*512);
2105         u_char *arcsoft;
2106         short length,offset;
2107         u_char daddr,saddr;
2108         
2109         lp->stats.rx_packets++;
2110 
2111         saddr=arcpacket->hardheader.source;
2112         daddr=arcpacket->hardheader.destination;
2113         
2114         /* if source is 0, it's a "used" packet! */
2115         if (saddr==0)
2116         {
2117                 BUGMSG(D_NORMAL,"discarding old packet. (status=%Xh)\n",
2118                         inb(STATUS));
2119                 lp->stats.rx_errors++;
2120                 return;
2121         }
2122         arcpacket->hardheader.source=0;
2123         
2124         if (arcpacket->hardheader.offset1) /* Normal Packet */
2125         {
2126                 offset=arcpacket->hardheader.offset1;
2127                 arcsoft=&arcpacket->raw[offset];
2128                 length=256-offset;
2129         }
2130         else            /* ExtendedPacket or ExceptionPacket */
2131         {
2132                 offset=arcpacket->hardheader.offset2;
2133                 arcsoft=&arcpacket->raw[offset];
2134 
2135                 length=512-offset;
2136         }
2137         
2138         BUGMSG(D_DURING,"received packet from %02Xh to %02Xh (%d bytes)\n",
2139                         saddr,daddr,length);
2140 
2141         /* call the right receiver for the protocol */
2142         switch (arcsoft[0])
2143         {
2144         case ARC_P_IP:
2145         case ARC_P_ARP:
2146         case ARC_P_RARP:
2147         case ARC_P_IPX:
2148         case ARC_P_NOVELL_EC:
2149                 arcnetA_rx(lp->adev,arcsoft,length,saddr,daddr);
2150                 break;
2151 #ifdef CONFIG_ARCNET_ETH
2152         case ARC_P_ETHER:
2153                 arcnetE_rx(lp->edev,arcsoft,length,saddr,daddr);
2154                 break;
2155 #endif
2156 #ifdef CONFIG_ARCNET_1051
2157         case ARC_P_IP_RFC1051:
2158         case ARC_P_ARP_RFC1051:
2159                 arcnetS_rx(lp->sdev,arcsoft,length,saddr,daddr);
2160                 break;
2161 #endif
2162         case ARC_P_LANSOFT: /* don't understand.  fall through. */
2163         default:
2164                 BUGMSG(D_NORMAL,"received unknown protocol %d (%Xh) from station %d.\n",
2165                         arcsoft[0],arcsoft[0],saddr);
2166                 lp->stats.rx_errors++;
2167                 lp->stats.rx_crc_errors++;
2168                 break;
2169         }
2170 
2171         BUGLVL(D_RX)
2172         {
2173                 int countx,county;
2174                 
2175                 printk("%6s: packet dump [rx] follows:",dev->name);
2176 
2177                 for (county=0; county<16+(length>240)*16; county++)
2178                 {
2179                         printk("\n[%04X] ",county*16);
2180                         for (countx=0; countx<16; countx++)
2181                                 printk("%02X ",
2182                                         arcpacket->raw[county*16+countx]);
2183                 }
2184                 
2185                 printk("\n");
2186         }
2187 
2188 
2189 #ifndef SLOW_XMIT_COPY
2190         /* clean out the page to make debugging make more sense :) */
2191         BUGLVL(D_DURING)
2192                 memset((void *)arcpacket->raw,0x42,512);
2193 #endif
2194 
2195 
2196         /* If any worth-while packets have been received, a mark_bh(NET_BH)
2197          * has been done by netif_rx and Linux will handle them after we
2198          * return.
2199          */
2200 }
2201 
2202 
2203 /* Packet receiver for "standard" RFC1201-style packets
2204  */
2205 static void
2206 arcnetA_rx(struct device *dev,u_char *buf,
     /* [previous][next][first][last][top][bottom][index][help] */
2207         int length,u_char saddr, u_char daddr)
2208 {
2209         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
2210         struct sk_buff *skb;
2211         struct ClientData *arcsoft,*soft;
2212         
2213         BUGMSG(D_DURING,"it's an RFC1201 packet (length=%d)\n",
2214                         length);
2215                         
2216         /* compensate for EXTRA_CLIENTDATA (which isn't actually in the
2217          * packet)
2218          */
2219         arcsoft=(struct ClientData *)(buf-EXTRA_CLIENTDATA);
2220         length+=EXTRA_CLIENTDATA;
2221 
2222         if (arcsoft->split_flag==0xFF)  /* Exception Packet */
2223         {
2224                 BUGMSG(D_DURING,"compensating for exception packet\n");
2225 
2226                 /* skip over 4-byte junkola */
2227                 arcsoft=(struct ClientData *)
2228                         ((u_char *)arcsoft + 4);
2229                 length-=4;
2230         }
2231         
2232         if (!arcsoft->split_flag)               /* not split */
2233         {
2234                 struct Incoming *in=&lp->incoming[saddr];
2235 
2236                 BUGMSG(D_RX,"incoming is not split (splitflag=%d)\n",
2237                         arcsoft->split_flag);
2238                         
2239                 if (in->skb)    /* already assembling one! */
2240                 {
2241                         BUGMSG(D_NORMAL,"aborting assembly (seq=%d) for unsplit packet (splitflag=%d, seq=%d)\n",
2242                                 in->sequence,arcsoft->split_flag,
2243                                 arcsoft->sequence);
2244                         kfree_skb(in->skb,FREE_WRITE);
2245                         lp->stats.rx_errors++;
2246                         lp->stats.rx_missed_errors++;
2247                         in->skb=NULL;
2248                 }
2249                 
2250                 in->sequence=arcsoft->sequence;
2251 
2252                 skb = alloc_skb(length, GFP_ATOMIC);
2253                 if (skb == NULL) {
2254                         BUGMSG(D_NORMAL,"Memory squeeze, dropping packet.\n");
2255                         lp->stats.rx_dropped++;
2256                         return;
2257                 }
2258                 soft=(struct ClientData *)skb->data;
2259                 
2260                 skb->len = length;
2261                 skb->dev = dev;
2262                 
2263                 memcpy((u_char *)soft+EXTRA_CLIENTDATA,
2264                         (u_char *)arcsoft+EXTRA_CLIENTDATA,
2265                         length-EXTRA_CLIENTDATA);
2266                 soft->daddr=daddr;
2267                 soft->saddr=saddr;
2268                 
2269                 /* ARP packets have problems when sent from DOS.
2270                  * source address is always 0 on some systems!  So we take
2271                  * the hardware source addr (which is impossible to fumble)
2272                  * and insert it ourselves.
2273                  */
2274                 if (soft->protocol_id == ARC_P_ARP)
2275                 {
2276                         struct arphdr *arp=(struct arphdr *)
2277                                         ((char *)soft+sizeof(struct ClientData));
2278 
2279                         /* make sure addresses are the right length */
2280                         if (arp->ar_hln==1 && arp->ar_pln==4)
2281                         {
2282                                 char *cptr=(char *)(arp)+sizeof(struct arphdr);
2283                                 
2284                                 if (!*cptr)     /* is saddr = 00? */
2285                                 {
2286                                         BUGMSG(D_EXTRA,"ARP source address was 00h, set to %02Xh.\n",
2287                                                         saddr);
2288                                         lp->stats.rx_crc_errors++;
2289                                         *cptr=saddr;
2290                                 }
2291                                 else
2292                                 {
2293                                         BUGMSG(D_DURING,"ARP source address (%Xh) is fine.\n",
2294                                                 *cptr);
2295                                 }
2296                         }
2297                         else
2298                         {
2299                                 BUGMSG(D_NORMAL,"funny-shaped ARP packet. (%Xh, %Xh)\n",
2300                                         arp->ar_hln,arp->ar_pln);
2301                                 lp->stats.rx_errors++;
2302                                 lp->stats.rx_crc_errors++;
2303                         }
2304                 }
2305                 
2306                 BUGLVL(D_SKB)
2307                 {
2308                     short i;
2309                         for(i=0; i< skb->len; i++)
2310                         {
2311                                 if( i%16 == 0 ) printk("\n[%04hX] ",i);
2312                                 printk("%02hX ",((unsigned char*)skb->data)[i]);
2313                         }
2314                         printk("\n");
2315                 }
2316 
2317                 skb->protocol=arcnetA_type_trans(skb,dev);
2318 
2319                 netif_rx(skb);
2320          }
2321          else                   /* split packet */
2322          {
2323                  /* NOTE:  MSDOS ARP packet correction should only need to
2324                   * apply to unsplit packets, since ARP packets are so short.
2325                   *
2326                   * My interpretation of the RFC1201 (ARCnet) document is that
2327                   * if a packet is received out of order, the entire assembly
2328                   * process should be aborted.
2329                   *
2330                   * The RFC also mentions "it is possible for successfully
2331                   * received packets to be retransmitted."  As of 0.40 all
2332                   * previously received packets are allowed, not just the
2333                   * most recent one.
2334                   *
2335                   * We allow multiple assembly processes, one for each
2336                   * ARCnet card possible on the network.  Seems rather like
2337                   * a waste of memory.  Necessary?
2338                   */
2339                   
2340                 struct Incoming *in=&lp->incoming[saddr];
2341                 
2342                 BUGMSG(D_RX,"packet is split (splitflag=%d, seq=%d)\n",
2343                         arcsoft->split_flag,in->sequence);
2344 
2345                 if (in->skb && in->sequence!=arcsoft->sequence)
2346                 {
2347                         BUGMSG(D_NORMAL,"wrong seq number (saddr=%d, expected=%d, seq=%d, splitflag=%d)\n",
2348                                 saddr,in->sequence,arcsoft->sequence,
2349                                 arcsoft->split_flag);
2350                         kfree_skb(in->skb,FREE_WRITE);
2351                         in->skb=NULL;
2352                         lp->stats.rx_errors++;
2353                         lp->stats.rx_missed_errors++;
2354                         in->lastpacket=in->numpackets=0;
2355                 }
2356                   
2357                 if (arcsoft->split_flag & 1)    /* first packet in split */
2358                 {
2359                         BUGMSG(D_RX,"brand new splitpacket (splitflag=%d)\n",
2360                                 arcsoft->split_flag);
2361                         if (in->skb)    /* already assembling one! */
2362                         {
2363                                 BUGMSG(D_NORMAL,"aborting previous (seq=%d) assembly (splitflag=%d, seq=%d)\n",
2364                                         in->sequence,arcsoft->split_flag,
2365                                         arcsoft->sequence);
2366                                 lp->stats.rx_errors++;
2367                                 lp->stats.rx_missed_errors++;
2368                                 kfree_skb(in->skb,FREE_WRITE);
2369                         }
2370 
2371                         in->sequence=arcsoft->sequence;
2372                         in->numpackets=((unsigned)arcsoft->split_flag>>1)+2;
2373                         in->lastpacket=1;
2374                         
2375                         if (in->numpackets>16)
2376                         {
2377                                 BUGMSG(D_NORMAL,"incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
2378                                         arcsoft->split_flag);
2379                                 lp->stats.rx_errors++;
2380                                 lp->stats.rx_length_errors++;
2381                                 return;
2382                         }
2383                 
2384                         in->skb=skb=alloc_skb(508*in->numpackets
2385                                         + sizeof(struct ClientData),
2386                                         GFP_ATOMIC);
2387                         if (skb == NULL) {
2388                                 BUGMSG(D_NORMAL,"(split) memory squeeze, dropping packet.\n");
2389                                 lp->stats.rx_dropped++;
2390                                 return;
2391                         }
2392                                         
2393                         /* I don't know what this is for, but it DOES avoid
2394                          * warnings...
2395                          */
2396                         skb->free=1;
2397                         
2398                         soft=(struct ClientData *)skb->data;
2399                         
2400                         skb->len=sizeof(struct ClientData);
2401                         skb->dev=dev;
2402 
2403                         memcpy((u_char *)soft+EXTRA_CLIENTDATA,
2404                                 (u_char *)arcsoft+EXTRA_CLIENTDATA,
2405                                 sizeof(struct ClientData)-EXTRA_CLIENTDATA);
2406                         soft->split_flag=0; /* final packet won't be split */
2407                 }
2408                 else                    /* not first packet */
2409                 {
2410                         int packetnum=((unsigned)arcsoft->split_flag>>1) + 1;
2411 
2412                         /* if we're not assembling, there's no point
2413                          * trying to continue.
2414                          */                     
2415                         if (!in->skb)
2416                         {
2417                                 BUGMSG(D_NORMAL,"can't continue split without starting first! (splitflag=%d, seq=%d)\n",
2418                                         arcsoft->split_flag,arcsoft->sequence);
2419                                 lp->stats.rx_errors++;
2420                                 lp->stats.rx_missed_errors++;
2421                                 return;
2422                         }
2423 
2424                         in->lastpacket++;
2425                         if (packetnum!=in->lastpacket) /* not the right flag! */
2426                         {
2427                                 /* harmless duplicate? ignore. */
2428                                 if (packetnum<=in->lastpacket-1)
2429                                 {
2430                                         BUGMSG(D_NORMAL,"duplicate splitpacket ignored! (splitflag=%d)\n",
2431                                                 arcsoft->split_flag);
2432                                         lp->stats.rx_errors++;
2433                                         lp->stats.rx_frame_errors++;
2434                                         return;
2435                                 }
2436                                 
2437                                 /* "bad" duplicate, kill reassembly */
2438                                 BUGMSG(D_NORMAL,"out-of-order splitpacket, reassembly (seq=%d) aborted (splitflag=%d, seq=%d)\n",       
2439                                         in->sequence,arcsoft->split_flag,
2440                                         arcsoft->sequence);
2441                                 kfree_skb(in->skb,FREE_WRITE);
2442                                 in->skb=NULL;
2443                                 lp->stats.rx_errors++;
2444                                 lp->stats.rx_missed_errors++;
2445                                 in->lastpacket=in->numpackets=0;
2446                                 return;
2447                         }
2448 
2449                         soft=(struct ClientData *)in->skb->data;
2450                 }
2451                 
2452                 skb=in->skb;
2453                 
2454                 memcpy(skb->data+skb->len,
2455                        (u_char *)arcsoft+sizeof(struct ClientData),
2456                        length-sizeof(struct ClientData));
2457 
2458                 skb->len+=length-sizeof(struct ClientData);
2459                 
2460                 soft->daddr=daddr;
2461                 soft->saddr=saddr;
2462                 
2463                 /* are we done? */
2464                 if (in->lastpacket == in->numpackets)
2465                 {
2466                         if (!skb || !in->skb)
2467                         {
2468                                 BUGMSG(D_NORMAL,"?!? done reassembling packet, no skb? (skb=%ph, in->skb=%ph)\n",
2469                                         skb,in->skb);
2470                         }
2471                         else
2472                         {
2473                                 in->skb=NULL;
2474                                 in->lastpacket=in->numpackets=0;
2475                         
2476                                 BUGLVL(D_SKB)
2477                                 {
2478                                         short i;
2479                                         for(i=0; i<skb->len; i++)
2480                                         {
2481                                                 if( i%16 == 0 ) printk("\n[%04hX] ",i);
2482                                                 printk("%02hX ",((unsigned char*)skb->data)[i]);
2483                                         }
2484                                         printk("\n");
2485                                 }
2486 
2487                         
2488                                 skb->protocol=arcnetA_type_trans(skb,dev);
2489                                 
2490                                 netif_rx(skb);
2491                         }
2492                 }
2493          }
2494 }
2495 
2496 
2497 
2498 
2499 /****************************************************************************
2500  *                                                                          *
2501  * Miscellaneous routines                                                   *
2502  *                                                                          *
2503  ****************************************************************************/
2504 
2505 
2506 /* Get the current statistics.  This may be called with the card open or
2507  * closed.
2508  */
2509 
2510 static struct enet_statistics *
2511 arcnet_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2512 {
2513         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
2514 
2515         return &lp->stats;
2516 }
2517 
2518 #if 0
2519 /* Set or clear the multicast filter for this adaptor.
2520  * num_addrs == -1      Promiscuous mode, receive all packets
2521  * num_addrs == 0       Normal mode, clear multicast list
2522  * num_addrs > 0        Multicast mode, receive normal and MC packets, and do
2523  *                      best-effort filtering.
2524  */
2525 static void
2526 set_multicast_list(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2527 {
2528 #if 0     /* no promiscuous mode at all on most ARCnet models */
2529         struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
2530 
2531         short ioaddr = dev->base_addr;
2532         if (num_addrs) {
2533                 outw(69, ioaddr);               /* Enable promiscuous mode */
2534         } else
2535                 outw(99, ioaddr);               /* Disable promiscuous mode, use normal mode */
2536 #endif
2537 }
2538 #endif
2539 
2540 /* Create the ARCnet ClientData header for an arbitrary protocol layer
2541  *
2542  * saddr=NULL   means use device source address (always will anyway)
2543  * daddr=NULL   means leave destination address (eg unresolved arp)
2544  */
2545 int arcnetA_header(struct sk_buff *skb,struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
2546                 unsigned short type,void *daddr,void *saddr,unsigned len)
2547 {
2548         struct ClientData *head = (struct ClientData *)
2549                 skb_push(skb,dev->hard_header_len);
2550         struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
2551 
2552         BUGMSG(D_DURING,"create header from %d to %d; protocol %d (%Xh); size %u.\n",
2553                         saddr ? *(u_char*)saddr : -1,
2554                         daddr ? *(u_char*)daddr : -1,
2555                         type,type,len);
2556 
2557         /* set the protocol ID according to RFC1201 */
2558         switch(type)
2559         {
2560         case ETH_P_IP:
2561                 head->protocol_id=ARC_P_IP;
2562                 break;
2563         case ETH_P_ARP:
2564                 head->protocol_id=ARC_P_ARP;
2565                 break;
2566         case ETH_P_RARP:
2567                 head->protocol_id=ARC_P_RARP;
2568                 break;
2569         case ETH_P_IPX:
2570         case ETH_P_802_3:
2571         case ETH_P_802_2:
2572                 head->protocol_id=ARC_P_IPX;
2573                 break;
2574         case ETH_P_ATALK:
2575                 head->protocol_id=ARC_P_ATALK;
2576                 break;
2577         default:
2578                 BUGMSG(D_NORMAL,"I don't understand protocol %d (%Xh)\n",
2579                         type,type);
2580                 lp->stats.tx_errors++;
2581                 lp->stats.tx_aborted_errors++;
2582                 return 0;
2583         }       
2584 
2585         /*
2586          * Set the source hardware address.
2587          *
2588          * This is pretty pointless for most purposes, but it can help
2589          * in debugging.  saddr is stored in the ClientData header and
2590          * removed before sending the packet (since ARCnet does not allow
2591          * us to change the source address in the actual packet sent)
2592          */
2593         if(saddr)
2594                 head->saddr=((u_char*)saddr)[0];
2595         else
2596                 head->saddr=((u_char*)(dev->dev_addr))[0];
2597 
2598         head->split_flag=0;     /* split packets are done elsewhere */
2599         head->sequence=0;       /* so are sequence numbers */
2600 
2601         /* supposedly if daddr is NULL, we should ignore it... */
2602         if(daddr)
2603         {
2604                 head->daddr=((u_char*)daddr)[0];
2605                 return dev->hard_header_len;
2606         }
2607         else
2608                 head->daddr=0;  /* better fill one in anyway */
2609                 
2610         return -dev->hard_header_len;
2611 }
2612 
2613 
2614 
2615 /* Rebuild the ARCnet ClientData header. This is called after an ARP
2616  * (or in future other address resolution) has completed on this
2617  * sk_buff. We now let ARP fill in the other fields.
2618  */
2619 int arcnetA_rebuild_header(void *buff,struct device *dev,unsigned long dst,
     /* [previous][next][first][last][top][bottom][index][help] */
2620                 struct sk_buff *skb)
2621 {
2622         struct ClientData *head = (struct ClientData *)buff;
2623         struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
2624         int status;
2625 
2626         /*
2627          * Only ARP and IP are currently supported
2628          */
2629          
2630         if(head->protocol_id != ARC_P_IP) 
2631         {
2632                 BUGMSG(D_NORMAL,"I don't understand protocol type %d (%Xh) addresses!\n",
2633                         head->protocol_id,head->protocol_id);
2634                 lp->stats.tx_errors++;
2635                 lp->stats.tx_aborted_errors++;
2636                 head->daddr=0;
2637                 /*memcpy(eth->h_source, dev->dev_addr, dev->addr_len);*/
2638                 return 0;
2639         }
2640 
2641         /*
2642          * Try and get ARP to resolve the header.
2643          */
2644 #ifdef CONFIG_INET       
2645         BUGMSG(D_DURING,"rebuild header from %d to %d; protocol %Xh\n",
2646                         head->saddr,head->daddr,head->protocol_id);
2647         status=arp_find(&(head->daddr), dst, dev, dev->pa_addr, skb)? 1 : 0;
2648         BUGMSG(D_DURING," rebuilt: from %d to %d; protocol %Xh\n",
2649                         head->saddr,head->daddr,head->protocol_id);
2650         return status;
2651 #else
2652         return 0;       
2653 #endif  
2654 }
2655 
2656 
2657 /* Determine a packet's protocol ID.
2658  *
2659  * With ARCnet we have to convert everything to Ethernet-style stuff.
2660  */
2661 unsigned short arcnetA_type_trans(struct sk_buff *skb,struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2662 {
2663         struct ClientData *head;
2664         struct arcnet_local *lp=(struct arcnet_local *) (dev->priv);
2665 
2666         /* Pull off the arcnet header. */
2667         skb->mac.raw=skb->data;
2668         skb_pull(skb,dev->hard_header_len);
2669         head=(struct ClientData *)skb->mac.raw;
2670         
2671         if (head->daddr==0)
2672                 skb->pkt_type=PACKET_BROADCAST;
2673         else if (dev->flags&IFF_PROMISC)
2674         {
2675                 /* if we're not sending to ourselves :) */
2676                 if (head->daddr != dev->dev_addr[0])
2677                         skb->pkt_type=PACKET_OTHERHOST;
2678         }
2679         
2680         /* now return the protocol number */
2681         switch (head->protocol_id)
2682         {
2683         case ARC_P_IP:          return htons(ETH_P_IP);
2684         case ARC_P_ARP:         return htons(ETH_P_ARP);
2685         case ARC_P_RARP:        return htons(ETH_P_RARP);
2686 
2687         case ARC_P_IPX:
2688         case ARC_P_NOVELL_EC:
2689                 return htons(ETH_P_802_3);
2690         default:
2691                 BUGMSG(D_NORMAL,"received packet of unknown protocol id %d (%Xh)\n",
2692                                 head->protocol_id,head->protocol_id);
2693                 lp->stats.rx_errors++;
2694                 lp->stats.rx_crc_errors++;
2695                 return 0;
2696         }
2697 
2698         return htons(ETH_P_IP);
2699 }
2700 
2701 
2702 #ifdef CONFIG_ARCNET_ETH
2703 /****************************************************************************
2704  *                                                                          *
2705  * Ethernet-Encap Support                                                   *
2706  *                                                                          *
2707  ****************************************************************************/
2708 
2709 /* Initialize the arc0e device.
2710  */
2711 static int arcnetE_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2712 {
2713         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
2714 
2715         ether_setup(dev); /* we're emulating ether here, not ARCnet */
2716         dev->dev_addr[0]=0;
2717         dev->dev_addr[5]=lp->stationid;
2718         dev->mtu=512-sizeof(struct HardHeader)-dev->hard_header_len-1;
2719         dev->open=NULL;
2720         dev->stop=NULL;
2721         dev->hard_start_xmit=arcnetE_send_packet;
2722 
2723         BUGMSG(D_EXTRA,"ARCnet Ethernet-Encap protocol initialized.\n");
2724                         
2725         return 0;
2726 }
2727 
2728 
2729 /* Called by the kernel in order to transmit an ethernet-type packet.
2730  */
2731 static int
2732 arcnetE_send_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2733 {
2734         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
2735         int ioaddr=dev->base_addr,bad;
2736         union ArcPacket *arcpacket = 
2737                 (union ArcPacket *)(dev->mem_start+512*(lp->txbuf^1));
2738         u_char *arcsoft,daddr;
2739         short offset,length=skb->len+1;
2740 
2741         lp->intx++;
2742         
2743         bad=arcnet_send_packet_bad(skb,dev);
2744         if (bad)
2745         {
2746                 lp->intx--;
2747                 return bad;
2748         }
2749 
2750         TBUSY=1;
2751                 
2752         if (length>XMTU)
2753         {
2754                 BUGMSG(D_NORMAL,"MTU must be <= 493 for ethernet encap (length=%d).\n",
2755                         length);
2756                 BUGMSG(D_NORMAL,"transmit aborted.\n");
2757 
2758                 dev_kfree_skb(skb,FREE_WRITE);
2759                 lp->intx--;
2760                 return 0;
2761         }
2762                 
2763         BUGMSG(D_DURING,"starting tx sequence...\n");
2764 
2765         lp->txbuf=lp->txbuf^1; /* XOR with 1 to alternate btw 2 & 3 */
2766 
2767 #ifndef SLOW_XMIT_COPY
2768         /* clean out the page to make debugging make more sense :) */
2769         BUGLVL(D_DURING)
2770                 memset((void *)dev->mem_start+lp->txbuf*512,0x42,512);
2771 #endif
2772 
2773         /* broadcasts have address FF:FF:FF:FF:FF:FF in etherspeak */
2774         if (((struct ethhdr*)(skb->data))->h_dest[0] == 0xFF)
2775                 daddr=arcpacket->hardheader.destination=0;
2776         else
2777                 daddr=arcpacket->hardheader.destination=
2778                         ((struct ethhdr*)(skb->data))->h_dest[5];
2779 
2780         /* load packet into shared memory */
2781         offset=512-length;
2782         if (length>MTU)         /* long/exception packet */
2783         {
2784                 if (length<MinTU) offset-=3;
2785                 arcpacket->hardheader.offset1=0;
2786                 arcpacket->hardheader.offset2=offset;
2787         }
2788         else                    /* short packet */
2789         {
2790                 arcpacket->hardheader.offset1=(offset-=256);
2791         }
2792         
2793         BUGMSG(D_DURING," length=%Xh, offset=%Xh, offset1=%Xh, offset2=%Xh\n",
2794                         length,offset,arcpacket->hardheader.offset1,
2795                         arcpacket->hardheader.offset2);
2796         
2797         arcsoft=&arcpacket->raw[offset];
2798         arcsoft[0]=ARC_P_ETHER;
2799         arcsoft++;
2800                 
2801         /* copy the packet into ARCnet shmem
2802          *  - the first bytes of ClientData header are skipped
2803          */
2804         BUGMSG(D_DURING,"ready to memcpy\n");
2805         
2806         memcpy(arcsoft,skb->data,skb->len);
2807                 
2808         BUGMSG(D_DURING,"transmitting packet to station %02Xh (%d bytes)\n",
2809                         daddr,length);
2810                                 
2811         BUGLVL(D_TX)
2812         {
2813                 int countx,county;
2814                         
2815                 printk("%6s: packet dump [tx] follows:",dev->name);
2816 
2817                 for (county=0; county<16+(length>=240)*16; county++)
2818                 {
2819                         printk("\n[%04X] ",county*16);
2820                         for (countx=0; countx<16; countx++)
2821                                 printk("%02X ",
2822                                         arcpacket->raw[county*16+countx]);
2823                 }
2824                 
2825                 printk("\n");
2826         }
2827 
2828         lp->lastload_dest=daddr;
2829         lp->txready=lp->txbuf;  /* packet is ready for sending */
2830 
2831         dev_kfree_skb(skb,FREE_WRITE);
2832 
2833         if (arcnet_go_tx(dev,1))
2834         {
2835                 /* inform upper layers */
2836                 TBUSY=0;
2837                 mark_bh(NET_BH);
2838         }
2839 
2840         dev->trans_start=jiffies;
2841         lp->intx--;
2842         
2843         /* make sure we didn't ignore a TX IRQ while we were in here */
2844         lp->intmask |= TXFREEflag;
2845         SETMASK;
2846 
2847         return 0;
2848 }
2849 
2850 
2851 /* Packet receiver for ethernet-encap packets.
2852  */
2853 static void
2854 arcnetE_rx(struct device *dev,u_char *arcsoft,
     /* [previous][next][first][last][top][bottom][index][help] */
2855         int length,u_char saddr, u_char daddr)
2856 {
2857         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
2858         struct sk_buff *skb;
2859         
2860         BUGMSG(D_DURING,"it's an ethernet-encap packet (length=%d)\n",
2861                         length);
2862 
2863         skb = alloc_skb(length, GFP_ATOMIC);
2864         if (skb == NULL) {
2865                 BUGMSG(D_NORMAL,"Memory squeeze, dropping packet.\n");
2866                 lp->stats.rx_dropped++;
2867                 return;
2868         }
2869         
2870         skb->len = length;
2871         skb->dev = dev;
2872         
2873         memcpy(skb->data,(u_char *)arcsoft+1,length-1);
2874 
2875         BUGLVL(D_SKB)
2876         {
2877                 short i;
2878                 printk("%6s: rx skb dump follows:\n",dev->name);
2879                 for(i=0; i<skb->len; i++)
2880                 {
2881                         if (i%16==0)
2882                                 printk("\n[%04hX] ",i);
2883                         else
2884                                 printk("%02hX ",((u_char *)skb->data)[i]);
2885                 }
2886                 printk("\n");
2887         }
2888         
2889         skb->protocol=eth_type_trans(skb,dev);
2890         
2891         netif_rx(skb);
2892 }
2893 
2894 #endif /* CONFIG_ARCNET_ETH */
2895 
2896 #ifdef CONFIG_ARCNET_1051
2897 /****************************************************************************
2898  *                                                                          *
2899  * RFC1051 Support                                                          *
2900  *                                                                          *
2901  ****************************************************************************/
2902 
2903 /* Initialize the arc0s device.
2904  */
2905 static int arcnetS_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2906 {
2907         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
2908 
2909         arcnet_setup(dev);
2910        
2911         /* And now fill particular fields with arcnet values */
2912         dev->dev_addr[0]=lp->stationid;
2913         dev->hard_header_len=sizeof(struct S_ClientData);
2914         dev->mtu=512-sizeof(struct HardHeader)-dev->hard_header_len
2915                 + S_EXTRA_CLIENTDATA;
2916         dev->open=NULL;
2917         dev->stop=NULL;
2918         dev->hard_start_xmit=arcnetS_send_packet;
2919         dev->hard_header=arcnetS_header;
2920         dev->rebuild_header=arcnetS_rebuild_header;
2921         BUGMSG(D_EXTRA,"ARCnet RFC1051 (NetBSD, AmiTCP) protocol initialized.\n");
2922 
2923         return 0;
2924 }
2925 
2926 
2927 /* Called by the kernel in order to transmit an RFC1051-type packet.
2928  */
2929 static int
2930 arcnetS_send_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2931 {
2932         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
2933         int ioaddr=dev->base_addr,bad,length;
2934         struct S_ClientData *hdr=(struct S_ClientData *)skb->data;
2935 
2936         lp->intx++;
2937         
2938         bad=arcnet_send_packet_bad(skb,dev);
2939         if (bad)
2940         {
2941                 lp->intx--;
2942                 return bad;
2943         }
2944         
2945         TBUSY=1;
2946 
2947         length = 1 < skb->len ? skb->len : 1;
2948 
2949         BUGLVL(D_SKB)
2950         {
2951                 short i;
2952                 for(i=0; i<skb->len; i++)
2953                 {
2954                         if (i%16 == 0) printk("\n[%04hX] ",i);
2955                         printk("%02hX ",((unsigned char*)skb->data)[i]);
2956                 }
2957                 printk("\n");
2958         }
2959 
2960         /* fits in one packet? */
2961         if (length-S_EXTRA_CLIENTDATA<=XMTU)
2962         {
2963                 arcnetAS_prepare_tx(dev,
2964                         skb->data+S_EXTRA_CLIENTDATA,
2965                         sizeof(struct S_ClientData)-S_EXTRA_CLIENTDATA,
2966                         skb->data+sizeof(struct S_ClientData),
2967                         length-sizeof(struct S_ClientData),
2968                         hdr->daddr,0);
2969 
2970                 /* done right away */
2971                 dev_kfree_skb(skb,FREE_WRITE);
2972                                 
2973                 if (arcnet_go_tx(dev,1))
2974                 {
2975                         /* inform upper layers */
2976                         TBUSY=0;
2977                         mark_bh(NET_BH);
2978                 }
2979         }
2980         else                    /* too big for one - not accepted */
2981         {
2982                 BUGMSG(D_NORMAL,"packet too long (length=%d)\n",
2983                         length);
2984                 dev_kfree_skb(skb,FREE_WRITE);
2985                 lp->stats.tx_dropped++;
2986                 TBUSY=0;
2987                 mark_bh(NET_BH);        
2988         }
2989 
2990         dev->trans_start=jiffies;
2991         lp->intx--;
2992 
2993         /* make sure we didn't ignore a TX IRQ while we were in here */
2994         lp->intmask |= TXFREEflag;
2995         SETMASK;
2996 
2997         return 0;
2998 }
2999 
3000 
3001 /* Packet receiver for RFC1051 packets; 
3002  */
3003 static void
3004 arcnetS_rx(struct device *dev,u_char *buf,
     /* [previous][next][first][last][top][bottom][index][help] */
3005         int length,u_char saddr, u_char daddr)
3006 {
3007         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
3008         struct sk_buff *skb;
3009         struct S_ClientData *arcsoft,*soft;
3010         
3011         arcsoft=(struct S_ClientData *)(buf-S_EXTRA_CLIENTDATA);
3012         length+=S_EXTRA_CLIENTDATA;
3013         
3014         BUGMSG(D_DURING,"it's an RFC1051 packet (length=%d)\n",
3015                         length);
3016                         
3017         
3018                         
3019         {    /* was "if not split" in A protocol, S is never split */   
3020 
3021                 skb = alloc_skb(length, GFP_ATOMIC);
3022                 if (skb == NULL) {
3023                         BUGMSG(D_NORMAL,"Memory squeeze, dropping packet.\n");
3024                         lp->stats.rx_dropped++;
3025                         return;
3026                 }
3027                 soft=(struct S_ClientData *)skb->data;
3028                 skb->len = length;
3029                 memcpy((u_char *)soft + sizeof(struct S_ClientData)
3030                                 - S_EXTRA_CLIENTDATA,
3031                         (u_char *)arcsoft + sizeof(struct S_ClientData)
3032                                 - S_EXTRA_CLIENTDATA, 
3033                         length - sizeof(struct S_ClientData)
3034                                 + S_EXTRA_CLIENTDATA);
3035                 soft->protocol_id=arcsoft->protocol_id;
3036                 soft->daddr=daddr;
3037                 soft->saddr=saddr;
3038                 skb->dev = dev;  /* is already lp->sdev */
3039                 
3040                 BUGLVL(D_SKB)
3041                 {
3042                         short i;
3043                         for(i=0; i<skb->len; i++)
3044                         {
3045                                 if( i%16 == 0 ) printk("\n[%04hX] ",i);
3046                                 printk("%02hX ",((unsigned char*)skb->data)[i]);
3047                         }
3048                         printk("\n");
3049                 }
3050 
3051                 skb->protocol=arcnetS_type_trans(skb,dev);
3052 
3053                 netif_rx(skb);
3054          }
3055 }
3056 
3057 
3058 /* Create the ARCnet ClientData header for an arbitrary protocol layer
3059  *
3060  * saddr=NULL   means use device source address (always will anyway)
3061  * daddr=NULL   means leave destination address (eg unresolved arp)
3062  */
3063 int arcnetS_header(struct sk_buff *skb,struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
3064                 unsigned short type,void *daddr,void *saddr,unsigned len)
3065 {
3066         struct S_ClientData *head = (struct S_ClientData *)
3067                 skb_push(skb,dev->hard_header_len);
3068         struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
3069 
3070         /* set the protocol ID according to RFC1051 */
3071         switch(type)
3072         {
3073         case ETH_P_IP:
3074                 head->protocol_id=ARC_P_IP_RFC1051;
3075                 BUGMSG(D_DURING,"S_header: IP_RFC1051 packet.\n");
3076                 break;
3077         case ETH_P_ARP:
3078                 head->protocol_id=ARC_P_ARP_RFC1051;
3079                 BUGMSG(D_DURING,"S_header: ARP_RFC1051 packet.\n");
3080                 break;
3081         default:
3082                 BUGMSG(D_NORMAL,"I don't understand protocol %d (%Xh)\n",
3083                         type,type);
3084                 lp->stats.tx_errors++;
3085                 lp->stats.tx_aborted_errors++;
3086                 return 0;
3087         }       
3088 
3089         /*
3090          * Set the source hardware address.
3091          *
3092          * This is pretty pointless for most purposes, but it can help
3093          * in debugging.  saddr is stored in the ClientData header and
3094          * removed before sending the packet (since ARCnet does not allow
3095          * us to change the source address in the actual packet sent)
3096          */
3097         if(saddr)
3098                 head->saddr=((u_char*)saddr)[0];
3099         else
3100                 head->saddr=((u_char*)(dev->dev_addr))[0];
3101 
3102         /* supposedly if daddr is NULL, we should ignore it... */
3103         if(daddr)
3104         {
3105                 head->daddr=((u_char*)daddr)[0];
3106                 return dev->hard_header_len;
3107         }
3108         else
3109                 head->daddr=0;  /* better fill one in anyway */
3110                 
3111         return -dev->hard_header_len;
3112 }
3113 
3114 
3115 /* Rebuild the ARCnet ClientData header. This is called after an ARP
3116  * (or in future other address resolution) has completed on this
3117  * sk_buff. We now let ARP fill in the other fields.
3118  */
3119 int arcnetS_rebuild_header(void *buff,struct device *dev,unsigned long dst,
     /* [previous][next][first][last][top][bottom][index][help] */
3120                 struct sk_buff *skb)
3121 {
3122         struct S_ClientData *head = (struct S_ClientData *)buff;
3123         struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
3124 
3125         /*
3126          * Only ARP and IP are currently supported
3127          */
3128          
3129         if(head->protocol_id != ARC_P_IP_RFC1051) 
3130         {
3131                 BUGMSG(D_NORMAL,"I don't understand protocol type %d (%Xh) addresses!\n",
3132                         head->protocol_id,head->protocol_id);
3133                 lp->stats.tx_errors++;
3134                 lp->stats.tx_aborted_errors++;
3135                 head->daddr=0;
3136                 /*memcpy(eth->h_source, dev->dev_addr, dev->addr_len);*/
3137                 return 0;
3138         }
3139 
3140         /*
3141          * Try and get ARP to resolve the header.
3142          */
3143 #ifdef CONFIG_INET       
3144         return arp_find(&(head->daddr), dst, dev, dev->pa_addr, skb)? 1 : 0;
3145 #else
3146         return 0;       
3147 #endif  
3148 }
3149 
3150                 
3151 /* Determine a packet's protocol ID.
3152  *
3153  * With ARCnet we have to convert everything to Ethernet-style stuff.
3154  */
3155 unsigned short arcnetS_type_trans(struct sk_buff *skb,struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
3156 {
3157         struct S_ClientData *head;
3158         struct arcnet_local *lp=(struct arcnet_local *) (dev->priv);
3159 
3160         /* Pull off the arcnet header. */
3161         skb->mac.raw=skb->data;
3162         skb_pull(skb,dev->hard_header_len);
3163         head=(struct S_ClientData *)skb->mac.raw;
3164         
3165         if (head->daddr==0)
3166                 skb->pkt_type=PACKET_BROADCAST;
3167         else if (dev->flags&IFF_PROMISC)
3168         {
3169                 /* if we're not sending to ourselves :) */
3170                 if (head->daddr != dev->dev_addr[0])
3171                         skb->pkt_type=PACKET_OTHERHOST;
3172         }
3173         
3174         /* now return the protocol number */
3175         switch (head->protocol_id)
3176         {
3177         case ARC_P_IP_RFC1051:  return htons(ETH_P_IP);
3178         case ARC_P_ARP_RFC1051: return htons(ETH_P_ARP);
3179         case ARC_P_ATALK:   return htons(ETH_P_ATALK); /* untested appletalk */
3180         default:
3181                 BUGMSG(D_NORMAL,"received packet of unknown protocol id %d (%Xh)\n",
3182                         head->protocol_id,head->protocol_id);
3183                 lp->stats.rx_errors++;
3184                 lp->stats.rx_crc_errors++;
3185                 return 0;
3186         }
3187 
3188         return htons(ETH_P_IP);
3189 }
3190 
3191 #endif  /* CONFIG_ARCNET_1051 */
3192 
3193 /****************************************************************************
3194  *                                                                          *
3195  * Kernel Loadable Module Support                                           *
3196  *                                                                          *
3197  ****************************************************************************/
3198 
3199 
3200 #ifdef MODULE
3201 static char devicename[9] = { 0, };
3202 static struct device thiscard = {
3203   devicename, /* device name is inserted by linux/drivers/net/net_init.c */
3204   0, 0, 0, 0,
3205   0, 0,  /* I/O address, IRQ */
3206   0, 0, 0, NULL, arcnet_probe
3207 };
3208         
3209         
3210 static int io=0x0;      /* <--- EDIT THESE LINES FOR YOUR CONFIGURATION */
3211 static int irqnum=0;    /* or use the insmod io= irqnum= shmem= options */
3212 static int shmem=0;
3213 static int num=0;       /* number of device (ie for 0 for arc0, 1 for arc1...) */
3214 
3215 int
3216 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3217 {
3218         sprintf(thiscard.name,"arc%d",num);
3219 
3220         thiscard.base_addr=io;
3221 
3222         thiscard.irq=irqnum;
3223         if (thiscard.irq==2) thiscard.irq=9;
3224 
3225         if (shmem)
3226         {
3227                 thiscard.mem_start=shmem;
3228                 thiscard.mem_end=thiscard.mem_start+512*4-1;
3229                 thiscard.rmem_start=thiscard.mem_start+512*0;
3230                 thiscard.rmem_end=thiscard.mem_start+512*2-1;
3231         }
3232 
3233         if (register_netdev(&thiscard) != 0)
3234                 return -EIO;
3235         return 0;
3236 }
3237 
3238 void
3239 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3240 {
3241         int ioaddr=thiscard.base_addr;
3242 
3243         if (thiscard.start) arcnet_close(&thiscard);
3244 
3245         /* Flush TX and disable RX */
3246         if (ioaddr)
3247         {
3248                 outb(0,INTMASK);        /* disable IRQ's */
3249                 outb(NOTXcmd,COMMAND);  /* stop transmit */
3250                 outb(NORXcmd,COMMAND);  /* disable receive */
3251         }
3252 
3253         if (thiscard.irq)
3254         {
3255                 free_irq(thiscard.irq);
3256                 /* very important! */
3257                 irq2dev_map[thiscard.irq] = NULL;
3258         }
3259         
3260         if (thiscard.base_addr) release_region(thiscard.base_addr,
3261                                                 ARCNET_TOTAL_SIZE);
3262         unregister_netdev(&thiscard);
3263         kfree(thiscard.priv);
3264         thiscard.priv = NULL;
3265 }
3266 
3267 #endif /* MODULE */
3268 
3269 
3270 
3271 /*
3272  * Local variables:
3273  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c arcnet.c"
3274  *  version-control: t
3275  *  kept-new-versions: 5
3276  *  tab-width: 8
3277  * End:
3278  */
3279 

/* [previous][next][first][last][top][bottom][index][help] */