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 <linux/errno.h>
 103 
 104 #include <asm/system.h>
 105 #include <asm/bitops.h>
 106 #include <asm/io.h>
 107 #include <asm/dma.h>
 108 
 109 #include <linux/netdevice.h>
 110 #include <linux/etherdevice.h>
 111 #include <linux/skbuff.h>
 112 #include "arp.h"
 113 
 114 /* debug levels:
 115  * D_OFF        production
 116  * D_NORMAL     verification
 117  * D_INIT       show init/detect messages
 118  * D_DURING     show messages during normal use (ie interrupts)
 119  * D_TX         show tx packets
 120  * D_RX         show tx+rx packets
 121  */
 122 #define D_OFF           0
 123 #define D_NORMAL        1
 124 #define D_INIT          2
 125 #define D_EXTRA         3
 126 #define D_DURING        4
 127 #define D_TX            5
 128 #define D_RX            6
 129 
 130 #ifndef NET_DEBUG
 131 #define NET_DEBUG       D_INIT
 132 #endif
 133 static unsigned int net_debug = NET_DEBUG;
 134 
 135 #ifndef HAVE_AUTOIRQ
 136 /* From auto_irq.c, in ioport.h for later versions. */
 137 extern void autoirq_setup(int waittime);
 138 extern int autoirq_report(int waittime);
 139 /* The map from IRQ number (as passed to the interrupt handler) to
 140    'struct device'. */
 141 extern struct device *irq2dev_map[16];
 142 #endif
 143 
 144 #ifndef HAVE_PORTRESERVE
 145 #define check_region(ioaddr, size)              0
 146 #define snarf_region(ioaddr, size);             do ; while (0)
 147 #endif
 148 
 149 /* macro to simplify debug checking */
 150 #define BUGLVL(x) if (net_debug>=x)
 151 
 152 /* The number of low I/O ports used by the ethercard. */
 153 #define ETHERCARD_TOTAL_SIZE    16
 154 
 155 
 156 /* Handy defines for ARCnet specific stuff */
 157         /* COM 9026 (?) --> ARCnet register addresses */
 158 #define INTMASK (ioaddr+0)              /* writable */
 159 #define STATUS  (ioaddr+0)              /* readable */
 160 #define COMMAND (ioaddr+1)      /* writable, returns random vals on read (?) */
 161 #define RESET  (ioaddr+8)               /* software reset writable */
 162 
 163         /* time needed for various things (in clock ticks, 1/100 sec) */
 164 #define RESETtime 40            /* reset */
 165 #define XMITtime 10             /* send */
 166 #define ACKtime 10              /* acknowledge */
 167 
 168         /* these are the max/min lengths of packet data. (including
 169          * ClientData header)
 170          * note: packet sizes 250, 251, 252 are impossible (God knows why)
 171          *  so exception packets become necessary.
 172          *
 173          * These numbers are compared with the length of the full packet,
 174          * including ClientData header.
 175          */
 176 #define MTU     (253+EXTRA_CLIENTDATA)  /* normal packet max size */
 177 #define MinTU   (257+EXTRA_CLIENTDATA)  /* extended packet min size */
 178 #define XMTU    (508+EXTRA_CLIENTDATA)  /* extended packet max size */
 179 
 180         /* status/interrupt mask bit fields */
 181 #define TXFREEflag      0x001            /* transmitter available */
 182 #define TXACKflag       0x002            /* transmitted msg. ackd */
 183 #define RECONflag       0x004            /* system reconfigured */
 184 #define TESTflag        0x008            /* test flag */
 185 #define RESETflag       0x010            /* power-on-reset */
 186 #define RES1flag        0x020            /* unused */
 187 #define RES2flag        0x040            /* unused */
 188 #define NORXflag        0x080            /* receiver inhibited */
 189 
 190        /* in the command register, the following bits have these meanings:
 191         *                0-2     command
 192         *                3-4     page number (for enable rcv/xmt command)
 193         *                 7      receive broadcasts
 194         */
 195 #define NOTXcmd         0x001            /* disable transmitter */
 196 #define NORXcmd         0x002            /* disable receiver */
 197 #define TXcmd           0x003            /* enable transmitter */
 198 #define RXcmd           0x004            /* enable receiver */
 199 #define CONFIGcmd       0x005            /* define configuration */
 200 #define CFLAGScmd       0x006            /* clear flags */
 201 #define TESTcmd         0x007            /* load test flags */
 202 
 203        /* flags for "clear flags" command */
 204 #define RESETclear      0x008            /* power-on-reset */
 205 #define CONFIGclear     0x010            /* system reconfigured */
 206 
 207       /* flags for "load test flags" command */
 208 #define TESTload        0x008            /* test flag (diagnostic) */
 209 
 210       /* byte deposited into first address of buffers on reset */
 211 #define TESTvalue       0321             /* that's octal for 0xD1 :) */
 212 
 213       /* for "enable receiver" command */
 214 #define RXbcasts        0x080            /* receive broadcasts */
 215 
 216       /* flags for "define configuration" command */
 217 #define NORMALconf      0x000            /* 1-249 byte packets */
 218 #define EXTconf         0x008            /* 250-504 byte packets */
 219 
 220         /* buffers (4 total) used for receive and xmit.
 221          */
 222 #define EnableReceiver()        outb(RXcmd|(recbuf<<3)|RXbcasts,COMMAND)
 223 #define TXbuf           2
 224 
 225         /* Protocol ID's */
 226 #define ARC_P_IP        212             /* 0xD4 */
 227 #define ARC_P_ARP       213             /* 0xD5 */
 228 #define ARC_P_RARP      214             /* 0xD6 */
 229 
 230         /* Length of time between "stuck" checks */
 231 #define TIMERval        (HZ/8)          /* about 1/8 second */
 232 
 233         /* these structures define the format of an arcnet packet. */
 234 #define NORMAL          0
 235 #define EXTENDED        1
 236 #define EXCEPTION       2
 237 
 238         /* the header required by the card itself */
 239 struct HardHeader
 240 {
 241         u_char  source,         /* source ARCnet - filled in automagically */
 242                 destination,    /* destination ARCnet - 0 for broadcast */
 243                 offset1,        /* offset of ClientData (256-byte packets) */
 244                 offset2;        /* offset of ClientData (512-byte packets) */
 245 };
 246 
 247         /* a complete ARCnet packet */
 248 union ArcPacket
 249 {
 250         struct HardHeader hardheader;   /* the hardware header */
 251         u_char raw[512];                /* raw packet info, incl ClientData */
 252 };
 253 
 254         /* the "client data" header - RFC-1201 information
 255          * notice that this screws up if it's not an even number of bytes
 256          * <sigh>
 257          */
 258 struct ClientData
 259 {
 260         /* data that's NOT part of real packet */
 261         u_char  daddr;          /* Destination address - stored here,
 262                                  *   but WE MUST GET RID OF IT BEFORE SENDING A
 263                                  *   PACKET!!
 264                                  */
 265         u_char  stupid;         /* filler to make struct an even # of bytes */
 266         
 267         /* data that IS part of real packet */
 268         u_char  protocol_id,    /* ARC_P_IP, ARC_P_ARP, or ARC_P_RARP */
 269                 split_flag;     /* for use with split packets */
 270         u_short sequence;       /* sequence number (?) */
 271 };
 272 #define EXTRA_CLIENTDATA (sizeof(struct ClientData)-4)
 273 
 274 
 275 /* "Incoming" is information needed for each address that could be sending
 276  * to us.  Mostly for partially-received split packets.
 277  */
 278 struct Incoming
 279 {
 280         struct sk_buff *skb;            /* packet data buffer             */
 281         unsigned char lastpacket,       /* number of last packet (from 1) */
 282                       numpackets;       /* number of packets in split     */
 283         u_short sequence;               /* sequence number of assembly    */
 284 };
 285 
 286 
 287 /* Information that needs to be kept for each board. */
 288 struct arcnet_local {
 289         struct enet_statistics stats;
 290         u_char arcnum;          /* arcnet number - our 8-bit address */
 291         u_short sequence;       /* sequence number (incs with each packet) */
 292         u_char recbuf;          /* receive buffer # (0 or 1) */
 293         int intx;               /* in TX routine? */
 294         struct timer_list timer; /* the timer interrupt struct */
 295         struct Incoming incoming[256];  /* one from each address */
 296 };
 297 
 298 
 299 /* Index to functions, as function prototypes. */
 300 
 301 extern int arcnet_probe(struct device *dev);
 302 static int arcnet_memprobe(struct device *dev,u_char *addr);
 303 static int arcnet_ioprobe(struct device *dev, short ioaddr);
 304 
 305 static int arcnet_open(struct device *dev);
 306 static int arcnet_close(struct device *dev);
 307 
 308 static int arcnet_send_packet(struct sk_buff *skb, struct device *dev);
 309 static void careful_xmit_wait(struct device *dev);
 310 static int arcnet_tx(struct device *dev,struct ClientData *hdr,short length,
 311                         char *data);
 312 
 313 static void arcnet_interrupt(int irq, struct pt_regs *regs);
 314 static void arcnet_inthandler(struct device *dev);
 315 static void arcnet_rx(struct device *dev,int recbuf);
 316 
 317 
 318 static void arcnet_timer(unsigned long arg);
 319 
 320 static struct enet_statistics *arcnet_get_stats(struct device *dev);
 321 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
 322 
 323         /* annoying functions for header/arp/etc building */
 324 int arc_header(unsigned char *buff,struct device *dev,unsigned short type,
 325                 void *daddr,void *saddr,unsigned len,struct sk_buff *skb);
 326 int arc_rebuild_header(void *eth,struct device *dev,unsigned long raddr,
 327                 struct sk_buff *skb);
 328 unsigned short arc_type_trans(struct sk_buff *skb,struct device *dev);
 329 
 330 
 331 #define tx_done(dev) 1
 332 #define JIFFER(time) for (delayval=jiffies+(time); delayval>=jiffies;);
 333 static int arcnet_reset(struct device *dev);
 334 
 335 
 336 /* Check for a network adaptor of this type, and return '0' if one exists.
 337  *  If dev->base_addr == 0, probe all likely locations.
 338  *  If dev->base_addr == 1, always return failure.
 339  *  If dev->base_addr == 2, allocate space for the device and return success
 340  *  (detachable devices only).
 341  */
 342 int
 343 arcnet_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 344 {
 345         /* I refuse to probe anything less than 0x200, because anyone using
 346          * an address like that should probably be shot.
 347          */
 348         int *port, ports[] = {/* first the suggested values! */
 349                               0x300,0x2E0,0x2F0,0x2D0,
 350                               /* ...now everything else possible. */
 351                               0x200,0x210,0x220,0x230,0x240,0x250,0x260,0x270,
 352                               0x280,0x290,0x2a0,0x2b0,0x2c0,
 353                                     0x310,0x320,0x330,0x340,0x350,0x360,0x370,
 354                               0x380,0x390,0x3a0,0x3b0,0x3c0,0x3d0,0x3e0,0x3f0,
 355                               /* a null ends the list */
 356                               0};
 357         /* I'm not going to probe under 0xA0000 either, for similar reasons.
 358          */
 359         unsigned long *addr, addrs[] = {0xD0000,0xE0000,0xA0000,0xB0000,
 360                                         0xC0000,0xF0000,
 361                                         /* from <mdrejhon@magi.com> */
 362                                         0xE1000,
 363                                         0xDD000,0xDC000,
 364                                         0xD9000,0xD8000,0xD5000,0xD4000,0xD1000,
 365                                         0xCD000,0xCC000,
 366                                         0xC9000,0xC8000,0xC5000,0xC4000,
 367                                         /* terminator */
 368                                         0};
 369         int base_addr=dev->base_addr, status=0,delayval;
 370         struct arcnet_local *lp;
 371 
 372         if (net_debug) printk(version);
 373         
 374         BUGLVL(D_INIT)
 375                 printk("arcnet: given: base %Xh, IRQ %Xh, shmem %lXh\n",
 376                         dev->base_addr,dev->irq,dev->mem_start);
 377                 
 378         if (base_addr > 0x1ff)          /* Check a single specified location. */
 379                 status=arcnet_ioprobe(dev, base_addr);
 380         else if (base_addr > 0)         /* Don't probe at all. */
 381                 return ENXIO;
 382         else for (port = &ports[0]; *port; port++)
 383         {
 384                 int ioaddr = *port;
 385                 if (check_region(ioaddr, ETHERCARD_TOTAL_SIZE))
 386                 {
 387                         BUGLVL(D_INIT)
 388                                 printk("arcnet: Skipping %Xh because of check_region...\n",
 389                                         ioaddr);
 390                         continue;
 391                 }
 392 
 393                 status=arcnet_ioprobe(dev, ioaddr);
 394                 if (!status) break;
 395         }
 396         
 397         if (status) return status;
 398         
 399         /* ioprobe turned out okay.  Now give it a couple seconds to finish
 400          * initializing...
 401          */
 402         BUGLVL(D_INIT)
 403                 printk("arcnet: ioprobe okay!  Waiting for reset...\n");
 404         JIFFER(100);
 405 
 406         /* okay, now we have to find the shared memory area. */
 407         BUGLVL(D_INIT)
 408                 printk("arcnet: starting memory probe, given %lXh\n",
 409                         dev->mem_start);
 410         if (dev->mem_start)     /* value given - probe just that one */
 411         {
 412                 status=arcnet_memprobe(dev,(u_char *)dev->mem_start);
 413                 if (status) return status;
 414         }
 415         else                    /* no value given - probe everything */
 416         {
 417                 for (addr = &addrs[0]; *addr; addr++) {
 418                         status=arcnet_memprobe(dev,(u_char *)(*addr));
 419                         if (!status) break;
 420                 }
 421                 
 422                 if (status) return status;
 423         }
 424 
 425         /* now reserve the irq... */
 426         {        int irqval = request_irq(dev->irq, &arcnet_interrupt, 0, "arcnet");
 427                  if (irqval) {
 428                          printk("%s: unable to get IRQ %d (irqval=%d).\n", dev->name,
 429                                          dev->irq, irqval);
 430                          return EAGAIN;
 431                  }
 432          }
 433          
 434         /* Grab the region so we can find another board if autoIRQ fails. */
 435         snarf_region(dev->base_addr, ETHERCARD_TOTAL_SIZE);
 436 
 437         printk("%s: ARCnet card found at %03Xh, IRQ %d, ShMem at %lXh.\n", dev->name,
 438                 dev->base_addr, dev->irq, dev->mem_start);
 439 
 440         /* Initialize the device structure. */
 441         dev->priv = kmalloc(sizeof(struct arcnet_local), GFP_KERNEL);
 442         memset(dev->priv, 0, sizeof(struct arcnet_local));
 443         lp=(struct arcnet_local *)(dev->priv);
 444 
 445         dev->open               = arcnet_open;
 446         dev->stop               = arcnet_close;
 447         dev->hard_start_xmit = arcnet_send_packet;
 448         dev->get_stats  = arcnet_get_stats;
 449 #ifdef HAVE_MULTICAST
 450         dev->set_multicast_list = &set_multicast_list;
 451 #endif
 452 
 453         /* Fill in the fields of the device structure with ethernet-generic values. */
 454         ether_setup(dev);
 455 
 456         /* And now fill particular ones with arcnet values :) */
 457         
 458         dev->type=ARPHRD_ARCNET;
 459         dev->hard_header_len=sizeof(struct ClientData);
 460         BUGLVL(D_EXTRA)
 461                 printk("arcnet: ClientData header size is %d.\narcnet: HardHeader size is %d.\n",
 462                         sizeof(struct ClientData),sizeof(struct HardHeader));
 463 #if LIMIT_MTU   /* the old way - normally, now use ethernet default */
 464         dev->mtu=512-sizeof(struct HardHeader)+EXTRA_CLIENTDATA;
 465 #endif
 466                 /* since we strip EXTRA_CLIENTDATA bytes off before sending,
 467                  * we let Linux add that many bytes to the packet data...
 468                  */
 469         dev->addr_len=1;
 470         dev->broadcast[0]=0x00;
 471         
 472         BUGLVL(D_INIT) printk("arcnet: arcnet_probe: resetting card.\n");
 473         arcnet_reset(dev);
 474         JIFFER(50);
 475         BUGLVL(D_NORMAL)
 476                 printk("arcnet: We appear to be station %d (%02Xh)\n",
 477                         lp->arcnum,lp->arcnum);
 478         if (lp->arcnum==0)
 479                 printk("arcnet: WARNING!  Station address 0 is reserved for broadcasts!\n");
 480         if (lp->arcnum==255)
 481                 printk("arcnet: WARNING!  Station address 255 may confuse DOS networking programs!\n");
 482         dev->dev_addr[0]=lp->arcnum;
 483         lp->sequence=1;
 484         lp->recbuf=0;
 485 
 486         dev->hard_header        = arc_header;
 487 /*      dev->add_arp            = arc_add_arp;  AVE unavailable in 1.1.51?! */
 488         dev->rebuild_header     = arc_rebuild_header;
 489         dev->type_trans         = arc_type_trans;
 490 
 491         return 0;
 492 }
 493 
 494 int arcnet_ioprobe(struct device *dev, short ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 495 {
 496         int delayval,airq;
 497 
 498         BUGLVL(D_INIT)
 499                 printk("arcnet: probing address %Xh\n",ioaddr);
 500 
 501         BUGLVL(D_INIT)
 502                 printk("arcnet:  status1=%Xh\n",inb(STATUS));
 503 
 504                 
 505         /* very simple - all we have to do is reset the card, and if there's
 506          * no irq, it's not an ARCnet.  We can also kill two birds with
 507          * one stone because we detect the IRQ at the same time :)
 508          */
 509 
 510         /* reset the card by reading the reset port */
 511         inb(RESET);
 512         JIFFER(RESETtime);
 513 
 514         /* if status port is FF, there's certainly no arcnet... give up. */
 515         if (inb(STATUS)==0xFF)
 516         {
 517                 BUGLVL(D_INIT)
 518                         printk("arcnet:  probe failed.  Status port empty.\n");
 519                 return ENODEV;
 520         }
 521 
 522         /* we'll try to be reasonably sure it's an arcnet by making sure
 523          * the value of the COMMAND port changes automatically once in a
 524          * while.  I have no idea what those values ARE, but at least
 525          * they work.
 526          */
 527         {
 528                 int initval,curval;
 529                 
 530                 curval=initval=inb(COMMAND);
 531                 delayval=jiffies+5;
 532                 while (delayval>=jiffies && curval==initval)
 533                         curval=inb(COMMAND);
 534                         
 535                 if (curval==initval)
 536                 {
 537                         printk("arcnet:  probe failed.  never-changing command port (%02Xh).\n",
 538                                 initval);
 539                         return ENODEV;
 540                 }
 541         }
 542 
 543         BUGLVL(D_INIT)
 544                 printk("arcnet:  status2=%Xh\n",inb(STATUS));
 545 
 546         /* now we turn the reset bit off so we can IRQ next reset... */
 547         outb(CFLAGScmd|RESETclear|CONFIGclear,COMMAND);
 548         JIFFER(ACKtime);
 549         if (inb(STATUS) & RESETflag) /* reset flag STILL on */
 550         {
 551                 BUGLVL(D_INIT)
 552                         printk("arcnet:  probe failed.  eternal reset flag1...(status=%Xh)\n",
 553                                 inb(STATUS));
 554                 return ENODEV;
 555         }
 556 
 557         /* set up automatic IRQ detection */
 558         autoirq_setup(0);
 559         
 560         /* enable reset IRQ's (shouldn't be necessary, but hey) */
 561         outb(RESETflag,INTMASK);
 562 
 563         /* now reset it again to generate an IRQ */
 564         inb(RESET);
 565         JIFFER(RESETtime);
 566 
 567         BUGLVL(D_INIT)
 568                 printk("arcnet:  status3=%Xh\n",inb(STATUS));
 569 
 570         /* enable reset IRQ's again */
 571         outb(RESETflag,INTMASK);
 572 
 573         /* and turn the reset flag back off */
 574         outb(CFLAGScmd|RESETclear|CONFIGclear,COMMAND);
 575         JIFFER(ACKtime);
 576 
 577         BUGLVL(D_INIT)
 578                 printk("arcnet:  status4=%Xh\n",inb(STATUS));
 579 
 580         /* now reset it again to generate an IRQ */
 581         inb(RESET);
 582         JIFFER(RESETtime);
 583         
 584         BUGLVL(D_INIT)
 585                 printk("arcnet:  status5=%Xh\n",inb(STATUS));
 586 
 587         airq = autoirq_report(0);
 588         if (net_debug>=D_INIT && airq)
 589                 printk("arcnet:  autoirq is %d\n", airq);
 590 
 591         /* if there was no autoirq AND the user hasn't set any defaults,
 592          * give up.
 593          */
 594         if (!airq && !(dev->base_addr && dev->irq))
 595         {
 596                 BUGLVL(D_INIT)
 597                         printk("arcnet:  probe failed.  no autoirq...\n");
 598                 return ENODEV;
 599         }
 600 
 601         /* otherwise we probably have a card.  Let's make sure. */
 602         
 603         if (inb(STATUS) & RESETflag) /* reset flag on */
 604         {
 605                 /* now we turn the reset bit off */
 606                 outb(CFLAGScmd|RESETclear|CONFIGclear,COMMAND);
 607                 JIFFER(ACKtime);
 608         }
 609         
 610         if (inb(STATUS) & RESETflag) /* reset flag STILL on */
 611         {
 612                 BUGLVL(D_INIT)
 613                         printk("arcnet:  probe failed.  eternal reset flag...(status=%Xh)\n",
 614                                 inb(STATUS));
 615                 return ENODEV;
 616         }
 617         
 618         /* okay, we've got a real, live ARCnet on our hands. */
 619         if (!dev->base_addr) dev->base_addr=ioaddr;
 620         
 621         if (dev->irq < 2)               /* "Auto-IRQ" */
 622         {
 623                 /* we already did the autoirq above, so store the values */
 624                 dev->irq=airq;
 625         }
 626         else if (dev->irq == 2)
 627         {
 628                 if (net_debug)
 629                         printk("arcnet: IRQ2 == IRQ9, don't worry.\n");
 630                 dev->irq = 9;
 631         }
 632 
 633         BUGLVL(D_INIT)
 634                 printk("arcnet: irq and base address seem okay. (%Xh, IRQ %d)\n",
 635                         dev->base_addr,dev->irq);
 636         return 0;
 637 }
 638 
 639 /* A memory probe that is called after the card is reset.
 640  * It checks for the official TESTvalue in byte 0 and makes sure the buffer
 641  * has certain characteristics of an ARCnet...
 642  */
 643 int arcnet_memprobe(struct device *dev,u_char *addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 644 {
 645         BUGLVL(D_INIT)
 646                 printk("arcnet: probing memory at %lXh\n",(u_long)addr);
 647                 
 648         dev->mem_start=0;
 649 
 650 #ifdef STRICT_MEM_DETECT /* probably better. */
 651         /* ARCnet memory byte 0 is TESTvalue */
 652         if (addr[0]!=TESTvalue)
 653         {
 654                 BUGLVL(D_INIT)
 655                         printk("arcnet:  probe failed.  addr=%lXh, addr[0]=%Xh (not %Xh)\n",
 656                                 (unsigned long)addr,addr[0],TESTvalue);
 657                 return ENODEV;
 658         }
 659         
 660         /* now verify the shared memory writability */
 661         addr[0]=0x42;
 662         if (addr[0]!=0x42)
 663         {
 664                 BUGLVL(D_INIT)
 665                         printk("arcnet:  probe failed.  addr=%lXh, addr[0]=%Xh (not 42h)\n",
 666                                 (unsigned long)addr,addr[0]);
 667                 return ENODEV;
 668         }
 669 #else
 670         if (addr[0]!=TESTvalue)
 671         {
 672                 BUGLVL(D_INIT)
 673                         printk("arcnet:  probe failed.  addr=%lXh, addr[0]=%Xh (not %Xh)\n",
 674                                 (unsigned long)addr,addr[0],TESTvalue);
 675                 return ENODEV;
 676         }
 677 #endif
 678 
 679         /* got it!  fill in dev */
 680         dev->mem_start=(unsigned long)addr;
 681         dev->mem_end=dev->mem_start+512*4-1;
 682         dev->rmem_start=dev->mem_start+512*0;
 683         dev->rmem_end=dev->mem_start+512*2-1;
 684 
 685         return 0;
 686 }
 687 
 688 
 689 /* Open/initialize the board.  This is called (in the current kernel)
 690    sometime after booting when the 'ifconfig' program is run.
 691 
 692    This routine should set everything up anew at each open, even
 693    registers that "should" only need to be set once at boot, so that
 694    there is non-reboot way to recover if something goes wrong.
 695    */
 696 static int
 697 arcnet_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 698 {
 699         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
 700 /*      int ioaddr = dev->base_addr;*/
 701 
 702         if (net_debug) printk(version);
 703 
 704 #if 0   /* Yup, they're hardwired in arcnets */
 705         /* This is used if the interrupt line can turned off (shared).
 706            See 3c503.c for an example of selecting the IRQ at config-time. */
 707         if (request_irq(dev->irq, &arcnet_interrupt, 0, "arcnet")) {
 708                 return -EAGAIN;
 709         }
 710 #endif
 711 
 712         irq2dev_map[dev->irq] = dev;
 713 
 714         /* Reset the hardware here. */
 715         BUGLVL(D_EXTRA) printk("arcnet: arcnet_open: resetting card.\n");
 716         if (arcnet_reset(dev)) return -ENODEV;
 717         
 718 /*      chipset_init(dev, 1);*/
 719 /*      outb(0x00, ioaddr);*/
 720 
 721 /*      lp->open_time = jiffies;*/
 722 
 723         dev->tbusy = 0;
 724         dev->interrupt = 0;
 725         dev->start = 1;
 726 
 727         /* grab a timer handler to recover from any missed IRQ's */
 728         init_timer(&lp->timer);
 729         lp->timer.expires = TIMERval;         /* length of time */
 730         lp->timer.data = (unsigned long)dev;  /* pointer to "dev" structure */
 731         lp->timer.function = &arcnet_timer;    /* timer handler */
 732 #ifdef USE_TIMER_HANDLER
 733         add_timer(&lp->timer);
 734 #endif
 735                                         
 736         return 0;
 737 }
 738 
 739 
 740 /* The inverse routine to arcnet_open(). */
 741 static int
 742 arcnet_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 743 {
 744         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
 745         int ioaddr = dev->base_addr, delayval;
 746 
 747 /*      lp->open_time = 0;*/
 748 
 749         dev->tbusy = 1;
 750         dev->start = 0;
 751         
 752         /* release the timer */
 753         del_timer(&lp->timer);
 754 
 755         /* Flush the Tx and disable Rx here.     */
 756         /* resetting the card should do the job. */
 757         /*inb(RESET);*/
 758 
 759         outb(0,INTMASK);        /* no IRQ's */
 760         outb(NOTXcmd,COMMAND);  /* disable transmit */
 761         JIFFER(ACKtime);
 762         outb(NORXcmd,COMMAND);  /* disable receive */
 763 
 764 #if 0   /* we better not do this - hard wired IRQ's */
 765         /* If not IRQ jumpered, free up the line. */
 766         outw(0x00, ioaddr+0);           /* Release the physical interrupt line. */
 767         free_irq(dev->irq);
 768         irq2dev_map[dev->irq] = 0;
 769 #endif
 770 
 771         /* Update the statistics here. */
 772 
 773         return 0;
 774 }
 775 
 776 
 777 static int
 778 arcnet_send_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 779 {
 780         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
 781         int ioaddr=dev->base_addr,stat=0;
 782 /*      short daddr;*/
 783 
 784         lp->intx++;
 785         
 786         BUGLVL(D_DURING)
 787                 printk("arcnet: transmit requested (status=%Xh, inTX=%d)\n",
 788                         inb(STATUS),lp->intx);
 789 
 790         if (dev->tbusy)
 791         {
 792                 /* If we get here, some higher level has decided we are broken.
 793                    There should really be a "kick me" function call instead. */
 794                 int tickssofar = jiffies - dev->trans_start;
 795                 int recbuf=lp->recbuf;
 796                 int status=inb(STATUS);
 797                 
 798                 if (tickssofar < 5) return 1;
 799 
 800                 BUGLVL(D_INIT)
 801                         printk("arcnet: transmit timed out (status=%Xh, inTX=%d, tickssofar=%d)\n",
 802                                 status,lp->intx,tickssofar);
 803 
 804                 /* Try to restart the adaptor. */
 805                 /*arcnet_reset(dev);*/
 806                 
 807                 if (status&NORXflag) EnableReceiver();
 808                 if (!(status&TXFREEflag)) outb(NOTXcmd,COMMAND);
 809                 dev->tbusy=0;
 810                 mark_bh(NET_BH);
 811                 dev->trans_start = jiffies;
 812                 lp->intx--;
 813                 return 1;
 814         }
 815 
 816         /* If some higher layer thinks we've missed a tx-done interrupt
 817            we are passed NULL. Caution: dev_tint() handles the cli()/sti()
 818            itself. */
 819         if (skb == NULL) {
 820                 BUGLVL(D_INIT)
 821                         printk("arcnet: tx passed null skb (status=%Xh, inTX=%d, tickssofar=%ld)\n",
 822                                 inb(STATUS),lp->intx,jiffies-dev->trans_start);
 823                 dev_tint(dev);
 824                 lp->intx--;
 825                 return 0;
 826         }
 827 
 828         /* Block a timer-based transmit from overlapping.  This could better be
 829            done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
 830         if (set_bit(0, (void*)&dev->tbusy) != 0)
 831         {
 832             printk("arcnet: Transmitter called with busy bit set! (status=%Xh, inTX=%d, tickssofar=%ld)\n",
 833                         inb(STATUS),lp->intx,jiffies-dev->trans_start);
 834             stat=-EBUSY;
 835         }
 836         else {
 837                 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
 838                 struct ClientData *hdr=(struct ClientData*)skb->data;
 839                 
 840                 if (length<=XMTU)       /* fits in one packet? */
 841                 {
 842                         BUGLVL(D_TX) printk("arcnet: not splitting %d-byte packet. (split_flag=%d)\n",
 843                                         length,hdr->split_flag);
 844                         BUGLVL(D_INIT) if (hdr->split_flag)
 845                                 printk("arcnet: short packet has split_flag set?! (split_flag=%d)\n",
 846                                         hdr->split_flag);
 847                         stat=arcnet_tx(dev,hdr,
 848                                 length-sizeof(struct ClientData),
 849                                 ((char *)skb->data)+sizeof(struct ClientData));
 850                 }
 851                 else                    /* too big for one - split it */
 852                 {
 853                         u_char *data=(u_char *)skb->data
 854                                         + sizeof(struct ClientData);
 855                         int dataleft=length-sizeof(struct ClientData),
 856                                 maxsegsize=XMTU-sizeof(struct ClientData),
 857                                 numsegs=(dataleft+maxsegsize-1)/maxsegsize,
 858                                 seglen,segnum=0;
 859                                 
 860                         BUGLVL(D_TX) printk("arcnet: packet (%d bytes) split into %d fragments:\n",
 861                                 length,numsegs);
 862 
 863                         while (!stat && dataleft)
 864                         {
 865                                 if (!segnum)    /* first packet */
 866                                         hdr->split_flag=((numsegs-2)<<1)+1;
 867                                 else
 868                                         hdr->split_flag=segnum<<1;
 869 
 870                                 seglen=maxsegsize;
 871                                 if (seglen>dataleft) seglen=dataleft;
 872                                         
 873                                 BUGLVL(D_TX) printk("arcnet: packet #%d (%d bytes) of %d (%d total), splitflag=%d\n",
 874                                         segnum+1,seglen,numsegs,length,hdr->split_flag);
 875 
 876                                 stat=arcnet_tx(dev,hdr,seglen,data);
 877                                 
 878                                 dataleft-=seglen;
 879                                 data+=seglen;
 880                                 segnum++;
 881                                 
 882 #if 0   /* sequence # should not update here... I think! */
 883                                 /* sequence number goes up on each packet */
 884                                 hdr->sequence++;
 885                                 lp->sequence++;
 886 #endif
 887                         }
 888                 }
 889                         
 890                 /* I don't know if this should be in or out of these braces,
 891                  * but freeing it too often seems worse than too little.
 892                  * (maybe?)  (v0.30)
 893                  */
 894                 if (!stat) dev_kfree_skb(skb, FREE_WRITE);
 895                 
 896                 /* we're done now */
 897                 if (stat!=-EBUSY)
 898                 {
 899                         dev->tbusy=0;
 900                         mark_bh(NET_BH);        /* Inform upper layers. */
 901                         /* this should be on an IRQ, but can't
 902                          * because ARCnets (at least mine) are stupid.
 903                          */
 904                 }
 905         }
 906         
 907         lp->intx--;
 908 
 909         if (!stat) lp->stats.tx_packets++;
 910         return stat;
 911 }
 912 
 913 #ifdef CAREFUL_XMIT
 914 static void careful_xmit_wait(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 915 {
 916         int ioaddr=dev->base_addr;
 917         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
 918 
 919         /* wait patiently for tx to become available again */
 920         while ( !(inb(STATUS)&TXFREEflag) )
 921         {
 922                 if (jiffies-dev->trans_start > 20 || !dev->tbusy)
 923                 {
 924                         BUGLVL(D_INIT)
 925                                 printk("arcnet: CAREFUL_XMIT timeout. (busy=%d, status=%Xh)\n",
 926                                         dev->tbusy,inb(STATUS));
 927                         lp->stats.tx_errors++;
 928                         
 929                         outb(NOTXcmd,COMMAND);
 930                         return;
 931                 }
 932         }
 933         BUGLVL(D_TX) printk("arcnet: transmit completed successfully. (status=%Xh)\n",
 934                                 inb(STATUS));
 935 }
 936 #endif
 937 
 938 static int
 939 arcnet_tx(struct device *dev,struct ClientData *hdr,short length,
     /* [previous][next][first][last][top][bottom][index][help] */
 940                 char *data)
 941 {
 942         int ioaddr = dev->base_addr;
 943 #if 0
 944         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
 945 #endif
 946         struct ClientData *arcsoft;
 947         union ArcPacket *arcpacket = 
 948                 (union ArcPacket *)(dev->mem_start+512*TXbuf);
 949         u_char pkttype;
 950         int offset;
 951         short daddr;
 952         
 953         length+=sizeof(struct ClientData);
 954 
 955         BUGLVL(D_TX)
 956                 printk("arcnet: arcnet_tx: hdr:%ph, length:%d, data:%ph\n",
 957                         hdr,length,data);
 958 
 959 #if 0
 960         /* make sure transmitter is available before sending */
 961         if (! (inb(STATUS) & TXFREEflag))
 962         {
 963                 BUGLVL(D_TX)
 964                         printk("arcnet: transmitter in use! (status=%Xh)\n",
 965                                 inb(STATUS));
 966                 return -EBUSY;
 967         }
 968 #endif
 969         /* <blah> Gruesome hack because tx+rx irq's don't work at
 970          * the same time (or so it seems to me)
 971          *
 972          * Our transmits just won't be interrupt driven, I guess. (ugh)
 973          */             
 974 #ifdef CAREFUL_XMIT
 975         careful_xmit_wait(dev);
 976 #endif
 977 
 978         /* clean out the page to make debugging make more sense :) */
 979         BUGLVL(D_DURING)
 980                 memset((void *)dev->mem_start+TXbuf*512,0x42,512);
 981 
 982         daddr=arcpacket->hardheader.destination=hdr->daddr;
 983 
 984         /* load packet into shared memory */
 985         if (length<=MTU)        /* Normal (256-byte) Packet */
 986         {
 987                 pkttype=NORMAL;
 988                         
 989                 arcpacket->hardheader.offset1=offset=256-length
 990                                 + EXTRA_CLIENTDATA;
 991                 arcsoft=(struct ClientData *)
 992                         (&arcpacket->raw[offset-EXTRA_CLIENTDATA]);
 993         }
 994         else if (length>=MinTU) /* Extended (512-byte) Packet */
 995         {
 996                 pkttype=EXTENDED;
 997                 
 998                 arcpacket->hardheader.offset1=0;
 999                 arcpacket->hardheader.offset2=offset=512-length
1000                         + EXTRA_CLIENTDATA;
1001                 arcsoft=(struct ClientData *)
1002                         (&arcpacket->raw[offset-EXTRA_CLIENTDATA]);
1003         }
1004         else                            /* Exception Packet */
1005         {
1006                 pkttype=EXCEPTION;
1007                 
1008                 arcpacket->hardheader.offset1=0;
1009                 arcpacket->hardheader.offset2=offset=512-length-4
1010                         + EXTRA_CLIENTDATA;
1011                 arcsoft=(struct ClientData *)
1012                         (&arcpacket->raw[offset+4-EXTRA_CLIENTDATA]);
1013                 
1014                 /* exception-specific stuff - these four bytes
1015                  * make the packet long enough to fit in a 512-byte
1016                  * frame.
1017                  */
1018                 arcpacket->raw[offset+0]=arcsoft->protocol_id;
1019                 arcpacket->raw[offset+1]=0xFF; /* FF flag */
1020                         arcpacket->raw[offset+2]=0xFF; /* FF padding */
1021                         arcpacket->raw[offset+3]=0xFF; /* FF padding */
1022         }
1023 
1024 
1025         /* copy the packet into ARCnet shmem
1026          *  - the first bytes of ClientData header are skipped
1027          */
1028         memcpy((u_char*)arcsoft+EXTRA_CLIENTDATA,
1029                 (u_char*)hdr+EXTRA_CLIENTDATA,
1030                 sizeof(struct ClientData)-EXTRA_CLIENTDATA);
1031         memcpy((u_char*)arcsoft+sizeof(struct ClientData),
1032                 data,
1033                 length-sizeof(struct ClientData));
1034                 
1035         BUGLVL(D_DURING) printk("arcnet: transmitting packet to station %02Xh (%d bytes, type=%d)\n",
1036                         daddr,length,pkttype);
1037                         
1038         BUGLVL(D_TX)
1039         {
1040                 int countx,county;
1041                 
1042                 printk("arcnet: packet dump [tx] follows:");
1043 
1044                 for (county=0; county<16+(pkttype!=NORMAL)*16; county++)
1045                 {
1046                         printk("\n[%04X] ",county*16);
1047                         for (countx=0; countx<16; countx++)
1048                                 printk("%02X ",
1049                                         arcpacket->raw[county*16+countx]);
1050                 }
1051                 
1052                 printk("\n");
1053         }
1054         
1055 
1056         /* start sending */
1057         outb(TXcmd|(TXbuf<<3),COMMAND);
1058 
1059         dev->trans_start = jiffies;
1060 
1061         BUGLVL(D_TX) printk("arcnet: transmit started successfully. (status=%Xh)\n",
1062                                 inb(STATUS));
1063 #ifdef CAREFUL_XMIT
1064  #if 0
1065         careful_xmit_wait(dev);
1066 
1067         /* if we're not broadcasting, make sure the xmit was ack'd.
1068          * if it wasn't, there is probably no card with that
1069          * address... or else it missed our tx somehow.
1070          */
1071         if (daddr && !(inb(STATUS)&TXACKflag))
1072         {       
1073                 BUGLVL(D_INIT)
1074                         printk("arcnet: transmit not acknowledged. (status=%Xh, daddr=%02Xh)\n",
1075                                 inb(STATUS),daddr);
1076                 lp->stats.tx_errors++;
1077                 return -ENONET; /* "machine is not on the network" */
1078         }
1079  #endif
1080 #endif
1081 
1082         return 0;
1083 }
1084 
1085 
1086 /* The typical workload of the driver:
1087    Handle the network interface interrupts. */
1088 static void
1089 arcnet_interrupt(int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
1090 {
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] */