summaryrefslogtreecommitdiff
path: root/arch/arm/cpu/ixp/npe/IxEthDBWiFi.c
blob: 0a6043f3640567c2cce37cc88b17ae966a12fb20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/**
 * @file IxEthDBAPI.c
 *
 * @brief Implementation of the public API
 * 
 * @par
 * IXP400 SW Release version 2.0
 * 
 * -- Copyright Notice --
 * 
 * @par
 * Copyright 2001-2005, Intel Corporation.
 * All rights reserved.
 * 
 * @par
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Intel Corporation nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * @par
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 * @par
 * -- End of Copyright Notice --
 */

#include "IxEthDB_p.h"

/* forward prototypes */
IX_ETH_DB_PUBLIC
MacTreeNode *ixEthDBGatewaySelect(MacTreeNode *stations);

/**
 * @brief sets the BBSID value for the WiFi header conversion feature
 *
 * @param portID ID of the port
 * @param bbsid pointer to the 6-byte BBSID value
 *
 * Note that this function is documented in the main component
 * header file, IxEthDB.h.
 *
 * @return IX_ETH_DB_SUCCESS if the operation completed successfully
 * or an appropriate error message otherwise
 */
IX_ETH_DB_PUBLIC
IxEthDBStatus ixEthDBWiFiBBSIDSet(IxEthDBPortId portID, IxEthDBMacAddr *bbsid)
{
    IxNpeMhMessage message;
    IX_STATUS result;
    
    IX_ETH_DB_CHECK_PORT(portID);
    
    IX_ETH_DB_CHECK_SINGLE_NPE(portID);
    
    IX_ETH_DB_CHECK_FEATURE(portID, IX_ETH_DB_WIFI_HEADER_CONVERSION);
 
    IX_ETH_DB_CHECK_REFERENCE(bbsid);
    
    memcpy(ixEthDBPortInfo[portID].bbsid, bbsid, sizeof (IxEthDBMacAddr));

    FILL_SETBBSID_MSG(message, portID, bbsid);

    IX_ETHDB_SEND_NPE_MSG(IX_ETH_DB_PORT_ID_TO_NPE(portID), message, result);
    
    return result;
}

/**
 * @brief updates the Frame Control and Duration/ID WiFi header
 * conversion parameters in an NPE
 *
 * @param portID ID of the port
 *
 * This function will send a message to the NPE updating the 
 * frame conversion parameters for 802.3 => 802.11 header conversion.
 *
 * @return IX_ETH_DB_SUCCESS if the operation completed successfully
 * or IX_ETH_DB_FAIL otherwise
 *
 * @internal
 */
IX_ETH_DB_PRIVATE
IxEthDBStatus ixEthDBWiFiFrameControlDurationIDUpdate(IxEthDBPortId portID)
{
    IxNpeMhMessage message;
    IX_STATUS result;

    FILL_SETFRAMECONTROLDURATIONID(message, portID, ixEthDBPortInfo[portID].frameControlDurationID);
    
    IX_ETHDB_SEND_NPE_MSG(IX_ETH_DB_PORT_ID_TO_NPE(portID), message, result);
    
    return result;
}

/**
 * @brief sets the Duration/ID WiFi frame header conversion parameter
 *
 * @param portID ID of the port
 * @param durationID 16-bit value containing the new Duration/ID parameter
 *
 * Note that this function is documented in the main component
 * header file, IxEthDB.h.
 *
 * @return IX_ETH_DB_SUCCESS if the operation completed successfully
 * or an appropriate error message otherwise
 */
IX_ETH_DB_PUBLIC
IxEthDBStatus ixEthDBWiFiDurationIDSet(IxEthDBPortId portID, UINT16 durationID)
{
    IX_ETH_DB_CHECK_PORT(portID);
    
    IX_ETH_DB_CHECK_SINGLE_NPE(portID);
    
    IX_ETH_DB_CHECK_FEATURE(portID, IX_ETH_DB_WIFI_HEADER_CONVERSION);

    ixEthDBPortInfo[portID].frameControlDurationID = (ixEthDBPortInfo[portID].frameControlDurationID & 0xFFFF0000) | durationID;
    
    return ixEthDBWiFiFrameControlDurationIDUpdate(portID);
}

