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 /*
 972  * Setup a struct device for ARCnet.  This should really be in net_init.c
 973  * but since there are three different ARCnet devices ANYWAY... <gargle>
 974  *
 975  * Actually, the whole idea of having all this kernel-dependent stuff (ie.
 976  * "new-style flags") setup per-net-device is kind of weird anyway.
 977  *
 978  * Intelligent defaults?!  Nah.
 979  */
 980 
 981 void arcnet_setup(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 982 {
 983         int i;
 984         for (i=0; i<DEV_NUMBUFFS; i++)
 985                 skb_queue_head_init(&dev->buffs[i]);
 986 
 987         dev->broadcast[0]       = 0x00; /* broadcasts on ARCnet are address 0 */
 988         dev->addr_len           = 1;
 989         dev->type               = ARPHRD_ARCNET;
 990         dev->tx_queue_len       = 30;   /* Fairly long queue, arcnet is quite speedy */
 991 
 992         /* New-style flags. */
 993         dev->flags              = IFF_BROADCAST;
 994         dev->family             = AF_INET;
 995         dev->pa_addr            = 0;
 996         dev->pa_brdaddr         = 0;
 997         dev->pa_mask            = 0;
 998         dev->pa_alen            = 4;
 999 }
1000 
1001 
1002 /* Initialize the arc0e device.
1003  */
1004 static int arcnetE_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1005 {
1006         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1007 
1008         ether_setup(dev); /* we're emulating ether here, not ARCnet */
1009         dev->dev_addr[0]=0;
1010         dev->dev_addr[5]=lp->stationid;
1011         dev->mtu=512-sizeof(struct HardHeader)-dev->hard_header_len-1;
1012         dev->open=NULL;
1013         dev->stop=NULL;
1014         dev->hard_start_xmit=arcnetE_send_packet;
1015 
1016         BUGMSG(D_EXTRA,"ARCnet Ethernet-Encap protocol initialized.\n");
1017                         
1018         return 0;
1019 }
1020 
1021 /* Initialize the arc0s device.
1022  */
1023 static int arcnetS_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1024 {
1025         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1026 
1027         arcnet_setup(dev);
1028        
1029         /* And now fill particular fields with arcnet values */
1030         dev->dev_addr[0]=lp->stationid;
1031         dev->hard_header_len=sizeof(struct S_ClientData);
1032         dev->mtu=512-sizeof(struct HardHeader)-dev->hard_header_len
1033                 + S_EXTRA_CLIENTDATA;
1034         dev->open=NULL;
1035         dev->stop=NULL;
1036         dev->hard_start_xmit=arcnetS_send_packet;
1037         dev->hard_header=arcnetS_header;
1038         dev->rebuild_header=arcnetS_rebuild_header;
1039         BUGMSG(D_EXTRA,"ARCnet RFC1051 (NetBSD, AmiTCP) protocol initialized.\n");
1040 
1041         return 0;
1042 }
1043 
1044 
1045 /****************************************************************************
1046  *                                                                          *
1047  * Open and close the driver                                                *
1048  *                                                                          *
1049  ****************************************************************************/
1050  
1051 
1052 /* Open/initialize the board.  This is called sometime after booting when
1053  * the 'ifconfig' program is run.
1054  *
1055  * This routine should set everything up anew at each open, even
1056  * registers that "should" only need to be set once at boot, so that
1057  * there is non-reboot way to recover if something goes wrong.
1058  */
1059 static int
1060 arcnet_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1061 {
1062         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1063         int ioaddr=dev->base_addr;
1064         
1065         if (dev->metric>=1000)
1066         {
1067                 arcnet_debug=dev->metric-1000;
1068                 printk("%6s: debug level set to %d\n",dev->name,arcnet_debug);
1069                 dev->metric=1;
1070         }
1071 
1072         BUGLVL(D_EXTRA) printk(version);
1073 
1074         BUGMSG(D_EXTRA,"arcnet_open: resetting card.\n");
1075         
1076         /* try to reset - twice if it fails the first time */
1077         if (arcnet_reset(dev) && arcnet_reset(dev))
1078                 return -ENODEV;
1079         
1080         dev->tbusy=0;
1081         dev->interrupt=0;
1082         lp->intx=0;
1083         lp->in_txhandler=0;
1084         
1085         /* The RFC1201 driver is the default - just store */
1086         lp->adev=dev;
1087         BUGMSG(D_EXTRA,"ARCnet RFC1201 protocol initialized.\n");
1088         
1089         /* Initialize the ethernet-encap protocol driver */
1090         lp->edev=(struct device *)kmalloc(sizeof(struct device),GFP_KERNEL);
1091         if (lp->edev == NULL)
1092                 return -ENOMEM;
1093         memcpy(lp->edev,dev,sizeof(struct device));
1094         lp->edev->name=(char *)kmalloc(10,GFP_KERNEL);
1095         if (lp->edev->name == NULL) {
1096                 kfree(lp->edev);
1097                 lp->edev = NULL;
1098                 return -ENOMEM;
1099         }
1100         sprintf(lp->edev->name,"%se",dev->name);
1101         lp->edev->init=arcnetE_init;
1102         register_netdev(lp->edev);
1103 
1104         /* Initialize the RFC1051-encap protocol driver */
1105         lp->sdev=(struct device *)kmalloc(sizeof(struct device),GFP_KERNEL);
1106         memcpy(lp->sdev,dev,sizeof(struct device));
1107         lp->sdev->name=(char *)kmalloc(10,GFP_KERNEL);
1108         sprintf(lp->sdev->name,"%ss",dev->name);
1109         lp->sdev->init=arcnetS_init;
1110         register_netdev(lp->sdev);
1111 
1112         /* we're started */
1113         START=1;
1114         
1115         /* make sure we're ready to receive IRQ's.
1116          * arcnet_reset sets this for us, but if we receive one before
1117          * START is set to 1, it could be ignored.  So, we turn IRQ's
1118          * off, then on again to clean out the IRQ controller.
1119          */
1120         outb(0,INTMASK);
1121         udelay(1);      /* give it time to set the mask before
1122                          * we reset it again. (may not even be
1123                          * necessary)
1124                          */
1125         SETMASK;
1126         
1127         MOD_INC_USE_COUNT;
1128         return 0;
1129 }
1130 
1131 
1132 /* The inverse routine to arcnet_open - shuts down the card.
1133  */
1134 static int
1135 arcnet_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1136 {
1137         int ioaddr = dev->base_addr;
1138         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1139         
1140         TBUSY=1;
1141         START=0;
1142 
1143         /* Flush TX and disable RX */
1144         lp->intmask=0;
1145         SETMASK;        /* no IRQ's (except RESET, of course) */
1146         outb(NOTXcmd,COMMAND);  /* stop transmit */
1147         outb(NORXcmd,COMMAND);  /* disable receive */
1148 
1149         /* reset more flags */
1150         INTERRUPT=0;
1151         
1152         /* do NOT free lp->adev!!  It's static! */
1153         lp->adev=NULL;
1154         
1155         /* free the ethernet-encap protocol device */
1156         lp->edev->priv=NULL;
1157         dev_close(lp->edev);
1158         unregister_netdev(lp->edev);
1159         kfree(lp->edev->name);
1160         kfree(lp->edev);
1161         lp->edev=NULL;
1162 
1163         /* free the RFC1051-encap protocol device */
1164         lp->sdev->priv=NULL;
1165         dev_close(lp->sdev);
1166         unregister_netdev(lp->sdev);
1167         kfree(lp->sdev->name);
1168         kfree(lp->sdev);
1169         lp->sdev=NULL;
1170 
1171         /* Update the statistics here. (not necessary in ARCnet) */
1172 
1173         MOD_DEC_USE_COUNT;
1174         return 0;
1175 }
1176 
1177 
1178 
1179 /****************************************************************************
1180  *                                                                          *
1181  * Transmitter routines                                                     *
1182  *                                                                          *
1183  ****************************************************************************/
1184 
1185 /* Generic error checking routine for arcnet??_send_packet
1186  */
1187 static int
1188 arcnet_send_packet_bad(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1189 {
1190         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1191         int ioaddr=dev->base_addr;
1192         
1193         BUGMSG(D_DURING,"transmit requested (status=%Xh, inTX=%d)\n",
1194                         inb(STATUS),lp->intx);
1195                         
1196         if (lp->in_txhandler)
1197         {
1198                 BUGMSG(D_NORMAL,"send_packet called while in txhandler!\n");
1199                 lp->stats.tx_dropped++;
1200                 return 1;
1201         }
1202 
1203         if (lp->intx>1)
1204         {
1205                 BUGMSG(D_NORMAL,"send_packet called while intx!\n");
1206                 lp->stats.tx_dropped++;
1207                 return 1;
1208         }
1209 
1210         if (IF_TBUSY)
1211         {
1212                 /* If we get here, some higher level has decided we are broken.
1213                    There should really be a "kick me" function call instead. */
1214                 int tickssofar = jiffies - dev->trans_start;
1215                 /*int recbuf=lp->recbuf;*/
1216                 int status=inb(STATUS);
1217                 
1218                 if (tickssofar < TX_TIMEOUT) 
1219                 {
1220                         BUGMSG(D_DURING,"premature kickme! (status=%Xh ticks=%d o.skb=%ph numsegs=%d segnum=%d\n",
1221                                         status,tickssofar,lp->outgoing.skb,
1222                                         lp->outgoing.numsegs,
1223                                         lp->outgoing.segnum);
1224                         return 1;
1225                 }
1226 
1227                 lp->intmask &= ~TXFREEflag;
1228                 SETMASK;
1229                 
1230                 if (status&TXFREEflag)  /* transmit _DID_ finish */
1231                 {
1232                         BUGMSG(D_NORMAL,"tx timeout - missed IRQ? (status=%Xh, inTXh=%d, ticks=%d, mask=%Xh)\n",
1233                                         status,lp->in_txhandler,tickssofar,
1234                                         lp->intmask);
1235                         lp->stats.tx_errors++;
1236                 }
1237                 else
1238                 {
1239                         BUGMSG(D_NORMAL,"tx timed out (status=%Xh, inTXh=%d, tickssofar=%d, intmask=%Xh)\n",
1240                                         status,lp->in_txhandler,tickssofar,
1241                                         lp->intmask);
1242                         lp->stats.tx_errors++;
1243                         lp->stats.tx_aborted_errors++;
1244 
1245                         outb(NOTXcmd,COMMAND);
1246                 }
1247 
1248                 if (lp->outgoing.skb)
1249                 {
1250                         dev_kfree_skb(lp->outgoing.skb,FREE_WRITE);
1251                         lp->stats.tx_dropped++;
1252                 }
1253                 lp->outgoing.skb=NULL;
1254                 
1255                 TBUSY=0;
1256                 lp->txready=0;
1257                 lp->sending=0;
1258 
1259                 return 1;
1260         }
1261 
1262         /* If some higher layer thinks we've missed a tx-done interrupt
1263            we are passed NULL. Caution: dev_tint() handles the cli()/sti()
1264            itself. */
1265         if (skb == NULL) {
1266                 BUGMSG(D_NORMAL,"tx passed null skb (status=%Xh, inTX=%d, tickssofar=%ld)\n",
1267                         inb(STATUS),lp->intx,jiffies-dev->trans_start);
1268                 lp->stats.tx_errors++;
1269                 dev_tint(dev);
1270                 return 0;
1271         }
1272         
1273         if (lp->txready)        /* transmit already in progress! */
1274         {
1275                 BUGMSG(D_NORMAL,"trying to start new packet while busy! (status=%Xh)\n",
1276                         inb(STATUS));
1277                 lp->intmask &= ~TXFREEflag;
1278                 SETMASK;
1279                 outb(NOTXcmd,COMMAND); /* abort current send */
1280                 arcnet_inthandler(dev); /* fake an interrupt */
1281                 lp->stats.tx_errors++;
1282                 lp->stats.tx_fifo_errors++;
1283                 lp->txready=0;  /* we definitely need this line! */
1284                 
1285                 return 1;
1286         }
1287 
1288         /* Block a timer-based transmit from overlapping.  This could better be
1289            done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
1290         if (set_bit(0, (void*)&dev->tbusy) != 0)
1291         {
1292             BUGMSG(D_NORMAL,"transmitter called with busy bit set! (status=%Xh, inTX=%d, tickssofar=%ld)\n",
1293                         inb(STATUS),lp->intx,jiffies-dev->trans_start);
1294             lp->stats.tx_errors++;
1295             lp->stats.tx_fifo_errors++;
1296             return -EBUSY;
1297         }
1298 
1299         return 0;
1300 }
1301 
1302 
1303 /* Called by the kernel in order to transmit a packet.
1304  */
1305 static int
1306 arcnetA_send_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1307 {
1308         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1309         int ioaddr=dev->base_addr,bad;
1310         struct Outgoing *out=&(lp->outgoing);
1311 
1312         lp->intx++;
1313         
1314         bad=arcnet_send_packet_bad(skb,dev);
1315         if (bad)
1316         {
1317                 lp->intx--;
1318                 return bad;
1319         }
1320         
1321         TBUSY=1;
1322         
1323         out->length = 1 < skb->len ? skb->len : 1;
1324         out->hdr=(struct ClientData*)skb->data;
1325         out->skb=skb;
1326                 
1327         BUGLVL(D_SKB)
1328         {
1329                 short i;
1330                 for(i=0; i<skb->len; i++)
1331                 {
1332                         if(i%16 == 0) printk("\n[%04hX] ",i);
1333                         printk("%02hX ",((unsigned char*)skb->data)[i]);
1334                 }
1335                 printk("\n");
1336         }
1337 
1338         out->hdr->sequence=(lp->sequence++);
1339         
1340         /* fits in one packet? */
1341         if (out->length-EXTRA_CLIENTDATA<=XMTU)
1342         {
1343                 BUGMSG(D_DURING,"not splitting %d-byte packet. (split_flag=%d)\n",
1344                                 out->length,out->hdr->split_flag);
1345                 if (out->hdr->split_flag)
1346                         BUGMSG(D_NORMAL,"short packet has split_flag set?! (split_flag=%d)\n",
1347                                 out->hdr->split_flag);
1348                 out->numsegs=1;
1349                 out->segnum=1;
1350                 arcnetAS_prepare_tx(dev,
1351                         ((char *)out->hdr)+EXTRA_CLIENTDATA,
1352                         sizeof(struct ClientData)-EXTRA_CLIENTDATA,
1353                         ((char *)skb->data)+sizeof(struct ClientData),
1354                         out->length-sizeof(struct ClientData),
1355                         out->hdr->daddr,1);
1356 
1357                 /* done right away */
1358                 dev_kfree_skb(out->skb,FREE_WRITE);
1359                 out->skb=NULL;
1360                                         
1361                 if (arcnet_go_tx(dev,1))
1362                 {
1363                         /* inform upper layers */
1364                         TBUSY=0;
1365                         mark_bh(NET_BH);
1366                 }
1367         }
1368         else                    /* too big for one - split it */
1369         {
1370                 int maxsegsize=XMTU-4;
1371 
1372                 out->data=(u_char *)skb->data
1373                                 + sizeof(struct ClientData);
1374                 out->dataleft=out->length-sizeof(struct ClientData);
1375                 out->numsegs=(out->dataleft+maxsegsize-1)/maxsegsize;
1376                 out->segnum=0;
1377                 
1378                 BUGMSG(D_TX,"packet (%d bytes) split into %d fragments:\n",
1379                         out->length,out->numsegs);
1380 
1381                 /* if a packet waiting, launch it */
1382                 arcnet_go_tx(dev,1);
1383 
1384                 if (!lp->txready)
1385                 {
1386                         /* prepare a packet, launch it and prepare
1387                          * another.
1388                          */
1389                         arcnetA_continue_tx(dev);
1390                         if (arcnet_go_tx(dev,1))
1391                         {
1392                                 arcnetA_continue_tx(dev);
1393                                 arcnet_go_tx(dev,1);
1394                         }
1395                 }
1396                 
1397                 /* if segnum==numsegs, the transmission is finished;
1398                  * free the skb right away.
1399                  */
1400                 if (out->segnum==out->numsegs)
1401                 {
1402                         /* transmit completed */
1403                         out->segnum++;
1404                         if (out->skb)
1405                                 dev_kfree_skb(out->skb,FREE_WRITE);
1406                         out->skb=NULL;
1407                 }
1408         }
1409 
1410         dev->trans_start=jiffies;
1411         lp->intx--;
1412         
1413         /* make sure we didn't ignore a TX IRQ while we were in here */
1414         lp->intmask |= TXFREEflag;
1415         SETMASK;
1416 
1417         return 0;
1418 }
1419 
1420 
1421 /* Called by the kernel in order to transmit an ethernet-type packet.
1422  */
1423 static int
1424 arcnetE_send_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1425 {
1426         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1427         int ioaddr=dev->base_addr,bad;
1428         union ArcPacket *arcpacket = 
1429                 (union ArcPacket *)(dev->mem_start+512*(lp->txbuf^1));
1430         u_char *arcsoft,daddr;
1431         short offset,length=skb->len+1;
1432 
1433         lp->intx++;
1434         
1435         bad=arcnet_send_packet_bad(skb,dev);
1436         if (bad)
1437         {
1438                 lp->intx--;
1439                 return bad;
1440         }
1441 
1442         TBUSY=1;
1443                 
1444         if (length>XMTU)
1445         {
1446                 BUGMSG(D_NORMAL,"MTU must be <= 493 for ethernet encap (length=%d).\n",
1447                         length);
1448                 BUGMSG(D_NORMAL,"transmit aborted.\n");
1449 
1450                 dev_kfree_skb(skb,FREE_WRITE);
1451                 lp->intx--;
1452                 return 0;
1453         }
1454                 
1455         BUGMSG(D_DURING,"starting tx sequence...\n");
1456 
1457         lp->txbuf=lp->txbuf^1; /* XOR with 1 to alternate btw 2 & 3 */
1458 
1459         /* clean out the page to make debugging make more sense :) */
1460         BUGLVL(D_DURING)
1461                 memset((void *)dev->mem_start+lp->txbuf*512,0x42,512);
1462 
1463         /* broadcasts have address FF:FF:FF:FF:FF:FF in etherspeak */
1464         if (((struct ethhdr*)(skb->data))->h_dest[0] == 0xFF)
1465                 daddr=arcpacket->hardheader.destination=0;
1466         else
1467                 daddr=arcpacket->hardheader.destination=
1468                         ((struct ethhdr*)(skb->data))->h_dest[5];
1469 
1470         /* load packet into shared memory */
1471         offset=512-length;
1472         if (length>MTU)         /* long/exception packet */
1473         {
1474                 if (length<MinTU) offset-=3;
1475                 arcpacket->hardheader.offset1=0;
1476                 arcpacket->hardheader.offset2=offset;
1477         }
1478         else                    /* short packet */
1479         {
1480                 arcpacket->hardheader.offset1=(offset-=256);
1481         }
1482         
1483         BUGMSG(D_DURING," length=%Xh, offset=%Xh, offset1=%Xh, offset2=%Xh\n",
1484                         length,offset,arcpacket->hardheader.offset1,
1485                         arcpacket->hardheader.offset2);
1486         
1487         arcsoft=&arcpacket->raw[offset];
1488         arcsoft[0]=ARC_P_ETHER;
1489         arcsoft++;
1490                 
1491         /* copy the packet into ARCnet shmem
1492          *  - the first bytes of ClientData header are skipped
1493          */
1494         BUGMSG(D_DURING,"ready to memcpy\n");
1495         
1496         memcpy(arcsoft,skb->data,skb->len);
1497                 
1498         BUGMSG(D_DURING,"transmitting packet to station %02Xh (%d bytes)\n",
1499                         daddr,length);
1500                                 
1501         BUGLVL(D_TX)
1502         {
1503                 int countx,county;
1504                         
1505                 printk("%6s: packet dump [tx] follows:",dev->name);
1506 
1507                 for (county=0; county<16+(length>=240)*16; county++)
1508                 {
1509                         printk("\n[%04X] ",county*16);
1510                         for (countx=0; countx<16; countx++)
1511                                 printk("%02X ",
1512                                         arcpacket->raw[county*16+countx]);
1513                 }
1514                 
1515                 printk("\n");
1516         }
1517 
1518 #ifdef VERIFY_ACK
1519         lp->lastload_dest=daddr;
1520 #endif
1521         lp->txready=lp->txbuf;  /* packet is ready for sending */
1522 
1523         dev_kfree_skb(skb,FREE_WRITE);
1524 
1525         if (arcnet_go_tx(dev,1))
1526         {
1527                 /* inform upper layers */
1528                 TBUSY=0;
1529                 mark_bh(NET_BH);
1530         }
1531 
1532         dev->trans_start=jiffies;
1533         lp->intx--;
1534         
1535         /* make sure we didn't ignore a TX IRQ while we were in here */
1536         lp->intmask |= TXFREEflag;
1537         SETMASK;
1538 
1539         return 0;
1540 }
1541 
1542 
1543 /* Called by the kernel in order to transmit an RFC1051-type packet.
1544  */
1545 static int
1546 arcnetS_send_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1547 {
1548         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1549         int ioaddr=dev->base_addr,bad,length;
1550         struct S_ClientData *hdr=(struct S_ClientData *)skb->data;
1551 
1552         lp->intx++;
1553         
1554         bad=arcnet_send_packet_bad(skb,dev);
1555         if (bad)
1556         {
1557                 lp->intx--;
1558                 return bad;
1559         }
1560         
1561         TBUSY=1;
1562 
1563         length = 1 < skb->len ? skb->len : 1;
1564 
1565         BUGLVL(D_SKB)
1566         {
1567                 short i;
1568                 for(i=0; i<skb->len; i++)
1569                 {
1570                         if (i%16 == 0) printk("\n[%04hX] ",i);
1571                         printk("%02hX ",((unsigned char*)skb->data)[i]);
1572                 }
1573                 printk("\n");
1574         }
1575 
1576         /* fits in one packet? */
1577         if (length-S_EXTRA_CLIENTDATA<=XMTU)
1578         {
1579                 arcnetAS_prepare_tx(dev,
1580                         skb->data+S_EXTRA_CLIENTDATA,
1581                         sizeof(struct S_ClientData)-S_EXTRA_CLIENTDATA,
1582                         skb->data+sizeof(struct S_ClientData),
1583                         length-sizeof(struct S_ClientData),
1584                         hdr->daddr,0);
1585 
1586                 /* done right away */
1587                 dev_kfree_skb(skb,FREE_WRITE);
1588                                 
1589                 if (arcnet_go_tx(dev,1))
1590                 {
1591                         /* inform upper layers */
1592                         TBUSY=0;
1593                         mark_bh(NET_BH);
1594                 }
1595         }
1596         else                    /* too big for one - not accepted */
1597         {
1598                 BUGMSG(D_NORMAL,"packet too long (length=%d)\n",
1599                         length);
1600                 dev_kfree_skb(skb,FREE_WRITE);
1601                 lp->stats.tx_dropped++;
1602                 TBUSY=0;
1603                 mark_bh(NET_BH);        
1604         }
1605 
1606         dev->trans_start=jiffies;
1607         lp->intx--;
1608 
1609         /* make sure we didn't ignore a TX IRQ while we were in here */
1610         lp->intmask |= TXFREEflag;
1611         SETMASK;
1612 
1613         return 0;
1614 }
1615 
1616 
1617 /* After an RFC1201 split packet has been set up, this function calls
1618  * arcnetAS_prepare_tx to load the next segment into the card.  This function
1619  * does NOT automatically call arcnet_go_tx.
1620  */
1621 static void arcnetA_continue_tx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1622 {
1623         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1624         int ioaddr=dev->base_addr,maxsegsize=XMTU-4;
1625         struct Outgoing *out=&(lp->outgoing);
1626         
1627         BUGMSG(D_DURING,"continue_tx called (status=%Xh, intx=%d, intxh=%d, intmask=%Xh\n",
1628                 inb(STATUS),lp->intx,lp->in_txhandler,lp->intmask);
1629         
1630         if (lp->txready)
1631         {
1632                 BUGMSG(D_NORMAL,"continue_tx: called with packet in buffer!\n");
1633                 return;
1634         }
1635         
1636         if (out->segnum>=out->numsegs)
1637         {
1638                 BUGMSG(D_NORMAL,"continue_tx: building segment %d of %d!\n",
1639                         out->segnum+1,out->numsegs);
1640         }
1641 
1642         if (!out->segnum)       /* first packet */
1643                 out->hdr->split_flag=((out->numsegs-2)<<1)+1;
1644         else
1645                 out->hdr->split_flag=out->segnum<<1;
1646 
1647         out->seglen=maxsegsize;
1648         if (out->seglen>out->dataleft) out->seglen=out->dataleft;
1649                         
1650         BUGMSG(D_TX,"building packet #%d (%d bytes) of %d (%d total), splitflag=%d\n",
1651                 out->segnum+1,out->seglen,out->numsegs,
1652                 out->length,out->hdr->split_flag);
1653 
1654         arcnetAS_prepare_tx(dev,((char *)out->hdr)+EXTRA_CLIENTDATA,
1655                 sizeof(struct ClientData)-EXTRA_CLIENTDATA,
1656                 out->data,out->seglen,out->hdr->daddr,1);
1657                 
1658         out->dataleft-=out->seglen;
1659         out->data+=out->seglen;
1660         out->segnum++;
1661 }
1662 
1663 
1664 /* Given an skb, copy a packet into the ARCnet buffers for later transmission
1665  * by arcnet_go_tx.
1666  */
1667 static void
1668 arcnetAS_prepare_tx(struct device *dev,u_char *hdr,int hdrlen,
     /* [previous][next][first][last][top][bottom][index][help] */
1669                 char *data,int length,int daddr,int exceptA)
1670 {
1671         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1672         struct ClientData *arcsoft;
1673         union ArcPacket *arcpacket = 
1674                 (union ArcPacket *)(dev->mem_start+512*(lp->txbuf^1));
1675         int offset;
1676         
1677         lp->txbuf=lp->txbuf^1;  /* XOR with 1 to alternate between 2 and 3 */
1678         
1679         length+=hdrlen;
1680 
1681         BUGMSG(D_TX,"arcnetAS_prep_tx: hdr:%ph, length:%d, data:%ph\n",
1682                         hdr,length,data);
1683 
1684         /* clean out the page to make debugging make more sense :) */
1685         BUGLVL(D_DURING)
1686                 memset((void *)dev->mem_start+lp->txbuf*512,0x42,512);
1687 
1688         arcpacket->hardheader.destination=daddr;
1689 
1690         /* load packet into shared memory */
1691         if (length<=MTU)        /* Normal (256-byte) Packet */
1692         {
1693                 arcpacket->hardheader.offset1=offset=256-length;
1694                 arcsoft=(struct ClientData *)
1695                         (&arcpacket->raw[offset]);
1696         }
1697         else if (length>=MinTU) /* Extended (512-byte) Packet */
1698         {
1699                 arcpacket->hardheader.offset1=0;
1700                 arcpacket->hardheader.offset2=offset=512-length;
1701                 
1702                 arcsoft=(struct ClientData *)
1703                         (&arcpacket->raw[offset]);
1704         }
1705         else if (exceptA)               /* RFC1201 Exception Packet */
1706         {
1707                 arcpacket->hardheader.offset1=0;
1708                 arcpacket->hardheader.offset2=offset=512-length-4;
1709                 arcsoft=(struct ClientData *)
1710                         (&arcpacket->raw[offset+4]);
1711                 
1712                 /* exception-specific stuff - these four bytes
1713                  * make the packet long enough to fit in a 512-byte
1714                  * frame.
1715                  */
1716                 arcpacket->raw[offset+0]=hdr[0];
1717                 arcpacket->raw[offset+1]=0xFF; /* FF flag */
1718                         arcpacket->raw[offset+2]=0xFF; /* FF padding */
1719                         arcpacket->raw[offset+3]=0xFF; /* FF padding */
1720         }
1721         else                            /* "other" Exception packet */
1722         {
1723                 /* RFC1051 - set 4 trailing bytes to 0 */
1724                 memset(&arcpacket->raw[508],0,4);
1725 
1726                 /* now round up to MinTU */
1727                 arcpacket->hardheader.offset1=0;
1728                 arcpacket->hardheader.offset2=offset=512-MinTU;
1729                 arcsoft=(struct ClientData *)(&arcpacket->raw[offset]);
1730         }
1731 
1732 
1733         /* copy the packet into ARCnet shmem
1734          *  - the first bytes of ClientData header are skipped
1735          */
1736         memcpy((u_char*)arcsoft,
1737                 (u_char*)hdr,hdrlen);
1738         memcpy((u_char*)arcsoft+hdrlen,
1739                 data,length-hdrlen);
1740                 
1741         BUGMSG(D_DURING,"transmitting packet to station %02Xh (%d bytes)\n",
1742                         daddr,length);
1743                         
1744         BUGLVL(D_TX)
1745         {
1746                 int countx,county;
1747                 
1748                 printk("%6s: packet dump [tx] follows:",dev->name);
1749 
1750                 for (county=0; county<16+(length>MTU)*16; county++)
1751                 {
1752                         printk("\n[%04X] ",county*16);
1753                         for (countx=0; countx<16; countx++)
1754                                 printk("%02X ",
1755                                         arcpacket->raw[county*16+countx]);
1756                 }
1757                 
1758                 printk("\n");
1759         }
1760 
1761 #ifdef VERIFY_ACK
1762         lp->lastload_dest=daddr;
1763 #endif
1764         lp->txready=lp->txbuf;  /* packet is ready for sending */
1765 }
1766 
1767 /* Actually start transmitting a packet that was placed in the card's
1768  * buffer by arcnetAS_prepare_tx.  Returns 1 if a Tx is really started.
1769  *
1770  * This should probably always be called with the INTMASK register set to 0,
1771  * so go_tx is not called recursively.
1772  *
1773  * The enable_irq flag determines whether to actually write INTMASK value
1774  * to the card; TXFREEflag is always OR'ed into the memory variable either
1775  * way.
1776  */
1777 static int
1778 arcnet_go_tx(struct device *dev,int enable_irq)
     /* [previous][next][first][last][top][bottom][index][help] */
1779 {
1780         struct arcnet_local *lp=(struct arcnet_local *)dev->priv;
1781         int ioaddr=dev->base_addr;
1782 
1783         BUGMSG(D_DURING,"go_tx: status=%Xh, intmask=%Xh, txready=%d, sending=%d\n",
1784                 inb(STATUS),lp->intmask,lp->txready,lp->sending);
1785 
1786         if (lp->sending || !lp->txready)
1787         {
1788                 if (enable_irq && lp->sending)
1789                 {
1790                         lp->intmask |= TXFREEflag;
1791                         SETMASK;
1792                 }
1793                 return 0;
1794         }
1795 
1796         /* start sending */
1797         outb(TXcmd|(lp->txready<<3),COMMAND);
1798 
1799         lp->stats.tx_packets++;
1800         lp->txready=0;
1801         lp->sending++;
1802 
1803 #ifdef VERIFY_ACK       
1804         lp->lasttrans_dest=lp->lastload_dest;
1805         lp->lastload_dest=0;
1806 #endif
1807 
1808         lp->intmask |= TXFREEflag;
1809 
1810         if (enable_irq) SETMASK;
1811 
1812         return 1;
1813 }
1814 
1815 
1816 /****************************************************************************
1817  *                                                                          *
1818  * Interrupt handler                                                        *
1819  *                                                                          *
1820  ****************************************************************************/
1821 
1822 
1823 /* The typical workload of the driver:  Handle the network interface
1824  * interrupts.  This doesn't do much right now except call arcnet_inthandler,
1825  * which takes different parameters but may be called from other places
1826  * as well.
1827  */
1828 static void
1829 arcnet_interrupt(int irq,struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
1830 {
1831         struct device *dev = (struct device *)(irq2dev_map[irq]);
1832         
1833         BUGMSG(D_DURING,"in arcnet_interrupt\n");
1834 
1835         if (dev==NULL)
1836         {
1837                 BUGLVL(D_EXTRA)
1838                         printk("arcnet: irq %d for unknown device.\n", irq);
1839                 return;
1840         }
1841         
1842         if (!dev->start) return;
1843 
1844         arcnet_inthandler(dev);
1845 }
1846 
1847 
1848 /* The actual interrupt handler routine - handle various IRQ's generated
1849  * by the card.
1850  */
1851 static void
1852 arcnet_inthandler(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1853 {       
1854         struct arcnet_local *lp=(struct arcnet_local *)dev->priv;
1855         int ioaddr=dev->base_addr, status, boguscount = 3, didsomething;
1856 
1857         if (IF_INTERRUPT)
1858         {
1859                 BUGMSG(D_NORMAL,"DRIVER PROBLEM!  Nested arcnet interrupts!\n");
1860                 return; /* don't even try. */
1861         }
1862         
1863         outb(0,INTMASK);
1864         INTERRUPT = 1;
1865 
1866         BUGMSG(D_DURING,"in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
1867                 inb(STATUS),lp->intmask);
1868 
1869         do
1870         {
1871                 status = inb(STATUS);
1872                 didsomething=0;
1873         
1874                 /* RESET flag was enabled - card is resetting and if RX
1875                  * is disabled, it's NOT because we just got a packet.
1876                  */
1877                 if (status & RESETflag)
1878                 {
1879                         outb(CFLAGScmd|RESETclear,COMMAND);
1880                         BUGMSG(D_INIT,"reset irq (status=%Xh)\n",
1881                                         status);
1882                 }
1883 #ifdef DETECT_RECONFIGS
1884                 if (status & (lp->intmask) & RECONflag)
1885                 {
1886                         outb(CFLAGScmd|CONFIGclear,COMMAND);
1887                         lp->stats.tx_carrier_errors++;
1888                         
1889                         #ifdef SHOW_RECONFIGS
1890                         BUGMSG(D_NORMAL,"Network reconfiguration detected (status=%Xh)\n",
1891                                         status);
1892                         #endif /* SHOW_RECONFIGS */
1893                         
1894                         #ifdef RECON_THRESHOLD
1895                         /* is the RECON info empty or old? */
1896                         if (!lp->first_recon || !lp->last_recon || 
1897                                 jiffies-lp->last_recon > HZ*10)
1898                         {
1899                                 if (lp->network_down)
1900                                         BUGMSG(D_NORMAL,"reconfiguration detected: cabling restored?\n");
1901                                 lp->first_recon=lp->last_recon=jiffies;
1902                                 lp->num_recons=lp->network_down=0;
1903                                 
1904                                 BUGMSG(D_DURING,"recon: clearing counters.\n");
1905                         }
1906                         else /* add to current RECON counter */
1907                         {
1908                                 lp->last_recon=jiffies;
1909                                 lp->num_recons++;
1910                                 
1911                                 BUGMSG(D_DURING,"recon: counter=%d, time=%lds, net=%d\n",
1912                                         lp->num_recons,
1913                                         (lp->last_recon-lp->first_recon)/HZ,
1914                                         lp->network_down);
1915 
1916                                 /* if network is marked up;
1917                                  * and first_recon and last_recon are 60+ sec
1918                                  *   apart;
1919                                  * and the average no. of recons counted is
1920                                  *   > RECON_THRESHOLD/min;
1921                                  * then print a warning message.
1922                                  */
1923                                 if (!lp->network_down
1924                                     && (lp->last_recon-lp->first_recon)<=HZ*60
1925                                     && lp->num_recons >= RECON_THRESHOLD)
1926                                 {
1927                                         lp->network_down=1;
1928                                         BUGMSG(D_NORMAL,"many reconfigurations detected: cabling problem?\n");
1929                                 }
1930                                 else if (!lp->network_down
1931                                     && lp->last_recon-lp->first_recon > HZ*60)
1932                                 {
1933                                         /* reset counters if we've gone for
1934                                          * over a minute.
1935                                          */
1936                                         lp->first_recon=lp->last_recon;
1937                                         lp->num_recons=1;
1938                                 }
1939                         }
1940                         #endif
1941                 }
1942                 #ifdef RECON_THRESHOLD
1943                 else if (lp->network_down && jiffies-lp->last_recon > HZ*10)
1944                 {
1945                         if (lp->network_down)
1946                                 BUGMSG(D_NORMAL,"cabling restored?\n");
1947                         lp->first_recon=lp->last_recon=0;
1948                         lp->num_recons=lp->network_down=0;
1949                         
1950                         BUGMSG(D_DURING,"not recon: clearing counters anyway.\n");
1951                 }
1952                 #endif
1953 #endif /* DETECT_RECONFIGS */
1954 
1955                 /* RX is inhibited - we must have received something. */
1956                 if (status & lp->intmask & NORXflag)
1957                 {
1958                         int recbuf=lp->recbuf=!lp->recbuf;
1959 
1960                         BUGMSG(D_DURING,"receive irq (status=%Xh)\n",
1961                                         status);
1962 
1963                         /* enable receive of our next packet */
1964                         EnableReceiver();
1965 
1966                         /* Got a packet. */
1967                         arcnet_rx(dev,!recbuf);
1968                         didsomething++;
1969                 }
1970                 
1971                 /* it can only be an xmit-done irq if we're xmitting :) */
1972                 /*if (status&TXFREEflag && !lp->in_txhandler && lp->sending)*/
1973                 if (status & lp->intmask & TXFREEflag)
1974                 {
1975                         struct Outgoing *out=&(lp->outgoing);
1976                         int was_sending=lp->sending;
1977                         
1978                         lp->intmask &= ~TXFREEflag;
1979 
1980                         lp->in_txhandler++;
1981                         if (was_sending) lp->sending--;
1982 
1983                         BUGMSG(D_DURING,"TX IRQ (stat=%Xh, numsegs=%d, segnum=%d, skb=%ph)\n",
1984                                         status,out->numsegs,out->segnum,out->skb);
1985                                         
1986 #ifdef VERIFY_ACK
1987                         if (was_sending && !(status&TXACKflag))
1988                         {
1989                                 if (lp->lasttrans_dest != 0)
1990                                 {
1991                                         BUGMSG(D_NORMAL,"transmit was not acknowledged! (status=%Xh, dest=%d)\n",
1992                                                 status,lp->lasttrans_dest);
1993                                         lp->stats.tx_errors++;
1994                                         lp->stats.tx_carrier_errors++;
1995                                 }
1996                                 else
1997                                 {
1998                                         BUGMSG(D_DURING,"broadcast was not acknowledged; that's normal (status=%Xh, dest=%d)\n",
1999                                                         status,
2000                                                         lp->lasttrans_dest);
2001                                 }
2002                         }
2003 #endif
2004                         /* send packet if there is one */
2005                         arcnet_go_tx(dev,0);
2006                         didsomething++;
2007                         
2008                         if (lp->intx)
2009                         {
2010                                 BUGMSG(D_DURING,"TXDONE while intx! (status=%Xh, intx=%d)\n",
2011                                         inb(STATUS),lp->intx);
2012                                 lp->in_txhandler--;
2013                                 continue;
2014                         }
2015 
2016                         if (!lp->outgoing.skb)
2017                         {
2018                                 BUGMSG(D_DURING,"TX IRQ done: no split to continue.\n");
2019                                 
2020                                 /* inform upper layers */
2021                                 if (!lp->txready && IF_TBUSY)
2022                                 {
2023                                         TBUSY=0;
2024                                         mark_bh(NET_BH);
2025                                 }
2026                                 lp->in_txhandler--;
2027                                 continue;
2028                         }
2029                         
2030                         /* if more than one segment, and not all segments
2031                          * are done, then continue xmit.
2032                          */
2033                         if (out->segnum<out->numsegs)
2034                                 arcnetA_continue_tx(dev);
2035                         arcnet_go_tx(dev,0);
2036 
2037                         /* if segnum==numsegs, the transmission is finished;
2038                          * free the skb.
2039                          */
2040                         if (out->segnum>=out->numsegs)
2041                         {
2042                                 /* transmit completed */
2043                                 out->segnum++;
2044                                 if (out->skb)
2045                                         dev_kfree_skb(out->skb,FREE_WRITE);
2046                                 out->skb=NULL;
2047 
2048                                 /* inform upper layers */
2049                                 if (!lp->txready && IF_TBUSY)
2050                                 {
2051                                         TBUSY=0;
2052                                         mark_bh(NET_BH);
2053                                 }
2054                         }
2055                         didsomething++;
2056 
2057                         lp->in_txhandler--;
2058                 }
2059                 else if (lp->txready && !lp->sending && !lp->intx)
2060                 {
2061                         BUGMSG(D_NORMAL,"recovery from silent TX (status=%Xh)\n",
2062                                                 status);
2063                         arcnet_go_tx(dev,0);
2064                         didsomething++;
2065                 }
2066         } while (--boguscount && didsomething);
2067 
2068         BUGMSG(D_DURING,"net_interrupt complete (status=%Xh, count=%d)\n\n",
2069                         inb(STATUS),boguscount);
2070 
2071         SETMASK;        /* put back interrupt mask */
2072 
2073         INTERRUPT=0;
2074 }
2075 
2076 
2077 /****************************************************************************
2078  *                                                                          *
2079  * Receiver routines                                                        *
2080  *                                                                          *
2081  ****************************************************************************/
2082 
2083 
2084 /* A packet has arrived; grab it from the buffers and possibly unsplit it.
2085  * This is a generic packet receiver that calls arcnet??_rx depending on the
2086  * protocol ID found.
2087  */
2088 static void
2089 arcnet_rx(struct device *dev,int recbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
2090 {
2091         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
2092         int ioaddr = dev->base_addr;
2093         union ArcPacket *arcpacket=
2094                 (union ArcPacket *)(dev->mem_start+recbuf*512);
2095         u_char *arcsoft;
2096         short length,offset;
2097         u_char daddr,saddr;
2098         
2099         lp->stats.rx_packets++;
2100 
2101         saddr=arcpacket->hardheader.source;
2102         daddr=arcpacket->hardheader.destination;
2103         
2104         /* if source is 0, it's a "used" packet! */
2105         if (saddr==0)
2106         {
2107                 BUGMSG(D_NORMAL,"discarding old packet. (status=%Xh)\n",
2108                         inb(STATUS));
2109                 lp->stats.rx_errors++;
2110                 return;
2111         }
2112         arcpacket->hardheader.source=0;
2113         
2114         if (arcpacket->hardheader.offset1) /* Normal Packet */
2115         {
2116                 offset=arcpacket->hardheader.offset1;
2117                 arcsoft=&arcpacket->raw[offset];
2118                 length=256-offset;
2119         }
2120         else            /* ExtendedPacket or ExceptionPacket */
2121         {
2122                 offset=arcpacket->hardheader.offset2;
2123                 arcsoft=&arcpacket->raw[offset];
2124 
2125                 length=512-offset;
2126         }
2127         
2128         BUGMSG(D_DURING,"received packet from %02Xh to %02Xh (%d bytes)\n",
2129                         saddr,daddr,length);
2130 
2131         /* call the right receiver for the protocol */
2132         switch (arcsoft[0])
2133         {
2134         case ARC_P_IP:
2135         case ARC_P_ARP:
2136         case ARC_P_RARP:
2137         case ARC_P_IPX:
2138         case ARC_P_NOVELL_EC:
2139                 arcnetA_rx(lp->adev,arcsoft,length,saddr,daddr);
2140                 break;
2141         case ARC_P_ETHER:
2142                 arcnetE_rx(lp->edev,arcsoft,length,saddr,daddr);
2143                 break;          
2144         case ARC_P_IP_RFC1051:
2145         case ARC_P_ARP_RFC1051:
2146                 arcnetS_rx(lp->sdev,arcsoft,length,saddr,daddr);
2147                 break;
2148         case ARC_P_LANSOFT: /* don't understand.  fall through. */
2149         default:
2150                 BUGMSG(D_NORMAL,"received unknown protocol %d (%Xh) from station %d.\n",
2151                         arcsoft[0],arcsoft[0],saddr);
2152                 lp->stats.rx_errors++;
2153                 lp->stats.rx_crc_errors++;
2154                 break;
2155         }
2156 
2157         BUGLVL(D_RX)
2158         {
2159                 int countx,county;
2160                 
2161                 printk("%6s: packet dump [rx] follows:",dev->name);
2162 
2163                 for (county=0; county<16+(length>240)*16; county++)
2164                 {
2165                         printk("\n[%04X] ",county*16);
2166                         for (countx=0; countx<16; countx++)
2167                                 printk("%02X ",
2168                                         arcpacket->raw[county*16+countx]);
2169                 }
2170                 
2171                 printk("\n");
2172         }
2173 
2174 
2175         /* clean out the page to make debugging make more sense :) */
2176         BUGLVL(D_DURING)
2177                 memset((void *)arcpacket->raw,0x42,512);
2178 
2179 
2180         /* If any worth-while packets have been received, a mark_bh(NET_BH)
2181          * has been done by netif_rx and Linux will handle them after we
2182          * return.
2183          */
2184 }
2185 
2186 
2187 /* Packet receiver for "standard" RFC1201-style packets
2188  */
2189 static void
2190 arcnetA_rx(struct device *dev,u_char *buf,
     /* [previous][next][first][last][top][bottom][index][help] */
2191         int length,u_char saddr, u_char daddr)
2192 {
2193         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
2194         struct sk_buff *skb;
2195         struct ClientData *arcsoft,*soft;
2196         
2197         BUGMSG(D_DURING,"it's an RFC1201 packet (length=%d)\n",
2198                         length);
2199                         
2200         /* compensate for EXTRA_CLIENTDATA (which isn't actually in the
2201          * packet)
2202          */
2203         arcsoft=(struct ClientData *)(buf-EXTRA_CLIENTDATA);
2204         length+=EXTRA_CLIENTDATA;
2205 
2206         if (arcsoft->split_flag==0xFF)  /* Exception Packet */
2207         {
2208                 BUGMSG(D_DURING,"compensating for exception packet\n");
2209 
2210                 /* skip over 4-byte junkola */
2211                 arcsoft=(struct ClientData *)
2212                         ((u_char *)arcsoft + 4);
2213                 length-=4;
2214         }
2215         
2216         if (!arcsoft->split_flag)               /* not split */
2217         {
2218                 struct Incoming *in=&lp->incoming[saddr];
2219 
2220                 BUGMSG(D_RX,"incoming is not split (splitflag=%d)\n",
2221                         arcsoft->split_flag);
2222                         
2223                 if (in->skb)    /* already assembling one! */
2224                 {
2225                         BUGMSG(D_NORMAL,"aborting assembly (seq=%d) for unsplit packet (splitflag=%d, seq=%d)\n",
2226                                 in->sequence,arcsoft->split_flag,
2227                                 arcsoft->sequence);
2228                         kfree_skb(in->skb,FREE_WRITE);
2229                         lp->stats.rx_errors++;
2230                         lp->stats.rx_missed_errors++;
2231                         in->skb=NULL;
2232                 }
2233                 
2234                 in->sequence=arcsoft->sequence;
2235 
2236                 skb = alloc_skb(length, GFP_ATOMIC);
2237                 if (skb == NULL) {
2238                         BUGMSG(D_NORMAL,"Memory squeeze, dropping packet.\n");
2239                         lp->stats.rx_dropped++;
2240                         return;
2241                 }
2242                 soft=(struct ClientData *)skb->data;
2243                 
2244                 skb->len = length;
2245                 skb->dev = dev;
2246                 
2247                 memcpy((u_char *)soft+EXTRA_CLIENTDATA,
2248                         (u_char *)arcsoft+EXTRA_CLIENTDATA,
2249                         length-EXTRA_CLIENTDATA);
2250                 soft->daddr=daddr;
2251                 soft->saddr=saddr;
2252                 
2253                 /* ARP packets have problems when sent from DOS.
2254                  * source address is always 0 on some systems!  So we take
2255                  * the hardware source addr (which is impossible to fumble)
2256                  * and insert it ourselves.
2257                  */
2258                 if (soft->protocol_id == ARC_P_ARP)
2259                 {
2260                         struct arphdr *arp=(struct arphdr *)
2261                                         ((char *)soft+sizeof(struct ClientData));
2262 
2263                         /* make sure addresses are the right length */
2264                         if (arp->ar_hln==1 && arp->ar_pln==4)
2265                         {
2266                                 char *cptr=(char *)(arp)+sizeof(struct arphdr);
2267                                 
2268                                 if (!*cptr)     /* is saddr = 00? */
2269                                 {
2270                                         BUGMSG(D_EXTRA,"ARP source address was 00h, set to %02Xh.\n",
2271                                                         saddr);
2272                                         lp->stats.rx_crc_errors++;
2273                                         *cptr=saddr;
2274                                 }
2275                                 else
2276                                 {
2277                                         BUGMSG(D_DURING,"ARP source address (%Xh) is fine.\n",
2278                                                 *cptr);
2279                                 }
2280                         }
2281                         else
2282                         {
2283                                 BUGMSG(D_NORMAL,"funny-shaped ARP packet. (%Xh, %Xh)\n",
2284                                         arp->ar_hln,arp->ar_pln);
2285                                 lp->stats.rx_errors++;
2286                                 lp->stats.rx_crc_errors++;
2287                         }
2288                 }
2289                 
2290                 BUGLVL(D_SKB)
2291                 {
2292                     short i;
2293                         for(i=0; i< skb->len; i++)
2294                         {
2295                                 if( i%16 == 0 ) printk("\n[%04hX] ",i);
2296                                 printk("%02hX ",((unsigned char*)skb->data)[i]);
2297                         }
2298                         printk("\n");
2299                 }
2300 
2301                 skb->protocol=arcnetA_type_trans(skb,dev);
2302 
2303                 netif_rx(skb);
2304          }
2305          else                   /* split packet */
2306          {
2307                  /* NOTE:  MSDOS ARP packet correction should only need to
2308                   * apply to unsplit packets, since ARP packets are so short.
2309                   *
2310                   * My interpretation of the RFC1201 (ARCnet) document is that
2311                   * if a packet is received out of order, the entire assembly
2312                   * process should be aborted.
2313                   *
2314                   * The RFC also mentions "it is possible for successfully
2315                   * received packets to be retransmitted."  As of 0.40 all
2316                   * previously received packets are allowed, not just the
2317                   * most recent one.
2318                   *
2319                   * We allow multiple assembly processes, one for each
2320                   * ARCnet card possible on the network.  Seems rather like
2321                   * a waste of memory.  Necessary?
2322                   */
2323                   
2324                 struct Incoming *in=&lp->incoming[saddr];
2325                 
2326                 BUGMSG(D_RX,"packet is split (splitflag=%d, seq=%d)\n",
2327                         arcsoft->split_flag,in->sequence);
2328 
2329                 if (in->skb && in->sequence!=arcsoft->sequence)
2330                 {
2331                         BUGMSG(D_NORMAL,"wrong seq number, aborting assembly (saddr=%d, expected=%d, seq=%d, splitflag=%d)\n",
2332                                 saddr,in->sequence,arcsoft->sequence,
2333                                 arcsoft->split_flag);
2334                         kfree_skb(in->skb,FREE_WRITE);
2335                         in->skb=NULL;
2336                         lp->stats.rx_errors++;
2337                         lp->stats.rx_missed_errors++;
2338                         in->lastpacket=in->numpackets=0;
2339                 }
2340                   
2341                 if (arcsoft->split_flag & 1)    /* first packet in split */
2342                 {
2343                         BUGMSG(D_RX,"brand new splitpacket (splitflag=%d)\n",
2344                                 arcsoft->split_flag);
2345                         if (in->skb)    /* already assembling one! */
2346                         {
2347                                 BUGMSG(D_NORMAL,"aborting previous (seq=%d) assembly (splitflag=%d, seq=%d)\n",
2348                                         in->sequence,arcsoft->split_flag,
2349                                         arcsoft->sequence);
2350                                 lp->stats.rx_errors++;
2351                                 lp->stats.rx_missed_errors++;
2352                                 kfree_skb(in->skb,FREE_WRITE);
2353                         }
2354 
2355                         in->sequence=arcsoft->sequence;
2356                         in->numpackets=((unsigned)arcsoft->split_flag>>1)+2;
2357                         in->lastpacket=1;
2358                         
2359                         if (in->numpackets>16)
2360                         {
2361                                 BUGMSG(D_NORMAL,"incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
2362                                         arcsoft->split_flag);
2363                                 lp->stats.rx_errors++;
2364                                 lp->stats.rx_length_errors++;
2365                                 return;
2366                         }
2367                 
2368                         in->skb=skb=alloc_skb(508*in->numpackets
2369                                         + sizeof(struct ClientData),
2370                                         GFP_ATOMIC);
2371                         if (skb == NULL) {
2372                                 BUGMSG(D_NORMAL,"(split) memory squeeze, dropping packet.\n");
2373                                 lp->stats.rx_dropped++;
2374                                 return;
2375                         }
2376                                         
2377                         /* I don't know what this is for, but it DOES avoid
2378                          * warnings...
2379                          */
2380                         skb->free=1;
2381                         
2382                         soft=(struct ClientData *)skb->data;
2383                         
2384                         skb->len=sizeof(struct ClientData);
2385                         skb->dev=dev;
2386 
2387                         memcpy((u_char *)soft+EXTRA_CLIENTDATA,
2388                                 (u_char *)arcsoft+EXTRA_CLIENTDATA,
2389                                 sizeof(struct ClientData)-EXTRA_CLIENTDATA);
2390                         soft->split_flag=0; /* final packet won't be split */
2391                 }
2392                 else                    /* not first packet */
2393                 {
2394                         int packetnum=((unsigned)arcsoft->split_flag>>1) + 1;
2395 
2396                         /* if we're not assembling, there's no point
2397                          * trying to continue.
2398                          */                     
2399                         if (!in->skb)
2400                         {
2401                                 BUGMSG(D_NORMAL,"can't continue split without starting first! (splitflag=%d, seq=%d)\n",
2402                                         arcsoft->split_flag,arcsoft->sequence);
2403                                 lp->stats.rx_errors++;
2404                                 lp->stats.rx_missed_errors++;
2405                                 return;
2406                         }
2407 
2408                         in->lastpacket++;
2409                         if (packetnum!=in->lastpacket) /* not the right flag! */
2410                         {
2411                                 /* harmless duplicate? ignore. */
2412                                 if (packetnum<=in->lastpacket-1)
2413                                 {
2414                                         BUGMSG(D_NORMAL,"duplicate splitpacket ignored! (splitflag=%d)\n",
2415                                                 arcsoft->split_flag);
2416                                         lp->stats.rx_errors++;
2417                                         lp->stats.rx_frame_errors++;
2418                                         return;
2419                                 }
2420                                 
2421                                 /* "bad" duplicate, kill reassembly */
2422                                 BUGMSG(D_NORMAL,"out-of-order splitpacket, reassembly (seq=%d) aborted (splitflag=%d, seq=%d)\n",       
2423                                         in->sequence,arcsoft->split_flag,
2424                                         arcsoft->sequence);
2425                                 kfree_skb(in->skb,FREE_WRITE);
2426                                 in->skb=NULL;
2427                                 lp->stats.rx_errors++;
2428                                 lp->stats.rx_missed_errors++;
2429                                 in->lastpacket=in->numpackets=0;
2430                                 return;
2431                         }
2432 
2433                         soft=(struct ClientData *)in->skb->data;
2434                 }
2435                 
2436                 skb=in->skb;
2437                 
2438                 memcpy(skb->data+skb->len,
2439                        (u_char *)arcsoft+sizeof(struct ClientData),
2440                        length-sizeof(struct ClientData));
2441 
2442                 skb->len+=length-sizeof(struct ClientData);
2443                 
2444                 soft->daddr=daddr;
2445                 soft->saddr=saddr;
2446                 
2447                 /* are we done? */
2448                 if (in->lastpacket == in->numpackets)
2449                 {
2450                         if (!skb || !in->skb)
2451                         {
2452                                 BUGMSG(D_NORMAL,"?!? done reassembling packet, no skb? (skb=%ph, in->skb=%ph)\n",
2453                                         skb,in->skb);
2454                         }
2455                         else
2456                         {
2457                                 in->skb=NULL;
2458                                 in->lastpacket=in->numpackets=0;
2459                         
2460                                 BUGLVL(D_SKB)
2461                                 {
2462                                         short i;
2463                                         for(i=0; i<skb->len; i++)
2464                                         {
2465                                                 if( i%16 == 0 ) printk("\n[%04hX] ",i);
2466                                                 printk("%02hX ",((unsigned char*)skb->data)[i]);
2467                                         }
2468                                         printk("\n");
2469                                 }
2470 
2471                         
2472                                 skb->protocol=arcnetA_type_trans(skb,dev);
2473                                 
2474                                 netif_rx(skb);
2475                         }
2476                 }
2477          }
2478 }
2479 
2480 
2481 /* Packet receiver for non-standard ethernet-style packets
2482  */
2483 static void
2484 arcnetE_rx(struct device *dev,u_char *arcsoft,
     /* [previous][next][first][last][top][bottom][index][help] */
2485         int length,u_char saddr, u_char daddr)
2486 {
2487         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
2488         struct sk_buff *skb;
2489         
2490         BUGMSG(D_DURING,"it's an ethernet-encap packet (length=%d)\n",
2491                         length);
2492 
2493         skb = alloc_skb(length, GFP_ATOMIC);
2494         if (skb == NULL) {
2495                 BUGMSG(D_NORMAL,"Memory squeeze, dropping packet.\n");
2496                 lp->stats.rx_dropped++;
2497                 return;
2498         }
2499         
2500         skb->len = length;
2501         skb->dev = dev;
2502         
2503         memcpy(skb->data,(u_char *)arcsoft+1,length-1);
2504 
2505         BUGLVL(D_SKB)
2506         {
2507                 short i;
2508                 printk("%6s: rx skb dump follows:\n",dev->name);
2509                 for(i=0; i<skb->len; i++)
2510                 {
2511                         if (i%16==0)
2512                                 printk("\n[%04hX] ",i);
2513                         else
2514                                 printk("%02hX ",((u_char *)skb->data)[i]);
2515                 }
2516                 printk("\n");
2517         }
2518         
2519         skb->protocol=eth_type_trans(skb,dev);
2520         
2521         netif_rx(skb);
2522 }
2523 
2524 /* Packet receiver for RFC1051 packets; 
2525  */
2526 static void
2527 arcnetS_rx(struct device *dev,u_char *buf,
     /* [previous][next][first][last][top][bottom][index][help] */
2528         int length,u_char saddr, u_char daddr)
2529 {
2530         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
2531         struct sk_buff *skb;
2532         struct S_ClientData *arcsoft,*soft;
2533         
2534         arcsoft=(struct S_ClientData *)(buf-S_EXTRA_CLIENTDATA);
2535         length+=S_EXTRA_CLIENTDATA;
2536         
2537         BUGMSG(D_DURING,"it's an RFC1051 packet (length=%d)\n",
2538                         length);
2539                         
2540         
2541                         
2542         {    /* was "if not split" in A protocol, S is never split */   
2543 
2544                 skb = alloc_skb(length, GFP_ATOMIC);
2545                 if (skb == NULL) {
2546                         BUGMSG(D_NORMAL,"Memory squeeze, dropping packet.\n");
2547                         lp->stats.rx_dropped++;
2548                         return;
2549                 }
2550                 soft=(struct S_ClientData *)skb->data;
2551                 skb->len = length;
2552                 memcpy((u_char *)soft + sizeof(struct S_ClientData)
2553                                 - S_EXTRA_CLIENTDATA,
2554                         (u_char *)arcsoft + sizeof(struct S_ClientData)
2555                                 - S_EXTRA_CLIENTDATA, 
2556                         length - sizeof(struct S_ClientData)
2557                                 + S_EXTRA_CLIENTDATA);
2558                 soft->protocol_id=arcsoft->protocol_id;
2559                 soft->daddr=daddr;
2560                 soft->saddr=saddr;
2561                 skb->dev = dev;  /* is already lp->sdev */
2562                 
2563                 BUGLVL(D_SKB)
2564                 {
2565                         short i;
2566                         for(i=0; i<skb->len; i++)
2567                         {
2568                                 if( i%16 == 0 ) printk("\n[%04hX] ",i);
2569                                 printk("%02hX ",((unsigned char*)skb->data)[i]);
2570                         }
2571                         printk("\n");
2572                 }
2573 
2574                 skb->protocol=arcnetS_type_trans(skb,dev);
2575 
2576                 netif_rx(skb);
2577          }
2578 }
2579 
2580 
2581 
2582 /****************************************************************************
2583  *                                                                          *
2584  * Miscellaneous routines                                                   *
2585  *                                                                          *
2586  ****************************************************************************/
2587 
2588 
2589 /* Get the current statistics.  This may be called with the card open or
2590  * closed.
2591  */
2592 
2593 static struct enet_statistics *
2594 arcnet_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2595 {
2596         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
2597 
2598         return &lp->stats;
2599 }
2600 
2601 #if 0
2602 /* Set or clear the multicast filter for this adaptor.
2603  * num_addrs == -1      Promiscuous mode, receive all packets
2604  * num_addrs == 0       Normal mode, clear multicast list
2605  * num_addrs > 0        Multicast mode, receive normal and MC packets, and do
2606  *                      best-effort filtering.
2607  */
2608 static void
2609 set_multicast_list(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2610 {
2611 #if 0     /* no promiscuous mode at all on most ARCnet models */
2612         struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
2613 
2614         short ioaddr = dev->base_addr;
2615         if (num_addrs) {
2616                 outw(69, ioaddr);               /* Enable promiscuous mode */
2617         } else
2618                 outw(99, ioaddr);               /* Disable promiscuous mode, use normal mode */
2619 #endif
2620 }
2621 #endif
2622 
2623 /* Create the ARCnet ClientData header for an arbitrary protocol layer
2624  *
2625  * saddr=NULL   means use device source address (always will anyway)
2626  * daddr=NULL   means leave destination address (eg unresolved arp)
2627  */
2628 int arcnetA_header(struct sk_buff *skb,struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
2629                 unsigned short type,void *daddr,void *saddr,unsigned len)
2630 {
2631         struct ClientData *head = (struct ClientData *)
2632                 skb_push(skb,dev->hard_header_len);
2633         struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
2634 
2635         BUGMSG(D_DURING,"create header from %d to %d; protocol %d (%Xh); size %u.\n",
2636                         saddr ? *(u_char*)saddr : -1,
2637                         daddr ? *(u_char*)daddr : -1,
2638                         type,type,len);
2639 
2640         /* set the protocol ID according to RFC1201 */
2641         switch(type)
2642         {
2643         case ETH_P_IP:
2644                 head->protocol_id=ARC_P_IP;
2645                 break;
2646         case ETH_P_ARP:
2647                 head->protocol_id=ARC_P_ARP;
2648                 break;
2649         case ETH_P_RARP:
2650                 head->protocol_id=ARC_P_RARP;
2651                 break;
2652         case ETH_P_IPX:
2653         case ETH_P_802_3:
2654         case ETH_P_802_2:
2655                 head->protocol_id=ARC_P_IPX;
2656                 break;
2657         case ETH_P_ATALK:
2658                 head->protocol_id=ARC_P_ATALK;
2659                 break;
2660         default:
2661                 BUGMSG(D_NORMAL,"I don't understand protocol %d (%Xh)\n",
2662                         type,type);
2663                 lp->stats.tx_errors++;
2664                 lp->stats.tx_aborted_errors++;
2665                 return 0;
2666         }       
2667 
2668         /*
2669          * Set the source hardware address.
2670          *
2671          * This is pretty pointless for most purposes, but it can help
2672          * in debugging.  saddr is stored in the ClientData header and
2673          * removed before sending the packet (since ARCnet does not allow
2674          * us to change the source address in the actual packet sent)
2675          */
2676         if(saddr)
2677                 head->saddr=((u_char*)saddr)[0];
2678         else
2679                 head->saddr=((u_char*)(dev->dev_addr))[0];
2680 
2681         head->split_flag=0;     /* split packets are done elsewhere */
2682         head->sequence=0;       /* so are sequence numbers */
2683 
2684         /* supposedly if daddr is NULL, we should ignore it... */
2685         if(daddr)
2686         {
2687                 head->daddr=((u_char*)daddr)[0];
2688                 return dev->hard_header_len;
2689         }
2690         else
2691                 head->daddr=0;  /* better fill one in anyway */
2692                 
2693         return -dev->hard_header_len;
2694 }
2695 
2696 
2697 /* Create the ARCnet ClientData header for an arbitrary protocol layer
2698  *
2699  * saddr=NULL   means use device source address (always will anyway)
2700  * daddr=NULL   means leave destination address (eg unresolved arp)
2701  */
2702 int arcnetS_header(struct sk_buff *skb,struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
2703                 unsigned short type,void *daddr,void *saddr,unsigned len)
2704 {
2705         struct S_ClientData *head = (struct S_ClientData *)
2706                 skb_push(skb,dev->hard_header_len);
2707         struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
2708 
2709         /* set the protocol ID according to RFC1051 */
2710         switch(type)
2711         {
2712         case ETH_P_IP:
2713                 head->protocol_id=ARC_P_IP_RFC1051;
2714                 BUGMSG(D_DURING,"S_header: IP_RFC1051 packet.\n");
2715                 break;
2716         case ETH_P_ARP:
2717                 head->protocol_id=ARC_P_ARP_RFC1051;
2718                 BUGMSG(D_DURING,"S_header: ARP_RFC1051 packet.\n");
2719                 break;
2720         default:
2721                 BUGMSG(D_NORMAL,"I don't understand protocol %d (%Xh)\n",
2722                         type,type);
2723                 lp->stats.tx_errors++;
2724                 lp->stats.tx_aborted_errors++;
2725                 return 0;
2726         }       
2727 
2728         /*
2729          * Set the source hardware address.
2730          *
2731          * This is pretty pointless for most purposes, but it can help
2732          * in debugging.  saddr is stored in the ClientData header and
2733          * removed before sending the packet (since ARCnet does not allow
2734          * us to change the source address in the actual packet sent)
2735          */
2736         if(saddr)
2737                 head->saddr=((u_char*)saddr)[0];
2738         else
2739                 head->saddr=((u_char*)(dev->dev_addr))[0];
2740 
2741         /* supposedly if daddr is NULL, we should ignore it... */
2742         if(daddr)
2743         {
2744                 head->daddr=((u_char*)daddr)[0];
2745                 return dev->hard_header_len;
2746         }
2747         else
2748                 head->daddr=0;  /* better fill one in anyway */
2749                 
2750         return -dev->hard_header_len;
2751 }
2752 
2753 
2754 
2755 /* Rebuild the ARCnet ClientData header. This is called after an ARP
2756  * (or in future other address resolution) has completed on this
2757  * sk_buff. We now let ARP fill in the other fields.
2758  */
2759 int arcnetA_rebuild_header(void *buff,struct device *dev,unsigned long dst,
     /* [previous][next][first][last][top][bottom][index][help] */
2760                 struct sk_buff *skb)
2761 {
2762         struct ClientData *head = (struct ClientData *)buff;
2763         struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
2764         int status;
2765 
2766         /*
2767          * Only ARP and IP are currently supported
2768          */
2769          
2770         if(head->protocol_id != ARC_P_IP) 
2771         {
2772                 BUGMSG(D_NORMAL,"I don't understand protocol type %d (%Xh) addresses!\n",
2773                         head->protocol_id,head->protocol_id);
2774                 lp->stats.tx_errors++;
2775                 lp->stats.tx_aborted_errors++;
2776                 head->daddr=0;
2777                 /*memcpy(eth->h_source, dev->dev_addr, dev->addr_len);*/
2778                 return 0;
2779         }
2780 
2781         /*
2782          * Try and get ARP to resolve the header.
2783          */
2784 #ifdef CONFIG_INET       
2785         BUGMSG(D_DURING,"rebuild header from %d to %d; protocol %Xh\n",
2786                         head->saddr,head->daddr,head->protocol_id);
2787         status=arp_find(&(head->daddr), dst, dev, dev->pa_addr, skb)? 1 : 0;
2788         BUGMSG(D_DURING," rebuilt: from %d to %d; protocol %Xh\n",
2789                         head->saddr,head->daddr,head->protocol_id);
2790         return status;
2791 #else
2792         return 0;       
2793 #endif  
2794 }
2795 
2796 
2797 int arcnetS_rebuild_header(void *buff,struct device *dev,unsigned long dst,
     /* [previous][next][first][last][top][bottom][index][help] */
2798                 struct sk_buff *skb)
2799 {
2800         struct S_ClientData *head = (struct S_ClientData *)buff;
2801         struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
2802 
2803         /*
2804          * Only ARP and IP are currently supported
2805          */
2806          
2807         if(head->protocol_id != ARC_P_IP_RFC1051) 
2808         {
2809                 BUGMSG(D_NORMAL,"I don't understand protocol type %d (%Xh) addresses!\n",
2810                         head->protocol_id,head->protocol_id);
2811                 lp->stats.tx_errors++;
2812                 lp->stats.tx_aborted_errors++;
2813                 head->daddr=0;
2814                 /*memcpy(eth->h_source, dev->dev_addr, dev->addr_len);*/
2815                 return 0;
2816         }
2817 
2818         /*
2819          * Try and get ARP to resolve the header.
2820          */
2821 #ifdef CONFIG_INET       
2822         return arp_find(&(head->daddr), dst, dev, dev->pa_addr, skb)? 1 : 0;
2823 #else
2824         return 0;       
2825 #endif  
2826 }
2827 
2828                 
2829 /* Determine a packet's protocol ID.
2830  *
2831  * With ARCnet we have to convert everything to Ethernet-style stuff.
2832  */
2833 unsigned short arcnetA_type_trans(struct sk_buff *skb,struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2834 {
2835         struct ClientData *head;
2836         struct arcnet_local *lp=(struct arcnet_local *) (dev->priv);
2837 
2838         /* Pull off the arcnet header. */
2839         skb->mac.raw=skb->data;
2840         skb_pull(skb,dev->hard_header_len);
2841         head=(struct ClientData *)skb->mac.raw;
2842         
2843         if (head->daddr==0)
2844                 skb->pkt_type=PACKET_BROADCAST;
2845         else if (dev->flags&IFF_PROMISC)
2846         {
2847                 /* if we're not sending to ourselves :) */
2848                 if (head->daddr != dev->dev_addr[0])
2849                         skb->pkt_type=PACKET_OTHERHOST;
2850         }
2851         
2852         /* now return the protocol number */
2853         switch (head->protocol_id)
2854         {
2855         case ARC_P_IP:          return htons(ETH_P_IP);
2856         case ARC_P_ARP:         return htons(ETH_P_ARP);
2857         case ARC_P_RARP:        return htons(ETH_P_RARP);
2858 
2859         case ARC_P_IPX:
2860         case ARC_P_NOVELL_EC:
2861                 return htons(ETH_P_802_3);
2862         default:
2863                 BUGMSG(D_NORMAL,"received packet of unknown protocol id %d (%Xh)\n",
2864                                 head->protocol_id,head->protocol_id);
2865                 lp->stats.rx_errors++;
2866                 lp->stats.rx_crc_errors++;
2867                 return 0;
2868         }
2869 
2870         return htons(ETH_P_IP);
2871 }
2872 
2873 
2874 unsigned short arcnetS_type_trans(struct sk_buff *skb,struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2875 {
2876         struct S_ClientData *head;
2877         struct arcnet_local *lp=(struct arcnet_local *) (dev->priv);
2878 
2879         /* Pull off the arcnet header. */
2880         skb->mac.raw=skb->data;
2881         skb_pull(skb,dev->hard_header_len);
2882         head=(struct S_ClientData *)skb->mac.raw;
2883         
2884         if (head->daddr==0)
2885                 skb->pkt_type=PACKET_BROADCAST;
2886         else if (dev->flags&IFF_PROMISC)
2887         {
2888                 /* if we're not sending to ourselves :) */
2889                 if (head->daddr != dev->dev_addr[0])
2890                         skb->pkt_type=PACKET_OTHERHOST;
2891         }
2892         
2893         /* now return the protocol number */
2894         switch (head->protocol_id)
2895         {
2896         case ARC_P_IP_RFC1051:  return htons(ETH_P_IP);
2897         case ARC_P_ARP_RFC1051: return htons(ETH_P_ARP);
2898         case ARC_P_ATALK:   return htons(ETH_P_ATALK); /* untested appletalk */
2899         default:
2900                 BUGMSG(D_NORMAL,"received packet of unknown protocol id %d (%Xh)\n",
2901                         head->protocol_id,head->protocol_id);
2902                 lp->stats.rx_errors++;
2903                 lp->stats.rx_crc_errors++;
2904                 return 0;
2905         }
2906 
2907         return htons(ETH_P_IP);
2908 }
2909 
2910 
2911 
2912 /****************************************************************************
2913  *                                                                          *
2914  * Kernel Loadable Module Support                                           *
2915  *                                                                          *
2916  ****************************************************************************/
2917 
2918 
2919 #ifdef MODULE
2920 static char devicename[9] = { 0, };
2921 static struct device thiscard = {
2922   devicename, /* device name is inserted by linux/drivers/net/net_init.c */
2923   0, 0, 0, 0,
2924   0, 0,  /* I/O address, IRQ */
2925   0, 0, 0, NULL, arcnet_probe
2926 };
2927         
2928         
2929 static int io=0x0;      /* <--- EDIT THESE LINES FOR YOUR CONFIGURATION */
2930 static int irqnum=0;    /* or use the insmod io= irqnum= shmem= options */
2931 static int shmem=0;
2932 static int num=0;       /* number of device (ie for 0 for arc0, 1 for arc1...) */
2933 
2934 int
2935 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2936 {
2937         sprintf(thiscard.name,"arc%d",num);
2938 
2939         thiscard.base_addr=io;
2940 
2941         thiscard.irq=irqnum;
2942         if (thiscard.irq==2) thiscard.irq=9;
2943 
2944         if (shmem)
2945         {
2946                 thiscard.mem_start=shmem;
2947                 thiscard.mem_end=thiscard.mem_start+512*4-1;
2948                 thiscard.rmem_start=thiscard.mem_start+512*0;
2949                 thiscard.rmem_end=thiscard.mem_start+512*2-1;
2950         }
2951 
2952         if (register_netdev(&thiscard) != 0)
2953                 return -EIO;
2954         return 0;
2955 }
2956 
2957 void
2958 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2959 {
2960         int ioaddr=thiscard.base_addr;
2961 
2962         if (thiscard.start) arcnet_close(&thiscard);
2963 
2964         /* Flush TX and disable RX */
2965         if (ioaddr)
2966         {
2967                 outb(0,INTMASK);        /* disable IRQ's */
2968                 outb(NOTXcmd,COMMAND);  /* stop transmit */
2969                 outb(NORXcmd,COMMAND);  /* disable receive */
2970         }
2971 
2972         if (thiscard.irq)
2973         {
2974                 free_irq(thiscard.irq);
2975                 /* very important! */
2976                 irq2dev_map[thiscard.irq] = NULL;
2977         }
2978         
2979         if (thiscard.base_addr) release_region(thiscard.base_addr,
2980                                                 ARCNET_TOTAL_SIZE);
2981         unregister_netdev(&thiscard);
2982         kfree(thiscard.priv);
2983         thiscard.priv = NULL;
2984 }
2985 
2986 #endif /* MODULE */
2987 
2988 
2989 
2990 /*
2991  * Local variables:
2992  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c arcnet.c"
2993  *  version-control: t
2994  *  kept-new-versions: 5
2995  *  tab-width: 8
2996  * End:
2997  */
2998 

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