blob: 0abc7d453693b21067a38a0286107a5da5e38e4b [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001#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
wdenkbf9e3b32004-02-12 00:47:09 +000016static u32 addressTableHashMode[GAL_ETH_DEVS] = { 0, };
17static u32 addressTableHashSize[GAL_ETH_DEVS] = { 0, };
18static addrTblEntry *addressTableBase[GAL_ETH_DEVS] = { 0, };
19static void *realAddrTableBase[GAL_ETH_DEVS] = { 0, };
wdenkc6097192002-11-03 00:24:07 +000020
wdenkbf9e3b32004-02-12 00:47:09 +000021static const u32 hashLength[2] = {
22 (0x8000), /* 8K * 4 entries */
23 (0x8000 / 16), /* 512 * 4 entries */
wdenkc6097192002-11-03 00:24:07 +000024};
25
26/* Initialize the address table for a port, if needed */
wdenkbf9e3b32004-02-12 00:47:09 +000027unsigned int initAddressTable (u32 port, u32 hashMode, u32 hashSizeSelector)
wdenkc6097192002-11-03 00:24:07 +000028{
wdenkbf9e3b32004-02-12 00:47:09 +000029 unsigned int tableBase;
wdenkc6097192002-11-03 00:24:07 +000030
wdenkbf9e3b32004-02-12 00:47:09 +000031 if (port < 0 || port >= GAL_ETH_DEVS) {
32 printf ("%s: Invalid port number %d\n", __FUNCTION__, port);
wdenkc6097192002-11-03 00:24:07 +000033 return 0;
34 }
35
36 if (hashMode > 1) {
wdenkbf9e3b32004-02-12 00:47:09 +000037 printf ("%s: Invalid Hash Mode %d\n", __FUNCTION__, port);
wdenkc6097192002-11-03 00:24:07 +000038 return 0;
39 }
40
wdenkbf9e3b32004-02-12 00:47:09 +000041 if (realAddrTableBase[port] &&
42 (addressTableHashSize[port] != hashSizeSelector)) {
wdenkc6097192002-11-03 00:24:07 +000043 /* we have been here before,
44 * but now we want a different sized table
45 */
wdenkbf9e3b32004-02-12 00:47:09 +000046 free (realAddrTableBase[port]);
wdenkc6097192002-11-03 00:24:07 +000047 realAddrTableBase[port] = 0;
48 addressTableBase[port] = 0;
49
50 }
51
wdenkbf9e3b32004-02-12 00:47:09 +000052 tableBase = (unsigned int) addressTableBase[port];
wdenkc6097192002-11-03 00:24:07 +000053 /* we get called for every probe, so only do this once */
wdenkbf9e3b32004-02-12 00:47:09 +000054 if (!tableBase) {
55 int bytes =
56 hashLength[hashSizeSelector] * sizeof (addrTblEntry);
wdenkc6097192002-11-03 00:24:07 +000057
wdenkbf9e3b32004-02-12 00:47:09 +000058 tableBase = (unsigned int) realAddrTableBase[port] =
59 malloc (bytes + 64);
wdenkc6097192002-11-03 00:24:07 +000060
wdenkbf9e3b32004-02-12 00:47:09 +000061 if (!tableBase) {
62 printf ("%s: alloc memory failed \n", __FUNCTION__);
wdenkc6097192002-11-03 00:24:07 +000063 return 0;
64 }
65
wdenkbf9e3b32004-02-12 00:47:09 +000066 /* align to octal byte */
67 if (tableBase & 63)
68 tableBase = (tableBase + 63) & ~63;
wdenkc6097192002-11-03 00:24:07 +000069
wdenkbf9e3b32004-02-12 00:47:09 +000070 addressTableHashMode[port] = hashMode;
71 addressTableHashSize[port] = hashSizeSelector;
72 addressTableBase[port] = (addrTblEntry *) tableBase;
wdenkc6097192002-11-03 00:24:07 +000073
wdenkbf9e3b32004-02-12 00:47:09 +000074 memset ((void *) tableBase, 0, bytes);
wdenkc6097192002-11-03 00:24:07 +000075 }
76
wdenkbf9e3b32004-02-12 00:47:09 +000077 return tableBase;
wdenkc6097192002-11-03 00:24:07 +000078}
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 */
wdenkbf9e3b32004-02-12 00:47:09 +000092u32 hashTableFunction (u32 macH, u32 macL, u32 HashSize, u32 hash_mode)
wdenkc6097192002-11-03 00:24:07 +000093{
wdenkbf9e3b32004-02-12 00:47:09 +000094 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;
wdenkc6097192002-11-03 00:24:07 +0000103
104
wdenkbf9e3b32004-02-12 00:47:09 +0000105 addrH = NIBBLE_SWAPPING_16_BIT (macH);
106 addrL = NIBBLE_SWAPPING_32_BIT (macL);
wdenkc6097192002-11-03 00:24:07 +0000107
wdenkbf9e3b32004-02-12 00:47:09 +0000108 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);
wdenkc6097192002-11-03 00:24:07 +0000112
wdenkbf9e3b32004-02-12 00:47:09 +0000113 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);
wdenkc6097192002-11-03 00:24:07 +0000121
wdenkbf9e3b32004-02-12 00:47:09 +0000122 addrH = addrHSwapped;
123 addrL = addrLSwapped;
wdenkc6097192002-11-03 00:24:07 +0000124
wdenkbf9e3b32004-02-12 00:47:09 +0000125 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 }
wdenkc6097192002-11-03 00:24:07 +0000137
wdenkbf9e3b32004-02-12 00:47:09 +0000138 hashResult = (addr0 << 9) | (addr1 ^ addr2 ^ addr3);
wdenkc6097192002-11-03 00:24:07 +0000139
wdenkbf9e3b32004-02-12 00:47:09 +0000140 if (HashSize == _8K_TABLE) {
141 hashResult = hashResult & 0xffff;
142 } else {
143 hashResult = hashResult & 0x07ff;
144 }
wdenkc6097192002-11-03 00:24:07 +0000145
wdenkbf9e3b32004-02-12 00:47:09 +0000146 return (hashResult);
wdenkc6097192002-11-03 00:24:07 +0000147}
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 */
wdenkbf9e3b32004-02-12 00:47:09 +0000165int addAddressTableEntry (u32 port, u32 macH, u32 macL, u32 rd, u32 skip)
wdenkc6097192002-11-03 00:24:07 +0000166{
wdenkbf9e3b32004-02-12 00:47:09 +0000167 addrTblEntry *entry;
168 u32 newHi;
169 u32 newLo;
170 u32 i;
wdenkc6097192002-11-03 00:24:07 +0000171
wdenkbf9e3b32004-02-12 00:47:09 +0000172 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;
wdenkc6097192002-11-03 00:24:07 +0000181
wdenkbf9e3b32004-02-12 00:47:09 +0000182 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);
wdenkc6097192002-11-03 00:24:07 +0000187
wdenkbf9e3b32004-02-12 00:47:09 +0000188 /*
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 }
wdenk8bde7f72003-06-27 21:31:46 +0000204 }
wdenkc6097192002-11-03 00:24:07 +0000205
wdenkbf9e3b32004-02-12 00:47:09 +0000206 if (i == HOP_NUMBER) {
207 PRINTF ("addGT64260addressTableEntry: table section is full\n");
208 return (FALSE);
209 }
wdenkc6097192002-11-03 00:24:07 +0000210
wdenkbf9e3b32004-02-12 00:47:09 +0000211 /*
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);
wdenkc6097192002-11-03 00:24:07 +0000218}
219
220#endif /* CONFIG_GT_USE_MAC_HASH_TABLE */