/**
 * @brief sets the Frame Control WiFi frame header conversion parameter
 *
 * @param portID ID of the port
 * @param durationID 16-bit value containing the new Frame Control parameter
 *
 * Note that this function is documented in the main component
 * header file, IxEthDB.h.
 *
 * @return IX_ETH_DB_SUCCESS if the operation completed successfully
 * or an appropriate error message otherwise
 */
IX_ETH_DB_PUBLIC
IxEthDBStatus ixEthDBWiFiFrameControlSet(IxEthDBPortId portID, UINT16 frameControl)
{
    IX_ETH_DB_CHECK_PORT(portID);
    
    IX_ETH_DB_CHECK_SINGLE_NPE(portID);
    
    IX_ETH_DB_CHECK_FEATURE(portID, IX_ETH_DB_WIFI_HEADER_CONVERSION);

    ixEthDBPortInfo[portID].frameControlDurationID = (ixEthDBPortInfo[portID].frameControlDurationID & 0xFFFF) | (frameControl << 16); 
    
    return ixEthDBWiFiFrameControlDurationIDUpdate(portID);
}

/**
 * @brief removes a WiFi header conversion record
 *
 * @param portID ID of the port
 * @param macAddr MAC address of the record to remove
 *
 * Note that this function is documented in the main
 * component header file, IxEthDB.h.
 *
 * @return IX_ETH_DB_SUCCESS if the operation completed
 * successfully or an appropriate error message otherwise
 */
IX_ETH_DB_PUBLIC
IxEthDBStatus ixEthDBWiFiEntryRemove(IxEthDBPortId portID, IxEthDBMacAddr *macAddr)
{
    MacDescriptor recordTemplate;
    
    IX_ETH_DB_CHECK_PORT(portID);
    
    IX_ETH_DB_CHECK_SINGLE_NPE(portID);
    
    IX_ETH_DB_CHECK_REFERENCE(macAddr);
    
    IX_ETH_DB_CHECK_FEATURE(portID, IX_ETH_DB_WIFI_HEADER_CONVERSION);
    
    memcpy(recordTemplate.macAddress, macAddr, sizeof (IxEthDBMacAddr));
    
    recordTemplate.type   = IX_ETH_DB_WIFI_RECORD;
    recordTemplate.portID = portID;
    
    return ixEthDBRemove(&recordTemplate, NULL);
}

/**
 * @brief adds a WiFi header conversion record
 *
 * @param portID ID of the port
 * @param macAddr MAC address of the record to add
 * @param gatewayMacAddr address of the gateway (or
 * NULL if this is a station record)
 *
 * This function adds a record of type AP_TO_AP (gateway is not NULL)
 * or AP_TO_STA (gateway is NULL) in the main database as a 
 * WiFi header conversion record.
 *
 * @return IX_ETH_DB_SUCCESS if the operation completed
 * successfully or an appropriate error message otherwise
 *
 * @internal
 */
IX_ETH_DB_PRIVATE
IxEthDBStatus ixEthDBWiFiEntryAdd(IxEthDBPortId portID, IxEthDBMacAddr *macAddr, IxEthDBMacAddr *gatewayMacAddr)
{
    MacDescriptor recordTemplate;

    IX_ETH_DB_CHECK_PORT(portID);

    IX_ETH_DB_CHECK_SINGLE_NPE(portID);

    IX_ETH_DB_CHECK_REFERENCE(macAddr);
    
    IX_ETH_DB_CHECK_FEATURE(portID, IX_ETH_DB_WIFI_HEADER_CONVERSION);

    memcpy(recordTemplate.macAddress, macAddr, sizeof (IxEthDBMacAddr));
    
    recordTemplate.type   = IX_ETH_DB_WIFI_RECORD;
    recordTemplate.portID = portID;
    
    if (gatewayMacAddr != NULL)
    {
        memcpy(recordTemplate.recordData.wifiData.gwMacAddress, gatewayMacAddr, sizeof (IxEthDBMacAddr));
        
        recordTemplate.recordData.wifiData.type = IX_ETH_DB_WIFI_AP_TO_AP;
    }
    else
    {
        memset(recordTemplate.recordData.wifiData.gwMacAddress, 0, sizeof (IxEthDBMacAddr));

        recordTemplate.recordData.wifiData.type = IX_ETH_DB_WIFI_AP_TO_STA;
    }
    
    return ixEthDBAdd(&recordTemplate, NULL);
}

