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. arcnetA_send_packet
  11. arcnetA_continue_tx
  12. arcnetAS_prepare_tx
  13. arcnetAS_go_tx
  14. arcnetE_send_packet
  15. arcnetS_send_packet
  16. arcnet_interrupt
  17. arcnet_inthandler
  18. arcnet_rx
  19. arcnetA_rx
  20. arcnetE_rx
  21. arcnetS_rx
  22. arcnet_get_stats
  23. set_multicast_list
  24. arcnetA_header
  25. arcnetS_header
  26. arcnetA_rebuild_header
  27. arcnetS_rebuild_header
  28. arcnetA_type_trans
  29. arcnetS_type_trans
  30. init_module
  31. cleanup_module

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

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