source: rtems/c/src/lib/libbsp/powerpc/mvme5500/network/GT64260eth.c @ 4953659b

4.104.114.84.95
Last change on this file since 4953659b was 4953659b, checked in by Jennifer Averett <Jennifer.Averett@…>, on 04/15/05 at 18:10:29

2005-04-15 Jennifer Averett <jennifer.averett@…>

PR 779/bsp

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