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

5
Last change on this file since 787f51f was 787f51f, checked in by Sebastian Huber <sebastian.huber@…>, on 06/06/17 at 09:08:16

Do not include <sys/ioctl.h> in kernel-space

Update #2833.

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