source: rtems/c/src/lib/libbsp/powerpc/mvme5500/network/GT64260eth.c @ 46a6fa91

4.104.114.84.95
Last change on this file since 46a6fa91 was 46a6fa91, checked in by Joel Sherrill <joel.sherrill@…>, on 04/15/05 at 20:13:18

2005-04-15 Joel Sherrill <joel@…>

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