source: rtems/c/src/lib/libbsp/i386/pc386/ne2000/ne2000.c @ 4c39764

4.104.115
Last change on this file since 4c39764 was 4c39764, checked in by Chris Johns <chrisj@…>, on 10/15/09 at 06:30:08

2009-10-15 Chris Johns <chrisj@…>

  • ne2000/ne2000.c: Add --ne2k-irq and --ne2k-port boot command line configure options.
  • ide/ide.c: Fix a bug which left 4 words in the buffer of the disk. Some devices do not follow the standard and terminate the command which a new command occurs and/or low data ready when data is still to be read.
  • Property mode set to 100644
File size: 31.5 KB
Line 
1/*  ne2k.c -- RTEMS NE2000 Ethernet driver.
2 *  Written by Ian Lance Taylor, Zembu Labs.
3 *  October, 1998.
4 *
5 *  The license and distribution terms for this file may be
6 *  found in found in the file LICENSE in this distribution or at
7 *  http://www.rtems.com/license/LICENSE.
8 *
9 *  $Id$
10 *
11 *  Both the ne2000 and the wd80x3 are based on the National Semiconductor
12 *  8390 chip, so there is a fair amount of overlap between the two
13 *  drivers.  It would be possible in principle to combine some code into
14 *  a separate set of subroutines called by both.  In fact, the drivers in
15 *  both OpenBSD and Linux work this way.  I didn't bother, because for
16 *  the relatively simple drivers used by RTEMS, the overlap is not
17 *  especially large, and any reasonable use of subroutines would lead to
18 *  slightly less efficient code.
19
20 *  This ne2000 driver uses two transmit buffers.  While one packet is
21 *  being transmitted over the Ethernet, RTEMS will upload another.  Since
22 *  uploading a packet to the ne2000 is rather slow, I don't think there
23 *  is any point to having more than two transmit buffers.  However, the
24 *  code does make it possible, by changing NE_TX_BUFS, although that
25 *  would of course reduce the number of receive buffers.
26 *
27 *  I suspect that the wd80x3 driver would benefit slightly from copying
28 *  the multiple transmit buffer code.  However, I have no way to test
29 *  that.
30 */
31
32#include <bsp.h>
33#include <wd80x3.h>
34
35#include <stdio.h>
36#include <stdlib.h>
37#include <assert.h>
38#include <errno.h>
39
40#include <rtems/error.h>
41#include <rtems/rtems_bsdnet.h>
42
43#include <sys/param.h>
44#include <sys/mbuf.h>
45#include <sys/socket.h>
46#include <sys/sockio.h>
47
48#include <net/if.h>
49
50#include <netinet/in.h>
51#include <netinet/if_ether.h>
52
53#include <bsp/irq.h>
54
55/* Define this to force byte-wide data transfers with the NIC. This
56   is needed for boards like the TS-1325 386EX PC, which support only
57   an 8-bit PC/104 bus.  Undefine this on a normal PC.*/
58
59/* #define NE2000_BYTE_TRANSFERS */
60
61/* Define this to print debugging messages with printk.  */
62
63/* #define DEBUG_NE2000 */
64/* #define DEBUG_NE */
65
66/* We expect to be able to read a complete packet into an mbuf.  */
67
68#if (MCLBYTES < 1520)
69# error "Driver must have MCLBYTES >= 1520"
70#endif
71
72/* The 8390 macro definitions in wd80x3.h expect RO to be defined.  */
73#define RO 0
74
75/* Minimum size of Ethernet packet.  */
76#define ET_MINLEN 60
77
78/* The number of NE2000 devices supported by this driver.  */
79
80#define NNEDRIVER 1
81
82/* RTEMS event number used by the interrupt handler to signal the
83   driver task.  This must not be any of the events used by the
84   network task synchronization.  */
85#define INTERRUPT_EVENT RTEMS_EVENT_1
86
87/* RTEMS event number used to start the transmit daemon.  This must
88   not be the same as INTERRUPT_EVENT.  */
89#define START_TRANSMIT_EVENT RTEMS_EVENT_2
90
91/* Interrupts we want to handle from the device.  */
92
93#define NE_INTERRUPTS \
94  (MSK_PRX | MSK_PTX | MSK_RXE | MSK_TXE | MSK_OVW | MSK_CNT)
95
96/* The size of a page in device memory.  */
97
98#define NE_PAGE_SIZE (256)
99
100/* The first page address in device memory.  */
101
102#define NE_START_PAGE (0x40)
103
104/* The last page address, plus 1.  */
105
106#define NE_STOP_PAGE (0x80)
107
108/* The number of pages used for a single transmit buffer.  This is
109   1536 bytes, enough for a full size packet.  */
110
111#define NE_TX_PAGES (6)
112
113/* The number of transmit buffers.  We use two, so we can load one
114   packet while the other is being sent.  */
115
116#define NE_TX_BUFS (2)
117
118/* We use the first pages in memory as transmit buffers, and the
119   remaining ones as receive buffers.  */
120
121#define NE_FIRST_TX_PAGE (NE_START_PAGE)
122
123#define NE_FIRST_RX_PAGE (NE_FIRST_TX_PAGE + NE_TX_PAGES * NE_TX_BUFS)
124
125/* Data we store for each NE2000 device.  */
126
127struct ne_softc {
128  /* The bsdnet information structure.  */
129  struct arpcom arpcom;
130
131  /* The interrupt request number.  */
132  unsigned int irno;
133  /* The base IO port number.  */
134  unsigned int port;
135
136  /* Whether we accept broadcasts.  */
137  int accept_broadcasts;
138
139  /* The thread ID of the transmit task.   */
140  rtems_id tx_daemon_tid;
141  /* The thread ID of the receive task.  */
142  rtems_id rx_daemon_tid;
143
144  /* Whether we use byte-transfers with the device. */
145  bool byte_transfers;
146
147  /* The number of memory buffers which the transmit daemon has loaded
148     with data to be sent, but which have not yet been completely
149     sent.  */
150  int inuse;
151  /* The index of the next available transmit memory buffer.  */
152  int nextavail;
153  /* The index of the next transmit buffer to send.  */
154  int nextsend;
155  /* Nonzero if the device is currently transmitting a packet.  */
156  int transmitting;
157  /* The length of the data stored in each transmit buffer.  */
158  int sendlen[NE_TX_BUFS];
159
160  /* Set if we have a packet overrun while receiving.  */
161  int overrun;
162  /* Set if we should resend after an overrun.  */
163  int resend;
164
165  /* Statistics.  */
166  struct {
167    /* Number of packets received.  */
168    unsigned long rx_packets;
169    /* Number of packets sent.  */
170    unsigned long tx_packets;
171    /* Number of interrupts.  */
172    unsigned long interrupts;
173    /* Number of receive acknowledgements.  */
174    unsigned long rx_acks;
175    /* Number of transmit acknowledgements.  */
176    unsigned long tx_acks;
177    /* Number of packet overruns.  */
178    unsigned long overruns;
179    /* Number of frame errors.  */
180    unsigned long rx_frame_errors;
181    /* Number of CRC errors.  */
182    unsigned long rx_crc_errors;
183    /* Number of missed packets.  */
184    unsigned long rx_missed_errors;
185  } stats;
186};
187
188/* The list of NE2000 devices on this system.  */
189
190static struct ne_softc ne_softc[NNEDRIVER];
191
192/*
193 * receive ring descriptor
194 *
195 * The National Semiconductor DS8390 Network interface controller uses
196 * the following receive ring headers.  The way this works is that the
197 * memory on the interface card is chopped up into 256 bytes blocks.
198 * A contiguous portion of those blocks are marked for receive packets
199 * by setting start and end block #'s in the NIC.  For each packet that
200 * is put into the receive ring, one of these headers (4 bytes each) is
201 * tacked onto the front. The first byte is a copy of the receiver status
202 * register at the time the packet was received.
203 */
204struct ne_ring
205{
206        unsigned char rsr;    /* receiver status */
207        unsigned char next;   /* pointer to next packet */
208        unsigned short count; /* bytes in packet (length + 4)   */
209};
210
211/* Forward declarations to avoid warnings */
212
213static void ne_init_irq_handler (int irno);
214static void ne_stop (struct ne_softc *sc);
215static void ne_stop_hardware (struct ne_softc *sc);
216static void ne_init (void *arg);
217static void ne_init_hardware (struct ne_softc *sc);
218
219static void ne_reset(struct ne_softc *sc);
220#ifdef DEBUG_NE
221static void ne_dump(struct ne_softc *sc);
222#endif
223
224/* Find the NE2000 device which is attached at a particular interrupt
225   vector.  */
226
227static struct ne_softc *
228ne_device_for_irno (int irno)
229{
230  int i;
231
232  for (i = 0; i < NNEDRIVER; ++i)
233    {
234      if (ne_softc[i].irno == irno
235          && ne_softc[i].arpcom.ac_if.if_softc != NULL)
236        return &ne_softc[i];
237    }
238
239  return NULL;
240}
241
242/* Read data from an NE2000 device.  Read LEN bytes at ADDR, storing
243   them into P.  */
244
245static void
246ne_read_data (struct ne_softc *sc, int addr, int len, unsigned char *p)
247{
248  unsigned int port = sc->port;
249  unsigned int dport = port + DATAPORT;
250
251  outport_byte (port + CMDR, MSK_PG0 | MSK_RD2 | MSK_STA);
252  outport_byte (port + RBCR0, len);
253  outport_byte (port + RBCR1, len >> 8);
254  outport_byte (port + RSAR0, addr);
255  outport_byte (port + RSAR1, addr >> 8);
256  outport_byte (port + CMDR, MSK_PG0 | MSK_RRE | MSK_STA);
257
258  if (sc->byte_transfers)
259  {
260    unsigned char d;
261    while (len > 0)
262    {
263      inport_byte(dport, d);
264      *p++ = d;
265      len--;
266    }
267  }
268  else  /* word transfers */
269  {
270    unsigned short d;
271    while (len > 1)
272    {
273      inport_word(dport, d);
274      *p++ = d;
275      *p++ = d >> 8;
276      len -= 2;
277    }
278    if (len)
279    {
280      inport_word(dport, d);
281      *p++ = d;
282    }
283  }
284
285  outport_byte (port + ISR, MSK_RDC);
286}
287
288/* Handle the current NE2000 status.  This is called when the device
289   signals an interrupt.  It is also called at other times while
290   NE2000 interrupts have been disabled.  */
291
292static void
293ne_check_status (struct ne_softc *sc, int from_irq_handler)
294{
295  struct ifnet *ifp = &sc->arpcom.ac_if;
296  unsigned int port = sc->port;
297  unsigned char status;
298
299  /* It seems that we need to use a loop here, because if the NE2000
300     signals an interrupt because packet transmission is complete, and
301     then receives a packet while interrupts are disabled, it seems to
302     sometimes fail to signal the interrupt for the received packet
303     when interrupts are reenabled.  (Based on the behaviour of the
304     Realtek 8019AS chip).  */
305
306  /* int count = 0; */
307  while (1)
308  {
309    inport_byte (port + ISR, status);
310    if (status == 0)
311      break;
312
313    /* ack */
314    outport_byte (port + ISR, status);
315
316#ifdef DEBUG_NE2000
317    printk ("NE2000 status 0x%x (8259 enabled: %s; mask: %x)\n", status,
318            i8259s_cache & (1 << sc->irno) ? "no" : "yes",
319            i8259s_cache);
320#endif
321
322    /* Check for incoming packet overwrite.  */
323    if (status & MSK_OVW)
324    {
325      ifp->if_timer = 0;
326#ifdef DEBUG_NE
327      printk("^");
328#endif
329      ++sc->stats.overruns;
330      ne_reset(sc);
331      /* Reenable device interrupts.  */
332      if (from_irq_handler)
333        outport_byte(port + IMR, NE_INTERRUPTS);
334      return;
335    }
336
337    /* Check for transmitted packet.  The transmit daemon may now be
338       able to send another packet to the device.  */
339    if ((status & (MSK_PTX | MSK_TXE)) != 0)
340    {
341      ifp->if_timer = 0;
342      ++sc->stats.tx_acks;
343      --sc->inuse;
344      sc->transmitting = 0;
345      if (sc->inuse > 0 || (sc->arpcom.ac_if.if_flags & IFF_OACTIVE) != 0)
346        rtems_event_send (sc->tx_daemon_tid, START_TRANSMIT_EVENT);
347    }
348
349    /* Check for received packet.  */
350    if ((status & (MSK_PRX | MSK_RXE)) != 0)
351    {
352      ++sc->stats.rx_acks;
353      rtems_event_send (sc->rx_daemon_tid, INTERRUPT_EVENT);
354    }
355
356    /* Check for counter change.  */
357    if ((status & MSK_CNT) != 0)
358    {
359      unsigned char add;
360      inport_byte (port + CNTR0, add);
361      sc->stats.rx_frame_errors += add;
362      inport_byte (port + CNTR1, add);
363      sc->stats.rx_crc_errors += add;
364      inport_byte (port + CNTR2, add);
365      sc->stats.rx_missed_errors += add;
366    }
367
368    break;
369    /* if (++count >= 1000)
370    {
371      printk("status: %x\n", status);
372      ne_reset(sc);
373      if (from_irq_handler)
374        outport_byte(port + IMR, NE_INTERRUPTS);
375      return;
376    } */
377  }
378
379  outport_byte (port + CMDR, MSK_PG0 | MSK_STA | MSK_RD2);
380}
381
382/* Handle an NE2000 interrupt.  */
383
384static void
385ne_interrupt_handler (rtems_irq_hdl_param cdata)
386{
387  rtems_vector_number v = (rtems_vector_number) cdata;
388  struct ne_softc *sc;
389
390  sc = ne_device_for_irno (v);
391  if (sc == NULL)
392    return;
393
394  ++sc->stats.interrupts;
395
396#ifdef DEBUG_NE
397  printk("!");
398#endif
399  ne_check_status(sc, 1);
400}
401
402/* Turn NE2000 interrupts on.  */
403
404static void
405ne_interrupt_on (const rtems_irq_connect_data *irq)
406{
407  struct ne_softc *sc;
408
409#ifdef DEBUG_NE
410  printk ("ne_interrupt_on()\n");
411#endif
412  sc = ne_device_for_irno (irq->name);
413  if (sc != NULL)
414    outport_byte (sc->port + IMR, NE_INTERRUPTS);
415}
416
417/* Turn NE2000 interrupts off.  See ne_interrupt_on.  */
418
419static void
420ne_interrupt_off (const rtems_irq_connect_data *irq)
421{
422  struct ne_softc *sc;
423
424#ifdef DEBUG_NE
425  printk ("ne_interrupt_off()\n");
426#endif
427  sc = ne_device_for_irno (irq->name);
428  if (sc != NULL)
429    outport_byte (sc->port + IMR, 0);
430}
431
432/* Return whether NE2000 interrupts are on.  */
433
434static int
435ne_interrupt_is_on (const rtems_irq_connect_data *irq)
436{
437  return BSP_irq_enabled_at_i8259s (irq->name);
438}
439
440/* Initialize the NE2000 hardware.  */
441
442static void
443ne_init_hardware (struct ne_softc *sc)
444{
445  unsigned int port = sc->port;
446  int i;
447
448#ifdef DEBUG_NE2000
449  printk ("ne_init_hardware()\n");
450#endif
451
452  /* Initialize registers.  */
453
454  /* Set interface for page 0, Remote DMA complete, Stopped */
455  outport_byte (port + CMDR, MSK_PG0 | MSK_RD2 | MSK_STP);
456
457  /* Set FIFO threshold to 8, No auto-init Remote DMA, byte order=80x86 */
458  /* byte-wide DMA xfers */
459  if (sc->byte_transfers)
460    outport_byte (port + DCR, MSK_FT10 | MSK_BMS);
461  /* word-wide DMA xfers */
462  else
463    outport_byte (port + DCR, MSK_FT10 | MSK_BMS | MSK_WTS);
464
465  /* Clear Remote Byte Count Registers */
466  outport_byte (port + RBCR0, 0);
467  outport_byte (port + RBCR1, 0);
468
469  /* For the moment, don't store incoming packets in memory. */
470  outport_byte (port + RCR, MSK_MON);
471
472  /* Place NIC in internal loopback mode */
473  outport_byte (port + TCR, MSK_LOOP);
474
475  /* Initialize transmit/receive (ring-buffer) Page Start */
476  outport_byte (port + TPSR, NE_FIRST_TX_PAGE);
477  outport_byte (port + PSTART, NE_FIRST_RX_PAGE);
478
479  /* Initialize Receiver (ring-buffer) Page Stop and Boundary */
480  outport_byte (port + PSTOP, NE_STOP_PAGE);
481  outport_byte (port + BNRY, NE_STOP_PAGE - 1);
482
483  /* Clear all interrupts */
484  outport_byte (port + ISR, 0xff);
485  /* Disable all interrupts */
486  outport_byte (port + IMR, 0);
487
488  /* Program Command Register for page 1 */
489  outport_byte (port + CMDR, MSK_PG1 | MSK_RD2 | MSK_STP);
490
491  /* Set the Ethernet hardware address.  */
492  for (i = 0; i < ETHER_ADDR_LEN; ++i)
493    outport_byte (port + PAR + i, sc->arpcom.ac_enaddr[i]);
494
495  /* Set Current Page pointer to next_packet */
496  outport_byte (port + CURR, NE_FIRST_RX_PAGE);
497
498  /* Clear the multicast address.  */
499  for (i = 0; i < MARsize; ++i)
500    outport_byte (port + MAR + i, 0);
501
502  /* Set page 0 registers */
503  outport_byte (port + CMDR, MSK_PG0 | MSK_RD2 | MSK_STP);
504
505  /* accept broadcast */
506  outport_byte (port + RCR, (sc->accept_broadcasts ? MSK_AB : 0));
507
508  /* Start interface */
509  outport_byte (port + CMDR, MSK_PG0 | MSK_RD2 | MSK_STA);
510
511  /* Take interface out of loopback */
512  outport_byte (port + TCR, 0);
513}
514
515/* Set up interrupts.
516*/
517static void
518ne_init_irq_handler(int irno)
519{
520  rtems_irq_connect_data irq;
521
522#ifdef DEBUG_NE
523  printk("ne_init_irq_handler(%d)\n", irno);
524#endif
525  irq.name = irno;
526  irq.hdl = ne_interrupt_handler;
527  irq.handle = (rtems_irq_hdl) irno;
528  irq.on = ne_interrupt_on;
529  irq.off = ne_interrupt_off;
530  irq.isOn = ne_interrupt_is_on;
531
532  if (!BSP_install_rtems_irq_handler (&irq))
533    rtems_panic ("Can't attach NE interrupt handler for irq %d\n", irno);
534}
535
536/* The NE2000 packet receive daemon.  This task is started when the
537   NE2000 driver is initialized.  */
538
539#ifdef DEBUG_NE
540static int ccc = 0; /* experinent! */
541#endif
542
543static void
544ne_rx_daemon (void *arg)
545{
546  struct ne_softc *sc = (struct ne_softc *) arg;
547  struct ifnet *ifp = &sc->arpcom.ac_if;
548  unsigned int port = sc->port;
549
550  while (1)
551  {
552    rtems_event_set events;
553
554    /* Wait for the interrupt handler to tell us that there is a
555       packet ready to receive.  */
556    rtems_bsdnet_event_receive (INTERRUPT_EVENT,
557                                RTEMS_WAIT | RTEMS_EVENT_ANY,
558                                RTEMS_NO_TIMEOUT,
559                                &events);
560
561    /* Don't let the device interrupt us now.  */
562    outport_byte (port + IMR, 0);
563
564    while (1)
565    {
566      unsigned char startpage, currpage;
567      unsigned short len;
568      unsigned char next, stat, cnt1, cnt2;
569      struct mbuf *m;
570      unsigned char *p;
571      int startaddr;
572      int toend;
573      struct ether_header *eh;
574      struct ne_ring hdr; /* ring buffer header */
575      int reset;
576
577      inport_byte (port + BNRY, startpage);
578
579      outport_byte (port + CMDR, MSK_PG1 | MSK_RD2);
580      inport_byte (port + CURR, currpage);
581      outport_byte (port + CMDR, MSK_PG0 | MSK_RD2);
582
583      ++startpage;
584      if (startpage >= NE_STOP_PAGE)
585        startpage = NE_FIRST_RX_PAGE;
586
587      if (startpage == currpage)
588        break;
589
590#ifdef DEBUG_NE2000
591      printk ("ne_rx_daemon: start page %x; current page %x\n",
592              startpage, currpage);
593#endif
594
595      reset = 0;
596
597      /* Read the buffer header */
598      startaddr = startpage * NE_PAGE_SIZE;
599      ne_read_data(sc, startaddr, sizeof(hdr), (unsigned char *)&hdr);
600      next = hdr.next;
601      if (next >= NE_STOP_PAGE)
602        next = NE_FIRST_RX_PAGE;
603
604      /* check packet length */
605      len = hdr.count;
606      if (currpage < startpage)
607        cnt1 = currpage + (NE_STOP_PAGE - NE_FIRST_RX_PAGE) - startpage;
608      else
609        cnt1 = currpage - startpage;
610      cnt2 = len / NE_PAGE_SIZE;
611      if (len % NE_PAGE_SIZE)
612        cnt2++;
613      if (cnt1 < cnt2)
614      {
615#ifdef DEBUG_NE
616        printk("(%x<%x:%x)", cnt1, cnt2, len);
617/*
618        printk("start page 0x%x; current page 0x%x\n",
619                startpage, currpage);
620        printk("cnt1 < cnt2 (0x%x, 0x%x); len 0x%x\n",
621                cnt1, cnt2, len);
622*/
623#endif
624        reset = 1;
625      }
626      if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN + sizeof(struct ne_ring)) ||
627          len < (ETHER_MIN_LEN - ETHER_CRC_LEN + sizeof(struct ne_ring)) ||
628          len > MCLBYTES)
629      {
630#ifdef DEBUG_NE
631        printk("(%x)", len);
632/*
633        printk("start page 0x%x; current page 0x%x\n",
634                startpage, currpage);
635        printk("len out of range: 0x%x\n", len);
636        printk("stat: 0x%x, next: 0x%x\n", hdr.rsr, hdr.next);
637*/
638#endif
639        reset = 1;
640      }
641#ifdef DEBUG_NE
642      if (++ccc == 100)
643      { ccc = 0; reset = 1;
644        printk("T");
645      }
646#endif
647
648      /* reset interface */
649      if (reset)
650      {
651        ne_reset(sc);
652        goto Next;
653      }
654
655      stat = hdr.rsr;
656
657      /* The first four bytes of the length are the buffer header.  */
658      len -= sizeof(struct ne_ring);
659      startaddr += sizeof(struct ne_ring);
660
661      MGETHDR (m, M_WAIT, MT_DATA);
662      MCLGET (m, M_WAIT);
663      m->m_pkthdr.rcvif = ifp;
664
665      p = mtod (m, unsigned char *);
666      m->m_len = m->m_pkthdr.len = len - sizeof(struct ether_header);
667
668      toend = NE_STOP_PAGE * NE_PAGE_SIZE - startaddr;
669      if (toend < len)
670      {
671        ne_read_data (sc, startaddr, toend, p);
672        p += toend;
673        len -= toend;
674        startaddr = NE_FIRST_RX_PAGE * NE_PAGE_SIZE;
675      }
676
677      if (len > 0)
678        ne_read_data (sc, startaddr, len, p);
679
680      eh = mtod (m, struct ether_header *);
681      m->m_data += sizeof (struct ether_header);
682
683#ifdef DEBUG_NE
684  /* printk("[r%d]", hdr.count - sizeof(hdr)); */
685  printk("<");
686#endif
687      ether_input (ifp, eh, m);
688      ++sc->stats.rx_packets;
689
690      outport_byte (port + BNRY, next - 1);
691    }
692
693    if (sc->overrun) {
694      outport_byte (port + ISR, MSK_OVW);
695      outport_byte (port + TCR, 0);
696      if (sc->resend)
697        outport_byte (port + CMDR, MSK_PG0 | MSK_TXP | MSK_RD2 | MSK_STA);
698      sc->resend = 0;
699      sc->overrun = 0;
700    }
701
702  Next:
703    /* Reenable device interrupts.  */
704    outport_byte (port + IMR, NE_INTERRUPTS);
705  }
706}
707
708/* Load an NE2000 packet onto the device.  */
709
710static void
711ne_loadpacket (struct ne_softc *sc, struct mbuf *m)
712{
713  unsigned int port = sc->port;
714  unsigned int dport = port + DATAPORT;
715  struct mbuf *mhold = m;
716  int leftover;
717  unsigned char leftover_data;
718  int timeout;
719  int send_cnt = 0;
720
721#ifdef DEBUG_NE2000
722  printk ("Uploading NE2000 packet\n");
723#endif
724
725  /* Reset remote DMA complete flag.  */
726  outport_byte (port + ISR, MSK_RDC);
727
728  /* Write out the count.  */
729  outport_byte (port + RBCR0, m->m_pkthdr.len);
730  outport_byte (port + RBCR1, m->m_pkthdr.len >> 8);
731
732  sc->sendlen[sc->nextavail] = m->m_pkthdr.len;
733
734  /* Tell the device which address we want to write to.  */
735  outport_byte (port + RSAR0, 0);
736  outport_byte (port + RSAR1,
737                NE_FIRST_TX_PAGE + (sc->nextavail * NE_TX_PAGES));
738
739  /* Set up the write.  */
740  outport_byte (port + CMDR, MSK_PG0 | MSK_RWR | MSK_STA);
741
742  /* Transfer the mbuf chain to device memory.  NE2000 devices require
743     that the data be transferred as words, so we need to handle odd
744     length mbufs.  Never occurs if we force byte transfers. */
745
746  leftover = 0;
747  leftover_data = '\0';
748
749  for (; m != NULL; m = m->m_next) {
750    int len;
751    unsigned char *data;
752
753    len = m->m_len;
754    if (len == 0)
755      continue;
756
757    data = mtod (m, unsigned char *);
758
759    if (leftover) {
760      unsigned char next;
761
762      /* Data left over from previous mbuf in chain.  */
763      next = *data++;
764      --len;
765      outport_word (dport, leftover_data | (next << 8));
766      send_cnt += 2;
767      leftover = 0;
768    }
769
770    /* If using byte transfers, len always ends up as zero so
771       there are no leftovers. */
772
773    if (sc->byte_transfers)
774      while (len > 0) {
775        outport_byte (dport, *data++);
776        len--;
777      }
778    else
779      while (len > 1) {
780        outport_word (dport, data[0] | (data[1] << 8));
781        data += 2;
782        len -= 2;
783        send_cnt += 2;
784      }
785
786    if (len > 0)
787      {
788        leftover = 1;
789        leftover_data = *data++;
790      }
791  }
792
793  if (leftover)
794  {
795    outport_word (dport, leftover_data);
796    send_cnt += 2;
797  }
798
799#ifdef DEBUG_NE
800  /* printk("{l%d|%d}", send_cnt, sc->nextavail); */
801  printk("v");
802#endif
803  m_freem (mhold);
804
805  /* Wait for the device to complete accepting the data, with a
806     limiting counter so that we don't wait too long.  */
807  for (timeout = 0; timeout < 100; ++timeout)
808    {
809      unsigned char status;
810
811      inport_byte (port + ISR, status);
812
813#ifdef DEBUG_NE2000
814      if ((status &~ MSK_RDC) != 0)
815        printk ("Status 0x%x while waiting for acknowledgement of uploaded packet\n",
816                status);
817#endif
818
819      if ((status & MSK_RDC) != 0) {
820        outport_byte (port + ISR, MSK_RDC);
821        break;
822      }
823    }
824
825  if (timeout >= 100)
826    printk ("Timed out waiting for acknowledgement of uploaded NE2000 packet\n");
827
828  ++sc->nextavail;
829  if (sc->nextavail == NE_TX_BUFS)
830    sc->nextavail = 0;
831}
832
833/* Tell the NE2000 to transmit a buffer whose contents we have already
834   loaded onto the device.  */
835
836static void
837ne_transmit (struct ne_softc *sc)
838{
839  struct ifnet *ifp = &sc->arpcom.ac_if;
840  unsigned int port = sc->port;
841  int len;
842
843#ifdef DEBUG_NE2000
844  printk ("Transmitting NE2000 packet\n");
845#endif
846
847  len = sc->sendlen[sc->nextsend];
848  if (len < ET_MINLEN)
849    len = ET_MINLEN;
850  outport_byte (port + TBCR0, len);
851  outport_byte (port + TBCR1, len >> 8);
852
853  outport_byte (port + TPSR, NE_FIRST_TX_PAGE + (sc->nextsend * NE_TX_PAGES));
854
855  outport_byte (port + CMDR, MSK_PG0 | MSK_TXP | MSK_RD2 | MSK_STA);
856
857#ifdef DEBUG_NE
858  /* printk("{s%d|%d}", len, sc->nextsend); */
859  printk(">");
860#endif
861  ++sc->nextsend;
862  if (sc->nextsend == NE_TX_BUFS)
863    sc->nextsend = 0;
864
865  ++sc->stats.tx_packets;
866
867  /* set watchdog timer */
868  ifp->if_timer = 2;
869}
870
871/* The NE2000 packet transmit daemon.  This task is started when the
872   NE2000 driver is initialized.  */
873
874static void
875ne_tx_daemon (void *arg)
876{
877  struct ne_softc *sc = (struct ne_softc *) arg;
878  unsigned int port = sc->port;
879  struct ifnet *ifp = &sc->arpcom.ac_if;
880
881  while (1) {
882    rtems_event_set events;
883
884    /* Wait for a packet to be ready for sending, or for there to be
885       room for another packet in the device memory.  */
886    rtems_bsdnet_event_receive (START_TRANSMIT_EVENT,
887                                RTEMS_EVENT_ANY | RTEMS_WAIT,
888                                RTEMS_NO_TIMEOUT,
889                                &events);
890
891#ifdef DEBUG_NE2000
892    printk ("ne_tx_daemon\n");
893#endif
894
895    /* This daemon handles both uploading data onto the device and
896       telling the device to transmit data which has been uploaded.
897       These are separate tasks, because while the device is
898       transmitting one buffer we will upload another.  */
899
900    /* Don't let the device interrupt us now.  */
901    outport_byte (port + IMR, 0);
902
903    while (1) {
904      struct mbuf *m;
905
906      /* If the device is not transmitting a packet, and we have
907         uploaded a packet, tell the device to transmit it.  */
908      if (! sc->transmitting && sc->inuse > 0) {
909        sc->transmitting = 1;
910        ne_transmit (sc);
911      }
912
913      /* If we don't have any more buffers to send, quit now.  */
914      if (ifp->if_snd.ifq_head == NULL) {
915        ifp->if_flags &= ~IFF_OACTIVE;
916        break;
917      }
918
919      /* Allocate a buffer to load data into.  If there are none
920         available, quit until a buffer has been transmitted.  */
921      if (sc->inuse >= NE_TX_BUFS)
922        break;
923
924      ++sc->inuse;
925
926      IF_DEQUEUE (&ifp->if_snd, m);
927      if (m == NULL)
928        panic ("ne_tx_daemon");
929
930      ne_loadpacket (sc, m);
931
932      /* Check the device status.  It may have finished transmitting
933         the last packet.  */
934      ne_check_status(sc, 0);
935    }
936
937    /* Reenable device interrupts.  */
938    outport_byte (port + IMR, NE_INTERRUPTS);
939  }
940}
941
942/* Start sending an NE2000 packet.  */
943
944static void
945ne_start (struct ifnet *ifp)
946{
947  struct ne_softc *sc = ifp->if_softc;
948
949#ifdef DEBUG_NE
950  printk("S");
951#endif
952  /* Tell the transmit daemon to wake up and send a packet.  */
953  rtems_event_send (sc->tx_daemon_tid, START_TRANSMIT_EVENT);
954  ifp->if_flags |= IFF_OACTIVE;
955}
956
957/* Initialize and start and NE2000.  */
958
959static void
960ne_init (void *arg)
961{
962  struct ne_softc *sc = (struct ne_softc *) arg;
963  struct ifnet *ifp = &sc->arpcom.ac_if;
964
965#ifdef DEBUG_NE
966  printk("ne_init()\n");
967  ne_dump(sc);
968#endif
969
970  /* only once... */
971  if (sc->tx_daemon_tid == 0)
972  {
973    sc->inuse = 0;
974    sc->nextavail = 0;
975    sc->nextsend = 0;
976    sc->transmitting = 0;
977
978    ne_init_hardware (sc);
979
980    sc->tx_daemon_tid = rtems_bsdnet_newproc ("SCtx", 4096, ne_tx_daemon, sc);
981    sc->rx_daemon_tid = rtems_bsdnet_newproc ("SCrx", 4096, ne_rx_daemon, sc);
982
983    /* install rtems irq handler */
984    ne_init_irq_handler(sc->irno);
985  }
986
987  ifp->if_flags |= IFF_RUNNING;
988}
989
990/* Stop an NE2000.  */
991
992static void
993ne_stop (struct ne_softc *sc)
994{
995  sc->arpcom.ac_if.if_flags &= ~IFF_RUNNING;
996
997  ne_stop_hardware(sc);
998
999  sc->inuse = 0;
1000  sc->nextavail = 0;
1001  sc->nextsend = 0;
1002  sc->transmitting = 0;
1003  sc->overrun = 0;
1004  sc->resend = 0;
1005}
1006
1007static void
1008ne_stop_hardware (struct ne_softc *sc)
1009{
1010  unsigned int port = sc->port;
1011  int i;
1012
1013  /* Stop everything.  */
1014  outport_byte (port + CMDR, MSK_STP | MSK_RD2);
1015
1016  /* Wait for the interface to stop, using I as a time limit.  */
1017  for (i = 0; i < 5000; ++i)
1018    {
1019      unsigned char status;
1020
1021      inport_byte (port + ISR, status);
1022      if ((status & MSK_RST) != 0)
1023        break;
1024    }
1025}
1026
1027/* reinitializing interface
1028*/
1029static void
1030ne_reset(struct ne_softc *sc)
1031{
1032  ne_stop(sc);
1033  ne_init_hardware(sc);
1034  sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
1035  sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
1036#ifdef DEBUG_NE
1037  printk("*");
1038#endif
1039}
1040
1041#ifdef DEBUG_NE
1042/* show anything about ne
1043*/
1044static void
1045ne_dump(struct ne_softc *sc)
1046{
1047  int i;
1048  printk("\nne configuration:\n");
1049  printk("ethernet addr:");
1050  for (i=0; i<ETHER_ADDR_LEN; i++)
1051    printk(" %x", sc->arpcom.ac_enaddr[i]);
1052  printk("\n");
1053  printk("irq = %d\n", sc->irno);
1054  printk("port = 0x%x\n", sc->port);
1055  printk("accept_broadcasts = %d\n", sc->accept_broadcasts);
1056  printk("byte_transfers = %d\n", sc->byte_transfers);
1057}
1058#endif
1059
1060/* Show NE2000 interface statistics.  */
1061
1062static void
1063ne_stats (struct ne_softc *sc)
1064{
1065  printf ("    Received packets: %-8lu", sc->stats.rx_packets);
1066  printf (" Transmitted packets: %-8lu\n", sc->stats.tx_packets);
1067  printf ("        Receive acks: %-8lu", sc->stats.rx_acks);
1068  printf ("       Transmit acks: %-8lu\n", sc->stats.tx_acks);
1069  printf ("     Packet overruns: %-8lu", sc->stats.overruns);
1070  printf ("        Frame errors: %-8lu\n", sc->stats.rx_frame_errors);
1071  printf ("          CRC errors: %-8lu", sc->stats.rx_crc_errors);
1072  printf ("      Missed packets: %-8lu\n", sc->stats.rx_missed_errors);
1073  printf ("          Interrupts: %-8lu\n", sc->stats.interrupts);
1074}
1075
1076/* NE2000 driver ioctl handler.  */
1077
1078static int
1079ne_ioctl (struct ifnet *ifp, ioctl_command_t command, caddr_t data)
1080{
1081  struct ne_softc *sc = ifp->if_softc;
1082  int error = 0;
1083
1084  switch (command) {
1085  case SIOCGIFADDR:
1086  case SIOCSIFADDR:
1087    error = ether_ioctl (ifp, command, data);
1088    break;
1089
1090  case SIOCSIFFLAGS:
1091    switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
1092    case IFF_RUNNING:
1093      ne_stop (sc);
1094      break;
1095
1096    case IFF_UP:
1097      ne_init (sc);
1098      break;
1099
1100    case IFF_UP | IFF_RUNNING:
1101      ne_stop (sc);
1102      ne_init (sc);
1103      break;
1104
1105    default:
1106      break;
1107    }
1108    break;
1109
1110  case SIO_RTEMS_SHOW_STATS:
1111    ne_stats (sc);
1112    break;
1113
1114    /* FIXME: Multicast commands must be added here.  */
1115
1116  default:
1117    error = EINVAL;
1118    break;
1119  }
1120
1121  return error;
1122}
1123
1124/*
1125 * Device timeout/watchdog routine. Entered if the device neglects to
1126 *      generate an interrupt after a transmit has been started on it.
1127 */
1128static void
1129ne_watchdog(struct ifnet *ifp)
1130{
1131  struct ne_softc *sc = ifp->if_softc;
1132
1133  printk("ne2000: device timeout\n");
1134    ifp->if_oerrors++;
1135
1136  ne_reset(sc);
1137}
1138
1139static void
1140print_byte(unsigned char b)
1141{
1142  printk("%x%x", b >> 4, b & 0x0f);
1143}
1144
1145/* Attach an NE2000 driver to the system.  */
1146
1147int
1148rtems_ne_driver_attach (struct rtems_bsdnet_ifconfig *config, int attach)
1149{
1150  int i;
1151  struct ne_softc *sc;
1152  struct ifnet *ifp;
1153  int mtu;
1154
1155  /* dettach ... */
1156  if (!attach)
1157    return 0;
1158
1159  /* Find a free driver.  */
1160  sc = NULL;
1161  for (i = 0; i < NNEDRIVER; ++i) {
1162    sc = &ne_softc[i];
1163    ifp = &sc->arpcom.ac_if;
1164    if (ifp->if_softc == NULL)
1165      break;
1166  }
1167
1168  if (sc == NULL) {
1169    printf ("Too many NE2000 drivers.\n");
1170    return 0;
1171  }
1172
1173  memset (sc, 0, sizeof *sc);
1174
1175  /* Check whether we do byte-wide or word-wide transfers.  */
1176
1177#ifdef NE2000_BYTE_TRANSFERS
1178  sc->byte_transfers = true;
1179#else
1180  sc->byte_transfers = false;
1181#endif
1182
1183  /* Handle the options passed in by the caller.  */
1184
1185  if (config->mtu != 0)
1186    mtu = config->mtu;
1187  else
1188    mtu = ETHERMTU;
1189
1190  if (config->irno != 0)
1191    sc->irno = config->irno;
1192  else {
1193    const char* opt;
1194    opt = bsp_cmdline_arg ("--ne2k-irq=");
1195    if (opt) {
1196      opt += sizeof ("--ne2k-irq=") - 1;
1197      sc->irno = strtoul (opt, 0, 0);
1198    }
1199    if (sc->irno == 0) {
1200      /* We use 5 as the default IRQ.  */
1201      sc->irno = 5;
1202    }
1203  }
1204
1205  if (config->port != 0)
1206    sc->port = config->port;
1207  else {
1208    const char* opt;
1209    opt = bsp_cmdline_arg ("--ne2k-port=");
1210    if (opt) {
1211      opt += sizeof ("--ne2k-port=") - 1;
1212      sc->port = strtoul (opt, 0, 0);
1213    }
1214    if (config->port != 0) {
1215      /* We use 0x300 as the default IO port number.  */
1216      sc->port = 0x300;
1217    }
1218  }
1219
1220  sc->accept_broadcasts = ! config->ignore_broadcast;
1221
1222  if (config->hardware_address != NULL)
1223    memcpy (sc->arpcom.ac_enaddr, config->hardware_address,
1224            ETHER_ADDR_LEN);
1225  else
1226    {
1227      unsigned char prom[16];
1228      int ia;
1229
1230      /* Read the PROM to get the Ethernet hardware address.  */
1231
1232      outport_byte (sc->port + CMDR, MSK_PG0 | MSK_RD2 | MSK_STP);
1233
1234      if (sc->byte_transfers) {
1235        outport_byte (sc->port + DCR, MSK_FT10 | MSK_BMS);
1236      }
1237      else {
1238        outport_byte (sc->port + DCR, MSK_FT10 | MSK_BMS | MSK_WTS);
1239      }
1240
1241      outport_byte (sc->port + RBCR0, 0);
1242      outport_byte (sc->port + RBCR1, 0);
1243      outport_byte (sc->port + RCR, MSK_MON);
1244      outport_byte (sc->port + TCR, MSK_LOOP);
1245      outport_byte (sc->port + IMR, 0);
1246      outport_byte (sc->port + ISR, 0xff);
1247
1248      ne_read_data (sc, 0, sizeof prom, prom);
1249
1250      outport_byte (sc->port + CMDR, MSK_PG0 | MSK_RD2 | MSK_STP);
1251
1252      for (ia = 0; ia < ETHER_ADDR_LEN; ++ia)
1253        sc->arpcom.ac_enaddr[ia] = prom[ia * 2];
1254    }
1255
1256  /* Set up the network interface.  */
1257
1258  ifp->if_softc = sc;
1259  ifp->if_unit = i + 1;
1260  ifp->if_name = "ne";
1261  ifp->if_mtu = mtu;
1262  ifp->if_init = ne_init;
1263  ifp->if_ioctl = ne_ioctl;
1264  ifp->if_watchdog = ne_watchdog;
1265  ifp->if_start = ne_start;
1266  ifp->if_output = ether_output;
1267  ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
1268  if (ifp->if_snd.ifq_maxlen == 0)
1269    ifp->if_snd.ifq_maxlen = ifqmaxlen;
1270
1271  /* Attach the interface.  */
1272
1273  if_attach (ifp);
1274  ether_ifattach (ifp);
1275
1276  printk("network device '%s' <", config->name);
1277  print_byte(sc->arpcom.ac_enaddr[0]);
1278  for (i=1; i<ETHER_ADDR_LEN; i++)
1279  { printk(":");
1280    print_byte(sc->arpcom.ac_enaddr[i]);
1281  }
1282  printk("> initialized on port 0x%x, irq %d\n", sc->port, sc->irno);
1283
1284  return 1;
1285}
Note: See TracBrowser for help on using the repository browser.