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

4.104.114.84.95
Last change on this file since ee732739 was ee732739, checked in by Joel Sherrill <joel.sherrill@…>, on 09/13/07 at 14:26:24

2007-09-07 Kate Feng <feng1@…>

  • ChangeLog?, Makefile.am, README, README.booting, README.irq, preinstall.am, GT64260/MVME5500I2C.c, include/bsp.h, irq/irq.c, irq/irq.h, irq/irq_init.c, pci/detect_host_bridge.c, pci/pci.c, pci/pci_interface.c, pci/pcifinddevice.c, start/preload.S, startup/bspclean.c, startup/bspstart.c, startup/pgtbl_activate.c, startup/reboot.c, vectors/bspException.h, vectors/exceptionhandler.c: Merge my improvements in this BSP including a new network driver for the 1GHz NIC.
  • network/if_100MHz/GT64260eth.c, network/if_100MHz/GT64260eth.h, network/if_100MHz/GT64260ethreg.h, network/if_100MHz/Makefile.am, network/if_1GHz/Makefile.am, network/if_1GHz/POSSIBLEBUG, network/if_1GHz/if_wm.c, network/if_1GHz/if_wmreg.h, network/if_1GHz/pci_map.c, network/if_1GHz/pcireg.h: New files.
  • Property mode set to 100644
