source: rtems/c/src/lib/libbsp/powerpc/mvme5500/network/if_100MHz/GT64260eth.c @ 5d2f5196

4.104.114.95
Last change on this file since 5d2f5196 was 5d2f5196, checked in by Ralf Corsepius <ralf.corsepius@…>, on 08/20/08 at 11:32:46

Add missing prototypes.

  • Property mode set to 100644
File size: 44.3 KB
RevLine 
[ee732739]1/* GT64260eth.c : GT64260 10/100 Mb ethernet MAC driver
2 *
3 * Copyright (c) 2003,2004 Brookhaven National  Laboratory
4 *               S. Kate Feng <feng1@bnl.gov>
5 * All rights reserved
6 *
7 * Acknowledgements:
8 * netBSD : Copyright (c) 2002 Allegro Networks, Inc., Wasabi Systems, Inc.
9 * Marvell : NDA document for the discovery system controller
10 * The author referenced two RTEMS network drivers of other NICs.
11 * rtems : 1) dec21140.c, a network driver for for TULIP based Ethernet Controller
12 *            (C) 1999 Emmanuel Raguet. raguet@crf.canon.fr
13 *
14 *         2) yellowfin.c, a network driver for the SVGM5 BSP.
15 *           Stanford Linear Accelerator Center, Till Straumann
16 *
17 * Some notes from the author, S. Kate Feng :
18 *
19 * 1) Mvme5500 uses Eth0 (controller 0) of the GT64260 to implement
20 *    the 10/100 BaseT Ethernet with PCI Master Data Byte Swap\
21 *    control.
22 * 2) Implemented hardware snoop instead of software snoop
23 *    to ensure SDRAM cache coherency. (Copyright : NDA item)
24 * 3) Added S/W support for multi mbuf.  (TODO : Let the H/W do it)
25 *   
26 */
27#define BYTE_ORDER BIG_ENDIAN
28
29#define INET
30
31#include <rtems.h>
32#include <rtems/bspIo.h>            /* printk */
33#include <stdio.h>                  /* printf for statistics */
34#include <string.h>
35
36#include <libcpu/io.h>              /* inp & friends */
37#include <libcpu/spr.h>             /* registers.h is included here */
38#include <bsp.h>
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/mbuf.h>
43
44#include <rtems/rtems_bsdnet.h>
45#include <rtems/rtems_bsdnet_internal.h>
46#include <rtems/error.h>           
47#include <errno.h>
48
49#include <rtems/rtems/types.h>
50
51#include <sys/queue.h>
52
53#include <sys/ioctl.h>
54#include <sys/socket.h>
55#include <sys/sockio.h>             /* SIOCADDMULTI, SIOC...     */
56#include <net/if.h>
57#include <net/if_dl.h>
58#include <netinet/in.h>
59#include <netinet/if_ether.h>
60
61#ifdef INET
62#include <netinet/in_var.h>
63#endif
64
65#include <bsp/irq.h>
66#include <bsp/GT64260ethreg.h>
67#include <bsp/GT64260eth.h>
68#include <bsp/VPD.h>
69
70#define GT_ETH_TASK_NAME  "Geth"
71#define PKT_BUF_SZ 1536
72#define SOFTC_ALIGN  31   
73#define HASH_ALIGN   15   
74
75#define TXQ_HiLmt_OFF 2
76
77/* <skf>
78 * 1. printk debug is for diagnosis only, which may cause
79 * unexpected result, especially if txq is under heavy load
80 * because CPU is fast with a decent cache.
81 */
82#define GTeth_debug 0
83#define GTeth_rx_debug 0
84
85#if 0
86#define GE_FORGOT
87#define GE_NORX
88#define GT_DEBUG
89#endif
90
91/* RTEMS event to kill the daemon */
92#define KILL_EVENT              RTEMS_EVENT_1
93/* RTEMS event to (re)start the transmitter */
94#define START_TRANSMIT_EVENT    RTEMS_EVENT_2
95/* RTEMS events used by the ISR */
96#define RX_EVENT                RTEMS_EVENT_3
97#define TX_EVENT                RTEMS_EVENT_4
98#define ERR_EVENT               RTEMS_EVENT_5
99
100#define ALL_EVENTS (KILL_EVENT|START_TRANSMIT_EVENT|RX_EVENT|TX_EVENT|ERR_EVENT)
101
102enum GTeth_whack_op {
103        GE_WHACK_START,         GE_WHACK_RESTART,
104        GE_WHACK_CHANGE,        GE_WHACK_STOP
105};
106
107enum GTeth_hash_op {
108        GE_HASH_ADD,            GE_HASH_REMOVE,
109};
110
111#define ET_MINLEN 64            /* minimum message length */
112
113static int GTeth_ifioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
114static void GTeth_ifstart (struct ifnet *);
115static void GTeth_ifchange(struct GTeth_softc *sc);
116static void GTeth_init_rx_ring(struct GTeth_softc *sc);
117static void GT64260eth_daemon(void *arg);
118static int GT64260eth_sendpacket(struct GTeth_softc *sc,struct mbuf *m);
119static unsigned GTeth_txq_done(struct GTeth_softc *sc);
120static void GTeth_tx_cleanup(struct GTeth_softc *sc);
121static void GTeth_tx_start(struct GTeth_softc *sc);
122static void GTeth_tx_stop(struct GTeth_softc *sc);
123static void GTeth_rx_cleanup(struct GTeth_softc *sc);
124static int GT64260eth_rx(struct GTeth_softc *sc);
125static void GTeth_rx_setup(struct GTeth_softc *sc);
126static void GTeth_rxprio_setup(struct GTeth_softc *sc);
127static void GTeth_rx_stop(struct GTeth_softc *dc);
[6771a9e7]128static void GT64260eth_isr(void);
[ee732739]129static int GTeth_hash_compute(struct GTeth_softc *sc,unsigned char eaddr[ETHER_ADDR_LEN]);
130static int GTeth_hash_entry_op(struct GTeth_softc *sc, enum GTeth_hash_op op,
131        enum GTeth_rxprio prio,unsigned char eaddr[ETHER_ADDR_LEN]);
132
133static int GTeth_hash_fill(struct GTeth_softc *sc);
134static void GTeth_hash_init(struct GTeth_softc *sc);
135
136static struct GTeth_softc *root_GT64260eth_dev = NULL;
137
138static void GT64260eth_irq_on(const rtems_irq_connect_data *irq)
139{
140  struct GTeth_softc *sc;
141
142  for (sc= root_GT64260eth_dev; sc; sc= sc-> next_module) {
143    outl(0x30883444,ETH0_EIMR); /* MOTLoad default interrupt mask */
144    return;
145  } 
146}
147
148static void GT64260eth_irq_off(const rtems_irq_connect_data *irq)
149{
150  struct GTeth_softc *sc;
151
152  for (sc= root_GT64260eth_dev; sc; sc= sc-> next_module)
153      outl(0, ETH0_EIMR);
154}
155
156static int GT64260eth_irq_is_on(const rtems_irq_connect_data *irq)
157{
158  return(inl(ETH0_EICR) & ETH_IR_EtherIntSum);
159}
160
[6771a9e7]161static void GT64260eth_isr(void)
[ee732739]162{
163  struct GTeth_softc *sc = root_GT64260eth_dev;
164  rtems_event_set  events=0;
165  uint32_t cause;
166
167
168  cause = inl(ETH0_EICR);
169  outl( ~cause,ETH0_EICR);  /* clear the ICR */
170
171  if ( (!cause) || (cause & 0x803d00)) {
172       sc->intr_errsts[sc->intr_err_ptr2++]=cause;
173       sc->intr_err_ptr2 %=INTR_ERR_SIZE;   /* Till Straumann */
174       events |= ERR_EVENT;
175  } 
176
177  /* ETH_IR_RxBuffer_3|ETH_IR_RxError_3 */
178  if (cause & 0x880000) {
179     sc->stats.rxInterrupts++;
180     events |= RX_EVENT;
181  }
182  /* If there is an error, we want to continue to next descriptor */
183  /* ETH_IR_TxBufferHigh|ETH_IR_TxEndHigh|ETH_IR_TxErrorHigh */
184  if (cause & 0x444) {
185       sc->stats.txInterrupts++;
186       events |= TX_EVENT;
187       /* It seems to be unnecessary. However, it's there
188        * to be on the safe side due to the datasheet.
189        * So far, it does not seem to affect the network performance
190        * based on the EPICS catime.
191        */
192       /* ETH_ESDCMR_TXDH | ETH_ESDCMR_ERD = 0x800080 */
193       if ((sc->txq_nactive > 1)&& ((inl(ETH0_ESDCMR)&ETH_ESDCMR_TXDH)==0))
194          outl(0x800080,ETH0_ESDCMR);
195
196
197  }
198  rtems_event_send(sc->daemonTid, events);
199}
200
201static rtems_irq_connect_data GT64260ethIrqData={
202        BSP_MAIN_ETH0_IRQ,
203        (rtems_irq_hdl) GT64260eth_isr,
204        NULL,
205        (rtems_irq_enable) GT64260eth_irq_on,
206        (rtems_irq_disable) GT64260eth_irq_off,
207        (rtems_irq_is_enabled) GT64260eth_irq_is_on,
208};
209
210static void GT64260eth_init_hw(struct GTeth_softc *sc)
211{
212
213#ifdef GT_DEBUG
214  printk("GT64260eth_init_hw(");
215#endif
216  /* Kate Feng : Turn the hardware snoop on as MOTLoad did not have
217   * it on by default.
218   */
219  outl(RxBSnoopEn|TxBSnoopEn|RxDSnoopEn|TxDSnoopEn, GT_CUU_Eth0_AddrCtrlLow);
220  outl(HashSnoopEn, GT_CUU_Eth0_AddrCtrlHigh);
221
222  sc->rxq_intrbits=0;
223  sc->sc_flags=0;
224 
225#ifndef GE_NORX
226  GTeth_rx_setup(sc);
227#endif
228
229#ifndef GE_NOTX
230  GTeth_tx_start(sc);
231#endif
232
233  sc->sc_pcr |= ETH_EPCR_HS_512;
234  outl(sc->sc_pcr, ETH0_EPCR);
235  outl(sc->sc_pcxr, ETH0_EPCXR); /* port config. extended reg. */
236  outl(0, ETH0_EICR); /* interrupt cause register */
237  outl(sc->sc_intrmask, ETH0_EIMR);
238#ifndef GE_NOHASH
239  /* Port Hash Table Pointer Reg*/
240  outl(((unsigned) sc->sc_hashtable),ETH0_EHTPR);
241#endif
242#ifndef GE_NORX
243  outl(ETH_ESDCMR_ERD,ETH0_ESDCMR); /* enable Rx DMA in SDMA Command Register */
244  sc->sc_flags |= GE_RXACTIVE;
245#endif
246#ifdef GT_DEBUG
247  printk("SDCMR 0x%x ", inl(ETH0_ESDCMR));
248#endif
249
250  /* connect the interrupt handler which should
251   * take care of enabling interrupts
252   */
253  if (!BSP_install_rtems_irq_handler(&GT64260ethIrqData))
254     printk("GT64260eth: unable to install ISR");
255   
256  /* The ethernet port is ready to transmit/receive */
257  outl(sc->sc_pcr | ETH_EPCR_EN, ETH0_EPCR);
258
259#ifdef GT_DEBUG
260  printk(")\n");
261#endif
262}
263
264static void GT64260eth_stop_hw(struct GTeth_softc *sc)
265{
266
267  printk("GT64260eth_stop_hw(");
268
269  /* remove our interrupt handler which will also
270  * disable interrupts at the MPIC and the device
271  * itself
272  */
273  if (!BSP_remove_rtems_irq_handler(&GT64260ethIrqData))
274     printk("GT64260eth: unable to remove IRQ handler!");
275
276  outl(sc->sc_pcr, ETH0_EPCR);
277  outl(0, ETH0_EIMR);
278
279  sc->arpcom.ac_if.if_flags &= ~IFF_RUNNING;
280#ifndef GE_NOTX
281  GTeth_tx_stop(sc);
282#endif
283#ifndef GE_NORX
284  GTeth_rx_stop(sc);
285#endif
286  sc->sc_hashtable = NULL;
287  if (GTeth_debug>0) printk(")");
288}
289
290static void GT64260eth_stop(struct GTeth_softc *sc)
291{
292  if (GTeth_debug>0) printk("GT64260eth_stop(");
293
294  /* The hardware is shutdown in the daemon */
295  /* kill the daemon. We also must release the networking
296   * semaphore or there'll be a deadlock...
297   */
298  rtems_event_send(sc->daemonTid, KILL_EVENT);
299  rtems_bsdnet_semaphore_release();
300
301  sc->daemonTid=0;
302  /* now wait for it to die */
303  rtems_semaphore_obtain(sc->daemonSync,RTEMS_WAIT,RTEMS_NO_TIMEOUT);
304
305  /* reacquire the bsdnet semaphore */
306  rtems_bsdnet_semaphore_obtain();
307  if (GTeth_debug>0) printk(")");
308}
309
310static void GT64260eth_ifinit(void *arg)
311{
312  struct GTeth_softc *sc = (struct GTeth_softc*)arg;
313  int i;
314
315#ifdef GT_DEBUG
316  printk("GT64260eth_ifinit(): daemon ID: 0x%08x)\n", sc->daemonTid);
317#endif
318  if (sc->daemonTid) {
319#ifdef GT_DEBUG
320     printk("GT64260eth: daemon already up, doing nothing\n");
321#endif
322     return;
323  }
324
325#ifndef GE_NOHASH
326  /* Mvme5500, the user must initialize the hash table before enabling the
327   * Ethernet controller
328   */
329  GTeth_hash_init(sc);
330  GTeth_hash_fill(sc);
331#endif
332
333  sc->intr_err_ptr1=0;
334  sc->intr_err_ptr2=0;
335  for (i=0; i< INTR_ERR_SIZE; i++) sc->intr_errsts[i]=0;
336
337  /* initialize the hardware (we are holding the network semaphore at this point) */
338  (void)GT64260eth_init_hw(sc);
339
340  /* launch network daemon */
341
342  /* NOTE:
343   * in ss-20011025 (and later) any task created by 'bsdnet_newproc' is
344   * wrapped by code which acquires the network semaphore...
345   */
346   sc->daemonTid = rtems_bsdnet_newproc(GT_ETH_TASK_NAME,4096,GT64260eth_daemon,arg);
347
348  /* Tell the world that we're running */
349  sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
350
351}
352
353/* attach parameter tells us whether to attach or to detach the driver */
354/* Attach this instance, and then all the sub-devices */
355int rtems_GT64260eth_driver_attach(struct rtems_bsdnet_ifconfig *config, int attach)
356{
357  struct GTeth_softc *sc;
358  struct ifnet *ifp;
359  unsigned sdcr, data;
360  unsigned char hwaddr[6];
361  int i, unit, phyaddr;
362  void     *softc_mem;
363  char     *name;
364
365  unit = rtems_bsdnet_parse_driver_name(config, &name);
366  if (unit < 0) return 0;
367 
368  printk("\nEthernet driver name %s unit %d \n",name, unit);
369  printk("(c) 2004, Brookhaven National Lab. <feng1@bnl.gov> (RTEMS/mvme5500 port)\n");
370
371  /* Make certain elements e.g. descriptor lists are aligned. */
372  softc_mem = rtems_bsdnet_malloc(sizeof(*sc) + SOFTC_ALIGN, M_FREE, M_NOWAIT);
373
374  /* Check for the very unlikely case of no memory. */
375  if (softc_mem == NULL)
376     printk("GT64260eth: OUT OF MEMORY");
377
378  sc = (void *)(((long)softc_mem + SOFTC_ALIGN) & ~SOFTC_ALIGN);
379  memset(sc, 0, sizeof(*sc));
380
381  if (GTeth_debug>0) printk("txq_desc[0] addr:%x, rxq_desc[0] addr:%x, sizeof sc %d\n",&sc->txq_desc[0], &sc->rxq_desc[0], sizeof(*sc));
382
383  sc->sc_macno = unit-1;
384
385  data = inl(ETH_EPAR);
386  phyaddr = ETH_EPAR_PhyAD_GET(data, sc->sc_macno);
387
388  /* try to read HW address from the device if not overridden
389   * by config
390   */
391  if (config->hardware_address) {
392     memcpy(hwaddr, config->hardware_address, ETHER_ADDR_LEN);
393  } else {
394    printk("Read EEPROM ");
395     for (i = 0; i < 6; i++)
396       hwaddr[i] = ConfVPD_buff[VPD_ENET0_OFFSET+i];
397  }
398
399#ifdef GT_DEBUG
400  printk("using MAC addr from device:");
401  for (i = 0; i < ETHER_ADDR_LEN; i++) printk("%x:", hwaddr[i]);
402  printk("\n");
403#endif
404
405  memcpy(sc->arpcom.ac_enaddr, hwaddr, ETHER_ADDR_LEN);
406
407  ifp = &sc->arpcom.ac_if;
408
409  sc->sc_pcr = inl(ETH0_EPCR);
410  sc->sc_pcxr = inl(ETH0_EPCXR);
411  sc->sc_intrmask = inl(ETH0_EIMR) | ETH_IR_MIIPhySTC;
412
413  printk("address %s\n", ether_sprintf(hwaddr));
414
415#ifdef GT_DEBUG
416  printk(", pcr %x, pcxr %x ", sc->sc_pcr, sc->sc_pcxr);
417#endif
418
419
420  sc->sc_pcxr |= ETH_EPCXR_PRIOrx_Override;
421  sc->sc_pcxr |= (3<<6); /* highest priority only */
422  sc->sc_pcxr &= ~ETH_EPCXR_RMIIEn;  /* MII mode */
423
424  /* Max. Frame Length (packet) allowed for reception is 1536 bytes,
425   * instead of 2048 (MOTLoad default) or 64K.
426   */
427  sc->sc_pcxr &= ~(3 << 14);
428  sc->sc_pcxr |= (ETH_EPCXR_MFL_1536 << 14);
429  sc->sc_max_frame_length = PKT_BUF_SZ;
430
431
432  if (sc->sc_pcr & ETH_EPCR_EN) {
433      int tries = 1000;
434      /* Abort transmitter and receiver and wait for them to quiese*/
435      outl(ETH_ESDCMR_AR|ETH_ESDCMR_AT,ETH0_ESDCMR);
436      do {
437        rtems_bsp_delay(100);
438      } while (tries-- > 0 && (inl(ETH0_ESDCMR) & (ETH_ESDCMR_AR|ETH_ESDCMR_AT)));
439  }
440#ifdef GT_DEBUG
441  printk(", phy %d (mii)\n", phyaddr);
442  printk("ETH0_ESDCMR %x ", inl(ETH0_ESDCMR));
443#endif
444
445  sc->sc_pcr &= ~(ETH_EPCR_EN | ETH_EPCR_RBM | ETH_EPCR_PM | ETH_EPCR_PBF);
446
447#ifdef GT_DEBUG
448        printk("Now sc_pcr %x,sc_pcxr %x", sc->sc_pcr, sc->sc_pcxr);
449#endif
450
451  /*
452   * Now turn off the GT.  If it didn't quiese, too ***ing bad.
453   */
454  outl(sc->sc_pcr, ETH0_EPCR);
455  outl(sc->sc_intrmask, ETH0_EIMR);
456  sdcr = inl(ETH0_ESDCR);
457  /* Burst size is limited to 4 64bit words */
458  ETH_ESDCR_BSZ_SET(sdcr, ETH_ESDCR_BSZ_4);
459  sdcr |= ETH_ESDCR_RIFB;/*limit interrupt on frame boundaries, instead of buffer*/
460#if 0
461  sdcr &= ~(ETH_ESDCR_BLMT|ETH_ESDCR_BLMR); /* MOTLoad defualt Big-endian */
462#endif
463  outl(sdcr, ETH0_ESDCR);
464
465#ifdef GT_DEBUG
466  printk("sdcr %x \n", sdcr);
467#endif
468
469  if (phyaddr== -1) printk("MII auto negotiation ?");
470
471  ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
472
473  ifp->if_softc = sc;
474
475  /* set this interface's name and unit */
476  ifp->if_unit = unit;
477  ifp->if_name = name;
478
479  ifp->if_mtu = config->mtu ? config->mtu : ETHERMTU;
480
481  ifp->if_init = GT64260eth_ifinit;
482  ifp->if_ioctl = GTeth_ifioctl;
483  ifp->if_start = GTeth_ifstart;
484  ifp->if_output = ether_output;
485
486  /*  ifp->if_watchdog = GTeth_ifwatchdog;*/
487
488  if (ifp->if_snd.ifq_maxlen == 0)
489    ifp->if_snd.ifq_maxlen = ifqmaxlen;
490
491  /* create the synchronization semaphore */
492  if (RTEMS_SUCCESSFUL != rtems_semaphore_create(
493     rtems_build_name('G','e','t','h'),0,0,0,&sc->daemonSync))
494     printk("GT64260eth: semaphore creation failed");
495
496  sc->next_module = root_GT64260eth_dev;
497  root_GT64260eth_dev = sc;
498
499  /* Actually attach the interface */
500  if_attach(ifp);
501  ether_ifattach(ifp);
502
503#ifdef GT_DEBUG
504  printk("GT64260eth: Ethernet driver has been attached (handle 0x%08x,ifp 0x%08x)\n",sc, ifp);
505#endif
506
507  return(1);
508}
509
510static void GT64260eth_stats(struct GTeth_softc *sc)
511{
512  struct ifnet *ifp = &sc->arpcom.ac_if;
513
514#if 0
515  printf("       Rx Interrupts:%-8lu\n", sc->stats.rxInterrupts);
516  printf("     Receive Packets:%-8lu\n", ifp->if_ipackets);
517  printf("     Receive  errors:%-8lu\n", ifp->if_ierrors);
518  printf("      Framing Errors:%-8lu\n", sc->stats.frame_errors);
519  printf("          Crc Errors:%-8lu\n", sc->stats.crc_errors);
520  printf("    Oversized Frames:%-8lu\n", sc->stats.length_errors);
521  printf("         Active Rxqs:%-8u\n",  sc->rxq_active);
522  printf("       Tx Interrupts:%-8lu\n", sc->stats.txInterrupts);
523#endif
524  printf("Multi-BuffTx Packets:%-8lu\n", sc->stats.txMultiBuffPacket);
525  printf("Multi-BuffTx max len:%-8lu\n", sc->stats.txMultiMaxLen);
526  printf("SingleBuffTx max len:%-8lu\n", sc->stats.txSinglMaxLen);
527  printf("Multi-BuffTx maxloop:%-8lu\n", sc->stats.txMultiMaxLoop);
528  printf("Tx buffer max len   :%-8lu\n", sc->stats.txBuffMaxLen);
529#if 0
530  printf("   Transmitt Packets:%-8lu\n", ifp->if_opackets);
531  printf("   Transmitt  errors:%-8lu\n", ifp->if_oerrors);
532  printf("    Tx/Rx collisions:%-8lu\n", ifp->if_collisions);
533  printf("         Active Txqs:%-8u\n", sc->txq_nactive);
534#endif
535}
536
[5d2f5196]537void GT64260eth_printStats(void)
[ee732739]538{
539  GT64260eth_stats(root_GT64260eth_dev);
540}
541
542static int GTeth_ifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
543{
544  struct GTeth_softc *sc = ifp->if_softc;
545  struct ifreq *ifr = (struct ifreq *) data;
546
547  int error = 0;
548
549#ifdef GT_DEBUG
550  printk("GTeth_ifioctl(");
551#endif
552
553  switch (cmd) {
554    default:
555      if (GTeth_debug >0) {
556         printk("etherioctl(");
557         if (cmd== SIOCSIFADDR) printk("SIOCSIFADDR ");
558      }
559      return ether_ioctl(ifp, cmd, data);
560
561    case SIOCSIFFLAGS:
562       switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
563            case IFF_RUNNING:   /* not up, so we stop */
564                 GT64260eth_stop(sc);
565                 break;
566            case IFF_UP:  /* not running, so we start */
567                 GT64260eth_ifinit(sc);
568                 break;
569            case IFF_UP|IFF_RUNNING:/* active->active, update */
570                 GT64260eth_stop(sc);
571                 GT64260eth_ifinit(sc);
572                 break;
573            default:                    /* idle->idle: do nothing */
574                 break;
575       }
576       break;
577    case SIO_RTEMS_SHOW_STATS:
578       GT64260eth_stats (sc);
579       break;
580    case SIOCADDMULTI:
581    case SIOCDELMULTI:
582       error = (cmd == SIOCADDMULTI)
583                  ? ether_addmulti(ifr, &sc->arpcom)
584                  : ether_delmulti(ifr, &sc->arpcom);
585       if (error == ENETRESET) {
586           if (ifp->if_flags & IFF_RUNNING)
587              GTeth_ifchange(sc);
[1966665]588           else
[ee732739]589              error = 0;
590       }
591       break;
592    case SIOCSIFMTU:
593       if (ifr->ifr_mtu > ETHERMTU || ifr->ifr_mtu < ETHERMIN) {
594          error = EINVAL;
595          break;
596       }
597       ifp->if_mtu = ifr->ifr_mtu;
598       break;
599  }
600
601#ifdef GT_DEBUG
602  printk("exit ioctl\n");
603#endif 
604  return error;
605}
606
607static void GTeth_ifstart(struct ifnet *ifp)
608{
609  struct GTeth_softc *sc = ifp->if_softc;
610
611#ifdef GT_DEBUG
612  printk("GTeth_ifstart(");
613#endif
614
615  if ((ifp->if_flags & IFF_RUNNING) == 0) {
616#ifdef GT_DEBUG
617     printk("IFF_RUNNING==0\n");
618#endif         
619     return;
620  }
621
622  ifp->if_flags |= IFF_OACTIVE;
623  rtems_event_send (sc->daemonTid, START_TRANSMIT_EVENT);
624#ifdef GT_DEBUG
625  printk(")\n");
626#endif
627}
628
629/* Initialize the Rx rings */
630static void GTeth_init_rx_ring(struct GTeth_softc *sc)
631{
632  int i;
633  volatile struct GTeth_desc *rxd;
634  unsigned nxtaddr;
635
636  sc->rxq_fi=0;
637  sc->rxq_head_desc = &sc->rxq_desc[0];
638  rxd = sc->rxq_head_desc;
639
640  sc->rxq_desc_busaddr = (unsigned long) sc->rxq_head_desc;
641#ifdef GT_DEBUG
642  printk("rxq_desc_busaddr %x ,&sc->rxq_desc[0] %x\n",
643        sc->rxq_desc_busaddr, sc->rxq_head_desc);
644#endif
645
646  nxtaddr = sc->rxq_desc_busaddr + sizeof(*rxd);
647  sc->rx_buf_sz = (sc->arpcom.ac_if.if_mtu <= 1500 ? PKT_BUF_SZ : sc->arpcom.ac_if.if_mtu + 32);
648  sc->rxq_active = RX_RING_SIZE;
649
650  for (i = 0; i < RX_RING_SIZE; i++, rxd++, nxtaddr += sizeof(*rxd)) {
651    struct mbuf *m;
652 
653    rxd->ed_lencnt= sc->rx_buf_sz <<16;
654    rxd->ed_cmdsts = RX_CMD_F|RX_CMD_L|RX_CMD_O|RX_CMD_EI;
655
656    MGETHDR(m, M_WAIT, MT_DATA);
657    MCLGET(m, M_WAIT);
658    m->m_pkthdr.rcvif =  &sc->arpcom.ac_if;
659    sc->rxq_mbuf[i] = m;
660
661    /* convert mbuf pointer to data pointer of correct type */ 
662    rxd->ed_bufptr = (unsigned) mtod(m, void *);
663
664    /*
665     * update the nxtptr to point to the next txd.
666     */
667    if (i == RX_RING_SIZE - 1)
668        nxtaddr = sc->rxq_desc_busaddr;
669    rxd->ed_nxtptr = nxtaddr;
670
671#ifdef GT_DEBUG
672  printk("ed_lencnt %x, rx_buf_sz %x ",rxd->ed_lencnt, sc->rx_buf_sz);
673  printk("ed_cmdsts %x \n",rxd->ed_cmdsts);
674  printk("mbuf @ 0x%x, next desc. @ 0x%x\n",rxd->ed_bufptr,rxd->ed_nxtptr);
675#endif
676  }
677}
678
679void GTeth_rxprio_setup(struct GTeth_softc *sc)
680{
681
682  GTeth_init_rx_ring(sc);
683
684  sc->rxq_intrbits = ETH_IR_RxBuffer|ETH_IR_RxError|ETH_IR_RxBuffer_3|ETH_IR_RxError_3;
685}
686
687static int GT64260eth_rx(struct GTeth_softc *sc)
688{
689  struct ifnet *ifp = &sc->arpcom.ac_if;
690  struct mbuf *m;
691  int nloops=0;
692
693#ifdef GT_DEBUG
694  if (GTeth_rx_debug>0) printk("GT64260eth_rx(");
695#endif
696
697  while (sc->rxq_active > 0) {
698    volatile struct GTeth_desc *rxd = &sc->rxq_desc[sc->rxq_fi];
699    struct ether_header *eh;
700    unsigned int cmdsts;
701    unsigned int byteCount;
702
703    cmdsts = rxd->ed_cmdsts;
704
705    /*
706     * Sometimes the GE "forgets" to reset the ownership bit.
707     * But if the length has been rewritten, the packet is ours
708     * so pretend the O bit is set.
709     *
710     */
711    byteCount = rxd->ed_lencnt & 0xffff;
712
713    if (cmdsts & RX_CMD_O) {
714      if (byteCount == 0)
715         return(0);
716
717     /* <Kate Feng> Setting command/status to be zero seems to eliminate
718      * the spurious interrupt associated with the GE_FORGOT issue.
719      */
720      rxd->ed_cmdsts=0;
721
722#ifdef GE_FORGOT
723      /*
724       * For dignosis purpose only. Not a good practice to turn it on
725       */
726      printk("Rxq %d %d %d\n", sc->rxq_fi, byteCount,nloops);
727#endif
728    }
729
730    /* GT gave the ownership back to the CPU or the length has
731     * been rewritten , which means there
732     * is new packet in the descriptor/buffer
733     */
734
735    nloops++;
736    /*
737     * If this is not a single buffer packet with no errors
738     * or for some reason it's bigger than our frame size,
739     * ignore it and go to the next packet.
740     */
741    if ((cmdsts & (RX_CMD_F|RX_CMD_L|RX_STS_ES)) !=
742                            (RX_CMD_F|RX_CMD_L) ||
743                    byteCount > sc->sc_max_frame_length) {
744        --sc->rxq_active;
745        ifp->if_ipackets++;
746        ifp->if_ierrors++;
747        if (cmdsts & RX_STS_OR) sc->stats.or_errors++;
748        if (cmdsts & RX_STS_CE) sc->stats.crc_errors++;
749        if (cmdsts & RX_STS_MFL) sc->stats.length_errors++;
750        if (cmdsts & RX_STS_SF) sc->stats.frame_errors++;
751        if ((cmdsts & RX_STS_LC) || (cmdsts & RX_STS_COL))
752           ifp->if_collisions++;
753        goto give_it_back;
754     }
755     m = sc->rxq_mbuf[sc->rxq_fi];
756     m->m_len = m->m_pkthdr.len = byteCount - sizeof(struct ether_header);
757     eh = mtod (m, struct ether_header *);
758     m->m_data += sizeof(struct ether_header);
759     ether_input (ifp, eh, m);
760
761     ifp->if_ipackets++;
762     ifp->if_ibytes+=byteCount;
763     --sc->rxq_active;
764
765     give_it_back:
766     MGETHDR (m, M_WAIT, MT_DATA);
767     MCLGET (m, M_WAIT);
768     m->m_pkthdr.rcvif = ifp;
769     sc->rxq_mbuf[sc->rxq_fi]= m;
770     /* convert mbuf pointer to data pointer of correct type */
771     rxd->ed_bufptr = (unsigned) mtod(m, void*);
772     rxd->ed_lencnt = (unsigned long) sc->rx_buf_sz <<16;
773     rxd->ed_cmdsts = RX_CMD_F|RX_CMD_L|RX_CMD_O|RX_CMD_EI;
774
775     if (++sc->rxq_fi == RX_RING_SIZE) sc->rxq_fi = 0;
776
777     sc->rxq_active++;
778  } /* while (sc->rxq_active > 0) */
779#ifdef GT_DEBUG
780  if (GTeth_rx_debug>0) printk(")");
781#endif
782  return nloops;
783}
784
785static void GTeth_rx_setup(struct GTeth_softc *sc)
786{
787
788  if (GTeth_rx_debug>0) printk("GTeth_rx_setup(");
789
790  GTeth_rxprio_setup(sc);
791
792  if ((sc->sc_flags & GE_RXACTIVE) == 0) {
793     /* First Rx Descriptor Pointer 3 */
794     outl( sc->rxq_desc_busaddr, ETH0_EFRDP3);
795     /* Current Rx Descriptor Pointer 3 */
796     outl( sc->rxq_desc_busaddr,ETH0_ECRDP3);
797#ifdef GT_DEBUG
798     printk("ETH0_EFRDP3 0x%x, ETH0_ECRDP3 0x%x \n", inl(ETH0_EFRDP3),
799            inl(ETH0_ECRDP3));
800#endif
801  }
802  sc->sc_intrmask |= sc->rxq_intrbits;
803
804  if (GTeth_rx_debug>0) printk(")\n");
805}
806
807static void GTeth_rx_cleanup(struct GTeth_softc *sc)
808{
809  int i;
810
811  if (GTeth_rx_debug>0) printk( "GTeth_rx_cleanup(");
812
813  for (i=0; i< RX_RING_SIZE; i++) {
814    if (sc->rxq_mbuf[i]) {
815      m_freem(sc->rxq_mbuf[i]);
816      sc->rxq_mbuf[i]=0;
817    }
818  }
819  if (GTeth_rx_debug>0) printk(")");
820}
821
822static void GTeth_rx_stop(struct GTeth_softc *sc)
823{
824  if (GTeth_rx_debug>0) printk( "GTeth_rx_stop(");
825  sc->sc_flags &= ~GE_RXACTIVE;
826  sc->sc_idlemask &= ~(ETH_IR_RxBits|ETH_IR_RxBuffer_3|ETH_IR_RxError_3);
827  sc->sc_intrmask &= ~(ETH_IR_RxBits|ETH_IR_RxBuffer_3|ETH_IR_RxError_3);
828  outl(sc->sc_intrmask, ETH0_EIMR);
829  outl(ETH_ESDCMR_AR, ETH0_ESDCMR); /* abort receive */
830  do {
831     rtems_bsp_delay(10);
832  } while (inl(ETH0_ESDCMR) & ETH_ESDCMR_AR);
833  GTeth_rx_cleanup(sc);
834  if (GTeth_rx_debug>0) printk(")");
835}
836
837static void GTeth_txq_free(struct GTeth_softc *sc, unsigned cmdsts)
838{
839  struct ifnet *ifp = &sc->arpcom.ac_if;
840  volatile struct GTeth_desc *txd = &sc->txq_desc[sc->txq_fi];
841
842  /* ownership is sent back to CPU */
843  if (GTeth_debug>0) printk("txq%d,active %d\n", sc->txq_fi, sc->txq_nactive);
844
845  txd->ed_cmdsts &= ~TX_CMD_O; /* <skf> in case GT forgot */
846
847  /* statistics */
848  ifp->if_opackets++;
849  ifp->if_obytes += sc->txq_mbuf[sc->txq_fi]->m_len;
850  if (cmdsts & TX_STS_ES) {
851       ifp->if_oerrors++;
852       if ((cmdsts & TX_STS_LC) || (cmdsts & TX_STS_COL))
853           ifp->if_collisions++;
854  }
855  /* Free the original mbuf chain */
856  m_freem(sc->txq_mbuf[sc->txq_fi]);
857  sc->txq_mbuf[sc->txq_fi] = 0;
858  ifp->if_timer = 5;
859
860  sc->txq_free++;
861  if (++sc->txq_fi == TX_RING_SIZE) sc->txq_fi = 0;
862  --sc->txq_nactive;
863}
864
865static int txq_high_limit(struct GTeth_softc *sc)
866{
867  /*
868   * Have we [over]consumed our limit of descriptors?
869   * Do we have enough free descriptors?
870   */
871  if ( TX_RING_SIZE == sc->txq_nactive + TXQ_HiLmt_OFF) {
872     volatile struct GTeth_desc *txd2 = &sc->txq_desc[sc->txq_fi];
873     unsigned cmdsts;
874
875     cmdsts = txd2->ed_cmdsts;
876     if (cmdsts & TX_CMD_O) {  /* Ownership (1=GT 0=CPU) */
877         int nextin;
878
879         /*
880          * Sometime the Discovery forgets to update the
881          * last descriptor.  See if CPU owned the descriptor
882          * after it (since we know we've turned that to
883          * the discovery and if CPU owned it, the Discovery
884          * gave it back).  If CPU does, we know the Discovery
885          * gave back this one but forgot to mark it back to CPU.
886          */
887         nextin = (sc->txq_fi + 1) % TX_RING_SIZE;
888         if (sc->txq_desc[nextin].ed_cmdsts & TX_CMD_O) {
889#if 0
890            printk("Overconsuming Tx descriptors!\n");
891#endif
892            return 1;
893         }
894         printk("Txq %d forgot\n", sc->txq_fi);
895     }
896    /* Txq ring is almost full, let's free the current buffer here */
897#if 0
898    printk("Txq ring near full, free desc%d\n",sc->txq_fi);
899#endif
900    GTeth_txq_free(sc, cmdsts);
901  } /* end if ( TX_RING_SIZE == sc->txq_nactive + TXQ_HiLmt_OFF) */
902  return 0;
903}
904
905static int GT64260eth_sendpacket(struct GTeth_softc *sc,struct mbuf *m)
906{
907  volatile struct GTeth_desc *txd = &sc->txq_desc[sc->txq_lo];
908  unsigned intrmask = sc->sc_intrmask;
909  unsigned loop=0, index= sc->txq_lo;
910
911  /*
912   * The end-of-list descriptor we put on last time is the starting point
913   * for this packet.  The GT is supposed to terminate list processing on
914   * a NULL nxtptr but that currently is broken so a CPU-owned descriptor
915   * must terminate the list.
916   */
917  intrmask = sc->sc_intrmask;
918
919  if ( !(m->m_next)) {/* single buffer packet */
920    sc->txq_mbuf[index]= m;
921    sc->stats.txSinglMaxLen= MAX(m->m_len, sc->stats.txSinglMaxLen);
922  }
923  else /* multiple mbufs in this packet */
924  {
925    struct mbuf *mtp, *mdest;
926    volatile unsigned char *pt;
927    int len, y;
928
929#ifdef GT_DEBUG
930    printk("multi mbufs ");
931#endif   
932    MGETHDR(mdest, M_WAIT, MT_DATA);
933    MCLGET(mdest, M_WAIT);
934    pt = (volatile unsigned char *)mdest->m_data;
935    for (mtp=m,len=0;mtp;mtp=mtp->m_next) {
936      loop++;
937      if ( (y=(len+mtp->m_len)) > sizeof(union mcluster)) {
938        /* GT64260 allows us to chain the remaining to the next
939         * free descriptors.
940         */
941        printk("packet size %x > mcluster %x\n", y,sizeof(union mcluster));
942        printk("GT64260eth : packet too large ");
943      }
944      memcpy((void *)pt,(char *)mtp->m_data, mtp->m_len);
945      pt += mtp->m_len;
946#if 0
947    printk("%d ",mtp->m_len);
948#endif
949      len += mtp->m_len;
950      sc->stats.txBuffMaxLen=MAX(mtp->m_len,sc->stats.txBuffMaxLen);
951    }
952    sc->stats.txMultiMaxLoop=MAX(loop, sc->stats.txMultiMaxLoop);
953#if 0
954    printk("\n");
955#endif
956    mdest->m_len=len;
957    /* free old mbuf chain */
958    m_freem(m);
959    sc->txq_mbuf[index] = m = mdest;
960    sc->stats.txMultiBuffPacket++;
961    sc->stats.txMultiMaxLen= MAX(m->m_len, sc->stats.txMultiMaxLen);
962  }
963  if (m->m_len < ET_MINLEN) m->m_len = ET_MINLEN;
964
965  txd->ed_bufptr = (unsigned) mtod(m, void*);
966  txd->ed_lencnt = m->m_len << 16;
967  /*txd->ed_cmdsts = TX_CMD_L|TX_CMD_GC|TX_CMD_P|TX_CMD_O|TX_CMD_F|TX_CMD_EI;*/
968  txd->ed_cmdsts = 0x80c70000;
969  while (txd->ed_cmdsts != 0x80c70000);
970  memBar();
971
972#ifdef GT_DEBUG
973  printk("len = %d, cmdsts 0x%x ", m->m_len,txd->ed_cmdsts);
974#endif
975
976  /*
977   * Tell the SDMA engine to "Fetch!"
978   * Start Tx high.
979   */
980  sc->txq_nactive++;
981  outl(0x800080, ETH0_ESDCMR); /* ETH_ESDCMR_TXDH| ETH_ESDCMR_ERD */
982  if ( ++sc->txq_lo == TX_RING_SIZE) sc->txq_lo = 0;
983  sc->txq_free--;
984
985#if 0
986  /*
987   * Since we have put an item into the packet queue, we now want
988   * an interrupt when the transmit queue finishes processing the
989   * list.  But only update the mask if needs changing.
990   */
991  intrmask |= sc->txq_intrbits & ( ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh);
992  if (sc->sc_intrmask != intrmask) {
993      sc->sc_intrmask = intrmask;
994      outl(sc->sc_intrmask, ETH0_EIMR);
995  }
996#endif
997
998#if 0
999  printk("EICR= %x, EIMR= %x ", inl(ETH0_EICR), inl(ETH0_EIMR));
1000  printk("%s:transmit frame #%d queued in slot %d.\n",
1001              sc->arpcom.ac_if.if_name, sc->txq_lo, index);
1002  printk("pcr %x, pcxr %x DMA dcr %x cmr %x\n", inl(ETH0_EPCR), inl(ETH0_EPCXR), inl(ETH0_ESDCR), inl(ETH0_ESDCMR));
1003#endif
1004
1005  return 1;
1006}
1007
1008static unsigned GTeth_txq_done(struct GTeth_softc *sc)
1009{
1010  if (GTeth_debug>0) printk("Txdone(" );
1011
1012  while (sc->txq_nactive > 0) {
1013    /* next to be returned to the CPU */
1014    volatile struct GTeth_desc *txd = &sc->txq_desc[sc->txq_fi];
1015    unsigned cmdsts;
1016
1017    /* if GT64260 still owns it ....... */
1018    if ((cmdsts = txd->ed_cmdsts) & TX_CMD_O) {
1019       int nextin;
1020
1021       /* Someone quoted :
1022        * "Sometimes the Discovery forgets to update the
1023        * ownership bit in the descriptor."
1024        * <skf> More correctly, the last descriptor of each
1025        * transmitted frame is returned to CPU ownership and
1026        * status is updated only after the actual transmission
1027        * of the packet is completed.  Also, if there is an error
1028        * during transmission, we want to continue the
1029        * transmission of the next descriptor, in additions to
1030        * reporting the error.
1031        */
1032       /* The last descriptor */
1033       if (sc->txq_nactive == 1) return(0);
1034
1035       /*
1036        * Sometimes the Discovery forgets to update the
1037        * ownership bit in the descriptor.  See if CPU owned
1038        * the descriptor after it (since we know we've turned
1039        * that to the Discovery and if CPU owned it now then the
1040        * Discovery gave it back).  If we do, we know the
1041        * Discovery gave back this one but forgot to mark it
1042        * back to CPU.
1043        */
1044       nextin = (sc->txq_fi + 1) % TX_RING_SIZE;
1045
1046       if (sc->txq_desc[nextin].ed_cmdsts & TX_CMD_O) return(0);
1047       printk("Txq%d forgot\n",sc->txq_fi);
1048    } /* end checking GT64260eth owner */
1049    GTeth_txq_free(sc, cmdsts);   
1050  }  /* end while */
1051  if (GTeth_debug>0) printk(")\n");
1052  return(1);
1053}
1054
1055static void GTeth_tx_start(struct GTeth_softc *sc)
1056{
1057  int i;
1058  volatile struct GTeth_desc *txd;
1059  unsigned nxtaddr;
1060
1061#ifdef GT_DEBUG
1062  printk("GTeth_tx_start(");
1063#endif
1064  sc->sc_intrmask &= ~(ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh|
1065                             ETH_IR_TxEndLow |ETH_IR_TxBufferLow);
1066
1067  txd = &sc->txq_desc[0];
1068  sc->txq_desc_busaddr = (unsigned long) &sc->txq_desc[0];
1069#ifdef GT_DEBUG
1070  printk("txq_desc_busaddr %x, &sc->txq_desc[0] %x \n",
1071         sc->txq_desc_busaddr,&sc->txq_desc[0]);
1072#endif
1073
1074  nxtaddr = sc->txq_desc_busaddr + sizeof(*txd);
1075
1076  sc->txq_pendq.ifq_maxlen = 10;
1077  sc->txq_pendq.ifq_head= NULL;
1078  sc->txq_pendq.ifq_tail= NULL;
1079  sc->txq_nactive = 0;
1080  sc->txq_fi = 0;
1081  sc->txq_lo = 0;
1082  sc->txq_inptr = PKT_BUF_SZ;
1083  sc->txq_outptr = 0;
1084  sc->txq_free = TX_RING_SIZE;
1085
1086  for (i = 0; i < TX_RING_SIZE;
1087       i++, txd++,  nxtaddr += sizeof(*txd)) {
1088      sc->txq_mbuf[i]=0;
1089      txd->ed_bufptr = 0;
1090
1091      /*
1092       * update the nxtptr to point to the next txd.
1093       */
1094      txd->ed_cmdsts = 0;
1095      if ( i== TX_RING_SIZE-1) nxtaddr = sc->txq_desc_busaddr;
1096      txd->ed_nxtptr =  nxtaddr;
1097#ifdef GT_DEBUG
1098      printk("next desc. @ 0x%x\n",txd->ed_nxtptr);
1099#endif
1100  }
1101 
1102  sc->txq_intrbits = ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh;
1103  sc->txq_esdcmrbits = ETH_ESDCMR_TXDH; /* Start Tx high */
1104  sc->txq_epsrbits = ETH_EPSR_TxHigh;
1105  /* offset to current tx desc ptr reg */
1106  sc->txq_ectdp = (caddr_t)ETH0_ECTDP1;
1107  /* Current Tx Desc Pointer 1 */
1108  outl(sc->txq_desc_busaddr,ETH0_ECTDP1);
1109
1110#ifdef GT_DEBUG
1111  printk(")\n");
1112#endif
1113}
1114
1115static void GTeth_tx_cleanup(struct GTeth_softc *sc)
1116{
1117  int i;
1118
1119  for (i=0; i< TX_RING_SIZE; i++) {
1120    if (sc->txq_mbuf[i]) {
1121      m_freem(sc->txq_mbuf[i]);
1122      sc->txq_mbuf[i]=0;
1123    }
1124  }
1125}
1126
1127static void GTeth_tx_stop(struct GTeth_softc *sc)
1128{
1129  /* SDMA command register : stop Tx high and low */
1130  outl(ETH_ESDCMR_STDH|ETH_ESDCMR_STDL, ETH0_ESDCMR);
1131
1132  GTeth_txq_done(sc);
1133  sc->sc_intrmask &= ~(ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh|
1134                             ETH_IR_TxEndLow |ETH_IR_TxBufferLow);
1135  GTeth_tx_cleanup(sc);
1136
1137  sc->arpcom.ac_if.if_timer = 0;
1138}
1139
1140/* TOCHECK : Should it be about rx or tx ? */
1141static void GTeth_ifchange(struct GTeth_softc *sc)
1142{
1143  if (GTeth_debug>0) printk("GTeth_ifchange(");
1144  if (GTeth_debug>5) printk("(pcr=%#x,imr=%#x)",inl(ETH0_EPCR),inl(ETH0_EIMR));
1145  /*  printk("SIOCADDMULTI (SIOCDELMULTI): is it about rx or tx ?\n");*/
1146  outl(sc->sc_pcr | ETH_EPCR_EN, ETH0_EPCR);
1147  outl(sc->sc_intrmask, ETH0_EIMR);
1148  GTeth_ifstart(&sc->arpcom.ac_if);
1149  /* Current Tx Desc Pointer 0 and 1 */
1150  if (GTeth_debug>5) printk("(ectdp0=%#x, ectdp1=%#x)",
1151            inl(ETH0_ECTDP0), inl(ETH0_ECTDP1));
1152  if (GTeth_debug>0) printk(")");
1153}
1154
1155static int GTeth_hash_compute(struct GTeth_softc *sc,unsigned char eaddr[ETHER_ADDR_LEN])
1156{
1157  unsigned w0, add0, add1;
1158  unsigned result;
1159
1160  if (GTeth_debug>0) printk("GTeth_hash_compute(");
1161  add0 = ((unsigned) eaddr[5] <<  0) |
1162         ((unsigned) eaddr[4] <<  8) |
1163         ((unsigned) eaddr[3] << 16);
1164
1165  add0 = ((add0 & 0x00f0f0f0) >> 4) | ((add0 & 0x000f0f0f) << 4);
1166  add0 = ((add0 & 0x00cccccc) >> 2) | ((add0 & 0x00333333) << 2);
1167  add0 = ((add0 & 0x00aaaaaa) >> 1) | ((add0 & 0x00555555) << 1);
1168
1169  add1 = ((unsigned) eaddr[2] <<  0) |
1170         ((unsigned) eaddr[1] <<  8) |
1171         ((unsigned) eaddr[0] << 16);
1172
1173  add1 = ((add1 & 0x00f0f0f0) >> 4) | ((add1 & 0x000f0f0f) << 4);
1174  add1 = ((add1 & 0x00cccccc) >> 2) | ((add1 & 0x00333333) << 2);
1175  add1 = ((add1 & 0x00aaaaaa) >> 1) | ((add1 & 0x00555555) << 1);
1176
1177#ifdef GT_DEBUG
1178  printk("eaddr= %s add1:%x add0:%x\n", ether_sprintf1(eaddr), add1, add0);
1179#endif
1180   
1181  /*
1182   * hashResult is the 15 bits Hash entry address.
1183   * ethernetADD is a 48 bit number, which is derived from the Ethernet
1184   * MAC address, by nibble swapping in every byte (i.e MAC address
1185   * of 0x123456789abc translates to ethernetADD of 0x21436587a9cb).
1186   */
1187  if ((sc->sc_pcr & ETH_EPCR_HM) == 0) {
1188     /*
1189      * hashResult[14:0] = hashFunc0(ethernetADD[47:0])
1190      *
1191      * hashFunc0 calculates the hashResult in the following manner:
1192      * hashResult[ 8:0] = ethernetADD[14:8,1,0]
1193      * XOR ethernetADD[23:15] XOR ethernetADD[32:24]
1194      */
1195     result = (add0 & 3) | ((add0 >> 6) & ~3);
1196     result ^= (add0 >> 15) ^ (add1 >>  0);
1197     result &= 0x1ff;
1198     /*
1199      *   hashResult[14:9] = ethernetADD[7:2]
1200      */
1201     result |= (add0 & ~3) << 7;        /* excess bits will be masked */
1202#ifdef GT_DEBUG
1203     printk("hash result %x  ", result & 0x7fff);
1204#endif
1205  } else {
1206#define TRIBITFLIP      073516240       /* yes its in octal */
1207     /*
1208      * hashResult[14:0] = hashFunc1(ethernetADD[47:0])
1209      *
1210      * hashFunc1 calculates the hashResult in the following manner:
1211      * hashResult[08:00] = ethernetADD[06:14]
1212      * XOR ethernetADD[15:23] XOR ethernetADD[24:32]
1213      */
1214     w0 = ((add0 >> 6) ^ (add0 >> 15) ^ (add1)) & 0x1ff;
1215     /*
1216      * Now bitswap those 9 bits
1217      */
1218     result = 0;
1219     result |= ((TRIBITFLIP >> (((w0 >> 0) & 7) * 3)) & 7) << 6;
1220     result |= ((TRIBITFLIP >> (((w0 >> 3) & 7) * 3)) & 7) << 3;
1221     result |= ((TRIBITFLIP >> (((w0 >> 6) & 7) * 3)) & 7) << 0;
1222
1223     /*
1224      *   hashResult[14:09] = ethernetADD[00:05]
1225      */
1226     result |= ((TRIBITFLIP >> (((add0 >> 0) & 7) * 3)) & 7) << 12;
1227     result |= ((TRIBITFLIP >> (((add0 >> 3) & 7) * 3)) & 7) << 9;
1228#ifdef GT_DEBUG
1229     printk("1(%#x)", result);
1230#endif
1231  }
1232#ifdef GT_DEBUG
1233  printk(")");
1234#endif
1235
1236  /* 1/2K address filtering (MOTLoad default )? ->16KB memory required
1237   * or 8k address filtering ? -> 256KB memory required
1238   */
1239  return result & ((sc->sc_pcr & ETH_EPCR_HS_512) ? 0x7ff : 0x7fff);
1240}
1241
1242static int GTeth_hash_entry_op(struct GTeth_softc *sc, enum GTeth_hash_op op,
1243        enum GTeth_rxprio prio,unsigned char eaddr[ETHER_ADDR_LEN])
1244{
1245  unsigned long long he;
1246  unsigned long long *maybe_he_p = NULL;
1247  int limit;
1248  int hash;
1249  int maybe_hash = 0;
1250
1251#ifdef GT_DEBUG
1252  printk("GTeth_hash_entry_op(prio %d ", prio);
1253#endif
1254
1255  hash = GTeth_hash_compute(sc, eaddr);
1256
1257  if (sc->sc_hashtable == NULL) {
1258        printk("hashtable == NULL!");
1259  }
1260#ifdef GT_DEBUG
1261  printk("Hash computed %x eaddr %s\n", hash,ether_sprintf1(eaddr));
1262#endif
1263
1264  /*
1265   * Assume we are going to insert so create the hash entry we
1266   * are going to insert.  We also use it to match entries we
1267   * will be removing.  The datasheet is wrong for this.
1268   */
1269  he = (((unsigned long long) eaddr[5]) << 43) |
1270       (((unsigned long long) eaddr[4]) << 35) |
1271       (((unsigned long long) eaddr[3]) << 27) |
1272       (((unsigned long long) eaddr[2]) << 19) |
1273       (((unsigned long long) eaddr[1]) << 11) |
1274       (((unsigned long long) eaddr[0]) <<  3) |
1275       ((unsigned long long) HSH_PRIO_INS(prio) | HSH_V | HSH_R);
1276  /*   he = 0x1b1acd87d08005;*/
1277  /*
1278   * The GT will search upto 12 entries for a hit, so we must mimic that.
1279   */
1280  hash &= (sc->sc_hashmask / sizeof(he));
1281
1282#ifdef GT_DEBUG
1283  if (GTeth_debug>0) {
1284    unsigned val1, val2;
1285
1286    val1= he & 0xffffffff;
1287    val2= (he >>32) & 0xffffffff;
1288    printk("Hash addr value %x%x, entry %x\n",val2,val1, hash);
1289  }
1290#endif
1291
1292  for (limit = HSH_LIMIT; limit > 0 ; --limit) {
1293      /*
1294       * Does the GT wrap at the end, stop at the, or overrun the
1295       * end?  Assume it wraps for now.  Stash a copy of the
1296       * current hash entry.
1297       */
1298      unsigned long long *he_p = &sc->sc_hashtable[hash];
1299      unsigned long long thishe = *he_p;
1300
1301      /*
1302       * If the hash entry isn't valid, that break the chain.  And
1303       * this entry a good candidate for reuse.
1304       */
1305      if ((thishe & HSH_V) == 0) {
1306         maybe_he_p = he_p;
1307         break;
1308      }
1309
1310      /*
1311       * If the hash entry has the same address we are looking for
1312       * then ...  if we are removing and the skip bit is set, its
1313       * already been removed.  if are adding and the skip bit is
1314       * clear, then its already added.  In either return EBUSY
1315       * indicating the op has already been done.  Otherwise flip
1316       * the skip bit and return 0.
1317       */
1318      if (((he ^ thishe) & HSH_ADDR_MASK) == 0) {
1319         if (((op == GE_HASH_REMOVE) && (thishe & HSH_S)) ||
1320            ((op == GE_HASH_ADD) && (thishe & HSH_S) == 0))
1321            return EBUSY;
1322          *he_p = thishe ^ HSH_S;
1323
1324          if (GTeth_debug>0) {
1325             unsigned val1, val2;
1326
1327             val1= *he_p & 0xffffffff;
1328             val2= (*he_p >>32) & 0xffffffff;
1329             printk("flip skip bit result %x%x entry %x ",val2,val1, hash);
1330          }
1331          return 0;
1332       }
1333
1334       /*
1335        * If we haven't found a slot for the entry and this entry
1336        * is currently being skipped, return this entry.
1337        */
1338       if (maybe_he_p == NULL && (thishe & HSH_S)) {
1339          maybe_he_p = he_p;
1340          maybe_hash = hash;
1341       }               
1342       hash = (hash + 1) & (sc->sc_hashmask / sizeof(he));
1343  }
1344
1345  /*
1346   * If we got here, then there was no entry to remove.
1347   */
1348  if (op == GE_HASH_REMOVE) {
1349     printk("GT64260eth : No entry to remove\n");
1350     return ENOENT;
1351  }
1352
1353  /*
1354   * If we couldn't find a slot, return an error.
1355   */
1356  if (maybe_he_p == NULL) {
1357     printk("GT64260eth : No slot found");
1358     return ENOSPC;
1359  }
1360
1361  /* Update the entry.*/
1362  *maybe_he_p = he;
1363  if (GTeth_debug>0) {
1364    unsigned val1, val2;
1365#if 0
1366    unsigned long *pt= sc->sc_hashtable;
1367    int i, loop;
1368
1369  for (loop= 0; loop< 256; loop++) {
1370    printk("%d)", loop);
1371    for (i=0; i< 16; i++, pt++) printk("%x ",*pt);
1372    printk("\n");
1373  }
1374#endif
1375    val1= he & 0xffffffff;
1376    val2= (he >>32) & 0xffffffff;
1377    printk("Update Hash result %x%x, table addr %x entry %x )\n",val2, val1, maybe_he_p, hash);
1378  }
1379  return 0;
1380}
1381
1382static int GTeth_hash_fill(struct GTeth_softc *sc)
1383{
1384  struct ether_multistep step;
1385  struct ether_multi *enm;
1386  int error;
1387
1388#ifdef GT_DEBUG
1389  printk( "GTeth_hash_fill(");
1390#endif
1391  error = GTeth_hash_entry_op(sc,GE_HASH_ADD,GE_RXPRIO_HI,sc->arpcom.ac_enaddr);
1392
1393  if (error) {
1394     if (GTeth_debug>0) printk("!");
1395     return error;
1396  }
1397
1398  sc->sc_flags &= ~GE_ALLMULTI;
1399  if ((sc->arpcom.ac_if.if_flags & IFF_PROMISC) == 0)
1400     sc->sc_pcr &= ~ETH_EPCR_PM;
1401  /* see lib/include/netinet/if_ether.h */
1402  ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
1403  while (enm != NULL) {
1404    if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1405      /* Frames are received regardless of their destinatin address */
1406       sc->sc_flags |= GE_ALLMULTI;
1407       sc->sc_pcr |= ETH_EPCR_PM;
1408    } else {
1409      /* Frames are only received if the destinatin address is found
1410       * in the hash table
1411       */
1412       error = GTeth_hash_entry_op(sc, GE_HASH_ADD,
1413             GE_RXPRIO_MEDLO, enm->enm_addrlo);
1414       if (error == ENOSPC) break;
1415    }
1416    ETHER_NEXT_MULTI(step, enm);
1417  }
1418#ifdef GT_DEBUG
1419  printk(")\n");
1420#endif
1421  return error;
1422}
1423
1424static void GTeth_hash_init(struct GTeth_softc *sc)
1425{
1426  void *hash_mem;
1427
1428  if (GTeth_debug>0) printk("GTeth_hash_init(");
1429  /* MOTLoad defualt : 512 bytes of address filtering, which
1430   * requires 16KB of memory
1431   */
1432#if 1
1433  hash_mem = rtems_bsdnet_malloc(HASH_DRAM_SIZE + HASH_ALIGN, M_FREE, M_NOWAIT);
1434  sc->sc_hashtable  =(void *)(((long)hash_mem+ HASH_ALIGN) & ~HASH_ALIGN);
1435#else
1436  /* only for test */
1437  hash_mem = 0x68F000;
1438  sc->sc_hashtable  =(unsigned long long *)hash_mem;
1439#endif
1440  sc->sc_hashmask = HASH_DRAM_SIZE - 1;
1441
1442  memset((void *)sc->sc_hashtable, 0,HASH_DRAM_SIZE);
1443#ifdef GT_DEBUG
1444  printk("hashtable addr:%x, mask %x)\n", sc->sc_hashtable,sc->sc_hashmask);
1445#endif
1446}
1447
1448static void GT64260eth_error(struct GTeth_softc *sc)
1449{
1450  struct ifnet          *ifp = &sc->arpcom.ac_if;
1451  unsigned int          intr_status= sc->intr_errsts[sc->intr_err_ptr1];
1452
1453  /* read and reset the status; because this is written
1454   * by the ISR, we must disable interrupts here
1455   */
1456  if (intr_status) {
1457    printk("%s%d: ICR = 0x%x ",
1458           ifp->if_name, ifp->if_unit, intr_status);
1459#if 1
1460    if (intr_status & INTR_RX_ERROR) {
1461       printk("Rxq error, if_ierrors %d\n",
1462              ifp->if_ierrors);
1463    }
1464#endif
1465    /* Rx error is handled in GT64260eth_rx() */
1466    if (intr_status & INTR_TX_ERROR) {
1467       ifp->if_oerrors++;
1468       printk("Txq error,  if_oerrors %d\n",ifp->if_oerrors);
1469    }
1470  }
1471  else
1472    printk("%s%d: Ghost interrupt ?\n",ifp->if_name,
1473           ifp->if_unit);
1474  sc->intr_errsts[sc->intr_err_ptr1++]=0; 
1475  sc->intr_err_ptr1 %= INTR_ERR_SIZE;   /* Till Straumann */
1476}
1477
1478
1479/* The daemon does all of the work; RX, TX and cleaning up buffers/descriptors */
1480static void GT64260eth_daemon(void *arg)
1481{
1482  struct GTeth_softc *sc = (struct GTeth_softc*)arg;
1483  rtems_event_set       events;
1484  struct mbuf   *m=0;
1485  struct ifnet  *ifp=&sc->arpcom.ac_if;
1486
1487#if 0   
1488  /* see comments in GT64260eth_init(); in newer versions of
1489   * rtems, we hold the network semaphore at this point
1490   */
1491  rtems_semaphore_release(sc->daemonSync);
1492#endif
1493
1494  /* NOTE: our creator possibly holds the bsdnet_semaphore.
1495   *       since that has PRIORITY_INVERSION enabled, our
1496   *       subsequent call to bsdnet_event_receive() will
1497   *       _not_ release it. It's still in posession of our
1498   *       owner.
1499   *       This is different from how killing this task
1500   *       is handled.
1501   */
1502
1503  for (;;) {
1504     /* sleep until there's work to be done */
1505     /* Note: bsdnet_event_receive() acquires
1506      *       the global bsdnet semaphore for
1507      *       mutual exclusion.
1508      */
1509     rtems_bsdnet_event_receive(ALL_EVENTS,
1510                                RTEMS_WAIT | RTEMS_EVENT_ANY,
1511                                RTEMS_NO_TIMEOUT,
1512                                &events);
1513 
1514     if (KILL_EVENT & events) break;
1515
1516#ifndef GE_NORX
1517     if (events & RX_EVENT) GT64260eth_rx(sc);
1518#endif
1519#if 0
1520     printk("%x ", inb(ETH0_EPSR));
1521     if ( ((i++) % 15)==0) printk("\n");
1522#endif
1523
1524     /* clean up and try sending packets */
1525     do {
1526         if (sc->txq_nactive) GTeth_txq_done(sc);
1527
1528         while (sc->txq_free>0) {
1529           if (sc->txq_free>TXQ_HiLmt_OFF) {
1530              m=0;
1531              IF_DEQUEUE(&ifp->if_snd,m);
1532              if (m==0) break;
1533              GT64260eth_sendpacket(sc, m);
1534           }
1535           else {
1536              GTeth_txq_done(sc);
1537              break;
1538           }
1539         }
1540         /* we leave this loop
1541          *  - either because there's no free buffer
1542          *    (m=0 initializer && !sc->txq_free)
1543          *  - or there's nothing to send (IF_DEQUEUE
1544          *    returned 0
1545          */
1546       } while (m);
1547
1548       ifp->if_flags &= ~IFF_OACTIVE;
1549
1550       /* Log errors and other uncommon events. */
1551       if (events & ERR_EVENT) GT64260eth_error(sc);
1552  } /* end for(;;) { rtems_bsdnet_event_receive() .....*/
1553
1554  ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
1555
1556  /* shut down the hardware */
1557  GT64260eth_stop_hw(sc);
1558  /* flush the output queue */
1559  for (;;) {
1560      IF_DEQUEUE(&ifp->if_snd,m);
1561      if (!m) break;
1562      m_freem(m);
1563  }
1564  /* as of 'rtems_bsdnet_event_receive()' we own the
1565   * networking semaphore
1566   */
1567  rtems_bsdnet_semaphore_release();
1568  rtems_semaphore_release(sc->daemonSync);
1569
1570  /* Note that I dont use sc->daemonTid here -
1571   * theoretically, that variable could already
1572   * hold a newly created TID
1573   */
1574  rtems_task_delete(RTEMS_SELF);
1575}
1576
Note: See TracBrowser for help on using the repository browser.