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. careful_xmit_wait
  8. arcnet_tx
  9. arcnet_interrupt
  10. arcnet_inthandler
  11. arcnet_rx
  12. arcnet_timer
  13. arcnet_get_stats
  14. set_multicast_list
  15. arcnet_reset
  16. arc_header
  17. arc_rebuild_header
  18. arc_type_trans

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

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