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_open
  5. arcnet_close
  6. arcnet_send_packet
  7. arcnet_continue_tx
  8. careful_xmit_wait
  9. arcnet_prepare_tx
  10. arcnet_go_tx
  11. arcnet_interrupt
  12. arcnet_inthandler
  13. arcnet_rx
  14. arcnet_timer
  15. arcnet_get_stats
  16. set_multicast_list
  17. arcnet_reset
  18. arc_header
  19. arc_rebuild_header
  20. arc_type_trans
  21. init_module
  22. 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         skeleton.c Written 1993 by Donald Becker.
  11         Copyright 1993 United States Government as represented by the
  12         Director, National Security Agency.  This software may only be used
  13         and distributed according to the terms of the GNU Public License as
  14         modified by SRC, incorporated herein by reference.
  15          
  16         **********************
  17 
  18         v1.02 (95/06/21)
  19           - A fix to make "exception" packets sent from Linux receivable
  20             on other systems.  (The protocol_id byte was sometimes being set
  21             incorrectly, and Linux wasn't checking it on receive so it
  22             didn't show up)
  23           - Updated my email address.  Please use apenwarr@foxnet.net
  24             from now on.
  25         v1.01 (95/03/24)
  26           - Fixed some IPX-related bugs. (Thanks to Tomasz Motylewski
  27             <motyl@tichy.ch.uj.edu.pl> for the patches to make arcnet work
  28             with dosemu!)
  29         v1.0 (95/02/15)
  30           - Initial non-alpha release.
  31         
  32          
  33         TO DO:
  34         
  35          - Test in systems with NON-ARCnet network cards, just to see if
  36            autoprobe kills anything.  With any luck, it won't.  (It's pretty
  37            careful.)
  38                 - Except some unfriendly NE2000's die. (as of 0.40-ALPHA)
  39          - cards with shared memory that can be "turned off?"
  40          - NFS mount freezes after several megabytes to SOSS for DOS. 
  41            unmount/remount works.  Is this arcnet-specific?  I don't know.
  42          - Add support for the various stupid bugs ("I didn't read the RFC"
  43            syndrome) in Windows for Workgroups and LanMan.
  44  */
  45  
  46 /**************************************************************************/
  47 
  48 /* define this if you want to use the new but possibly dangerous ioprobe
  49  * If you get lockups right after status5, you probably need
  50  * to undefine this.  It should make more cards probe correctly,
  51  * I hope.
  52  */
  53 #define DANGER_PROBE
  54 
  55 /* define this if you want to use the "extra delays" which were removed
  56  * in 0.41 since they seemed needless.
  57  */
  58 #undef EXTRA_DELAYS
  59 
  60 /* undefine this if you want to use the non-IRQ-driven transmitter. (possibly
  61  * safer, although it takes more CPU time and IRQ_XMIT seems fine right now)
  62  */
  63 #define IRQ_XMIT
  64  
  65 /* define this for "careful" transmitting.  Try with and without if you have
  66  * problems.  If you use IRQ_XMIT, do NOT define this.
  67  */
  68 #undef CAREFUL_XMIT
  69 
  70 /* define this for an extra-careful memory detect.  This should work all
  71  * the time now, but you never know.
  72  */
  73 #define STRICT_MEM_DETECT
  74 
  75 /* define this to use the "old-style" limited MTU by default.  It basically
  76  * disables packet splitting.  ifconfig can still be used to reset the MTU.
  77  *
  78  * leave this disabled if possible, so it will use ethernet defaults,
  79  * which is our goal.
  80  */
  81 #undef LIMIT_MTU
  82 
  83 /* define this if you have a problem with the card getting "stuck" now and
  84  * then, which can only be fixed by a reboot or resetting the card manually
  85  * via ifconfig up/down.  ARCnet will set a timer function which is called
  86  * 8 times every second.
  87  *
  88  * This should no longer be necessary.  if you experience "stuck" ARCnet
  89  * drivers, please email apenwarr@foxnet.net or I will remove
  90  * this feature in a future release.
  91  */
  92 #undef USE_TIMER_HANDLER
  93 
  94 /**************************************************************************/
  95  
  96 static char *version =
  97  "arcnet.c:v1.02 95/06/21 Avery Pennarun <apenwarr@foxnet.net>\n";
  98 
  99 /*
 100   Sources:
 101         Crynwr arcnet.com/arcether.com packet drivers.
 102         arcnet.c v0.00 dated 1/1/94 and apparently by
 103                 Donald Becker - it didn't work :)
 104         skeleton.c v0.05 dated 11/16/93 by Donald Becker
 105                 (from Linux Kernel 1.1.45)
 106         ...I sure wish I had the ARCnet data sheets right about now!
 107         RFC's 1201 and 1051 (mostly 1201) - re: ARCnet IP packets
 108         net/inet/eth.c (from kernel 1.1.50) for header-building info...
 109         Alternate Linux ARCnet source by V.Shergin <vsher@sao.stavropol.su>
 110         Textual information and more alternate source from Joachim Koenig
 111                 <jojo@repas.de>
 112 */
 113 
 114 #include <linux/config.h>
 115 #ifdef MODULE
 116 #include <linux/module.h>
 117 #include <linux/version.h>
 118 #endif /* MODULE */
 119 
 120 #include <linux/kernel.h>
 121 #include <linux/sched.h>
 122 #include <linux/types.h>
 123 #include <linux/fcntl.h>
 124 #include <linux/interrupt.h>
 125 #include <linux/ptrace.h>
 126 #include <linux/ioport.h>
 127 #include <linux/in.h>
 128 #include <linux/malloc.h>
 129 #include <linux/string.h>
 130 #include <linux/timer.h>
 131 #include <linux/errno.h>
 132 #include <linux/delay.h>
 133 
 134 #include <asm/system.h>
 135 #include <asm/bitops.h>
 136 #include <asm/io.h>
 137 #include <asm/dma.h>
 138 
 139 #include <linux/netdevice.h>
 140 #include <linux/etherdevice.h>
 141 #include <linux/skbuff.h>
 142 #include "arp.h"
 143 
 144 
 145 /* debug levels:
 146  * D_OFF        production
 147  * D_NORMAL     verification
 148  * D_INIT       show init/detect messages
 149  * D_DURING     show messages during normal use (ie interrupts)
 150  * D_DATA   show packets data from skb's, not on Arcnet card
 151  * D_TX         show tx packets
 152  * D_RX         show tx+rx packets
 153  */
 154 #define D_OFF           0
 155 #define D_NORMAL        1
 156 #define D_INIT          2
 157 #define D_EXTRA         3
 158 #define D_DURING        4
 159 #define D_DATA          6
 160 #define D_TX            8
 161 #define D_RX            9
 162 
 163 #ifndef NET_DEBUG
 164 #define NET_DEBUG       D_INIT
 165 #endif
 166 static unsigned int net_debug = NET_DEBUG;
 167 
 168 #ifndef HAVE_AUTOIRQ
 169 /* From auto_irq.c, in ioport.h for later versions. */
 170 extern void autoirq_setup(int waittime);
 171 extern int autoirq_report(int waittime);
 172 /* The map from IRQ number (as passed to the interrupt handler) to
 173    'struct device'. */
 174 extern struct device *irq2dev_map[16];
 175 #endif
 176 
 177 #ifndef HAVE_PORTRESERVE
 178 #define check_region(ioaddr, size)              0
 179 #define request_region(ioaddr, size)            do ; while (0)
 180 #endif
 181 
 182 /* macro to simplify debug checking */
 183 #define BUGLVL(x) if (net_debug>=x)
 184 
 185 /* The number of low I/O ports used by the ethercard. */
 186 #define ETHERCARD_TOTAL_SIZE    16
 187 
 188 
 189 /* Handy defines for ARCnet specific stuff */
 190         /* COM 9026 (?) --> ARCnet register addresses */
 191 #define INTMASK (ioaddr+0)              /* writable */
 192 #define STATUS  (ioaddr+0)              /* readable */
 193 #define COMMAND (ioaddr+1)      /* writable, returns random vals on read (?) */
 194 #define RESET  (ioaddr+8)               /* software reset writable */
 195 
 196         /* time needed for various things (in clock ticks, 1/100 sec) */
 197 #define RESETtime 40            /* reset */
 198 #define XMITtime 10             /* send (?) */
 199 #define ACKtime 10              /* acknowledge (?) */
 200 
 201         /* these are the max/min lengths of packet data. (including
 202          * ClientData header)
 203          * note: packet sizes 250, 251, 252 are impossible (God knows why)
 204          *  so exception packets become necessary.
 205          *
 206          * These numbers are compared with the length of the full packet,
 207          * including ClientData header.
 208          */
 209 #define MTU     (253+EXTRA_CLIENTDATA)  /* normal packet max size */
 210 #define MinTU   (257+EXTRA_CLIENTDATA)  /* extended packet min size */
 211 #define XMTU    (508+EXTRA_CLIENTDATA)  /* extended packet max size */
 212 
 213         /* status/interrupt mask bit fields */
 214 #define TXFREEflag      0x001            /* transmitter available */
 215 #define TXACKflag       0x002            /* transmitted msg. ackd */
 216 #define RECONflag       0x004            /* system reconfigured */
 217 #define TESTflag        0x008            /* test flag */
 218 #define RESETflag       0x010            /* power-on-reset */
 219 #define RES1flag        0x020            /* unused */
 220 #define RES2flag        0x040            /* unused */
 221 #define NORXflag        0x080            /* receiver inhibited */
 222 
 223        /* in the command register, the following bits have these meanings:
 224         *                0-2     command
 225         *                3-4     page number (for enable rcv/xmt command)
 226         *                 7      receive broadcasts
 227         */
 228 #define NOTXcmd         0x001            /* disable transmitter */
 229 #define NORXcmd         0x002            /* disable receiver */
 230 #define TXcmd           0x003            /* enable transmitter */
 231 #define RXcmd           0x004            /* enable receiver */
 232 #define CONFIGcmd       0x005            /* define configuration */
 233 #define CFLAGScmd       0x006            /* clear flags */
 234 #define TESTcmd         0x007            /* load test flags */
 235 
 236        /* flags for "clear flags" command */
 237 #define RESETclear      0x008            /* power-on-reset */
 238 #define CONFIGclear     0x010            /* system reconfigured */
 239 
 240       /* flags for "load test flags" command */
 241 #define TESTload        0x008            /* test flag (diagnostic) */
 242 
 243       /* byte deposited into first address of buffers on reset */
 244 #define TESTvalue       0321             /* that's octal for 0xD1 :) */
 245 
 246       /* for "enable receiver" command */
 247 #define RXbcasts        0x080            /* receive broadcasts */
 248 
 249       /* flags for "define configuration" command */
 250 #define NORMALconf      0x000            /* 1-249 byte packets */
 251 #define EXTconf         0x008            /* 250-504 byte packets */
 252 
 253         /* buffers (4 total) used for receive and xmit.
 254          */
 255 #define EnableReceiver()        outb(RXcmd|(recbuf<<3)|RXbcasts,COMMAND)
 256 /*#define TXbuf         2 (Obsoleted by ping-pong xmits) */
 257 
 258         /* Protocol ID's */
 259 #define ARC_P_IP        212             /* 0xD4 */
 260 #define ARC_P_ARP       213             /* 0xD5 */
 261 #define ARC_P_RARP      214             /* 0xD6 */
 262 #define ARC_P_IPX       250             /* 0xFA */
 263 #define ARC_P_LANSOFT   251             /* 0xFB */
 264 #define ARC_P_ATALK     0xDD
 265 
 266         /* Length of time between "stuck" checks */
 267 #define TIMERval        (HZ/8)          /* about 1/8 second */
 268 
 269         /* these structures define the format of an arcnet packet. */
 270 #define NORMAL          0
 271 #define EXTENDED        1
 272 #define EXCEPTION       2
 273 
 274         /* the header required by the card itself */
 275 struct HardHeader
 276 {
 277         u_char  source,         /* source ARCnet - filled in automagically */
 278                 destination,    /* destination ARCnet - 0 for broadcast */
 279                 offset1,        /* offset of ClientData (256-byte packets) */
 280                 offset2;        /* offset of ClientData (512-byte packets) */
 281 };
 282 
 283         /* a complete ARCnet packet */
 284 union ArcPacket
 285 {
 286         struct HardHeader hardheader;   /* the hardware header */
 287         u_char raw[512];                /* raw packet info, incl ClientData */
 288 };
 289 
 290         /* the "client data" header - RFC-1201 information
 291          * notice that this screws up if it's not an even number of bytes
 292          * <sigh>
 293          */
 294 struct ClientData
 295 {
 296         /* data that's NOT part of real packet */
 297         u_char  daddr;          /* Destination address - stored here,
 298                                  *   but WE MUST GET RID OF IT BEFORE SENDING A
 299                                  *   PACKET!!
 300                                  */
 301         u_char  saddr;          /* Source address - necessary for IPX protocol */
 302         
 303         /* data that IS part of real packet */
 304         u_char  protocol_id,    /* ARC_P_IP, ARC_P_ARP, or ARC_P_RARP */
 305                 split_flag;     /* for use with split packets */
 306         u_short sequence;       /* sequence number (?) */
 307 };
 308 #define EXTRA_CLIENTDATA (sizeof(struct ClientData)-4)
 309 
 310 
 311 /* "Incoming" is information needed for each address that could be sending
 312  * to us.  Mostly for partially-received split packets.
 313  */
 314 struct Incoming
 315 {
 316         struct sk_buff *skb;            /* packet data buffer             */
 317         unsigned char lastpacket,       /* number of last packet (from 1) */
 318                       numpackets;       /* number of packets in split     */
 319         u_short sequence;               /* sequence number of assembly    */
 320 };
 321 
 322 struct Outgoing
 323 {
 324         struct sk_buff *skb;            /* buffer from upper levels */
 325         struct ClientData *hdr;         /* clientdata of last packet */
 326         u_char *data;                   /* pointer to data in packet */
 327         short length,                   /* bytes total */
 328               dataleft,                 /* bytes left */
 329               segnum,                   /* segment being sent */
 330               numsegs,                  /* number of segments */
 331               seglen;                   /* length of segment */
 332 };
 333 
 334 
 335 /* Information that needs to be kept for each board. */
 336 struct arcnet_local {
 337         struct enet_statistics stats;
 338         u_char arcnum;          /* arcnet number - our 8-bit address */
 339         u_short sequence;       /* sequence number (incs with each packet) */
 340         u_char recbuf,          /* receive buffer # (0 or 1) */
 341                txbuf,           /* transmit buffer # (2 or 3) */
 342                txready;         /* buffer where a packet is ready to send */
 343         short intx,             /* in TX routine? */
 344               in_txhandler,     /* in TX_IRQ handler? */
 345               sending;          /* transmit in progress? */
 346         short tx_left;          /* segments of split packet left to TX */
 347         struct timer_list timer; /* the timer interrupt struct */
 348         struct Incoming incoming[256];  /* one from each address */
 349         struct Outgoing outgoing; /* packet currently being sent */
 350 };
 351 
 352 
 353 /* Index to functions, as function prototypes. */
 354 extern int arcnet_probe(struct device *dev);
 355 #ifndef MODULE
 356 static int arcnet_memprobe(struct device *dev,u_char *addr);
 357 static int arcnet_ioprobe(struct device *dev, short ioaddr);
 358 #endif
 359 
 360 static int arcnet_open(struct device *dev);
 361 static int arcnet_close(struct device *dev);
 362 
 363 static int arcnet_send_packet(struct sk_buff *skb, struct device *dev);
 364 #ifdef CAREFUL_XMIT
 365  static void careful_xmit_wait(struct device *dev);
 366 #else
 367  #define careful_xmit_wait(dev)
 368 #endif
 369 static void arcnet_continue_tx(struct device *dev);
 370 static void arcnet_prepare_tx(struct device *dev,struct ClientData *hdr,
 371                 short length,char *data);
 372 static void arcnet_go_tx(struct device *dev);
 373 
 374 static void arcnet_interrupt(int irq,struct pt_regs *regs);
 375 static void arcnet_inthandler(struct device *dev);
 376 static void arcnet_rx(struct device *dev,int recbuf);
 377 
 378 #ifdef USE_TIMER_HANDLER
 379 static void arcnet_timer(unsigned long arg);
 380 #endif
 381 
 382 static struct enet_statistics *arcnet_get_stats(struct device *dev);
 383 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
 384 
 385         /* annoying functions for header/arp/etc building */
 386 int arc_header(unsigned char *buff,struct device *dev,unsigned short type,
 387                 void *daddr,void *saddr,unsigned len,struct sk_buff *skb);
 388 int arc_rebuild_header(void *eth,struct device *dev,unsigned long raddr,
 389                 struct sk_buff *skb);
 390 unsigned short arc_type_trans(struct sk_buff *skb,struct device *dev);
 391 
 392 static int arcnet_reset(struct device *dev);
 393 
 394 #ifdef MODULE
 395 int  init_module(void);
 396 void cleanup_module(void);
 397 #endif
 398 
 399 #define tx_done(dev) 1
 400 
 401 /*
 402 #define JIFFER(time) for (delayval=jiffies+(time); delayval>jiffies;);
 403 */
 404 #define JIFFER(time) for (delayval=0; delayval<(time*10); delayval++) \
 405                 udelay(1000);
 406 
 407 #ifdef EXTRA_DELAYS
 408  #define XJIFFER(time) JIFFER(time)
 409 #else
 410  #define XJIFFER(time)
 411 #endif
 412 
 413 
 414 /* Check for a network adaptor of this type, and return '0' if one exists.
 415  *  If dev->base_addr == 0, probe all likely locations.
 416  *  If dev->base_addr == 1, always return failure.
 417  *  If dev->base_addr == 2, allocate space for the device and return success
 418  *  (detachable devices only).
 419  */
 420 int
 421 arcnet_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 422 {
 423 #ifndef MODULE
 424         /* I refuse to probe anything less than 0x200, because anyone using
 425          * an address like that should probably be shot.
 426          */
 427         int *port, ports[] = {/* first the suggested values! */
 428                               0x300,0x2E0,0x2F0,0x2D0,
 429                               /* ...now everything else possible. */
 430                               0x200,0x210,0x220,0x230,0x240,0x250,0x260,0x270,
 431                               0x280,0x290,0x2a0,0x2b0,0x2c0,
 432                                     0x310,0x320,0x330,0x340,0x350,0x360,0x370,
 433                               0x380,0x390,0x3a0,/* video ports, */0x3e0,0x3f0,
 434                               /* a null ends the list */
 435                               0};
 436         /* I'm not going to probe below 0xA0000 either, for similar reasons.
 437          */
 438         unsigned long *addr, addrs[] = {0xD0000,0xE0000,0xA0000,0xB0000,
 439                                         0xC0000,0xF0000,
 440                                         /* from <mdrejhon@magi.com> */
 441                                         0xE1000,
 442                                         0xDD000,0xDC000,
 443                                         0xD9000,0xD8000,0xD5000,0xD4000,0xD1000,
 444                                         0xCD000,0xCC000,
 445                                         0xC9000,0xC8000,0xC5000,0xC4000,
 446                                         /* terminator */
 447                                         0};
 448         int base_addr=dev->base_addr, status=0;
 449 #endif /* MODULE */
 450         int delayval;
 451         struct arcnet_local *lp;
 452 
 453         if (net_debug)
 454         {
 455                 printk(version);
 456                 printk("arcnet: ***\n");
 457                 printk("arcnet: * Read linux/drivers/net/README.arcnet for important release notes!\n");
 458                 printk("arcnet: *\n");
 459                 printk("arcnet: * This version should be stable, but e-mail me if you have any\n");
 460                 printk("arcnet: * questions, comments, or bug reports!\n");
 461                 printk("arcnet: ***\n");
 462         }
 463 
 464         BUGLVL(D_INIT)
 465                 printk("arcnet: given: base %lXh, IRQ %Xh, shmem %lXh\n",
 466                         dev->base_addr,dev->irq,dev->mem_start);
 467                         
 468 #ifndef MODULE
 469         if (base_addr > 0x1ff)          /* Check a single specified location. */
 470                 status=arcnet_ioprobe(dev, base_addr);
 471         else if (base_addr > 0)         /* Don't probe at all. */
 472                 return ENXIO;
 473         else for (port = &ports[0]; *port; port++)
 474         {
 475                 int ioaddr = *port;
 476                 if (check_region(ioaddr, ETHERCARD_TOTAL_SIZE))
 477                 {
 478                         BUGLVL(D_INIT)
 479                                 printk("arcnet: Skipping %Xh because of check_region...\n",
 480                                         ioaddr);
 481                         continue;
 482                 }
 483 
 484                 status=arcnet_ioprobe(dev, ioaddr);
 485                 if (!status) break;
 486         }
 487         
 488         if (status) return status;
 489 
 490         /* ioprobe turned out okay.  Now give it a couple seconds to finish
 491          * initializing...
 492          */
 493         BUGLVL(D_INIT)
 494                 printk("arcnet: ioprobe okay!  Waiting for reset...\n");
 495         JIFFER(100);
 496 
 497         /* okay, now we have to find the shared memory area. */
 498         BUGLVL(D_INIT)
 499                 printk("arcnet: starting memory probe, given %lXh\n",
 500                         dev->mem_start);
 501         if (dev->mem_start)     /* value given - probe just that one */
 502         {
 503                 status=arcnet_memprobe(dev,(u_char *)dev->mem_start);
 504                 if (status) return status;
 505         }
 506         else                    /* no value given - probe everything */
 507         {
 508                 for (addr = &addrs[0]; *addr; addr++) {
 509                         status=arcnet_memprobe(dev,(u_char *)(*addr));
 510                         if (!status) break;
 511                 }
 512                 
 513                 if (status) return status;
 514         }
 515 #else /* MODULE */
 516         if (!dev->base_addr || !dev->irq || !dev->mem_start 
 517                 || !dev->rmem_start)
 518         {
 519                 printk("arcnet: loadable modules can't autoprobe!\n");
 520                 printk("arcnet:  try using io=, irqnum=, and shmem= on the insmod line.\n");
 521                 printk("arcnet:  you may also need num= to change the device name. (ie. num=1 for arc1)\n");
 522                 return ENODEV;
 523         }
 524 #endif
 525         /* now reserve the irq... */
 526         {       
 527                 int irqval = request_irq(dev->irq, &arcnet_interrupt, 0,
 528                         "arcnet");
 529                 if (irqval) {
 530                         printk("%s: unable to get IRQ %d (irqval=%d).\n",
 531                                 dev->name,dev->irq, irqval);
 532                         return EAGAIN;
 533                  }
 534          }
 535          
 536         /* Grab the region so we can find another board if autoIRQ fails. */
 537         request_region(dev->base_addr, ETHERCARD_TOTAL_SIZE,"arcnet");
 538         
 539         printk("%s: ARCnet card found at %03lXh, IRQ %d, ShMem at %lXh.\n", 
 540                 dev->name, dev->base_addr, dev->irq, dev->mem_start);
 541 
 542         /* Initialize the device structure. */
 543         dev->priv = kmalloc(sizeof(struct arcnet_local), GFP_KERNEL);
 544         memset(dev->priv, 0, sizeof(struct arcnet_local));
 545         lp=(struct arcnet_local *)(dev->priv);
 546 
 547         dev->open               = arcnet_open;
 548         dev->stop               = arcnet_close;
 549         dev->hard_start_xmit = arcnet_send_packet;
 550         dev->get_stats  = arcnet_get_stats;
 551 #ifdef HAVE_MULTICAST
 552         dev->set_multicast_list = &set_multicast_list;
 553 #endif
 554 
 555         /* Fill in the fields of the device structure with ethernet-generic values. */
 556         ether_setup(dev);
 557 
 558         /* And now fill particular ones with arcnet values :) */
 559         
 560         dev->type=ARPHRD_ARCNET;
 561         dev->hard_header_len=sizeof(struct ClientData);
 562         BUGLVL(D_EXTRA)
 563                 printk("arcnet: ClientData header size is %d.\narcnet: HardHeader size is %d.\n",
 564                         sizeof(struct ClientData),sizeof(struct HardHeader));
 565 #if LIMIT_MTU   /* the old way - normally, now use ethernet default */
 566         dev->mtu=512-sizeof(struct HardHeader)+EXTRA_CLIENTDATA;
 567 #endif
 568                 /* since we strip EXTRA_CLIENTDATA bytes off before sending,
 569                  * we let Linux add that many bytes to the packet data...
 570                  */
 571         dev->addr_len=1;
 572         dev->broadcast[0]=0x00;
 573         
 574         BUGLVL(D_INIT) printk("arcnet: arcnet_probe: resetting card.\n");
 575         arcnet_reset(dev);
 576         JIFFER(50);
 577         BUGLVL(D_NORMAL)
 578                 printk("arcnet: We appear to be station %d (%02Xh)\n",
 579                         lp->arcnum,lp->arcnum);
 580         if (lp->arcnum==0)
 581                 printk("arcnet: WARNING!  Station address 0 is reserved for broadcasts!\n");
 582         if (lp->arcnum==255)
 583                 printk("arcnet: WARNING!  Station address 255 may confuse DOS networking programs!\n");
 584         dev->dev_addr[0]=lp->arcnum;
 585         lp->sequence=1;
 586         lp->recbuf=0;
 587 
 588         dev->hard_header        = arc_header;
 589         dev->rebuild_header     = arc_rebuild_header;
 590         dev->type_trans         = arc_type_trans;
 591 
 592         return 0;
 593 }
 594 
 595 #ifndef MODULE
 596 
 597 int arcnet_ioprobe(struct device *dev, short ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 598 {
 599         int delayval,airq;
 600 
 601         BUGLVL(D_INIT)
 602                 printk("arcnet: probing address %Xh\n",ioaddr);
 603 
 604         BUGLVL(D_INIT)
 605                 printk("arcnet:  status1=%Xh\n",inb(STATUS));
 606 
 607                 
 608         /* very simple - all we have to do is reset the card, and if there's
 609          * no irq, it's not an ARCnet.  We can also kill two birds with
 610          * one stone because we detect the IRQ at the same time :)
 611          */
 612          
 613         /* reset the card by reading the reset port */
 614         inb(RESET);
 615         JIFFER(RESETtime);
 616 
 617         /* if status port is FF, there's certainly no arcnet... give up. */
 618         if (inb(STATUS)==0xFF)
 619         {
 620                 BUGLVL(D_INIT)
 621                         printk("arcnet:  probe failed.  Status port empty.\n");
 622                 return ENODEV;
 623         }
 624 
 625         /* we'll try to be reasonably sure it's an arcnet by making sure
 626          * the value of the COMMAND port changes automatically once in a
 627          * while.  I have no idea what those values ARE, but at least
 628          * they work.
 629          */
 630         {
 631                 int initval,curval;
 632                 
 633                 curval=initval=inb(COMMAND);
 634                 delayval=jiffies+5;
 635                 while (delayval>=jiffies && curval==initval)
 636                         curval=inb(COMMAND);
 637                         
 638                 if (curval==initval)
 639                 {
 640                         printk("arcnet:  probe failed.  never-changing command port (%02Xh).\n",
 641                                 initval);
 642                         return ENODEV;
 643                 }
 644         }
 645 
 646         BUGLVL(D_INIT)
 647                 printk("arcnet:  status2=%Xh\n",inb(STATUS));
 648 
 649         /* now we turn the reset bit off so we can IRQ next reset... */
 650         outb(CFLAGScmd|RESETclear|CONFIGclear,COMMAND);
 651         XJIFFER(ACKtime);
 652         if (inb(STATUS) & RESETflag) /* reset flag STILL on */
 653         {
 654                 BUGLVL(D_INIT)
 655                         printk("arcnet:  probe failed.  eternal reset flag1...(status=%Xh)\n",
 656                                 inb(STATUS));
 657                 return ENODEV;
 658         }
 659 
 660         /* set up automatic IRQ detection */
 661         autoirq_setup(0);
 662         
 663         /* enable reset IRQ's (shouldn't be necessary, but worth a try) */
 664         outb(RESETflag,INTMASK);
 665 
 666         /* now reset it again to generate an IRQ */
 667         inb(RESET);
 668         JIFFER(RESETtime);
 669 
 670         BUGLVL(D_INIT)
 671                 printk("arcnet:  status3=%Xh\n",inb(STATUS));
 672 
 673         /* and turn the reset flag back off */
 674         outb(CFLAGScmd|RESETclear|CONFIGclear,COMMAND);
 675         XJIFFER(ACKtime);
 676 
 677         BUGLVL(D_INIT)
 678                 printk("arcnet:  status4=%Xh\n",inb(STATUS));
 679 
 680         /* enable reset IRQ's again */
 681         outb(RESETflag,INTMASK);
 682 
 683         /* now reset it again to generate an IRQ */
 684         inb(RESET);
 685         JIFFER(RESETtime);
 686         
 687         BUGLVL(D_INIT)
 688                 printk("arcnet:  status5=%Xh\n",inb(STATUS));
 689 
 690         /* if we do this, we're sure to get an IRQ since the card has
 691          * just reset and the NORXflag is on until we tell it to start
 692          * receiving.
 693          *
 694          * However, this could, theoretically, cause a lockup.  Maybe I'm just
 695          * not very good at theory! :)
 696          */
 697 #ifdef DANGER_PROBE
 698         outb(NORXflag,INTMASK);
 699         JIFFER(RESETtime);
 700         outb(0,INTMASK);
 701 #endif
 702 
 703         /* and turn the reset flag back off */
 704         outb(CFLAGScmd|RESETclear|CONFIGclear,COMMAND);
 705         XJIFFER(ACKtime);
 706 
 707         airq = autoirq_report(0);
 708         if (net_debug>=D_INIT && airq)
 709                 printk("arcnet:  autoirq is %d\n", airq);
 710 
 711         /* if there was no autoirq AND the user hasn't set any defaults,
 712          * give up.
 713          */
 714         if (!airq && !(dev->base_addr && dev->irq))
 715         {
 716                 BUGLVL(D_INIT)
 717                         printk("arcnet:  probe failed.  no autoirq...\n");
 718                 return ENODEV;
 719         }
 720 
 721         /* otherwise we probably have a card.  Let's make sure. */
 722         
 723         if (inb(STATUS) & RESETflag) /* reset flag on */
 724         {
 725                 /* now we turn the reset bit off */
 726                 outb(CFLAGScmd|RESETclear|CONFIGclear,COMMAND);
 727                 XJIFFER(ACKtime);
 728         }
 729         
 730         if (inb(STATUS) & RESETflag) /* reset flag STILL on */
 731         {
 732                 BUGLVL(D_INIT)
 733                         printk("arcnet:  probe failed.  eternal reset flag...(status=%Xh)\n",
 734                                 inb(STATUS));
 735                 return ENODEV;
 736         }
 737         
 738         /* okay, we've got a real, live ARCnet on our hands. */
 739         if (!dev->base_addr) dev->base_addr=ioaddr;
 740         
 741         if (dev->irq < 2)               /* "Auto-IRQ" */
 742         {
 743                 /* we already did the autoirq above, so store the values */
 744                 dev->irq=airq;
 745         }
 746         else if (dev->irq == 2)
 747         {
 748                 if (net_debug)
 749                         printk("arcnet: IRQ2 == IRQ9, don't worry.\n");
 750                 dev->irq = 9;
 751         }
 752 
 753         BUGLVL(D_INIT)
 754                 printk("arcnet: irq and base address seem okay. (%lXh, IRQ %d)\n",
 755                         dev->base_addr,dev->irq);
 756         return 0;
 757 }
 758 
 759 
 760 /* A memory probe that is called after the card is reset.
 761  * It checks for the official TESTvalue in byte 0 and makes sure the buffer
 762  * has certain characteristics of an ARCnet...
 763  */
 764 int arcnet_memprobe(struct device *dev,u_char *addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 765 {
 766         BUGLVL(D_INIT)
 767                 printk("arcnet: probing memory at %lXh\n",(u_long)addr);
 768                 
 769         dev->mem_start=0;
 770 
 771 #ifdef STRICT_MEM_DETECT /* probably better. */
 772         /* ARCnet memory byte 0 is TESTvalue */
 773         if (addr[0]!=TESTvalue)
 774         {
 775                 BUGLVL(D_INIT)
 776                         printk("arcnet:  probe failed.  addr=%lXh, addr[0]=%Xh (not %Xh)\n",
 777                                 (unsigned long)addr,addr[0],TESTvalue);
 778                 return ENODEV;
 779         }
 780         
 781         /* now verify the shared memory writability */
 782         addr[0]=0x42;
 783         if (addr[0]!=0x42)
 784         {
 785                 BUGLVL(D_INIT)
 786                         printk("arcnet:  probe failed.  addr=%lXh, addr[0]=%Xh (not 42h)\n",
 787                                 (unsigned long)addr,addr[0]);
 788                 return ENODEV;
 789         }
 790 #else
 791         if (addr[0]!=TESTvalue)
 792         {
 793                 BUGLVL(D_INIT)
 794                         printk("arcnet:  probe failed.  addr=%lXh, addr[0]=%Xh (not %Xh)\n",
 795                                 (unsigned long)addr,addr[0],TESTvalue);
 796                 return ENODEV;
 797         }
 798 #endif
 799 
 800         /* got it!  fill in dev */
 801         dev->mem_start=(unsigned long)addr;
 802         dev->mem_end=dev->mem_start+512*4-1;
 803         dev->rmem_start=dev->mem_start+512*0;
 804         dev->rmem_end=dev->mem_start+512*2-1;
 805 
 806         return 0;
 807 }
 808 
 809 #endif /* MODULE */
 810 
 811 
 812 /* Open/initialize the board.  This is called (in the current kernel)
 813    sometime after booting when the 'ifconfig' program is run.
 814 
 815    This routine should set everything up anew at each open, even
 816    registers that "should" only need to be set once at boot, so that
 817    there is non-reboot way to recover if something goes wrong.
 818    */
 819 static int
 820 arcnet_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 821 {
 822         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
 823 /*      int ioaddr = dev->base_addr;*/
 824 
 825         if (dev->metric>=10)
 826         {
 827                 net_debug=dev->metric-10;
 828                 dev->metric=1;
 829         }
 830 
 831         if (net_debug) printk(version);
 832 
 833 #if 0   /* Yup, they're hardwired in arcnets */
 834         /* This is used if the interrupt line can turned off (shared).
 835            See 3c503.c for an example of selecting the IRQ at config-time. */
 836         if (request_irq(dev->irq, &arcnet_interrupt, 0, "arcnet")) {
 837                 return -EAGAIN;
 838         }
 839 #endif
 840 
 841         irq2dev_map[dev->irq] = dev;
 842 
 843         /* Reset the hardware here. */
 844         BUGLVL(D_EXTRA) printk("arcnet: arcnet_open: resetting card.\n");
 845         
 846         /* try to reset - twice if it fails the first time */
 847         if (arcnet_reset(dev) && arcnet_reset(dev))
 848                 return -ENODEV;
 849         
 850 /*      chipset_init(dev, 1);*/
 851 /*      outb(0x00, ioaddr);*/
 852 
 853 /*      lp->open_time = jiffies;*/
 854 
 855         dev->tbusy=0;
 856         dev->interrupt=0;
 857         dev->start=1;
 858         lp->intx=0;
 859         lp->in_txhandler=0;
 860 
 861 #ifdef USE_TIMER_HANDLER
 862         /* grab a timer handler to recover from any missed IRQ's */
 863         init_timer(&lp->timer);
 864         lp->timer.expires = TIMERval;         /* length of time */
 865         lp->timer.data = (unsigned long)dev;  /* pointer to "dev" structure */
 866         lp->timer.function = &arcnet_timer;    /* timer handler */
 867         add_timer(&lp->timer);
 868 #endif
 869 
 870 #ifdef MODULE
 871         MOD_INC_USE_COUNT;
 872 #endif
 873                                         
 874         return 0;
 875 }
 876 
 877 
 878 /* The inverse routine to arcnet_open(). */
 879 static int
 880 arcnet_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 881 {
 882         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
 883         int ioaddr = dev->base_addr;
 884 #ifdef EXTRA_DELAYS
 885         int delayval;
 886 #endif
 887 
 888 /*      lp->open_time = 0;*/
 889 
 890         dev->tbusy = 1;
 891         dev->start = 0;
 892         
 893         /* release the timer */
 894         del_timer(&lp->timer);
 895 
 896         /* Flush the Tx and disable Rx here.     */
 897         /* resetting the card should do the job. */
 898         /*inb(RESET);*/
 899 
 900         outb(0,INTMASK);        /* no IRQ's */
 901         outb(NOTXcmd,COMMAND);  /* disable transmit */
 902         XJIFFER(ACKtime);
 903         outb(NORXcmd,COMMAND);  /* disable receive */
 904 
 905         /* Update the statistics here. */
 906         
 907 #ifdef MODULE
 908         MOD_DEC_USE_COUNT;
 909 #endif
 910 
 911         return 0;
 912 }
 913 
 914 
 915 static int
 916 arcnet_send_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 917 {
 918         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
 919         int ioaddr=dev->base_addr;
 920 /*      short daddr;*/
 921 
 922         lp->intx++;
 923 
 924         BUGLVL(D_DURING)
 925                 printk("arcnet: transmit requested (status=%Xh, inTX=%d)\n",
 926                         inb(STATUS),lp->intx);
 927 
 928         if (dev->tbusy || lp->in_txhandler)
 929         {
 930                 /* If we get here, some higher level has decided we are broken.
 931                    There should really be a "kick me" function call instead. */
 932                 int tickssofar = jiffies - dev->trans_start;
 933                 int recbuf=lp->recbuf;
 934                 int status=inb(STATUS);
 935                 
 936                 /* resume any stopped tx's */
 937 #if 0
 938                 if (lp->txready && (inb(STATUS)&TXFREEflag))
 939                 {
 940                         printk("arcnet: kickme: starting a TX (status=%Xh)\n",
 941                                 inb(STATUS));
 942                         arcnet_go_tx(dev);
 943                         lp->intx--;
 944                         return 1;
 945                 }
 946 #endif
 947                 
 948                 if (tickssofar < 5) 
 949                 {
 950                         BUGLVL(D_DURING)
 951                                 printk("arcnet: premature kickme! (status=%Xh ticks=%d o.skb=%ph numsegs=%d segnum=%d\n",
 952                                         status,tickssofar,lp->outgoing.skb,
 953                                         lp->outgoing.numsegs,
 954                                         lp->outgoing.segnum);
 955                         lp->intx--;
 956                         return 1;
 957                 }
 958 
 959                 BUGLVL(D_INIT)
 960                         printk("arcnet: transmit timed out (status=%Xh, inTX=%d, tickssofar=%d)\n",
 961                                 status,lp->intx,tickssofar);
 962 
 963                 /* Try to restart the adaptor. */
 964                 /*arcnet_reset(dev);*/
 965                 
 966                 if (status&NORXflag) EnableReceiver();
 967                 if (!(status&TXFREEflag)) outb(NOTXcmd,COMMAND);
 968                 dev->trans_start = jiffies;
 969 
 970                 if (lp->outgoing.skb)
 971                         dev_kfree_skb(lp->outgoing.skb,FREE_WRITE);
 972                 lp->outgoing.skb=NULL;
 973 
 974                 dev->tbusy=0;
 975                 mark_bh(NET_BH);
 976                 lp->intx=0;
 977                 lp->in_txhandler=0;
 978                 lp->txready=0;
 979                 lp->sending=0;
 980 
 981                 return 1;
 982         }
 983 
 984         /* If some higher layer thinks we've missed a tx-done interrupt
 985            we are passed NULL. Caution: dev_tint() handles the cli()/sti()
 986            itself. */
 987         if (skb == NULL) {
 988                 BUGLVL(D_INIT)
 989                         printk("arcnet: tx passed null skb (status=%Xh, inTX=%d, tickssofar=%ld)\n",
 990                                 inb(STATUS),lp->intx,jiffies-dev->trans_start);
 991                 dev_tint(dev);
 992                 lp->intx--;
 993                 return 0;
 994         }
 995         
 996         if (lp->txready)        /* transmit already in progress! */
 997         {
 998                 printk("arcnet: trying to start new packet while busy!\n");
 999                 printk("arcnet: marking as not ready.\n");
1000                 lp->txready=0;
1001                 return 1;
1002         }
1003 
1004         /* Block a timer-based transmit from overlapping.  This could better be
1005            done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
1006         if (set_bit(0, (void*)&dev->tbusy) != 0)
1007         {
1008             printk("arcnet: transmitter called with busy bit set! (status=%Xh, inTX=%d, tickssofar=%ld)\n",
1009                         inb(STATUS),lp->intx,jiffies-dev->trans_start);
1010             lp->intx--;
1011             return -EBUSY;
1012         }
1013         else {
1014                 struct Outgoing *out=&(lp->outgoing);
1015                 out->length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
1016                 out->hdr=(struct ClientData*)skb->data;
1017                 out->skb=skb;
1018                 BUGLVL( D_DATA ) {
1019                         short i;
1020                         for( i=0; i< skb->len; i++)
1021                         {
1022                                 if( i%16 == 0 ) printk("\n[%04hX] ",i);
1023                                 printk("%02hX ",((unsigned char*)skb->data)[i]);
1024                         }
1025                         printk("\n");
1026                 }
1027 
1028 #ifdef IRQ_XMIT
1029                 if (lp->txready && inb(STATUS)&TXFREEflag)
1030                         arcnet_go_tx(dev);
1031 #endif
1032 
1033                 
1034                 if (out->length<=XMTU)  /* fits in one packet? */
1035                 {
1036                         BUGLVL(D_TX) printk("arcnet: not splitting %d-byte packet. (split_flag=%d)\n",
1037                                         out->length,out->hdr->split_flag);
1038                         BUGLVL(D_INIT) if (out->hdr->split_flag)
1039                                 printk("arcnet: short packet has split_flag set?! (split_flag=%d)\n",
1040                                         out->hdr->split_flag);
1041                         out->numsegs=1;
1042                         out->segnum=1;
1043                         arcnet_prepare_tx(dev,out->hdr,
1044                                 out->length-sizeof(struct ClientData),
1045                                 ((char *)skb->data)+sizeof(struct ClientData));
1046                         careful_xmit_wait(dev);
1047 
1048                         /* done right away */
1049                         dev_kfree_skb(out->skb,FREE_WRITE);
1050                         out->skb=NULL;
1051                                         
1052                         if (!lp->sending)
1053                         {
1054                                 arcnet_go_tx(dev);
1055                                 
1056                                 /* inform upper layers */
1057                                 dev->tbusy=0;
1058                                 mark_bh(NET_BH);
1059                         }
1060                 }
1061                 else                    /* too big for one - split it */
1062                 {
1063                         int maxsegsize=XMTU-sizeof(struct ClientData);
1064 
1065                         out->data=(u_char *)skb->data
1066                                         + sizeof(struct ClientData);
1067                         out->dataleft=out->length-sizeof(struct ClientData);
1068                         out->numsegs=(out->dataleft+maxsegsize-1)/maxsegsize;
1069                                 
1070                         out->segnum=0;
1071                                 
1072                         BUGLVL(D_TX) printk("arcnet: packet (%d bytes) split into %d fragments:\n",
1073                                 out->length,out->numsegs);
1074 
1075 #ifdef IRQ_XMIT
1076                         /* if a packet waiting, launch it */
1077                         if (lp->txready && inb(STATUS)&TXFREEflag)
1078                                 arcnet_go_tx(dev);
1079 
1080                         if (!lp->txready)
1081                         {
1082                                 /* prepare a packet, launch it and prepare
1083                                  * another.
1084                                  */
1085                                 arcnet_continue_tx(dev);
1086                                 if (!lp->sending)
1087                                 {
1088                                         arcnet_go_tx(dev);
1089                                         arcnet_continue_tx(dev);
1090                                         if (!lp->sending)
1091                                                 arcnet_go_tx(dev);
1092                                 }
1093                         }
1094                         
1095                         /* if segnum==numsegs, the transmission is finished;
1096                          * free the skb right away.
1097                          */
1098                         if (out->segnum==out->numsegs)
1099                         {
1100                                 /* transmit completed */
1101                                 out->segnum++;
1102                                 if (out->skb)
1103                                         dev_kfree_skb(out->skb,FREE_WRITE);
1104                                 out->skb=NULL;
1105 #if 0
1106                                 /* inform upper layers */
1107                                 dev->tbusy=0;
1108                                 mark_bh(NET_BH);
1109 #endif
1110                         }
1111                         
1112 #else /* non-irq xmit */
1113                         while (out->segnum<out->numsegs)
1114                         {
1115                                 arcnet_continue_tx(dev);
1116                                 careful_xmit_wait(dev);
1117                                 arcnet_go_tx(dev);
1118                                 dev->trans_start=jiffies;
1119                         }
1120 
1121                         dev_kfree_skb(out->skb,FREE_WRITE);
1122                         out->skb=NULL;
1123 
1124                         /* inform upper layers */
1125                         dev->tbusy = 0;
1126                         mark_bh(NET_BH);
1127 #endif
1128                 }
1129         }
1130 
1131         lp->intx--;
1132         lp->stats.tx_packets++;
1133         dev->trans_start=jiffies;
1134         return 0;
1135 }
1136 
1137 static void arcnet_continue_tx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1138 {
1139         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1140         int maxsegsize=XMTU-sizeof(struct ClientData);
1141         struct Outgoing *out=&(lp->outgoing);
1142         
1143         if (lp->txready)
1144         {
1145                 printk("arcnet: continue_tx: called with packet in buffer!\n");
1146                 return;
1147         }
1148 
1149         if (out->segnum>=out->numsegs)
1150         {
1151                 printk("arcnet: continue_tx: building segment %d of %d!\n",
1152                         out->segnum+1,out->numsegs);
1153         }
1154 
1155         if (!out->segnum)       /* first packet */
1156                 out->hdr->split_flag=((out->numsegs-2)<<1)+1;
1157         else
1158                 out->hdr->split_flag=out->segnum<<1;
1159 
1160         out->seglen=maxsegsize;
1161         if (out->seglen>out->dataleft) out->seglen=out->dataleft;
1162                         
1163         BUGLVL(D_TX) printk("arcnet: building packet #%d (%d bytes) of %d (%d total), splitflag=%d\n",
1164                 out->segnum+1,out->seglen,out->numsegs,
1165                 out->length,out->hdr->split_flag);
1166 
1167         arcnet_prepare_tx(dev,out->hdr,out->seglen,out->data);
1168                 
1169         out->dataleft-=out->seglen;
1170         out->data+=out->seglen;
1171         out->segnum++;
1172 }
1173 
1174 #ifdef CAREFUL_XMIT
1175 static void careful_xmit_wait(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1176 {
1177         int ioaddr=dev->base_addr;
1178         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1179 
1180         /* wait patiently for tx to become available again */
1181         while ( !(inb(STATUS)&TXFREEflag) )
1182         {
1183                 if (jiffies-dev->trans_start > 20 || !dev->tbusy)
1184                 {
1185                         BUGLVL(D_INIT)
1186                                 printk("arcnet: CAREFUL_XMIT timeout. (busy=%d, status=%Xh)\n",
1187                                         dev->tbusy,inb(STATUS));
1188                         lp->stats.tx_errors++;
1189                         
1190                         outb(NOTXcmd,COMMAND);
1191                         return;
1192                 }
1193         }
1194         BUGLVL(D_TX) printk("arcnet: transmit completed successfully. (status=%Xh)\n",
1195                                 inb(STATUS));
1196 }
1197 #endif
1198 
1199 static void
1200 arcnet_prepare_tx(struct device *dev,struct ClientData *hdr,short length,
     /* [previous][next][first][last][top][bottom][index][help] */
1201                 char *data)
1202 {
1203 /*      int ioaddr = dev->base_addr;*/
1204         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1205         struct ClientData *arcsoft;
1206         union ArcPacket *arcpacket = 
1207                 (union ArcPacket *)(dev->mem_start+512*(lp->txbuf^1));
1208         u_char pkttype;
1209         int offset;
1210         short daddr;
1211         
1212         lp->txbuf=lp->txbuf^1;  /* XOR with 1 to alternate between 2 and 3 */
1213         
1214         length+=sizeof(struct ClientData);
1215 
1216         BUGLVL(D_TX)
1217                 printk("arcnet: arcnet_prep_tx: hdr:%ph, length:%d, data:%ph\n",
1218                         hdr,length,data);
1219 
1220         /* clean out the page to make debugging make more sense :) */
1221         BUGLVL(D_DURING)
1222                 memset((void *)dev->mem_start+lp->txbuf*512,0x42,512);
1223 
1224         daddr=arcpacket->hardheader.destination=hdr->daddr;
1225 
1226         /* load packet into shared memory */
1227         if (length<=MTU)        /* Normal (256-byte) Packet */
1228         {
1229                 pkttype=NORMAL;
1230                         
1231                 arcpacket->hardheader.offset1=offset=256-length
1232                                 + EXTRA_CLIENTDATA;
1233                 arcsoft=(struct ClientData *)
1234                         (&arcpacket->raw[offset-EXTRA_CLIENTDATA]);
1235         }
1236         else if (length>=MinTU) /* Extended (512-byte) Packet */
1237         {
1238                 pkttype=EXTENDED;
1239                 
1240                 arcpacket->hardheader.offset1=0;
1241                 arcpacket->hardheader.offset2=offset=512-length
1242                         + EXTRA_CLIENTDATA;
1243                 arcsoft=(struct ClientData *)
1244                         (&arcpacket->raw[offset-EXTRA_CLIENTDATA]);
1245         }
1246         else                            /* Exception Packet */
1247         {
1248                 pkttype=EXCEPTION;
1249                 
1250                 arcpacket->hardheader.offset1=0;
1251                 arcpacket->hardheader.offset2=offset=512-length-4
1252                         + EXTRA_CLIENTDATA;
1253                 arcsoft=(struct ClientData *)
1254                         (&arcpacket->raw[offset+4-EXTRA_CLIENTDATA]);
1255                 
1256                 /* exception-specific stuff - these four bytes
1257                  * make the packet long enough to fit in a 512-byte
1258                  * frame.
1259                  */
1260                 arcpacket->raw[offset+0]=hdr->protocol_id;
1261                 arcpacket->raw[offset+1]=0xFF; /* FF flag */
1262                         arcpacket->raw[offset+2]=0xFF; /* FF padding */
1263                         arcpacket->raw[offset+3]=0xFF; /* FF padding */
1264         }
1265 
1266 
1267         /* copy the packet into ARCnet shmem
1268          *  - the first bytes of ClientData header are skipped
1269          */
1270         memcpy((u_char*)arcsoft+EXTRA_CLIENTDATA,
1271                 (u_char*)hdr+EXTRA_CLIENTDATA,
1272                 sizeof(struct ClientData)-EXTRA_CLIENTDATA);
1273         memcpy((u_char*)arcsoft+sizeof(struct ClientData),
1274                 data,
1275                 length-sizeof(struct ClientData));
1276                 
1277         BUGLVL(D_DURING) printk("arcnet: transmitting packet to station %02Xh (%d bytes, type=%d)\n",
1278                         daddr,length,pkttype);
1279                         
1280         BUGLVL(D_TX)
1281         {
1282                 int countx,county;
1283                 
1284                 printk("arcnet: packet dump [tx] follows:");
1285 
1286                 for (county=0; county<16+(pkttype!=NORMAL)*16; county++)
1287                 {
1288                         printk("\n[%04X] ",county*16);
1289                         for (countx=0; countx<16; countx++)
1290                                 printk("%02X ",
1291                                         arcpacket->raw[county*16+countx]);
1292                 }
1293                 
1294                 printk("\n");
1295         }
1296 #ifdef CAREFUL_XMIT
1297  #if 0
1298         careful_xmit_wait(dev);
1299 
1300         /* if we're not broadcasting, make sure the xmit was ack'd.
1301          * if it wasn't, there is probably no card with that
1302          * address... or else it missed our tx somehow.
1303          */
1304         if (daddr && !(inb(STATUS)&TXACKflag))
1305         {       
1306                 BUGLVL(D_INIT)
1307                         printk("arcnet: transmit not acknowledged. (status=%Xh, daddr=%02Xh)\n",
1308                                 inb(STATUS),daddr);
1309                 lp->stats.tx_errors++;
1310                 return -ENONET; /* "machine is not on the network" */
1311         }
1312  #endif
1313 #endif
1314         lp->txready=lp->txbuf;  /* packet is ready for sending */
1315 
1316 #if 0
1317 #ifdef IRQ_XMIT
1318         if (inb(STATUS)&TXFREEflag) arcnet_go_tx(dev);
1319 #endif
1320 #endif
1321 }
1322 
1323 static void
1324 arcnet_go_tx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1325 {
1326         struct arcnet_local *lp=(struct arcnet_local *)dev->priv;
1327         int ioaddr=dev->base_addr;
1328 
1329         BUGLVL(D_DURING)
1330                 printk("arcnet: go_tx: status=%Xh\n",
1331                         inb(STATUS));
1332         
1333         if (!(inb(STATUS)&TXFREEflag) || !lp->txready) return;
1334 
1335         /* start sending */
1336         outb(TXcmd|(lp->txready<<3),COMMAND);
1337 
1338 #ifdef IRQ_XMIT
1339         outb(TXFREEflag|NORXflag,INTMASK);
1340 #endif
1341 
1342         dev->trans_start = jiffies;
1343         lp->txready=0;
1344         lp->sending++;
1345 }
1346 
1347 
1348 /* The typical workload of the driver:
1349    Handle the network interface interrupts. */
1350 static void
1351 arcnet_interrupt(int irq,struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
1352 {
1353         struct device *dev = (struct device *)(irq2dev_map[irq]);
1354 
1355         if (dev == NULL) {
1356                 if (net_debug >= D_DURING)
1357                         printk("arcnet: irq %d for unknown device.\n", irq);
1358                 return;
1359         }
1360         
1361         arcnet_inthandler(dev);
1362 }
1363 
1364 static void
1365 arcnet_inthandler(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1366 {       
1367         struct arcnet_local *lp;
1368         int ioaddr, status, boguscount = 3, didsomething;
1369         
1370         dev->interrupt = 1;
1371         sti();
1372 
1373         ioaddr = dev->base_addr;
1374         lp = (struct arcnet_local *)dev->priv;
1375 
1376 #ifdef IRQ_XMIT
1377         outb(0,INTMASK);
1378 #endif
1379         
1380         BUGLVL(D_DURING)
1381                 printk("arcnet: in net_interrupt (status=%Xh)\n",inb(STATUS));
1382 
1383         do
1384         {
1385                 status = inb(STATUS);
1386                 didsomething=0;
1387         
1388                 if (!dev->start)
1389                 {
1390                         BUGLVL(D_EXTRA)
1391                                 printk("arcnet: ARCnet not yet initialized.  irq ignored. (status=%Xh)\n",
1392                                         status);
1393 #ifdef IRQ_XMIT
1394                         if (!(status&NORXflag))
1395                                 outb(NORXflag,INTMASK);
1396 #endif
1397                         dev->interrupt=0;
1398                         return;
1399                 }
1400         
1401                 /* RESET flag was enabled - card is resetting and if RX
1402                  * is disabled, it's NOT because we just got a packet.
1403                  */
1404                 if (status & RESETflag)
1405                 {
1406                         BUGLVL(D_INIT)
1407                                 printk("arcnet: reset irq (status=%Xh)\n",
1408                                         status);
1409                         dev->interrupt=0;
1410                         return;
1411                 }
1412 
1413 #if 1   /* yes, it's silly to disable this part but it makes good testing */
1414                 /* RX is inhibited - we must have received something. */
1415                 if (status & NORXflag)
1416                 {
1417                         int recbuf=lp->recbuf=!lp->recbuf;
1418 
1419                         BUGLVL(D_DURING)
1420                                 printk("arcnet: receive irq (status=%Xh)\n",
1421                                         status);
1422 
1423                         /* enable receive of our next packet */
1424                         EnableReceiver();
1425                 
1426                         /* Got a packet. */
1427                         arcnet_rx(dev,!recbuf);
1428                         
1429                         didsomething++;
1430                 }
1431 #endif
1432 #ifdef IRQ_XMIT
1433                 /* it can only be an xmit-done irq if we're xmitting :) */
1434                 if (status&TXFREEflag && !lp->in_txhandler && lp->sending)
1435                 {
1436                         struct Outgoing *out=&(lp->outgoing);
1437                         
1438                         lp->in_txhandler++;
1439                         lp->sending--;
1440                         
1441                         BUGLVL(D_DURING)
1442                                 printk("arcnet: TX IRQ (stat=%Xh, numsegs=%d, segnum=%d, skb=%ph)\n",
1443                                         status,out->numsegs,out->segnum,out->skb);
1444                                         
1445                         /* send packet if there is one */
1446                         if (lp->txready)
1447                         {
1448                                 arcnet_go_tx(dev);
1449                                 didsomething++;
1450                         }
1451                         
1452                         if (lp->intx)
1453                         {
1454                                 lp->in_txhandler--;
1455                                 continue;
1456                         }
1457 
1458                         if (!lp->outgoing.skb)
1459                         {
1460                                 BUGLVL(D_DURING)
1461                                         printk("arcnet: TX IRQ done: no split to continue.\n");
1462                                 
1463                                 /* inform upper layers */
1464                                 if (!lp->txready && dev->tbusy)
1465                                 {
1466                                         dev->tbusy=0;
1467                                         mark_bh(NET_BH);
1468                                 }
1469                                 
1470                                 lp->in_txhandler--;
1471                                 continue;
1472                         }
1473                         
1474                         /*lp->stats.tx_packets++;*/
1475                         
1476                         /* if more than one segment, and not all segments
1477                          * are done, then continue xmit.
1478                          */
1479                         if (out->segnum<out->numsegs)
1480                                 arcnet_continue_tx(dev);
1481                         if (lp->txready && !lp->sending)
1482                                 arcnet_go_tx(dev);
1483 
1484                         /* if segnum==numsegs, the transmission is finished;
1485                          * free the skb.
1486                          */
1487                         if (out->segnum>=out->numsegs)
1488                         {
1489                                 /* transmit completed */
1490                                 out->segnum++;
1491                                 if (out->skb)
1492                                         dev_kfree_skb(out->skb,FREE_WRITE);
1493                                 out->skb=NULL;
1494 
1495                                 /* inform upper layers */
1496                                 if (!lp->txready && dev->tbusy)
1497                                 {
1498                                         dev->tbusy=0;
1499                                         mark_bh(NET_BH);
1500                                 }
1501                         }
1502                         didsomething++;
1503                         
1504                         lp->in_txhandler--;
1505                 }
1506 #endif /* IRQ_XMIT */
1507         } while (--boguscount && didsomething);
1508 
1509         BUGLVL(D_DURING)
1510                 printk("arcnet: net_interrupt complete (status=%Xh)\n",
1511                         inb(STATUS));
1512 
1513 #ifdef IRQ_XMIT
1514         if (dev->start && lp->sending )
1515                 outb(NORXflag|TXFREEflag,INTMASK);
1516         else
1517                 outb(NORXflag,INTMASK);
1518 #endif
1519 
1520         dev->interrupt=0;
1521 }
1522 
1523 /* A packet has arrived; grab it from the buffers and possibly unsplit it.
1524  */
1525 static void
1526 arcnet_rx(struct device *dev,int recbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
1527 {
1528         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1529         int ioaddr = dev->base_addr;
1530 /*      int status = inb(STATUS);*/
1531 
1532         struct sk_buff *skb;
1533 
1534         union ArcPacket *arcpacket=
1535                 (union ArcPacket *)(dev->mem_start+recbuf*512);
1536         struct ClientData *soft,*arcsoft;
1537         short length,offset;
1538         u_char pkttype,daddr,saddr;
1539 
1540         daddr=arcpacket->hardheader.destination;
1541         saddr=arcpacket->hardheader.source;
1542         
1543         /* if source is 0, it's not a "used" packet! */
1544         if (saddr==0)
1545         {
1546                 /*BUGLVL(D_DURING)*/
1547                         printk("arcnet: discarding old packet. (status=%Xh)\n",
1548                                 inb(STATUS));
1549                 lp->stats.rx_errors++;
1550                 return;
1551         }
1552         arcpacket->hardheader.source=0;
1553         
1554         if (arcpacket->hardheader.offset1) /* Normal Packet */
1555         {
1556                 offset=arcpacket->hardheader.offset1;
1557                 arcsoft=(struct ClientData *)
1558                         (&arcpacket->raw[offset-EXTRA_CLIENTDATA]);
1559                 length=256-offset+EXTRA_CLIENTDATA;
1560                 pkttype=NORMAL;
1561         }
1562         else            /* ExtendedPacket or ExceptionPacket */
1563         {
1564                 offset=arcpacket->hardheader.offset2;
1565                 arcsoft=(struct ClientData *)
1566                         (&arcpacket->raw[offset-EXTRA_CLIENTDATA]);
1567 
1568                 if (arcsoft->split_flag!=0xFF)  /* Extended Packet */
1569                 {
1570                         length=512-offset+EXTRA_CLIENTDATA;
1571                         pkttype=EXTENDED;
1572                 }
1573                 else                            /* Exception Packet */
1574                 {
1575                         /* skip over 4-byte junkola */
1576                         arcsoft=(struct ClientData *)
1577                                 ((u_char *)arcsoft + 4);
1578                         length=512-offset+EXTRA_CLIENTDATA-4;
1579                         pkttype=EXCEPTION;
1580                 }
1581         }
1582         
1583         if (!arcsoft->split_flag)               /* not split */
1584         {
1585                 struct Incoming *in=&lp->incoming[saddr];
1586 
1587                 BUGLVL(D_RX) printk("arcnet: incoming is not split (splitflag=%d)\n",
1588                         arcsoft->split_flag);
1589                         
1590         if (in->skb)    /* already assembling one! */
1591                 {
1592                         BUGLVL(D_INIT) printk("arcnet: aborting assembly (seq=%d) for unsplit packet (splitflag=%d, seq=%d)\n",
1593                                 in->sequence,arcsoft->split_flag,
1594                                 arcsoft->sequence);
1595                         kfree_skb(in->skb,FREE_WRITE);
1596                         in->skb=NULL;
1597                 }
1598                 
1599                 in->sequence=arcsoft->sequence;
1600 
1601                 skb = alloc_skb(length, GFP_ATOMIC);
1602                 if (skb == NULL) {
1603                         printk("%s: Memory squeeze, dropping packet.\n", 
1604                                 dev->name);
1605                         lp->stats.rx_dropped++;
1606                         return;
1607                 }
1608                 soft=(struct ClientData *)skb->data;
1609                 
1610                 skb->len = length;
1611                 skb->dev = dev;
1612                 
1613                 memcpy((u_char *)soft+EXTRA_CLIENTDATA,
1614                         (u_char *)arcsoft+EXTRA_CLIENTDATA,
1615                         length-EXTRA_CLIENTDATA);
1616                 soft->daddr=daddr;
1617                 soft->saddr=saddr;
1618                 
1619                 BUGLVL(D_DURING)
1620                         printk("arcnet: received packet from %02Xh to %02Xh (%d bytes, type=%d)\n",
1621                                 saddr,daddr,length,pkttype);
1622                 BUGLVL(D_RX)
1623                 {
1624                         int countx,county;
1625                         
1626                         printk("arcnet: packet dump [rx-unsplit] follows:");
1627 
1628                         for (county=0; county<16+(pkttype!=NORMAL)*16; county++)
1629                         {
1630                                 printk("\n[%04X] ",county*16);
1631                                 for (countx=0; countx<16; countx++)
1632                                         printk("%02X ",
1633                                                 arcpacket->raw[county*16+countx]);
1634                         }
1635                         
1636                         printk("\n");
1637                 }
1638 
1639                 /* ARP packets have problems when sent from DOS.
1640                  * source address is always 0!  So we take the hardware
1641                  * source addr (which is impossible to fumble) and insert
1642                  * it ourselves.
1643                  */
1644                 if (soft->protocol_id == ARC_P_ARP)
1645                 {
1646                         struct arphdr *arp=(struct arphdr *)
1647                                         ((char *)soft+sizeof(struct ClientData));
1648 
1649                         /* make sure addresses are the right length */
1650                         if (arp->ar_hln==1 && arp->ar_pln==4)
1651                         {
1652                                 char *cptr=(char *)(arp)+sizeof(struct arphdr);
1653                                 
1654                                 if (!*cptr)     /* is saddr = 00? */
1655                                 {
1656                                         BUGLVL(D_DURING)
1657                                                 printk("arcnet: ARP source address was 00h, set to %02Xh.\n",
1658                                                         saddr);
1659                                         *cptr=saddr;
1660                                 }
1661                                 else BUGLVL(D_DURING) 
1662                                 {
1663                                         printk("arcnet: ARP source address (%Xh) is fine.\n",
1664                                                 *cptr);
1665                                 }
1666                         }
1667                         else
1668                         {
1669                                 printk("arcnet: funny-shaped ARP packet. (%Xh, %Xh)\n",
1670                                         arp->ar_hln,arp->ar_pln);
1671                         }
1672                 }
1673                 BUGLVL( D_DATA ) {
1674                     short i;
1675                         for( i=0; i< skb->len; i++)
1676                         {
1677                                 if( i%16 == 0 ) printk("\n[%04hX] ",i);
1678                                 printk("%02hX ",((unsigned char*)skb->data)[i]);
1679                         }
1680                         printk("\n");
1681                 }
1682                 netif_rx(skb);
1683                 lp->stats.rx_packets++;
1684          }
1685          else                   /* split packet */
1686          {
1687                  /* NOTE:  MSDOS ARP packet correction should only need to
1688                   * apply to unsplit packets, since ARP packets are so short.
1689                   *
1690                   * My interpretation of the RFC1201 (ARCnet) document is that
1691                   * if a packet is received out of order, the entire assembly
1692                   * process should be aborted.
1693                   *
1694                   * The RFC also mentions "it is possible for successfully
1695                   * received packets to be retransmitted."  As of 0.40 all
1696                   * previously received packets are allowed, not just the
1697                   * most recent one.
1698                   *
1699                   * We allow multiple assembly processes, one for each
1700                   * ARCnet card possible on the network.  Seems rather like
1701                   * a waste of memory.  Necessary?
1702                   */
1703                   
1704                 struct Incoming *in=&lp->incoming[saddr];
1705                 
1706                 BUGLVL(D_RX) printk("arcnet: packet is split (splitflag=%d, seq=%d)\n",
1707                         arcsoft->split_flag,in->sequence);
1708 
1709                 if (in->skb && in->sequence!=arcsoft->sequence)
1710                 {
1711                         BUGLVL(D_INIT) printk("arcnet: wrong seq number, aborting assembly (expected=%d, seq=%d, splitflag=%d)\n",      
1712                                 in->sequence,arcsoft->sequence,
1713                                 arcsoft->split_flag);
1714                         kfree_skb(in->skb,FREE_WRITE);
1715                         in->skb=NULL;
1716                         in->lastpacket=in->numpackets=0;
1717                 }
1718                   
1719                 if (arcsoft->split_flag & 1)    /* first packet in split */
1720                 {
1721                         BUGLVL(D_RX) printk("arcnet: brand new splitpacket (splitflag=%d)\n",
1722                                 arcsoft->split_flag);
1723                         if (in->skb)    /* already assembling one! */
1724                         {
1725                                 BUGLVL(D_INIT) printk("arcnet: aborting previous (seq=%d) assembly (splitflag=%d, seq=%d)\n",
1726                                         in->sequence,arcsoft->split_flag,
1727                                         arcsoft->sequence);
1728                                 kfree_skb(in->skb,FREE_WRITE);
1729                         }
1730 
1731                         in->sequence=arcsoft->sequence;
1732                         in->numpackets=((unsigned)arcsoft->split_flag>>1)+2;
1733                         in->lastpacket=1;
1734                         
1735                         if (in->numpackets>16)
1736                         {
1737                                 printk("arcnet: incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
1738                                         arcsoft->split_flag);
1739                                 lp->stats.rx_dropped++;
1740                                 return;
1741                         }
1742                 
1743                         in->skb=skb=alloc_skb(508*in->numpackets
1744                                         + sizeof(struct ClientData),
1745                                         GFP_ATOMIC);
1746                         if (skb == NULL) {
1747                                 printk("%s: (split) memory squeeze, dropping packet.\n", 
1748                                         dev->name);
1749                                 lp->stats.rx_dropped++;
1750                                 return;
1751                         }
1752                                         
1753                         /* I don't know what this is for, but it DOES avoid
1754                          * warnings...
1755                          */
1756                         skb->free=1;
1757                         
1758                         soft=(struct ClientData *)skb->data;
1759                         
1760                         skb->len=sizeof(struct ClientData);
1761                         skb->dev=dev;
1762 
1763                         memcpy((u_char *)soft+EXTRA_CLIENTDATA,
1764                                 (u_char *)arcsoft+EXTRA_CLIENTDATA,
1765                                 sizeof(struct ClientData)-EXTRA_CLIENTDATA);
1766                         soft->split_flag=0; /* final packet won't be split */
1767                 }
1768                 else                    /* not first packet */
1769                 {
1770                         int packetnum=((unsigned)arcsoft->split_flag>>1) + 1;
1771 
1772                         /* if we're not assembling, there's no point
1773                          * trying to continue.
1774                          */                     
1775                         if (!in->skb)
1776                         {
1777                                 BUGLVL(D_INIT) printk("arcnet: can't continue split without starting first! (splitflag=%d, seq=%d)\n",
1778                                         arcsoft->split_flag,arcsoft->sequence);
1779                                 return;
1780                         }
1781 
1782                         in->lastpacket++;
1783                         if (packetnum!=in->lastpacket) /* not the right flag! */
1784                         {
1785                                 /* harmless duplicate? ignore. */
1786                                 if (packetnum<=in->lastpacket-1)
1787                                 {
1788                                         BUGLVL(D_INIT) printk("arcnet: duplicate splitpacket ignored! (splitflag=%d)\n",
1789                                                 arcsoft->split_flag);
1790                                         return;
1791                                 }
1792                                 
1793                                 /* "bad" duplicate, kill reassembly */
1794                                 BUGLVL(D_INIT) printk("arcnet: out-of-order splitpacket, reassembly (seq=%d) aborted (splitflag=%d, seq=%d)\n", 
1795                                         in->sequence,arcsoft->split_flag,
1796                                         arcsoft->sequence);
1797                                 kfree_skb(in->skb,FREE_WRITE);
1798                                 in->skb=NULL;
1799                                 in->lastpacket=in->numpackets=0;
1800                                 return;
1801                         }
1802 
1803                         soft=(struct ClientData *)in->skb->data;
1804                 }
1805                 
1806                 skb=in->skb;
1807                 
1808                 memcpy(skb->data+skb->len,
1809                        (u_char *)arcsoft+sizeof(struct ClientData),
1810                        length-sizeof(struct ClientData));
1811 
1812                 skb->len+=length-sizeof(struct ClientData);
1813                 
1814                 soft->daddr=daddr;
1815                 soft->saddr=saddr;
1816                 
1817                 BUGLVL(D_DURING)
1818                         printk("arcnet: received packet from %02Xh to %02Xh (%d bytes, type=%d)\n",
1819                                 saddr,daddr,length,pkttype);
1820                 BUGLVL(D_RX)
1821                 {
1822                         int countx,county;
1823                         
1824                         printk("arcnet: packet dump [rx-split] follows:");
1825 
1826                         for (county=0; county<16+(pkttype!=NORMAL)*16; county++)
1827                         {
1828                                 printk("\n[%04X] ",county*16);
1829                                 for (countx=0; countx<16; countx++)
1830                                         printk("%02X ",
1831                                                 arcpacket->raw[county*16+countx]);
1832                         }
1833                         
1834                         printk("\n");
1835                 }
1836                 
1837                 /* are we done? */
1838                 if (in->lastpacket == in->numpackets)
1839                 {
1840                         if (!skb || !in->skb)
1841                                 printk("arcnet: ?!? done reassembling packet, no skb? (skb=%ph, in->skb=%ph)\n",
1842                                         skb,in->skb);
1843                         in->skb=NULL;
1844                         in->lastpacket=in->numpackets=0;
1845                         BUGLVL( D_DATA ) {
1846                             short i;
1847                                 for( i=0; i< skb->len; i++)
1848                                 {
1849                                         if( i%16 == 0 ) printk("\n[%04hX] ",i);
1850                                         printk("%02hX ",((unsigned char*)skb->data)[i]);
1851                                 }
1852                                 printk("\n");
1853                         }
1854                         netif_rx(skb);
1855                         lp->stats.rx_packets++;
1856                 }
1857          }
1858         
1859         /* If any worth-while packets have been received, dev_rint()
1860            has done a mark_bh(NET_BH) for us and will work on them
1861            when we get to the bottom-half routine. */
1862         /* arcnet: pardon? */
1863 }
1864 
1865 
1866 #ifdef USE_TIMER_HANDLER
1867 /* this function is called every once in a while to make sure the ARCnet
1868  * isn't stuck.
1869  *
1870  * If we miss a receive IRQ, the receiver (and IRQ) is permanently disabled
1871  * and we might never receive a packet again!  This will check if this
1872  * is the case, and if so, re-enable the receiver.
1873  */
1874 static void
1875 arcnet_timer(unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1876 {
1877         struct device *dev=(struct device *)arg;
1878         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1879         short ioaddr=dev->base_addr;
1880         int status=inb(STATUS);
1881 
1882         /* if we didn't interrupt the IRQ handler, and RX's are still
1883          * disabled, and we're not resetting the card... then we're stuck!
1884          */
1885         if (!dev->interrupt && dev->start
1886           && status&NORXflag && !status&RESETflag)
1887         {
1888                 BUGLVL(D_INIT)
1889                         printk("arcnet: timer: ARCnet was stuck!  (status=%Xh)\n",
1890                                 status);
1891                 
1892                 arcnet_inthandler(dev);
1893         }
1894 
1895         /* requeue ourselves */
1896         init_timer(&lp->timer);
1897         lp->timer.expires=TIMERval;
1898         add_timer(&lp->timer);
1899 }
1900 #endif
1901 
1902 /* Get the current statistics.  This may be called with the card open or
1903    closed. */
1904 static struct enet_statistics *
1905 arcnet_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1906 {
1907         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1908 /*      short ioaddr = dev->base_addr;*/
1909 
1910         return &lp->stats;
1911 }
1912 
1913 /* Set or clear the multicast filter for this adaptor.
1914    num_addrs == -1      Promiscuous mode, receive all packets
1915    num_addrs == 0       Normal mode, clear multicast list
1916    num_addrs > 0        Multicast mode, receive normal and MC packets, and do
1917                         best-effort filtering.
1918  */
1919 static void
1920 set_multicast_list(struct device *dev, int num_addrs, void *addrs)
     /* [previous][next][first][last][top][bottom][index][help] */
1921 {
1922 #if 0     /* no promiscuous mode at all */
1923         struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
1924 
1925         short ioaddr = dev->base_addr;
1926         if (num_addrs) {
1927                 outw(69, ioaddr);               /* Enable promiscuous mode */
1928         } else
1929                 outw(99, ioaddr);               /* Disable promiscuous mode, use normal mode */
1930 #endif
1931 }
1932 
1933 int arcnet_reset(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1934 {
1935         struct arcnet_local *lp=(struct arcnet_local *)dev->priv;
1936         short ioaddr=dev->base_addr;
1937         int delayval,recbuf=lp->recbuf;
1938         
1939         outb(0,INTMASK);        /* no IRQ's, please! */
1940         
1941         BUGLVL(D_INIT)
1942                 printk("arcnet: Resetting %s (status=%Xh)\n",
1943                         dev->name,inb(STATUS));
1944 
1945         inb(RESET);             /* Reset by reading this port */
1946         JIFFER(RESETtime);
1947 
1948         outb(CFLAGScmd|RESETclear, COMMAND); /* clear flags & end reset */
1949         outb(CFLAGScmd|CONFIGclear,COMMAND);
1950 
1951         /* after a reset, the first byte of shared mem is TESTvalue and the
1952          * second byte is our 8-bit ARCnet address
1953          */
1954         {
1955                 u_char *cardmem = (u_char *) dev->mem_start;
1956                 if (cardmem[0] != TESTvalue)
1957                 {
1958                         BUGLVL(D_INIT)
1959                                 printk("arcnet: reset failed: TESTvalue not present.\n");
1960                         return 1;
1961                 }
1962                 lp->arcnum=cardmem[1];  /* save address for later use */
1963         }
1964         
1965         /* clear out status variables */
1966         recbuf=lp->recbuf=0;
1967         lp->txbuf=2;
1968         /*dev->tbusy=0;*/
1969 
1970         /* enable extended (512-byte) packets */
1971         outb(CONFIGcmd|EXTconf,COMMAND);
1972         XJIFFER(ACKtime);
1973         
1974         /* clean out all the memory to make debugging make more sense :) */
1975         BUGLVL(D_DURING)
1976                 memset((void *)dev->mem_start,0x42,2048);
1977         
1978         /* and enable receive of our first packet to the first buffer */
1979         EnableReceiver();
1980 
1981         /* re-enable interrupts */
1982         outb(NORXflag,INTMASK);
1983         
1984         /* done!  return success. */
1985         return 0;
1986 }
1987 
1988 
1989 /*
1990  *       Create the ARCnet ClientData header for an arbitrary protocol layer
1991  *
1992  *      saddr=NULL      means use device source address (always will anyway)
1993  *      daddr=NULL      means leave destination address (eg unresolved arp)
1994  */
1995 int arc_header(unsigned char *buff,struct device *dev,unsigned short type,
     /* [previous][next][first][last][top][bottom][index][help] */
1996                 void *daddr,void *saddr,unsigned len,struct sk_buff *skb)
1997 {
1998         struct ClientData *head = (struct ClientData *)buff;
1999         struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
2000 
2001         /* set the protocol ID according to RFC-1201 */
2002         switch(type)
2003         {
2004         case ETH_P_IP:
2005                 head->protocol_id=ARC_P_IP;
2006                 break;
2007         case ETH_P_ARP:
2008                 head->protocol_id=ARC_P_ARP;
2009                 break;
2010         case ETH_P_RARP:
2011                 head->protocol_id=ARC_P_RARP;
2012                 break;
2013         case ETH_P_IPX:
2014                 head->protocol_id=ARC_P_IPX;
2015                 break;
2016         case ETH_P_ATALK:
2017                 head->protocol_id=ARC_P_ATALK;
2018                 break;
2019         default:
2020                 printk("arcnet: I don't understand protocol %d (%Xh)\n",
2021                         type,type);
2022                 return 0;
2023         }       
2024 
2025 #if 1
2026         /*
2027          *      Set the source hardware address.
2028          *      AVE: we can't do this, so we don't.  Code below is directly
2029          *           stolen from eth.c driver and won't work.
2030          ** TM: but for debugging I would like to have saddr in the header
2031          */
2032         if(saddr)
2033                 head->saddr=((u_char*)saddr)[0];
2034         else
2035                 head->saddr=((u_char*)(dev->dev_addr))[0];
2036 #endif
2037 
2038 #if 0
2039         /*
2040          *      Anyway, the loopback-device should never use this function... 
2041          *
2042          *      And the chances of it using the ARCnet version of it are so
2043          *      tiny that I don't think we have to worry :)
2044          */
2045         if (dev->flags & IFF_LOOPBACK) 
2046         {
2047                 head->daddr=0;
2048                 return(dev->hard_header_len);
2049         }
2050 #endif
2051 
2052         head->split_flag=0;     /* split packets are done elsewhere */
2053         head->sequence=(lp->sequence++);
2054 
2055         /* supposedly if daddr is NULL, we should ignore it... */
2056         if(daddr)
2057         {
2058                 head->daddr=((u_char*)daddr)[0];
2059                 return dev->hard_header_len;
2060         }
2061         else
2062                 head->daddr=0;  /* better fill one in anyway */
2063                 
2064         return -dev->hard_header_len;
2065 }
2066 
2067 
2068 /*
2069  *      Rebuild the ARCnet ClientData header. This is called after an ARP
2070  *      (or in future other address resolution) has completed on this
2071  *      sk_buff. We now let ARP fill in the other fields.
2072  */
2073 int arc_rebuild_header(void *buff,struct device *dev,unsigned long dst,
     /* [previous][next][first][last][top][bottom][index][help] */
2074                 struct sk_buff *skb)
2075 {
2076         struct ClientData *head = (struct ClientData *)buff;
2077 
2078         /*
2079          *      Only ARP/IP is currently supported
2080          */
2081          
2082         if(head->protocol_id != ARC_P_IP) 
2083         {
2084                 printk("arcnet: I don't understand resolve type %d (%Xh) addresses!\n",
2085                         head->protocol_id,head->protocol_id);
2086                 head->daddr=0;
2087                 /*memcpy(eth->h_source, dev->dev_addr, dev->addr_len);*/
2088                 return 0;
2089         }
2090 
2091         /*
2092          *      Try and get ARP to resolve the header.
2093          */
2094 #ifdef CONFIG_INET       
2095         return arp_find(&(head->daddr), dst, dev, dev->pa_addr, skb)? 1 : 0;
2096 #else
2097         return 0;       
2098 #endif  
2099 }
2100                 
2101 /*
2102  *      Determine the packet's protocol ID.
2103  *
2104  *      With ARCnet we have to convert everything to Ethernet-style stuff.
2105  */
2106 unsigned short arc_type_trans(struct sk_buff *skb,struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2107 {
2108         struct ClientData *head = (struct ClientData *) skb->data;
2109         /*unsigned char *rawp;*/
2110         
2111         if (head->daddr==0)
2112                 skb->pkt_type=PACKET_BROADCAST;
2113                 
2114 #if 0 /* code for ethernet with multicast */
2115         if(*eth->h_dest&1)
2116         {
2117                 if(memcmp(eth->h_dest,dev->broadcast, ETH_ALEN)==0)
2118                         skb->pkt_type=PACKET_BROADCAST;
2119                 else
2120                         skb->pkt_type=PACKET_MULTICAST;
2121         }
2122 #endif
2123         
2124         if(dev->flags&IFF_PROMISC)
2125         {
2126                 /* if we're not sending to ourselves :) */
2127                 if (head->daddr != dev->dev_addr[0])
2128                         skb->pkt_type=PACKET_OTHERHOST;
2129         }
2130         
2131         /* now return the protocol number */
2132         switch (head->protocol_id)
2133         {
2134         case ARC_P_IP:          return htons(ETH_P_IP);
2135         case ARC_P_ARP:         return htons(ETH_P_ARP);
2136         case ARC_P_RARP:        return htons(ETH_P_RARP);
2137         case ARC_P_IPX:         return htons(ETH_P_IPX);
2138         case ARC_P_ATALK:   return htons(ETH_P_ATALK); /* appletalk, not tested */
2139         case ARC_P_LANSOFT: /* don't understand.  fall through. */
2140         default:
2141                 BUGLVL(D_DURING)
2142                         printk("arcnet: received packet of unknown protocol id %d (%Xh)\n",
2143                                 head->protocol_id,head->protocol_id);
2144                 return 0;
2145         }
2146 
2147 #if 0 /* more ethernet-specific junk */
2148         if (ntohs(eth->h_proto) >= 1536)
2149                 return eth->h_proto;
2150                 
2151         rawp = (unsigned char *)(eth + 1);
2152         
2153         if (*(unsigned short *)rawp == 0xFFFF)
2154                 return htons(ETH_P_802_3);
2155         if (*(unsigned short *)rawp == 0xAAAA)
2156                 return htons(ETH_P_SNAP);
2157                 
2158         return htons(ETH_P_802_2);
2159 #endif
2160 
2161         return htons(ETH_P_IP);
2162 }
2163 
2164 #ifdef MODULE
2165 char kernel_version[] = UTS_RELEASE;
2166 static struct device thisARCnet = {
2167   "      ",/* if blank, device name inserted by /linux/drivers/net/net_init.c */
2168   0, 0, 0, 0,
2169   0, 0,  /* I/O address, IRQ */
2170   0, 0, 0, NULL, arcnet_probe };
2171         
2172         
2173 int io=0x0;     /* <--- EDIT THESE LINES FOR YOUR CONFIGURATION */
2174 int irqnum=0;   /* or use the insmod io= irq= shmem= options */
2175 int shmem=0;
2176 int num=0;      /* number of device (ie for arc0, arc1, arc2...) */
2177 
2178 int
2179 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2180 {
2181         sprintf(thisARCnet.name,"arc%d",num);
2182 
2183         thisARCnet.base_addr=io;
2184 
2185         thisARCnet.irq=irqnum;
2186         if (thisARCnet.irq==2) thisARCnet.irq=9;
2187 
2188         if (shmem)
2189         {
2190                 thisARCnet.mem_start=shmem;
2191                 thisARCnet.mem_end=thisARCnet.mem_start+512*4-1;
2192                 thisARCnet.rmem_start=thisARCnet.mem_start+512*0;
2193                 thisARCnet.rmem_end=thisARCnet.mem_start+512*2-1;
2194         }
2195 
2196         if (register_netdev(&thisARCnet) != 0)
2197                 return -EIO;
2198         return 0;
2199 }
2200 
2201 void
2202 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2203 {
2204   if (MOD_IN_USE) {
2205     printk("%s: device busy, remove delayed\n",thisARCnet.name);
2206   } else {
2207     if (thisARCnet.start) arcnet_close(&thisARCnet);
2208     if (thisARCnet.irq) free_irq(thisARCnet.irq);
2209     if (thisARCnet.base_addr) release_region(thisARCnet.base_addr,
2210                                                 ETHERCARD_TOTAL_SIZE);
2211     unregister_netdev(&thisARCnet);
2212   }
2213 }
2214 
2215 #endif /* MODULE */
2216 
2217 
2218 
2219 /*
2220  * Local variables:
2221  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c skeleton.c"
2222  *  version-control: t
2223  *  kept-new-versions: 5
2224  *  tab-width: 4
2225  * End:
2226  */
2227 

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