* Some code cleanup
[platform/kernel/u-boot.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 =
56                         hashLength[hashSizeSelector] * sizeof (addrTblEntry);
57
58                 tableBase = (unsigned int) realAddrTableBase[port] =
59                         malloc (bytes + 64);
60
61                 if (!tableBase) {
62                         printf ("%s: alloc memory failed \n", __FUNCTION__);
63                         return 0;
64                 }
65
66                 /* align to octal byte */
67                 if (tableBase & 63)
68                         tableBase = (tableBase + 63) & ~63;
69
70                 addressTableHashMode[port] = hashMode;
71                 addressTableHashSize[port] = hashSizeSelector;
72                 addressTableBase[port] = (addrTblEntry *) tableBase;
73
74                 memset ((void *) tableBase, 0, bytes);
75         }
76
77         return tableBase;
78 }
79
80 /*
81  * ----------------------------------------------------------------------------
82  * This function will calculate the hash function of the address.
83  * depends on the hash mode and hash size.
84  * Inputs
85  * macH             - the 2 most significant bytes of the MAC address.
86  * macL             - the 4 least significant bytes of the MAC address.
87  * hashMode         - hash mode 0 or hash mode 1.
88  * hashSizeSelector - indicates number of hash table entries (0=0x8000,1=0x800)
89  * Outputs
90  * return the calculated entry.
91  */
92 u32 hashTableFunction (u32 macH, u32 macL, u32 HashSize, u32 hash_mode)
93 {
94         u32 hashResult;
95         u32 addrH;
96         u32 addrL;
97         u32 addr0;
98         u32 addr1;
99         u32 addr2;
100         u32 addr3;
101         u32 addrHSwapped;
102         u32 addrLSwapped;
103
104
105         addrH = NIBBLE_SWAPPING_16_BIT (macH);
106         addrL = NIBBLE_SWAPPING_32_BIT (macL);
107
108         addrHSwapped = FLIP_4_BITS (addrH & 0xf)
109                 + ((FLIP_4_BITS ((addrH >> 4) & 0xf)) << 4)
110                 + ((FLIP_4_BITS ((addrH >> 8) & 0xf)) << 8)
111                 + ((FLIP_4_BITS ((addrH >> 12) & 0xf)) << 12);
112
113         addrLSwapped = FLIP_4_BITS (addrL & 0xf)
114                 + ((FLIP_4_BITS ((addrL >> 4) & 0xf)) << 4)
115                 + ((FLIP_4_BITS ((addrL >> 8) & 0xf)) << 8)
116                 + ((FLIP_4_BITS ((addrL >> 12) & 0xf)) << 12)
117                 + ((FLIP_4_BITS ((addrL >> 16) & 0xf)) << 16)
118                 + ((FLIP_4_BITS ((addrL >> 20) & 0xf)) << 20)
119                 + ((FLIP_4_BITS ((addrL >> 24) & 0xf)) << 24)
120                 + ((FLIP_4_BITS ((addrL >> 28) & 0xf)) << 28);
121
122         addrH = addrHSwapped;
123         addrL = addrLSwapped;
124
125         if (hash_mode == 0) {
126                 addr0 = (addrL >> 2) & 0x03f;
127                 addr1 = (addrL & 0x003) | ((addrL >> 8) & 0x7f) << 2;
128                 addr2 = (addrL >> 15) & 0x1ff;
129                 addr3 = ((addrL >> 24) & 0x0ff) | ((addrH & 1) << 8);
130         } else {
131                 addr0 = FLIP_6_BITS (addrL & 0x03f);
132                 addr1 = FLIP_9_BITS (((addrL >> 6) & 0x1ff));
133                 addr2 = FLIP_9_BITS ((addrL >> 15) & 0x1ff);
134                 addr3 = FLIP_9_BITS ((((addrL >> 24) & 0x0ff) |
135                                       ((addrH & 0x1) << 8)));
136         }
137
138         hashResult = (addr0 << 9) | (addr1 ^ addr2 ^ addr3);
139
140         if (HashSize == _8K_TABLE) {
141                 hashResult = hashResult & 0xffff;
142         } else {
143                 hashResult = hashResult & 0x07ff;
144         }
145
146         return (hashResult);
147 }
148
149
150 /*
151  * ----------------------------------------------------------------------------
152  * This function will add an entry to the address table.
153  * depends on the hash mode and hash size that was initialized.
154  * Inputs
155  * port - ETHERNET port number.
156  * macH - the 2 most significant bytes of the MAC address.
157  * macL - the 4 least significant bytes of the MAC address.
158  * skip - if 1, skip this address.
159  * rd   - the RD field in the address table.
160  * Outputs
161  * address table entry is added.
162  * TRUE if success.
163  * FALSE if table full
164  */
165 int addAddressTableEntry (u32 port, u32 macH, u32 macL, u32 rd, u32 skip)
166 {
167         addrTblEntry *entry;
168         u32 newHi;
169         u32 newLo;
170         u32 i;
171
172         newLo = (((macH >> 4) & 0xf) << 15)
173                 | (((macH >> 0) & 0xf) << 11)
174                 | (((macH >> 12) & 0xf) << 7)
175                 | (((macH >> 8) & 0xf) << 3)
176                 | (((macL >> 20) & 0x1) << 31)
177                 | (((macL >> 16) & 0xf) << 27)
178                 | (((macL >> 28) & 0xf) << 23)
179                 | (((macL >> 24) & 0xf) << 19)
180                 | (skip << SKIP_BIT) | (rd << 2) | VALID;
181
182         newHi = (((macL >> 4) & 0xf) << 15)
183                 | (((macL >> 0) & 0xf) << 11)
184                 | (((macL >> 12) & 0xf) << 7)
185                 | (((macL >> 8) & 0xf) << 3)
186                 | (((macL >> 21) & 0x7) << 0);
187
188         /*
189          * Pick the appropriate table, start scanning for free/reusable
190          * entries at the index obtained by hashing the specified MAC address
191          */
192         entry = addressTableBase[port];
193         entry += hashTableFunction (macH, macL, addressTableHashSize[port],
194                                     addressTableHashMode[port]);
195         for (i = 0; i < HOP_NUMBER; i++, entry++) {
196                 if (!(entry->lo & VALID) /*|| (entry->lo & SKIP) */ ) {
197                         break;
198                 } else {        /* if same address put in same position */
199                         if (((entry->lo & 0xfffffff8) == (newLo & 0xfffffff8))
200                             && (entry->hi == newHi)) {
201                                 break;
202                         }
203                 }
204         }
205
206         if (i == HOP_NUMBER) {
207                 PRINTF ("addGT64260addressTableEntry: table section is full\n");
208                 return (FALSE);
209         }
210
211         /*
212          * Update the selected entry
213          */
214         entry->hi = newHi;
215         entry->lo = newLo;
216         DCACHE_FLUSH_N_SYNC ((u32) entry, MAC_ENTRY_SIZE);
217         return (TRUE);
218 }
219
220 #endif /* CONFIG_GT_USE_MAC_HASH_TABLE */