]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/evb64260/eth_addrtbl.c
Initial revision
[karo-tx-uboot.git] / board / evb64260 / eth_addrtbl.c
1 #include <common.h>
2 #include <malloc.h>
3 #include <galileo/gt64260R.h>
4 #include <galileo/core.h>
5 #include <asm/cache.h>
6 #include "eth.h"
7 #include "eth_addrtbl.h"
8
9 #define TRUE 1
10 #define FALSE 0
11
12 #define PRINTF printf
13
14 #ifdef CONFIG_GT_USE_MAC_HASH_TABLE
15
16 static u32           addressTableHashMode[ GAL_ETH_DEVS ] = { 0, };
17 static u32           addressTableHashSize[ GAL_ETH_DEVS ] = { 0, };
18 static addrTblEntry *addressTableBase[     GAL_ETH_DEVS ] = { 0, };
19 static void         *realAddrTableBase[    GAL_ETH_DEVS ] = { 0, };
20
21 static const u32 hashLength[ 2 ] = {
22     (0x8000),             /* 8K * 4 entries */
23     (0x8000/16),          /* 512 * 4 entries */
24 };
25
26 /* Initialize the address table for a port, if needed */
27 unsigned int initAddressTable( u32 port, u32 hashMode, u32 hashSizeSelector)
28 {
29     unsigned int tableBase;
30
31     if( port < 0 || port >= GAL_ETH_DEVS ) {
32                 printf("%s: Invalid port number %d\n", __FUNCTION__, port );
33                 return 0;
34         }
35
36         if (hashMode > 1) {
37                 printf("%s: Invalid Hash Mode %d\n", __FUNCTION__, port );
38                 return 0;
39         }
40
41         if ( realAddrTableBase[port] &&
42                 ( addressTableHashSize[port] != hashSizeSelector )) {
43                 /* we have been here before,
44                  * but now we want a different sized table
45                  */
46                 free( realAddrTableBase[port] );
47                 realAddrTableBase[port] = 0;
48                 addressTableBase[port] = 0;
49
50         }
51
52         tableBase = (unsigned int)addressTableBase[port];
53         /* we get called for every probe, so only do this once */
54         if ( !tableBase ) {
55         int bytes = hashLength[hashSizeSelector] * sizeof(addrTblEntry);
56
57                 tableBase = (unsigned int)realAddrTableBase[port] = malloc(bytes+64);
58
59             if(!tableBase)
60                 {
61                         printf("%s: alloc memory failed \n", __FUNCTION__);
62                         return 0;
63                 }
64
65         /* align to octal byte */
66             if(tableBase&63) tableBase=(tableBase+63) & ~63;
67
68         addressTableHashMode[port] = hashMode;
69             addressTableHashSize[port] = hashSizeSelector;
70         addressTableBase[port] = (addrTblEntry *)tableBase;
71
72             memset((void *)tableBase,0,bytes);
73         }
74
75     return tableBase;
76 }
77
78 /*
79  * ----------------------------------------------------------------------------
80  * This function will calculate the hash function of the address.
81  * depends on the hash mode and hash size.
82  * Inputs
83  * macH             - the 2 most significant bytes of the MAC address.
84  * macL             - the 4 least significant bytes of the MAC address.
85  * hashMode         - hash mode 0 or hash mode 1.
86  * hashSizeSelector - indicates number of hash table entries (0=0x8000,1=0x800)
87  * Outputs
88  * return the calculated entry.
89  */
90 u32
91 hashTableFunction( u32 macH, u32 macL, u32 HashSize, u32 hash_mode)
92 {
93     u32 hashResult;
94     u32 addrH;
95     u32 addrL;
96     u32 addr0;
97     u32 addr1;
98     u32 addr2;
99     u32 addr3;
100     u32 addrHSwapped;
101     u32 addrLSwapped;
102
103
104     addrH = NIBBLE_SWAPPING_16_BIT( macH );
105     addrL = NIBBLE_SWAPPING_32_BIT( macL );
106
107     addrHSwapped =   FLIP_4_BITS(  addrH        & 0xf )
108                  + ((FLIP_4_BITS( (addrH >>  4) & 0xf)) <<  4)
109                  + ((FLIP_4_BITS( (addrH >>  8) & 0xf)) <<  8)
110                  + ((FLIP_4_BITS( (addrH >> 12) & 0xf)) << 12);
111
112     addrLSwapped =   FLIP_4_BITS(  addrL        & 0xf )
113                  + ((FLIP_4_BITS( (addrL >>  4) & 0xf)) <<  4)
114                  + ((FLIP_4_BITS( (addrL >>  8) & 0xf)) <<  8)
115                  + ((FLIP_4_BITS( (addrL >> 12) & 0xf)) << 12)
116                  + ((FLIP_4_BITS( (addrL >> 16) & 0xf)) << 16)
117                  + ((FLIP_4_BITS( (addrL >> 20) & 0xf)) << 20)
118                  + ((FLIP_4_BITS( (addrL >> 24) & 0xf)) << 24)
119                  + ((FLIP_4_BITS( (addrL >> 28) & 0xf)) << 28);
120
121     addrH = addrHSwapped;
122     addrL = addrLSwapped;
123
124     if( hash_mode == 0 )  {
125         addr0 =  (addrL >>  2) & 0x03f;
126         addr1 =  (addrL        & 0x003) | ((addrL >> 8) & 0x7f) << 2;
127         addr2 =  (addrL >> 15) & 0x1ff;
128         addr3 = ((addrL >> 24) & 0x0ff) | ((addrH &  1)         << 8);
129     } else  {
130         addr0 = FLIP_6_BITS(    addrL        & 0x03f );
131         addr1 = FLIP_9_BITS(  ((addrL >>  6) & 0x1ff));
132         addr2 = FLIP_9_BITS(   (addrL >> 15) & 0x1ff);
133         addr3 = FLIP_9_BITS( (((addrL >> 24) & 0x0ff) | ((addrH & 0x1) << 8)));
134     }
135
136     hashResult = (addr0 << 9) | (addr1 ^ addr2 ^ addr3);
137
138     if( HashSize == _8K_TABLE )  {
139         hashResult = hashResult & 0xffff;
140     } else  {
141         hashResult = hashResult & 0x07ff;
142     }
143
144     return( hashResult );
145 }
146
147
148 /*
149  * ----------------------------------------------------------------------------
150  * This function will add an entry to the address table.
151  * depends on the hash mode and hash size that was initialized.
152  * Inputs
153  * port - ETHERNET port number.
154  * macH - the 2 most significant bytes of the MAC address.
155  * macL - the 4 least significant bytes of the MAC address.
156  * skip - if 1, skip this address.
157  * rd   - the RD field in the address table.
158  * Outputs
159  * address table entry is added.
160  * TRUE if success.
161  * FALSE if table full
162  */
163 int
164 addAddressTableEntry(
165     u32           port,
166     u32           macH,
167     u32           macL,
168     u32           rd,
169     u32           skip         )
170 {
171     addrTblEntry *entry;
172     u32           newHi;
173     u32           newLo;
174     u32           i;
175
176     newLo = (((macH >>  4) & 0xf) << 15)
177           | (((macH >>  0) & 0xf) << 11)
178           | (((macH >> 12) & 0xf) <<  7)
179           | (((macH >>  8) & 0xf) <<  3)
180           | (((macL >> 20) & 0x1) << 31)
181           | (((macL >> 16) & 0xf) << 27)
182           | (((macL >> 28) & 0xf) << 23)
183           | (((macL >> 24) & 0xf) << 19)
184           |   (skip << SKIP_BIT)  |  (rd << 2) | VALID;
185
186     newHi = (((macL >>  4) & 0xf) << 15)
187           | (((macL >>  0) & 0xf) << 11)
188           | (((macL >> 12) & 0xf) <<  7)
189           | (((macL >>  8) & 0xf) <<  3)
190           | (((macL >> 21) & 0x7) <<  0);
191
192     /*
193      * Pick the appropriate table, start scanning for free/reusable
194      * entries at the index obtained by hashing the specified MAC address
195      */
196     entry  = addressTableBase[port];
197     entry += hashTableFunction( macH, macL, addressTableHashSize[port],
198                                             addressTableHashMode[port]  );
199     for( i = 0;  i < HOP_NUMBER;  i++, entry++ )  {
200         if( !(entry->lo & VALID)   /*|| (entry->lo & SKIP)*/   )  {
201             break;
202         } else  {                    /* if same address put in same position */
203             if(   ((entry->lo & 0xfffffff8) == (newLo & 0xfffffff8))
204                 && (entry->hi               ==  newHi) )
205             {
206                     break;
207             }
208         }
209     }
210
211     if( i == HOP_NUMBER )  {
212         PRINTF( "addGT64260addressTableEntry: table section is full\n" );
213         return( FALSE );
214     }
215
216     /*
217      * Update the selected entry
218      */
219     entry->hi = newHi;
220     entry->lo = newLo;
221     DCACHE_FLUSH_N_SYNC( (u32)entry, MAC_ENTRY_SIZE );
222     return( TRUE );
223 }
224
225 #endif /* CONFIG_GT_USE_MAC_HASH_TABLE */