source: rtems/c/src/libchip/network/dec21140.c @ 5ce0489

5
Last change on this file since 5ce0489 was 5ce0489, checked in by Joel Sherrill <joel@…>, on 04/21/17 at 01:38:42

dec21140.c: Fix printk() warnings

  • 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
294/*
295 * This routine reads a word (16 bits) from the serial EEPROM.
296 */
297/*  EEPROM_Ctrl bits. */
298#define EE_SHIFT_CLK    0x02    /* EEPROM shift clock. */
299#define EE_CS           0x01    /* EEPROM chip select. */
300#define EE_DATA_WRITE   0x04    /* EEPROM chip data in. */
301#define EE_WRITE_0      0x01
302#define EE_WRITE_1      0x05
303#define EE_DATA_READ    0x08    /* EEPROM chip data out. */
304#define EE_ENB          (0x4800 | EE_CS)
305
306/* The EEPROM commands include the alway-set leading bit. */
307#define EE_WRITE_CMD    (5 << 6)
308#define EE_READ_CMD     (6 << 6)
309#define EE_ERASE_CMD    (7 << 6)
310
311static int eeget16(volatile uint32_t *ioaddr, int location)
312{
313   int i;
314   unsigned short retval = 0;
315   int read_cmd = location | EE_READ_CMD;
316
317   st_le32(ioaddr, EE_ENB & ~EE_CS);
318   st_le32(ioaddr, EE_ENB);
319
320   /* Shift the read command bits out. */
321   for (i = 10; i >= 0; i--) {
322      short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
323      st_le32(ioaddr, EE_ENB | dataval);
324      rtems_bsp_delay_in_bus_cycles(200);
325      st_le32(ioaddr, EE_ENB | dataval | EE_SHIFT_CLK);
326      rtems_bsp_delay_in_bus_cycles(200);
327      st_le32(ioaddr, EE_ENB | dataval); /* Finish EEPROM a clock tick. */
328      rtems_bsp_delay_in_bus_cycles(200);
329   }
330   st_le32(ioaddr, EE_ENB);
331
332   for (i = 16; i > 0; i--) {
333      st_le32(ioaddr, EE_ENB | EE_SHIFT_CLK);
334      rtems_bsp_delay_in_bus_cycles(200);
335      retval = (retval << 1) | ((ld_le32(ioaddr) & EE_DATA_READ) ? 1 : 0);
336      st_le32(ioaddr, EE_ENB);
337      rtems_bsp_delay_in_bus_cycles(200);
338   }
339
340   /* Terminate the EEPROM access. */
341   st_le32(ioaddr, EE_ENB & ~EE_CS);
342   return ( ((retval<<8)&0xff00) | ((retval>>8)&0xff) );
343}
344
345static void no_op(const rtems_irq_connect_data* irq)
346{
347   return;
348}
349
350/*
351 * DEC21140 interrupt handler
352 */
353static rtems_isr
354dec21140Enet_interrupt_handler ( struct dec21140_softc *sc )
355{
356   volatile uint32_t      *tbase;
357   uint32_t               status;
358
359   tbase = (uint32_t*)(sc->base);
360
361   /*
362    * Read status
363    */
364   status = ld_le32(tbase+memCSR5);
365   st_le32((tbase+memCSR5), status);
366
367   /*
368    * Frame received?
369    */
370   if( status & 0x000000c0 )
371   {
372      sc->rxInterrupts++;
373      rtems_bsdnet_event_send(rxDaemonTid, sc->ioevent);
374   }
375}
376
377static rtems_isr
378dec21140Enet_interrupt_handler_entry(void)
379{
380   int i;
381
382   /*
383   ** Check all the initialized dec units for interrupt service
384   */
385
386   for(i=0; i< NDECDRIVER; i++ )
387   {
388      if( dec21140_softc[i].base )
389         dec21140Enet_interrupt_handler( &dec21140_softc[i] );
390   }
391}
392
393/*
394 * Initialize the ethernet hardware
395 */
396static void
397dec21140Enet_initialize_hardware (struct dec21140_softc *sc)
398{
399   int i,st;
400   volatile uint32_t      *tbase;
401   volatile unsigned char *cp, *setup_frm, *eaddrs;
402   volatile unsigned char *buffer;
403   volatile struct MD     *rmd;
404
405
406#ifdef DEC_DEBUG
407   printk("dec2114x : %02x:%02x:%02x:%02x:%02x:%02x   name '%s%d', io %x, mem %x, int %d\n",
408          sc->arpcom.ac_enaddr[0], sc->arpcom.ac_enaddr[1],
409          sc->arpcom.ac_enaddr[2], sc->arpcom.ac_enaddr[3],
410          sc->arpcom.ac_enaddr[4], sc->arpcom.ac_enaddr[5],
411          sc->arpcom.ac_if.if_name, sc->arpcom.ac_if.if_unit,
412          sc->port, (unsigned) sc->base, sc->irqInfo.name );
413#endif
414
415   tbase = sc->base;
416
417   /*
418    * WARNING : First write in CSR6
419    *           Then Reset the chip ( 1 in CSR0)
420    */
421   st_le32( (tbase+memCSR6), CSR6_INIT);
422   st_le32( (tbase+memCSR0), RESET_CHIP);
423   rtems_bsp_delay_in_bus_cycles(200);
424
425   st_le32( (tbase+memCSR7), NO_IT);
426
427   /*
428    * Init CSR0
429    */
430   st_le32( (tbase+memCSR0), CSR0_MODE);
431
432   /*
433    * Init RX ring
434    */
435   cp = (volatile unsigned char *)malloc(((sc->numRxbuffers+sc->numTxbuffers)*sizeof(struct MD))
436                                         + (sc->numTxbuffers*RBUF_SIZE)
437                                         + CPU_CACHE_ALIGNMENT_FOR_BUFFER);
438   sc->bufferBase = cp;
439   cp += (CPU_CACHE_ALIGNMENT_FOR_BUFFER - (int)cp) & (CPU_CACHE_ALIGNMENT_FOR_BUFFER - 1);
440#if defined(__i386__)
441#ifdef PCI_BRIDGE_DOES_NOT_ENSURE_CACHE_COHERENCY_FOR_DMA
442   if (_CPU_is_paging_enabled())
443      _CPU_change_memory_mapping_attribute
444         (NULL, cp,
445          ((sc->numRxbuffers+sc->numTxbuffers)*sizeof(struct MD))
446          + (sc->numTxbuffers*RBUF_SIZE),
447          PTE_CACHE_DISABLE | PTE_WRITABLE);
448#endif
449#endif
450   rmd = (volatile struct MD*)cp;
451   sc->MDbase = rmd;
452   sc->nextRxMD = sc->MDbase;
453
454   buffer = cp + ((sc->numRxbuffers+sc->numTxbuffers)*sizeof(struct MD));
455   st_le32( (tbase+memCSR3), (long)(phys_to_bus((long)(sc->MDbase))));
456
457   for (i=0 ; i<sc->numRxbuffers; i++)
458   {
459      struct mbuf *m;
460
461      /* allocate an mbuf for each receive descriptor */
462      MGETHDR (m, M_WAIT, MT_DATA);
463      MCLGET (m, M_WAIT);
464      m->m_pkthdr.rcvif = &sc->arpcom.ac_if;
465      rmd->m = m;
466
467      rmd->buf2   = phys_to_bus(rmd+1);
468      rmd->buf1   = phys_to_bus(mtod(m, void *));
469      rmd->status = 0x80000000;
470      rmd->counts = 0xfdc00000 | (RBUF_SIZE);
471      rmd->next   = rmd+1;
472      rmd++;
473   }
474   /*
475    * mark last RX buffer.
476    */
477   sc->MDbase [sc->numRxbuffers-1].buf2   = 0;
478   sc->MDbase [sc->numRxbuffers-1].counts = 0xfec00000 | (RBUF_SIZE);
479   sc->MDbase [sc->numRxbuffers-1].next   = sc->MDbase;
480
481
482
483   /*
484    * Init TX ring
485    */
486   st_le32( (tbase+memCSR4), (long)(phys_to_bus((long)(rmd))) );
487   for (i=0 ; i<sc->numTxbuffers; i++){
488      (rmd+i)->buf2   = phys_to_bus(rmd+i+1);
489      (rmd+i)->buf1   = phys_to_bus(buffer + (i*RBUF_SIZE));
490      (rmd+i)->counts = 0x01000000;
491      (rmd+i)->status = 0x0;
492      (rmd+i)->next   = rmd+i+1;
493      (rmd+i)->m      = 0;
494   }
495
496   /*
497    * mark last TX buffer.
498    */
499   (rmd+sc->numTxbuffers-1)->buf2   = phys_to_bus(rmd);
500   (rmd+sc->numTxbuffers-1)->next   = rmd;
501
502
503   /*
504    * Build setup frame
505    */
506   setup_frm = (volatile unsigned char *)(bus_to_phys(rmd->buf1));
507   eaddrs = (unsigned char *)(sc->arpcom.ac_enaddr);
508   /* Fill the buffer with our physical address. */
509   for (i = 1; i < 16; i++) {
510      *setup_frm++ = eaddrs[0];
511      *setup_frm++ = eaddrs[1];
512      *setup_frm++ = eaddrs[0];
513      *setup_frm++ = eaddrs[1];
514      *setup_frm++ = eaddrs[2];
515      *setup_frm++ = eaddrs[3];
516      *setup_frm++ = eaddrs[2];
517      *setup_frm++ = eaddrs[3];
518      *setup_frm++ = eaddrs[4];
519      *setup_frm++ = eaddrs[5];
520      *setup_frm++ = eaddrs[4];
521      *setup_frm++ = eaddrs[5];
522   }
523
524   /* Add the broadcast address when doing perfect filtering */
525   memset((void*) setup_frm, 0xff, 12);
526   rmd->counts = 0x09000000 | 192 ;
527   rmd->status = 0x80000000;
528   st_le32( (tbase+memCSR6), CSR6_INIT | CSR6_TX);
529   st_le32( (tbase+memCSR1), 1);
530
531   while (rmd->status != 0x7fffffff);
532   rmd->counts = 0x01000000;
533
534   sc->TxMD = rmd+1;
535
536   sc->irqInfo.hdl  = (rtems_irq_hdl)dec21140Enet_interrupt_handler_entry;
537   sc->irqInfo.on   = no_op;
538   sc->irqInfo.off  = no_op;
539   sc->irqInfo.isOn = NULL;
540
541#ifdef DEC_DEBUG
542   printk( "dec2114x: Installing IRQ %d\n", sc->irqInfo.name );
543#endif
544#ifdef BSP_SHARED_HANDLER_SUPPORT
545   st = BSP_install_rtems_shared_irq_handler( &sc->irqInfo );
546#else
547   st = BSP_install_rtems_irq_handler( &sc->irqInfo );
548#endif
549
550   if (!st)
551      rtems_panic ("dec2114x : Interrupt name %d already in use\n", sc->irqInfo.name );
552}
553
554static void
555dec21140_rxDaemon (void *arg)
556{
557   volatile struct MD    *rmd;
558   struct dec21140_softc *sc;
559   struct ifnet          *ifp;
560   struct ether_header   *eh;
561   struct mbuf           *m;
562   unsigned int          i,len;
563   rtems_event_set       events;
564
565   for (;;)
566   {
567
568      rtems_bsdnet_event_receive( RTEMS_ALL_EVENTS,
569                                  RTEMS_WAIT|RTEMS_EVENT_ANY,
570                                  RTEMS_NO_TIMEOUT,
571                                  &events);
572
573      for(i=0; i< NDECDRIVER; i++ )
574      {
575         sc = &dec21140_softc[i];
576         if( sc->base )
577         {
578            if( events & sc->ioevent )
579            {
580               ifp   = &sc->arpcom.ac_if;
581               rmd   = sc->nextRxMD;
582
583               /*
584               ** Read off all the packets we've received on this unit
585               */
586               while((rmd->status & 0x80000000) == 0)
587               {
588                  /* printk("unit %i rx\n", ifp->if_unit ); */
589
590                  /* pass on the packet in the mbuf */
591                  len = (rmd->status >> 16) & 0x7ff;
592                  m = (struct mbuf *)(rmd->m);
593                  m->m_len = m->m_pkthdr.len = len - sizeof(struct ether_header);
594                  eh = mtod (m, struct ether_header *);
595                  m->m_data += sizeof(struct ether_header);
596                  ether_input (ifp, eh, m);
597
598                  /* get a new mbuf for the 21140 */
599                  MGETHDR (m, M_WAIT, MT_DATA);
600                  MCLGET (m, M_WAIT);
601                  m->m_pkthdr.rcvif = ifp;
602                  rmd->m = m;
603                  rmd->buf1 = phys_to_bus(mtod(m, void *));
604
605                  /* mark the descriptor as ready to receive */
606                  rmd->status = 0x80000000;
607
608                  rmd=rmd->next;
609               }
610
611               sc->nextRxMD = rmd;
612            }
613         }
614      }
615
616   }
617}
618
619static void
620sendpacket (struct ifnet *ifp, struct mbuf *m)
621{
622   struct dec21140_softc   *dp = ifp->if_softc;
623   volatile struct MD      *tmd;
624   volatile unsigned char  *temp;
625   struct mbuf             *n;
626   unsigned int            len;
627   volatile uint32_t      *tbase;
628
629   tbase = dp->base;
630   /*
631    * Waiting for Transmitter ready
632    */
633
634   tmd = dp->TxMD;
635   n = m;
636
637   while ((tmd->status & 0x80000000) != 0)
638   {
639      tmd=tmd->next;
640   }
641
642   len = 0;
643   temp = (volatile unsigned char *)(bus_to_phys(tmd->buf1));
644
645   for (;;)
646   {
647      len += m->m_len;
648      memcpy((void*) temp, (char *)m->m_data, m->m_len);
649      temp += m->m_len ;
650      if ((m = m->m_next) == NULL)
651         break;
652   }
653
654   if (len < ET_MINLEN) len = ET_MINLEN;
655   tmd->counts =  0xe1000000 | (len & 0x7ff);
656   tmd->status = 0x80000000;
657
658   st_le32( (tbase+memCSR1), 0x1);
659
660   m_freem(n);
661
662   dp->TxMD = tmd->next;
663}
664
665/*
666 * Driver transmit daemon
667 */
668void
669dec21140_txDaemon (void *arg)
670{
671   struct dec21140_softc *sc;
672   struct ifnet          *ifp;
673   struct mbuf           *m;
674   int i;
675   rtems_event_set       events;
676
677   for (;;)
678   {
679      /*
680       * Wait for packets bound for any of the dec units
681       */
682      rtems_bsdnet_event_receive( RTEMS_ALL_EVENTS,
683                                  RTEMS_EVENT_ANY | RTEMS_WAIT,
684                                  RTEMS_NO_TIMEOUT, &events);
685
686      for(i=0; i< NDECDRIVER; i++ )
687      {
688         sc  = &dec21140_softc[i];
689         if( sc->base )
690         {
691            if( events & sc->ioevent )
692            {
693               ifp = &sc->arpcom.ac_if;
694
695               /*
696                * Send packets till queue is empty
697                */
698               for(;;)
699               {
700                  IF_DEQUEUE(&ifp->if_snd, m);
701                  if( !m ) break;
702                  /* printk("unit %i tx\n", ifp->if_unit ); */
703                  sendpacket (ifp, m);
704               }
705
706               ifp->if_flags &= ~IFF_OACTIVE;
707            }
708         }
709      }
710
711   }
712}
713
714static void
715dec21140_start (struct ifnet *ifp)
716{
717   struct dec21140_softc *sc = ifp->if_softc;
718   rtems_bsdnet_event_send( txDaemonTid, sc->ioevent );
719   ifp->if_flags |= IFF_OACTIVE;
720}
721
722/*
723 * Initialize and start the device
724 */
725static void
726dec21140_init (void *arg)
727{
728   struct dec21140_softc *sc = arg;
729   struct ifnet *ifp = &sc->arpcom.ac_if;
730   volatile uint32_t *tbase;
731
732   /*
733    * Set up DEC21140 hardware if its not already been done
734    */
735   if( !sc->irqInfo.hdl )
736   {
737      dec21140Enet_initialize_hardware (sc);
738   }
739
740   /*
741    * Enable RX and TX
742    */
743   tbase = sc->base;
744   st_le32( (tbase+memCSR5), IT_SETUP);
745   st_le32( (tbase+memCSR7), IT_SETUP);
746   st_le32( (tbase+memCSR6), CSR6_INIT | CSR6_TXRX);
747
748   /*
749    * Tell the world that we're running.
750    */
751   ifp->if_flags |= IFF_RUNNING;
752}
753
754/*
755 * Stop the device
756 */
757static void
758dec21140_stop (struct dec21140_softc *sc)
759{
760  volatile uint32_t *tbase;
761  struct ifnet *ifp = &sc->arpcom.ac_if;
762
763  ifp->if_flags &= ~IFF_RUNNING;
764
765  /*
766   * Stop the transmitter
767   */
768  tbase = sc->base;
769  st_le32( (tbase+memCSR7), NO_IT);
770  st_le32( (tbase+memCSR6), CSR6_INIT);
771
772  /*  free((void*)sc->bufferBase); */
773}
774
775/*
776 * Show interface statistics
777 */
778static void
779dec21140_stats (struct dec21140_softc *sc)
780{
781  printf ("      Rx Interrupts:%-8lu", sc->rxInterrupts);
782  printf ("       Not First:%-8lu", sc->rxNotFirst);
783  printf ("        Not Last:%-8lu\n", sc->rxNotLast);
784  printf ("              Giant:%-8lu", sc->rxGiant);
785  printf ("            Runt:%-8lu", sc->rxRunt);
786  printf ("       Non-octet:%-8lu\n", sc->rxNonOctet);
787  printf ("            Bad CRC:%-8lu", sc->rxBadCRC);
788  printf ("         Overrun:%-8lu", sc->rxOverrun);
789  printf ("       Collision:%-8lu\n", sc->rxCollision);
790
791  printf ("      Tx Interrupts:%-8lu", sc->txInterrupts);
792  printf ("        Deferred:%-8lu", sc->txDeferred);
793  printf (" Missed Hearbeat:%-8lu\n", sc->txHeartbeat);
794  printf ("         No Carrier:%-8lu", sc->txLostCarrier);
795  printf ("Retransmit Limit:%-8lu", sc->txRetryLimit);
796  printf ("  Late Collision:%-8lu\n", sc->txLateCollision);
797  printf ("           Underrun:%-8lu", sc->txUnderrun);
798  printf (" Raw output wait:%-8lu\n", sc->txRawWait);
799}
800
801/*
802 * Driver ioctl handler
803 */
804static int
805dec21140_ioctl (struct ifnet *ifp, ioctl_command_t command, caddr_t data)
806{
807   struct dec21140_softc *sc = ifp->if_softc;
808   int error = 0;
809
810   switch (command) {
811      case SIOCGIFADDR:
812      case SIOCSIFADDR:
813         ether_ioctl (ifp, command, data);
814         break;
815
816      case SIOCSIFFLAGS:
817         switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
818            case IFF_RUNNING:
819               dec21140_stop (sc);
820               break;
821
822            case IFF_UP:
823               dec21140_init (sc);
824               break;
825
826            case IFF_UP | IFF_RUNNING:
827               dec21140_stop (sc);
828               dec21140_init (sc);
829               break;
830
831            default:
832               break;
833         }
834         break;
835
836      case SIO_RTEMS_SHOW_STATS:
837         dec21140_stats (sc);
838         break;
839
840         /*
841          * FIXME: All sorts of multicast commands need to be added here!
842          */
843      default:
844         error = EINVAL;
845         break;
846   }
847
848   return error;
849}
850
851
852/*
853int iftap(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m )
854{
855   int i;
856
857   if(  ifp->if_unit == 1 ) return 0;
858
859   printf("unit %i, src ", ifp->if_unit );
860   for(i=0; i< ETHER_ADDR_LEN; i++) printf("%02x", (char) eh->ether_shost[i] );
861   printf(" dest ");
862   for(i=0; i< ETHER_ADDR_LEN; i++) printf("%02x", (char) eh->ether_dhost[i] );
863   printf("\n");
864
865   return -1;
866}
867*/
868
869/*
870 * Attach an DEC21140 driver to the system
871 */
872int
873rtems_dec21140_driver_attach (struct rtems_bsdnet_ifconfig *config, int attach)
874{
875   struct dec21140_softc *sc;
876   struct ifnet *ifp;
877   char         *unitName;
878   int          unitNumber;
879   int          mtu;
880   unsigned char cvalue;
881#if defined(__i386__)
882   uint32_t     value;
883   uint8_t      interrupt;
884#endif
885   int          pbus, pdev, pfun;
886#if defined(__PPC__)
887   int          tmp;
888   uint32_t     lvalue;
889#endif
890
891   /*
892    * Get the instance number for the board we're going to configure
893    * from the user.
894    */
895   if( (unitNumber = rtems_bsdnet_parse_driver_name(config, &unitName)) == -1 )
896   {
897      return 0;
898   }
899   if( strcmp(unitName, DRIVER_PREFIX) )
900   {
901      printk("dec2114x : unit name '%s' not %s\n", unitName, DRIVER_PREFIX );
902      return 0;
903   }
904
905   /*
906    * Find the board
907    */
908   if ( pci_find_device(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21140,
909                          unitNumber-1, &pbus, &pdev, &pfun) == -1 ) {
910      if ( pci_find_device(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21143,
911                             unitNumber-1, &pbus, &pdev, &pfun) != -1 ) {
912
913        /* the 21143 chip must be enabled before it can be accessed */
914#if defined(__i386__)
915        pci_write_config_dword(pbus, pdev, pfun, 0x40, 0 );
916#else
917        pci_write_config_dword(pbus, pdev, pfun, 0x40, PCI_DEVICE_ID_DEC_21143);
918#endif
919
920      } else {
921         printk("dec2114x : device '%s' not found on PCI bus\n", config->name );
922         return 0;
923      }
924   }
925
926#ifdef DEC_DEBUG
927   else {
928      printk("dec21140 : found device '%s', bus 0x%02x, dev 0x%02x, func 0x%02x\n",
929             config->name, pbus, pdev, pfun);
930   }
931#endif
932
933   if ((unitNumber < 1) || (unitNumber > NDECDRIVER))
934   {
935      printk("dec2114x : unit %i is invalid, must be (1 <= n <= %d)\n",
936              unitNumber, NDECDRIVER);
937      return 0;
938   }
939
940   sc = &dec21140_softc[unitNumber - 1];
941   ifp = &sc->arpcom.ac_if;
942   if (ifp->if_softc != NULL)
943   {
944      printk("dec2114x : unit %i already in use.\n", unitNumber );
945      return 0;
946   }
947
948
949   /*
950   ** Get this unit's rx/tx event
951   */
952   sc->ioevent = unit_signals[unitNumber-1];
953
954   /*
955   ** Save the buffer counts
956   */
957   sc->numRxbuffers = (config->rbuf_count) ? config->rbuf_count : NRXBUFS;
958   sc->numTxbuffers = (config->xbuf_count) ? config->xbuf_count : NTXBUFS;
959
960
961   /*
962    * Get card address spaces & retrieve its isr vector
963    */
964#if defined(__i386__)
965
966   pci_read_config_dword(pbus, pdev, pfun, 16, &value);
967   sc->port = value & ~IO_MASK;
968
969   pci_read_config_dword(pbus, pdev, pfun, 20, &value);
970   if (_CPU_is_paging_enabled())
971      _CPU_map_phys_address((void **) &(sc->base),
972                            (void *)(value & ~MEM_MASK),
973                            DEC_REGISTER_SIZE ,
974                            PTE_CACHE_DISABLE | PTE_WRITABLE);
975   else
976      sc->base = (uint32_t *)(value & ~MEM_MASK);
977
978   pci_read_config_byte(pbus, pdev, pfun, 60, &interrupt);
979   cvalue = interrupt;
980#endif
981#if defined(__PPC__)
982   (void)pci_read_config_dword(pbus,
983                               pdev,
984                               pfun,
985                               PCI_BASE_ADDRESS_0,
986                               &lvalue);
987
988   sc->port = lvalue & (unsigned int)(~IO_MASK);
989
990   (void)pci_read_config_dword(pbus,
991                               pdev,
992                               pfun,
993                               PCI_BASE_ADDRESS_1,
994                               &lvalue);
995
996   tmp = (unsigned int)(lvalue & (unsigned int)(~MEM_MASK))
997      + (unsigned int)PCI_MEM_BASE;
998
999   sc->base = (uint32_t*)(tmp);
1000
1001   pci_read_config_byte(pbus,
1002                        pdev,
1003                        pfun,
1004                        PCI_INTERRUPT_LINE,
1005                        &cvalue);
1006
1007#endif
1008
1009   /*
1010   ** Prep the board
1011   */
1012
1013   pci_write_config_word(pbus, pdev, pfun,
1014      PCI_COMMAND,
1015      (uint16_t) ( PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER ) );
1016
1017   /*
1018   ** Store the interrupt name, we'll use it later when we initialize
1019   ** the board.
1020   */
1021   memset(&sc->irqInfo,0,sizeof(rtems_irq_connect_data));
1022   sc->irqInfo.name = cvalue;
1023
1024
1025#ifdef DEC_DEBUG
1026   printk("dec2114x : unit %d base address %p.\n", unitNumber, sc->base);
1027#endif
1028
1029
1030   /*
1031   ** Setup ethernet address
1032   */
1033   if (config->hardware_address) {
1034      memcpy (sc->arpcom.ac_enaddr, config->hardware_address,
1035              ETHER_ADDR_LEN);
1036   }
1037   else {
1038      union {char c[64]; unsigned short s[32];} rombuf;
1039      int i;
1040
1041      for (i=0; i<32; i++){
1042         rombuf.s[i] = eeget16( sc->base + memCSR9, i);
1043      }
1044#if defined(__i386__)
1045      for (i=0 ; i<(ETHER_ADDR_LEN/2); i++){
1046         sc->arpcom.ac_enaddr[2*i]   = rombuf.c[20+2*i+1];
1047         sc->arpcom.ac_enaddr[2*i+1] = rombuf.c[20+2*i];
1048      }
1049#endif
1050#if defined(__PPC__)
1051      memcpy (sc->arpcom.ac_enaddr, rombuf.c+20, ETHER_ADDR_LEN);
1052#endif
1053   }
1054
1055   if (config->mtu)
1056      mtu = config->mtu;
1057   else
1058      mtu = ETHERMTU;
1059
1060   sc->acceptBroadcast = !config->ignore_broadcast;
1061
1062   /*
1063    * Set up network interface values
1064    */
1065
1066/*   ifp->if_tap = iftap; */
1067
1068   ifp->if_softc = sc;
1069   ifp->if_unit = unitNumber;
1070   ifp->if_name = unitName;
1071   ifp->if_mtu = mtu;
1072   ifp->if_init = dec21140_init;
1073   ifp->if_ioctl = dec21140_ioctl;
1074   ifp->if_start = dec21140_start;
1075   ifp->if_output = ether_output;
1076   ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
1077   if (ifp->if_snd.ifq_maxlen == 0)
1078      ifp->if_snd.ifq_maxlen = ifqmaxlen;
1079
1080   /*
1081    * Attach the interface
1082    */
1083   if_attach (ifp);
1084   ether_ifattach (ifp);
1085
1086#ifdef DEC_DEBUG
1087   printk( "dec2114x : driver attached\n" );
1088#endif
1089
1090   /*
1091    * Start driver tasks if this is the first dec unit initialized
1092    */
1093   if (txDaemonTid == 0)
1094   {
1095      rxDaemonTid = rtems_bsdnet_newproc( "DCrx", 4096,
1096                                          dec21140_rxDaemon, NULL);
1097
1098      txDaemonTid = rtems_bsdnet_newproc( "DCtx", 4096,
1099                                          dec21140_txDaemon, NULL);
1100#ifdef DEC_DEBUG
1101      printk( "dec2114x : driver tasks created\n" );
1102#endif
1103   }
1104
1105   return 1;
1106};
1107
1108#endif /* DEC21140_SUPPORTED */
Note: See TracBrowser for help on using the repository browser.