source: rtems/c/src/libchip/network/dec21140.c @ 0d12670

4.115
Last change on this file since 0d12670 was 0d12670, checked in by Ralf Corsepius <ralf.corsepius@…>, on 10/22/11 at 04:36:14

2011-10-22 Ralf Corsépius <ralf.corsepius@…>

  • libchip/network/dec21140.c (dec21140_rxDaemon): Remove unused var "tbase".
  • Property mode set to 100644
File size: 29.6 KB
Line 
1/*
2 *  RTEMS driver for TULIP based Ethernet Controller
3 *
4 *  Copyright (C) 1999 Emmanuel Raguet. raguet@crf.canon.fr
5 *
6 *  The license and distribution terms for this file may be
7 *  found in the file LICENSE in this distribution or at
8 *  http://www.rtems.com/license/LICENSE.
9 *
10 * $Id$
11 *
12 * ------------------------------------------------------------------------
13 * [22.05.2000,StWi/CWA] added support for the DEC/Intel 21143 chip
14 *
15 * Thanks go to Andrew Klossner who provided the vital information about the
16 * Intel 21143 chip.  FWIW: The 21143 additions to this driver were initially
17 * tested with a PC386 BSP using a Kingston KNE100TX with 21143PD chip.
18 *
19 * The driver will automatically detect whether there is a 21140 or 21143
20 * network card in the system and activate support accordingly. It will
21 * look for the 21140 first. If the 21140 is not found the driver will
22 * look for the 21143.
23 *
24 * 2004-11-10, Joel/Richard - 21143 support works on MVME2100.
25 * ------------------------------------------------------------------------
26 *
27 * 2003-03-13, Greg Menke, gregory.menke@gsfc.nasa.gov
28 *
29 * Added support for up to 8 units (which is an arbitrary limit now),
30 * consolidating their support into a single pair of rx/tx daemons and a
31 * single ISR for all vectors servicing the DEC units.  The driver now
32 * simply uses whatever INTERRUPT_LINE the card supplies, requiring it
33 * be configured either by the boot monitor or bspstart() hackery.
34 * Tested on a MCP750 PPC based system with 2 DEC21140 boards.
35 *
36 * Also fixed a few bugs related to board configuration, start and stop.
37 *
38 */
39
40#include <rtems.h>
41
42/*
43 *  This driver only supports architectures with the new style
44 *  exception processing.  The following checks try to keep this
45 *  from being compiled on systems which can't support this driver.
46 */
47
48#if defined(__i386__)
49  #define DEC21140_SUPPORTED
50  #define PCI_DRAM_OFFSET 0
51#endif
52#if defined(__PPC__)
53  #define DEC21140_SUPPORTED
54#endif
55
56#include <bsp.h>
57
58#if !defined(PCI_DRAM_OFFSET)
59  #undef DEC21140_SUPPORTED
60#endif
61
62#if defined(DEC21140_SUPPORTED)
63#include <rtems/pci.h>
64
65#if defined(__PPC__)
66#include <libcpu/byteorder.h>
67#include <libcpu/io.h>
68#endif
69
70#if defined(__i386__)
71#include <libcpu/byteorder.h>
72#endif
73
74#include <stdlib.h>
75#include <stdio.h>
76#include <stdarg.h>
77#include <string.h>
78#include <errno.h>
79#include <rtems/error.h>
80#include <rtems/bspIo.h>
81#include <rtems/rtems_bsdnet.h>
82
83#include <sys/param.h>
84#include <sys/mbuf.h>
85
86#include <sys/socket.h>
87#include <sys/sockio.h>
88#include <net/if.h>
89#include <netinet/in.h>
90#include <netinet/if_ether.h>
91
92#include <bsp/irq.h>
93
94#ifdef malloc
95#undef malloc
96#endif
97#ifdef free
98#undef free
99#endif
100
101#define DEC_DEBUG
102
103/* note: the 21143 isn't really a DEC, it's an Intel chip */
104#define PCI_INVALID_VENDORDEVICEID      0xffffffff
105#define PCI_VENDOR_ID_DEC               0x1011
106#define PCI_DEVICE_ID_DEC_21140         0x0009
107#define PCI_DEVICE_ID_DEC_21143         0x0019
108
109#define DRIVER_PREFIX   "dc"
110
111#define IO_MASK   0x3
112#define MEM_MASK  0xF
113
114/* command and status registers, 32-bit access, only if IO-ACCESS */
115#define ioCSR0  0x00    /* bus mode register */
116#define ioCSR1  0x08    /* transmit poll demand */
117#define ioCSR2  0x10    /* receive poll demand */
118#define ioCSR3  0x18    /* receive list base address */
119#define ioCSR4  0x20    /* transmit list base address */
120#define ioCSR5  0x28    /* status register */
121#define ioCSR6  0x30    /* operation mode register */
122#define ioCSR7  0x38    /* interrupt mask register */
123#define ioCSR8  0x40    /* missed frame counter */
124#define ioCSR9  0x48    /* Ethernet ROM register */
125#define ioCSR10 0x50    /* reserved */
126#define ioCSR11 0x58    /* full-duplex register */
127#define ioCSR12 0x60    /* SIA status register */
128#define ioCSR13 0x68
129#define ioCSR14 0x70
130#define ioCSR15 0x78    /* SIA general register */
131
132/* command and status registers, 32-bit access, only if MEMORY-ACCESS */
133#define memCSR0  0x00    /* bus mode register */
134#define memCSR1  0x02    /* transmit poll demand */
135#define memCSR2  0x04    /* receive poll demand */
136#define memCSR3  0x06    /* receive list base address */
137#define memCSR4  0x08    /* transmit list base address */
138#define memCSR5  0x0A    /* status register */
139#define memCSR6  0x0C    /* operation mode register */
140#define memCSR7  0x0E    /* interrupt mask register */
141#define memCSR8  0x10    /* missed frame counter */
142#define memCSR9  0x12    /* Ethernet ROM register */
143#define memCSR10 0x14    /* reserved */
144#define memCSR11 0x16    /* full-duplex register */
145#define memCSR12 0x18    /* SIA status register */
146#define memCSR13 0x1A
147#define memCSR14 0x1C
148#define memCSR15 0x1E    /* SIA general register */
149
150#define DEC_REGISTER_SIZE    0x100   /* to reserve virtual memory */
151
152
153
154
155#define RESET_CHIP   0x00000001
156#if defined(__PPC__)
157#define CSR0_MODE    0x0030e002   /* 01b08000 */
158#else
159#define CSR0_MODE    0x0020e002   /* 01b08000 */
160#endif
161#define ROM_ADDRESS  0x00004800
162#define CSR6_INIT    0x022cc000   /* 022c0000 020c0000 */
163#define CSR6_TX      0x00002000
164#define CSR6_TXRX    0x00002002
165#define IT_SETUP     0x000100c0   /* 000100e0 */
166#define CLEAR_IT     0xFFFFFFFF
167#define NO_IT        0x00000000
168
169/* message descriptor entry */
170struct MD {
171    /* used by hardware */
172    volatile uint32_t   status;
173    volatile uint32_t   counts;
174    volatile uint32_t   buf1, buf2;
175    /* used by software */
176    volatile struct mbuf *m;
177    volatile struct MD *next;
178} __attribute__ ((packed));
179
180/*
181** These buffers allocated for each unit, so ensure
182**
183**   rtems_bsdnet_config.mbuf_bytecount
184**   rtems_bsdnet_config.mbuf_cluster_bytecount
185**
186** are adequately sized to provide enough clusters and mbufs for all the
187** units.  The default bsdnet configuration is sufficient for one dec
188** unit, but will be nearing exhaustion with 2 or more.  Although a
189** little expensive in memory, the following configuration should
190** eliminate all mbuf/cluster issues;
191**
192**   rtems_bsdnet_config.mbuf_bytecount           = 128*1024;
193**   rtems_bsdnet_config.mbuf_cluster_bytecount   = 256*1024;
194*/
195
196#define NRXBUFS 16    /* number of receive buffers */
197#define NTXBUFS 16    /* number of transmit buffers */
198
199/*
200 * Number of DEC boards supported by this driver
201 */
202#define NDECDRIVER    8
203
204/*
205 * Receive buffer size -- Allow for a full ethernet packet including CRC
206 */
207#define RBUF_SIZE    1536
208
209#define ET_MINLEN       60    /* minimum message length */
210
211/*
212** Events, one per unit.  The event is sent to the rx task from the isr
213** or from the stack to the tx task whenever a unit needs service.  The
214** rx/tx tasks identify the requesting unit(s) by their particular
215** events so only requesting units are serviced.
216*/
217
218static rtems_event_set unit_signals[NDECDRIVER]= { RTEMS_EVENT_1,
219                                                   RTEMS_EVENT_2,
220                                                   RTEMS_EVENT_3,
221                                                   RTEMS_EVENT_4,
222                                                   RTEMS_EVENT_5,
223                                                   RTEMS_EVENT_6,
224                                                   RTEMS_EVENT_7,
225                                                   RTEMS_EVENT_8 };
226
227#if defined(__PPC__)
228#define phys_to_bus(address) ((unsigned int)((address)) + PCI_DRAM_OFFSET)
229#define bus_to_phys(address) ((unsigned int)((address)) - PCI_DRAM_OFFSET)
230#define CPU_CACHE_ALIGNMENT_FOR_BUFFER PPC_CACHE_ALIGNMENT
231#else
232extern void Wait_X_ms( unsigned int timeToWait );
233#define phys_to_bus(address) ((unsigned int) ((address)))
234#define bus_to_phys(address) ((unsigned int) ((address)))
235#define rtems_bsp_delay_in_bus_cycles(cycle) Wait_X_ms( cycle/100 )
236#define CPU_CACHE_ALIGNMENT_FOR_BUFFER PG_SIZE
237#endif
238
239#if (MCLBYTES < RBUF_SIZE)
240# error "Driver must have MCLBYTES > RBUF_SIZE"
241#endif
242
243/*
244 * Per-device data
245 */
246struct dec21140_softc {
247
248      struct arpcom             arpcom;
249
250      rtems_irq_connect_data    irqInfo;
251      rtems_event_set           ioevent;
252
253      int                       numRxbuffers, numTxbuffers;
254
255      volatile struct MD        *MDbase;
256      volatile struct MD        *nextRxMD;
257      volatile unsigned char   *bufferBase;
258      int                       acceptBroadcast;
259
260      volatile struct MD       *TxMD;
261      volatile struct MD       *SentTxMD;
262      int                       PendingTxCount;
263      int                       TxSuspended;
264
265      unsigned int              port;
266      volatile uint32_t        *base;
267
268      /*
269       * Statistics
270       */
271      unsigned long     rxInterrupts;
272      unsigned long     rxNotFirst;
273      unsigned long     rxNotLast;
274      unsigned long     rxGiant;
275      unsigned long     rxNonOctet;
276      unsigned long     rxRunt;
277      unsigned long     rxBadCRC;
278      unsigned long     rxOverrun;
279      unsigned long     rxCollision;
280
281      unsigned long     txInterrupts;
282      unsigned long     txDeferred;
283      unsigned long     txHeartbeat;
284      unsigned long     txLateCollision;
285      unsigned long     txRetryLimit;
286      unsigned long     txUnderrun;
287      unsigned long     txLostCarrier;
288      unsigned long     txRawWait;
289};
290
291static struct dec21140_softc dec21140_softc[NDECDRIVER];
292static rtems_id rxDaemonTid;
293static rtems_id txDaemonTid;
294
295/*
296 * This routine reads a word (16 bits) from the serial EEPROM.
297 */
298/*  EEPROM_Ctrl bits. */
299#define EE_SHIFT_CLK    0x02    /* EEPROM shift clock. */
300#define EE_CS           0x01    /* EEPROM chip select. */
301#define EE_DATA_WRITE   0x04    /* EEPROM chip data in. */
302#define EE_WRITE_0      0x01
303#define EE_WRITE_1      0x05
304#define EE_DATA_READ    0x08    /* EEPROM chip data out. */
305#define EE_ENB          (0x4800 | EE_CS)
306
307/* The EEPROM commands include the alway-set leading bit. */
308#define EE_WRITE_CMD    (5 << 6)
309#define EE_READ_CMD     (6 << 6)
310#define EE_ERASE_CMD    (7 << 6)
311
312static int eeget16(volatile uint32_t *ioaddr, int location)
313{
314   int i;
315   unsigned short retval = 0;
316   int read_cmd = location | EE_READ_CMD;
317
318   st_le32(ioaddr, EE_ENB & ~EE_CS);
319   st_le32(ioaddr, EE_ENB);
320
321   /* Shift the read command bits out. */
322   for (i = 10; i >= 0; i--) {
323      short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
324      st_le32(ioaddr, EE_ENB | dataval);
325      rtems_bsp_delay_in_bus_cycles(200);
326      st_le32(ioaddr, EE_ENB | dataval | EE_SHIFT_CLK);
327      rtems_bsp_delay_in_bus_cycles(200);
328      st_le32(ioaddr, EE_ENB | dataval); /* Finish EEPROM a clock tick. */
329      rtems_bsp_delay_in_bus_cycles(200);
330   }
331   st_le32(ioaddr, EE_ENB);
332
333   for (i = 16; i > 0; i--) {
334      st_le32(ioaddr, EE_ENB | EE_SHIFT_CLK);
335      rtems_bsp_delay_in_bus_cycles(200);
336      retval = (retval << 1) | ((ld_le32(ioaddr) & EE_DATA_READ) ? 1 : 0);
337      st_le32(ioaddr, EE_ENB);
338      rtems_bsp_delay_in_bus_cycles(200);
339   }
340
341   /* Terminate the EEPROM access. */
342   st_le32(ioaddr, EE_ENB & ~EE_CS);
343   return ( ((retval<<8)&0xff00) | ((retval>>8)&0xff) );
344}
345
346static void no_op(const rtems_irq_connect_data* irq)
347{
348   return;
349}
350
351static int dec21140IsOn(const rtems_irq_connect_data* irq)
352{
353  return BSP_irq_enabled_at_i8259s (irq->name);
354}
355
356/*
357 * DEC21140 interrupt handler
358 */
359static rtems_isr
360dec21140Enet_interrupt_handler ( struct dec21140_softc *sc )
361{
362   volatile uint32_t      *tbase;
363   uint32_t               status;
364
365   tbase = (uint32_t*)(sc->base);
366
367   /*
368    * Read status
369    */
370   status = ld_le32(tbase+memCSR5);
371   st_le32((tbase+memCSR5), status);
372
373   /*
374    * Frame received?
375    */
376   if( status & 0x000000c0 )
377   {
378      sc->rxInterrupts++;
379      rtems_event_send(rxDaemonTid, sc->ioevent);
380   }
381}
382
383static rtems_isr
384dec21140Enet_interrupt_handler_entry(void)
385{
386   int i;
387
388   /*
389   ** Check all the initialized dec units for interrupt service
390   */
391
392   for(i=0; i< NDECDRIVER; i++ )
393   {
394      if( dec21140_softc[i].base )
395         dec21140Enet_interrupt_handler( &dec21140_softc[i] );
396   }
397}
398
399/*
400 * Initialize the ethernet hardware
401 */
402static void
403dec21140Enet_initialize_hardware (struct dec21140_softc *sc)
404{
405   int i,st;
406   volatile uint32_t      *tbase;
407   volatile unsigned char *cp, *setup_frm, *eaddrs;
408   volatile unsigned char *buffer;
409   volatile struct MD     *rmd;
410
411
412#ifdef DEC_DEBUG
413   printk("dec2114x : %02x:%02x:%02x:%02x:%02x:%02x   name '%s%d', io %x, mem %x, int %d\n",
414          sc->arpcom.ac_enaddr[0], sc->arpcom.ac_enaddr[1],
415          sc->arpcom.ac_enaddr[2], sc->arpcom.ac_enaddr[3],
416          sc->arpcom.ac_enaddr[4], sc->arpcom.ac_enaddr[5],
417          sc->arpcom.ac_if.if_name, sc->arpcom.ac_if.if_unit,
418          sc->port, (unsigned) sc->base, sc->irqInfo.name );
419#endif
420
421   tbase = sc->base;
422
423   /*
424    * WARNING : First write in CSR6
425    *           Then Reset the chip ( 1 in CSR0)
426    */
427   st_le32( (tbase+memCSR6), CSR6_INIT);
428   st_le32( (tbase+memCSR0), RESET_CHIP);
429   rtems_bsp_delay_in_bus_cycles(200);
430
431   st_le32( (tbase+memCSR7), NO_IT);
432
433   /*
434    * Init CSR0
435    */
436   st_le32( (tbase+memCSR0), CSR0_MODE);
437
438   /*
439    * Init RX ring
440    */
441   cp = (volatile unsigned char *)malloc(((sc->numRxbuffers+sc->numTxbuffers)*sizeof(struct MD))
442                                         + (sc->numTxbuffers*RBUF_SIZE)
443                                         + CPU_CACHE_ALIGNMENT_FOR_BUFFER);
444   sc->bufferBase = cp;
445   cp += (CPU_CACHE_ALIGNMENT_FOR_BUFFER - (int)cp) & (CPU_CACHE_ALIGNMENT_FOR_BUFFER - 1);
446#if defined(__i386__)
447#ifdef PCI_BRIDGE_DOES_NOT_ENSURE_CACHE_COHERENCY_FOR_DMA
448   if (_CPU_is_paging_enabled())
449      _CPU_change_memory_mapping_attribute
450         (NULL, cp,
451          ((sc->numRxbuffers+sc->numTxbuffers)*sizeof(struct MD))
452          + (sc->numTxbuffers*RBUF_SIZE),
453          PTE_CACHE_DISABLE | PTE_WRITABLE);
454#endif
455#endif
456   rmd = (volatile struct MD*)cp;
457   sc->MDbase = rmd;
458   sc->nextRxMD = sc->MDbase;
459
460   buffer = cp + ((sc->numRxbuffers+sc->numTxbuffers)*sizeof(struct MD));
461   st_le32( (tbase+memCSR3), (long)(phys_to_bus((long)(sc->MDbase))));
462
463   for (i=0 ; i<sc->numRxbuffers; i++)
464   {
465      struct mbuf *m;
466
467      /* allocate an mbuf for each receive descriptor */
468      MGETHDR (m, M_WAIT, MT_DATA);
469      MCLGET (m, M_WAIT);
470      m->m_pkthdr.rcvif = &sc->arpcom.ac_if;
471      rmd->m = m;
472
473      rmd->buf2   = phys_to_bus(rmd+1);
474      rmd->buf1   = phys_to_bus(mtod(m, void *));
475      rmd->status = 0x80000000;
476      rmd->counts = 0xfdc00000 | (RBUF_SIZE);
477      rmd->next   = rmd+1;
478      rmd++;
479   }
480   /*
481    * mark last RX buffer.
482    */
483   sc->MDbase [sc->numRxbuffers-1].buf2   = 0;
484   sc->MDbase [sc->numRxbuffers-1].counts = 0xfec00000 | (RBUF_SIZE);
485   sc->MDbase [sc->numRxbuffers-1].next   = sc->MDbase;
486
487
488
489   /*
490    * Init TX ring
491    */
492   st_le32( (tbase+memCSR4), (long)(phys_to_bus((long)(rmd))) );
493   for (i=0 ; i<sc->numTxbuffers; i++){
494      (rmd+i)->buf2   = phys_to_bus(rmd+i+1);
495      (rmd+i)->buf1   = phys_to_bus(buffer + (i*RBUF_SIZE));
496      (rmd+i)->counts = 0x01000000;
497      (rmd+i)->status = 0x0;
498      (rmd+i)->next   = rmd+i+1;
499      (rmd+i)->m      = 0;
500   }
501
502   /*
503    * mark last TX buffer.
504    */
505   (rmd+sc->numTxbuffers-1)->buf2   = phys_to_bus(rmd);
506   (rmd+sc->numTxbuffers-1)->next   = rmd;
507
508
509   /*
510    * Build setup frame
511    */
512   setup_frm = (volatile unsigned char *)(bus_to_phys(rmd->buf1));
513   eaddrs = (unsigned char *)(sc->arpcom.ac_enaddr);
514   /* Fill the buffer with our physical address. */
515   for (i = 1; i < 16; i++) {
516      *setup_frm++ = eaddrs[0];
517      *setup_frm++ = eaddrs[1];
518      *setup_frm++ = eaddrs[0];
519      *setup_frm++ = eaddrs[1];
520      *setup_frm++ = eaddrs[2];
521      *setup_frm++ = eaddrs[3];
522      *setup_frm++ = eaddrs[2];
523      *setup_frm++ = eaddrs[3];
524      *setup_frm++ = eaddrs[4];
525      *setup_frm++ = eaddrs[5];
526      *setup_frm++ = eaddrs[4];
527      *setup_frm++ = eaddrs[5];
528   }
529
530   /* Add the broadcast address when doing perfect filtering */
531   memset((void*) setup_frm, 0xff, 12);
532   rmd->counts = 0x09000000 | 192 ;
533   rmd->status = 0x80000000;
534   st_le32( (tbase+memCSR6), CSR6_INIT | CSR6_TX);
535   st_le32( (tbase+memCSR1), 1);
536
537   while (rmd->status != 0x7fffffff);
538   rmd->counts = 0x01000000;
539
540   sc->TxMD = rmd+1;
541
542   sc->irqInfo.hdl  = (rtems_irq_hdl)dec21140Enet_interrupt_handler_entry;
543   sc->irqInfo.on   = no_op;
544   sc->irqInfo.off  = no_op;
545   sc->irqInfo.isOn = dec21140IsOn;
546
547#ifdef DEC_DEBUG
548   printk( "dec2114x: Installing IRQ %d\n", sc->irqInfo.name );
549#endif
550#ifdef BSP_SHARED_HANDLER_SUPPORT
551   st = BSP_install_rtems_shared_irq_handler( &sc->irqInfo );
552#else
553   st = BSP_install_rtems_irq_handler( &sc->irqInfo );
554#endif
555
556   if (!st)
557      rtems_panic ("dec2114x : Interrupt name %d already in use\n", sc->irqInfo.name );
558}
559
560static void
561dec21140_rxDaemon (void *arg)
562{
563   volatile struct MD    *rmd;
564   struct dec21140_softc *sc;
565   struct ifnet          *ifp;
566   struct ether_header   *eh;
567   struct mbuf           *m;
568   unsigned int          i,len;
569   rtems_event_set       events;
570
571   for (;;)
572   {
573
574      rtems_bsdnet_event_receive( RTEMS_ALL_EVENTS,
575                                  RTEMS_WAIT|RTEMS_EVENT_ANY,
576                                  RTEMS_NO_TIMEOUT,
577                                  &events);
578
579      for(i=0; i< NDECDRIVER; i++ )
580      {
581         sc = &dec21140_softc[i];
582         if( sc->base )
583         {
584            if( events & sc->ioevent )
585            {
586               ifp   = &sc->arpcom.ac_if;
587               rmd   = sc->nextRxMD;
588
589               /*
590               ** Read off all the packets we've received on this unit
591               */
592               while((rmd->status & 0x80000000) == 0)
593               {
594                  /* printk("unit %i rx\n", ifp->if_unit ); */
595
596                  /* pass on the packet in the mbuf */
597                  len = (rmd->status >> 16) & 0x7ff;
598                  m = (struct mbuf *)(rmd->m);
599                  m->m_len = m->m_pkthdr.len = len - sizeof(struct ether_header);
600                  eh = mtod (m, struct ether_header *);
601                  m->m_data += sizeof(struct ether_header);
602                  ether_input (ifp, eh, m);
603
604                  /* get a new mbuf for the 21140 */
605                  MGETHDR (m, M_WAIT, MT_DATA);
606                  MCLGET (m, M_WAIT);
607                  m->m_pkthdr.rcvif = ifp;
608                  rmd->m = m;
609                  rmd->buf1 = phys_to_bus(mtod(m, void *));
610
611                  /* mark the descriptor as ready to receive */
612                  rmd->status = 0x80000000;
613
614                  rmd=rmd->next;
615               }
616
617               sc->nextRxMD = rmd;
618            }
619         }
620      }
621
622   }
623}
624
625static void
626sendpacket (struct ifnet *ifp, struct mbuf *m)
627{
628   struct dec21140_softc   *dp = ifp->if_softc;
629   volatile struct MD      *tmd;
630   volatile unsigned char  *temp;
631   struct mbuf             *n;
632   unsigned int            len;
633   volatile uint32_t      *tbase;
634
635   tbase = dp->base;
636   /*
637    * Waiting for Transmitter ready
638    */
639
640   tmd = dp->TxMD;
641   n = m;
642
643   while ((tmd->status & 0x80000000) != 0)
644   {
645      tmd=tmd->next;
646   }
647
648   len = 0;
649   temp = (volatile unsigned char *)(bus_to_phys(tmd->buf1));
650
651   for (;;)
652   {
653      len += m->m_len;
654      memcpy((void*) temp, (char *)m->m_data, m->m_len);
655      temp += m->m_len ;
656      if ((m = m->m_next) == NULL)
657         break;
658   }
659
660   if (len < ET_MINLEN) len = ET_MINLEN;
661   tmd->counts =  0xe1000000 | (len & 0x7ff);
662   tmd->status = 0x80000000;
663
664   st_le32( (tbase+memCSR1), 0x1);
665
666   m_freem(n);
667
668   dp->TxMD = tmd->next;
669}
670
671/*
672 * Driver transmit daemon
673 */
674void
675dec21140_txDaemon (void *arg)
676{
677   struct dec21140_softc *sc;
678   struct ifnet          *ifp;
679   struct mbuf           *m;
680   int i;
681   rtems_event_set       events;
682
683   for (;;)
684   {
685      /*
686       * Wait for packets bound for any of the dec units
687       */
688      rtems_bsdnet_event_receive( RTEMS_ALL_EVENTS,
689                                  RTEMS_EVENT_ANY | RTEMS_WAIT,
690                                  RTEMS_NO_TIMEOUT, &events);
691
692      for(i=0; i< NDECDRIVER; i++ )
693      {
694         sc  = &dec21140_softc[i];
695         if( sc->base )
696         {
697            if( events & sc->ioevent )
698            {
699               ifp = &sc->arpcom.ac_if;
700
701               /*
702                * Send packets till queue is empty
703                */
704               for(;;)
705               {
706                  IF_DEQUEUE(&ifp->if_snd, m);
707                  if( !m ) break;
708                  /* printk("unit %i tx\n", ifp->if_unit ); */
709                  sendpacket (ifp, m);
710               }
711
712               ifp->if_flags &= ~IFF_OACTIVE;
713            }
714         }
715      }
716
717   }
718}
719
720static void
721dec21140_start (struct ifnet *ifp)
722{
723   struct dec21140_softc *sc = ifp->if_softc;
724   rtems_event_send( txDaemonTid, sc->ioevent );
725   ifp->if_flags |= IFF_OACTIVE;
726}
727
728/*
729 * Initialize and start the device
730 */
731static void
732dec21140_init (void *arg)
733{
734   struct dec21140_softc *sc = arg;
735   struct ifnet *ifp = &sc->arpcom.ac_if;
736   volatile uint32_t *tbase;
737
738   /*
739    * Set up DEC21140 hardware if its not already been done
740    */
741   if( !sc->irqInfo.hdl )
742   {
743      dec21140Enet_initialize_hardware (sc);
744   }
745
746   /*
747    * Enable RX and TX
748    */
749   tbase = sc->base;
750   st_le32( (tbase+memCSR5), IT_SETUP);
751   st_le32( (tbase+memCSR7), IT_SETUP);
752   st_le32( (tbase+memCSR6), CSR6_INIT | CSR6_TXRX);
753
754   /*
755    * Tell the world that we're running.
756    */
757   ifp->if_flags |= IFF_RUNNING;
758}
759
760/*
761 * Stop the device
762 */
763static void
764dec21140_stop (struct dec21140_softc *sc)
765{
766  volatile uint32_t *tbase;
767  struct ifnet *ifp = &sc->arpcom.ac_if;
768
769  ifp->if_flags &= ~IFF_RUNNING;
770
771  /*
772   * Stop the transmitter
773   */
774  tbase = sc->base;
775  st_le32( (tbase+memCSR7), NO_IT);
776  st_le32( (tbase+memCSR6), CSR6_INIT);
777
778  /*  free((void*)sc->bufferBase); */
779}
780
781/*
782 * Show interface statistics
783 */
784static void
785dec21140_stats (struct dec21140_softc *sc)
786{
787  printf ("      Rx Interrupts:%-8lu", sc->rxInterrupts);
788  printf ("       Not First:%-8lu", sc->rxNotFirst);
789  printf ("        Not Last:%-8lu\n", sc->rxNotLast);
790  printf ("              Giant:%-8lu", sc->rxGiant);
791  printf ("            Runt:%-8lu", sc->rxRunt);
792  printf ("       Non-octet:%-8lu\n", sc->rxNonOctet);
793  printf ("            Bad CRC:%-8lu", sc->rxBadCRC);
794  printf ("         Overrun:%-8lu", sc->rxOverrun);
795  printf ("       Collision:%-8lu\n", sc->rxCollision);
796
797  printf ("      Tx Interrupts:%-8lu", sc->txInterrupts);
798  printf ("        Deferred:%-8lu", sc->txDeferred);
799  printf (" Missed Hearbeat:%-8lu\n", sc->txHeartbeat);
800  printf ("         No Carrier:%-8lu", sc->txLostCarrier);
801  printf ("Retransmit Limit:%-8lu", sc->txRetryLimit);
802  printf ("  Late Collision:%-8lu\n", sc->txLateCollision);
803  printf ("           Underrun:%-8lu", sc->txUnderrun);
804  printf (" Raw output wait:%-8lu\n", sc->txRawWait);
805}
806
807/*
808 * Driver ioctl handler
809 */
810static int
811dec21140_ioctl (struct ifnet *ifp, ioctl_command_t command, caddr_t data)
812{
813   struct dec21140_softc *sc = ifp->if_softc;
814   int error = 0;
815
816   switch (command) {
817      case SIOCGIFADDR:
818      case SIOCSIFADDR:
819         ether_ioctl (ifp, command, data);
820         break;
821
822      case SIOCSIFFLAGS:
823         switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
824            case IFF_RUNNING:
825               dec21140_stop (sc);
826               break;
827
828            case IFF_UP:
829               dec21140_init (sc);
830               break;
831
832            case IFF_UP | IFF_RUNNING:
833               dec21140_stop (sc);
834               dec21140_init (sc);
835               break;
836
837            default:
838               break;
839         }
840         break;
841
842      case SIO_RTEMS_SHOW_STATS:
843         dec21140_stats (sc);
844         break;
845
846         /*
847          * FIXME: All sorts of multicast commands need to be added here!
848          */
849      default:
850         error = EINVAL;
851         break;
852   }
853
854   return error;
855}
856
857
858/*
859int iftap(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m )
860{
861   int i;
862
863   if(  ifp->if_unit == 1 ) return 0;
864
865   printf("unit %i, src ", ifp->if_unit );
866   for(i=0; i< ETHER_ADDR_LEN; i++) printf("%02x", (char) eh->ether_shost[i] );
867   printf(" dest ");
868   for(i=0; i< ETHER_ADDR_LEN; i++) printf("%02x", (char) eh->ether_dhost[i] );
869   printf("\n");
870
871   return -1;
872}
873*/
874
875/*
876 * Attach an DEC21140 driver to the system
877 */
878int
879rtems_dec21140_driver_attach (struct rtems_bsdnet_ifconfig *config, int attach)
880{
881   struct dec21140_softc *sc;
882   struct ifnet *ifp;
883   char         *unitName;
884   int          unitNumber;
885   int          mtu;
886   unsigned char cvalue;
887#if defined(__i386__)
888   uint32_t     value;
889   uint8_t      interrupt;
890#endif
891   int          pbus, pdev, pfun;
892#if defined(__PPC__)
893   int          tmp;
894   uint32_t     lvalue;
895#endif
896
897   /*
898    * Get the instance number for the board we're going to configure
899    * from the user.
900    */
901   if( (unitNumber = rtems_bsdnet_parse_driver_name(config, &unitName)) == -1 )
902   {
903      return 0;
904   }
905   if( strcmp(unitName, DRIVER_PREFIX) )
906   {
907      printk("dec2114x : unit name '%s' not %s\n", unitName, DRIVER_PREFIX );
908      return 0;
909   }
910
911   /*
912    * Find the board
913    */
914   if ( pci_find_device(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21140,
915                          unitNumber-1, &pbus, &pdev, &pfun) == -1 ) {
916      if ( pci_find_device(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21143,
917                             unitNumber-1, &pbus, &pdev, &pfun) != -1 ) {
918
919        /* the 21143 chip must be enabled before it can be accessed */
920#if defined(__i386__)
921        pci_write_config_dword(pbus, pdev, pfun, 0x40, 0 );
922#else
923        pci_write_config_dword(pbus, pdev, pfun, 0x40, PCI_DEVICE_ID_DEC_21143);
924#endif
925
926      } else {
927         printk("dec2114x : device '%s' not found on PCI bus\n", config->name );
928         return 0;
929      }
930   }
931
932#ifdef DEC_DEBUG
933   else {
934      printk("dec21140 : found device '%s', bus 0x%02x, dev 0x%02x, func 0x%02x\n",
935             config->name, pbus, pdev, pfun);
936   }
937#endif
938
939   if ((unitNumber < 1) || (unitNumber > NDECDRIVER))
940   {
941      printk("dec2114x : unit %i is invalid, must be (1 <= n <= %d)\n", unitNumber);
942      return 0;
943   }
944
945   sc = &dec21140_softc[unitNumber - 1];
946   ifp = &sc->arpcom.ac_if;
947   if (ifp->if_softc != NULL)
948   {
949      printk("dec2114x : unit %i already in use.\n", unitNumber );
950      return 0;
951   }
952
953
954   /*
955   ** Get this unit's rx/tx event
956   */
957   sc->ioevent = unit_signals[unitNumber-1];
958
959   /*
960   ** Save the buffer counts
961   */
962   sc->numRxbuffers = (config->rbuf_count) ? config->rbuf_count : NRXBUFS;
963   sc->numTxbuffers = (config->xbuf_count) ? config->xbuf_count : NTXBUFS;
964
965
966   /*
967    * Get card address spaces & retrieve its isr vector
968    */
969#if defined(__i386__)
970
971   pci_read_config_dword(pbus, pdev, pfun, 16, &value);
972   sc->port = value & ~IO_MASK;
973
974   pci_read_config_dword(pbus, pdev, pfun, 20, &value);
975   if (_CPU_is_paging_enabled())
976      _CPU_map_phys_address((void **) &(sc->base),
977                            (void *)(value & ~MEM_MASK),
978                            DEC_REGISTER_SIZE ,
979                            PTE_CACHE_DISABLE | PTE_WRITABLE);
980   else
981      sc->base = (uint32_t *)(value & ~MEM_MASK);
982
983   pci_read_config_byte(pbus, pdev, pfun, 60, &interrupt);
984   cvalue = interrupt;
985#endif
986#if defined(__PPC__)
987   (void)pci_read_config_dword(pbus,
988                               pdev,
989                               pfun,
990                               PCI_BASE_ADDRESS_0,
991                               &lvalue);
992
993   sc->port = lvalue & (unsigned int)(~IO_MASK);
994
995   (void)pci_read_config_dword(pbus,
996                               pdev,
997                               pfun,
998                               PCI_BASE_ADDRESS_1,
999                               &lvalue);
1000
1001   tmp = (unsigned int)(lvalue & (unsigned int)(~MEM_MASK))
1002      + (unsigned int)PCI_MEM_BASE;
1003
1004   sc->base = (uint32_t*)(tmp);
1005
1006   pci_read_config_byte(pbus,
1007                        pdev,
1008                        pfun,
1009                        PCI_INTERRUPT_LINE,
1010                        &cvalue);
1011
1012#endif
1013
1014   /*
1015   ** Prep the board
1016   */
1017
1018   pci_write_config_word(pbus, pdev, pfun,
1019      PCI_COMMAND,
1020      (uint16_t) ( PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER ) );
1021
1022   /*
1023   ** Store the interrupt name, we'll use it later when we initialize
1024   ** the board.
1025   */
1026   memset(&sc->irqInfo,0,sizeof(rtems_irq_connect_data));
1027   sc->irqInfo.name = cvalue;
1028
1029
1030#ifdef DEC_DEBUG
1031   printk("dec2114x : unit %d base address %08x.\n", unitNumber, sc->base );
1032#endif
1033
1034
1035   /*
1036   ** Setup ethernet address
1037   */
1038   if (config->hardware_address) {
1039      memcpy (sc->arpcom.ac_enaddr, config->hardware_address,
1040              ETHER_ADDR_LEN);
1041   }
1042   else {
1043      union {char c[64]; unsigned short s[32];} rombuf;
1044      int i;
1045
1046      for (i=0; i<32; i++){
1047         rombuf.s[i] = eeget16( sc->base + memCSR9, i);
1048      }
1049#if defined(__i386__)
1050      for (i=0 ; i<(ETHER_ADDR_LEN/2); i++){
1051         sc->arpcom.ac_enaddr[2*i]   = rombuf.c[20+2*i+1];
1052         sc->arpcom.ac_enaddr[2*i+1] = rombuf.c[20+2*i];
1053      }
1054#endif
1055#if defined(__PPC__)
1056      memcpy (sc->arpcom.ac_enaddr, rombuf.c+20, ETHER_ADDR_LEN);
1057#endif
1058   }
1059
1060   if (config->mtu)
1061      mtu = config->mtu;
1062   else
1063      mtu = ETHERMTU;
1064
1065   sc->acceptBroadcast = !config->ignore_broadcast;
1066
1067   /*
1068    * Set up network interface values
1069    */
1070
1071/*   ifp->if_tap = iftap; */
1072
1073   ifp->if_softc = sc;
1074   ifp->if_unit = unitNumber;
1075   ifp->if_name = unitName;
1076   ifp->if_mtu = mtu;
1077   ifp->if_init = dec21140_init;
1078   ifp->if_ioctl = dec21140_ioctl;
1079   ifp->if_start = dec21140_start;
1080   ifp->if_output = ether_output;
1081   ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
1082   if (ifp->if_snd.ifq_maxlen == 0)
1083      ifp->if_snd.ifq_maxlen = ifqmaxlen;
1084
1085   /*
1086    * Attach the interface
1087    */
1088   if_attach (ifp);
1089   ether_ifattach (ifp);
1090
1091#ifdef DEC_DEBUG
1092   printk( "dec2114x : driver attached\n" );
1093#endif
1094
1095   /*
1096    * Start driver tasks if this is the first dec unit initialized
1097    */
1098   if (txDaemonTid == 0)
1099   {
1100      rxDaemonTid = rtems_bsdnet_newproc( "DCrx", 4096,
1101                                          dec21140_rxDaemon, NULL);
1102
1103      txDaemonTid = rtems_bsdnet_newproc( "DCtx", 4096,
1104                                          dec21140_txDaemon, NULL);
1105#ifdef DEC_DEBUG
1106      printk( "dec2114x : driver tasks created\n" );
1107#endif
1108   }
1109
1110   return 1;
1111};
1112
1113#endif /* DEC21140_SUPPORTED */
Note: See TracBrowser for help on using the repository browser.