/**
 * @brief adds a WiFi header conversion record
 *
 * @param portID ID of the port
 * @param macAddr MAC address of the record to add
 * @param gatewayMacAddr address of the gateway 
 *
 * This function adds a record of type AP_TO_AP
 * in the main database as a WiFi header conversion record.
 *
 * This is simply a wrapper over @ref ixEthDBWiFiEntryAdd().
 *
 * Note that this function is documented in the main
 * component header file, IxEthDB.h.
 *
 * @return IX_ETH_DB_SUCCESS if the operation completed
 * successfully or an appropriate error message otherwise
 */
IX_ETH_DB_PUBLIC
IxEthDBStatus ixEthDBWiFiAccessPointEntryAdd(IxEthDBPortId portID, IxEthDBMacAddr *macAddr, IxEthDBMacAddr *gatewayMacAddr)
{
    IX_ETH_DB_CHECK_REFERENCE(gatewayMacAddr);

    return ixEthDBWiFiEntryAdd(portID, macAddr, gatewayMacAddr);
}

/**
 * @brief adds a WiFi header conversion record
 *
 * @param portID ID of the port
 * @param macAddr MAC address of the record to add
 *
 * This function adds a record of type AP_TO_STA
 * in the main database as a WiFi header conversion record.
 *
 * This is simply a wrapper over @ref ixEthDBWiFiEntryAdd().
 *
 * Note that this function is documented in the main
 * component header file, IxEthDB.h.
 *
 * @return IX_ETH_DB_SUCCESS if the operation completed
 * successfully or an appropriate error message otherwise
 */
IX_ETH_DB_PUBLIC
IxEthDBStatus ixEthDBWiFiStationEntryAdd(IxEthDBPortId portID, IxEthDBMacAddr *macAddr)
{
    return ixEthDBWiFiEntryAdd(portID, macAddr, NULL);
}

/**
 * @brief selects a set of gateways from a tree of 
 * WiFi header conversion records
 *
 * @param stations binary tree containing pointers to WiFi header
 * conversion records
 *
 * This function browses through the input binary tree, identifies
 * records of type AP_TO_AP, clones these records and appends them
 * to a vine (a single right-branch binary tree) which is returned
 * as result. A maximum of MAX_GW_SIZE entries containing gateways
 * will be cloned from the original tree.
 *
 * @return vine (linear binary tree) containing record
 * clones of AP_TO_AP type, which have a gateway field
 *
 * @internal
 */
IX_ETH_DB_PUBLIC
MacTreeNode *ixEthDBGatewaySelect(MacTreeNode *stations)
{
    MacTreeNodeStack *stack;
    MacTreeNode *gateways, *insertionPlace;
    UINT32 gwIndex = 1; /* skip the empty root */
    
    if (stations == NULL)
    {
        return NULL;
    }

    stack = ixOsalCacheDmaMalloc(sizeof (MacTreeNodeStack));

    if (stack == NULL)
    {
        ERROR_LOG("DB: (WiFi) failed to allocate the node stack for gateway tree linearization, out of memory?\n");
        return NULL;
    }
    
    /* initialize root node */
    gateways = insertionPlace = NULL;
        
    /* start browsing the station tree */
    NODE_STACK_INIT(stack);
    
    /* initialize stack by pushing the tree root at offset 0 */
    NODE_STACK_PUSH(stack, stations, 0);
    
    while (NODE_STACK_NONEMPTY(stack))
    {
        MacTreeNode *node;
        UINT32 offset;
       
        NODE_STACK_POP(stack, node, offset);

        /* we can store maximum 31 (32 total, 1 empty root) entries in the gateway tree */
        if (offset > (MAX_GW_SIZE - 1)) break;
        
        /* check if this record has a gateway address */
        if (node->descriptor != NULL && node->descriptor->recordData.wifiData.type == IX_ETH_DB_WIFI_AP_TO_AP)
        {
            /* found a record, create an insertion place */
            if (insertionPlace != NULL)
            {
                insertionPlace->right = ixEthDBAllocMacTreeNode();
                insertionPlace        = insertionPlace->right;
            }
            else
            {
                gateways       = ixEthDBAllocMacTreeNode();
                insertionPlace = gateways;
            }

            if (insertionPlace == NULL)
            {
                /* no nodes left, bail out with what we have */
                ixOsalCacheDmaFree(stack);
                return gateways;
            }
            
            /* clone the original record for the gateway tree */
            insertionPlace->descriptor = ixEthDBCloneMacDescriptor(node->descriptor);
            
            /* insert and update the offset in the original record */
            node->descriptor->recordData.wifiData.gwAddressIndex = gwIndex++;
        }
        
        /* browse the tree */
        if (node->left != NULL)
        {
            NODE_STACK_PUSH(stack, node->left, LEFT_CHILD_OFFSET(offset));
        }

        if (node->right != NULL)
        {
            NODE_STACK_PUSH(stack, node->right, RIGHT_CHILD_OFFSET(offset));
        }
    }
    
    ixOsalCacheDmaFree(stack);
    return gateways;    
}

