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

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