xref: /wlan-dirver/qca-wifi-host-cmn/umac/scan/core/src/wlan_scan_manager.c (revision 3149adf58a329e17232a4c0e58d460d025edd55a)
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: contains scan manager functionality
21  */
22 
23 #include <wlan_serialization_api.h>
24 #include <wlan_scan_ucfg_api.h>
25 #include <wlan_scan_tgt_api.h>
26 #include "wlan_scan_main.h"
27 #include "wlan_scan_manager.h"
28 #include "wlan_utility.h"
29 #ifdef FEATURE_WLAN_SCAN_PNO
30 #include <host_diag_core_event.h>
31 #endif
32 
33 QDF_STATUS
34 scm_scan_free_scan_request_mem(struct scan_start_request *req)
35 {
36 	void *ie;
37 
38 	if (!req) {
39 		scm_err("null request");
40 		QDF_ASSERT(0);
41 		return QDF_STATUS_E_FAILURE;
42 	}
43 	scm_info("freed scan request: 0x%pK, scan_id: %d, requester: %d",
44 		req, req->scan_req.scan_id, req->scan_req.scan_req_id);
45 	/* Free vendor(extra) ie */
46 	ie = req->scan_req.extraie.ptr;
47 	if (ie) {
48 		req->scan_req.extraie.ptr = NULL;
49 		req->scan_req.extraie.len = 0;
50 		qdf_mem_free(ie);
51 	}
52 
53 	/* Free htcap ie */
54 	ie = req->scan_req.htcap.ptr;
55 	if (ie) {
56 		req->scan_req.htcap.len = 0;
57 		req->scan_req.htcap.ptr = NULL;
58 		qdf_mem_free(ie);
59 	}
60 
61 	/* Free vhtcap ie */
62 	ie = req->scan_req.vhtcap.ptr;
63 	if (ie) {
64 		req->scan_req.vhtcap.len = 0;
65 		req->scan_req.vhtcap.ptr = NULL;
66 		qdf_mem_free(ie);
67 	}
68 	/* free scan_start_request memory */
69 	qdf_mem_free(req);
70 
71 	return QDF_STATUS_SUCCESS;
72 }
73 
74 static QDF_STATUS
75 scm_scan_get_pdev_global_event_handlers(struct scan_event_listeners *listeners,
76 		struct pdev_scan_ev_handler *pdev_ev_handler)
77 {
78 	uint32_t i;
79 	struct cb_handler *cb_handlers  = &(pdev_ev_handler->cb_handlers[0]);
80 
81 	for (i = 0; i < MAX_SCAN_EVENT_HANDLERS_PER_PDEV; i++, cb_handlers++) {
82 		if ((cb_handlers->func) &&
83 		    (listeners->count < MAX_SCAN_EVENT_LISTENERS)) {
84 			listeners->cb[listeners->count].func =
85 				cb_handlers->func;
86 			listeners->cb[listeners->count].arg =
87 				cb_handlers->arg;
88 			listeners->count++;
89 		}
90 	}
91 
92 	return QDF_STATUS_SUCCESS;
93 }
94 
95 static QDF_STATUS
96 scm_scan_get_requester_event_handler(struct scan_event_listeners *listeners,
97 		struct scan_requester_info *requesters,
98 		wlan_scan_requester requester_id)
99 {
100 	uint32_t idx;
101 	struct cb_handler *ev_handler;
102 
103 	idx = requester_id & WLAN_SCAN_REQUESTER_ID_PREFIX;
104 	if (idx != WLAN_SCAN_REQUESTER_ID_PREFIX)
105 		return QDF_STATUS_SUCCESS;
106 
107 	idx = requester_id & WLAN_SCAN_REQUESTER_ID_MASK;
108 	if (idx < WLAN_MAX_REQUESTORS) {
109 		ev_handler = &(requesters[idx].ev_handler);
110 		if (ev_handler->func) {
111 			if (listeners->count < MAX_SCAN_EVENT_LISTENERS) {
112 				listeners->cb[listeners->count].func =
113 							     ev_handler->func;
114 				listeners->cb[listeners->count].arg =
115 							     ev_handler->arg;
116 				listeners->count++;
117 			}
118 		}
119 		return QDF_STATUS_SUCCESS;
120 	} else {
121 		scm_err("invalid requester id");
122 		return QDF_STATUS_E_INVAL;
123 	}
124 
125 }
126 
127 static void scm_scan_post_event(struct wlan_objmgr_vdev *vdev,
128 		struct scan_event *event)
129 {
130 	uint32_t i = 0;
131 	struct wlan_scan_obj *scan;
132 	struct pdev_scan_ev_handler *pdev_ev_handler;
133 	struct cb_handler *cb_handlers;
134 	struct scan_requester_info *requesters;
135 	struct scan_event_listeners *listeners;
136 
137 	if (!vdev || !event) {
138 		scm_err("vdev: 0x%pK, event: 0x%pK", vdev, event);
139 		return;
140 	}
141 	if (!event->requester) {
142 		scm_err("invalid requester id");
143 		QDF_ASSERT(0);
144 	}
145 	scm_info("vdev: 0x%pK, event: 0x%pK", vdev, event);
146 
147 	scan = wlan_vdev_get_scan_obj(vdev);
148 	pdev_ev_handler = wlan_vdev_get_pdev_scan_ev_handlers(vdev);
149 	cb_handlers = &(pdev_ev_handler->cb_handlers[0]);
150 	requesters = scan->requesters;
151 
152 	scm_info("vdev: %d, type: %d, reason: %d, freq: %d, req: %d, scanid: %d",
153 		event->vdev_id, event->type, event->reason, event->chan_freq,
154 		event->requester, event->scan_id);
155 
156 	listeners = qdf_mem_malloc(sizeof(*listeners));
157 	if (!listeners) {
158 		scm_warn("couldn't allocate listeners list");
159 		return;
160 	}
161 
162 	/* initialize number of listeners */
163 	listeners->count = 0;
164 
165 	/*
166 	 * Initiator of scan request decides which all scan events
167 	 * he is interested in and FW will send only those scan events
168 	 * to host driver.
169 	 * All the events received by scan module will be notified
170 	 * to all registered handlers.
171 	 */
172 
173 	qdf_spin_lock_bh(&scan->lock);
174 	/* find all global scan event handlers on this pdev */
175 	scm_scan_get_pdev_global_event_handlers(listeners, pdev_ev_handler);
176 	/* find owner who triggered this scan request */
177 	scm_scan_get_requester_event_handler(listeners, requesters,
178 			event->requester);
179 	qdf_spin_unlock_bh(&scan->lock);
180 
181 	/* notify all interested handlers */
182 	for (i = 0; i < listeners->count; i++) {
183 		scm_debug("func: 0x%pK, arg: 0x%pK",
184 			listeners->cb[i].func, listeners->cb[i].arg);
185 		listeners->cb[i].func(vdev, event, listeners->cb[i].arg);
186 	}
187 	qdf_mem_free(listeners);
188 }
189 
190 static QDF_STATUS
191 scm_release_serialization_command(struct wlan_objmgr_vdev *vdev,
192 		uint32_t scan_id)
193 {
194 	struct wlan_serialization_queued_cmd_info cmd = {0};
195 
196 	cmd.requestor = WLAN_UMAC_COMP_SCAN;
197 	cmd.cmd_type = WLAN_SER_CMD_SCAN;
198 	cmd.cmd_id = scan_id;
199 	cmd.req_type = WLAN_SER_CANCEL_SINGLE_SCAN;
200 	cmd.vdev = vdev;
201 	cmd.queue_type = WLAN_SERIALIZATION_ACTIVE_QUEUE;
202 
203 	/* Inform serialization for command completion */
204 	wlan_serialization_remove_cmd(&cmd);
205 
206 	return QDF_STATUS_SUCCESS;
207 }
208 
209 static QDF_STATUS
210 scm_post_internal_scan_complete_event(struct scan_start_request *req,
211 		enum scan_completion_reason reason)
212 {
213 	struct scan_event event = {0, };
214 
215 	/* prepare internal scan complete event */
216 	event.type = SCAN_EVENT_TYPE_COMPLETED;
217 	event.reason = reason;
218 	event.chan_freq = 0; /* Invalid frequency */
219 	event.vdev_id =  req->scan_req.vdev_id;
220 	event.requester = req->scan_req.scan_req_id;
221 	event.scan_id = req->scan_req.scan_id;
222 	/* Fill scan_start_request used to trigger this scan */
223 	event.scan_start_req = req;
224 	/* post scan event to registered handlers */
225 	scm_scan_post_event(req->vdev, &event);
226 
227 	return QDF_STATUS_SUCCESS;
228 }
229 
230 static inline struct pdev_scan_info *
231 scm_scan_get_pdev_priv_info(uint8_t pdev_id, struct wlan_scan_obj *scan_obj)
232 {
233 	return &scan_obj->pdev_info[pdev_id];
234 }
235 
236 static QDF_STATUS
237 scm_update_last_scan_time(struct scan_start_request *req)
238 {
239 	uint8_t pdev_id;
240 	struct wlan_scan_obj *scan_obj;
241 	struct pdev_scan_info *pdev_scan_info;
242 
243 	scan_obj = wlan_vdev_get_scan_obj(req->vdev);
244 	pdev_id = wlan_scan_vdev_get_pdev_id(req->vdev);
245 	pdev_scan_info = scm_scan_get_pdev_priv_info(pdev_id, scan_obj);
246 	/* update last scan start time */
247 	pdev_scan_info->last_scan_time = qdf_system_ticks();
248 
249 	return QDF_STATUS_SUCCESS;
250 }
251 
252 static QDF_STATUS
253 scm_activate_scan_request(struct scan_start_request *req)
254 {
255 	QDF_STATUS status;
256 
257 	status = tgt_scan_start(req);
258 	if (status != QDF_STATUS_SUCCESS) {
259 		scm_err("tgt_scan_start failed, status: %d", status);
260 		/* scan could not be started and hence
261 		 * we will not receive any completions.
262 		 * post scan cancelled
263 		 */
264 		scm_post_internal_scan_complete_event(req,
265 				SCAN_REASON_CANCELLED);
266 		return status;
267 	}
268 	/* save last scan start time */
269 	status = scm_update_last_scan_time(req);
270 
271 	return status;
272 }
273 
274 static QDF_STATUS
275 scm_cancel_scan_request(struct scan_start_request *req)
276 {
277 	struct scan_cancel_request cancel_req = {0, };
278 	QDF_STATUS status;
279 
280 	cancel_req.vdev = req->vdev;
281 	cancel_req.cancel_req.scan_id = req->scan_req.scan_id;
282 	cancel_req.cancel_req.requester = req->scan_req.scan_req_id;
283 	cancel_req.cancel_req.req_type = WLAN_SCAN_CANCEL_SINGLE;
284 	cancel_req.cancel_req.vdev_id = req->scan_req.vdev_id;
285 	/* send scan cancel to fw */
286 	status = tgt_scan_cancel(&cancel_req);
287 	if (status != QDF_STATUS_SUCCESS)
288 		scm_err("tgt_scan_cancel failed: status: %d, scanid: %d",
289 			status, req->scan_req.scan_id);
290 	/* notify event handler about scan cancellation */
291 	scm_post_internal_scan_complete_event(req, SCAN_REASON_CANCELLED);
292 
293 	return status;
294 }
295 
296 static QDF_STATUS
297 scm_scan_serialize_callback(struct wlan_serialization_command *cmd,
298 	enum wlan_serialization_cb_reason reason)
299 {
300 	struct scan_start_request *req;
301 	QDF_STATUS status;
302 
303 	if (!cmd) {
304 		scm_err("cmd is NULL, reason: %d", reason);
305 		QDF_ASSERT(0);
306 		return QDF_STATUS_E_NULL_VALUE;
307 	}
308 
309 	if (!cmd->umac_cmd) {
310 		scm_err("cmd->umac_cmd is NULL , reason: %d", reason);
311 		QDF_ASSERT(0);
312 		return QDF_STATUS_E_NULL_VALUE;
313 	}
314 
315 	req = cmd->umac_cmd;
316 	scm_debug("reason:%d, reqid:%d, scanid:%d, vdevid:%d, vdev:0x%pK",
317 		reason, req->scan_req.scan_req_id, req->scan_req.scan_id,
318 		req->scan_req.vdev_id, req->vdev);
319 
320 	if (!req->vdev) {
321 		scm_err("NULL vdev. req:0x%pK, reason:%d\n", req, reason);
322 		QDF_ASSERT(0);
323 		return QDF_STATUS_E_NULL_VALUE;
324 	}
325 
326 	switch (reason) {
327 	case WLAN_SER_CB_ACTIVATE_CMD:
328 		/* command moved to active list
329 		 * modify the params if required for concurency case.
330 		 */
331 		status = scm_activate_scan_request(req);
332 		break;
333 
334 	case WLAN_SER_CB_CANCEL_CMD:
335 		/* command removed from pending list.
336 		 * notify registered scan event handlers with
337 		 * status completed and reason cancelled.
338 		 */
339 		status = scm_post_internal_scan_complete_event(req,
340 				SCAN_REASON_CANCELLED);
341 		break;
342 
343 	case WLAN_SER_CB_ACTIVE_CMD_TIMEOUT:
344 		/* active command timed out.
345 		 * prepare internal scan cancel request
346 		 */
347 		status = scm_cancel_scan_request(req);
348 		break;
349 
350 	case WLAN_SER_CB_RELEASE_MEM_CMD:
351 		/* command successfully completed.
352 		 * Release vdev reference and free scan_start_request memory
353 		 */
354 		wlan_objmgr_vdev_release_ref(req->vdev, WLAN_SCAN_ID);
355 		status = scm_scan_free_scan_request_mem(req);
356 		break;
357 
358 	default:
359 		/* Do nothing but logging */
360 		QDF_ASSERT(0);
361 		status = QDF_STATUS_E_INVAL;
362 		break;
363 	}
364 
365 	return status;
366 }
367 
368 QDF_STATUS
369 scm_scan_start_req(struct scheduler_msg *msg)
370 {
371 	struct wlan_serialization_command cmd = {0, };
372 	enum wlan_serialization_status ser_cmd_status;
373 	struct scan_start_request *req;
374 	struct wlan_scan_obj *scan_obj;
375 	QDF_STATUS status = QDF_STATUS_SUCCESS;
376 
377 	if (!msg) {
378 		scm_err("msg received is NULL");
379 		QDF_ASSERT(0);
380 		return QDF_STATUS_E_NULL_VALUE;
381 	}
382 	if (!msg->bodyptr) {
383 		scm_err("bodyptr is NULL");
384 		QDF_ASSERT(0);
385 		return QDF_STATUS_E_NULL_VALUE;
386 	}
387 
388 	req = msg->bodyptr;
389 	scan_obj = wlan_vdev_get_scan_obj(req->vdev);
390 	if (!scan_obj) {
391 		scm_debug("Couldn't find scan object");
392 		return QDF_STATUS_E_NULL_VALUE;
393 	}
394 
395 	cmd.cmd_type = WLAN_SER_CMD_SCAN;
396 	cmd.cmd_id = req->scan_req.scan_id;
397 	cmd.cmd_cb = (wlan_serialization_cmd_callback)
398 		scm_scan_serialize_callback;
399 	cmd.umac_cmd = req;
400 	cmd.source = WLAN_UMAC_COMP_SCAN;
401 	cmd.is_high_priority = false;
402 	cmd.cmd_timeout_duration = req->scan_req.max_scan_time +
403 		SCAN_TIMEOUT_GRACE_PERIOD;
404 	cmd.vdev = req->vdev;
405 
406 	if (scan_obj->disable_timeout)
407 		cmd.cmd_timeout_duration = 0;
408 
409 	scm_info("req: 0x%pK, reqid: %d, scanid: %d, vdevid: %d",
410 		req, req->scan_req.scan_req_id, req->scan_req.scan_id,
411 		req->scan_req.vdev_id);
412 
413 	ser_cmd_status = wlan_serialization_request(&cmd);
414 	scm_info("wlan_serialization_request status:%d", ser_cmd_status);
415 
416 	switch (ser_cmd_status) {
417 	case WLAN_SER_CMD_PENDING:
418 		/* command moved to pending list.Do nothing */
419 		break;
420 	case WLAN_SER_CMD_ACTIVE:
421 		/* command moved to active list. Do nothing */
422 		break;
423 	case WLAN_SER_CMD_DENIED_LIST_FULL:
424 	case WLAN_SER_CMD_DENIED_RULES_FAILED:
425 	case WLAN_SER_CMD_DENIED_UNSPECIFIED:
426 		/* notify registered scan event handlers
427 		 * about internal error
428 		 */
429 		scm_post_internal_scan_complete_event(req,
430 				SCAN_REASON_INTERNAL_FAILURE);
431 		/* cmd can't be serviced.
432 		 * release vdev reference and free scan_start_request memory
433 		 */
434 		wlan_objmgr_vdev_release_ref(req->vdev, WLAN_SCAN_ID);
435 		scm_scan_free_scan_request_mem(req);
436 		break;
437 	default:
438 		QDF_ASSERT(0);
439 		status = QDF_STATUS_E_INVAL;
440 		/* cmd can't be serviced.
441 		 * release vdev reference and free scan_start_request memory
442 		 */
443 		wlan_objmgr_vdev_release_ref(req->vdev, WLAN_SCAN_ID);
444 		scm_scan_free_scan_request_mem(req);
445 		break;
446 	}
447 
448 	return status;
449 }
450 
451 static inline enum wlan_serialization_cancel_type
452 get_serialization_cancel_type(enum scan_cancel_req_type type)
453 {
454 	enum wlan_serialization_cancel_type serialization_type;
455 
456 	switch (type) {
457 	case WLAN_SCAN_CANCEL_SINGLE:
458 		serialization_type = WLAN_SER_CANCEL_SINGLE_SCAN;
459 		break;
460 	case WLAN_SCAN_CANCEL_VDEV_ALL:
461 		serialization_type = WLAN_SER_CANCEL_VDEV_SCANS;
462 		break;
463 	case WLAN_SCAN_CANCEL_PDEV_ALL:
464 		serialization_type = WLAN_SER_CANCEL_PDEV_SCANS;
465 		break;
466 	default:
467 		QDF_ASSERT(0);
468 		scm_warn("invalid scan_cancel_req_type: %d", type);
469 		serialization_type = WLAN_SER_CANCEL_PDEV_SCANS;
470 		break;
471 	}
472 
473 	return serialization_type;
474 }
475 
476 QDF_STATUS
477 scm_scan_cancel_req(struct scheduler_msg *msg)
478 {
479 	struct wlan_serialization_queued_cmd_info cmd = {0,};
480 	struct wlan_serialization_command ser_cmd = {0,};
481 	enum wlan_serialization_cmd_status ser_cmd_status;
482 	struct scan_cancel_request *req;
483 	QDF_STATUS status = QDF_STATUS_SUCCESS;
484 
485 	if (!msg) {
486 		scm_err("msg received is NULL");
487 		QDF_ASSERT(0);
488 		return QDF_STATUS_E_NULL_VALUE;
489 	}
490 	if (!msg->bodyptr) {
491 		scm_err("Bodyptr is NULL");
492 		QDF_ASSERT(0);
493 		return QDF_STATUS_E_NULL_VALUE;
494 	}
495 
496 	req = msg->bodyptr;
497 	/*
498 	 * If requester wants to wait for target scan cancel event
499 	 * instead of internally generated cancel event, just check
500 	 * which queue this scan request belongs to and send scan
501 	 * cancel request to FW accordingly.
502 	 * Else generate internal scan cancel event and notify
503 	 * handlers and free scan request resources.
504 	 */
505 	if (req->wait_tgt_cancel &&
506 			(req->cancel_req.req_type == WLAN_SCAN_CANCEL_SINGLE)) {
507 		ser_cmd.cmd_type = WLAN_SER_CMD_SCAN;
508 		ser_cmd.cmd_id = req->cancel_req.scan_id;
509 		ser_cmd.cmd_cb = NULL;
510 		ser_cmd.umac_cmd = NULL;
511 		ser_cmd.source = WLAN_UMAC_COMP_SCAN;
512 		ser_cmd.is_high_priority = false;
513 		ser_cmd.vdev = req->vdev;
514 		if (wlan_serialization_is_cmd_present_in_active_queue(NULL, &ser_cmd))
515 			ser_cmd_status = WLAN_SER_CMD_IN_ACTIVE_LIST;
516 		else if (wlan_serialization_is_cmd_present_in_pending_queue(NULL, &ser_cmd))
517 			ser_cmd_status = WLAN_SER_CMD_IN_PENDING_LIST;
518 		else
519 			ser_cmd_status = WLAN_SER_CMD_NOT_FOUND;
520 	} else {
521 		cmd.requestor = 0;
522 		cmd.cmd_type = WLAN_SER_CMD_SCAN;
523 		cmd.cmd_id = req->cancel_req.scan_id;
524 		cmd.vdev = req->vdev;
525 		cmd.queue_type = WLAN_SERIALIZATION_ACTIVE_QUEUE |
526 			WLAN_SERIALIZATION_PENDING_QUEUE;
527 		cmd.req_type = get_serialization_cancel_type(req->cancel_req.req_type);
528 
529 		ser_cmd_status = wlan_serialization_cancel_request(&cmd);
530 	}
531 
532 	scm_debug("status: %d, reqid: %d, scanid: %d, vdevid: %d, type: %d",
533 		ser_cmd_status, req->cancel_req.requester,
534 		req->cancel_req.scan_id, req->cancel_req.vdev_id,
535 		req->cancel_req.req_type);
536 
537 	switch (ser_cmd_status) {
538 	case WLAN_SER_CMD_IN_PENDING_LIST:
539 		/* do nothing */
540 		break;
541 	case WLAN_SER_CMD_IN_ACTIVE_LIST:
542 	case WLAN_SER_CMDS_IN_ALL_LISTS:
543 		/* send wmi scan cancel to fw */
544 		status = tgt_scan_cancel(req);
545 		break;
546 	case WLAN_SER_CMD_NOT_FOUND:
547 		/* do nothing */
548 		break;
549 	default:
550 		QDF_ASSERT(0);
551 		status = QDF_STATUS_E_INVAL;
552 		break;
553 	}
554 
555 	/* Release vdev reference and scan cancel request
556 	 * processing is complete
557 	 */
558 	wlan_objmgr_vdev_release_ref(req->vdev, WLAN_SCAN_ID);
559 	/* Free cancel request memory */
560 	qdf_mem_free(req);
561 
562 	return status;
563 }
564 
565 #ifdef FEATURE_WLAN_SCAN_PNO
566 static QDF_STATUS
567 scm_pno_event_handler(struct wlan_objmgr_vdev *vdev,
568 	struct scan_event *event)
569 {
570 	struct scan_vdev_obj *scan_vdev_obj;
571 	struct wlan_scan_obj *scan_psoc_obj;
572 	scan_event_handler pno_cb;
573 	void *cb_arg;
574 
575 	scan_vdev_obj = wlan_get_vdev_scan_obj(vdev);
576 	scan_psoc_obj = wlan_vdev_get_scan_obj(vdev);
577 	if (!scan_vdev_obj || !scan_psoc_obj) {
578 		scm_err("null scan_vdev_obj %pK scan_obj %pK",
579 			scan_vdev_obj, scan_psoc_obj);
580 		return QDF_STATUS_E_INVAL;
581 	}
582 
583 	switch (event->type) {
584 	case SCAN_EVENT_TYPE_NLO_COMPLETE:
585 		if (!scan_vdev_obj->pno_match_evt_received)
586 			return QDF_STATUS_SUCCESS;
587 		qdf_wake_lock_release(&scan_psoc_obj->pno_cfg.pno_wake_lock,
588 			WIFI_POWER_EVENT_WAKELOCK_PNO);
589 		qdf_wake_lock_timeout_acquire(
590 			&scan_psoc_obj->pno_cfg.pno_wake_lock,
591 			SCAN_PNO_SCAN_COMPLETE_WAKE_LOCK_TIMEOUT);
592 		scan_vdev_obj->pno_match_evt_received = false;
593 		break;
594 	case SCAN_EVENT_TYPE_NLO_MATCH:
595 		scan_vdev_obj->pno_match_evt_received = true;
596 		qdf_wake_lock_timeout_acquire(
597 			&scan_psoc_obj->pno_cfg.pno_wake_lock,
598 			SCAN_PNO_MATCH_WAKE_LOCK_TIMEOUT);
599 		return QDF_STATUS_SUCCESS;
600 	default:
601 		return QDF_STATUS_E_INVAL;
602 	}
603 	qdf_spin_lock_bh(&scan_psoc_obj->lock);
604 	pno_cb = scan_psoc_obj->pno_cfg.pno_cb.func;
605 	cb_arg = scan_psoc_obj->pno_cfg.pno_cb.arg;
606 	qdf_spin_unlock_bh(&scan_psoc_obj->lock);
607 
608 	if (pno_cb)
609 		pno_cb(vdev, event, cb_arg);
610 
611 	return QDF_STATUS_SUCCESS;
612 }
613 #else
614 
615 static QDF_STATUS
616 scm_pno_event_handler(struct wlan_objmgr_vdev *vdev,
617 	struct scan_event *event)
618 {
619 	return QDF_STATUS_SUCCESS;
620 }
621 #endif
622 
623 /**
624  * scm_scan_update_scan_event() - update scan event
625  * @scan: scan object
626  * @event: scan event
627  * @scan_start_req: scan_start_req used for triggering scan
628  *
629  * update scan params in scan event
630  *
631  * Return: QDF_STATUS
632  */
633 static QDF_STATUS
634 scm_scan_update_scan_event(struct wlan_scan_obj *scan,
635 		struct scan_event *event,
636 		struct scan_start_request *scan_start_req)
637 {
638 	if (!event)
639 		return QDF_STATUS_E_NULL_VALUE;
640 
641 	if (!scan || !scan_start_req) {
642 		event->scan_start_req = NULL;
643 		return QDF_STATUS_E_NULL_VALUE;
644 	}
645 	/* copy scan start request to pass back buffer */
646 	qdf_mem_copy(&scan->scan_start_request_buff, scan_start_req,
647 			sizeof(struct scan_start_request));
648 	/* reset all pointers */
649 	scan->scan_start_request_buff.scan_req.extraie.ptr = NULL;
650 	scan->scan_start_request_buff.scan_req.extraie.len = 0;
651 	scan->scan_start_request_buff.scan_req.htcap.ptr = NULL;
652 	scan->scan_start_request_buff.scan_req.htcap.len = 0;
653 	scan->scan_start_request_buff.scan_req.vhtcap.ptr = NULL;
654 	scan->scan_start_request_buff.scan_req.vhtcap.len = 0;
655 
656 	event->scan_start_req = &scan->scan_start_request_buff;
657 
658 	return QDF_STATUS_SUCCESS;
659 }
660 
661 QDF_STATUS
662 scm_scan_event_handler(struct scheduler_msg *msg)
663 {
664 	struct wlan_objmgr_vdev *vdev;
665 	struct scan_event *event;
666 	struct scan_event_info *event_info;
667 	struct wlan_serialization_command cmd = {0,};
668 	struct wlan_serialization_command *queued_cmd;
669 	struct scan_start_request *scan_start_req;
670 	struct wlan_scan_obj *scan;
671 
672 	if (!msg) {
673 		scm_err("NULL msg received ");
674 		QDF_ASSERT(0);
675 		return QDF_STATUS_E_NULL_VALUE;
676 	}
677 	if (!msg->bodyptr) {
678 		scm_err("NULL scan event received");
679 		QDF_ASSERT(0);
680 		return QDF_STATUS_E_NULL_VALUE;
681 	}
682 
683 	event_info = msg->bodyptr;
684 	vdev = event_info->vdev;
685 	event = &(event_info->event);
686 
687 	scm_info("vdevid:%d, type:%d, reason:%d, freq:%d, reqstr:%d, scanid:%d",
688 		event->vdev_id, event->type, event->reason, event->chan_freq,
689 		event->requester, event->scan_id);
690 	/*
691 	 * NLO requests are never queued, so post NLO events
692 	 * without checking for their presence in active queue.
693 	 */
694 	switch (event->type) {
695 	case SCAN_EVENT_TYPE_NLO_COMPLETE:
696 	case SCAN_EVENT_TYPE_NLO_MATCH:
697 		scm_pno_event_handler(vdev, event);
698 		goto exit;
699 	default:
700 		break;
701 	}
702 
703 	cmd.cmd_type = WLAN_SER_CMD_SCAN;
704 	cmd.cmd_id = event->scan_id;
705 	cmd.cmd_cb = NULL;
706 	cmd.umac_cmd = NULL;
707 	cmd.source = WLAN_UMAC_COMP_SCAN;
708 	cmd.is_high_priority = false;
709 	cmd.vdev = vdev;
710 	if (!wlan_serialization_is_cmd_present_in_active_queue(NULL, &cmd)) {
711 		/*
712 		 * We received scan event for an already completed/cancelled
713 		 * scan request. Drop this event.
714 		 */
715 		scm_warn("Received scan event while request not in active queue");
716 		goto exit;
717 	}
718 
719 	/* Fill scan_start_request used to trigger this scan */
720 	queued_cmd = wlan_serialization_get_scan_cmd_using_scan_id(
721 			wlan_vdev_get_psoc(vdev), wlan_vdev_get_id(vdev),
722 			event->scan_id, true);
723 
724 	if (!queued_cmd) {
725 		scm_err("NULL queued_cmd");
726 		goto exit;
727 	}
728 	if (!queued_cmd->umac_cmd) {
729 		scm_err("NULL umac_cmd");
730 		goto exit;
731 	}
732 	scan_start_req = queued_cmd->umac_cmd;
733 
734 	if (scan_start_req->scan_req.scan_req_id != event->requester) {
735 		scm_err("req ID mismatch, scan_req_id:%d, event_req_id:%d",
736 				scan_start_req->scan_req.scan_req_id,
737 				event->requester);
738 		goto exit;
739 	}
740 
741 	scan = wlan_vdev_get_scan_obj(vdev);
742 	if (scan)
743 		scm_scan_update_scan_event(scan, event, scan_start_req);
744 
745 	switch (event->type) {
746 	case SCAN_EVENT_TYPE_COMPLETED:
747 		scm_11d_decide_country_code(vdev);
748 		/* fall through to release the command */
749 	case SCAN_EVENT_TYPE_START_FAILED:
750 	case SCAN_EVENT_TYPE_DEQUEUED:
751 		scm_release_serialization_command(vdev, event->scan_id);
752 		break;
753 	default:
754 		break;
755 	}
756 
757 	/* Notify all interested parties */
758 	scm_scan_post_event(vdev, event);
759 
760 exit:
761 	/* free event info memory */
762 	qdf_mem_free(event_info);
763 	wlan_objmgr_vdev_release_ref(vdev, WLAN_SCAN_ID);
764 
765 	return QDF_STATUS_SUCCESS;
766 }
767 
768 QDF_STATUS scm_scan_event_flush_callback(struct scheduler_msg *msg)
769 {
770 	struct wlan_objmgr_vdev *vdev;
771 	struct scan_event_info *event_info;
772 
773 	if (!msg || !msg->bodyptr) {
774 		scm_err("msg or msg->bodyptr is NULL");
775 		return QDF_STATUS_E_NULL_VALUE;
776 	}
777 
778 	event_info = msg->bodyptr;
779 	vdev = event_info->vdev;
780 
781 	/* free event info memory */
782 	qdf_mem_free(event_info);
783 	wlan_objmgr_vdev_release_ref(vdev, WLAN_SCAN_ID);
784 
785 	return QDF_STATUS_SUCCESS;
786 }
787 
788 QDF_STATUS scm_bcn_probe_flush_callback(struct scheduler_msg *msg)
789 {
790 	struct scan_bcn_probe_event *bcn;
791 
792 	bcn = msg->bodyptr;
793 
794 	if (!bcn) {
795 		scm_err("bcn is NULL");
796 		return QDF_STATUS_E_NULL_VALUE;
797 	}
798 	if (bcn->psoc)
799 		wlan_objmgr_psoc_release_ref(bcn->psoc, WLAN_SCAN_ID);
800 	if (bcn->rx_data)
801 		qdf_mem_free(bcn->rx_data);
802 	if (bcn->buf)
803 		qdf_nbuf_free(bcn->buf);
804 	qdf_mem_free(bcn);
805 
806 	return QDF_STATUS_SUCCESS;
807 }
808 
809 QDF_STATUS scm_scan_start_flush_callback(struct scheduler_msg *msg)
810 {
811 	struct scan_start_request *req;
812 
813 	if (!msg || !msg->bodyptr) {
814 		scm_err("msg or msg->bodyptr is NULL");
815 		return QDF_STATUS_E_NULL_VALUE;
816 	}
817 
818 	req = msg->bodyptr;
819 	wlan_objmgr_vdev_release_ref(req->vdev, WLAN_SCAN_ID);
820 	scm_scan_free_scan_request_mem(req);
821 
822 	return QDF_STATUS_SUCCESS;
823 }
824 
825 QDF_STATUS scm_scan_cancel_flush_callback(struct scheduler_msg *msg)
826 {
827 	struct scan_cancel_request *req;
828 
829 	if (!msg || !msg->bodyptr) {
830 		scm_err("msg or msg->bodyptr is NULL");
831 		return QDF_STATUS_E_NULL_VALUE;
832 	}
833 
834 	req = msg->bodyptr;
835 	wlan_objmgr_vdev_release_ref(req->vdev, WLAN_SCAN_ID);
836 	/* Free cancel request memory */
837 	qdf_mem_free(req);
838 
839 	return QDF_STATUS_SUCCESS;
840 }
841