1 /*
2  * Copyright (c) 2020 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /**
21  * DOC: Declare APIs which shall be used for monitor thread access.
22  */
23 
24 #ifndef _WLAN_PKT_CAPTURE_MON_THREAD_H_
25 #define _WLAN_PKT_CAPTURE_MON_THREAD_H_
26 
27 #include "wlan_pkt_capture_main.h"
28 
29 #define PKT_CAPTURE_RX_POST_EVENT 0x01
30 #define PKT_CAPTURE_RX_SUSPEND_EVENT 0x02
31 #define PKT_CAPTURE_RX_SHUTDOWN_EVENT 0x04
32 #define PKT_CAPTURE_REGISTER_EVENT 0x08
33 
34 /*
35  * Maximum number of packets to be allocated for
36  * Packet Capture Monitor thread.
37  */
38 #define MAX_MON_PKT_SIZE 4000
39 
40 /* timeout in msec to wait for mon thread to suspend */
41 #define PKT_CAPTURE_SUSPEND_TIMEOUT 200
42 
43 typedef void (*pkt_capture_mon_thread_cb)(
44 			void *context, void *ppdev, void *monpkt,
45 			uint8_t vdev_id, uint8_t tid,
46 			uint16_t status, bool pkt_format,
47 			uint8_t *bssid,
48 			uint8_t tx_retry_cnt);
49 
50 /**
51  * struct pkt_capture_mon_pkt - mon packet wrapper for mon data from TXRX
52  * @list: List for storing mon packets
53  * @context: Callback context
54  * @pdev: pointer to pdev handle
55  * @monpkt: Mon skb
56  * @vdev_id: Vdev id to which this packet is destined
57  * @tid: Tid of mon packet
58  * @status: Tx packet status
59  * @pkt_format: Mon packet format, 0 = 802.3 format , 1 = 802.11 format
60  * @bssid: bssid
61  * @tx_retry_cnt: tx retry count
62  * @callback: Mon callback
63  */
64 struct pkt_capture_mon_pkt {
65 	struct list_head list;
66 	void *context;
67 	void *pdev;
68 	void *monpkt;
69 	uint8_t vdev_id;
70 	uint8_t tid;
71 	uint16_t status;
72 	bool pkt_format;
73 	uint8_t bssid[QDF_MAC_ADDR_SIZE];
74 	uint8_t tx_retry_cnt;
75 	pkt_capture_mon_thread_cb callback;
76 };
77 
78 /**
79  * struct pkt_capture_mon_context - packet capture mon thread context
80  * @mon_thread_lock: MON thread lock
81  * @mon_thread: MON thread handle
82  * @mon_start_event: Handle of Event for MON thread to signal startup
83  * @suspend_mon_event: Completion to suspend packet capture MON thread
84  * @resume_mon_event: Completion to resume packet capture MON thread
85  * @mon_shutdown: Completion for packet capture MON thread shutdown
86  * @mon_register_event: Completion for packet capture register
87  * @mon_wait_queue: Waitq for packet capture MON thread
88  * @mon_event_flag: Mon event flag
89  * @mon_thread_queue: MON buffer queue
90  * @mon_queue_lock: Spinlock to synchronize between tasklet and thread
91  * @mon_pkt_freeq_lock: Lock to synchronize free buffer queue access
92  * @mon_pkt_freeq: Free message queue for packet capture MON processing
93  * @is_mon_thread_suspended: flag to check mon thread suspended or not
94  */
95 struct pkt_capture_mon_context {
96 	/* MON thread lock */
97 	spinlock_t mon_thread_lock;
98 	struct task_struct *mon_thread;
99 	struct completion mon_start_event;
100 	struct completion suspend_mon_event;
101 	struct completion resume_mon_event;
102 	struct completion mon_shutdown;
103 	struct completion mon_register_event;
104 	wait_queue_head_t mon_wait_queue;
105 	unsigned long mon_event_flag;
106 	struct list_head mon_thread_queue;
107 
108 	/* Spinlock to synchronize between tasklet and thread */
109 	spinlock_t mon_queue_lock;
110 
111 	/* Lock to synchronize free buffer queue access */
112 	spinlock_t mon_pkt_freeq_lock;
113 
114 	struct list_head mon_pkt_freeq;
115 	bool is_mon_thread_suspended;
116 };
117 
118 /**
119  * struct radiotap_header - base radiotap header
120  * @it_version: radiotap version, always 0
121  * @it_pad: padding (or alignment)
122  * @it_len: overall radiotap header length
123  * @it_present: (first) present word
124  */
125 struct radiotap_header {
126 	uint8_t it_version;
127 	uint8_t it_pad;
128 	__le16 it_len;
129 	__le32 it_present;
130 } __packed;
131 
132 /**
133  * pkt_capture_suspend_mon_thread() - suspend packet capture mon thread
134  * @vdev: pointer to vdev object manager
135  *
136  * Return: 0 on success, -EINVAL on failure
137  */
138 int pkt_capture_suspend_mon_thread(struct wlan_objmgr_vdev *vdev);
139 
140 /**
141  * pkt_capture_resume_mon_thread() - resume packet capture mon thread
142  * @vdev: pointer to vdev object manager
143  *
144  * Resume packet capture MON thread by completing RX thread resume event.
145  *
146  * Return: None
147  */
148 void pkt_capture_resume_mon_thread(struct wlan_objmgr_vdev *vdev);
149 
150 /**
151  * pkt_capture_drop_monpkt() - API to drop pending mon packets
152  * @mon_ctx: pointer to packet capture mon context
153  *
154  * This api drops all the pending packets in the queue.
155  *
156  * Return: None
157  */
158 void pkt_capture_drop_monpkt(struct pkt_capture_mon_context *mon_ctx);
159 
160 /**
161  * pkt_capture_indicate_monpkt() - API to Indicate rx data packet
162  * @vdev: pointer to vdev object manager
163  * @pkt: MON pkt pointer containing to mon data message buffer
164  *
165  * Return: None
166  */
167 void pkt_capture_indicate_monpkt(struct wlan_objmgr_vdev *vdev,
168 				 struct pkt_capture_mon_pkt *pkt);
169 
170 /**
171  * pkt_capture_wakeup_mon_thread() - wakeup packet capture mon thread
172  * @vdev: pointer to vdev object
173  *
174  * This api wake up pkt_capture_mon_thread() to process pkt.
175  *
176  * Return: None
177  */
178 void pkt_capture_wakeup_mon_thread(struct wlan_objmgr_vdev *vdev);
179 
180 /**
181  * pkt_capture_close_mon_thread() - close packet capture MON thread
182  * @mon_ctx: pointer to packet capture mon context
183  *
184  * This api closes packet capture MON thread.
185  *
186  * Return: None
187  */
188 void pkt_capture_close_mon_thread(struct pkt_capture_mon_context *mon_ctx);
189 
190 /**
191  * pkt_capture_open_mon_thread() - open packet capture MON thread
192  * @mon_ctx: pointer to packet capture mon context
193  *
194  * This api opens the packet capture MON thread.
195  *
196  * Return: QDF_STATUS
197  */
198 QDF_STATUS
199 pkt_capture_open_mon_thread(struct pkt_capture_mon_context *mon_ctx);
200 
201 /**
202  * pkt_capture_alloc_mon_thread() - alloc resources for
203  * packet capture MON thread
204  * @mon_ctx: pointer to packet capture mon context
205  *
206  * This api alloc resources for packet capture MON thread.
207  *
208  * Return: QDF_STATUS
209  */
210 QDF_STATUS
211 pkt_capture_alloc_mon_thread(struct pkt_capture_mon_context *mon_ctx);
212 
213 /**
214  * pkt_capture_alloc_mon_pkt() - API to return next available mon packet
215  * @vdev: pointer to vdev object manager
216  *
217  * This api returns next available mon packet buffer used for mon data
218  * processing.
219  *
220  * Return: Pointer to pkt_capture_mon_pkt
221  */
222 struct pkt_capture_mon_pkt *
223 pkt_capture_alloc_mon_pkt(struct wlan_objmgr_vdev *vdev);
224 
225 /**
226  * pkt_capture_free_mon_pkt_freeq() - free mon packet free queue
227  * @mon_ctx: pointer to packet capture mon context
228  *
229  * This API does mem free of the buffers available in free mon packet
230  * queue which is used for mon Data processing.
231  *
232  * Return: None
233  */
234 void pkt_capture_free_mon_pkt_freeq(struct pkt_capture_mon_context *mon_ctx);
235 #endif /* _WLAN_PKT_CAPTURE_MON_THREAD_H_ */
236