* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / net / arcnet.c
blob353b44dc8ef619e66ebcc4f7d811ecbb46b30377
1 /* $Id: arcnet.c,v 1.34 1997/11/09 11:04:55 mj Exp $
3 Written 1994-1996 by Avery Pennarun,
4 derived from skeleton.c by Donald Becker.
6 **********************
8 The original copyright was as follows:
10 skeleton.c Written 1993 by Donald Becker.
11 Copyright 1993 United States Government as represented by the
12 Director, National Security Agency. This software may only be used
13 and distributed according to the terms of the GNU Public License as
14 modified by SRC, incorporated herein by reference.
16 **********************
18 v3.02 (98/06/07)
19 - Use register_netdevice() instead of register_netdev() to create
20 new devices for RFC1051 and Ethernet encapsulation in arcnet_open.
21 Likewise for unregistering them later. This avoids the deadlock
22 encountered because the original routines call rtnl_lock() when
23 it's already locked. [dw]
25 v3.01 (98/04/17)
26 - Interrupt handler now also checks dev->[se]dev are non-NULL
27 to avoid crashes in interrupts during card init. [dw]
29 v3.00 (97/11/09)
30 - Minor cleanup of debugging messages. [mj]
32 v2.93 ALPHA (97/11/06)
33 - irq2dev mapping removed.
34 - Interrupt handler now checks whether dev->priv is non-null in order
35 to avoid crashes in interrupts which come during card init. [mj]
37 v2.92 ALPHA (97/09/02)
38 - Code cleanup [Martin Mares <mj@atrey.karlin.mff.cuni.cz>]
39 - Better probing for the COM90xx chipset, although only as
40 a temporary solution until we implement adding of all found
41 devices at once. [mj]
43 v2.91 ALPHA (97/08/19)
44 - Add counting of octets in/out.
46 v2.90 ALPHA (97/08/08)
47 - Add support for kernel command line parsing so that chipset
48 drivers are usable when compiled in.
50 v2.80 ALPHA (97/08/01)
51 - Split source into multiple files; generic arcnet support and
52 individual chipset drivers. <Dave@imladris.demon.co.uk>
54 v2.61 ALPHA (97/07/30) by David Woodhouse (Dave@imladris.demon.co.uk)
55 for Nortel (Northern Telecom).
56 - Added support for IO-mapped modes and for SMC COM20020 chipset.
57 - Fixed (avoided) race condition in send_packet routines which was
58 discovered when the buffer copy routines got slow (?).
59 - Fixed support for device naming at load time.
60 - Added backplane, clock and timeout options for COM20020.
61 - Added support for promiscuous mode.
63 v2.60 ALPHA (96/11/23)
64 - Added patch from Vojtech Pavlik <vojtech@atrey.karlin.mff.cuni.cz>
65 and Martin Mares <mj@k332.feld.cvut.cz> to make the driver work
66 with the new Linux 2.1.x memory management. I modified their
67 patch quite a bit though; bugs are my fault. More changes should
68 be made to get eliminate any remaining phys_to_virt calls.
69 - Quietly ignore protocol id's 0, 1, 8, and 243. Thanks to Jake
70 Messinger <jake@ams.com> for reporting these codes and their
71 meanings.
72 - Smarter shmem probe for cards with 4k mirrors. (does it work?)
73 - Initial support for RIM I type cards which use no I/O ports at
74 all. To use this option, you need to compile with RIM_I_MODE
75 enabled. Thanks to Kolja Waschk <kawk@yo.com> for explaining
76 RIM I programming to me. Now, does my RIM I code actually
77 work?
79 v2.56 (96/10/18)
80 - Turned arc0e/arc0s startup messages back on by default, as most
81 people will probably not notice the additional devices
82 otherwise. This causes undue confusion.
83 - Fixed a tiny but noticeable bug in the packet debugging routines
84 (thanks Tomasz)
86 The following has been SUMMARIZED. The complete ChangeLog is
87 available in the full Linux-ARCnet package at
88 http://www.worldvisions.ca/~apenwarr/arcnet
90 v2.50 (96/02/24)
91 - Massively improved autoprobe routines; they now work even as a
92 module. Thanks to Vojtech Pavlik <Vojtech.Pavlik@st.mff.cuni.cz>
93 for his ideas and help in this area.
94 - Changed printk's around quite a lot.
96 v2.22 (95/12/08)
97 - Major cleanups, speedups, and better code-sharing.
98 - Eliminated/changed many useless/meaningless/scary debug messages
99 (and, in most cases, the bugs that caused them).
100 - Better IPX support.
101 - lp->stats updated properly.
102 - RECON checking now by default only prints a message if there are
103 excessive errors (ie. your cable is probably broken).
104 - New RFC1051-compliant "arc0s" virtual device by Tomasz
105 Motylewski.
106 - Excess debug messages can be compiled out to reduce code size.
108 v2.00 (95/09/06)
109 - ARCnet RECON messages are now detected and logged as "carrier"
110 errors.
111 - The TXACK flag is now checked, and errors are logged.
112 - Debug levels are now completely different. See the README.
113 - Massive code cleanups, with several no-longer-necessary and some
114 completely useless options removed.
115 - Multiprotocol support. You can now use the "arc0e" device to
116 send "Ethernet-Encapsulation" packets, which are compatible with
117 Windows for Workgroups and LAN Manager, and possibly other
118 software. See the README for more information.
120 v1.02 (95/06/21)
121 - A fix to make "exception" packets sent from Linux receivable
122 on other systems. (The protocol_id byte was sometimes being set
123 incorrectly, and Linux wasn't checking it on receive so it
124 didn't show up)
126 v1.01 (95/03/24)
127 - Fixed some IPX-related bugs. (Thanks to Tomasz Motylewski
128 <motyl@tichy.ch.uj.edu.pl> for the patches to make arcnet work
129 with dosemu!)
131 v1.00 (95/02/15)
132 - Initial non-alpha release.
135 TO DO: (semi-prioritized)
137 - Use cleaner "architecture-independent" shared memory access.
138 This is half-done in ARCnet 2.60, but still uses some
139 undocumented i386 stuff. (We shouldn't call phys_to_virt,
140 for example.)
141 - Allow use of RFC1051 or Ether devices without RFC1201.
142 - Keep separate stats for each device.
143 - Support "arpless" mode like NetBSD does, and as recommended
144 by the (obsoleted) RFC1051.
145 - Smarter recovery from RECON-during-transmit conditions. (ie.
146 retransmit immediately)
147 - Add support for the new 1.3.x IP header cache, and other features.
148 - Replace setting of debug level with the "metric" flag hack by
149 something that still exists. SIOCDEVPRIVATE is a good candidate,
150 but it would require an extra user-level utility.
152 - What about cards with shared memory that can be "turned off?"
153 (or that have none at all, like the SMC PC500longboard)
154 Does this work now, with IO_MAPPED_BUFFERS?
156 - Autoconfigure PDI5xxPlus cards. (I now have a PDI508Plus to play
157 with temporarily.) Update: yes, the Pure Data config program
158 for DOS works fine, but the PDI508Plus I have doesn't! :)
159 - ATA protocol support??
160 - VINES TCP/IP encapsulation?? (info needed)
162 Sources:
163 - Crynwr arcnet.com/arcether.com packet drivers.
164 - arcnet.c v0.00 dated 1/1/94 and apparently by
165 Donald Becker - it didn't work :)
166 - skeleton.c v0.05 dated 11/16/93 by Donald Becker
167 (from Linux Kernel 1.1.45)
168 - RFC's 1201 and 1051 - re: TCP/IP over ARCnet
169 - The official ARCnet COM9026 data sheets (!) thanks to Ken
170 Cornetet <kcornete@nyx10.cs.du.edu>
171 - The official ARCnet COM20020 data sheets.
172 - Information on some more obscure ARCnet controller chips, thanks
173 to the nice people at SMC.
174 - net/inet/eth.c (from kernel 1.1.50) for header-building info.
175 - Alternate Linux ARCnet source by V.Shergin <vsher@sao.stavropol.su>
176 - Textual information and more alternate source from Joachim Koenig
177 <jojo@repas.de>
180 static const char *version =
181 "arcnet.c: v3.02 98/06/07 Avery Pennarun <apenwarr@worldvisions.ca> et al.\n";
183 #include <linux/module.h>
184 #include <linux/config.h>
185 #include <linux/version.h>
187 #include <linux/kernel.h>
188 #include <linux/sched.h>
189 #include <linux/types.h>
190 #include <linux/fcntl.h>
191 #include <linux/interrupt.h>
192 #include <linux/ptrace.h>
193 #include <linux/ioport.h>
194 #include <linux/in.h>
195 #include <linux/malloc.h>
196 #include <linux/string.h>
197 #include <linux/timer.h>
198 #include <linux/errno.h>
199 #include <linux/delay.h>
200 #include <linux/netdevice.h>
201 #include <linux/if_arp.h>
202 #include <linux/etherdevice.h>
203 #include <linux/skbuff.h>
204 #include <linux/init.h>
206 #include <linux/if_arcnet.h>
207 #include <linux/arcdevice.h>
209 #include <asm/system.h>
210 #include <asm/bitops.h>
211 #include <asm/io.h>
212 #include <asm/dma.h>
214 #include <net/arp.h>
216 /* Define this if you want to make it easier to use the "call trace" when
217 * a kernel NULL pointer assignment occurs. Hopefully unnecessary, most of
218 * the time. It will make all the function names (and other things) show
219 * up as kernel symbols. (especially handy when using arcnet as a module)
221 #undef static
223 /**************************************************************************/
225 /* These are now provided by the chipset driver. There's a performance
226 * overhead in using them.
229 #define AINTMASK(x) ((*lp->asetmask)(dev, x))
230 #define ARCSTATUS ((*lp->astatus)(dev))
231 #define ACOMMAND(x) ((*lp->acommand)(dev, x))
233 int arcnet_debug=ARCNET_DEBUG;
235 /* Exported function prototypes */
237 #ifdef MODULE
238 int init_module(void);
239 void cleanup_module(void);
240 #else
241 void arcnet_init(void);
242 static int init_module(void);
243 #ifdef CONFIG_ARCNET_COM90xx
244 extern char com90xx_explicit;
245 extern int arc90xx_probe(struct net_device *dev);
246 #endif
247 #endif
249 void arcnet_tx_done(struct net_device *dev, struct arcnet_local *lp);
250 void arcnet_use_count (int open);
251 void arcnet_setup(struct net_device *dev);
252 void arcnet_makename(char *device);
253 void arcnetA_continue_tx(struct net_device *dev);
254 int arcnet_go_tx(struct net_device *dev,int enable_irq);
255 void arcnet_interrupt(int irq,void *dev_id,struct pt_regs *regs);
256 void arcnet_rx(struct arcnet_local *lp, u_char *arcsoft, short length, int saddr, int daddr);
258 EXPORT_SYMBOL(arcnet_debug);
259 EXPORT_SYMBOL(arcnet_tx_done);
260 EXPORT_SYMBOL(arcnet_use_count);
261 EXPORT_SYMBOL(arcnet_setup);
262 EXPORT_SYMBOL(arcnet_makename);
263 EXPORT_SYMBOL(arcnetA_continue_tx);
264 EXPORT_SYMBOL(arcnet_go_tx);
265 EXPORT_SYMBOL(arcnet_interrupt);
266 EXPORT_SYMBOL(arcnet_rx);
268 #if ARCNET_DEBUG_MAX & D_SKB
269 void arcnet_dump_skb(struct net_device *dev,struct sk_buff *skb,
270 char *desc);
271 EXPORT_SYMBOL(arcnet_dump_skb);
272 #else
273 # define arcnet_dump_skb(dev,skb,desc) ;
274 #endif
276 #if (ARCNET_DEBUG_MAX & D_RX) || (ARCNET_DEBUG_MAX & D_TX)
277 void arcnet_dump_packet(struct net_device *dev,u_char *buffer,int ext,
278 char *desc);
279 EXPORT_SYMBOL(arcnet_dump_packet);
280 #else
281 # define arcnet_dump_packet(dev,buffer,ext,desc) ;
282 #endif
284 /* Internal function prototypes */
286 static int arcnet_open(struct net_device *dev);
287 static int arcnet_close(struct net_device *dev);
288 static int arcnetA_header(struct sk_buff *skb,struct net_device *dev,
289 unsigned short type,void *daddr,void *saddr,unsigned len);
290 static int arcnetA_rebuild_header(struct sk_buff *skb);
291 static int arcnet_send_packet_bad(struct sk_buff *skb,struct net_device *dev);
292 static int arcnetA_send_packet(struct sk_buff *skb, struct net_device *dev);
293 static void arcnetA_rx(struct net_device *dev,u_char *buf,
294 int length,u_char saddr, u_char daddr);
295 static struct net_device_stats *arcnet_get_stats(struct net_device *dev);
296 static unsigned short arcnetA_type_trans(struct sk_buff *skb,
297 struct net_device *dev);
300 #ifdef CONFIG_ARCNET_ETH
301 /* functions specific to Ethernet-Encap */
302 static int arcnetE_init(struct net_device *dev);
303 static int arcnetE_open_close(struct net_device *dev);
304 static int arcnetE_send_packet(struct sk_buff *skb, struct net_device *dev);
305 static void arcnetE_rx(struct net_device *dev,u_char *arcsoft,
306 int length,u_char saddr, u_char daddr);
307 #endif
310 #ifdef CONFIG_ARCNET_1051
311 /* functions specific to RFC1051 */
312 static int arcnetS_init(struct net_device *dev);
313 static int arcnetS_open_close(struct net_device *dev);
314 static int arcnetS_send_packet(struct sk_buff *skb, struct net_device *dev);
315 static void arcnetS_rx(struct net_device *dev,u_char *buf,
316 int length,u_char saddr, u_char daddr);
317 static int arcnetS_header(struct sk_buff *skb,struct net_device *dev,
318 unsigned short type,void *daddr,void *saddr,unsigned len);
319 static int arcnetS_rebuild_header(struct sk_buff *skb);
320 static unsigned short arcnetS_type_trans(struct sk_buff *skb,struct net_device *dev);
321 #endif
324 /****************************************************************************
326 * Packet dumps for debugging *
328 ****************************************************************************/
330 /* Dump the contents of an sk_buff
332 #if ARCNET_DEBUG_MAX & D_SKB
333 void arcnet_dump_skb(struct net_device *dev,struct sk_buff *skb,char *desc)
335 int i;
336 long flags;
338 save_flags(flags);
339 cli();
340 printk(KERN_DEBUG "%6s: skb dump (%s) follows:",dev->name,desc);
341 for(i=0; i<skb->len; i++)
343 if (i%16==0)
344 printk("\n" KERN_DEBUG "[%04X] ",i);
345 printk("%02X ",((u_char *)skb->data)[i]);
347 printk("\n");
348 restore_flags(flags);
350 #endif
353 /* Dump the contents of an ARCnet buffer
355 #if (ARCNET_DEBUG_MAX & D_RX) || (ARCNET_DEBUG_MAX & D_TX)
356 void arcnet_dump_packet(struct net_device *dev,u_char *buffer,int ext,char *desc)
358 int i;
359 long flags;
361 save_flags(flags);
362 cli();
363 printk(KERN_DEBUG "%6s: packet dump (%s) follows:",dev->name,desc);
364 for (i=0; i<256+(ext!=0)*256; i++)
366 if (i%16==0)
367 printk("\n" KERN_DEBUG "[%04X] ",i);
368 printk("%02X ",buffer[i]);
370 printk("\n");
371 restore_flags(flags);
373 #endif
376 /* Setup a struct net_device for ARCnet. This should really be in net_init.c
377 * but since there are three different ARCnet devices ANYWAY... <gargle>
379 * Actually, the whole idea of having all this kernel-dependent stuff (ie.
380 * "new-style flags") setup per-net-device is kind of weird anyway.
382 * Intelligent defaults?! Nah.
385 void arcnet_setup(struct net_device *dev)
387 dev_init_buffers(dev);
389 dev->broadcast[0] = 0x00; /* for us, broadcasts are address 0 */
390 dev->addr_len = 1;
391 dev->type = ARPHRD_ARCNET;
392 dev->tx_queue_len = 30;
394 /* New-style flags. */
395 dev->flags = IFF_BROADCAST;
397 /* Put in this stuff here, so we don't have to export the symbols
398 * to the chipset drivers.
401 dev->open=arcnet_open;
402 dev->stop=arcnet_close;
403 dev->hard_start_xmit=arcnetA_send_packet;
404 dev->get_stats=arcnet_get_stats;
405 dev->hard_header=arcnetA_header;
406 dev->rebuild_header=arcnetA_rebuild_header;
410 /****************************************************************************
412 * Open and close the driver *
414 ****************************************************************************/
416 /* Open/initialize the board. This is called sometime after booting when
417 * the 'ifconfig' program is run.
419 * This routine should set everything up anew at each open, even
420 * registers that "should" only need to be set once at boot, so that
421 * there is non-reboot way to recover if something goes wrong.
423 static int
424 arcnet_open(struct net_device *dev)
426 struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
428 /* if (dev->metric>=1000)
430 * arcnet_debug=dev->metric-1000;
431 * printk(KERN_INFO "%6s: debug level set to %d\n",dev->name,arcnet_debug);
432 * dev->metric=1;
435 BUGMSG(D_INIT,"arcnet_open: resetting card.\n");
437 /* try to put the card in a defined state - if it fails the first
438 * time, actually reset it.
440 if ((*lp->arcnet_reset)(dev,0) && (*lp->arcnet_reset)(dev,1))
441 return -ENODEV;
443 dev->tbusy=0;
444 dev->interrupt=0;
445 lp->intx=0;
446 lp->in_txhandler=0;
448 /* The RFC1201 driver is the default - just store */
449 lp->adev=dev;
451 /* we're started */
452 dev->start=1;
454 #ifdef CONFIG_ARCNET_ETH
455 /* Initialize the ethernet-encap protocol driver */
456 lp->edev=(struct net_device *)kmalloc(sizeof(struct net_device),GFP_KERNEL);
457 if (lp->edev == NULL)
458 return -ENOMEM;
459 memcpy(lp->edev,dev,sizeof(struct net_device));
460 lp->edev->type=ARPHRD_ETHER;
461 lp->edev->name=(char *)kmalloc(10,GFP_KERNEL);
462 if (lp->edev->name == NULL) {
463 kfree(lp->edev);
464 lp->edev = NULL;
465 return -ENOMEM;
467 sprintf(lp->edev->name,"%se",dev->name);
468 lp->edev->init=arcnetE_init;
469 register_netdevice(lp->edev);
470 #endif
472 #ifdef CONFIG_ARCNET_1051
473 /* Initialize the RFC1051-encap protocol driver */
474 lp->sdev=(struct net_device *)kmalloc(sizeof(struct net_device)+10,GFP_KERNEL);
475 if(lp->sdev == NULL)
477 #ifdef CONFIG_ARCNET_ETH
478 if(lp->edev)
479 kfree(lp->edev);
480 lp->edev=NULL;
481 return -ENOMEM;
482 #endif
484 memcpy(lp->sdev,dev,sizeof(struct net_device));
485 lp->sdev->name=(char *)(lp+1);
486 sprintf(lp->sdev->name,"%ss",dev->name);
487 lp->sdev->init=arcnetS_init;
488 register_netdevice(lp->sdev);
489 #endif
491 /* Enable TX if we need to */
492 if (lp->en_dis_able_TX)
493 (*lp->en_dis_able_TX)(dev, 1);
495 /* make sure we're ready to receive IRQ's.
496 * arcnet_reset sets this for us, but if we receive one before
497 * START is set to 1, it could be ignored. So, we turn IRQ's
498 * off, then on again to clean out the IRQ controller.
501 AINTMASK(0);
502 udelay(1); /* give it time to set the mask before
503 * we reset it again. (may not even be
504 * necessary)
506 SETMASK;
508 /* Let it increase its use count */
509 (*lp->openclose_device)(1);
511 return 0;
515 /* The inverse routine to arcnet_open - shuts down the card.
517 static int
518 arcnet_close(struct net_device *dev)
520 struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
522 if (test_and_set_bit(0, (int *)&dev->tbusy))
523 BUGMSG(D_NORMAL, "arcnet_close: tbusy already set!\n");
525 dev->start=0;
526 #ifdef CONFIG_ARCNET_1051
527 lp->sdev->tbusy=1;
528 lp->sdev->start=0;
529 #endif
530 #ifdef CONFIG_ARCNET_ETH
531 lp->edev->tbusy=1;
532 lp->edev->start=0;
533 #endif
535 /* Shut down the card */
537 /* Disable TX if we need to */
538 if (lp->en_dis_able_TX)
539 (*lp->en_dis_able_TX)(dev, 0);
541 (*lp->arcnet_reset)(dev, 3); /* reset IRQ won't run if START=0 */
542 #if 0
543 lp->intmask=0;
544 SETMASK; /* no IRQ's (except RESET, of course) */
545 ACOMMAND(NOTXcmd); /* stop transmit */
546 ACOMMAND(NORXcmd); /* disable receive */
547 #endif
549 /* reset more flags */
550 dev->interrupt=0;
551 #ifdef CONFIG_ARCNET_ETH
552 lp->edev->interrupt=0;
553 #endif
554 #ifdef CONFIG_ARCNET_1051
555 lp->sdev->interrupt=0;
556 #endif
558 /* do NOT free lp->adev!! It's static! */
559 lp->adev=NULL;
561 #ifdef CONFIG_ARCNET_ETH
562 /* free the ethernet-encap protocol device */
563 lp->edev->priv=NULL;
564 unregister_netdevice(lp->edev);
565 kfree(lp->edev->name);
566 kfree(lp->edev);
567 lp->edev=NULL;
568 #endif
570 #ifdef CONFIG_ARCNET_1051
571 /* free the RFC1051-encap protocol device */
572 lp->sdev->priv=NULL;
573 unregister_netdevice(lp->sdev);
574 kfree(lp->sdev);
575 lp->sdev=NULL;
576 #endif
578 /* Update the statistics here. (not necessary in ARCnet) */
580 /* Decrease the use count */
581 (*lp->openclose_device)(0);
583 return 0;
587 /****************************************************************************
589 * Transmitter routines *
591 ****************************************************************************/
593 /* Generic error checking routine for arcnet??_send_packet
595 static int
596 arcnet_send_packet_bad(struct sk_buff *skb, struct net_device *dev)
598 struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
600 BUGMSG(D_DURING,"transmit requested (status=%Xh, inTX=%d)\n",
601 ARCSTATUS,lp->intx);
603 if (lp->in_txhandler)
605 BUGMSG(D_NORMAL,"send_packet called while in txhandler!\n");
606 lp->stats.tx_dropped++;
607 return 1;
610 if (lp->intx>1)
612 BUGMSG(D_NORMAL,"send_packet called while intx!\n");
613 lp->stats.tx_dropped++;
614 return 1;
617 if (test_bit(0, (int *)&dev->tbusy))
619 /* If we get here, some higher level has decided we are broken.
620 There should really be a "kick me" function call instead. */
621 int tickssofar = jiffies - dev->trans_start;
623 int status=ARCSTATUS;
625 if (tickssofar < TX_TIMEOUT)
627 BUGMSG(D_DURING,"premature kickme! (status=%Xh ticks=%d o.skb=%ph numsegs=%d segnum=%d\n",
628 status,tickssofar,lp->outgoing.skb,
629 lp->outgoing.numsegs,
630 lp->outgoing.segnum);
631 return 1;
634 lp->intmask &= ~TXFREEflag;
635 SETMASK;
637 if (status&TXFREEflag) /* transmit _DID_ finish */
639 BUGMSG(D_NORMAL,"tx timeout - missed IRQ? (status=%Xh, ticks=%d, mask=%Xh, dest=%02Xh)\n",
640 status,tickssofar,lp->intmask,lp->lasttrans_dest);
641 lp->stats.tx_errors++;
643 else
645 BUGMSG(D_EXTRA,"tx timed out (status=%Xh, tickssofar=%d, intmask=%Xh, dest=%02Xh)\n",
646 status,tickssofar,lp->intmask,lp->lasttrans_dest);
647 lp->stats.tx_errors++;
648 lp->stats.tx_aborted_errors++;
650 ACOMMAND(NOTXcmd);
653 if (lp->outgoing.skb)
655 dev_kfree_skb(lp->outgoing.skb);
656 lp->stats.tx_dropped++;
658 lp->outgoing.skb=NULL;
660 #ifdef CONFIG_ARCNET_ETH
661 lp->edev->tbusy=0;
662 #endif
663 #ifdef CONFIG_ARCNET_1051
664 lp->sdev->tbusy=0;
665 #endif
666 if (!test_and_clear_bit(0,(int *)&dev->tbusy))
667 BUGMSG(D_EXTRA, "after timing out, tbusy was clear!\n");
669 lp->txready=0;
670 lp->sending=0;
672 return 1;
675 if (lp->txready) /* transmit already in progress! */
677 BUGMSG(D_NORMAL,"trying to start new packet while busy! (status=%Xh)\n",
678 ARCSTATUS);
679 lp->intmask &= ~TXFREEflag;
680 SETMASK;
681 ACOMMAND(NOTXcmd); /* abort current send */
682 (*lp->inthandler)(dev); /* fake an interrupt */
683 lp->stats.tx_errors++;
684 lp->stats.tx_fifo_errors++;
685 lp->txready=0; /* we definitely need this line! */
687 return 1;
690 /* Block a timer-based transmit from overlapping. This could better be
691 done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
692 if (test_and_set_bit(0, (int *)&lp->adev->tbusy))
694 BUGMSG(D_NORMAL,"transmitter called with busy bit set! (status=%Xh, inTX=%d, tickssofar=%ld)\n",
695 ARCSTATUS,lp->intx,jiffies-dev->trans_start);
696 lp->stats.tx_errors++;
697 lp->stats.tx_fifo_errors++;
698 return -EBUSY;
700 #ifdef CONFIG_ARCNET_1051
701 lp->sdev->tbusy=1;
702 #endif
703 #ifdef CONFIG_ARCNET_ETH
704 lp->edev->tbusy=1;
705 #endif
707 return 0;
711 /* Called by the kernel in order to transmit a packet.
713 static int
714 arcnetA_send_packet(struct sk_buff *skb, struct net_device *dev)
716 struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
717 int bad,oldmask=0;
718 struct Outgoing *out=&(lp->outgoing);
720 lp->intx++;
722 oldmask |= lp->intmask;
723 lp->intmask=0;
724 SETMASK;
726 bad=arcnet_send_packet_bad(skb,dev);
727 if (bad)
729 lp->intx--;
730 lp->intmask=oldmask;
731 SETMASK;
732 return bad;
735 /* arcnet_send_packet_pad has already set tbusy - don't bother here. */
737 lp->intmask = oldmask & ~TXFREEflag;
738 SETMASK;
740 out->length = 1 < skb->len ? skb->len : 1;
741 out->hdr=(struct ClientData*)skb->data;
742 out->skb=skb;
744 BUGLVL(D_SKB) arcnet_dump_skb(dev,skb,"tx");
746 out->hdr->sequence=(lp->sequence++);
748 /* fits in one packet? */
749 if (out->length-EXTRA_CLIENTDATA<=XMTU)
751 BUGMSG(D_DURING,"not splitting %d-byte packet. (split_flag=%d)\n",
752 out->length,out->hdr->split_flag);
753 if (out->hdr->split_flag)
754 BUGMSG(D_NORMAL,"short packet has split_flag set?! (split_flag=%d)\n",
755 out->hdr->split_flag);
756 out->numsegs=1;
757 out->segnum=1;
758 (*lp->prepare_tx)(dev,
759 ((char *)out->hdr)+EXTRA_CLIENTDATA,
760 sizeof(struct ClientData)-EXTRA_CLIENTDATA,
761 ((char *)skb->data)+sizeof(struct ClientData),
762 out->length-sizeof(struct ClientData),
763 out->hdr->daddr,1,0);
765 /* done right away */
766 lp->stats.tx_bytes += out->skb->len;
767 dev_kfree_skb(out->skb);
768 out->skb=NULL;
770 if (arcnet_go_tx(dev,1))
772 /* inform upper layers */
773 arcnet_tx_done(dev, lp);
776 else /* too big for one - split it */
778 int maxsegsize=XMTU-4;
780 out->data=(u_char *)skb->data
781 + sizeof(struct ClientData);
782 out->dataleft=out->length-sizeof(struct ClientData);
783 out->numsegs=(out->dataleft+maxsegsize-1)/maxsegsize;
784 out->segnum=0;
786 BUGMSG(D_TX,"packet (%d bytes) split into %d fragments:\n",
787 out->length,out->numsegs);
789 /* if a packet waiting, launch it */
790 arcnet_go_tx(dev,1);
792 if (!lp->txready)
794 /* prepare a packet, launch it and prepare
795 * another.
797 arcnetA_continue_tx(dev);
798 if (arcnet_go_tx(dev,1))
800 arcnetA_continue_tx(dev);
801 arcnet_go_tx(dev,1);
805 /* if segnum==numsegs, the transmission is finished;
806 * free the skb right away.
809 if (out->segnum==out->numsegs)
811 /* transmit completed */
812 out->segnum++;
813 if (out->skb)
815 lp->stats.tx_bytes += skb->len;
816 dev_kfree_skb(out->skb);
818 out->skb=NULL;
822 dev->trans_start=jiffies;
823 lp->intx--;
825 /* make sure we didn't ignore a TX IRQ while we were in here */
826 lp->intmask |= TXFREEflag;
827 SETMASK;
829 return 0;
833 /* After an RFC1201 split packet has been set up, this function calls
834 * arcnetAS_prepare_tx to load the next segment into the card. This function
835 * does NOT automatically call arcnet_go_tx.
837 void arcnetA_continue_tx(struct net_device *dev)
839 struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
840 int maxsegsize=XMTU-4;
841 struct Outgoing *out=&(lp->outgoing);
843 BUGMSG(D_DURING,"continue_tx called (status=%Xh, intx=%d, intxh=%d, intmask=%Xh\n",
844 ARCSTATUS,lp->intx,lp->in_txhandler,lp->intmask);
846 if (lp->txready)
848 BUGMSG(D_NORMAL,"continue_tx: called with packet in buffer!\n");
849 return;
852 if (out->segnum>=out->numsegs)
854 BUGMSG(D_NORMAL,"continue_tx: building segment %d of %d!\n",
855 out->segnum+1,out->numsegs);
858 if (!out->segnum) /* first packet */
859 out->hdr->split_flag=((out->numsegs-2)<<1)+1;
860 else
861 out->hdr->split_flag=out->segnum<<1;
863 out->seglen=maxsegsize;
864 if (out->seglen>out->dataleft) out->seglen=out->dataleft;
866 BUGMSG(D_TX,"building packet #%d (%d bytes) of %d (%d total), splitflag=%d\n",
867 out->segnum+1,out->seglen,out->numsegs,
868 out->length,out->hdr->split_flag);
870 (*lp->prepare_tx)(dev,((char *)out->hdr)+EXTRA_CLIENTDATA,
871 sizeof(struct ClientData)-EXTRA_CLIENTDATA,
872 out->data,out->seglen,out->hdr->daddr,1,0);
874 out->dataleft-=out->seglen;
875 out->data+=out->seglen;
876 out->segnum++;
880 /* Actually start transmitting a packet that was placed in the card's
881 * buffer by arcnetAS_prepare_tx. Returns 1 if a Tx is really started.
883 * This should probably always be called with the INTMASK register set to 0,
884 * so go_tx is not called recursively.
886 * The enable_irq flag determines whether to actually write INTMASK value
887 * to the card; TXFREEflag is always OR'ed into the memory variable either
888 * way.
890 int arcnet_go_tx(struct net_device *dev,int enable_irq)
892 struct arcnet_local *lp=(struct arcnet_local *)dev->priv;
894 BUGMSG(D_DURING,"go_tx: status=%Xh, intmask=%Xh, txready=%d, sending=%d\n",
895 ARCSTATUS,lp->intmask,lp->txready,lp->sending);
897 if (lp->sending || !lp->txready)
899 if (enable_irq && lp->sending)
901 lp->intmask |= TXFREEflag;
902 SETMASK;
904 return 0;
907 /* start sending */
908 ACOMMAND(TXcmd|(lp->txready<<3));
910 lp->stats.tx_packets++;
911 lp->txready=0;
912 lp->sending++;
914 lp->lasttrans_dest=lp->lastload_dest;
915 lp->lastload_dest=0;
917 lp->intmask |= TXFREEflag;
919 if (enable_irq) SETMASK;
921 return 1;
925 /****************************************************************************
927 * Interrupt handler *
929 ****************************************************************************/
932 /* The typical workload of the driver: Handle the network interface
933 * interrupts. Establish which device needs attention, and call the correct
934 * chipset interrupt handler.
936 void
937 arcnet_interrupt(int irq,void *dev_id,struct pt_regs *regs)
939 struct net_device *dev = dev_id;
940 struct arcnet_local *lp;
942 if (dev==NULL)
944 BUGMSG(D_DURING, "arcnet: irq %d for unknown device.\n", irq);
945 return;
948 BUGMSG(D_DURING,"in arcnet_interrupt\n");
950 lp=(struct arcnet_local *)dev->priv;
951 if (!lp)
953 BUGMSG(D_DURING, "arcnet: irq ignored.\n");
954 return;
957 /* RESET flag was enabled - if !dev->start, we must clear it right
958 * away (but nothing else) since inthandler() is never called.
961 if (!dev->start)
963 if (ARCSTATUS & RESETflag)
964 ACOMMAND(CFLAGScmd|RESETclear);
965 return;
969 if (test_and_set_bit(0, (int *)&dev->interrupt))
971 BUGMSG(D_NORMAL,"DRIVER PROBLEM! Nested arcnet interrupts!\n");
972 return; /* don't even try. */
974 #ifdef CONFIG_ARCNET_1051
975 if (lp->sdev)
976 lp->sdev->interrupt=1;
977 #endif
978 #ifdef CONFIG_ARCNET_ETH
979 if (lp->edev)
980 lp->edev->interrupt=1;
981 #endif
983 /* Call the "real" interrupt handler. */
984 (*lp->inthandler)(dev);
986 #ifdef CONFIG_ARCNET_ETH
987 if (lp->edev)
988 lp->edev->interrupt=0;
989 #endif
990 #ifdef CONFIG_ARCNET_1051
991 if (lp->sdev)
992 lp->sdev->interrupt=0;
993 #endif
994 if (!test_and_clear_bit(0, (int *)&dev->interrupt))
995 BUGMSG(D_NORMAL, "Someone cleared our dev->interrupt flag!\n");
999 void arcnet_tx_done(struct net_device *dev, struct arcnet_local *lp)
1001 if (dev->tbusy)
1003 #ifdef CONFIG_ARCNET_ETH
1004 lp->edev->tbusy=0;
1005 #endif
1006 #ifdef CONFIG_ARCNET_1051
1007 lp->sdev->tbusy=0;
1008 #endif
1009 if (!test_and_clear_bit(0, (int *)&dev->tbusy))
1010 BUGMSG(D_NORMAL, "In arcnet_tx_done: Someone cleared our dev->tbusy"
1011 " flag!\n");
1013 mark_bh(NET_BH);
1018 /****************************************************************************
1020 * Receiver routines *
1022 ****************************************************************************/
1025 * This is a generic packet receiver that calls arcnet??_rx depending on the
1026 * protocol ID found.
1029 void arcnet_rx(struct arcnet_local *lp, u_char *arcsoft, short length, int saddr, int daddr)
1031 struct net_device *dev=lp->adev;
1033 BUGMSG(D_DURING,"received packet from %02Xh to %02Xh (%d bytes)\n",
1034 saddr,daddr,length);
1036 /* call the right receiver for the protocol */
1037 switch (arcsoft[0])
1039 case ARC_P_IP:
1040 case ARC_P_ARP:
1041 case ARC_P_RARP:
1042 case ARC_P_IPX:
1043 case ARC_P_NOVELL_EC:
1044 arcnetA_rx(lp->adev,arcsoft,length,saddr,daddr);
1045 break;
1046 #ifdef CONFIG_ARCNET_ETH
1047 case ARC_P_ETHER:
1048 arcnetE_rx(lp->edev,arcsoft,length,saddr,daddr);
1049 break;
1050 #endif
1051 #ifdef CONFIG_ARCNET_1051
1052 case ARC_P_IP_RFC1051:
1053 case ARC_P_ARP_RFC1051:
1054 arcnetS_rx(lp->sdev,arcsoft,length,saddr,daddr);
1055 break;
1056 #endif
1057 case ARC_P_DATAPOINT_BOOT:
1058 case ARC_P_DATAPOINT_MOUNT:
1059 break;
1060 case ARC_P_POWERLAN_BEACON:
1061 case ARC_P_POWERLAN_BEACON2:
1062 break;
1063 case ARC_P_LANSOFT: /* don't understand. fall through. */
1064 default:
1065 BUGMSG(D_EXTRA,"received unknown protocol %d (%Xh) from station %d.\n",
1066 arcsoft[0],arcsoft[0],saddr);
1067 lp->stats.rx_errors++;
1068 lp->stats.rx_crc_errors++;
1069 break;
1072 /* If any worth-while packets have been received, a mark_bh(NET_BH)
1073 * has been done by netif_rx and Linux will handle them after we
1074 * return.
1082 /* Packet receiver for "standard" RFC1201-style packets
1084 static void
1085 arcnetA_rx(struct net_device *dev,u_char *buf,
1086 int length, u_char saddr, u_char daddr)
1088 struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1089 struct sk_buff *skb;
1090 struct ClientData *arcsoft,*soft;
1092 BUGMSG(D_DURING,"it's an RFC1201 packet (length=%d)\n",
1093 length);
1095 /* compensate for EXTRA_CLIENTDATA (which isn't actually in the
1096 * packet)
1098 arcsoft=(struct ClientData *)(buf-EXTRA_CLIENTDATA);
1099 length+=EXTRA_CLIENTDATA;
1101 if (arcsoft->split_flag==0xFF) /* Exception Packet */
1103 BUGMSG(D_DURING,"compensating for exception packet\n");
1105 /* skip over 4-byte junkola */
1106 arcsoft=(struct ClientData *)
1107 ((u_char *)arcsoft + 4);
1108 length-=4;
1111 if (!arcsoft->split_flag) /* not split */
1113 struct Incoming *in=&lp->incoming[saddr];
1115 BUGMSG(D_RX,"incoming is not split (splitflag=%d)\n",
1116 arcsoft->split_flag);
1118 if (in->skb) /* already assembling one! */
1120 BUGMSG(D_EXTRA,"aborting assembly (seq=%d) for unsplit packet (splitflag=%d, seq=%d)\n",
1121 in->sequence,arcsoft->split_flag,
1122 arcsoft->sequence);
1123 lp->aborted_seq=arcsoft->sequence;
1124 kfree_skb(in->skb);
1125 lp->stats.rx_errors++;
1126 lp->stats.rx_missed_errors++;
1127 in->skb=NULL;
1130 in->sequence=arcsoft->sequence;
1132 skb = alloc_skb(length, GFP_ATOMIC);
1133 if (skb == NULL) {
1134 BUGMSG(D_NORMAL,"Memory squeeze, dropping packet.\n");
1135 lp->stats.rx_dropped++;
1136 return;
1138 soft=(struct ClientData *)skb->data;
1140 skb_put(skb,length);
1141 skb->dev = dev;
1143 memcpy((u_char *)soft+EXTRA_CLIENTDATA,
1144 (u_char *)arcsoft+EXTRA_CLIENTDATA,
1145 length-EXTRA_CLIENTDATA);
1146 soft->daddr=daddr;
1147 soft->saddr=saddr;
1149 /* ARP packets have problems when sent from DOS.
1150 * source address is always 0 on some systems! So we take
1151 * the hardware source addr (which is impossible to fumble)
1152 * and insert it ourselves.
1154 if (soft->protocol_id == ARC_P_ARP)
1156 struct arphdr *arp=(struct arphdr *)
1157 ((char *)soft+sizeof(struct ClientData));
1159 /* make sure addresses are the right length */
1160 if (arp->ar_hln==1 && arp->ar_pln==4)
1162 char *cptr=(char *)(arp)+sizeof(struct arphdr);
1164 if (!*cptr) /* is saddr = 00? */
1166 BUGMSG(D_EXTRA,"ARP source address was 00h, set to %02Xh.\n",
1167 saddr);
1168 lp->stats.rx_crc_errors++;
1169 *cptr=saddr;
1171 else
1173 BUGMSG(D_DURING,"ARP source address (%Xh) is fine.\n",
1174 *cptr);
1177 else
1179 BUGMSG(D_NORMAL,"funny-shaped ARP packet. (%Xh, %Xh)\n",
1180 arp->ar_hln,arp->ar_pln);
1181 lp->stats.rx_errors++;
1182 lp->stats.rx_crc_errors++;
1186 BUGLVL(D_SKB) arcnet_dump_skb(dev,skb,"rx");
1188 lp->stats.rx_bytes += skb->len;
1189 skb->protocol=arcnetA_type_trans(skb,dev);
1190 netif_rx(skb);
1192 else /* split packet */
1194 /* NOTE: MSDOS ARP packet correction should only need to
1195 * apply to unsplit packets, since ARP packets are so short.
1197 * My interpretation of the RFC1201 (ARCnet) document is that
1198 * if a packet is received out of order, the entire assembly
1199 * process should be aborted.
1201 * The RFC also mentions "it is possible for successfully
1202 * received packets to be retransmitted." As of 0.40 all
1203 * previously received packets are allowed, not just the
1204 * most recent one.
1206 * We allow multiple assembly processes, one for each
1207 * ARCnet card possible on the network. Seems rather like
1208 * a waste of memory. Necessary?
1211 struct Incoming *in=&lp->incoming[saddr];
1213 BUGMSG(D_RX,"packet is split (splitflag=%d, seq=%d)\n",
1214 arcsoft->split_flag,in->sequence);
1216 if (in->skb && in->sequence!=arcsoft->sequence)
1218 BUGMSG(D_EXTRA,"wrong seq number (saddr=%d, expected=%d, seq=%d, splitflag=%d)\n",
1219 saddr,in->sequence,arcsoft->sequence,
1220 arcsoft->split_flag);
1221 kfree_skb(in->skb);
1222 in->skb=NULL;
1223 lp->stats.rx_errors++;
1224 lp->stats.rx_missed_errors++;
1225 in->lastpacket=in->numpackets=0;
1228 if (arcsoft->split_flag & 1) /* first packet in split */
1230 BUGMSG(D_RX,"brand new splitpacket (splitflag=%d)\n",
1231 arcsoft->split_flag);
1232 if (in->skb) /* already assembling one! */
1234 BUGMSG(D_EXTRA,"aborting previous (seq=%d) assembly (splitflag=%d, seq=%d)\n",
1235 in->sequence,arcsoft->split_flag,
1236 arcsoft->sequence);
1237 lp->stats.rx_errors++;
1238 lp->stats.rx_missed_errors++;
1239 kfree_skb(in->skb);
1242 in->sequence=arcsoft->sequence;
1243 in->numpackets=((unsigned)arcsoft->split_flag>>1)+2;
1244 in->lastpacket=1;
1246 if (in->numpackets>16)
1248 BUGMSG(D_EXTRA,"incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
1249 arcsoft->split_flag);
1250 lp->stats.rx_errors++;
1251 lp->stats.rx_length_errors++;
1252 return;
1255 in->skb=skb=alloc_skb(508*in->numpackets
1256 + sizeof(struct ClientData),
1257 GFP_ATOMIC);
1258 if (skb == NULL) {
1259 BUGMSG(D_NORMAL,"(split) memory squeeze, dropping packet.\n");
1260 lp->stats.rx_dropped++;
1261 return;
1264 soft=(struct ClientData *)skb->data;
1266 skb_put(skb,sizeof(struct ClientData));
1267 skb->dev=dev;
1269 memcpy((u_char *)soft+EXTRA_CLIENTDATA,
1270 (u_char *)arcsoft+EXTRA_CLIENTDATA,
1271 sizeof(struct ClientData)-EXTRA_CLIENTDATA);
1272 soft->split_flag=0; /* final packet won't be split */
1274 else /* not first packet */
1276 int packetnum=((unsigned)arcsoft->split_flag>>1) + 1;
1278 /* if we're not assembling, there's no point
1279 * trying to continue.
1281 if (!in->skb)
1283 if (lp->aborted_seq != arcsoft->sequence)
1285 BUGMSG(D_EXTRA,"can't continue split without starting first! (splitflag=%d, seq=%d, aborted=%d)\n",
1286 arcsoft->split_flag,arcsoft->sequence, lp->aborted_seq);
1287 lp->stats.rx_errors++;
1288 lp->stats.rx_missed_errors++;
1290 return;
1293 in->lastpacket++;
1294 if (packetnum!=in->lastpacket) /* not the right flag! */
1296 /* harmless duplicate? ignore. */
1297 if (packetnum<=in->lastpacket-1)
1299 BUGMSG(D_EXTRA,"duplicate splitpacket ignored! (splitflag=%d)\n",
1300 arcsoft->split_flag);
1301 lp->stats.rx_errors++;
1302 lp->stats.rx_frame_errors++;
1303 return;
1306 /* "bad" duplicate, kill reassembly */
1307 BUGMSG(D_EXTRA,"out-of-order splitpacket, reassembly (seq=%d) aborted (splitflag=%d, seq=%d)\n",
1308 in->sequence,arcsoft->split_flag,
1309 arcsoft->sequence);
1310 lp->aborted_seq=arcsoft->sequence;
1311 kfree_skb(in->skb);
1312 in->skb=NULL;
1313 lp->stats.rx_errors++;
1314 lp->stats.rx_missed_errors++;
1315 in->lastpacket=in->numpackets=0;
1316 return;
1319 soft=(struct ClientData *)in->skb->data;
1322 skb=in->skb;
1324 memcpy(skb->data+skb->len,
1325 (u_char *)arcsoft+sizeof(struct ClientData),
1326 length-sizeof(struct ClientData));
1327 skb_put(skb,length-sizeof(struct ClientData));
1329 soft->daddr=daddr;
1330 soft->saddr=saddr;
1332 /* are we done? */
1333 if (in->lastpacket == in->numpackets)
1335 if (!skb || !in->skb)
1337 BUGMSG(D_NORMAL,"?!? done reassembling packet, no skb? (skb=%ph, in->skb=%ph)\n",
1338 skb,in->skb);
1340 else
1342 in->skb=NULL;
1343 in->lastpacket=in->numpackets=0;
1345 BUGLVL(D_SKB) arcnet_dump_skb(dev,skb,"rx");
1347 lp->stats.rx_bytes += skb->len;
1348 skb->protocol=arcnetA_type_trans(skb,dev);
1349 netif_rx(skb);
1356 /****************************************************************************
1358 * Miscellaneous routines *
1360 ****************************************************************************/
1362 /* Get the current statistics. This may be called with the card open or
1363 * closed.
1366 static struct net_device_stats *arcnet_get_stats(struct net_device *dev)
1368 struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1370 return &lp->stats;
1374 /* Create the ARCnet ClientData header for an arbitrary protocol layer
1376 * saddr=NULL means use device source address (always will anyway)
1377 * daddr=NULL means leave destination address (eg unresolved arp)
1379 static int arcnetA_header(struct sk_buff *skb,struct net_device *dev,
1380 unsigned short type,void *daddr,void *saddr,unsigned len)
1382 struct ClientData *head = (struct ClientData *)
1383 skb_push(skb,dev->hard_header_len);
1384 struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
1386 BUGMSG(D_DURING,"create header from %d to %d; protocol %d (%Xh); size %u.\n",
1387 saddr ? *(u_char*)saddr : -1,
1388 daddr ? *(u_char*)daddr : -1,
1389 type,type,len);
1391 /* set the protocol ID according to RFC1201 */
1392 switch(type)
1394 case ETH_P_IP:
1395 head->protocol_id=ARC_P_IP;
1396 break;
1397 case ETH_P_ARP:
1398 head->protocol_id=ARC_P_ARP;
1399 break;
1400 case ETH_P_RARP:
1401 head->protocol_id=ARC_P_RARP;
1402 break;
1403 case ETH_P_IPX:
1404 case ETH_P_802_3:
1405 case ETH_P_802_2:
1406 head->protocol_id=ARC_P_IPX;
1407 break;
1408 case ETH_P_ATALK:
1409 head->protocol_id=ARC_P_ATALK;
1410 break;
1411 default:
1412 BUGMSG(D_NORMAL,"I don't understand protocol %d (%Xh)\n",
1413 type,type);
1414 lp->stats.tx_errors++;
1415 lp->stats.tx_aborted_errors++;
1416 return 0;
1420 * Set the source hardware address.
1422 * This is pretty pointless for most purposes, but it can help
1423 * in debugging. saddr is stored in the ClientData header and
1424 * removed before sending the packet (since ARCnet does not allow
1425 * us to change the source address in the actual packet sent)
1427 if(saddr)
1428 head->saddr=((u_char*)saddr)[0];
1429 else
1430 head->saddr=((u_char*)(dev->dev_addr))[0];
1432 head->split_flag=0; /* split packets are done elsewhere */
1433 head->sequence=0; /* so are sequence numbers */
1435 /* supposedly if daddr is NULL, we should ignore it... */
1436 if(daddr)
1438 head->daddr=((u_char*)daddr)[0];
1439 return dev->hard_header_len;
1441 else
1442 head->daddr=0; /* better fill one in anyway */
1444 return -dev->hard_header_len;
1448 /* Rebuild the ARCnet ClientData header. This is called after an ARP
1449 * (or in future other address resolution) has completed on this
1450 * sk_buff. We now let ARP fill in the other fields.
1452 static int arcnetA_rebuild_header(struct sk_buff *skb)
1454 struct ClientData *head = (struct ClientData *)skb->data;
1455 struct net_device *dev=skb->dev;
1456 struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
1457 #ifdef CONFIG_INET
1458 int status;
1459 #endif
1462 * Only ARP and IP are currently supported
1464 * FIXME: Anyone want to spec IPv6 over ARCnet ?
1467 if(head->protocol_id != ARC_P_IP)
1469 BUGMSG(D_NORMAL,"I don't understand protocol type %d (%Xh) addresses!\n",
1470 head->protocol_id,head->protocol_id);
1471 lp->stats.tx_errors++;
1472 lp->stats.tx_aborted_errors++;
1473 head->daddr=0;
1474 /*memcpy(eth->h_source, dev->dev_addr, dev->addr_len);*/
1475 return 0;
1479 * Try to get ARP to resolve the header.
1481 #ifdef CONFIG_INET
1482 BUGMSG(D_DURING,"rebuild header from %d to %d; protocol %Xh\n",
1483 head->saddr,head->daddr,head->protocol_id);
1484 status=arp_find(&(head->daddr),skb)? 1 : 0;
1485 BUGMSG(D_DURING," rebuilt: from %d to %d; protocol %Xh\n",
1486 head->saddr,head->daddr,head->protocol_id);
1487 return status;
1488 #else
1489 return 0;
1490 #endif
1494 /* Determine a packet's protocol ID.
1496 * With ARCnet we have to convert everything to Ethernet-style stuff.
1498 static unsigned short arcnetA_type_trans(struct sk_buff *skb,struct net_device *dev)
1500 struct ClientData *head;
1501 struct arcnet_local *lp=(struct arcnet_local *) (dev->priv);
1503 /* Pull off the arcnet header. */
1504 skb->mac.raw=skb->data;
1505 skb_pull(skb,dev->hard_header_len);
1506 head=(struct ClientData *)skb->mac.raw;
1508 if (head->daddr==0)
1509 skb->pkt_type=PACKET_BROADCAST;
1510 else if (dev->flags&IFF_PROMISC)
1512 /* if we're not sending to ourselves :) */
1513 if (head->daddr != dev->dev_addr[0])
1514 skb->pkt_type=PACKET_OTHERHOST;
1517 /* now return the protocol number */
1518 switch (head->protocol_id)
1520 case ARC_P_IP: return htons(ETH_P_IP);
1521 case ARC_P_ARP: return htons(ETH_P_ARP);
1522 case ARC_P_RARP: return htons(ETH_P_RARP);
1524 case ARC_P_IPX:
1525 case ARC_P_NOVELL_EC:
1526 return htons(ETH_P_802_3);
1527 default:
1528 lp->stats.rx_errors++;
1529 lp->stats.rx_crc_errors++;
1530 return 0;
1533 return htons(ETH_P_IP);
1537 #ifdef CONFIG_ARCNET_ETH
1538 /****************************************************************************
1540 * Ethernet-Encap Support *
1542 ****************************************************************************/
1544 /* Initialize the arc0e device.
1546 static int arcnetE_init(struct net_device *dev)
1548 struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1550 ether_setup(dev); /* we're emulating ether here, not ARCnet */
1551 dev->dev_addr[0]=0;
1552 dev->dev_addr[5]=lp->stationid;
1553 dev->mtu=512-sizeof(struct archdr)-dev->hard_header_len-1;
1554 dev->open=arcnetE_open_close;
1555 dev->stop=arcnetE_open_close;
1556 dev->hard_start_xmit=arcnetE_send_packet;
1558 return 0;
1562 /* Bring up/down the arc0e device - we don't actually have to do anything,
1563 * since our parent arc0 handles the card I/O itself.
1565 static int arcnetE_open_close(struct net_device *dev)
1567 return 0;
1571 /* Called by the kernel in order to transmit an ethernet-type packet.
1573 static int
1574 arcnetE_send_packet(struct sk_buff *skb, struct net_device *dev)
1576 struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1577 int bad,oldmask=0;
1578 u_char daddr;
1579 short offset,length=skb->len+1;
1580 u_char proto=ARC_P_ETHER;
1582 lp->intx++;
1584 oldmask |= lp->intmask;
1585 lp->intmask=0;
1586 SETMASK;
1588 bad=arcnet_send_packet_bad(skb,dev);
1589 if (bad)
1591 lp->intx--;
1592 lp->intmask=oldmask;
1593 SETMASK;
1594 return bad;
1597 /* arcnet_send_packet_pad has already set tbusy - don't bother here. */
1599 lp->intmask=oldmask;
1600 SETMASK;
1602 if (length>XMTU)
1604 BUGMSG(D_NORMAL,"MTU must be <= 493 for ethernet encap (length=%d).\n",
1605 length);
1606 BUGMSG(D_NORMAL,"transmit aborted.\n");
1608 dev_kfree_skb(skb);
1609 lp->intx--;
1610 return 0;
1613 BUGMSG(D_DURING,"starting tx sequence...\n");
1615 /* broadcasts have address FF:FF:FF:FF:FF:FF in etherspeak */
1616 if (((struct ethhdr*)(skb->data))->h_dest[0] == 0xFF)
1617 daddr=0;
1618 else
1619 daddr=((struct ethhdr*)(skb->data))->h_dest[5];
1621 /* load packet into shared memory */
1622 offset=512-length;
1623 if (length>MTU) /* long/exception packet */
1625 if (length<MinTU) offset-=3;
1627 else /* short packet */
1629 offset-=256;
1632 BUGMSG(D_DURING," length=%Xh, offset=%Xh\n",
1633 length,offset);
1635 (*lp->prepare_tx)(dev, &proto, 1, skb->data, length-1, daddr, 0,
1636 offset);
1638 dev_kfree_skb(skb);
1640 if (arcnet_go_tx(dev,1))
1642 /* inform upper layers */
1643 arcnet_tx_done(lp->adev, lp);
1646 dev->trans_start=jiffies;
1647 lp->intx--;
1649 /* make sure we didn't ignore a TX IRQ while we were in here */
1650 lp->intmask |= TXFREEflag;
1651 SETMASK;
1653 return 0;
1657 /* Packet receiver for ethernet-encap packets.
1659 static void
1660 arcnetE_rx(struct net_device *dev,u_char *arcsoft,
1661 int length,u_char saddr, u_char daddr)
1663 struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1664 struct sk_buff *skb;
1666 BUGMSG(D_DURING,"it's an ethernet-encap packet (length=%d)\n",
1667 length);
1669 skb = alloc_skb(length, GFP_ATOMIC);
1670 if (skb == NULL) {
1671 BUGMSG(D_NORMAL,"Memory squeeze, dropping packet.\n");
1672 lp->stats.rx_dropped++;
1673 return;
1676 skb_put(skb,length);
1678 skb->dev = dev;
1680 memcpy(skb->data,(u_char *)arcsoft+1,length-1);
1682 BUGLVL(D_SKB) arcnet_dump_skb(dev,skb,"rx");
1684 lp->stats.rx_bytes += skb->len;
1685 skb->protocol=eth_type_trans(skb,dev);
1686 netif_rx(skb);
1689 #endif /* CONFIG_ARCNET_ETH */
1691 #ifdef CONFIG_ARCNET_1051
1692 /****************************************************************************
1694 * RFC1051 Support *
1696 ****************************************************************************/
1698 /* Initialize the arc0s device.
1700 static int arcnetS_init(struct net_device *dev)
1702 struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1704 arcnet_setup(dev);
1706 /* And now fill particular fields with arcnet values */
1707 dev->dev_addr[0]=lp->stationid;
1708 dev->hard_header_len=sizeof(struct S_ClientData);
1709 dev->mtu=512-sizeof(struct archdr)-dev->hard_header_len
1710 + S_EXTRA_CLIENTDATA;
1711 dev->open=arcnetS_open_close;
1712 dev->stop=arcnetS_open_close;
1713 dev->hard_start_xmit=arcnetS_send_packet;
1714 dev->hard_header=arcnetS_header;
1715 dev->rebuild_header=arcnetS_rebuild_header;
1717 return 0;
1721 /* Bring up/down the arc0s device - we don't actually have to do anything,
1722 * since our parent arc0 handles the card I/O itself.
1724 static int arcnetS_open_close(struct net_device *dev)
1726 return 0;
1730 /* Called by the kernel in order to transmit an RFC1051-type packet.
1732 static int
1733 arcnetS_send_packet(struct sk_buff *skb, struct net_device *dev)
1735 struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1736 int bad,length;
1737 struct S_ClientData *hdr=(struct S_ClientData *)skb->data;
1739 lp->intx++;
1741 bad=arcnet_send_packet_bad(skb,dev);
1742 if (bad)
1744 lp->intx--;
1745 return bad;
1748 /* arcnet_send_packet_pad has already set tbusy - don't bother here. */
1750 length = 1 < skb->len ? skb->len : 1;
1752 BUGLVL(D_SKB) arcnet_dump_skb(dev,skb,"tx");
1754 /* fits in one packet? */
1755 if (length-S_EXTRA_CLIENTDATA<=XMTU)
1757 (*lp->prepare_tx)(dev,
1758 skb->data+S_EXTRA_CLIENTDATA,
1759 sizeof(struct S_ClientData)-S_EXTRA_CLIENTDATA,
1760 skb->data+sizeof(struct S_ClientData),
1761 length-sizeof(struct S_ClientData),
1762 hdr->daddr,0,0);
1764 /* done right away */
1765 dev_kfree_skb(skb);
1767 if (arcnet_go_tx(dev,1))
1769 /* inform upper layers */
1770 arcnet_tx_done(lp->adev, lp);
1773 else /* too big for one - not accepted */
1775 BUGMSG(D_NORMAL,"packet too long (length=%d)\n",
1776 length);
1777 dev_kfree_skb(skb);
1778 lp->stats.tx_dropped++;
1779 arcnet_tx_done(lp->adev, lp);
1782 dev->trans_start=jiffies;
1783 lp->intx--;
1785 /* make sure we didn't ignore a TX IRQ while we were in here */
1786 lp->intmask |= TXFREEflag;
1787 SETMASK;
1789 return 0;
1793 /* Packet receiver for RFC1051 packets;
1795 static void
1796 arcnetS_rx(struct net_device *dev,u_char *buf,
1797 int length,u_char saddr, u_char daddr)
1799 struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1800 struct sk_buff *skb;
1801 struct S_ClientData *arcsoft,*soft;
1803 arcsoft=(struct S_ClientData *)(buf-S_EXTRA_CLIENTDATA);
1804 length+=S_EXTRA_CLIENTDATA;
1806 BUGMSG(D_DURING,"it's an RFC1051 packet (length=%d)\n",
1807 length);
1809 { /* was "if not split" in A protocol, S is never split */
1811 skb = alloc_skb(length, GFP_ATOMIC);
1812 if (skb == NULL) {
1813 BUGMSG(D_NORMAL,"Memory squeeze, dropping packet.\n");
1814 lp->stats.rx_dropped++;
1815 return;
1817 soft=(struct S_ClientData *)skb->data;
1818 skb_put(skb,length);
1820 memcpy((u_char *)soft + sizeof(struct S_ClientData) - S_EXTRA_CLIENTDATA,
1821 (u_char *)arcsoft + sizeof(struct S_ClientData) -S_EXTRA_CLIENTDATA,
1822 length - sizeof(struct S_ClientData) + S_EXTRA_CLIENTDATA);
1823 soft->protocol_id=arcsoft->protocol_id;
1824 soft->daddr=daddr;
1825 soft->saddr=saddr;
1826 skb->dev = dev; /* is already lp->sdev */
1828 BUGLVL(D_SKB) arcnet_dump_skb(dev,skb,"rx");
1830 lp->stats.rx_bytes += skb->len;
1831 skb->protocol=arcnetS_type_trans(skb,dev);
1832 netif_rx(skb);
1837 /* Create the ARCnet ClientData header for an arbitrary protocol layer
1839 * saddr=NULL means use device source address (always will anyway)
1840 * daddr=NULL means leave destination address (eg unresolved arp)
1842 static int arcnetS_header(struct sk_buff *skb,struct net_device *dev,
1843 unsigned short type,void *daddr,void *saddr,unsigned len)
1845 struct S_ClientData *head = (struct S_ClientData *)
1846 skb_push(skb,dev->hard_header_len);
1847 struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
1849 /* set the protocol ID according to RFC1051 */
1850 switch(type)
1852 case ETH_P_IP:
1853 head->protocol_id=ARC_P_IP_RFC1051;
1854 BUGMSG(D_DURING,"S_header: IP_RFC1051 packet.\n");
1855 break;
1856 case ETH_P_ARP:
1857 head->protocol_id=ARC_P_ARP_RFC1051;
1858 BUGMSG(D_DURING,"S_header: ARP_RFC1051 packet.\n");
1859 break;
1860 default:
1861 BUGMSG(D_NORMAL,"I don't understand protocol %d (%Xh)\n",
1862 type,type);
1863 lp->stats.tx_errors++;
1864 lp->stats.tx_aborted_errors++;
1865 return 0;
1869 * Set the source hardware address.
1871 * This is pretty pointless for most purposes, but it can help
1872 * in debugging. saddr is stored in the ClientData header and
1873 * removed before sending the packet (since ARCnet does not allow
1874 * us to change the source address in the actual packet sent)
1876 if(saddr)
1877 head->saddr=((u_char*)saddr)[0];
1878 else
1879 head->saddr=((u_char*)(dev->dev_addr))[0];
1881 /* supposedly if daddr is NULL, we should ignore it... */
1882 if(daddr)
1884 head->daddr=((u_char*)daddr)[0];
1885 return dev->hard_header_len;
1887 else
1888 head->daddr=0; /* better fill one in anyway */
1890 return -dev->hard_header_len;
1894 /* Rebuild the ARCnet ClientData header. This is called after an ARP
1895 * (or in future other address resolution) has completed on this
1896 * sk_buff. We now let ARP fill in the other fields.
1898 static int arcnetS_rebuild_header(struct sk_buff *skb)
1900 struct net_device *dev=skb->dev;
1901 struct S_ClientData *head = (struct S_ClientData *)skb->data;
1902 struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
1905 * Only ARP and IP are currently supported
1908 if(head->protocol_id != ARC_P_IP_RFC1051)
1910 BUGMSG(D_NORMAL,"I don't understand protocol type %d (%Xh) addresses!\n",
1911 head->protocol_id,head->protocol_id);
1912 lp->stats.tx_errors++;
1913 lp->stats.tx_aborted_errors++;
1914 head->daddr=0;
1915 /*memcpy(eth->h_source, dev->dev_addr, dev->addr_len);*/
1916 return 0;
1920 * Try to get ARP to resolve the header.
1922 #ifdef CONFIG_INET
1923 return arp_find(&(head->daddr),skb)? 1 : 0;
1924 #else
1925 return 0;
1926 #endif
1930 /* Determine a packet's protocol ID.
1932 * With ARCnet we have to convert everything to Ethernet-style stuff.
1934 unsigned short arcnetS_type_trans(struct sk_buff *skb,struct net_device *dev)
1936 struct S_ClientData *head;
1937 struct arcnet_local *lp=(struct arcnet_local *) (dev->priv);
1939 /* Pull off the arcnet header. */
1940 skb->mac.raw=skb->data;
1941 skb_pull(skb,dev->hard_header_len);
1942 head=(struct S_ClientData *)skb->mac.raw;
1944 if (head->daddr==0)
1945 skb->pkt_type=PACKET_BROADCAST;
1946 else if (dev->flags&IFF_PROMISC)
1948 /* if we're not sending to ourselves :) */
1949 if (head->daddr != dev->dev_addr[0])
1950 skb->pkt_type=PACKET_OTHERHOST;
1953 /* now return the protocol number */
1954 switch (head->protocol_id)
1956 case ARC_P_IP_RFC1051: return htons(ETH_P_IP);
1957 case ARC_P_ARP_RFC1051: return htons(ETH_P_ARP);
1958 case ARC_P_ATALK: return htons(ETH_P_ATALK); /* untested appletalk */
1959 default:
1960 lp->stats.rx_errors++;
1961 lp->stats.rx_crc_errors++;
1962 return 0;
1965 return htons(ETH_P_IP);
1968 #endif /* CONFIG_ARCNET_1051 */
1971 /****************************************************************************
1973 * Kernel Loadable Module Support *
1975 ****************************************************************************/
1977 #ifdef MODULE
1979 void cleanup_module(void)
1981 printk("Generic arcnet support removed.\n");
1984 void arcnet_use_count(int open)
1986 if (open)
1987 MOD_INC_USE_COUNT;
1988 else
1989 MOD_DEC_USE_COUNT;
1992 #else
1994 void arcnet_use_count(int open)
1998 struct net_device arcnet_devs[MAX_ARCNET_DEVS];
1999 int arcnet_num_devs=0;
2000 char arcnet_dev_names[MAX_ARCNET_DEVS][10];
2002 void __init arcnet_init(void)
2004 int c;
2006 init_module();
2008 /* Don't register_netdev here. The chain hasn't been initialised. */
2010 #ifdef CONFIG_ARCNET_COM90xx
2011 if ((!com90xx_explicit) && arcnet_num_devs < MAX_ARCNET_DEVS)
2013 arcnet_devs[arcnet_num_devs].init=arc90xx_probe;
2014 arcnet_devs[arcnet_num_devs].name=
2015 (char *)&arcnet_dev_names[arcnet_num_devs];
2016 arcnet_num_devs++;
2018 #endif
2020 if (!arcnet_num_devs)
2022 printk("Don't forget to load the chipset driver.\n");
2023 return;
2026 /* Link into the device chain */
2028 /* Q: Should we put ourselves at the beginning or the end of the chain? */
2029 /* Probably the end, because we're not so fast, but... */
2031 for (c=0; c< (arcnet_num_devs-1); c++)
2032 arcnet_devs[c].next=&arcnet_devs[c+1];
2034 write_lock_bh(&dev_base_lock);
2035 arcnet_devs[c].next=dev_base;
2036 dev_base=&arcnet_devs[0];
2037 write_unlock_bh(&dev_base_lock);
2039 /* Give names to those without them */
2041 for (c=0; c< arcnet_num_devs; c++)
2042 if (!arcnet_dev_names[c][0])
2043 arcnet_makename((char *)&arcnet_dev_names[c]);
2046 #endif /* MODULE */
2049 #ifdef MODULE
2050 int init_module(void)
2051 #else
2052 static int __init init_module(void)
2053 #endif
2055 #ifdef ALPHA_WARNING
2056 BUGLVL(D_EXTRA)
2058 printk("arcnet: ***\n");
2059 printk("arcnet: * Read arcnet.txt for important release notes!\n");
2060 printk("arcnet: *\n");
2061 printk("arcnet: * This is an ALPHA version! (Last stable release: v2.56) E-mail me if\n");
2062 printk("arcnet: * you have any questions, comments, or bug reports.\n");
2063 printk("arcnet: ***\n");
2065 #endif
2067 printk("%sAvailable protocols: ARCnet RFC1201"
2068 #ifdef CONFIG_ARCNET_ETH
2069 ", Ethernet-Encap"
2070 #endif
2071 #ifdef CONFIG_ARCNET_1051
2072 ", ARCnet RFC1051"
2073 #endif
2074 #ifdef MODULE
2075 ".\nDon't forget to load the chipset driver"
2076 #endif
2077 ".\n",version);
2078 return 0;
2082 void arcnet_makename(char *device)
2084 struct net_device *dev;
2085 int arcnum;
2087 arcnum = 0;
2088 for (;;)
2090 sprintf(device, "arc%d", arcnum);
2091 read_lock_bh(&dev_base_lock);
2092 for (dev = dev_base; dev; dev=dev->next)
2093 if (dev->name != device && !strcmp(dev->name, device))
2094 break;
2095 read_unlock_bh(&dev_base_lock);
2096 if (!dev)
2097 return;
2098 arcnum++;