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

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