1 #include "wifi_hal.h"
2 
3 #ifndef __WIFI_HAL_LOGGER_H
4 #define __WIFI_HAL_LOGGER_H
5 
6 #ifdef __cplusplus
7 extern "C"
8 {
9 #endif /* __cplusplus */
10 
11 #define LOGGER_MAJOR_VERSION    1
12 #define LOGGER_MINOR_VERSION    0
13 #define LOGGER_MICRO_VERSION    0
14 
15 
16 
17 /**
18  * WiFi logger life cycle is as follow:
19  *
20  * - At initialization time, framework will call wifi_get_ring_buffers_status
21  *   so as to obtain the names and list of supported buffers.
22  * - When WiFi operation start framework will call wifi_start_logging
23  *   so as to trigger log collection.
24  * - Developper UI will provide an option to the user, so as it can set the verbose level
25  *   of individual buffer as reported by wifi_get_ring_buffers_status.
26  * - During wifi operations, driver will periodically report per ring data to framework
27  *   by invoking the on_ring_buffer_data call back.
28  * - when capturing a bug report, framework will indicate to driver that all the data
29  *   has to be uploaded, urgently, by calling wifi_get_ring_data.
30  *
31  * The data uploaded by driver will be stored by framework in separate files, with one stream
32  *   of file per ring.
33  * Framework will store the files in pcapng format, allowing for easy merging and parsing
34  *   with network analyzer tools.
35  */
36 
37 
38 typedef int wifi_radio;
39 typedef int wifi_ring_buffer_id;
40 
41 #define PER_PACKET_ENTRY_FLAGS_DIRECTION_TX  1    // 0: TX, 1: RX
42 #define PER_PACKET_ENTRY_FLAGS_TX_SUCCESS    2    // whether packet was transmitted or
43                                                   // received/decrypted successfully
44 #define PER_PACKET_ENTRY_FLAGS_80211_HEADER  4    // has full 802.11 header, else has 802.3 header
45 #define PER_PACKET_ENTRY_FLAGS_PROTECTED     8    // whether packet was encrypted
46 
47 typedef struct {
48     u8 flags;
49     u8 tid;     // transmit or received tid
50     u16 MCS;    // modulation and bandwidth
51     u8 rssi;    // TX: RSSI of ACK for that packet
52                 // RX: RSSI of packet
53     u8 num_retries;                   // number of attempted retries
54     u16 last_transmit_rate;           // last transmit rate in .5 mbps
55     u16 link_layer_transmit_sequence; // transmit/reeive sequence for that MPDU packet
56     u64 firmware_entry_timestamp;     // TX: firmware timestamp (us) when packet is queued within
57                                       // firmware buffer for SDIO/HSIC or into PCIe buffer
58                                       // RX: firmware receive timestamp
59     u64 start_contention_timestamp; // firmware timestamp (us) when packet start contending for the
60                                     // medium for the first time, at head of its AC queue,
61                                     // or as part of an MPDU or A-MPDU. This timestamp is
62                                     // not updated for each retry, only the first transmit attempt.
63     u64 transmit_success_timestamp; // fimrware timestamp (us) when packet is successfully
64                                     // transmitted or aborted because it has exhausted
65                                     // its maximum number of retries.
66     u8 data[0]; // packet data. The length of packet data is determined by the entry_size field of
67                 // the wifi_ring_buffer_entry structure. It is expected that first bytes of the
68                 // packet, or packet headers only (up to TCP or RTP/UDP headers)
69                 // will be copied into the ring
70 } __attribute__((packed)) wifi_ring_per_packet_status_entry;
71 
72 
73 /* Below events refer to the wifi_connectivity_event ring and shall be supported */
74 #define WIFI_EVENT_ASSOCIATION_REQUESTED    0  // driver receives association command from kernel
75 #define WIFI_EVENT_AUTH_COMPLETE            1
76 #define WIFI_EVENT_ASSOC_COMPLETE           2
77 #define WIFI_EVENT_FW_AUTH_STARTED          3  // fw event indicating auth frames are sent
78 #define WIFI_EVENT_FW_ASSOC_STARTED         4  // fw event indicating assoc frames are sent
79 #define WIFI_EVENT_FW_RE_ASSOC_STARTED      5  // fw event indicating reassoc frames are sent
80 #define WIFI_EVENT_DRIVER_SCAN_REQUESTED    6
81 #define WIFI_EVENT_DRIVER_SCAN_RESULT_FOUND 7
82 #define WIFI_EVENT_DRIVER_SCAN_COMPLETE     8
83 #define WIFI_EVENT_G_SCAN_STARTED           9
84 #define WIFI_EVENT_G_SCAN_COMPLETE          10
85 #define WIFI_EVENT_DISASSOCIATION_REQUESTED 11
86 #define WIFI_EVENT_RE_ASSOCIATION_REQUESTED 12
87 #define WIFI_EVENT_ROAM_REQUESTED           13
88 #define WIFI_EVENT_BEACON_RECEIVED          14  // received beacon from AP (event enabled
89                                                 // only in verbose mode)
90 #define WIFI_EVENT_ROAM_SCAN_STARTED        15  // firmware has triggered a roam scan (not g-scan)
91 #define WIFI_EVENT_ROAM_SCAN_COMPLETE       16  // firmware has completed a roam scan (not g-scan)
92 #define WIFI_EVENT_ROAM_SEARCH_STARTED      17  // firmware has started searching for roam
93                                                 // candidates (with reason =xx)
94 #define WIFI_EVENT_ROAM_SEARCH_STOPPED      18  // firmware has stopped searching for roam
95                                                 // candidates (with reason =xx)
96 #define WIFI_EVENT_CHANNEL_SWITCH_ANOUNCEMENT     20 // received channel switch anouncement from AP
97 #define WIFI_EVENT_FW_EAPOL_FRAME_TRANSMIT_START  21 // fw start transmit eapol frame, with
98                                                      // EAPOL index 1-4
99 #define WIFI_EVENT_FW_EAPOL_FRAME_TRANSMIT_STOP   22 // fw gives up eapol frame, with rate,
100                                                      // success/failure and number retries
101 #define WIFI_EVENT_DRIVER_EAPOL_FRAME_TRANSMIT_REQUESTED 23 // kernel queue EAPOL for transmission
102                                                             // in driver with EAPOL index 1-4
103 #define WIFI_EVENT_FW_EAPOL_FRAME_RECEIVED        24 // with rate, regardless of the fact that
104                                                      // EAPOL frame is accepted or rejected by fw
105 #define WIFI_EVENT_DRIVER_EAPOL_FRAME_RECEIVED    26 // with rate, and eapol index, driver has
106                                                      // received EAPOL frame and will queue it up
107                                                      // to wpa_supplicant
108 #define WIFI_EVENT_BLOCK_ACK_NEGOTIATION_COMPLETE 27 // with success/failure, parameters
109 #define WIFI_EVENT_BT_COEX_BT_SCO_START     28
110 #define WIFI_EVENT_BT_COEX_BT_SCO_STOP      29
111 #define WIFI_EVENT_BT_COEX_BT_SCAN_START    30  // for paging/scan etc., when BT starts transmiting
112                                                 // twice per BT slot
113 #define WIFI_EVENT_BT_COEX_BT_SCAN_STOP     31
114 #define WIFI_EVENT_BT_COEX_BT_HID_START     32
115 #define WIFI_EVENT_BT_COEX_BT_HID_STOP      33
116 #define WIFI_EVENT_ROAM_AUTH_STARTED        34  // fw sends auth frame in roaming to next candidate
117 #define WIFI_EVENT_ROAM_AUTH_COMPLETE       35  // fw receive auth confirm from ap
118 #define WIFI_EVENT_ROAM_ASSOC_STARTED       36  // firmware sends assoc/reassoc frame in
119                                                 // roaming to next candidate
120 #define WIFI_EVENT_ROAM_ASSOC_COMPLETE      37  // firmware receive assoc/reassoc confirm from ap
121 #define WIFI_EVENT_G_SCAN_STOP              38  // firmware sends stop G_SCAN
122 #define WIFI_EVENT_G_SCAN_CYCLE_STARTED     39  // firmware indicates G_SCAN scan cycle started
123 #define WIFI_EVENT_G_SCAN_CYCLE_COMPLETED   40  // firmware indicates G_SCAN scan cycle completed
124 #define WIFI_EVENT_G_SCAN_BUCKET_STARTED    41  // firmware indicates G_SCAN scan start
125                                                 // for a particular bucket
126 #define WIFI_EVENT_G_SCAN_BUCKET_COMPLETED  42  // firmware indicates G_SCAN scan completed for
127                                                 // for a particular bucket
128 #define WIFI_EVENT_G_SCAN_RESULTS_AVAILABLE 43  // Event received from firmware about G_SCAN scan
129                                                 // results being available
130 #define WIFI_EVENT_G_SCAN_CAPABILITIES      44  // Event received from firmware with G_SCAN
131                                                 // capabilities
132 #define WIFI_EVENT_ROAM_CANDIDATE_FOUND     45  // Event received from firmware when eligible
133                                                 // candidate is found
134 #define WIFI_EVENT_ROAM_SCAN_CONFIG         46  // Event received from firmware when roam scan
135                                                 // configuration gets enabled or disabled
136 #define WIFI_EVENT_AUTH_TIMEOUT             47  // firmware/driver timed out authentication
137 #define WIFI_EVENT_ASSOC_TIMEOUT            48  // firmware/driver timed out association
138 #define WIFI_EVENT_MEM_ALLOC_FAILURE        49  // firmware/driver encountered allocation failure
139 #define WIFI_EVENT_DRIVER_PNO_ADD           50  // driver added a PNO network in firmware
140 #define WIFI_EVENT_DRIVER_PNO_REMOVE        51  // driver removed a PNO network in firmware
141 #define WIFI_EVENT_DRIVER_PNO_NETWORK_FOUND 52  // driver received PNO networks
142                                                 // found indication from firmware
143 #define WIFI_EVENT_DRIVER_PNO_SCAN_REQUESTED 53  // driver triggered a scan for PNO networks
144 #define WIFI_EVENT_DRIVER_PNO_SCAN_RESULT_FOUND 54  // driver received scan results
145                                                     // of PNO networks
146 #define WIFI_EVENT_DRIVER_PNO_SCAN_COMPLETE 55  // driver updated scan results from
147                                                 // PNO networks to cfg80211
148 
149 /**
150  * Parameters of wifi logger events are TLVs
151  * Event parameters tags are defined as:
152  */
153 #define WIFI_TAG_VENDOR_SPECIFIC    0   // take a byte stream as parameter
154 #define WIFI_TAG_BSSID              1   // takes a 6 bytes MAC address as parameter
155 #define WIFI_TAG_ADDR               2   // takes a 6 bytes MAC address as parameter
156 #define WIFI_TAG_SSID               3   // takes a 32 bytes SSID address as parameter
157 #define WIFI_TAG_STATUS             4   // takes an integer as parameter
158 #define WIFI_TAG_CHANNEL_SPEC       5   // takes one or more wifi_channel_spec as parameter
159 #define WIFI_TAG_WAKE_LOCK_EVENT    6   // takes a wake_lock_event struct as parameter
160 #define WIFI_TAG_ADDR1              7   // takes a 6 bytes MAC address as parameter
161 #define WIFI_TAG_ADDR2              8   // takes a 6 bytes MAC address as parameter
162 #define WIFI_TAG_ADDR3              9   // takes a 6 bytes MAC address as parameter
163 #define WIFI_TAG_ADDR4              10  // takes a 6 bytes MAC address as parameter
164 #define WIFI_TAG_TSF                11  // take a 64 bits TSF value as parameter
165 #define WIFI_TAG_IE                 12  // take one or more specific 802.11 IEs parameter,
166                                         // IEs are in turn indicated in TLV format as per
167                                         // 802.11 spec
168 #define WIFI_TAG_INTERFACE          13  // take interface name as parameter
169 #define WIFI_TAG_REASON_CODE        14  // take a reason code as per 802.11 as parameter
170 #define WIFI_TAG_RATE_MBPS          15  // take a wifi rate in 0.5 mbps
171 #define WIFI_TAG_REQUEST_ID         16  // take an integer as parameter
172 #define WIFI_TAG_BUCKET_ID          17  // take an integer as parameter
173 #define WIFI_TAG_GSCAN_PARAMS       18  // takes a wifi_scan_cmd_params struct as parameter
174 #define WIFI_TAG_GSCAN_CAPABILITIES 19  // takes a wifi_gscan_capabilities struct as parameter
175 #define WIFI_TAG_SCAN_ID            20  // take an integer as parameter
176 #define WIFI_TAG_RSSI               21  // take an integer as parameter
177 #define WIFI_TAG_CHANNEL            22  // take an integer as parameter
178 #define WIFI_TAG_LINK_ID            23  // take an integer as parameter
179 #define WIFI_TAG_LINK_ROLE          24  // take an integer as parameter
180 #define WIFI_TAG_LINK_STATE         25  // take an integer as parameter
181 #define WIFI_TAG_LINK_TYPE          26  // take an integer as parameter
182 #define WIFI_TAG_TSCO               27  // take an integer as parameter
183 #define WIFI_TAG_RSCO               28  // take an integer as parameter
184 #define WIFI_TAG_EAPOL_MESSAGE_TYPE 29  // take an integer as parameter
185                                         // M1-1, M2-2, M3-3, M4-4
186 
187 typedef struct {
188     u16 tag;
189     u16 length; // length of value
190     u8 value[0];
191 } __attribute__((packed)) tlv_log;
192 
193 typedef struct {
194     u16 event;
195     tlv_log tlvs[0];   // separate parameter structure per event to be provided and optional data
196                        // the event_data is expected to include an official android part, with some
197                        // parameter as transmit rate, num retries, num scan result found etc...
198                        // as well, event_data can include a vendor proprietary part which is
199                        // understood by the developer only.
200 } __attribute__((packed)) wifi_ring_buffer_driver_connectivity_event;
201 
202 
203 /**
204  * Ring buffer name for power events ring. note that power event are extremely frequents
205  * and thus should be stored in their own ring/file so as not to clobber connectivity events.
206  */
207 typedef struct {
208     int status;      // 0 taken, 1 released
209     int reason;      // reason why this wake lock is taken
210     char name[0];    // null terminated
211 } __attribute__((packed)) wake_lock_event;
212 
213 typedef struct {
214     u16 event;
215     tlv_log tlvs[0];
216 } __attribute__((packed)) wifi_power_event;
217 
218 
219 /**
220  * This structure represent a logger entry within a ring buffer.
221  * Wifi driver are responsible to manage the ring buffer and write the debug
222  * information into those rings.
223  *
224  * In general, the debug entries can be used to store meaningful 802.11 information (SME, MLME,
225  * connection and packet statistics) as well as vendor proprietary data that is specific to a
226  * specific driver or chipset.
227  * Binary entries can be used so as to store packet data or vendor specific information and
228  * will be treated as blobs of data by android.
229  *
230  * A user land process will be started by framework so as to periodically retrieve the
231  * data logged by drivers into their ring buffer, store the data into log files and include
232  * the logs into android bugreports.
233  */
234 enum {
235     RING_BUFFER_ENTRY_FLAGS_HAS_BINARY = (1 << (0)),    // set for binary entries
236     RING_BUFFER_ENTRY_FLAGS_HAS_TIMESTAMP = (1 << (1))  // set if 64 bits timestamp is present
237 };
238 
239 enum {
240     ENTRY_TYPE_CONNECT_EVENT = 1,
241     ENTRY_TYPE_PKT,
242     ENTRY_TYPE_WAKE_LOCK,
243     ENTRY_TYPE_POWER_EVENT,
244     ENTRY_TYPE_DATA
245 };
246 
247 typedef struct {
248     u16 entry_size; // the size of payload excluding the header.
249     u8 flags;
250     u8 type;        // entry type
251     u64 timestamp;  // present if has_timestamp bit is set.
252 } __attribute__((packed)) wifi_ring_buffer_entry;
253 
254 #define WIFI_RING_BUFFER_FLAG_HAS_BINARY_ENTRIES 0x00000001   // set if binary entries are present
255 #define WIFI_RING_BUFFER_FLAG_HAS_ASCII_ENTRIES  0x00000002   // set if ascii entries are present
256 
257 
258 /* ring buffer params */
259 /**
260  * written_bytes and read_bytes implement a producer consumer API
261  *     hence written_bytes >= read_bytes
262  * a modulo arithmetic of the buffer size has to be applied to those counters:
263  * actual offset into ring buffer = written_bytes % ring_buffer_byte_size
264  *
265  */
266 typedef struct {
267     u8 name[32];
268     u32 flags;
269     wifi_ring_buffer_id ring_id; // unique integer representing the ring
270     u32 ring_buffer_byte_size;   // total memory size allocated for the buffer
271     u32 verbose_level;           // verbose level for ring buffer
272     u32 written_bytes;           // number of bytes that was written to the buffer by driver,
273                                  // monotonously increasing integer
274     u32 read_bytes;              // number of bytes that was read from the buffer by user land,
275                                  // monotonously increasing integer
276     u32 written_records;         // number of records that was written to the buffer by driver,
277                                  // monotonously increasing integer
278 } wifi_ring_buffer_status;
279 
280 
281 /**
282  * Callback for reporting ring data
283  *
284  * The ring buffer data collection is event based:
285  *   - Driver calls on_ring_buffer_data when new records are available, the wifi_ring_buffer_status
286  *     passed up to framework in the call back indicates to framework if more data is available in
287  *     the ring buffer. It is not expected that driver will necessarily always empty the ring
288  *     immediately as data is available, instead driver will report data every X seconds or if
289  *     N bytes are available.
290  *   - In the case where a bug report has to be captured, framework will require driver to upload
291  *     all data immediately. This is indicated to driver when framework calls wifi_get_ringdata.
292  *     When framework calls wifi_get_ring_data, driver will start sending all available data in the
293  *     indicated ring by repeatedly invoking the on_ring_buffer_data callback.
294  *
295  * The callback is called by log handler whenever ring data comes in driver.
296  */
297 typedef struct {
298   void (*on_ring_buffer_data) (char *ring_name, char *buffer, int buffer_size,
299         wifi_ring_buffer_status *status);
300 } wifi_ring_buffer_data_handler;
301 
302 /**
303  * API to set the log handler for getting ring data
304  *  - Only a single instance of log handler can be instantiated for each ring buffer.
305  */
306 wifi_error wifi_set_log_handler(wifi_request_id id, wifi_interface_handle iface,
307     wifi_ring_buffer_data_handler handler);
308 
309 /* API to reset the log handler */
310 wifi_error wifi_reset_log_handler(wifi_request_id id, wifi_interface_handle iface);
311 
312 
313 /**
314  * Callback for reporting FW dump
315  *
316  * The buffer data collection is event based such as FW health check or FW dump.
317  * The callback is called by alert handler.
318  */
319 typedef struct {
320    void (*on_alert) (wifi_request_id id, char *buffer, int buffer_size, int err_code);
321 } wifi_alert_handler;
322 
323 /*
324  * API to set the alert handler for the alert case in Wi-Fi Chip
325  *  - Only a single instance of alert handler can be instantiated.
326  */
327 wifi_error wifi_set_alert_handler(wifi_request_id id, wifi_interface_handle iface,
328     wifi_alert_handler handler);
329 
330 /* API to reset the alert handler */
331 wifi_error wifi_reset_alert_handler(wifi_request_id id, wifi_interface_handle iface);
332 
333 /* API for framework to indicate driver has to upload and drain all data of a given ring */
334 wifi_error wifi_get_ring_data(wifi_interface_handle iface, char *ring_name);
335 
336 
337 /**
338  * API to trigger the debug collection.
339  *  Unless his API is invoked - logging is not triggered.
340  *  - Verbose_level 0 corresponds to no collection,
341  *    and it makes log handler stop by no more events from driver.
342  *  - Verbose_level 1 correspond to normal log level, with minimal user impact.
343  *    This is the default value.
344  *  - Verbose_level 2 are enabled when user is lazily trying to reproduce a problem,
345  *    wifi performances and power can be impacted but device should not otherwise be
346  *    significantly impacted.
347  *  - Verbose_level 3+ are used when trying to actively debug a problem.
348  *
349  * ring_name represent the name of the ring for which data collection shall start.
350  *
351  * flags: TBD parameter used to enable/disable specific events on a ring
352  * max_interval: maximum interval in seconds for driver to invoke on_ring_buffer_data,
353  *               ignore if zero
354  * min_data_size: minimum data size in buffer for driver to invoke on_ring_buffer_data,
355  *                ignore if zero
356  */
357 wifi_error wifi_start_logging(wifi_interface_handle iface, u32 verbose_level, u32 flags,
358     u32 max_interval_sec, u32 min_data_size, char *ring_name);
359 
360 /**
361  * API to get the status of all ring buffers supported by driver.
362  *  - Caller is responsible to allocate / free ring buffer status.
363  *  - Maximum no of ring buffer would be 10.
364  */
365 wifi_error wifi_get_ring_buffers_status(wifi_interface_handle iface, u32 *num_rings,
366     wifi_ring_buffer_status *status);
367 
368 /**
369  * Synchronous memory dump by user request.
370  *  - Caller is responsible to store memory dump data into a local,
371  *      e.g., /data/misc/wifi/memdump.bin
372  */
373 typedef struct {
374     void (*on_firmware_memory_dump) (char *buffer, int buffer_size);
375 } wifi_firmware_memory_dump_handler;
376 
377 /**
378  * API to collect a firmware memory dump for a given iface by async memdump event.
379  *  - Triggered by Alerthandler, esp. when FW problem or FW health check happens
380  *  - Caller is responsible to store fw dump data into a local,
381  *      e.g., /data/misc/wifi/alertdump-1.bin
382  */
383 wifi_error wifi_get_firmware_memory_dump(wifi_interface_handle iface,
384     wifi_firmware_memory_dump_handler handler);
385 
386 /**
387  * API to collect a firmware version string.
388  *  - Caller is responsible to allocate / free a buffer to retrieve firmware verion info.
389  *  - Max string will be at most 256 bytes.
390  */
391 wifi_error wifi_get_firmware_version(wifi_interface_handle iface, char *buffer, int buffer_size);
392 
393 /**
394  * API to collect a driver version string.
395  *  - Caller is responsible to allocate / free a buffer to retrieve driver verion info.
396  *  - Max string will be at most 256 bytes.
397  */
398 wifi_error wifi_get_driver_version(wifi_interface_handle iface, char *buffer, int buffer_size);
399 
400 
401 /* Feature set */
402 enum {
403     WIFI_LOGGER_MEMORY_DUMP_SUPPORTED = (1 << (0)),             // Memory dump of FW
404     WIFI_LOGGER_PER_PACKET_TX_RX_STATUS_SUPPORTED = (1 << (1)), // PKT status
405     WIFI_LOGGER_CONNECT_EVENT_SUPPORTED = (1 << (2)),           // Connectivity event
406     WIFI_LOGGER_POWER_EVENT_SUPPORTED = (1 << (3)),             // POWER of Driver
407     WIFI_LOGGER_WAKE_LOCK_SUPPORTED = (1 << (4)),               // WAKE LOCK of Driver
408     WIFI_LOGGER_VERBOSE_SUPPORTED = (1 << (5)),                 // verbose log of FW
409     WIFI_LOGGER_WATCHDOG_TIMER_SUPPORTED = (1 << (6)),          // monitor the health of FW
410     WIFI_LOGGER_DRIVER_DUMP_SUPPORTED = (1 << (7)),             // dumps driver state
411     WIFI_LOGGER_PACKET_FATE_SUPPORTED = (1 << (8)),             // tracks connection packets' fate
412 };
413 
414 /**
415  * API to retrieve the current supportive features.
416  *  - An integer variable is enough to have bit mapping info by caller.
417  */
418 wifi_error wifi_get_logger_supported_feature_set(wifi_interface_handle iface,
419     unsigned int *support);
420 
421 typedef struct {
422     /* Buffer is to be allocated and freed by HAL implementation. */
423     void (*on_driver_memory_dump) (char *buffer, int buffer_size);
424 } wifi_driver_memory_dump_callbacks;
425 
426 /**
427     API to collect driver state.
428 
429     Framework will call this API soon before or after (but not
430     concurrently with) wifi_get_firmware_memory_dump(). Capturing
431     firmware and driver dumps is intended to help identify
432     inconsistent state between these components.
433 
434     - In response to this call, HAL implementation should make one or
435       more calls to callbacks.on_driver_memory_dump(). Framework will
436       copy data out of the received |buffer|s, and concatenate the
437       contents thereof.
438     - HAL implemention will indicate completion of the driver memory
439       dump by returning from this call.
440 */
441 wifi_error wifi_get_driver_memory_dump(
442     wifi_interface_handle iface,
443     wifi_driver_memory_dump_callbacks callbacks);
444 
445 
446 /* packet fate logs */
447 
448 #define MD5_PREFIX_LEN             4
449 #define MAX_FATE_LOG_LEN           32
450 #define MAX_FRAME_LEN_ETHERNET     1518
451 #define MAX_FRAME_LEN_80211_MGMT   2352  // 802.11-2012 Fig. 8-34
452 
453 typedef enum {
454     // Sent over air and ACKed.
455     TX_PKT_FATE_ACKED,
456 
457     // Sent over air but not ACKed. (Normal for broadcast/multicast.)
458     TX_PKT_FATE_SENT,
459 
460     // Queued within firmware, but not yet sent over air.
461     TX_PKT_FATE_FW_QUEUED,
462 
463     // Dropped by firmware as invalid. E.g. bad source address, bad checksum,
464     // or invalid for current state.
465     TX_PKT_FATE_FW_DROP_INVALID,
466 
467     // Dropped by firmware due to lack of buffer space.
468     TX_PKT_FATE_FW_DROP_NOBUFS,
469 
470     // Dropped by firmware for any other reason. Includes frames that
471     // were sent by driver to firmware, but unaccounted for by
472     // firmware.
473     TX_PKT_FATE_FW_DROP_OTHER,
474 
475     // Queued within driver, not yet sent to firmware.
476     TX_PKT_FATE_DRV_QUEUED,
477 
478     // Dropped by driver as invalid. E.g. bad source address, or
479     // invalid for current state.
480     TX_PKT_FATE_DRV_DROP_INVALID,
481 
482     // Dropped by driver due to lack of buffer space.
483     TX_PKT_FATE_DRV_DROP_NOBUFS,
484 
485     // Dropped by driver for any other reason.
486     TX_PKT_FATE_DRV_DROP_OTHER,
487 } wifi_tx_packet_fate;
488 
489 typedef enum {
490     // Valid and delivered to network stack (e.g., netif_rx()).
491     RX_PKT_FATE_SUCCESS,
492 
493     // Queued within firmware, but not yet sent to driver.
494     RX_PKT_FATE_FW_QUEUED,
495 
496     // Dropped by firmware due to host-programmable filters.
497     RX_PKT_FATE_FW_DROP_FILTER,
498 
499     // Dropped by firmware as invalid. E.g. bad checksum, decrypt failed,
500     // or invalid for current state.
501     RX_PKT_FATE_FW_DROP_INVALID,
502 
503     // Dropped by firmware due to lack of buffer space.
504     RX_PKT_FATE_FW_DROP_NOBUFS,
505 
506     // Dropped by firmware for any other reason.
507     RX_PKT_FATE_FW_DROP_OTHER,
508 
509     // Queued within driver, not yet delivered to network stack.
510     RX_PKT_FATE_DRV_QUEUED,
511 
512     // Dropped by driver due to filter rules.
513     RX_PKT_FATE_DRV_DROP_FILTER,
514 
515     // Dropped by driver as invalid. E.g. not permitted in current state.
516     RX_PKT_FATE_DRV_DROP_INVALID,
517 
518     // Dropped by driver due to lack of buffer space.
519     RX_PKT_FATE_DRV_DROP_NOBUFS,
520 
521     // Dropped by driver for any other reason.
522     RX_PKT_FATE_DRV_DROP_OTHER,
523 } wifi_rx_packet_fate;
524 
525 typedef enum {
526     FRAME_TYPE_UNKNOWN,
527     FRAME_TYPE_ETHERNET_II,
528     FRAME_TYPE_80211_MGMT,
529 } frame_type;
530 
531 typedef struct {
532     // The type of MAC-layer frame that this frame_info holds.
533     // - For data frames, use FRAME_TYPE_ETHERNET_II.
534     // - For management frames, use FRAME_TYPE_80211_MGMT.
535     // - If the type of the frame is unknown, use FRAME_TYPE_UNKNOWN.
536     frame_type payload_type;
537 
538     // The number of bytes included in |frame_content|. If the frame
539     // contents are missing (e.g. RX frame dropped in firmware),
540     // |frame_len| should be set to 0.
541     size_t frame_len;
542 
543     // Host clock when this frame was received by the driver (either
544     // outbound from the host network stack, or inbound from the
545     // firmware).
546     // - The timestamp should be taken from a clock which includes time
547     //   the host spent suspended (e.g. ktime_get_boottime()).
548     // - If no host timestamp is available (e.g. RX frame was dropped in
549     //   firmware), this field should be set to 0.
550     u32 driver_timestamp_usec;
551 
552     // Firmware clock when this frame was received by the firmware
553     // (either outbound from the host, or inbound from a remote
554     // station).
555     // - The timestamp should be taken from a clock which includes time
556     //   firmware spent suspended (if applicable).
557     // - If no firmware timestamp is available (e.g. TX frame was
558     //   dropped by driver), this field should be set to 0.
559     // - Consumers of |frame_info| should _not_ assume any
560     //   synchronization between driver and firmware clocks.
561     u32 firmware_timestamp_usec;
562 
563     // Actual frame content.
564     // - Should be provided for TX frames originated by the host.
565     // - Should be provided for RX frames received by the driver.
566     // - Optionally provided for TX frames originated by firmware. (At
567     //   discretion of HAL implementation.)
568     // - Optionally provided for RX frames dropped in firmware. (At
569     //   discretion of HAL implementation.)
570     // - If frame content is not provided, |frame_len| should be set
571     //   to 0.
572     union {
573       char ethernet_ii_bytes[MAX_FRAME_LEN_ETHERNET];
574       char ieee_80211_mgmt_bytes[MAX_FRAME_LEN_80211_MGMT];
575     } frame_content;
576 } frame_info;
577 
578 typedef struct {
579     // Prefix of MD5 hash of |frame_inf.frame_content|. If frame
580     // content is not provided, prefix of MD5 hash over the same data
581     // that would be in frame_content, if frame content were provided.
582     char md5_prefix[MD5_PREFIX_LEN];
583     wifi_tx_packet_fate fate;
584     frame_info frame_inf;
585 } wifi_tx_report;
586 
587 typedef struct {
588     // Prefix of MD5 hash of |frame_inf.frame_content|. If frame
589     // content is not provided, prefix of MD5 hash over the same data
590     // that would be in frame_content, if frame content were provided.
591     char md5_prefix[MD5_PREFIX_LEN];
592     wifi_rx_packet_fate fate;
593     frame_info frame_inf;
594 } wifi_rx_report;
595 
596 /**
597     API to start packet fate monitoring.
598     - Once stared, monitoring should remain active until HAL is unloaded.
599     - When HAL is unloaded, all packet fate buffers should be cleared.
600 */
601 wifi_error wifi_start_pkt_fate_monitoring(wifi_interface_handle handle);
602 
603 /**
604     API to retrieve fates of outbound packets.
605     - HAL implementation should fill |tx_report_bufs| with fates of
606       _first_ min(n_requested_fates, actual packets) frames
607       transmitted for the most recent association. The fate reports
608       should follow the same order as their respective packets.
609     - HAL implementation may choose (but is not required) to include
610       reports for management frames.
611     - Packets reported by firmware, but not recognized by driver,
612       should be included.  However, the ordering of the corresponding
613       reports is at the discretion of HAL implementation.
614     - Framework may call this API multiple times for the same association.
615     - Framework will ensure |n_requested_fates <= MAX_FATE_LOG_LEN|.
616     - Framework will allocate and free the referenced storage.
617 */
618 wifi_error wifi_get_tx_pkt_fates(wifi_interface_handle handle,
619         wifi_tx_report *tx_report_bufs,
620         size_t n_requested_fates,
621         size_t *n_provided_fates);
622 
623 /**
624     API to retrieve fates of inbound packets.
625     - HAL implementation should fill |rx_report_bufs| with fates of
626       _first_ min(n_requested_fates, actual packets) frames
627       received for the most recent association. The fate reports
628       should follow the same order as their respective packets.
629     - HAL implementation may choose (but is not required) to include
630       reports for management frames.
631     - Packets reported by firmware, but not recognized by driver,
632       should be included.  However, the ordering of the corresponding
633       reports is at the discretion of HAL implementation.
634     - Framework may call this API multiple times for the same association.
635     - Framework will ensure |n_requested_fates <= MAX_FATE_LOG_LEN|.
636     - Framework will allocate and free the referenced storage.
637 */
638 wifi_error wifi_get_rx_pkt_fates(wifi_interface_handle handle,
639         wifi_rx_report *rx_report_bufs,
640         size_t n_requested_fates,
641         size_t *n_provided_fates);
642 
643 #ifdef __cplusplus
644 }
645 #endif /* __cplusplus */
646 
647 #endif /*__WIFI_HAL_STATS_ */
648