xref: /wlan-dirver/qca-wifi-host-cmn/umac/green_ap/core/src/wlan_green_ap_main.c (revision 0626a4da6c07f30da06dd6747e8cc290a60371d8)
1 /*
2  * Copyright (c) 2017-2018 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /**
20  * DOC: This file contains main green ap function definitions
21  */
22 
23 #include "wlan_green_ap_main_i.h"
24 
25 /*
26  * wlan_green_ap_ant_ps_reset() - Reset function
27  * @green_ap - green ap context
28  *
29  * Reset fiunction, so that Antenna Mask can come into effect.
30  *                This applies for only few of the hardware chips
31  *
32  * Return: QDF_STATUS
33  */
34 static QDF_STATUS wlan_green_ap_ant_ps_reset
35 		(struct wlan_pdev_green_ap_ctx *green_ap_ctx)
36 {
37 	struct wlan_lmac_if_green_ap_tx_ops *green_ap_tx_ops;
38 	struct wlan_objmgr_pdev *pdev;
39 
40 	if (!green_ap_ctx) {
41 		green_ap_err("green ap context obtained is NULL");
42 		return QDF_STATUS_E_FAILURE;
43 	}
44 	pdev = green_ap_ctx->pdev;
45 
46 	green_ap_tx_ops = wlan_psoc_get_green_ap_tx_ops(green_ap_ctx);
47 	if (!green_ap_tx_ops) {
48 		green_ap_err("green ap tx ops obtained are NULL");
49 		return QDF_STATUS_E_FAILURE;
50 	}
51 
52 	if (!green_ap_tx_ops->reset_dev)
53 		return QDF_STATUS_SUCCESS;
54 
55 	/*
56 	 * Add protection against green AP enabling interrupts
57 	 * when not valid or no VAPs exist
58 	 */
59 	if (wlan_util_is_vdev_active(pdev, WLAN_GREEN_AP_ID) ==
60 						QDF_STATUS_SUCCESS)
61 		green_ap_tx_ops->reset_dev(pdev);
62 	else
63 		green_ap_err("Green AP tried to enable IRQs when invalid");
64 
65 	return QDF_STATUS_SUCCESS;
66 }
67 
68 struct wlan_lmac_if_green_ap_tx_ops *
69 wlan_psoc_get_green_ap_tx_ops(struct wlan_pdev_green_ap_ctx *green_ap_ctx)
70 {
71 	struct wlan_objmgr_psoc *psoc;
72 	struct wlan_objmgr_pdev *pdev = green_ap_ctx->pdev;
73 
74 	if (!pdev) {
75 		green_ap_err("pdev context obtained is NULL");
76 		return NULL;
77 	}
78 
79 	psoc = wlan_pdev_get_psoc(pdev);
80 	if (!psoc) {
81 		green_ap_err("pdev context obtained is NULL");
82 		return NULL;
83 	}
84 
85 	return &((psoc->soc_cb.tx_ops.green_ap_tx_ops));
86 }
87 
88 bool wlan_is_egap_enabled(struct wlan_pdev_green_ap_ctx *green_ap_ctx)
89 {
90 	struct wlan_green_ap_egap_params *egap_params;
91 
92 	if (!green_ap_ctx) {
93 		green_ap_err("green ap context passed is NULL");
94 		return QDF_STATUS_E_INVAL;
95 	}
96 	egap_params = &green_ap_ctx->egap_params;
97 
98 	if (egap_params->fw_egap_support &&
99 	    egap_params->host_enable_egap &&
100 	    egap_params->egap_feature_flags)
101 		return true;
102 	return false;
103 }
104 qdf_export_symbol(wlan_is_egap_enabled);
105 
106 /**
107  * wlan_green_ap_ps_event_state_update() - Update PS state and event
108  * @pdev: pdev pointer
109  * @state: ps state
110  * @event: ps event
111  *
112  * @Return: Success or Failure
113  */
114 static QDF_STATUS wlan_green_ap_ps_event_state_update(
115 			struct wlan_pdev_green_ap_ctx *green_ap_ctx,
116 			enum wlan_green_ap_ps_state state,
117 			enum wlan_green_ap_ps_event event)
118 {
119 	if (!green_ap_ctx) {
120 		green_ap_err("green ap context obtained is NULL");
121 		return QDF_STATUS_E_FAILURE;
122 	}
123 
124 	green_ap_ctx->ps_state = state;
125 	green_ap_ctx->ps_event = event;
126 
127 	return QDF_STATUS_SUCCESS;
128 }
129 
130 QDF_STATUS wlan_green_ap_state_mc(struct wlan_pdev_green_ap_ctx *green_ap_ctx,
131 				  enum wlan_green_ap_ps_event event)
132 {
133 	struct wlan_lmac_if_green_ap_tx_ops *green_ap_tx_ops;
134 	uint8_t pdev_id;
135 
136 	/*
137 	 * Remove the assignments once channel info is available for
138 	 * converged component.
139 	 */
140 	uint16_t channel = 1;
141 	uint32_t channel_flags = 1;
142 
143 	if (!green_ap_ctx) {
144 		green_ap_err("green ap context obtained is NULL");
145 		return QDF_STATUS_E_FAILURE;
146 	}
147 
148 	if (!green_ap_ctx->pdev) {
149 		green_ap_err("pdev obtained is NULL");
150 		return QDF_STATUS_E_FAILURE;
151 	}
152 	pdev_id = wlan_objmgr_pdev_get_pdev_id(green_ap_ctx->pdev);
153 
154 	green_ap_tx_ops = wlan_psoc_get_green_ap_tx_ops(green_ap_ctx);
155 	if (!green_ap_tx_ops) {
156 		green_ap_err("green ap tx ops obtained are NULL");
157 		return QDF_STATUS_E_FAILURE;
158 	}
159 
160 	if (!green_ap_tx_ops->ps_on_off_send) {
161 		green_ap_err("tx op for sending enbale/disable green ap is NULL");
162 		return QDF_STATUS_E_FAILURE;
163 	}
164 
165 	qdf_spin_lock_bh(&green_ap_ctx->lock);
166 
167 	if (green_ap_tx_ops->get_current_channel)
168 		channel = green_ap_tx_ops->get_current_channel(
169 						green_ap_ctx->pdev);
170 
171 	if (green_ap_tx_ops->get_current_channel_flags)
172 		channel_flags = green_ap_tx_ops->get_current_channel_flags(
173 							green_ap_ctx->pdev);
174 
175 	/* handle the green ap ps event */
176 	switch (event) {
177 	case WLAN_GREEN_AP_ADD_STA_EVENT:
178 		green_ap_ctx->num_nodes++;
179 		break;
180 
181 	case WLAN_GREEN_AP_DEL_STA_EVENT:
182 		if (green_ap_ctx->num_nodes)
183 			green_ap_ctx->num_nodes--;
184 		break;
185 
186 	case WLAN_GREEN_AP_PS_START_EVENT:
187 	case WLAN_GREEN_AP_PS_STOP_EVENT:
188 	case WLAN_GREEN_AP_PS_ON_EVENT:
189 	case WLAN_GREEN_AP_PS_WAIT_EVENT:
190 		break;
191 
192 	default:
193 		green_ap_err("Invalid event: %d", event);
194 		break;
195 	}
196 
197 	green_ap_debug("Green-AP event: %d, state: %d, num_nodes: %d",
198 		       event, green_ap_ctx->ps_state, green_ap_ctx->num_nodes);
199 
200 	/* Confirm that power save is enabled before doing state transitions */
201 	if (!green_ap_ctx->ps_enable) {
202 		green_ap_debug("Green-AP is disabled");
203 		if (green_ap_ctx->ps_state == WLAN_GREEN_AP_PS_ON_STATE) {
204 			if (green_ap_tx_ops->ps_on_off_send(green_ap_ctx->pdev,
205 								false, pdev_id))
206 				green_ap_err("failed to set green ap mode");
207 			wlan_green_ap_ant_ps_reset(green_ap_ctx);
208 		}
209 		wlan_green_ap_ps_event_state_update(
210 				green_ap_ctx,
211 				WLAN_GREEN_AP_PS_IDLE_STATE,
212 				WLAN_GREEN_AP_PS_WAIT_EVENT);
213 		goto done;
214 	}
215 
216 	/* handle the green ap ps state */
217 	switch (green_ap_ctx->ps_state) {
218 	case WLAN_GREEN_AP_PS_IDLE_STATE:
219 		if (green_ap_ctx->num_nodes) {
220 			/* Active nodes present, Switchoff the power save */
221 			green_ap_info("Transition to OFF from IDLE");
222 			wlan_green_ap_ps_event_state_update(
223 					green_ap_ctx,
224 					WLAN_GREEN_AP_PS_OFF_STATE,
225 					WLAN_GREEN_AP_PS_WAIT_EVENT);
226 		} else {
227 			/* No Active nodes, get into power save */
228 			green_ap_info("Transition to WAIT from IDLE");
229 			wlan_green_ap_ps_event_state_update(
230 					green_ap_ctx,
231 					WLAN_GREEN_AP_PS_WAIT_STATE,
232 					WLAN_GREEN_AP_PS_WAIT_EVENT);
233 			qdf_timer_start(&green_ap_ctx->ps_timer,
234 					green_ap_ctx->ps_trans_time * 1000);
235 		}
236 		break;
237 
238 	case WLAN_GREEN_AP_PS_OFF_STATE:
239 		if (!green_ap_ctx->num_nodes) {
240 			green_ap_info("Transition to WAIT from OFF");
241 			wlan_green_ap_ps_event_state_update(
242 						green_ap_ctx,
243 						WLAN_GREEN_AP_PS_WAIT_STATE,
244 						WLAN_GREEN_AP_PS_WAIT_EVENT);
245 			qdf_timer_start(&green_ap_ctx->ps_timer,
246 					green_ap_ctx->ps_trans_time * 1000);
247 		}
248 		break;
249 
250 	case WLAN_GREEN_AP_PS_WAIT_STATE:
251 		if (!green_ap_ctx->num_nodes) {
252 			if ((channel == 0) || (channel_flags == 0)) {
253 				/*
254 				 * Stay in the current state and restart the
255 				 * timer to check later.
256 				 */
257 				qdf_timer_start(&green_ap_ctx->ps_timer,
258 					green_ap_ctx->ps_on_time * 1000);
259 			} else {
260 				wlan_green_ap_ps_event_state_update(
261 						green_ap_ctx,
262 						WLAN_GREEN_AP_PS_ON_STATE,
263 						WLAN_GREEN_AP_PS_WAIT_EVENT);
264 
265 				green_ap_info("Transition to ON from WAIT");
266 				green_ap_tx_ops->ps_on_off_send(
267 					green_ap_ctx->pdev, true, pdev_id);
268 				wlan_green_ap_ant_ps_reset(green_ap_ctx);
269 
270 				if (green_ap_ctx->ps_on_time)
271 					qdf_timer_start(&green_ap_ctx->ps_timer,
272 						green_ap_ctx->ps_on_time * 1000);
273 			}
274 		} else {
275 			green_ap_info("Transition to OFF from WAIT");
276 			qdf_timer_stop(&green_ap_ctx->ps_timer);
277 			wlan_green_ap_ps_event_state_update(
278 						green_ap_ctx,
279 						WLAN_GREEN_AP_PS_OFF_STATE,
280 						WLAN_GREEN_AP_PS_WAIT_EVENT);
281 		}
282 		break;
283 
284 	case WLAN_GREEN_AP_PS_ON_STATE:
285 		if (green_ap_ctx->num_nodes) {
286 			qdf_timer_stop(&green_ap_ctx->ps_timer);
287 			if (green_ap_tx_ops->ps_on_off_send(
288 					green_ap_ctx->pdev, false, pdev_id)) {
289 				green_ap_err("Failed to set Green AP mode");
290 				goto done;
291 			}
292 			wlan_green_ap_ant_ps_reset(green_ap_ctx);
293 			green_ap_info("Transition to OFF from ON\n");
294 			wlan_green_ap_ps_event_state_update(
295 						green_ap_ctx,
296 						WLAN_GREEN_AP_PS_OFF_STATE,
297 						WLAN_GREEN_AP_PS_WAIT_EVENT);
298 		} else if ((green_ap_ctx->ps_event ==
299 					WLAN_GREEN_AP_PS_WAIT_EVENT) &&
300 			   (green_ap_ctx->ps_on_time)) {
301 			/* ps_on_time timeout, switch to ps wait */
302 			wlan_green_ap_ps_event_state_update(
303 						green_ap_ctx,
304 						WLAN_GREEN_AP_PS_WAIT_STATE,
305 						WLAN_GREEN_AP_PS_ON_EVENT);
306 
307 			if (green_ap_tx_ops->ps_on_off_send(
308 					green_ap_ctx->pdev, false, pdev_id)) {
309 				green_ap_err("Failed to set Green AP mode");
310 				goto done;
311 			}
312 
313 			wlan_green_ap_ant_ps_reset(green_ap_ctx);
314 			green_ap_info("Transition to WAIT from ON\n");
315 			qdf_timer_start(&green_ap_ctx->ps_timer,
316 					green_ap_ctx->ps_trans_time * 1000);
317 		}
318 		break;
319 
320 	default:
321 		green_ap_err("invalid state %d", green_ap_ctx->ps_state);
322 		wlan_green_ap_ps_event_state_update(
323 						green_ap_ctx,
324 						WLAN_GREEN_AP_PS_OFF_STATE,
325 						WLAN_GREEN_AP_PS_WAIT_EVENT);
326 		break;
327 	}
328 
329 done:
330 	qdf_spin_unlock_bh(&green_ap_ctx->lock);
331 	return QDF_STATUS_SUCCESS;
332 }
333 
334 void wlan_green_ap_timer_fn(void *pdev)
335 {
336 	struct wlan_pdev_green_ap_ctx *green_ap_ctx;
337 	struct wlan_objmgr_pdev *pdev_ctx = (struct wlan_objmgr_pdev *)pdev;
338 
339 	if (!pdev_ctx) {
340 		green_ap_err("pdev context passed is NULL");
341 		return;
342 	}
343 
344 	green_ap_ctx = wlan_objmgr_pdev_get_comp_private_obj(
345 			pdev_ctx, WLAN_UMAC_COMP_GREEN_AP);
346 	if (!green_ap_ctx) {
347 		green_ap_err("green ap context obtained is NULL");
348 		return;
349 	}
350 	wlan_green_ap_state_mc(green_ap_ctx, green_ap_ctx->ps_event);
351 }
352 
353 void wlan_green_ap_check_mode(struct wlan_objmgr_pdev *pdev,
354 		void *object,
355 		void *arg)
356 {
357 	struct wlan_objmgr_vdev *vdev = (struct wlan_objmgr_vdev *)object;
358 	uint8_t *flag = (uint8_t *)arg;
359 
360 	wlan_vdev_obj_lock(vdev);
361 	if (wlan_vdev_mlme_get_opmode(vdev) != QDF_SAP_MODE)
362 		*flag = 1;
363 
364 	wlan_vdev_obj_unlock(vdev);
365 }
366 
367