File size: 44.2 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);
128static void GT64260eth_isr();
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
161static void GT64260eth_isr()
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
537void GT64260eth_printStats()
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);
588              error = 0;
589       }
590       break;
591    case SIOCSIFMTU:
592       if (ifr->ifr_mtu > ETHERMTU || ifr->ifr_mtu < ETHERMIN) {
593          error = EINVAL;
594          break;
595       }
596       ifp->if_mtu = ifr->ifr_mtu;
597       break;
598  }
599
600#ifdef GT_DEBUG
601  printk("exit ioctl\n");
602#endif 
603  return error;
604}
605
606static void GTeth_ifstart(struct ifnet *ifp)
607{
608  struct GTeth_softc *sc = ifp->if_softc;
609
610#ifdef GT_DEBUG
611  printk("GTeth_ifstart(");
612#endif
613
614  if ((ifp->if_flags & IFF_RUNNING) == 0) {
615#ifdef GT_DEBUG
616     printk("IFF_RUNNING==0\n");
617#endif         
618     return;
619  }
620
621  ifp->if_flags |= IFF_OACTIVE;
622  rtems_event_send (sc->daemonTid, START_TRANSMIT_EVENT);
623#ifdef GT_DEBUG
624  printk(")\n");
625#endif
626}
627
628/* Initialize the Rx rings */
629static void GTeth_init_rx_ring(struct GTeth_softc *sc)
630{
631  int i;
632  volatile struct GTeth_desc *rxd;
633  unsigned nxtaddr;
634
635  sc->rxq_fi=0;
636  sc->rxq_head_desc = &sc->rxq_desc[0];
637  rxd = sc->rxq_head_desc;
638
639  sc->rxq_desc_busaddr = (unsigned long) sc->rxq_head_desc;
640#ifdef GT_DEBUG
641  printk("rxq_desc_busaddr %x ,&sc->rxq_desc[0] %x\n",
642        sc->rxq_desc_busaddr, sc->rxq_head_desc);
643#endif
644
645  nxtaddr = sc->rxq_desc_busaddr + sizeof(*rxd);
646  sc->rx_buf_sz = (sc->arpcom.ac_if.if_mtu <= 1500 ? PKT_BUF_SZ : sc->arpcom.ac_if.if_mtu + 32);
647  sc->rxq_active = RX_RING_SIZE;
648
649  for (i = 0; i < RX_RING_SIZE; i++, rxd++, nxtaddr += sizeof(*rxd)) {
650    struct mbuf *m;
651 
652    rxd->ed_lencnt= sc->rx_buf_sz <<16;
653    rxd->ed_cmdsts = RX_CMD_F|RX_CMD_L|RX_CMD_O|RX_CMD_EI;
654
655    MGETHDR(m, M_WAIT, MT_DATA);
656    MCLGET(m, M_WAIT);
657    m->m_pkthdr.rcvif =  &sc->arpcom.ac_if;
658    sc->rxq_mbuf[i] = m;
659
660    /* convert mbuf pointer to data pointer of correct type */ 
661    rxd->ed_bufptr = (unsigned) mtod(m, void *);
662
663    /*
664     * update the nxtptr to point to the next txd.
665     */
666    if (i == RX_RING_SIZE - 1)
667        nxtaddr = sc->rxq_desc_busaddr;
668    rxd->ed_nxtptr = nxtaddr;
669
670#ifdef GT_DEBUG
671  printk("ed_lencnt %x, rx_buf_sz %x ",rxd->ed_lencnt, sc->rx_buf_sz);
672  printk("ed_cmdsts %x \n",rxd->ed_cmdsts);
673  printk("mbuf @ 0x%x, next desc. @ 0x%x\n",rxd->ed_bufptr,rxd->ed_nxtptr);
674#endif
675  }
676}
677
678void GTeth_rxprio_setup(struct GTeth_softc *sc)
679{
680
681  GTeth_init_rx_ring(sc);
682
683  sc->rxq_intrbits = ETH_IR_RxBuffer|ETH_IR_RxError|ETH_IR_RxBuffer_3|ETH_IR_RxError_3;
684}
685
686static int GT64260eth_rx(struct GTeth_softc *sc)
687{
688  struct ifnet *ifp = &sc->arpcom.ac_if;
689  struct mbuf *m;
690  int nloops=0;
691
692#ifdef GT_DEBUG
693  if (GTeth_rx_debug>0) printk("GT64260eth_rx(");
694#endif
695
696  while (sc->rxq_active > 0) {
697    volatile struct GTeth_desc *rxd = &sc->rxq_desc[sc->rxq_fi];
698    struct ether_header *eh;
699    unsigned int cmdsts;
700    unsigned int byteCount;
701
702    cmdsts = rxd->ed_cmdsts;
703
704    /*
705     * Sometimes the GE "forgets" to reset the ownership bit.
706     * But if the length has been rewritten, the packet is ours
707     * so pretend the O bit is set.
708     *
709     */
710    byteCount = rxd->ed_lencnt & 0xffff;
711
712    if (cmdsts & RX_CMD_O) {
713      if (byteCount == 0)
714         return(0);
715
716     /* <Kate Feng> Setting command/status to be zero seems to eliminate
717      * the spurious interrupt associated with the GE_FORGOT issue.
718      */
719      rxd->ed_cmdsts=0;
720
721#ifdef GE_FORGOT
722      /*
723       * For dignosis purpose only. Not a good practice to turn it on
724       */
725      printk("Rxq %d %d %d\n", sc->rxq_fi, byteCount,nloops);
726#endif
727    }
728
729    /* GT gave the ownership back to the CPU or the length has
730     * been rewritten , which means there
731     * is new packet in the descriptor/buffer
732     */
733
734    nloops++;
735    /*
736     * If this is not a single buffer packet with no errors
737     * or for some reason it's bigger than our frame size,
738     * ignore it and go to the next packet.
739     */
740    if ((cmdsts & (RX_CMD_F|RX_CMD_L|RX_STS_ES)) !=
741                            (RX_CMD_F|RX_CMD_L) ||
742                    byteCount > sc->sc_max_frame_length) {
743        --sc->rxq_active;
744        ifp->if_ipackets++;
745        ifp->if_ierrors++;
746        if (cmdsts & RX_STS_OR) sc->stats.or_errors++;
747        if (cmdsts & RX_STS_CE) sc->stats.crc_errors++;
748        if (cmdsts & RX_STS_MFL) sc->stats.length_errors++;
749        if (cmdsts & RX_STS_SF) sc->stats.frame_errors++;
750        if ((cmdsts & RX_STS_LC) || (cmdsts & RX_STS_COL))
751           ifp->if_collisions++;
752        goto give_it_back;
753     }
754     m = sc->rxq_mbuf[sc->rxq_fi];
755     m->m_len = m->m_pkthdr.len = byteCount - sizeof(struct ether_header);
756     eh = mtod (m, struct ether_header *);
757     m->m_data += sizeof(struct ether_header);
758     ether_input (ifp, eh, m);
759
760     ifp->if_ipackets++;
761     ifp->if_ibytes+=byteCount;
762     --sc->rxq_active;
763
764     give_it_back:
765     MGETHDR (m, M_WAIT, MT_DATA);
766     MCLGET (m, M_WAIT);
767     m->m_pkthdr.rcvif = ifp;
768     sc->rxq_mbuf[sc->rxq_fi]= m;
769     /* convert mbuf pointer to data pointer of correct type */
770     rxd->ed_bufptr = (unsigned) mtod(m, void*);
771     rxd->ed_lencnt = (unsigned long) sc->rx_buf_sz <<16;
772     rxd->ed_cmdsts = RX_CMD_F|RX_CMD_L|RX_CMD_O|RX_CMD_EI;
773
774     if (++sc->rxq_fi == RX_RING_SIZE) sc->rxq_fi = 0;
775
776     sc->rxq_active++;
777  } /* while (sc->rxq_active > 0) */
778#ifdef GT_DEBUG
779  if (GTeth_rx_debug>0) printk(")");
780#endif
781  return nloops;
782}
783
784static void GTeth_rx_setup(struct GTeth_softc *sc)
785{
786
787  if (GTeth_rx_debug>0) printk("GTeth_rx_setup(");
788
789  GTeth_rxprio_setup(sc);
790
791  if ((sc->sc_flags & GE_RXACTIVE) == 0) {
792     /* First Rx Descriptor Pointer 3 */
793     outl( sc->rxq_desc_busaddr, ETH0_EFRDP3);
794     /* Current Rx Descriptor Pointer 3 */
795     outl( sc->rxq_desc_busaddr,ETH0_ECRDP3);
796#ifdef GT_DEBUG
797     printk("ETH0_EFRDP3 0x%x, ETH0_ECRDP3 0x%x \n", inl(ETH0_EFRDP3),
798            inl(ETH0_ECRDP3));
799#endif
800  }
801  sc->sc_intrmask |= sc->rxq_intrbits;
802
803  if (GTeth_rx_debug>0) printk(")\n");
804}
805
806static void GTeth_rx_cleanup(struct GTeth_softc *sc)
807{
808  int i;
809
810  if (GTeth_rx_debug>0) printk( "GTeth_rx_cleanup(");
811
812  for (i=0; i< RX_RING_SIZE; i++) {
813    if (sc->rxq_mbuf[i]) {
814      m_freem(sc->rxq_mbuf[i]);
815      sc->rxq_mbuf[i]=0;
816    }
817  }
818  if (GTeth_rx_debug>0) printk(")");
819}
820
821static void GTeth_rx_stop(struct GTeth_softc *sc)
822{
823  if (GTeth_rx_debug>0) printk( "GTeth_rx_stop(");
824  sc->sc_flags &= ~GE_RXACTIVE;
825  sc->sc_idlemask &= ~(ETH_IR_RxBits|ETH_IR_RxBuffer_3|ETH_IR_RxError_3);
826  sc->sc_intrmask &= ~(ETH_IR_RxBits|ETH_IR_RxBuffer_3|ETH_IR_RxError_3);
827  outl(sc->sc_intrmask, ETH0_EIMR);
828  outl(ETH_ESDCMR_AR, ETH0_ESDCMR); /* abort receive */
829  do {
830     rtems_bsp_delay(10);
831  } while (inl(ETH0_ESDCMR) & ETH_ESDCMR_AR);
832  GTeth_rx_cleanup(sc);
833  if (GTeth_rx_debug>0) printk(")");
834}
835
836static void GTeth_txq_free(struct GTeth_softc *sc, unsigned cmdsts)
837{
838  struct ifnet *ifp = &sc->arpcom.ac_if;
839  volatile struct GTeth_desc *txd = &sc->txq_desc[sc->txq_fi];
840
841  /* ownership is sent back to CPU */
842  if (GTeth_debug>0) printk("txq%d,active %d\n", sc->txq_fi, sc->txq_nactive);
843
844  txd->ed_cmdsts &= ~TX_CMD_O; /* <skf> in case GT forgot */
845
846  /* statistics */
847  ifp->if_opackets++;
848  ifp->if_obytes += sc->txq_mbuf[sc->txq_fi]->m_len;
849  if (cmdsts & TX_STS_ES) {
850       ifp->if_oerrors++;
851       if ((cmdsts & TX_STS_LC) || (cmdsts & TX_STS_COL))
852           ifp->if_collisions++;
853  }
854  /* Free the original mbuf chain */
855  m_freem(sc->txq_mbuf[sc->txq_fi]);
856  sc->txq_mbuf[sc->txq_fi] = 0;
857  ifp->if_timer = 5;
858
859  sc->txq_free++;
860  if (++sc->txq_fi == TX_RING_SIZE) sc->txq_fi = 0;
861  --sc->txq_nactive;
862}
863
864static int txq_high_limit(struct GTeth_softc *sc)
865{
866  /*
867   * Have we [over]consumed our limit of descriptors?
868   * Do we have enough free descriptors?
869   */
870  if ( TX_RING_SIZE == sc->txq_nactive + TXQ_HiLmt_OFF) {
871     volatile struct GTeth_desc *txd2 = &sc->txq_desc[sc->txq_fi];
872     unsigned cmdsts;
873
874     cmdsts = txd2->ed_cmdsts;
875     if (cmdsts & TX_CMD_O) {  /* Ownership (1=GT 0=CPU) */
876         int nextin;
877
878         /*
879          * Sometime the Discovery forgets to update the
880          * last descriptor.  See if CPU owned the descriptor
881          * after it (since we know we've turned that to
882          * the discovery and if CPU owned it, the Discovery
883          * gave it back).  If CPU does, we know the Discovery
884          * gave back this one but forgot to mark it back to CPU.
885          */
886         nextin = (sc->txq_fi + 1) % TX_RING_SIZE;
887         if (sc->txq_desc[nextin].ed_cmdsts & TX_CMD_O) {
888#if 0
889            printk("Overconsuming Tx descriptors!\n");
890#endif
891            return 1;
892         }
893         printk("Txq %d forgot\n", sc->txq_fi);
894     }
895    /* Txq ring is almost full, let's free the current buffer here */
896#if 0
897    printk("Txq ring near full, free desc%d\n",sc->txq_fi);
898#endif
899    GTeth_txq_free(sc, cmdsts);
900  } /* end if ( TX_RING_SIZE == sc->txq_nactive + TXQ_HiLmt_OFF) */
901  return 0;
902}
903
904static int GT64260eth_sendpacket(struct GTeth_softc *sc,struct mbuf *m)
905{
906  volatile struct GTeth_desc *txd = &sc->txq_desc[sc->txq_lo];
907  unsigned intrmask = sc->sc_intrmask;
908  unsigned loop=0, index= sc->txq_lo;
909
910  /*
911   * The end-of-list descriptor we put on last time is the starting point
912   * for this packet.  The GT is supposed to terminate list processing on
913   * a NULL nxtptr but that currently is broken so a CPU-owned descriptor
914   * must terminate the list.
915   */
916  intrmask = sc->sc_intrmask;
917
918  if ( !(m->m_next)) {/* single buffer packet */
919    sc->txq_mbuf[index]= m;
920    sc->stats.txSinglMaxLen= MAX(m->m_len, sc->stats.txSinglMaxLen);
921  }
922  else /* multiple mbufs in this packet */
923  {
924    struct mbuf *mtp, *mdest;
925    volatile unsigned char *pt;
926    int len, y;
927
928#ifdef GT_DEBUG
929    printk("multi mbufs ");
930#endif   
931    MGETHDR(mdest, M_WAIT, MT_DATA);
932    MCLGET(mdest, M_WAIT);
933    pt = (volatile unsigned char *)mdest->m_data;
934    for (mtp=m,len=0;mtp;mtp=mtp->m_next) {
935      loop++;
936      if ( (y=(len+mtp->m_len)) > sizeof(union mcluster)) {
937        /* GT64260 allows us to chain the remaining to the next
938         * free descriptors.
939         */
940        printk("packet size %x > mcluster %x\n", y,sizeof(union mcluster));
941        printk("GT64260eth : packet too large ");
942      }
943      memcpy((void *)pt,(char *)mtp->m_data, mtp->m_len);
944      pt += mtp->m_len;
945#if 0
946    printk("%d ",mtp->m_len);
947#endif
948      len += mtp->m_len;
949      sc->stats.txBuffMaxLen=MAX(mtp->m_len,sc->stats.txBuffMaxLen);
950    }
951    sc->stats.txMultiMaxLoop=MAX(loop, sc->stats.txMultiMaxLoop);
952#if 0
953    printk("\n");
954#endif
955    mdest->m_len=len;
956    /* free old mbuf chain */
957    m_freem(m);
958    sc->txq_mbuf[index] = m = mdest;
959    sc->stats.txMultiBuffPacket++;
960    sc->stats.txMultiMaxLen= MAX(m->m_len, sc->stats.txMultiMaxLen);
961  }
962  if (m->m_len < ET_MINLEN) m->m_len = ET_MINLEN;
963
964  txd->ed_bufptr = (unsigned) mtod(m, void*);
965  txd->ed_lencnt = m->m_len << 16;
966  /*txd->ed_cmdsts = TX_CMD_L|TX_CMD_GC|TX_CMD_P|TX_CMD_O|TX_CMD_F|TX_CMD_EI;*/
967  txd->ed_cmdsts = 0x80c70000;
968  while (txd->ed_cmdsts != 0x80c70000);
969  memBar();
970
971#ifdef GT_DEBUG
972  printk("len = %d, cmdsts 0x%x ", m->m_len,txd->ed_cmdsts);
973#endif
974
975  /*
976   * Tell the SDMA engine to "Fetch!"
977   * Start Tx high.
978   */
979  sc->txq_nactive++;
980  outl(0x800080, ETH0_ESDCMR); /* ETH_ESDCMR_TXDH| ETH_ESDCMR_ERD */
981  if ( ++sc->txq_lo == TX_RING_SIZE) sc->txq_lo = 0;
982  sc->txq_free--;
983
984#if 0
985  /*
986   * Since we have put an item into the packet queue, we now want
987   * an interrupt when the transmit queue finishes processing the
988   * list.  But only update the mask if needs changing.
989   */
990  intrmask |= sc->txq_intrbits & ( ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh);
991  if (sc->sc_intrmask != intrmask) {
992      sc->sc_intrmask = intrmask;
993      outl(sc->sc_intrmask, ETH0_EIMR);
994  }
995#endif
996
997#if 0
998  printk("EICR= %x, EIMR= %x ", inl(ETH0_EICR), inl(ETH0_EIMR));
999  printk("%s:transmit frame #%d queued in slot %d.\n",
1000              sc->arpcom.ac_if.if_name, sc->txq_lo, index);
1001  printk("pcr %x, pcxr %x DMA dcr %x cmr %x\n", inl(ETH0_EPCR), inl(ETH0_EPCXR), inl(ETH0_ESDCR), inl(ETH0_ESDCMR));
1002#endif
1003
1004  return 1;
1005}
1006
1007static unsigned GTeth_txq_done(struct GTeth_softc *sc)
1008{
1009  if (GTeth_debug>0) printk("Txdone(" );
1010
1011  while (sc->txq_nactive > 0) {
1012    /* next to be returned to the CPU */
1013    volatile struct GTeth_desc *txd = &sc->txq_desc[sc->txq_fi];
1014    unsigned cmdsts;
1015
1016    /* if GT64260 still owns it ....... */
1017    if ((cmdsts = txd->ed_cmdsts) & TX_CMD_O) {
1018       int nextin;
1019
1020       /* Someone quoted :
1021        * "Sometimes the Discovery forgets to update the
1022        * ownership bit in the descriptor."
1023        * <skf> More correctly, the last descriptor of each
1024        * transmitted frame is returned to CPU ownership and
1025        * status is updated only after the actual transmission
1026        * of the packet is completed.  Also, if there is an error
1027        * during transmission, we want to continue the
1028        * transmission of the next descriptor, in additions to
1029        * reporting the error.
1030        */
1031       /* The last descriptor */
1032       if (sc->txq_nactive == 1) return(0);
1033
1034       /*
1035        * Sometimes the Discovery forgets to update the
1036        * ownership bit in the descriptor.  See if CPU owned
1037        * the descriptor after it (since we know we've turned
1038        * that to the Discovery and if CPU owned it now then the
1039        * Discovery gave it back).  If we do, we know the
1040        * Discovery gave back this one but forgot to mark it
1041        * back to CPU.
1042        */
1043       nextin = (sc->txq_fi + 1) % TX_RING_SIZE;
1044
1045       if (sc->txq_desc[nextin].ed_cmdsts & TX_CMD_O) return(0);
1046       printk("Txq%d forgot\n",sc->txq_fi);
1047    } /* end checking GT64260eth owner */
1048    GTeth_txq_free(sc, cmdsts);   
1049  }  /* end while */
1050  if (GTeth_debug>0) printk(")\n");
1051  return(1);
1052}
1053
1054static void GTeth_tx_start(struct GTeth_softc *sc)
1055{
1056  int i;
1057  volatile struct GTeth_desc *txd;
1058  unsigned nxtaddr;
1059
1060#ifdef GT_DEBUG
1061  printk("GTeth_tx_start(");
1062#endif
1063  sc->sc_intrmask &= ~(ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh|
1064                             ETH_IR_TxEndLow |ETH_IR_TxBufferLow);
1065
1066  txd = &sc->txq_desc[0];
1067  sc->txq_desc_busaddr = (unsigned long) &sc->txq_desc[0];
1068#ifdef GT_DEBUG
1069  printk("txq_desc_busaddr %x, &sc->txq_desc[0] %x \n",
1070         sc->txq_desc_busaddr,&sc->txq_desc[0]);
1071#endif
1072
1073  nxtaddr = sc->txq_desc_busaddr + sizeof(*txd);
1074
1075  sc->txq_pendq.ifq_maxlen = 10;
1076  sc->txq_pendq.ifq_head= NULL;
1077  sc->txq_pendq.ifq_tail= NULL;
1078  sc->txq_nactive = 0;
1079  sc->txq_fi = 0;
1080  sc->txq_lo = 0;
1081  sc->txq_inptr = PKT_BUF_SZ;
1082  sc->txq_outptr = 0;
1083  sc->txq_free = TX_RING_SIZE;
1084
1085  for (i = 0; i < TX_RING_SIZE;
1086       i++, txd++,  nxtaddr += sizeof(*txd)) {
1087      sc->txq_mbuf[i]=0;
1088      txd->ed_bufptr = 0;
1089
1090      /*
1091       * update the nxtptr to point to the next txd.
1092       */
1093      txd->ed_cmdsts = 0;
1094      if ( i== TX_RING_SIZE-1) nxtaddr = sc->txq_desc_busaddr;
1095      txd->ed_nxtptr =  nxtaddr;
1096#ifdef GT_DEBUG
1097      printk("next desc. @ 0x%x\n",txd->ed_nxtptr);
1098#endif
1099  }
1100 
1101  sc->txq_intrbits = ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh;
1102  sc->txq_esdcmrbits = ETH_ESDCMR_TXDH; /* Start Tx high */
1103  sc->txq_epsrbits = ETH_EPSR_TxHigh;
1104  /* offset to current tx desc ptr reg */
1105  sc->txq_ectdp = (caddr_t)ETH0_ECTDP1;
1106  /* Current Tx Desc Pointer 1 */
1107  outl(sc->txq_desc_busaddr,ETH0_ECTDP1);
1108
1109#ifdef GT_DEBUG
1110  printk(")\n");
1111#endif
1112}
1113
1114static void GTeth_tx_cleanup(struct GTeth_softc *sc)
1115{
1116  int i;
1117
1118  for (i=0; i< TX_RING_SIZE; i++) {
1119    if (sc->txq_mbuf[i]) {
1120      m_freem(sc->txq_mbuf[i]);
1121      sc->txq_mbuf[i]=0;
1122    }
1123  }
1124}
1125
1126static void GTeth_tx_stop(struct GTeth_softc *sc)
1127{
1128  /* SDMA command register : stop Tx high and low */
1129  outl(ETH_ESDCMR_STDH|ETH_ESDCMR_STDL, ETH0_ESDCMR);
1130
1131  GTeth_txq_done(sc);
1132  sc->sc_intrmask &= ~(ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh|
1133                             ETH_IR_TxEndLow |ETH_IR_TxBufferLow);
1134  GTeth_tx_cleanup(sc);
1135
1136  sc->arpcom.ac_if.if_timer = 0;
1137}
1138
1139/* TOCHECK : Should it be about rx or tx ? */
1140static void GTeth_ifchange(struct GTeth_softc *sc)
1141{
1142  if (GTeth_debug>0) printk("GTeth_ifchange(");
1143  if (GTeth_debug>5) printk("(pcr=%#x,imr=%#x)",inl(ETH0_EPCR),inl(ETH0_EIMR));
1144  /*  printk("SIOCADDMULTI (SIOCDELMULTI): is it about rx or tx ?\n");*/
1145  outl(sc->sc_pcr | ETH_EPCR_EN, ETH0_EPCR);
1146  outl(sc->sc_intrmask, ETH0_EIMR);
1147  GTeth_ifstart(&sc->arpcom.ac_if);
1148  /* Current Tx Desc Pointer 0 and 1 */
1149  if (GTeth_debug>5) printk("(ectdp0=%#x, ectdp1=%#x)",
1150            inl(ETH0_ECTDP0), inl(ETH0_ECTDP1));
1151  if (GTeth_debug>0) printk(")");
1152}
1153
1154static int GTeth_hash_compute(struct GTeth_softc *sc,unsigned char eaddr[ETHER_ADDR_LEN])
1155{
1156  unsigned w0, add0, add1;
1157  unsigned result;
1158
1159  if (GTeth_debug>0) printk("GTeth_hash_compute(");
1160  add0 = ((unsigned) eaddr[5] <<  0) |
1161         ((unsigned) eaddr[4] <<  8) |
1162         ((unsigned) eaddr[3] << 16);
1163
1164  add0 = ((add0 & 0x00f0f0f0) >> 4) | ((add0 & 0x000f0f0f) << 4);
1165  add0 = ((add0 & 0x00cccccc) >> 2) | ((add0 & 0x00333333) << 2);
1166  add0 = ((add0 & 0x00aaaaaa) >> 1) | ((add0 & 0x00555555) << 1);
1167
1168  add1 = ((unsigned) eaddr[2] <<  0) |
1169         ((unsigned) eaddr[1] <<  8) |
1170         ((unsigned) eaddr[0] << 16);
1171
1172  add1 = ((add1 & 0x00f0f0f0) >> 4) | ((add1 & 0x000f0f0f) << 4);
1173  add1 = ((add1 & 0x00cccccc) >> 2) | ((add1 & 0x00333333) << 2);
1174  add1 = ((add1 & 0x00aaaaaa) >> 1) | ((add1 & 0x00555555) << 1);
1175
1176#ifdef GT_DEBUG
1177  printk("eaddr= %s add1:%x add0:%x\n", ether_sprintf1(eaddr), add1, add0);
1178#endif
1179   
1180  /*
1181   * hashResult is the 15 bits Hash entry address.
1182   * ethernetADD is a 48 bit number, which is derived from the Ethernet
1183   * MAC address, by nibble swapping in every byte (i.e MAC address
1184   * of 0x123456789abc translates to ethernetADD of 0x21436587a9cb).
1185   */
1186  if ((sc->sc_pcr & ETH_EPCR_HM) == 0) {
1187     /*
1188      * hashResult[14:0] = hashFunc0(ethernetADD[47:0])
1189      *
1190      * hashFunc0 calculates the hashResult in the following manner:
1191      * hashResult[ 8:0] = ethernetADD[14:8,1,0]
1192      * XOR ethernetADD[23:15] XOR ethernetADD[32:24]
1193      */
1194     result = (add0 & 3) | ((add0 >> 6) & ~3);
1195     result ^= (add0 >> 15) ^ (add1 >>  0);
1196     result &= 0x1ff;
1197     /*
1198      *   hashResult[14:9] = ethernetADD[7:2]
1199      */
1200     result |= (add0 & ~3) << 7;        /* excess bits will be masked */
1201#ifdef GT_DEBUG
1202     printk("hash result %x  ", result & 0x7fff);
1203#endif
1204  } else {
1205#define TRIBITFLIP      073516240       /* yes its in octal */
1206     /*
1207      * hashResult[14:0] = hashFunc1(ethernetADD[47:0])
1208      *
1209      * hashFunc1 calculates the hashResult in the following manner:
1210      * hashResult[08:00] = ethernetADD[06:14]
1211      * XOR ethernetADD[15:23] XOR ethernetADD[24:32]
1212      */
1213     w0 = ((add0 >> 6) ^ (add0 >> 15) ^ (add1)) & 0x1ff;
1214     /*
1215      * Now bitswap those 9 bits
1216      */
1217     result = 0;
1218     result |= ((TRIBITFLIP >> (((w0 >> 0) & 7) * 3)) & 7) << 6;
1219     result |= ((TRIBITFLIP >> (((w0 >> 3) & 7) * 3)) & 7) << 3;
1220     result |= ((TRIBITFLIP >> (((w0 >> 6) & 7) * 3)) & 7) << 0;
1221
1222     /*
1223      *   hashResult[14:09] = ethernetADD[00:05]
1224      */
1225     result |= ((TRIBITFLIP >> (((add0 >> 0) & 7) * 3)) & 7) << 12;
1226     result |= ((TRIBITFLIP >> (((add0 >> 3) & 7) * 3)) & 7) << 9;
1227#ifdef GT_DEBUG
1228     printk("1(%#x)", result);
1229#endif
1230  }
1231#ifdef GT_DEBUG
1232  printk(")");
1233#endif
1234
1235  /* 1/2K address filtering (MOTLoad default )? ->16KB memory required
1236   * or 8k address filtering ? -> 256KB memory required
1237   */
1238  return result & ((sc->sc_pcr & ETH_EPCR_HS_512) ? 0x7ff : 0x7fff);
1239}
1240
1241static int GTeth_hash_entry_op(struct GTeth_softc *sc, enum GTeth_hash_op op,
1242        enum GTeth_rxprio prio,unsigned char eaddr[ETHER_ADDR_LEN])
1243{
1244  unsigned long long he;
1245  unsigned long long *maybe_he_p = NULL;
1246  int limit;
1247  int hash;
1248  int maybe_hash = 0;
1249
1250#ifdef GT_DEBUG
1251  printk("GTeth_hash_entry_op(prio %d ", prio);
1252#endif
1253
1254  hash = GTeth_hash_compute(sc, eaddr);
1255
1256  if (sc->sc_hashtable == NULL) {
1257        printk("hashtable == NULL!");
1258  }
1259#ifdef GT_DEBUG
1260  printk("Hash computed %x eaddr %s\n", hash,ether_sprintf1(eaddr));
1261#endif
1262
1263  /*
1264   * Assume we are going to insert so create the hash entry we
1265   * are going to insert.  We also use it to match entries we
1266   * will be removing.  The datasheet is wrong for this.
1267   */
1268  he = (((unsigned long long) eaddr[5]) << 43) |
1269       (((unsigned long long) eaddr[4]) << 35) |
1270       (((unsigned long long) eaddr[3]) << 27) |
1271       (((unsigned long long) eaddr[2]) << 19) |
1272       (((unsigned long long) eaddr[1]) << 11) |
1273       (((unsigned long long) eaddr[0]) <<  3) |
1274       ((unsigned long long) HSH_PRIO_INS(prio) | HSH_V | HSH_R);
1275  /*   he = 0x1b1acd87d08005;*/
1276  /*
1277   * The GT will search upto 12 entries for a hit, so we must mimic that.
1278   */
1279  hash &= (sc->sc_hashmask / sizeof(he));
1280
1281#ifdef GT_DEBUG
1282  if (GTeth_debug>0) {
1283    unsigned val1, val2;
1284
1285    val1= he & 0xffffffff;
1286    val2= (he >>32) & 0xffffffff;
1287    printk("Hash addr value %x%x, entry %x\n",val2,val1, hash);
1288  }
1289#endif
1290
1291  for (limit = HSH_LIMIT; limit > 0 ; --limit) {
1292      /*
1293       * Does the GT wrap at the end, stop at the, or overrun the
1294       * end?  Assume it wraps for now.  Stash a copy of the
1295       * current hash entry.
1296       */
1297      unsigned long long *he_p = &sc->sc_hashtable[hash];
1298      unsigned long long thishe = *he_p;
1299
1300      /*
1301       * If the hash entry isn't valid, that break the chain.  And
1302       * this entry a good candidate for reuse.
1303       */
1304      if ((thishe & HSH_V) == 0) {
1305         maybe_he_p = he_p;
1306         break;
1307      }
1308
1309      /*
1310       * If the hash entry has the same address we are looking for
1311       * then ...  if we are removing and the skip bit is set, its
1312       * already been removed.  if are adding and the skip bit is
1313       * clear, then its already added.  In either return EBUSY
1314       * indicating the op has already been done.  Otherwise flip
1315       * the skip bit and return 0.
1316       */
1317      if (((he ^ thishe) & HSH_ADDR_MASK) == 0) {
1318         if (((op == GE_HASH_REMOVE) && (thishe & HSH_S)) ||
1319            ((op == GE_HASH_ADD) && (thishe & HSH_S) == 0))
1320            return EBUSY;
1321          *he_p = thishe ^ HSH_S;
1322
1323          if (GTeth_debug>0) {
1324             unsigned val1, val2;
1325
1326             val1= *he_p & 0xffffffff;
1327             val2= (*he_p >>32) & 0xffffffff;
1328             printk("flip skip bit result %x%x entry %x ",val2,val1, hash);
1329          }
1330          return 0;
1331       }
1332
1333       /*
1334        * If we haven't found a slot for the entry and this entry
1335        * is currently being skipped, return this entry.
1336        */
1337       if (maybe_he_p == NULL && (thishe & HSH_S)) {
1338          maybe_he_p = he_p;
1339          maybe_hash = hash;
1340       }               
1341       hash = (hash + 1) & (sc->sc_hashmask / sizeof(he));
1342  }
1343
1344  /*
1345   * If we got here, then there was no entry to remove.
1346   */
1347  if (op == GE_HASH_REMOVE) {
1348     printk("GT64260eth : No entry to remove\n");
1349     return ENOENT;
1350  }
1351
1352  /*
1353   * If we couldn't find a slot, return an error.
1354   */
1355  if (maybe_he_p == NULL) {
1356     printk("GT64260eth : No slot found");
1357     return ENOSPC;
1358  }
1359
1360  /* Update the entry.*/
1361  *maybe_he_p = he;
1362  if (GTeth_debug>0) {
1363    unsigned val1, val2;
1364#if 0
1365    unsigned long *pt= sc->sc_hashtable;
1366    int i, loop;
1367
1368  for (loop= 0; loop< 256; loop++) {
1369    printk("%d)", loop);
1370    for (i=0; i< 16; i++, pt++) printk("%x ",*pt);
1371    printk("\n");
1372  }
1373#endif
1374    val1= he & 0xffffffff;
1375    val2= (he >>32) & 0xffffffff;
1376    printk("Update Hash result %x%x, table addr %x entry %x )\n",val2, val1, maybe_he_p, hash);
1377  }
1378  return 0;
1379}
1380
1381static int GTeth_hash_fill(struct GTeth_softc *sc)
1382{
1383  struct ether_multistep step;
1384  struct ether_multi *enm;
1385  int error;
1386
1387#ifdef GT_DEBUG
1388  printk( "GTeth_hash_fill(");
1389#endif
1390  error = GTeth_hash_entry_op(sc,GE_HASH_ADD,GE_RXPRIO_HI,sc->arpcom.ac_enaddr);
1391
1392  if (error) {
1393     if (GTeth_debug>0) printk("!");
1394     return error;
1395  }
1396
1397  sc->sc_flags &= ~GE_ALLMULTI;
1398  if ((sc->arpcom.ac_if.if_flags & IFF_PROMISC) == 0)
1399     sc->sc_pcr &= ~ETH_EPCR_PM;
1400  /* see lib/include/netinet/if_ether.h */
1401  ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
1402  while (enm != NULL) {
1403    if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1404      /* Frames are received regardless of their destinatin address */
1405       sc->sc_flags |= GE_ALLMULTI;
1406       sc->sc_pcr |= ETH_EPCR_PM;
1407    } else {
1408      /* Frames are only received if the destinatin address is found
1409       * in the hash table
1410       */
1411       error = GTeth_hash_entry_op(sc, GE_HASH_ADD,
1412             GE_RXPRIO_MEDLO, enm->enm_addrlo);
1413       if (error == ENOSPC) break;
1414    }
1415    ETHER_NEXT_MULTI(step, enm);
1416  }
1417#ifdef GT_DEBUG
1418  printk(")\n");
1419#endif
1420  return error;
1421}
1422
1423static void GTeth_hash_init(struct GTeth_softc *sc)
1424{
1425  void *hash_mem;
1426
1427  if (GTeth_debug>0) printk("GTeth_hash_init(");
1428  /* MOTLoad defualt : 512 bytes of address filtering, which
1429   * requires 16KB of memory
1430   */
1431#if 1
1432  hash_mem = rtems_bsdnet_malloc(HASH_DRAM_SIZE + HASH_ALIGN, M_FREE, M_NOWAIT);
1433  sc->sc_hashtable  =(void *)(((long)hash_mem+ HASH_ALIGN) & ~HASH_ALIGN);
1434#else
1435  /* only for test */
1436  hash_mem = 0x68F000;
1437  sc->sc_hashtable  =(unsigned long long *)hash_mem;
1438#endif
1439  sc->sc_hashmask = HASH_DRAM_SIZE - 1;
1440
1441  memset((void *)sc->sc_hashtable, 0,HASH_DRAM_SIZE);
1442#ifdef GT_DEBUG
1443  printk("hashtable addr:%x, mask %x)\n", sc->sc_hashtable,sc->sc_hashmask);
1444#endif
1445}
1446
1447static void GT64260eth_error(struct GTeth_softc *sc)
1448{
1449  struct ifnet          *ifp = &sc->arpcom.ac_if;
1450  unsigned int          intr_status= sc->intr_errsts[sc->intr_err_ptr1];
1451
1452  /* read and reset the status; because this is written
1453   * by the ISR, we must disable interrupts here
1454   */
1455  if (intr_status) {
1456    printk("%s%d: ICR = 0x%x ",
1457           ifp->if_name, ifp->if_unit, intr_status);
1458#if 1
1459    if (intr_status & INTR_RX_ERROR) {
1460       printk("Rxq error, if_ierrors %d\n",
1461              ifp->if_ierrors);
1462    }
1463#endif
1464    /* Rx error is handled in GT64260eth_rx() */
1465    if (intr_status & INTR_TX_ERROR) {
1466       ifp->if_oerrors++;
1467       printk("Txq error,  if_oerrors %d\n",ifp->if_oerrors);
1468    }
1469  }
1470  else
1471    printk("%s%d: Ghost interrupt ?\n",ifp->if_name,
1472           ifp->if_unit);
1473  sc->intr_errsts[sc->intr_err_ptr1++]=0; 
1474  sc->intr_err_ptr1 %= INTR_ERR_SIZE;   /* Till Straumann */
1475}
1476
1477
1478/* The daemon does all of the work; RX, TX and cleaning up buffers/descriptors */
1479static void GT64260eth_daemon(void *arg)
1480{
1481  struct GTeth_softc *sc = (struct GTeth_softc*)arg;
1482  rtems_event_set       events;
1483  struct mbuf   *m=0;
1484  struct ifnet  *ifp=&sc->arpcom.ac_if;
1485
1486#if 0   
1487  /* see comments in GT64260eth_init(); in newer versions of
1488   * rtems, we hold the network semaphore at this point
1489   */
1490  rtems_semaphore_release(sc->daemonSync);
1491#endif
1492
1493  /* NOTE: our creator possibly holds the bsdnet_semaphore.
1494   *       since that has PRIORITY_INVERSION enabled, our
1495   *       subsequent call to bsdnet_event_receive() will
1496   *       _not_ release it. It's still in posession of our
1497   *       owner.
1498   *       This is different from how killing this task
1499   *       is handled.
1500   */
1501
1502  for (;;) {
1503     /* sleep until there's work to be done */
1504     /* Note: bsdnet_event_receive() acquires
1505      *       the global bsdnet semaphore for
1506      *       mutual exclusion.
1507      */
1508     rtems_bsdnet_event_receive(ALL_EVENTS,
1509                                RTEMS_WAIT | RTEMS_EVENT_ANY,
1510                                RTEMS_NO_TIMEOUT,
1511                                &events);
1512 
1513     if (KILL_EVENT & events) break;
1514
1515#ifndef GE_NORX
1516     if (events & RX_EVENT) GT64260eth_rx(sc);
1517#endif
1518#if 0
1519     printk("%x ", inb(ETH0_EPSR));
1520     if ( ((i++) % 15)==0) printk("\n");
1521#endif
1522
1523     /* clean up and try sending packets */
1524     do {
1525         if (sc->txq_nactive) GTeth_txq_done(sc);
1526
1527         while (sc->txq_free>0) {
1528           if (sc->txq_free>TXQ_HiLmt_OFF) {
1529              m=0;
1530              IF_DEQUEUE(&ifp->if_snd,m);
1531              if (m==0) break;
1532              GT64260eth_sendpacket(sc, m);
1533           }
1534           else {
1535              GTeth_txq_done(sc);
1536              break;
1537           }
1538         }
1539         /* we leave this loop
1540          *  - either because there's no free buffer
1541          *    (m=0 initializer && !sc->txq_free)
1542          *  - or there's nothing to send (IF_DEQUEUE
1543          *    returned 0
1544          */
1545       } while (m);
1546
1547       ifp->if_flags &= ~IFF_OACTIVE;
1548
1549       /* Log errors and other uncommon events. */
1550       if (events & ERR_EVENT) GT64260eth_error(sc);
1551  } /* end for(;;) { rtems_bsdnet_event_receive() .....*/
1552
1553  ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
1554
1555  /* shut down the hardware */
1556  GT64260eth_stop_hw(sc);
1557  /* flush the output queue */
1558  for (;;) {
1559      IF_DEQUEUE(&ifp->if_snd,m);
1560      if (!m) break;
1561      m_freem(m);
1562  }
1563  /* as of 'rtems_bsdnet_event_receive()' we own the
1564   * networking semaphore
1565   */
1566  rtems_bsdnet_semaphore_release();
1567  rtems_semaphore_release(sc->daemonSync);
1568
1569  /* Note that I dont use sc->daemonTid here -
1570   * theoretically, that variable could already
1571   * hold a newly created TID
1572   */
1573  rtems_task_delete(RTEMS_SELF);
1574}
1575
Note: See TracBrowser for help on using the repository browser.