/**
 * @brief downloads the WiFi header conversion table to an NPE
 *
 * @param portID ID of the port
 *
 * This function prepares the WiFi header conversion tables and
 * downloads them to the specified NPE port.
 *
 * The header conversion tables consist in the main table of
 * addresses and the secondary table of gateways. AP_TO_AP records
 * from the first table contain index fields into the second table
 * for gateway selection.
 *
 * Note that this function is documented in the main component
 * header file, IxEthDB.h.
 *
 * @return IX_ETH_DB_SUCCESS if the operation completed successfully
 * or an appropriate error message otherwise
 */
IX_ETH_DB_PUBLIC
IxEthDBStatus ixEthDBWiFiConversionTableDownload(IxEthDBPortId portID)
{
    IxEthDBPortMap query;
    MacTreeNode *stations = NULL, *gateways = NULL, *gateway = NULL;
    IxNpeMhMessage message;
    PortInfo *portInfo;
    IX_STATUS result;

    IX_ETH_DB_CHECK_PORT(portID);

    IX_ETH_DB_CHECK_SINGLE_NPE(portID);
    
    IX_ETH_DB_CHECK_FEATURE(portID, IX_ETH_DB_WIFI_HEADER_CONVERSION);

    portInfo = &ixEthDBPortInfo[portID];
   
    SET_DEPENDENCY_MAP(query, portID);
    
    ixEthDBUpdateLock();

    stations = ixEthDBQuery(NULL, query, IX_ETH_DB_WIFI_RECORD, MAX_ELT_SIZE);
    gateways = ixEthDBGatewaySelect(stations);
    
    /* clean up gw area */
    memset((void *) portInfo->updateMethod.npeGwUpdateZone, FULL_GW_BYTE_SIZE, 0);

    /* write all gateways */
    gateway = gateways;

    while (gateway != NULL)
    {
        ixEthDBNPEGatewayNodeWrite((void *) (((UINT32) portInfo->updateMethod.npeGwUpdateZone) 
            + gateway->descriptor->recordData.wifiData.gwAddressIndex * ELT_ENTRY_SIZE), 
            gateway);

        gateway = gateway->right;
    }

    /* free the gateway tree */
    if (gateways != NULL)
    {
        ixEthDBFreeMacTreeNode(gateways);
    }

    FILL_SETAPMACTABLE_MSG(message, 
        IX_OSAL_MMU_VIRT_TO_PHYS(portInfo->updateMethod.npeGwUpdateZone));

    IX_ETHDB_SEND_NPE_MSG(IX_ETH_DB_PORT_ID_TO_NPE(portID), message, result);

    if (result == IX_SUCCESS)
    {
        /* update the main tree (the stations tree) */
        portInfo->updateMethod.searchTree = stations;
        
        result = ixEthDBNPEUpdateHandler(portID, IX_ETH_DB_WIFI_RECORD);
    }

    ixEthDBUpdateUnlock();

    return result;
}