1 /* Copyright (c) 2015, The Linux Foundation. All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions
5  * are met:
6  *  * Redistributions of source code must retain the above copyright
7  *    notice, this list of conditions and the following disclaimer.
8  *  * Redistributions in binary form must reproduce the above
9  *    copyright notice, this list of conditions and the following
10  *    disclaimer in the documentation and/or other materials provided
11  *    with the distribution.
12  *  * Neither the name of The Linux Foundation nor the names of its
13  *    contributors may be used to endorse or promote products derived
14  *    from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef _PKT_STATS_H_
30 #define _PKT_STATS_H_
31 
32 /* Types of packet log events.
33  * Tx stats will be sent from driver with the help of multiple events.
34  * Need to parse the events PKTLOG_TYPE_TX_CTRL and PKTLOG_TYPE_TX_STAT
35  * as of now for the required stats. Rest of the events can ignored.
36  */
37 #define PKTLOG_TYPE_TX_CTRL         1
38 #define PKTLOG_TYPE_TX_STAT         2
39 #define PKTLOG_TYPE_TX_MSDU_ID      3
40 #define PKTLOG_TYPE_TX_FRM_HDR      4
41 /* Rx stats will be sent from driver with event ID- PKTLOG_TYPE_RX_STAT */
42 #define PKTLOG_TYPE_RX_STAT         5
43 #define PKTLOG_TYPE_RC_FIND         6
44 #define PKTLOG_TYPE_RC_UPDATE       7
45 #define PKTLOG_TYPE_TX_VIRT_ADDR    8
46 #define PKTLOG_TYPE_PKT_STATS       9
47 #define PKTLOG_TYPE_PKT_DUMP        10
48 #define PKTLOG_TYPE_PKT_DUMP_V2     11
49 #define PKTLOG_TYPE_MAX             12
50 #define BW_OFFSET 8
51 #define INVALID_RSSI 255
52 
53 #define PKT_INFO_FLG_TX_LOCAL_S          0x1
54 #define PKT_INFO_FLG_RX_HOST_RXD         0x2
55 #define PKT_INFO_FLG_TX_REMOTE_S         0x4
56 #define PKT_INFO_FLG_RX_LOCAL_S          0x8
57 #define PKT_INFO_FLG_RX_REMOTE_S         0x10
58 #define PKT_INFO_FLG_RX_LOCAL_DISCARD_S  0x20
59 #define PKT_INFO_FLG_RX_REMOTE_DISCARD_S 0x40
60 #define PKT_INFO_FLG_RX_REORDER_STORE_S  0x80
61 #define PKT_INFO_FLG_RX_REORDER_DROP_S   0x100
62 #define PKT_INFO_FLG_RX_PEER_INFO_S      0x200
63 #define PKT_INFO_FLG_UNKNOWN_S           0x400
64 #define PKT_INFO_FLG_PKT_DUMP_V2         0x8000
65 
66 /* MASK value of flags based on RX_STAT content.
67  * These are the events that carry Rx decriptor
68  */
69 #define PKT_INFO_FLG_RX_RXDESC_MASK \
70         (PKT_INFO_FLG_RX_HOST_RXD | \
71          PKT_INFO_FLG_RX_LOCAL_S | \
72          PKT_INFO_FLG_RX_REMOTE_S | \
73          PKT_INFO_FLG_RX_LOCAL_DISCARD_S | \
74          PKT_INFO_FLG_RX_REMOTE_DISCARD_S)
75 
76 /* Format of the packet stats event*/
77 typedef struct {
78     u16 flags;
79     u16 missed_cnt;
80     u16 log_type;
81     u16 size;
82     u32 timestamp;
83 } __attribute__((packed)) wh_pktlog_hdr_t;
84 
85 /* Format of the v2 packet stats event*/
86 typedef struct {
87     u16 flags;
88     u16 missed_cnt;
89     u16 log_type;
90     u16 size;
91     u32 timestamp;
92     u32 reserved;
93 } __attribute__((packed)) wh_pktlog_hdr_v2_t;
94 
95 /*Rx stats specific structures. */
96 struct rx_attention {
97     u32 first_mpdu                      :  1; //[0]
98     u32 last_mpdu                       :  1; //[1]
99     u32 reserved1                       :  6; //[7:2]
100     u32 mgmt_type                       :  1; //[8]
101     u32 ctrl_type                       :  1; //[9]
102     u32 reserved2                       :  6; //[15:10]
103     u32 overflow_err                    :  1; //[16]
104     u32 msdu_length_err                 :  1; //[17]
105     u32 tcp_udp_chksum_fail             :  1; //[18]
106     u32 ip_chksum_fail                  :  1; //[19]
107     u32 reserved3                       :  7; //[26:20]
108     u32 mpdu_length_err                 :  1; //[27]
109     u32 tkip_mic_err                    :  1; //[28]
110     u32 decrypt_err                     :  1; //[29]
111     u32 fcs_err                         :  1; //[30]
112     u32 msdu_done                       :  1; //[31]
113 } __attribute__((packed));
114 
115 struct rx_mpdu_start {
116     u32 reserved1                       : 13; //[12:0]
117     u32 encrypted                       :  1; //[13]
118     u32 retry                           :  1; //[14]
119     u32 reserved2                       :  1; //[15]
120     u32 seq_num                         : 12; //[27:16]
121     u32 reserved3                       :  4; //[31:28]
122     u32 reserved4;
123     u32 reserved5                       : 28; //[27:0]
124     u32 tid                             :  4; //[31:28]
125 } __attribute__((packed));
126 
127 /*Indicates the decap-format of the packet*/
128 enum {
129     RAW=0,      // RAW: No decapsulation
130     NATIVEWIFI,
131     ETHERNET2,  // (DIX)
132     ETHERNET    // (SNAP/LLC)
133 };
134 
135 struct rx_msdu_start {
136     u32 reserved1[2];
137     u32 reserved2                       :  8; //[7:0]
138     u32 decap_format                    :  2; //[9:8]
139     u32 reserved3                       : 22; //[31:10]
140 } __attribute__((packed));
141 
142 struct rx_msdu_end {
143     u32 reserved1[4];
144     u32 reserved2                       : 15;
145     u32 last_msdu                       :  1; //[15]
146     u32 reserved3                       : 16; //[31:16]
147 } __attribute__((packed));
148 
149 struct rx_mpdu_end {
150     u32 reserved1                       : 13; //[12:0]
151     u32 overflow_err                    :  1; //[13]
152     u32 last_mpdu                       :  1; //[14]
153     u32 post_delim_err                  :  1; //[15]
154     u32 reserved2                       : 12; //[27:16]
155     u32 mpdu_length_err                 :  1; //[28]
156     u32 tkip_mic_err                    :  1; //[29]
157     u32 decrypt_err                     :  1; //[30]
158     u32 fcs_err                         :  1; //[31]
159 } __attribute__((packed));
160 
161 #define PREAMBLE_L_SIG_RATE     0x04
162 #define PREAMBLE_VHT_SIG_A_1    0x08
163 #define PREAMBLE_VHT_SIG_A_2    0x0c
164 
165 /* Wifi Logger preamble */
166 #define WL_PREAMBLE_CCK  0
167 #define WL_PREAMBLE_OFDM 1
168 #define WL_PREAMBLE_HT   2
169 #define WL_PREAMBLE_VHT  3
170 
171 #define BITMASK(x) ((1<<(x)) - 1 )
172 #define MAX_BA_WINDOW_SIZE 64
173 #define SEQ_NUM_RANGE 4096
174 #define BITMAP_VAR_SIZE 32
175 
176 /* Contains MCS related stats */
177 struct rx_ppdu_start {
178     u32 reserved1[4];
179     u32 rssi_comb                       :  8; //[7:0]
180     u32 reserved2                       : 24; //[31:8]
181     u32 l_sig_rate                      :  4; //[3:0]
182     u32 l_sig_rate_select               :  1; //[4]
183     u32 reserved3                       : 19; //[23:5]
184     u32 preamble_type                   :  8; //[31:24]
185     u32 ht_sig_vht_sig_a_1              : 24; //[23:0]
186     u32 reserved4                       :  8; //[31:24]
187     u32 ht_sig_vht_sig_a_2              : 24; //[23:0]
188     u32 reserved5                       :  8; //[31:25]
189     u32 reserved6[2];
190 } __attribute__((packed));
191 
192 struct rx_ppdu_end {
193     u32 reserved1[16];
194     u32 tsf_timestamp;
195     u32 reserved2[5];
196 } __attribute__((packed));
197 
198 #define MAX_MSDUS_PER_MPDU 3
199 #define MAX_RXMPDUS_PER_AMPDU 64
200 #define RX_HTT_HDR_STATUS_LEN 64
201 typedef struct {
202     struct rx_attention attention;
203     u32 reserved1;
204     struct rx_mpdu_start mpdu_start;
205     struct rx_msdu_start msdu_start;
206     struct rx_msdu_end   msdu_end;
207     struct rx_mpdu_end   mpdu_end;
208     struct rx_ppdu_start ppdu_start;
209     struct rx_ppdu_end   ppdu_end;
210     char rx_hdr_status[RX_HTT_HDR_STATUS_LEN];
211 }__attribute__((packed)) rb_pkt_stats_t;
212 
213 /*Tx stats specific structures. */
214 struct ppdu_status {
215     u32 ba_start_seq_num                : 12; //[11:0]
216     u32 reserved1                       :  3; //[14:12]
217     u32 ba_status                       :  1; //[15]
218     u32 reserved2                       : 15; //[30:16]
219     u32 tx_ok                           :  1; //[31]
220     u32 ba_bitmap_31_0                  : 32; //[31:0]
221     u32 ba_bitmap_63_32                 : 32; //[31:0]
222     u32 reserved3[8];
223     u32 ack_rssi_ave                    :  8; //[7:0]
224     u32 reserved4                       : 16; //[23:8]
225     u32 total_tries                     :  5; //[28:24]
226     u32 reserved5                       :  3; //[31:29]
227     u32 reserved6[4];
228 } __attribute__((packed));
229 
230 /*Contains tx timestamp*/
231 struct try_status {
232     u32 timestamp                       : 23; //[22:0]
233     u32 reserved1                       :  1; //[23]
234     u32 series                          :  1; //[24]
235     u32 reserved2                       :  3; //[27:25]
236     u32 packet_bw                       :  2; //[29:28]
237     u32 reserved3                       :  1; //[30]
238     u32 tx_packet                       :  1; //[31]
239 } __attribute__((packed));
240 
241 struct try_list {
242     struct try_status try_st[16];
243 } __attribute__((packed));
244 
245 
246 struct tx_ppdu_end {
247     struct try_list try_list;
248     struct ppdu_status stat;
249 } __attribute__((packed));
250 
251 /*Tx MCS and data rate ralated stats */
252 struct series_bw {
253     u32 reserved1                       : 28; //[27:0]
254     u32 short_gi                        :  1; //[28]
255     u32 reserved2                       :  3; //[31:29]
256     u32 reserved3                       : 24; //[23:21]
257     u32 rate                            :  4; //[27:24]
258     u32 nss                             :  2; //[29:28]
259     u32 preamble_type                   :  2; //[31:30]
260     u32 reserved4[2];
261 } __attribute__((packed));
262 
263 enum tx_bw {
264     BW_20_MHZ,
265     BW_40_MHZ,
266     BW_80_MHZ,
267     BW_160_MHZ
268 };
269 
270 #define DATA_PROTECTED 14
271 struct tx_ppdu_start {
272     u32 reserved1[2];
273     u32 start_seq_num                   : 12; //[11:0]
274     u32 reserved2                       : 20; //[31:12]
275     u32 seqnum_bitmap_31_0              : 32; //[31:0]
276     u32 seqnum_bitmap_63_32             : 32; //[31:0]
277     u32 reserved3[8];
278     u32 reserved4                       : 15; //[14:0]
279     u32 ampdu                           :  1; //[15]
280     u32 no_ack                          :  1; //[16]
281     u32 reserved5                       : 15; //[31:17]
282     u32 reserved6                       : 16; //[15:0]
283     u32 frame_control                   : 16; //[31:16]
284     u32 reserved7                       : 16; //[23:21]
285     u32 qos_ctl                         : 16; //[31:16]
286     u32 reserved8[4];
287     u32 reserved9                       : 24; //[23:21]
288     u32 valid_s0_bw20                   :  1; //[24]
289     u32 valid_s0_bw40                   :  1; //[25]
290     u32 valid_s0_bw80                   :  1; //[26]
291     u32 valid_s0_bw160                  :  1; //[27]
292     u32 valid_s1_bw20                   :  1; //[28]
293     u32 valid_s1_bw40                   :  1; //[29]
294     u32 valid_s1_bw80                   :  1; //[30]
295     u32 valid_s1_bw160                  :  1; //[31]
296     struct series_bw s0_bw20;
297     struct series_bw s0_bw40;
298     struct series_bw s0_bw80;
299     struct series_bw s0_bw160;
300     struct series_bw s1_bw20;
301     struct series_bw s1_bw40;
302     struct series_bw s1_bw80;
303     struct series_bw s1_bw160;
304     u32 reserved10[3];
305 } __attribute__((packed));
306 
307 #define PKTLOG_MAX_TXCTL_WORDS 57 /* +2 words for bitmap */
308 typedef struct {
309     u32 reserved1[3];
310     union {
311         u32 txdesc_ctl[PKTLOG_MAX_TXCTL_WORDS];
312         struct tx_ppdu_start ppdu_start;
313     }u;
314 } __attribute__((packed)) wh_pktlog_txctl;
315 
316 /* Required stats are spread across multiple
317  * events(PKTLOG_TYPE_TX_CTRL and PKTLOG_TYPE_TX_STAT here).
318  * Need to aggregate the stats collected in each event and write to the
319  * ring buffer only after receiving all the expected stats.
320  * Need to preserve the stats in hal_info till then and use tx_stats_events
321  * flag to track the events.
322  * prev_seq_no: Can used to track the events that come from driver and identify
323  * if any event is missed.
324  */
325 
326 #define RING_BUF_ENTRY_SIZE 512
327 #define PKT_STATS_BUF_SIZE 128
328 struct pkt_stats_s {
329     u8 tx_stats_events;
330     /* TODO: Need to handle the case if size of the stats are more
331      * than 512 bytes. Currently, the tx size is 34 bytes and ring buffer entry
332      * size is 12 bytes.
333      */
334     u8 tx_stats[PKT_STATS_BUF_SIZE];
335     u8 num_msdu;
336     u16 start_seq_num;
337     u16 ba_seq_num;
338     u32 ba_bitmap_31_0;
339     u32 ba_bitmap_63_32;
340     u32 tx_seqnum_bitmap_31_0;
341     u32 tx_seqnum_bitmap_63_32;
342     u32 shifted_bitmap_31_0;
343     u32 shifted_bitmap_63_32;
344     bool isBlockAck;
345     u8 tx_bandwidth;
346     u8 series;
347 };
348 
349 typedef union {
350     struct {
351         u16 rate                            :  4;
352         u16 nss                             :  2;
353         u16 preamble                        :  2;
354         u16 bw                              :  2;
355         u16 short_gi                        :  1;
356         u16 reserved                        :  5;
357     } mcs_s;
358     u16 mcs;
359 } MCS;
360 
361 typedef struct {
362     MCS RxMCS;
363     u16 last_transmit_rate;
364     u16 rssi;
365     u32 timestamp;
366     u8  tid;
367 } rx_aggr_stats;
368 
369 
370 typedef struct drv_msg_s
371 {
372     u16 length;
373     u16 event_type;
374     u32 timestamp_low;
375     u32 timestamp_high;
376     union {
377         struct {
378             u32 version;
379             u32 msg_seq_no;
380             u32 payload_len;
381             u8  payload[0];
382         } __attribute__((packed)) pkt_stats_event;
383     } u;
384 } __attribute__((packed)) drv_msg_t;
385 
386 typedef enum {
387     START_MONITOR = 1,
388     STOP_MONITOR,
389     TX_MGMT_PKT,
390     TX_DATA_PKT,
391     RX_MGMT_PKT,
392     RX_DATA_PKT,
393 } pktdump_event_type;
394 
395 typedef struct {
396     u8 status;
397     u8 type;
398     u32 driver_ts;
399     u16 fw_ts;
400 } __attribute__((packed)) pktdump_hdr;
401 
402 typedef struct {
403     frame_type payload_type;
404     u32 driver_timestamp_usec;
405     u32 firmware_timestamp_usec;
406     size_t frame_len;
407     char *frame_content;
408 } frame_info_i;
409 
410 typedef struct {
411     // Prefix of MD5 hash of |frame_inf.frame_content|. If frame
412     // content is not provided, prefix of MD5 hash over the same data
413     // that would be in frame_content, if frame content were provided.
414     char md5_prefix[MD5_PREFIX_LEN];  // Prefix of MD5 hash of packet bytes
415     wifi_tx_packet_fate fate;
416     frame_info_i frame_inf;
417 } wifi_tx_report_i;
418 
419 typedef struct {
420     // Prefix of MD5 hash of |frame_inf.frame_content|. If frame
421     // content is not provided, prefix of MD5 hash over the same data
422     // that would be in frame_content, if frame content were provided.
423     char md5_prefix[MD5_PREFIX_LEN];
424     wifi_rx_packet_fate fate;
425     frame_info_i frame_inf;
426 } wifi_rx_report_i;
427 
428 typedef struct {
429     wifi_tx_report_i tx_fate_stats[MAX_FATE_LOG_LEN];
430     size_t n_tx_stats_collected;
431     wifi_rx_report_i rx_fate_stats[MAX_FATE_LOG_LEN];
432     size_t n_rx_stats_collected;
433 } packet_fate_monitor_info;
434 
435 #endif
436