[6319] | 1 | /** @file wlan_mac_event_log.c |
---|
| 2 | * @brief MAC Event Log Framework |
---|
| 3 | * |
---|
| 4 | * Contains code for logging MAC events in DRAM. |
---|
| 5 | * |
---|
| 6 | * @copyright Copyright 2013-2019, Mango Communications. All rights reserved. |
---|
| 7 | * Distributed under the Mango Communications Reference Design License |
---|
| 8 | * See LICENSE.txt included in the design archive or |
---|
| 9 | * at http://mangocomm.com/802.11/license |
---|
| 10 | * |
---|
| 11 | * @note |
---|
| 12 | * The event log implements a circular buffer that will record various |
---|
| 13 | * event entries that occur within a WLAN node. If the buffer is full, then |
---|
| 14 | * entries will be dropped with only a single warning printed to the screen. |
---|
| 15 | * |
---|
| 16 | * There are configuration options to enable / disable wrapping (ie if |
---|
| 17 | * wrapping is enabled, then the buffer is never "full" and the oldest |
---|
| 18 | * events will be overwritten when there is no more free space). Wrapping |
---|
| 19 | * is disabled by default. |
---|
| 20 | * |
---|
| 21 | * Internally, the event log is just an array of bytes which can be externally |
---|
| 22 | * viewed as indexed from 0 to log_size (address translation is done internally). |
---|
| 23 | * When a new entry is requested, the size of the entry is allocated from the |
---|
| 24 | * buffer and a pointer to the allocated entry is provided so that the caller |
---|
| 25 | * can fill in the event information. By default, the event log will set up all |
---|
| 26 | * header information (defined in wlan_mac_event_log.h) and that information |
---|
| 27 | * will not be exposed to user code. |
---|
| 28 | * |
---|
| 29 | * The event log will always provide a contiguous piece of memory for events. |
---|
| 30 | * Therefore, some space could be wasted at the wrap boundary since a single event |
---|
| 31 | * will never wrap. |
---|
| 32 | * |
---|
| 33 | * Also, if an entry cannot be allocated due to it overflowing the array, then |
---|
| 34 | * the event log will check to see if wrapping is enabled. If wrapping is disabled, |
---|
| 35 | * the event log will set the full flag and not allow any more events to be |
---|
| 36 | * allocated. Otherwise, the event log will wrap and begin to overwrite the |
---|
| 37 | * oldest entries. |
---|
| 38 | * |
---|
| 39 | * Finally, the log does not keep track of event entries and it is up to |
---|
| 40 | * calling functions to interpret the bytes within the log correctly. |
---|
| 41 | * |
---|
| 42 | * |
---|
| 43 | * This file is part of the Mango 802.11 Reference Design (https://mangocomm.com/802.11) |
---|
| 44 | */ |
---|
| 45 | |
---|
| 46 | /***************************** Include Files *********************************/ |
---|
| 47 | #include "wlan_mac_high_sw_config.h" |
---|
| 48 | |
---|
| 49 | // SDK includes |
---|
| 50 | #include "stdio.h" |
---|
| 51 | #include "stdlib.h" |
---|
| 52 | #include "string.h" |
---|
| 53 | #include "xstatus.h" |
---|
| 54 | #include "xil_types.h" |
---|
| 55 | |
---|
| 56 | // WLAN includes |
---|
| 57 | #include "wlan_mac_event_log.h" |
---|
| 58 | #include "wlan_mac_entries.h" |
---|
| 59 | #include "wlan_mac_high.h" |
---|
| 60 | #include "wlan_platform_common.h" |
---|
| 61 | |
---|
| 62 | // WLAN Exp includes |
---|
| 63 | #include "wlan_exp_node.h" |
---|
| 64 | #include "wlan_exp_ip_udp.h" |
---|
| 65 | |
---|
| 66 | #if WLAN_SW_CONFIG_ENABLE_LOGGING |
---|
| 67 | |
---|
| 68 | /*************************** Constant Definitions ****************************/ |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | /*********************** Global Variable Definitions *************************/ |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | /*************************** Variable Definitions ****************************/ |
---|
| 77 | |
---|
| 78 | // Log definition variables |
---|
| 79 | static volatile u32 log_start_address; // Absolute start address of the log |
---|
| 80 | static volatile u32 log_soft_end_address; // Soft end address of the log |
---|
| 81 | static volatile u32 log_max_address; // Absolute end address of the log |
---|
| 82 | static volatile u32 log_size; // Size of the log in bytes |
---|
| 83 | |
---|
| 84 | // Log index variables |
---|
| 85 | static volatile u32 log_oldest_address; // Pointer to the oldest entry |
---|
| 86 | static volatile u32 log_next_address; // Pointer to the next entry |
---|
| 87 | static volatile u32 log_num_wraps; // Number of times the log has wrapped |
---|
| 88 | |
---|
| 89 | // Log config variables |
---|
| 90 | static volatile u8 log_wrap_enabled; // Will the log wrap or stop; By default wrapping is DISABLED |
---|
| 91 | static volatile u8 event_logging_enabled; // Will events be logged or not; By default logging is ENABLED |
---|
| 92 | static volatile u16 wrap_buffer; // Number of additional bytes that will be "erased" when |
---|
| 93 | // the log increments the oldest address (default = EVENT_LOG_WRAP_BUFFER) |
---|
| 94 | |
---|
| 95 | // Log status variables |
---|
| 96 | static volatile u8 log_empty; // log_empty = (log_oldest_address == log_next_address); |
---|
| 97 | static volatile u8 log_full; // log_full = (log_tail_address == log_next_address); |
---|
| 98 | static volatile u16 log_count; // Monotonic counter for log entry sequence number |
---|
| 99 | // (wraps every (2^16 - 1) entries) |
---|
| 100 | |
---|
| 101 | // Mutex for critical allocation loop |
---|
| 102 | static volatile u8 allocation_mutex; |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | /*************************** Functions Prototypes ****************************/ |
---|
| 106 | |
---|
| 107 | // Internal functions; Should not be called externally |
---|
| 108 | // |
---|
| 109 | void event_log_move_oldest_address(u32 end_address); |
---|
| 110 | void event_log_increment_oldest_address(u64 end_address, u32 size); |
---|
| 111 | int event_log_get_next_empty_address(u32 size, u32* address); |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | /******************************** Functions **********************************/ |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | /*****************************************************************************/ |
---|
| 120 | /** |
---|
| 121 | * Initialize the event log |
---|
| 122 | * |
---|
| 123 | * @param start_address - Starting address of the event log |
---|
| 124 | * @param size - Size in bytes of the event log |
---|
| 125 | * |
---|
| 126 | * @return None |
---|
| 127 | * |
---|
| 128 | * @note The event log will only use a integer number of event entries so |
---|
| 129 | * any bytes over an integer number will be unused. |
---|
| 130 | * |
---|
| 131 | *****************************************************************************/ |
---|
| 132 | void event_log_init( char* start_address, u32 size ) { |
---|
| 133 | |
---|
| 134 | u32 alignment; |
---|
| 135 | u32 disable_log = 0; |
---|
| 136 | |
---|
| 137 | wlan_exp_log_set_mac_payload_len(MIN_MAC_PAYLOAD_LOG_LEN); |
---|
| 138 | //wlan_exp_log_set_mac_payload_len(MAX_MAC_PAYLOAD_LOG_LEN); //Log full payloads |
---|
| 139 | |
---|
| 140 | // Make sure that the start_address is 64-bit aligned |
---|
| 141 | alignment = ((u32)start_address) % 8; |
---|
| 142 | |
---|
| 143 | if (alignment != 0) { |
---|
| 144 | start_address = (char *)(((u32)start_address) + (8 - alignment)); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | // The log needs to be at least 4kB long otherwise there is not enough space |
---|
| 148 | // to put entries (~2x the largest entry) |
---|
| 149 | if (size < 4096) { |
---|
| 150 | xil_printf("WARNING: Event log (%d bytes) at 0x%x too small!!!\n", size, start_address); |
---|
| 151 | xil_printf(" Disabled event log.\n"); |
---|
| 152 | disable_log = 1; |
---|
| 153 | } else { |
---|
| 154 | xil_printf("Initializing Event log (%d bytes) at 0x%x \n", size, start_address); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | // Set default state of the logging |
---|
| 158 | if (EVENT_LOG_DEFAULT_LOGGING) { |
---|
| 159 | event_log_config_logging(EVENT_LOG_LOGGING_ENABLE); |
---|
| 160 | } else { |
---|
| 161 | event_log_config_logging(EVENT_LOG_LOGGING_DISABLE); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | // Set the global variables that describe the log |
---|
| 165 | log_size = size; |
---|
| 166 | log_start_address = (u32) start_address; |
---|
| 167 | log_max_address = log_start_address + log_size - 1; |
---|
| 168 | |
---|
| 169 | // Set wrapping to be enabled |
---|
| 170 | log_wrap_enabled = 1; |
---|
| 171 | |
---|
| 172 | // Set the wrap buffer to EVENT_LOG_WRAP_BUFFER |
---|
| 173 | wrap_buffer = EVENT_LOG_WRAPPING_BUFFER; |
---|
| 174 | |
---|
| 175 | // Reset all the event log variables |
---|
| 176 | event_log_reset(); |
---|
| 177 | |
---|
| 178 | // Disable the log if needed |
---|
| 179 | if (disable_log == 1) { |
---|
| 180 | event_log_config_logging(EVENT_LOG_LOGGING_DISABLE); |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | /*****************************************************************************/ |
---|
| 187 | /** |
---|
| 188 | * Reset the event log |
---|
| 189 | * |
---|
| 190 | * @param None |
---|
| 191 | * |
---|
| 192 | * @return None |
---|
| 193 | * |
---|
| 194 | * @note This will not change the state of the wrapping configuration |
---|
| 195 | * |
---|
| 196 | *****************************************************************************/ |
---|
| 197 | void event_log_reset(){ |
---|
| 198 | log_soft_end_address = log_max_address; |
---|
| 199 | |
---|
| 200 | log_oldest_address = log_start_address; |
---|
| 201 | log_next_address = log_start_address; |
---|
| 202 | log_num_wraps = 0; |
---|
| 203 | |
---|
| 204 | log_empty = 1; |
---|
| 205 | log_full = 0; |
---|
| 206 | log_count = 0; |
---|
| 207 | |
---|
| 208 | allocation_mutex = 0; |
---|
| 209 | |
---|
| 210 | // Add a node info entry to the log |
---|
| 211 | // |
---|
| 212 | // NOTE: A node_info_entry is guaranteed to be present as the first entry in the log |
---|
| 213 | // |
---|
| 214 | add_node_info_entry(); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | /*****************************************************************************/ |
---|
| 220 | /** |
---|
| 221 | * Set the wrap configuration parameter |
---|
| 222 | * |
---|
| 223 | * @param enable - Is wrapping enabled? |
---|
| 224 | * EVENT_LOG_WRAP_ENABLE |
---|
| 225 | * EVENT_LOG_WRAP_DISABLE |
---|
| 226 | * |
---|
| 227 | * @return int - Status of the command |
---|
| 228 | * - SUCCESS = 0 |
---|
| 229 | * - FAILURE = -1 |
---|
| 230 | * |
---|
| 231 | *****************************************************************************/ |
---|
| 232 | int event_log_config_wrap(u32 enable) { |
---|
| 233 | int ret_val = 0; |
---|
| 234 | |
---|
| 235 | switch ( enable ) { |
---|
| 236 | case EVENT_LOG_WRAP_ENABLE: |
---|
| 237 | log_wrap_enabled = 1; |
---|
| 238 | break; |
---|
| 239 | |
---|
| 240 | case EVENT_LOG_WRAP_DISABLE: |
---|
| 241 | log_wrap_enabled = 0; |
---|
| 242 | break; |
---|
| 243 | |
---|
| 244 | default: |
---|
| 245 | ret_val = -1; |
---|
| 246 | break; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | return ret_val; |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | |
---|
| 253 | |
---|
| 254 | /*****************************************************************************/ |
---|
| 255 | /** |
---|
| 256 | * Set the event logging enable variable |
---|
| 257 | * |
---|
| 258 | * @param enable - Is logging enabled? |
---|
| 259 | * EVENT_LOG_LOGGING_ENABLE |
---|
| 260 | * EVENT_LOG_LOGGING_DISABLE |
---|
| 261 | * |
---|
| 262 | * @return int - Status of the command |
---|
| 263 | * - SUCCESS = 0 |
---|
| 264 | * - FAILURE = -1 |
---|
| 265 | * |
---|
| 266 | *****************************************************************************/ |
---|
| 267 | int event_log_config_logging( u32 enable ) { |
---|
| 268 | int ret_val = 0; |
---|
| 269 | |
---|
| 270 | switch ( enable ) { |
---|
| 271 | case EVENT_LOG_LOGGING_ENABLE: |
---|
| 272 | event_logging_enabled = 1; |
---|
| 273 | break; |
---|
| 274 | |
---|
| 275 | case EVENT_LOG_LOGGING_DISABLE: |
---|
| 276 | event_logging_enabled = 0; |
---|
| 277 | break; |
---|
| 278 | |
---|
| 279 | default: |
---|
| 280 | ret_val = -1; |
---|
| 281 | break; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | return ret_val; |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | |
---|
| 288 | |
---|
| 289 | /*****************************************************************************/ |
---|
| 290 | /** |
---|
| 291 | * Get event log data |
---|
| 292 | * |
---|
| 293 | * Based on the start address and the size, the function will fill in the |
---|
| 294 | * appropriate number of bytes in to the buffer. It is up to the caller |
---|
| 295 | * to determine if the bytes are "valid". |
---|
| 296 | * |
---|
| 297 | * @param start_index - Index in the event log to start the transfer |
---|
| 298 | * (ie byte index from 0 to log_size) |
---|
| 299 | * @param size - Size in bytes of the buffer |
---|
| 300 | * @param address_ret - Pointer to memory that will be filled in with address of first log entry |
---|
| 301 | * |
---|
| 302 | * @return u32 - The number of bytes available starting from *address_ret |
---|
| 303 | * |
---|
| 304 | * @note Any requests for data that is out of bounds will print a warning and |
---|
| 305 | * return 0 bytes. If a request exceeds the size of the array, then |
---|
| 306 | * the request will be truncated. |
---|
| 307 | * |
---|
| 308 | *****************************************************************************/ |
---|
| 309 | u32 event_log_get_data(u32 start_index, u32 size, u8** address_ret) { |
---|
| 310 | |
---|
| 311 | u32 start_address; |
---|
| 312 | u64 end_address; |
---|
| 313 | u32 num_bytes = 0; |
---|
| 314 | |
---|
| 315 | // If the log is empty, then return 0 |
---|
| 316 | if (log_empty == 1) { return num_bytes; } |
---|
| 317 | |
---|
| 318 | // Check that the start_address is less than the log_size |
---|
| 319 | if (start_index > log_size) { |
---|
| 320 | xil_printf("EVENT LOG: WARNING: Index out of bounds: Request starts at 0x%08x; Log only has 0x%08x bytes\n", start_index, log_size); |
---|
| 321 | return num_bytes; |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | // Translate the start address from an index to the actual memory location |
---|
| 325 | start_address = log_start_address + start_index; |
---|
| 326 | |
---|
| 327 | // Compute the end address for validity checks |
---|
| 328 | end_address = start_address + size; |
---|
| 329 | |
---|
| 330 | // Check that the end address is less than the end of the buffer |
---|
| 331 | // FIXME: there's an off-by-one error here, I think (at least if log_soft_end_address |
---|
| 332 | // is the address of the last valid log data byte, not the address of the first invalid |
---|
| 333 | // byte _after_ the last valid byte |
---|
| 334 | if ( end_address > log_soft_end_address ) { |
---|
| 335 | num_bytes = log_soft_end_address - start_address; |
---|
| 336 | } else { |
---|
| 337 | num_bytes = size; |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | *address_ret = (u8*)start_address; |
---|
| 341 | |
---|
| 342 | return num_bytes; |
---|
| 343 | |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | |
---|
| 347 | |
---|
| 348 | /*****************************************************************************/ |
---|
| 349 | /** |
---|
| 350 | * Get the size of the log in bytes from the start_index to the "end" of the log |
---|
| 351 | * |
---|
| 352 | * The "end" of the log is: |
---|
| 353 | * - log_next_address if start_address < log_next_address |
---|
| 354 | * - log_soft_end_address if start_address >= log_next_address |
---|
| 355 | * |
---|
| 356 | * This function will no longer return the total number of bytes in the log. |
---|
| 357 | * To get that, you would need to call: event_log_get_total_size() |
---|
| 358 | * |
---|
| 359 | * @param start_index - Starting byte of the log to use for size calculation |
---|
| 360 | * |
---|
| 361 | * @return u32 - Size of the log in bytes |
---|
| 362 | * |
---|
| 363 | *****************************************************************************/ |
---|
| 364 | u32 event_log_get_size(u32 start_index) { |
---|
| 365 | u32 size; |
---|
| 366 | u32 start_address = log_start_address + start_index; |
---|
| 367 | |
---|
| 368 | // Implemented this way b/c we are using unsigned integers, so we always need |
---|
| 369 | // to have positive integers at each point in the calculation |
---|
| 370 | if (start_address < log_next_address) { |
---|
| 371 | size = log_next_address - start_address; |
---|
| 372 | } else { |
---|
| 373 | size = log_soft_end_address - start_address; |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | return size; |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | |
---|
| 380 | |
---|
| 381 | /*****************************************************************************/ |
---|
| 382 | /** |
---|
| 383 | * Get the total size of the valid log entries in bytes |
---|
| 384 | * |
---|
| 385 | * @param None |
---|
| 386 | * |
---|
| 387 | * @return u32 - Total size of the valid log entries in bytes |
---|
| 388 | * |
---|
| 389 | *****************************************************************************/ |
---|
| 390 | u32 event_log_get_total_size(void) { |
---|
| 391 | u32 size; |
---|
| 392 | u32 oldest_index; |
---|
| 393 | u32 next_index; |
---|
| 394 | |
---|
| 395 | // Implemented this way b/c we are using unsigned integers, so we always need |
---|
| 396 | // to have positive integers at each point in the calculation |
---|
| 397 | oldest_index = event_log_get_oldest_entry_index(); |
---|
| 398 | next_index = event_log_get_next_entry_index(); |
---|
| 399 | |
---|
| 400 | if (oldest_index < next_index) { // Log has not wrapped |
---|
| 401 | size = event_log_get_size(oldest_index); |
---|
| 402 | } else { // Log has wrapped |
---|
| 403 | size = event_log_get_size(oldest_index) + next_index; |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | return size; |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | |
---|
| 410 | |
---|
| 411 | /*****************************************************************************/ |
---|
| 412 | /** |
---|
| 413 | * Get the maximum size of the log (capacity) |
---|
| 414 | * |
---|
| 415 | * @param None |
---|
| 416 | * |
---|
| 417 | * @return u32 - Maximum size of the log (capacity) |
---|
| 418 | * |
---|
| 419 | *****************************************************************************/ |
---|
| 420 | u32 event_log_get_capacity(void) { |
---|
| 421 | return log_size; |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | |
---|
| 425 | |
---|
| 426 | /*****************************************************************************/ |
---|
| 427 | /** |
---|
| 428 | * Get the address of the write pointer to the next entry index |
---|
| 429 | * |
---|
| 430 | * @param None |
---|
| 431 | * |
---|
| 432 | * @return u32 - Index of the event log of the write pointer |
---|
| 433 | * |
---|
| 434 | *****************************************************************************/ |
---|
| 435 | u32 event_log_get_next_entry_index(void) { |
---|
| 436 | return (log_next_address - log_start_address); |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | |
---|
| 440 | |
---|
| 441 | /*****************************************************************************/ |
---|
| 442 | /** |
---|
| 443 | * Get the index of the oldest entry |
---|
| 444 | * |
---|
| 445 | * @param None |
---|
| 446 | * |
---|
| 447 | * @return u32 - Index of the event log of the oldest entry |
---|
| 448 | * |
---|
| 449 | *****************************************************************************/ |
---|
| 450 | u32 event_log_get_oldest_entry_index(void) { |
---|
| 451 | return (log_oldest_address - log_start_address); |
---|
| 452 | } |
---|
| 453 | |
---|
| 454 | |
---|
| 455 | |
---|
| 456 | /*****************************************************************************/ |
---|
| 457 | /** |
---|
| 458 | * Get the number of times the log has wrapped |
---|
| 459 | * |
---|
| 460 | * @param None |
---|
| 461 | * |
---|
| 462 | * @return u32 - Number of times the log has wrapped |
---|
| 463 | * |
---|
| 464 | *****************************************************************************/ |
---|
| 465 | u32 event_log_get_num_wraps(void) { |
---|
| 466 | return log_num_wraps; |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | |
---|
| 470 | |
---|
| 471 | /*****************************************************************************/ |
---|
| 472 | /** |
---|
| 473 | * Get the flags associated with the log |
---|
| 474 | * |
---|
| 475 | * @param None |
---|
| 476 | * |
---|
| 477 | * @return u32 - Flags: |
---|
| 478 | * [ 0] - event_logging_enabled; |
---|
| 479 | * [ 1] - log_wrap_enabled; |
---|
| 480 | * |
---|
| 481 | * @note Should match CMD_PARAM_LOG_CONFIG_FLAG_ in wlan_exp_node.h |
---|
| 482 | * |
---|
| 483 | *****************************************************************************/ |
---|
| 484 | u32 event_log_get_flags(void) { |
---|
| 485 | return ((log_wrap_enabled & 0x1) << 1) + (event_logging_enabled & 0x1); |
---|
| 486 | } |
---|
| 487 | |
---|
| 488 | /*****************************************************************************/ |
---|
| 489 | /** |
---|
| 490 | * Move the oldest address past the end_address while still being aligned |
---|
| 491 | * to an entry boundary |
---|
| 492 | * |
---|
| 493 | * @param end_address - Address that the oldest address must move past |
---|
| 494 | * by an integer number of entries. |
---|
| 495 | * |
---|
| 496 | * @return None |
---|
| 497 | * |
---|
| 498 | * @note This function will blindly increment the oldest address past the |
---|
| 499 | * end_address. It does not check for the end of the log and that |
---|
| 500 | * is the responsibility of the calling function. |
---|
| 501 | * |
---|
| 502 | *****************************************************************************/ |
---|
| 503 | void event_log_move_oldest_address(u32 end_address) { |
---|
| 504 | |
---|
| 505 | entry_header* entry; |
---|
| 506 | |
---|
| 507 | // Move the oldest address an integer number of entries until it points to the |
---|
| 508 | // first entry after the allocation |
---|
| 509 | entry = (entry_header *) log_oldest_address; |
---|
| 510 | |
---|
| 511 | // Move the address in increments of an entry |
---|
| 512 | while (log_oldest_address <= end_address) { |
---|
| 513 | |
---|
| 514 | // Check that the entry is still valid. Otherwise, print a warning and |
---|
| 515 | // issue a log reset. |
---|
| 516 | if ((entry->entry_id & 0xFFFF0000) != EVENT_LOG_MAGIC_NUMBER) { |
---|
| 517 | xil_printf("EVENT LOG: ERROR: Oldest entry corrupted. Resetting event log\n"); |
---|
| 518 | xil_printf(" Please verify that no other code / data is using the event log memory space\n"); |
---|
| 519 | |
---|
| 520 | event_log_reset(); |
---|
| 521 | return; |
---|
| 522 | } |
---|
| 523 | |
---|
| 524 | // Increment the address and get the next entry |
---|
| 525 | log_oldest_address += (entry->entry_length + sizeof(entry_header)); |
---|
| 526 | entry = (entry_header *) log_oldest_address; |
---|
| 527 | } |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | |
---|
| 531 | |
---|
| 532 | /*****************************************************************************/ |
---|
| 533 | /** |
---|
| 534 | * Increment the oldest address |
---|
| 535 | * |
---|
| 536 | * @param end_address - Current ending address |
---|
| 537 | * @param size - Number of bytes to increment the oldest address |
---|
| 538 | * |
---|
| 539 | * @return None |
---|
| 540 | * |
---|
| 541 | * @note This function will blindly increment the oldest address by 'size' |
---|
| 542 | * bytes (ie it does not check the log_oldest_address relative to the |
---|
| 543 | * log_next_address). It is the responsibility of the calling |
---|
| 544 | * function to make sure this is only called when appropriate. |
---|
| 545 | * |
---|
| 546 | *****************************************************************************/ |
---|
| 547 | void event_log_increment_oldest_address(u64 end_address, u32 size) { |
---|
| 548 | |
---|
| 549 | u64 final_end_address; |
---|
| 550 | |
---|
| 551 | // Calculate end address (need to make sure we don't overflow u32) |
---|
| 552 | final_end_address = end_address + wrap_buffer; |
---|
| 553 | |
---|
| 554 | // Check to see if we will wrap with the current increment |
---|
| 555 | if (final_end_address >= log_soft_end_address) { |
---|
| 556 | // We will wrap the log |
---|
| 557 | |
---|
| 558 | // Reset the log_soft_end_address to the end of the array |
---|
| 559 | log_soft_end_address = log_max_address; |
---|
| 560 | |
---|
| 561 | // Move the log_oldest_address to the beginning of the array and move it |
---|
| 562 | // at least 'size' bytes from the front of the array. This is done to mirror |
---|
| 563 | // how allocation of the log_next_address works. Also, b/c of this allocation |
---|
| 564 | // scheme, we are guaranteed that log_start_address is the beginning of an entry. |
---|
| 565 | // |
---|
| 566 | // NOTE: We need to skip the node_info at the beginning of the buffer. |
---|
| 567 | // |
---|
| 568 | log_oldest_address = log_start_address + sizeof(node_info_entry) + sizeof(entry_header); |
---|
| 569 | final_end_address = log_oldest_address + size + wrap_buffer; |
---|
| 570 | } |
---|
| 571 | |
---|
| 572 | // Move the oldest address |
---|
| 573 | event_log_move_oldest_address(final_end_address); |
---|
| 574 | } |
---|
| 575 | |
---|
| 576 | |
---|
| 577 | |
---|
| 578 | /*****************************************************************************/ |
---|
| 579 | /** |
---|
| 580 | * Get the address of the next empty entry in the log and allocate size bytes |
---|
| 581 | * for that entry |
---|
| 582 | * |
---|
| 583 | * @param size - Size (in bytes) of entry to allocate |
---|
| 584 | * @param address - Pointer to address of empty entry of length 'size' |
---|
| 585 | * |
---|
| 586 | * @return int - Status of command |
---|
| 587 | * - 0 = Success |
---|
| 588 | * - 1 = Failure |
---|
| 589 | * |
---|
| 590 | * @note This will handle the circular nature of the buffer. It will also |
---|
| 591 | * set the log_full flag if there is no additional space and print |
---|
| 592 | * a warning message. If this function is called while the event log |
---|
| 593 | * is full, then it will always return max_entry_index |
---|
| 594 | * |
---|
| 595 | *****************************************************************************/ |
---|
| 596 | int event_log_get_next_empty_address( u32 size, u32* address ) { |
---|
| 597 | |
---|
| 598 | int status = 1; // Initialize status to FAILED |
---|
| 599 | u32 return_address = 0; |
---|
| 600 | u64 end_address; |
---|
| 601 | node_info_entry* entry; |
---|
| 602 | |
---|
| 603 | // If the log is empty, then set the flag to zero to indicate the log is not empty |
---|
| 604 | if (log_empty) { log_empty = 0; } |
---|
| 605 | |
---|
| 606 | // If the log is not full, then find the next address |
---|
| 607 | if (!log_full && !allocation_mutex) { |
---|
| 608 | |
---|
| 609 | // Set the mutex to '1' so that if an interrupt occurs, the event log allocation |
---|
| 610 | // will not be ruined |
---|
| 611 | allocation_mutex = 1; |
---|
| 612 | |
---|
| 613 | // Compute the end address of the newly allocated entry |
---|
| 614 | end_address = (u64)(log_next_address) + (u64)(size); |
---|
| 615 | |
---|
| 616 | // Check if the log has wrapped |
---|
| 617 | if ((log_next_address > log_oldest_address) || |
---|
| 618 | ((log_next_address == log_start_address) && (log_oldest_address == log_start_address))) { |
---|
| 619 | // The log has not wrapped |
---|
| 620 | |
---|
| 621 | // Check to see if we will wrap with the current allocation |
---|
| 622 | if ( end_address > log_soft_end_address ) { |
---|
| 623 | // Current allocation will wrap the log |
---|
| 624 | |
---|
| 625 | // Check to see if wrapping is enabled |
---|
| 626 | if ( log_wrap_enabled ) { |
---|
| 627 | |
---|
| 628 | xil_printf("EVENT LOG: LOG WRAP: Has wrapped %d times\n", log_num_wraps); |
---|
| 629 | |
---|
| 630 | // Compute new end address |
---|
| 631 | end_address = log_start_address + sizeof(node_info_entry) + sizeof(entry_header) + size; |
---|
| 632 | |
---|
| 633 | // Check that we are not going to pass the oldest address |
---|
| 634 | if (end_address > log_oldest_address) { |
---|
| 635 | event_log_increment_oldest_address( end_address, size ); |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | // Increment the number of wraps |
---|
| 639 | log_num_wraps += 1; |
---|
| 640 | |
---|
| 641 | // Update the node_info_entry timestamp |
---|
| 642 | entry = (node_info_entry *)(log_start_address + sizeof(entry_header)); |
---|
| 643 | entry->timestamp = get_mac_time_usec(); |
---|
| 644 | |
---|
| 645 | // Set the log_soft_end_address and allocate the new entry from the beginning of the buffer |
---|
| 646 | log_soft_end_address = log_next_address; |
---|
| 647 | log_next_address = end_address; |
---|
| 648 | |
---|
| 649 | // Return address is the beginning of the buffer |
---|
| 650 | // (skipping the node_info at the beginning of the buffer) |
---|
| 651 | return_address = log_start_address + sizeof(node_info_entry) + sizeof(entry_header); |
---|
| 652 | status = 0; |
---|
| 653 | |
---|
| 654 | } else { |
---|
| 655 | // Set the full flag and the soft end; then return |
---|
| 656 | log_full = 1; |
---|
| 657 | log_soft_end_address = log_next_address; |
---|
| 658 | |
---|
| 659 | // Set the value to the appropriate "full" state |
---|
| 660 | log_oldest_address = log_start_address; |
---|
| 661 | log_next_address = log_start_address; |
---|
| 662 | log_num_wraps += 1; |
---|
| 663 | |
---|
| 664 | // Log is now full, print warning |
---|
| 665 | xil_printf("---------------------------------------- \n"); |
---|
| 666 | xil_printf("EVENT LOG: WARNING - Event Log FULL !!! \n"); |
---|
| 667 | xil_printf("---------------------------------------- \n"); |
---|
| 668 | } |
---|
| 669 | } else { |
---|
| 670 | // Current allocation does not wrap |
---|
| 671 | |
---|
| 672 | // NOTE: This should be the most common case; since we know the log has not wrapped |
---|
| 673 | // we do not need to increment the log_oldest_address. |
---|
| 674 | |
---|
| 675 | // Set the return address and then move the log_next_address |
---|
| 676 | return_address = log_next_address; |
---|
| 677 | log_next_address = end_address; |
---|
| 678 | status = 0; |
---|
| 679 | } |
---|
| 680 | } else { |
---|
| 681 | // The log has wrapped |
---|
| 682 | // NOTE: Even though the log has wrapped, we cannot assume that the wrap flag |
---|
| 683 | // continues to allow the log to wrap. |
---|
| 684 | |
---|
| 685 | // Check that we are not going to pass the oldest address |
---|
| 686 | // NOTE: This will set the log_soft_end_address if the oldest_address passes the end of the array |
---|
| 687 | if (end_address > log_oldest_address) { |
---|
| 688 | event_log_increment_oldest_address( end_address, size ); |
---|
| 689 | } |
---|
| 690 | |
---|
| 691 | // Check to see if we will wrap with the current allocation |
---|
| 692 | if (end_address > log_soft_end_address) { |
---|
| 693 | // Current allocation will wrap the log |
---|
| 694 | |
---|
| 695 | // Check to see if wrapping is enabled |
---|
| 696 | if ( log_wrap_enabled ) { |
---|
| 697 | |
---|
| 698 | xil_printf("EVENT LOG: LOG WRAP: Has wrapped %d times\n", log_num_wraps); |
---|
| 699 | |
---|
| 700 | // Compute new end address |
---|
| 701 | end_address = log_start_address + sizeof(node_info_entry) + sizeof(entry_header) + size; |
---|
| 702 | |
---|
| 703 | // NOTE: We have already incremented the log_oldest_address by size. Since the |
---|
| 704 | // event_log_increment_oldest_address() function follows the same allocation scheme |
---|
| 705 | // we are guaranteed that at least 'size' bytes are available at the beginning of the |
---|
| 706 | // array if we wrapped. Therefore, we do not need to check the log_oldest_address again. |
---|
| 707 | |
---|
| 708 | // Increment the number of wraps |
---|
| 709 | log_num_wraps += 1; |
---|
| 710 | |
---|
| 711 | // Update the node_info_entry timestamp |
---|
| 712 | entry = (node_info_entry *)(log_start_address + sizeof(entry_header)); |
---|
| 713 | entry->timestamp = get_mac_time_usec(); |
---|
| 714 | |
---|
| 715 | // Set the log_soft_end_address and allocate the new entry from the beginning of the buffer |
---|
| 716 | log_soft_end_address = log_next_address; |
---|
| 717 | log_next_address = end_address; |
---|
| 718 | |
---|
| 719 | // Return address is the beginning of the buffer |
---|
| 720 | // (skipping the node_info at the beginning of the buffer) |
---|
| 721 | return_address = log_start_address + sizeof(node_info_entry) + sizeof(entry_header); |
---|
| 722 | status = 0; |
---|
| 723 | |
---|
| 724 | } else { |
---|
| 725 | // Set the full flag and the soft end; then return |
---|
| 726 | log_full = 1; |
---|
| 727 | log_soft_end_address = log_next_address; |
---|
| 728 | |
---|
| 729 | // Set the value to the appropriate "full" state |
---|
| 730 | log_oldest_address = log_start_address; |
---|
| 731 | log_next_address = log_start_address; |
---|
| 732 | log_num_wraps += 1; |
---|
| 733 | |
---|
| 734 | // Log is now full, print warning |
---|
| 735 | xil_printf("---------------------------------------- \n"); |
---|
| 736 | xil_printf("EVENT LOG: WARNING - Event Log FULL !!! \n"); |
---|
| 737 | xil_printf("---------------------------------------- \n"); |
---|
| 738 | } |
---|
| 739 | } else { |
---|
| 740 | // Current allocation does not wrap |
---|
| 741 | |
---|
| 742 | // Set the return address and then move the log_next_address |
---|
| 743 | return_address = log_next_address; |
---|
| 744 | log_next_address = end_address; |
---|
| 745 | status = 0; |
---|
| 746 | } |
---|
| 747 | } |
---|
| 748 | |
---|
| 749 | // Set the mutex to '0' to allow for future allocations |
---|
| 750 | allocation_mutex = 0; |
---|
| 751 | } |
---|
| 752 | |
---|
| 753 | // Set return parameter |
---|
| 754 | *address = return_address; |
---|
| 755 | |
---|
| 756 | return status; |
---|
| 757 | } |
---|
| 758 | |
---|
| 759 | |
---|
| 760 | |
---|
| 761 | /*****************************************************************************/ |
---|
| 762 | /** |
---|
| 763 | * Get the next empty entry |
---|
| 764 | * |
---|
| 765 | * @param entry_type - Type of entry |
---|
| 766 | * @param entry_size - Size of the entry payload |
---|
| 767 | * |
---|
| 768 | * @return void * - Pointer to the next entry payload |
---|
| 769 | * |
---|
| 770 | *****************************************************************************/ |
---|
| 771 | void* event_log_get_next_empty_entry(u16 entry_type, u16 entry_size) { |
---|
| 772 | |
---|
| 773 | u32 log_address; |
---|
| 774 | u32 total_size; |
---|
| 775 | entry_header* header = NULL; |
---|
| 776 | u32 header_size = sizeof( entry_header ); |
---|
| 777 | void* return_entry = NULL; |
---|
| 778 | |
---|
| 779 | // If Event Logging is enabled, then allocate entry |
---|
| 780 | if (event_logging_enabled) { |
---|
| 781 | |
---|
| 782 | // Entries must be 4 byte aligned so that there are not a lot of mis-aligned |
---|
| 783 | // accesses that could lead to significant processing overhead. |
---|
| 784 | // |
---|
| 785 | if ((entry_size % 4) != 0) { |
---|
| 786 | entry_size = ((entry_size >> 2) + 1) << 2; |
---|
| 787 | } |
---|
| 788 | |
---|
| 789 | total_size = entry_size + header_size; |
---|
| 790 | |
---|
| 791 | // Try to allocate the next entry |
---|
| 792 | if (!event_log_get_next_empty_address(total_size, &log_address)) { |
---|
| 793 | |
---|
| 794 | // Use successfully allocated address for the entry |
---|
| 795 | header = (entry_header*) log_address; |
---|
| 796 | |
---|
| 797 | // Zero out entry |
---|
| 798 | // Note: this operation takes ~14usec for the 300 bytes Rx OFDM entry -- a substantial |
---|
| 799 | // NOTE: Based on characterization, this bzero operation was considerable |
---|
| 800 | // overhead in relation to the tasks that are getting empty log entries. |
---|
| 801 | // Therefore, we are removing it. The one thing to note is that empty |
---|
| 802 | // log entries will have random data and cannot be assumed to have a |
---|
| 803 | // starting value. |
---|
| 804 | // bzero((void *) header, total_size); |
---|
| 805 | |
---|
| 806 | // Set header parameters |
---|
| 807 | // - Use the upper 16 bits of the timestamp to place a magic number |
---|
| 808 | header->entry_id = EVENT_LOG_MAGIC_NUMBER + (0x0000FFFF & log_count++); |
---|
| 809 | header->entry_type = entry_type; |
---|
| 810 | header->entry_length = entry_size; |
---|
| 811 | |
---|
| 812 | // Get a pointer to the entry payload |
---|
| 813 | return_entry = (void *)(log_address + header_size); |
---|
| 814 | } |
---|
| 815 | } |
---|
| 816 | |
---|
| 817 | return return_entry; |
---|
| 818 | } |
---|
| 819 | |
---|
| 820 | |
---|
| 821 | /*****************************************************************************/ |
---|
| 822 | /** |
---|
| 823 | * Prints the size of the event log |
---|
| 824 | * |
---|
| 825 | * @param None |
---|
| 826 | * |
---|
| 827 | * @return None |
---|
| 828 | * |
---|
| 829 | *****************************************************************************/ |
---|
| 830 | void print_event_log_size(){ |
---|
| 831 | u32 size; |
---|
| 832 | u64 timestamp; |
---|
| 833 | |
---|
| 834 | size = event_log_get_total_size(); |
---|
| 835 | timestamp = get_mac_time_usec(); |
---|
| 836 | |
---|
| 837 | xil_printf("EVENT LOG: (%10d us) %10d of %10d bytes used\n", (u32)timestamp, size, log_size); |
---|
| 838 | } |
---|
| 839 | |
---|
| 840 | #endif //WLAN_SW_CONFIG_ENABLE_LOGGING |
---|
| 841 | |
---|
| 842 | |
---|
| 843 | |
---|