source: rtems/c/src/libchip/network/dec21140.c @ 169d6f6e

5
Last change on this file since 169d6f6e was fc91f51, checked in by Joel Sherrill <joel@…>, on 04/21/17 at 01:40:51

dec21140.c: Add prototype of dec21140_txDaemon()

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