1 /* 2 * Copyright (c) 2017-2019 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: defines driver functions interfacing with linux kernel 21 */ 22 23 #include <qdf_list.h> 24 #include <qdf_status.h> 25 #include <linux/wireless.h> 26 #include <linux/netdevice.h> 27 #include <net/cfg80211.h> 28 #include <wlan_scan_utils_api.h> 29 #include <wlan_cfg80211.h> 30 #include <wlan_cfg80211_scan.h> 31 #include <wlan_osif_priv.h> 32 #include <wlan_scan_public_structs.h> 33 #include <wlan_scan_ucfg_api.h> 34 #include <wlan_cfg80211_scan.h> 35 #include <qdf_mem.h> 36 #include <wlan_utility.h> 37 #ifdef WLAN_POLICY_MGR_ENABLE 38 #include <wlan_policy_mgr_api.h> 39 #endif 40 #include <wlan_reg_services_api.h> 41 42 static const 43 struct nla_policy scan_policy[QCA_WLAN_VENDOR_ATTR_SCAN_MAX + 1] = { 44 [QCA_WLAN_VENDOR_ATTR_SCAN_FLAGS] = {.type = NLA_U32}, 45 [QCA_WLAN_VENDOR_ATTR_SCAN_TX_NO_CCK_RATE] = {.type = NLA_FLAG}, 46 [QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE] = {.type = NLA_U64}, 47 }; 48 49 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) 50 static uint32_t hdd_config_sched_scan_start_delay( 51 struct cfg80211_sched_scan_request *request) 52 { 53 return request->delay; 54 } 55 #else 56 static uint32_t hdd_config_sched_scan_start_delay( 57 struct cfg80211_sched_scan_request *request) 58 { 59 return 0; 60 } 61 #endif 62 63 #if defined(CFG80211_SCAN_RANDOM_MAC_ADDR) || \ 64 (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) 65 /** 66 * wlan_fill_scan_rand_attrs() - Populate the scan randomization attrs 67 * @vdev: pointer to objmgr vdev 68 * @flags: cfg80211 scan flags 69 * @mac_addr: random mac addr from cfg80211 70 * @mac_addr_mask: mac addr mask from cfg80211 71 * @randomize: output variable to check scan randomization status 72 * @addr: output variable to hold random addr 73 * @mask: output variable to hold mac mask 74 * 75 * Return: None 76 */ 77 static void wlan_fill_scan_rand_attrs(struct wlan_objmgr_vdev *vdev, 78 uint32_t flags, 79 uint8_t *mac_addr, 80 uint8_t *mac_addr_mask, 81 bool *randomize, 82 uint8_t *addr, 83 uint8_t *mask) 84 { 85 *randomize = false; 86 if (!(flags & NL80211_SCAN_FLAG_RANDOM_ADDR)) 87 return; 88 89 if (wlan_vdev_mlme_get_opmode(vdev) != QDF_STA_MODE) 90 return; 91 92 if (wlan_vdev_is_connected(vdev)) 93 return; 94 95 *randomize = true; 96 memcpy(addr, mac_addr, QDF_MAC_ADDR_SIZE); 97 memcpy(mask, mac_addr_mask, QDF_MAC_ADDR_SIZE); 98 cfg80211_debug("Random mac addr: %pM and Random mac mask: %pM", 99 addr, mask); 100 } 101 102 /** 103 * wlan_scan_rand_attrs() - Wrapper function to fill scan random attrs 104 * @vdev: pointer to objmgr vdev 105 * @request: pointer to cfg80211 scan request 106 * @req: pointer to cmn module scan request 107 * 108 * This is a wrapper function which invokes wlan_fill_scan_rand_attrs() 109 * to fill random attributes of internal scan request with cfg80211_scan_request 110 * 111 * Return: None 112 */ 113 static void wlan_scan_rand_attrs(struct wlan_objmgr_vdev *vdev, 114 struct cfg80211_scan_request *request, 115 struct scan_start_request *req) 116 { 117 bool *randomize = &req->scan_req.scan_random.randomize; 118 uint8_t *mac_addr = req->scan_req.scan_random.mac_addr; 119 uint8_t *mac_mask = req->scan_req.scan_random.mac_mask; 120 121 wlan_fill_scan_rand_attrs(vdev, request->flags, request->mac_addr, 122 request->mac_addr_mask, randomize, mac_addr, 123 mac_mask); 124 if (!*randomize) 125 return; 126 127 req->scan_req.scan_f_add_spoofed_mac_in_probe = true; 128 req->scan_req.scan_f_add_rand_seq_in_probe = true; 129 } 130 #else 131 /** 132 * wlan_scan_rand_attrs() - Wrapper function to fill scan random attrs 133 * @vdev: pointer to objmgr vdev 134 * @request: pointer to cfg80211 scan request 135 * @req: pointer to cmn module scan request 136 * 137 * This is a wrapper function which invokes wlan_fill_scan_rand_attrs() 138 * to fill random attributes of internal scan request with cfg80211_scan_request 139 * 140 * Return: None 141 */ 142 static void wlan_scan_rand_attrs(struct wlan_objmgr_vdev *vdev, 143 struct cfg80211_scan_request *request, 144 struct scan_start_request *req) 145 { 146 } 147 #endif 148 149 #ifdef FEATURE_WLAN_SCAN_PNO 150 #if ((LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) || \ 151 defined(CFG80211_MULTI_SCAN_PLAN_BACKPORT)) 152 153 /** 154 * wlan_config_sched_scan_plan() - configures the sched scan plans 155 * from the framework. 156 * @pno_req: pointer to PNO scan request 157 * @request: pointer to scan request from framework 158 * 159 * Return: None 160 */ 161 static void wlan_config_sched_scan_plan(struct pno_scan_req_params *pno_req, 162 struct cfg80211_sched_scan_request *request) 163 { 164 /* 165 * As of now max 2 scan plans were supported by firmware 166 * if number of scan plan supported by firmware increased below logic 167 * must change. 168 */ 169 if (request->n_scan_plans == SCAN_PNO_MAX_PLAN_REQUEST) { 170 pno_req->fast_scan_period = 171 request->scan_plans[0].interval * MSEC_PER_SEC; 172 pno_req->fast_scan_max_cycles = 173 request->scan_plans[0].iterations; 174 pno_req->slow_scan_period = 175 request->scan_plans[1].interval * MSEC_PER_SEC; 176 } else if (request->n_scan_plans == 1) { 177 pno_req->fast_scan_period = 178 request->scan_plans[0].interval * MSEC_PER_SEC; 179 /* 180 * if only one scan plan is configured from framework 181 * then both fast and slow scan should be configured with the 182 * same value that is why fast scan cycles are hardcoded to one 183 */ 184 pno_req->fast_scan_max_cycles = 1; 185 pno_req->slow_scan_period = 186 request->scan_plans[0].interval * MSEC_PER_SEC; 187 } else { 188 cfg80211_err("Invalid number of scan plans %d !!", 189 request->n_scan_plans); 190 } 191 } 192 #else 193 static void wlan_config_sched_scan_plan(struct pno_scan_req_params *pno_req, 194 struct cfg80211_sched_scan_request *request) 195 { 196 pno_req->fast_scan_period = request->interval; 197 pno_req->fast_scan_max_cycles = SCAN_PNO_DEF_SCAN_TIMER_REPEAT; 198 pno_req->slow_scan_period = 199 SCAN_PNO_DEF_SLOW_SCAN_MULTIPLIER * 200 pno_req->fast_scan_period; 201 } 202 #endif 203 204 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0) 205 static inline void 206 wlan_cfg80211_sched_scan_results(struct wiphy *wiphy, uint64_t reqid) 207 { 208 cfg80211_sched_scan_results(wiphy); 209 } 210 #else 211 static inline void 212 wlan_cfg80211_sched_scan_results(struct wiphy *wiphy, uint64_t reqid) 213 { 214 cfg80211_sched_scan_results(wiphy, reqid); 215 } 216 #endif 217 218 /** 219 * wlan_cfg80211_pno_callback() - pno callback function to handle 220 * pno events. 221 * @vdev: vdev ptr 222 * @event: scan events 223 * @args: argument 224 * 225 * Return: void 226 */ 227 static void wlan_cfg80211_pno_callback(struct wlan_objmgr_vdev *vdev, 228 struct scan_event *event, 229 void *args) 230 { 231 struct wlan_objmgr_pdev *pdev; 232 struct pdev_osif_priv *pdev_ospriv; 233 234 if (event->type != SCAN_EVENT_TYPE_NLO_COMPLETE) 235 return; 236 237 cfg80211_debug("vdev id = %d", event->vdev_id); 238 239 pdev = wlan_vdev_get_pdev(vdev); 240 if (!pdev) { 241 cfg80211_err("pdev is NULL"); 242 return; 243 } 244 245 pdev_ospriv = wlan_pdev_get_ospriv(pdev); 246 if (!pdev_ospriv) { 247 cfg80211_err("pdev_ospriv is NULL"); 248 return; 249 } 250 wlan_cfg80211_sched_scan_results(pdev_ospriv->wiphy, 0); 251 } 252 253 #ifdef WLAN_POLICY_MGR_ENABLE 254 static bool wlan_cfg80211_is_ap_go_present(struct wlan_objmgr_psoc *psoc) 255 { 256 return policy_mgr_mode_specific_connection_count(psoc, 257 PM_SAP_MODE, 258 NULL) || 259 policy_mgr_mode_specific_connection_count(psoc, 260 PM_P2P_GO_MODE, 261 NULL); 262 } 263 264 static QDF_STATUS wlan_cfg80211_is_chan_ok_for_dnbs( 265 struct wlan_objmgr_psoc *psoc, 266 u8 channel, bool *ok) 267 { 268 QDF_STATUS status = policy_mgr_is_chan_ok_for_dnbs(psoc, channel, ok); 269 270 if (QDF_IS_STATUS_ERROR(status)) { 271 cfg80211_err("DNBS check failed"); 272 return status; 273 } 274 275 return QDF_STATUS_SUCCESS; 276 } 277 #else 278 static bool wlan_cfg80211_is_ap_go_present(struct wlan_objmgr_psoc *psoc) 279 { 280 return false; 281 } 282 283 static QDF_STATUS wlan_cfg80211_is_chan_ok_for_dnbs( 284 struct wlan_objmgr_psoc *psoc, 285 u8 channel, 286 bool *ok) 287 { 288 if (!ok) 289 return QDF_STATUS_E_INVAL; 290 291 *ok = true; 292 return QDF_STATUS_SUCCESS; 293 } 294 #endif 295 296 #if defined(CFG80211_SCAN_RANDOM_MAC_ADDR) || \ 297 (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) 298 /** 299 * wlan_pno_scan_rand_attr() - Wrapper function to fill sched scan random attrs 300 * @vdev: pointer to objmgr vdev 301 * @request: pointer to cfg80211 sched scan request 302 * @req: pointer to cmn module pno scan request 303 * 304 * This is a wrapper function which invokes wlan_fill_scan_rand_attrs() 305 * to fill random attributes of internal pno scan 306 * with cfg80211_sched_scan_request 307 * 308 * Return: None 309 */ 310 static void wlan_pno_scan_rand_attr(struct wlan_objmgr_vdev *vdev, 311 struct cfg80211_sched_scan_request *request, 312 struct pno_scan_req_params *req) 313 { 314 bool *randomize = &req->scan_random.randomize; 315 uint8_t *mac_addr = req->scan_random.mac_addr; 316 uint8_t *mac_mask = req->scan_random.mac_mask; 317 318 wlan_fill_scan_rand_attrs(vdev, request->flags, request->mac_addr, 319 request->mac_addr_mask, randomize, mac_addr, 320 mac_mask); 321 } 322 #else 323 /** 324 * wlan_pno_scan_rand_attr() - Wrapper function to fill sched scan random attrs 325 * @vdev: pointer to objmgr vdev 326 * @request: pointer to cfg80211 sched scan request 327 * @req: pointer to cmn module pno scan request 328 * 329 * This is a wrapper function which invokes wlan_fill_scan_rand_attrs() 330 * to fill random attributes of internal pno scan 331 * with cfg80211_sched_scan_request 332 * 333 * Return: None 334 */ 335 static void wlan_pno_scan_rand_attr(struct wlan_objmgr_vdev *vdev, 336 struct cfg80211_sched_scan_request *request, 337 struct pno_scan_req_params *req) 338 { 339 } 340 #endif 341 342 /** 343 * wlan_hdd_sched_scan_update_relative_rssi() - update CPNO params 344 * @pno_request: pointer to PNO scan request 345 * @request: Pointer to cfg80211 scheduled scan start request 346 * 347 * This function is used to update Connected PNO params sent by kernel 348 * 349 * Return: None 350 */ 351 #if defined(CFG80211_REPORT_BETTER_BSS_IN_SCHED_SCAN) || \ 352 (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)) 353 static inline void wlan_hdd_sched_scan_update_relative_rssi( 354 struct pno_scan_req_params *pno_request, 355 struct cfg80211_sched_scan_request *request) 356 { 357 pno_request->relative_rssi_set = request->relative_rssi_set; 358 pno_request->relative_rssi = request->relative_rssi; 359 if (NL80211_BAND_2GHZ == request->rssi_adjust.band) 360 pno_request->band_rssi_pref.band = WLAN_BAND_2_4_GHZ; 361 else if (NL80211_BAND_5GHZ == request->rssi_adjust.band) 362 pno_request->band_rssi_pref.band = WLAN_BAND_5_GHZ; 363 pno_request->band_rssi_pref.rssi = request->rssi_adjust.delta; 364 } 365 #else 366 static inline void wlan_hdd_sched_scan_update_relative_rssi( 367 struct pno_scan_req_params *pno_request, 368 struct cfg80211_sched_scan_request *request) 369 { 370 } 371 #endif 372 373 int wlan_cfg80211_sched_scan_start(struct wlan_objmgr_vdev *vdev, 374 struct cfg80211_sched_scan_request *request, 375 uint8_t scan_backoff_multiplier) 376 { 377 struct pno_scan_req_params *req; 378 int i, j, ret = 0; 379 QDF_STATUS status; 380 uint8_t num_chan = 0, channel; 381 struct wlan_objmgr_pdev *pdev = wlan_vdev_get_pdev(vdev); 382 struct wlan_objmgr_psoc *psoc; 383 uint32_t valid_ch[SCAN_PNO_MAX_NETW_CHANNELS_EX] = {0}; 384 385 if (ucfg_scan_get_pno_in_progress(vdev)) { 386 cfg80211_debug("pno is already in progress"); 387 return -EBUSY; 388 } 389 390 if (ucfg_scan_get_pdev_status(pdev) != 391 SCAN_NOT_IN_PROGRESS) { 392 status = wlan_abort_scan(pdev, 393 wlan_objmgr_pdev_get_pdev_id(pdev), 394 INVAL_VDEV_ID, INVAL_SCAN_ID, true); 395 if (QDF_IS_STATUS_ERROR(status)) { 396 cfg80211_err("aborting the existing scan is unsuccessful"); 397 return -EBUSY; 398 } 399 } 400 401 req = qdf_mem_malloc(sizeof(*req)); 402 if (!req) { 403 cfg80211_err("req malloc failed"); 404 return -ENOMEM; 405 } 406 407 wlan_pdev_obj_lock(pdev); 408 psoc = wlan_pdev_get_psoc(pdev); 409 wlan_pdev_obj_unlock(pdev); 410 411 req->networks_cnt = request->n_match_sets; 412 req->vdev_id = wlan_vdev_get_id(vdev); 413 414 if ((!req->networks_cnt) || 415 (req->networks_cnt > SCAN_PNO_MAX_SUPP_NETWORKS)) { 416 cfg80211_err("Network input is not correct %d", 417 req->networks_cnt); 418 ret = -EINVAL; 419 goto error; 420 } 421 422 if (request->n_channels > SCAN_PNO_MAX_NETW_CHANNELS_EX) { 423 cfg80211_err("Incorrect number of channels %d", 424 request->n_channels); 425 ret = -EINVAL; 426 goto error; 427 } 428 429 if (request->n_channels) { 430 char chl[(request->n_channels * 5) + 1]; 431 int len = 0; 432 bool ap_or_go_present = wlan_cfg80211_is_ap_go_present(psoc); 433 434 for (i = 0; i < request->n_channels; i++) { 435 channel = request->channels[i]->hw_value; 436 if (wlan_reg_is_dsrc_chan(pdev, channel)) 437 continue; 438 439 if (ap_or_go_present) { 440 bool ok; 441 442 status = 443 wlan_cfg80211_is_chan_ok_for_dnbs(psoc, 444 channel, 445 &ok); 446 if (QDF_IS_STATUS_ERROR(status)) { 447 cfg80211_err("DNBS check failed"); 448 ret = -EINVAL; 449 goto error; 450 } 451 if (!ok) 452 continue; 453 } 454 len += snprintf(chl + len, 5, "%d ", channel); 455 valid_ch[num_chan++] = wlan_chan_to_freq(channel); 456 } 457 cfg80211_notice("No. of Scan Channels: %d", num_chan); 458 cfg80211_notice("Channel-List: %s", chl); 459 /* If all channels are DFS and dropped, 460 * then ignore the PNO request 461 */ 462 if (!num_chan) { 463 cfg80211_notice("Channel list empty due to filtering of DSRC"); 464 ret = -EINVAL; 465 goto error; 466 } 467 } 468 469 /* Filling per profile params */ 470 for (i = 0; i < req->networks_cnt; i++) { 471 req->networks_list[i].ssid.length = 472 request->match_sets[i].ssid.ssid_len; 473 474 if ((!req->networks_list[i].ssid.length) || 475 (req->networks_list[i].ssid.length > WLAN_SSID_MAX_LEN)) { 476 cfg80211_err(" SSID Len %d is not correct for network %d", 477 req->networks_list[i].ssid.length, i); 478 ret = -EINVAL; 479 goto error; 480 } 481 482 qdf_mem_copy(req->networks_list[i].ssid.ssid, 483 request->match_sets[i].ssid.ssid, 484 req->networks_list[i].ssid.length); 485 req->networks_list[i].authentication = 0; /*eAUTH_TYPE_ANY */ 486 req->networks_list[i].encryption = 0; /*eED_ANY */ 487 req->networks_list[i].bc_new_type = 0; /*eBCAST_UNKNOWN */ 488 489 cfg80211_notice("Received ssid:%.*s", 490 req->networks_list[i].ssid.length, 491 req->networks_list[i].ssid.ssid); 492 493 /*Copying list of valid channel into request */ 494 qdf_mem_copy(req->networks_list[i].channels, valid_ch, 495 num_chan * sizeof(uint32_t)); 496 req->networks_list[i].channel_cnt = num_chan; 497 req->networks_list[i].rssi_thresh = 498 request->match_sets[i].rssi_thold; 499 } 500 501 /* set scan to passive if no SSIDs are specified in the request */ 502 if (0 == request->n_ssids) 503 req->do_passive_scan = true; 504 else 505 req->do_passive_scan = false; 506 507 for (i = 0; i < request->n_ssids; i++) { 508 j = 0; 509 while (j < req->networks_cnt) { 510 if ((req->networks_list[j].ssid.length == 511 request->ssids[i].ssid_len) && 512 (!qdf_mem_cmp(req->networks_list[j].ssid.ssid, 513 request->ssids[i].ssid, 514 req->networks_list[j].ssid.length))) { 515 req->networks_list[j].bc_new_type = 516 SSID_BC_TYPE_HIDDEN; 517 break; 518 } 519 j++; 520 } 521 } 522 cfg80211_notice("Number of hidden networks being Configured = %d", 523 request->n_ssids); 524 525 /* 526 * Before Kernel 4.4 527 * Driver gets only one time interval which is hard coded in 528 * supplicant for 10000ms. 529 * 530 * After Kernel 4.4 531 * User can configure multiple scan_plans, each scan would have 532 * separate scan cycle and interval. (interval is in unit of second.) 533 * For our use case, we would only have supplicant set one scan_plan, 534 * and firmware also support only one as well, so pick up the first 535 * index. 536 * 537 * Taking power consumption into account 538 * firmware after gPNOScanTimerRepeatValue times fast_scan_period 539 * switches slow_scan_period. This is less frequent scans and firmware 540 * shall be in slow_scan_period mode until next PNO Start. 541 */ 542 wlan_config_sched_scan_plan(req, request); 543 req->delay_start_time = hdd_config_sched_scan_start_delay(request); 544 req->scan_backoff_multiplier = scan_backoff_multiplier; 545 cfg80211_notice("Base scan interval: %d sec, scan cycles: %d, slow scan interval %d", 546 req->fast_scan_period, req->fast_scan_max_cycles, 547 req->slow_scan_period); 548 wlan_hdd_sched_scan_update_relative_rssi(req, request); 549 550 psoc = wlan_pdev_get_psoc(pdev); 551 ucfg_scan_register_pno_cb(psoc, 552 wlan_cfg80211_pno_callback, NULL); 553 ucfg_scan_get_pno_def_params(vdev, req); 554 555 if (req->scan_random.randomize) 556 wlan_pno_scan_rand_attr(vdev, request, req); 557 558 if (ucfg_ie_whitelist_enabled(psoc, vdev)) 559 ucfg_copy_ie_whitelist_attrs(psoc, &req->ie_whitelist); 560 status = ucfg_scan_pno_start(vdev, req); 561 if (QDF_IS_STATUS_ERROR(status)) { 562 cfg80211_err("Failed to enable PNO"); 563 ret = -EINVAL; 564 goto error; 565 } 566 567 cfg80211_info("PNO scan request offloaded"); 568 569 error: 570 qdf_mem_free(req); 571 return ret; 572 } 573 574 int wlan_cfg80211_sched_scan_stop(struct wlan_objmgr_vdev *vdev) 575 { 576 QDF_STATUS status; 577 578 status = ucfg_scan_pno_stop(vdev); 579 if (QDF_IS_STATUS_ERROR(status)) 580 cfg80211_err("Failed to disabled PNO"); 581 else 582 cfg80211_info("PNO scan disabled"); 583 584 return 0; 585 } 586 #endif /*FEATURE_WLAN_SCAN_PNO */ 587 588 /** 589 * wlan_copy_bssid_scan_request() - API to copy the bssid to Scan request 590 * @scan_req: Pointer to scan_start_request 591 * @request: scan request from Supplicant 592 * 593 * This API copies the BSSID in scan request from Supplicant and copies it to 594 * the scan_start_request 595 * 596 * Return: None 597 */ 598 #if defined(CFG80211_SCAN_BSSID) || \ 599 (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)) 600 static inline void 601 wlan_copy_bssid_scan_request(struct scan_start_request *scan_req, 602 struct cfg80211_scan_request *request) 603 { 604 qdf_mem_copy(scan_req->scan_req.bssid_list[0].bytes, 605 request->bssid, QDF_MAC_ADDR_SIZE); 606 } 607 #else 608 static inline void 609 wlan_copy_bssid_scan_request(struct scan_start_request *scan_req, 610 struct cfg80211_scan_request *request) 611 { 612 613 } 614 #endif 615 616 /** 617 * wlan_scan_request_enqueue() - enqueue Scan Request 618 * @pdev: pointer to pdev object 619 * @req: Pointer to the scan request 620 * @source: source of the scan request 621 * @scan_id: scan identifier 622 * 623 * Enqueue scan request in the global scan list.This list 624 * stores the active scan request information. 625 * 626 * Return: 0 on success, error number otherwise 627 */ 628 static int wlan_scan_request_enqueue(struct wlan_objmgr_pdev *pdev, 629 struct cfg80211_scan_request *req, 630 uint8_t source, uint32_t scan_id) 631 { 632 struct scan_req *scan_req; 633 QDF_STATUS status; 634 struct pdev_osif_priv *osif_ctx; 635 struct osif_scan_pdev *osif_scan; 636 637 scan_req = qdf_mem_malloc(sizeof(*scan_req)); 638 if (NULL == scan_req) { 639 cfg80211_alert("malloc failed for Scan req"); 640 return -ENOMEM; 641 } 642 643 /* Get NL global context from objmgr*/ 644 osif_ctx = wlan_pdev_get_ospriv(pdev); 645 osif_scan = osif_ctx->osif_scan; 646 scan_req->scan_request = req; 647 scan_req->source = source; 648 scan_req->scan_id = scan_id; 649 scan_req->dev = req->wdev->netdev; 650 651 qdf_mutex_acquire(&osif_scan->scan_req_q_lock); 652 status = qdf_list_insert_back(&osif_scan->scan_req_q, 653 &scan_req->node); 654 qdf_mutex_release(&osif_scan->scan_req_q_lock); 655 if (QDF_STATUS_SUCCESS != status) { 656 cfg80211_err("Failed to enqueue Scan Req"); 657 qdf_mem_free(scan_req); 658 return -EINVAL; 659 } 660 661 return 0; 662 } 663 664 /** 665 * wlan_scan_request_dequeue() - dequeue scan request 666 * @nl_ctx: Global HDD context 667 * @scan_id: scan id 668 * @req: scan request 669 * @dev: net device 670 * @source : returns source of the scan request 671 * 672 * Return: QDF_STATUS 673 */ 674 static QDF_STATUS wlan_scan_request_dequeue( 675 struct wlan_objmgr_pdev *pdev, 676 uint32_t scan_id, struct cfg80211_scan_request **req, 677 uint8_t *source, struct net_device **dev) 678 { 679 QDF_STATUS status = QDF_STATUS_E_FAILURE; 680 struct scan_req *scan_req; 681 qdf_list_node_t *node = NULL, *next_node = NULL; 682 struct pdev_osif_priv *osif_ctx; 683 struct osif_scan_pdev *scan_priv; 684 685 cfg80211_debug("Dequeue Scan id: %d", scan_id); 686 687 if ((source == NULL) || (req == NULL)) { 688 cfg80211_err("source or request is NULL"); 689 return QDF_STATUS_E_NULL_VALUE; 690 } 691 692 /* Get NL global context from objmgr*/ 693 osif_ctx = wlan_pdev_get_ospriv(pdev); 694 if (!osif_ctx) { 695 cfg80211_err("Failed to retrieve osif context"); 696 return status; 697 } 698 scan_priv = osif_ctx->osif_scan; 699 700 if (qdf_list_empty(&scan_priv->scan_req_q)) { 701 cfg80211_info("Scan List is empty"); 702 return QDF_STATUS_E_FAILURE; 703 } 704 705 qdf_mutex_acquire(&scan_priv->scan_req_q_lock); 706 if (QDF_STATUS_SUCCESS != 707 qdf_list_peek_front(&scan_priv->scan_req_q, &next_node)) { 708 qdf_mutex_release(&scan_priv->scan_req_q_lock); 709 cfg80211_err("Failed to remove Scan Req from queue"); 710 return QDF_STATUS_E_FAILURE; 711 } 712 713 do { 714 node = next_node; 715 scan_req = qdf_container_of(node, struct scan_req, 716 node); 717 if (scan_req->scan_id == scan_id) { 718 status = qdf_list_remove_node(&scan_priv->scan_req_q, 719 node); 720 if (status == QDF_STATUS_SUCCESS) { 721 *req = scan_req->scan_request; 722 *source = scan_req->source; 723 *dev = scan_req->dev; 724 qdf_mem_free(scan_req); 725 qdf_mutex_release(&scan_priv->scan_req_q_lock); 726 cfg80211_debug("removed Scan id: %d, req = %pK, pending scans %d", 727 scan_id, req, 728 qdf_list_size(&scan_priv-> 729 scan_req_q)); 730 return QDF_STATUS_SUCCESS; 731 } else { 732 qdf_mutex_release(&scan_priv->scan_req_q_lock); 733 cfg80211_err("Failed to remove node scan id %d, pending scans %d", 734 scan_id, 735 qdf_list_size(&scan_priv->scan_req_q)); 736 return status; 737 } 738 } 739 } while (QDF_STATUS_SUCCESS == 740 qdf_list_peek_next(&scan_priv->scan_req_q, node, &next_node)); 741 qdf_mutex_release(&scan_priv->scan_req_q_lock); 742 cfg80211_err("Failed to find scan id %d", scan_id); 743 744 return status; 745 } 746 747 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)) 748 /** 749 * wlan_cfg80211_scan_done() - Scan completed callback to cfg80211 750 * @netdev: Net device 751 * @req : Scan request 752 * @aborted : true scan aborted false scan success 753 * 754 * This function notifies scan done to cfg80211 755 * 756 * Return: none 757 */ 758 static void wlan_cfg80211_scan_done(struct net_device *netdev, 759 struct cfg80211_scan_request *req, 760 bool aborted) 761 { 762 struct cfg80211_scan_info info = { 763 .aborted = aborted 764 }; 765 766 if (netdev->flags & IFF_UP) 767 cfg80211_scan_done(req, &info); 768 } 769 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)) 770 /** 771 * wlan_cfg80211_scan_done() - Scan completed callback to cfg80211 772 * @netdev: Net device 773 * @req : Scan request 774 * @aborted : true scan aborted false scan success 775 * 776 * This function notifies scan done to cfg80211 777 * 778 * Return: none 779 */ 780 static void wlan_cfg80211_scan_done(struct net_device *netdev, 781 struct cfg80211_scan_request *req, 782 bool aborted) 783 { 784 if (netdev->flags & IFF_UP) 785 cfg80211_scan_done(req, aborted); 786 } 787 #endif 788 789 /** 790 * wlan_vendor_scan_callback() - Scan completed callback event 791 * 792 * @req : Scan request 793 * @aborted : true scan aborted false scan success 794 * 795 * This function sends scan completed callback event to NL. 796 * 797 * Return: none 798 */ 799 static void wlan_vendor_scan_callback(struct cfg80211_scan_request *req, 800 bool aborted) 801 { 802 struct sk_buff *skb; 803 struct nlattr *attr; 804 int i; 805 uint8_t scan_status; 806 uint64_t cookie; 807 808 skb = cfg80211_vendor_event_alloc(req->wdev->wiphy, req->wdev, 809 SCAN_DONE_EVENT_BUF_SIZE + 4 + NLMSG_HDRLEN, 810 QCA_NL80211_VENDOR_SUBCMD_SCAN_DONE_INDEX, 811 GFP_ATOMIC); 812 813 if (!skb) { 814 cfg80211_err("skb alloc failed"); 815 qdf_mem_free(req); 816 return; 817 } 818 819 cookie = (uintptr_t)req; 820 821 attr = nla_nest_start(skb, QCA_WLAN_VENDOR_ATTR_SCAN_SSIDS); 822 if (!attr) 823 goto nla_put_failure; 824 for (i = 0; i < req->n_ssids; i++) { 825 if (nla_put(skb, i, req->ssids[i].ssid_len, req->ssids[i].ssid)) 826 goto nla_put_failure; 827 } 828 nla_nest_end(skb, attr); 829 830 attr = nla_nest_start(skb, QCA_WLAN_VENDOR_ATTR_SCAN_FREQUENCIES); 831 if (!attr) 832 goto nla_put_failure; 833 for (i = 0; i < req->n_channels; i++) { 834 if (nla_put_u32(skb, i, req->channels[i]->center_freq)) 835 goto nla_put_failure; 836 } 837 nla_nest_end(skb, attr); 838 839 if (req->ie && 840 nla_put(skb, QCA_WLAN_VENDOR_ATTR_SCAN_IE, req->ie_len, 841 req->ie)) 842 goto nla_put_failure; 843 844 if (req->flags && 845 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_SCAN_FLAGS, req->flags)) 846 goto nla_put_failure; 847 848 if (wlan_cfg80211_nla_put_u64(skb, QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE, 849 cookie)) 850 goto nla_put_failure; 851 852 scan_status = (aborted == true) ? VENDOR_SCAN_STATUS_ABORTED : 853 VENDOR_SCAN_STATUS_NEW_RESULTS; 854 if (nla_put_u8(skb, QCA_WLAN_VENDOR_ATTR_SCAN_STATUS, scan_status)) 855 goto nla_put_failure; 856 857 cfg80211_vendor_event(skb, GFP_ATOMIC); 858 qdf_mem_free(req); 859 860 return; 861 862 nla_put_failure: 863 kfree_skb(skb); 864 qdf_mem_free(req); 865 } 866 867 868 /** 869 * wlan_cfg80211_scan_done_callback() - scan done callback function called after 870 * scan is finished 871 * @vdev: vdev ptr 872 * @event: Scan event 873 * @args: Scan cb arg 874 * 875 * Return: void 876 */ 877 static void wlan_cfg80211_scan_done_callback( 878 struct wlan_objmgr_vdev *vdev, 879 struct scan_event *event, 880 void *args) 881 { 882 struct cfg80211_scan_request *req = NULL; 883 bool success = false; 884 uint32_t scan_id = event->scan_id; 885 uint8_t source = NL_SCAN; 886 struct wlan_objmgr_pdev *pdev; 887 struct pdev_osif_priv *osif_priv; 888 struct net_device *netdev = NULL; 889 QDF_STATUS status; 890 891 qdf_mtrace(QDF_MODULE_ID_SCAN, QDF_MODULE_ID_OS_IF, event->type, 892 event->vdev_id, event->scan_id); 893 894 if (!util_is_scan_completed(event, &success)) 895 return; 896 897 cfg80211_debug("scan ID = %d vdev id = %d, event type %s(%d) reason = %s(%d)", 898 scan_id, event->vdev_id, 899 util_scan_get_ev_type_name(event->type), event->type, 900 util_scan_get_ev_reason_name(event->reason), 901 event->reason); 902 903 pdev = wlan_vdev_get_pdev(vdev); 904 status = wlan_scan_request_dequeue( 905 pdev, scan_id, &req, &source, &netdev); 906 if (QDF_IS_STATUS_ERROR(status)) { 907 cfg80211_err("Dequeue of scan request failed ID: %d", scan_id); 908 goto allow_suspend; 909 } 910 911 if (!netdev) { 912 cfg80211_err("net dev is NULL,Drop scan event Id: %d", 913 scan_id); 914 goto allow_suspend; 915 } 916 917 /* Make sure vdev is active */ 918 status = wlan_objmgr_vdev_try_get_ref(vdev, WLAN_OSIF_ID); 919 if (QDF_IS_STATUS_ERROR(status)) { 920 cfg80211_err("Failed to get vdev reference: scan Id: %d", 921 scan_id); 922 goto allow_suspend; 923 } 924 925 /* 926 * Scan can be triggred from NL or vendor scan 927 * - If scan is triggered from NL then cfg80211 scan done should be 928 * called to updated scan completion to NL. 929 * - If scan is triggred through vendor command then 930 * scan done event will be posted 931 */ 932 if (NL_SCAN == source) 933 wlan_cfg80211_scan_done(netdev, req, !success); 934 else 935 wlan_vendor_scan_callback(req, !success); 936 937 wlan_objmgr_vdev_release_ref(vdev, WLAN_OSIF_ID); 938 allow_suspend: 939 osif_priv = wlan_pdev_get_ospriv(pdev); 940 if (qdf_list_empty(&osif_priv->osif_scan->scan_req_q)) 941 qdf_runtime_pm_allow_suspend( 942 &osif_priv->osif_scan->runtime_pm_lock); 943 944 } 945 946 QDF_STATUS wlan_scan_runtime_pm_init(struct wlan_objmgr_pdev *pdev) 947 { 948 struct pdev_osif_priv *osif_priv; 949 struct osif_scan_pdev *scan_priv; 950 951 wlan_pdev_obj_lock(pdev); 952 osif_priv = wlan_pdev_get_ospriv(pdev); 953 wlan_pdev_obj_unlock(pdev); 954 955 scan_priv = osif_priv->osif_scan; 956 957 return qdf_runtime_lock_init(&scan_priv->runtime_pm_lock); 958 } 959 960 void wlan_scan_runtime_pm_deinit(struct wlan_objmgr_pdev *pdev) 961 { 962 struct pdev_osif_priv *osif_priv; 963 struct osif_scan_pdev *scan_priv; 964 965 wlan_pdev_obj_lock(pdev); 966 osif_priv = wlan_pdev_get_ospriv(pdev); 967 wlan_pdev_obj_unlock(pdev); 968 969 scan_priv = osif_priv->osif_scan; 970 qdf_runtime_lock_deinit(&scan_priv->runtime_pm_lock); 971 } 972 973 QDF_STATUS wlan_cfg80211_scan_priv_init(struct wlan_objmgr_pdev *pdev) 974 { 975 struct pdev_osif_priv *osif_priv; 976 struct osif_scan_pdev *scan_priv; 977 struct wlan_objmgr_psoc *psoc; 978 wlan_scan_requester req_id; 979 980 psoc = wlan_pdev_get_psoc(pdev); 981 982 req_id = ucfg_scan_register_requester(psoc, "CFG", 983 wlan_cfg80211_scan_done_callback, NULL); 984 985 osif_priv = wlan_pdev_get_ospriv(pdev); 986 scan_priv = qdf_mem_malloc(sizeof(*scan_priv)); 987 if (!scan_priv) { 988 cfg80211_err("failed to allocate memory"); 989 return QDF_STATUS_E_NOMEM; 990 } 991 /* Initialize the scan request queue */ 992 osif_priv->osif_scan = scan_priv; 993 qdf_list_create(&scan_priv->scan_req_q, WLAN_MAX_SCAN_COUNT); 994 qdf_mutex_create(&scan_priv->scan_req_q_lock); 995 scan_priv->req_id = req_id; 996 997 return QDF_STATUS_SUCCESS; 998 } 999 1000 QDF_STATUS wlan_cfg80211_scan_priv_deinit(struct wlan_objmgr_pdev *pdev) 1001 { 1002 struct pdev_osif_priv *osif_priv; 1003 struct osif_scan_pdev *scan_priv; 1004 struct wlan_objmgr_psoc *psoc; 1005 1006 psoc = wlan_pdev_get_psoc(pdev); 1007 osif_priv = wlan_pdev_get_ospriv(pdev); 1008 1009 wlan_cfg80211_cleanup_scan_queue(pdev, NULL); 1010 scan_priv = osif_priv->osif_scan; 1011 ucfg_scan_unregister_requester(psoc, scan_priv->req_id); 1012 qdf_list_destroy(&scan_priv->scan_req_q); 1013 qdf_mutex_destroy(&scan_priv->scan_req_q_lock); 1014 qdf_mem_free(scan_priv); 1015 osif_priv->osif_scan = NULL; 1016 1017 return QDF_STATUS_SUCCESS; 1018 } 1019 1020 /** 1021 * wlan_cfg80211_enqueue_for_cleanup() - Function to populate scan cleanup queue 1022 * @scan_cleanup_q: Scan cleanup queue to be populated 1023 * @scan_priv: Pointer to scan related data used by cfg80211 scan 1024 * @dev: Netdevice pointer 1025 * 1026 * The function synchrounously iterates through the global scan queue to 1027 * identify entries that have to be cleaned up, copies identified entries 1028 * to another queue(to send scan complete event to NL later) and removes the 1029 * entry from the global scan queue. 1030 * 1031 * Return: None 1032 */ 1033 static void 1034 wlan_cfg80211_enqueue_for_cleanup(qdf_list_t *scan_cleanup_q, 1035 struct osif_scan_pdev *scan_priv, 1036 struct net_device *dev) 1037 { 1038 struct scan_req *scan_req, *scan_cleanup; 1039 qdf_list_node_t *node = NULL, *next_node = NULL; 1040 1041 qdf_mutex_acquire(&scan_priv->scan_req_q_lock); 1042 if (QDF_STATUS_SUCCESS != 1043 qdf_list_peek_front(&scan_priv->scan_req_q, 1044 &node)) { 1045 qdf_mutex_release(&scan_priv->scan_req_q_lock); 1046 return; 1047 } 1048 1049 while (node) { 1050 /* 1051 * Keep track of the next node, to traverse through the list 1052 * in the event of the current node being deleted. 1053 */ 1054 qdf_list_peek_next(&scan_priv->scan_req_q, 1055 node, &next_node); 1056 scan_req = qdf_container_of(node, struct scan_req, node); 1057 if (!dev || (dev == scan_req->dev)) { 1058 scan_cleanup = qdf_mem_malloc(sizeof(struct scan_req)); 1059 if (!scan_cleanup) { 1060 qdf_mutex_release(&scan_priv->scan_req_q_lock); 1061 cfg80211_err("Failed to allocate memory"); 1062 return; 1063 } 1064 scan_cleanup->scan_request = scan_req->scan_request; 1065 scan_cleanup->scan_id = scan_req->scan_id; 1066 scan_cleanup->source = scan_req->source; 1067 scan_cleanup->dev = scan_req->dev; 1068 qdf_list_insert_back(scan_cleanup_q, 1069 &scan_cleanup->node); 1070 if (QDF_STATUS_SUCCESS != 1071 qdf_list_remove_node(&scan_priv->scan_req_q, 1072 node)) { 1073 qdf_mutex_release(&scan_priv->scan_req_q_lock); 1074 cfg80211_err("Failed to remove scan request"); 1075 return; 1076 } 1077 qdf_mem_free(scan_req); 1078 } 1079 node = next_node; 1080 next_node = NULL; 1081 } 1082 qdf_mutex_release(&scan_priv->scan_req_q_lock); 1083 } 1084 1085 void wlan_cfg80211_cleanup_scan_queue(struct wlan_objmgr_pdev *pdev, 1086 struct net_device *dev) 1087 { 1088 struct scan_req *scan_req; 1089 struct cfg80211_scan_request *req; 1090 uint8_t source; 1091 bool aborted = true; 1092 struct pdev_osif_priv *osif_priv; 1093 qdf_list_t scan_cleanup_q; 1094 qdf_list_node_t *node = NULL; 1095 1096 if (!pdev) { 1097 cfg80211_err("pdev is Null"); 1098 return; 1099 } 1100 1101 osif_priv = wlan_pdev_get_ospriv(pdev); 1102 1103 /* 1104 * To avoid any race conditions, create a local list to copy all the 1105 * scan entries to be removed and then send scan complete for each of 1106 * the identified entries to NL. 1107 */ 1108 qdf_list_create(&scan_cleanup_q, WLAN_MAX_SCAN_COUNT); 1109 wlan_cfg80211_enqueue_for_cleanup(&scan_cleanup_q, 1110 osif_priv->osif_scan, dev); 1111 1112 while (!qdf_list_empty(&scan_cleanup_q)) { 1113 if (QDF_STATUS_SUCCESS != qdf_list_remove_front(&scan_cleanup_q, 1114 &node)) { 1115 cfg80211_err("Failed to remove scan request"); 1116 return; 1117 } 1118 scan_req = container_of(node, struct scan_req, node); 1119 req = scan_req->scan_request; 1120 source = scan_req->source; 1121 if (NL_SCAN == source) 1122 wlan_cfg80211_scan_done(scan_req->dev, req, 1123 aborted); 1124 else 1125 wlan_vendor_scan_callback(req, aborted); 1126 1127 qdf_mem_free(scan_req); 1128 } 1129 qdf_list_destroy(&scan_cleanup_q); 1130 1131 return; 1132 } 1133 1134 /** 1135 * wlan_cfg80211_update_scan_policy_type_flags() - Set scan flags according to 1136 * scan request 1137 * @scan_req: Pointer to csr scan req 1138 * 1139 * Return: None 1140 */ 1141 #if defined(CFG80211_SCAN_DBS_CONTROL_SUPPORT) || \ 1142 (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)) 1143 static void wlan_cfg80211_update_scan_policy_type_flags( 1144 struct cfg80211_scan_request *req, 1145 struct scan_req_params *scan_req) 1146 { 1147 if (req->flags & NL80211_SCAN_FLAG_HIGH_ACCURACY) 1148 scan_req->scan_policy_high_accuracy = true; 1149 if (req->flags & NL80211_SCAN_FLAG_LOW_SPAN) 1150 scan_req->scan_policy_low_span = true; 1151 if (req->flags & NL80211_SCAN_FLAG_LOW_POWER) 1152 scan_req->scan_policy_low_power = true; 1153 } 1154 #else 1155 static inline void wlan_cfg80211_update_scan_policy_type_flags( 1156 struct cfg80211_scan_request *req, 1157 struct scan_req_params *scan_req) 1158 { 1159 } 1160 #endif 1161 1162 int wlan_cfg80211_scan(struct wlan_objmgr_vdev *vdev, 1163 struct cfg80211_scan_request *request, 1164 struct scan_params *params) 1165 { 1166 struct scan_start_request *req; 1167 struct wlan_ssid *pssid; 1168 uint8_t i; 1169 int ret = 0; 1170 uint8_t num_chan = 0, channel; 1171 uint32_t c_freq; 1172 struct wlan_objmgr_pdev *pdev = wlan_vdev_get_pdev(vdev); 1173 wlan_scan_requester req_id; 1174 struct pdev_osif_priv *osif_priv; 1175 struct wlan_objmgr_psoc *psoc; 1176 wlan_scan_id scan_id; 1177 bool is_p2p_scan = false; 1178 enum wlan_band band; 1179 struct net_device *netdev = NULL; 1180 QDF_STATUS qdf_status; 1181 1182 psoc = wlan_pdev_get_psoc(pdev); 1183 if (!psoc) { 1184 cfg80211_err("Invalid psoc object"); 1185 return -EINVAL; 1186 } 1187 req = qdf_mem_malloc(sizeof(*req)); 1188 if (!req) { 1189 cfg80211_err("Failed to allocate scan request memory"); 1190 return -EINVAL; 1191 } 1192 /* Initialize the scan global params */ 1193 ucfg_scan_init_default_params(vdev, req); 1194 1195 /* Get NL global context from objmgr*/ 1196 osif_priv = wlan_pdev_get_ospriv(pdev); 1197 req_id = osif_priv->osif_scan->req_id; 1198 scan_id = ucfg_scan_get_scan_id(psoc); 1199 if (!scan_id) { 1200 cfg80211_err("Invalid scan id"); 1201 qdf_mem_free(req); 1202 return -EINVAL; 1203 } 1204 /* fill the scan request structure */ 1205 req->vdev = vdev; 1206 req->scan_req.vdev_id = wlan_vdev_get_id(vdev); 1207 req->scan_req.scan_id = scan_id; 1208 req->scan_req.scan_req_id = req_id; 1209 1210 /* Update scan policy type flags according to cfg scan request */ 1211 wlan_cfg80211_update_scan_policy_type_flags(request, 1212 &req->scan_req); 1213 /* 1214 * Even though supplicant doesn't provide any SSIDs, n_ssids is 1215 * set to 1. Because of this, driver is assuming that this is not 1216 * wildcard scan and so is not aging out the scan results. 1217 */ 1218 if ((request->ssids) && (request->n_ssids == 1) && 1219 ('\0' == request->ssids->ssid[0])) { 1220 request->n_ssids = 0; 1221 } 1222 1223 if ((request->ssids) && (0 < request->n_ssids)) { 1224 int j; 1225 req->scan_req.num_ssids = request->n_ssids; 1226 1227 if (req->scan_req.num_ssids > WLAN_SCAN_MAX_NUM_SSID) { 1228 cfg80211_info("number of ssid received %d is greater than MAX %d so copy only MAX nuber of SSIDs", 1229 req->scan_req.num_ssids, 1230 WLAN_SCAN_MAX_NUM_SSID); 1231 req->scan_req.num_ssids = WLAN_SCAN_MAX_NUM_SSID; 1232 } 1233 /* copy all the ssid's and their length */ 1234 for (j = 0; j < req->scan_req.num_ssids; j++) { 1235 pssid = &req->scan_req.ssid[j]; 1236 /* get the ssid length */ 1237 pssid->length = request->ssids[j].ssid_len; 1238 if (pssid->length > WLAN_SSID_MAX_LEN) 1239 pssid->length = WLAN_SSID_MAX_LEN; 1240 qdf_mem_copy(pssid->ssid, 1241 &request->ssids[j].ssid[0], 1242 pssid->length); 1243 cfg80211_info("SSID number %d: %.*s", j, pssid->length, 1244 pssid->ssid); 1245 } 1246 } 1247 if (request->ssids || 1248 (wlan_vdev_mlme_get_opmode(vdev) == QDF_P2P_GO_MODE)) 1249 req->scan_req.scan_f_passive = false; 1250 1251 if (params->half_rate) 1252 req->scan_req.scan_f_half_rate = true; 1253 else if (params->quarter_rate) 1254 req->scan_req.scan_f_quarter_rate = true; 1255 1256 if (params->strict_pscan) 1257 req->scan_req.scan_f_strict_passive_pch = true; 1258 1259 if ((request->n_ssids == 1) && request->ssids && 1260 !qdf_mem_cmp(&request->ssids[0], "DIRECT-", 7)) 1261 is_p2p_scan = true; 1262 1263 if (is_p2p_scan && request->no_cck) 1264 req->scan_req.p2p_scan_type = SCAN_P2P_SEARCH; 1265 1266 /* Set dwell time mode according to scan policy type flags */ 1267 if (req->scan_req.scan_policy_high_accuracy) 1268 req->scan_req.adaptive_dwell_time_mode = 1269 SCAN_DWELL_MODE_STATIC; 1270 if ((req->scan_req.scan_policy_low_power) || 1271 (req->scan_req.scan_policy_low_span)) 1272 req->scan_req.adaptive_dwell_time_mode = 1273 SCAN_DWELL_MODE_AGGRESSIVE; 1274 1275 /* 1276 * FW require at least 1 MAC to send probe request. 1277 * If MAC is all 0 set it to BC addr as this is the address on 1278 * which fw will send probe req. 1279 */ 1280 req->scan_req.num_bssid = 1; 1281 wlan_copy_bssid_scan_request(req, request); 1282 if (qdf_is_macaddr_zero(&req->scan_req.bssid_list[0])) 1283 qdf_set_macaddr_broadcast(&req->scan_req.bssid_list[0]); 1284 1285 if (request->n_channels) { 1286 char chl[(request->n_channels * 5) + 1]; 1287 int len = 0; 1288 #ifdef WLAN_POLICY_MGR_ENABLE 1289 bool ap_or_go_present = 1290 policy_mgr_mode_specific_connection_count( 1291 psoc, PM_SAP_MODE, NULL) || 1292 policy_mgr_mode_specific_connection_count( 1293 psoc, PM_P2P_GO_MODE, NULL); 1294 #endif 1295 1296 for (i = 0; i < request->n_channels; i++) { 1297 channel = request->channels[i]->hw_value; 1298 c_freq = wlan_reg_chan_to_freq(pdev, channel); 1299 if (wlan_reg_is_dsrc_chan(pdev, channel)) 1300 continue; 1301 #ifdef WLAN_POLICY_MGR_ENABLE 1302 if (ap_or_go_present) { 1303 bool ok; 1304 1305 qdf_status = 1306 policy_mgr_is_chan_ok_for_dnbs(psoc, 1307 channel, 1308 &ok); 1309 1310 if (QDF_IS_STATUS_ERROR(qdf_status)) { 1311 cfg80211_err("DNBS check failed"); 1312 qdf_mem_free(req); 1313 ret = -EINVAL; 1314 goto end; 1315 } 1316 if (!ok) 1317 continue; 1318 } 1319 #endif 1320 len += snprintf(chl + len, 5, "%d ", channel); 1321 req->scan_req.chan_list.chan[num_chan].freq = c_freq; 1322 band = util_scan_scm_freq_to_band(c_freq); 1323 if (band == WLAN_BAND_2_4_GHZ) 1324 req->scan_req.chan_list.chan[num_chan].phymode = 1325 SCAN_PHY_MODE_11G; 1326 else 1327 req->scan_req.chan_list.chan[num_chan].phymode = 1328 SCAN_PHY_MODE_11A; 1329 num_chan++; 1330 if (num_chan >= WLAN_SCAN_MAX_NUM_CHANNELS) 1331 break; 1332 } 1333 cfg80211_info("Channel-List: %s", chl); 1334 cfg80211_info("No. of Scan Channels: %d", num_chan); 1335 } 1336 if (!num_chan) { 1337 cfg80211_err("Received zero non-dsrc channels"); 1338 qdf_mem_free(req); 1339 ret = -EINVAL; 1340 goto end; 1341 } 1342 req->scan_req.chan_list.num_chan = num_chan; 1343 1344 /* P2P increase the scan priority */ 1345 if (is_p2p_scan) 1346 req->scan_req.scan_priority = SCAN_PRIORITY_HIGH; 1347 if (request->ie_len) { 1348 req->scan_req.extraie.ptr = qdf_mem_malloc(request->ie_len); 1349 if (!req->scan_req.extraie.ptr) { 1350 cfg80211_err("Failed to allocate memory"); 1351 ret = -ENOMEM; 1352 qdf_mem_free(req); 1353 goto end; 1354 } 1355 req->scan_req.extraie.len = request->ie_len; 1356 qdf_mem_copy(req->scan_req.extraie.ptr, request->ie, 1357 request->ie_len); 1358 } else if (params->default_ie.ptr && params->default_ie.len) { 1359 req->scan_req.extraie.ptr = 1360 qdf_mem_malloc(params->default_ie.len); 1361 if (!req->scan_req.extraie.ptr) { 1362 cfg80211_err("Failed to allocate memory"); 1363 ret = -ENOMEM; 1364 qdf_mem_free(req); 1365 goto end; 1366 } 1367 req->scan_req.extraie.len = params->default_ie.len; 1368 qdf_mem_copy(req->scan_req.extraie.ptr, params->default_ie.ptr, 1369 params->default_ie.len); 1370 } 1371 1372 if (!is_p2p_scan) { 1373 if (req->scan_req.scan_random.randomize) 1374 wlan_scan_rand_attrs(vdev, request, req); 1375 if (ucfg_ie_whitelist_enabled(psoc, vdev) && 1376 ucfg_copy_ie_whitelist_attrs(psoc, 1377 &req->scan_req.ie_whitelist)) 1378 req->scan_req.scan_f_en_ie_whitelist_in_probe = true; 1379 } 1380 1381 if (request->flags & NL80211_SCAN_FLAG_FLUSH) 1382 ucfg_scan_flush_results(pdev, NULL); 1383 1384 /* Enqueue the scan request */ 1385 wlan_scan_request_enqueue(pdev, request, params->source, 1386 req->scan_req.scan_id); 1387 1388 qdf_runtime_pm_prevent_suspend( 1389 &osif_priv->osif_scan->runtime_pm_lock); 1390 1391 qdf_status = ucfg_scan_start(req); 1392 if (QDF_IS_STATUS_ERROR(qdf_status)) { 1393 cfg80211_err("ucfg_scan_start returned error %d", qdf_status); 1394 if (qdf_status == QDF_STATUS_E_RESOURCES) 1395 cfg80211_err("HO is in progress.So defer the scan by informing busy"); 1396 wlan_scan_request_dequeue(pdev, scan_id, &request, 1397 ¶ms->source, &netdev); 1398 if (qdf_list_empty(&osif_priv->osif_scan->scan_req_q)) 1399 qdf_runtime_pm_allow_suspend( 1400 &osif_priv->osif_scan->runtime_pm_lock); 1401 } 1402 ret = qdf_status_to_os_return(qdf_status); 1403 1404 end: 1405 return ret; 1406 } 1407 1408 /** 1409 * wlan_get_scanid() - API to get the scan id 1410 * from the scan cookie attribute. 1411 * @pdev: Pointer to pdev object 1412 * @scan_id: Pointer to scan id 1413 * @cookie : Scan cookie attribute 1414 * 1415 * API to get the scan id from the scan cookie attribute 1416 * sent from supplicant by matching scan request. 1417 * 1418 * Return: 0 for success, non zero for failure 1419 */ 1420 static int wlan_get_scanid(struct wlan_objmgr_pdev *pdev, 1421 uint32_t *scan_id, uint64_t cookie) 1422 { 1423 struct scan_req *scan_req; 1424 qdf_list_node_t *node = NULL; 1425 qdf_list_node_t *ptr_node = NULL; 1426 int ret = -EINVAL; 1427 struct pdev_osif_priv *osif_ctx; 1428 struct osif_scan_pdev *scan_priv; 1429 1430 /* Get NL global context from objmgr*/ 1431 osif_ctx = wlan_pdev_get_ospriv(pdev); 1432 if (!osif_ctx) { 1433 cfg80211_err("Failed to retrieve osif context"); 1434 return ret; 1435 } 1436 scan_priv = osif_ctx->osif_scan; 1437 qdf_mutex_acquire(&scan_priv->scan_req_q_lock); 1438 if (qdf_list_empty(&scan_priv->scan_req_q)) { 1439 qdf_mutex_release(&scan_priv->scan_req_q_lock); 1440 cfg80211_err("Failed to retrieve scan id"); 1441 return ret; 1442 } 1443 1444 if (QDF_STATUS_SUCCESS != 1445 qdf_list_peek_front(&scan_priv->scan_req_q, 1446 &ptr_node)) { 1447 qdf_mutex_release(&scan_priv->scan_req_q_lock); 1448 return ret; 1449 } 1450 1451 do { 1452 node = ptr_node; 1453 scan_req = qdf_container_of(node, struct scan_req, node); 1454 if (cookie == 1455 (uintptr_t)(scan_req->scan_request)) { 1456 *scan_id = scan_req->scan_id; 1457 ret = 0; 1458 break; 1459 } 1460 } while (QDF_STATUS_SUCCESS == 1461 qdf_list_peek_next(&scan_priv->scan_req_q, 1462 node, &ptr_node)); 1463 1464 qdf_mutex_release(&scan_priv->scan_req_q_lock); 1465 1466 return ret; 1467 } 1468 1469 QDF_STATUS wlan_abort_scan(struct wlan_objmgr_pdev *pdev, 1470 uint32_t pdev_id, uint32_t vdev_id, 1471 wlan_scan_id scan_id, bool sync) 1472 { 1473 struct scan_cancel_request *req; 1474 struct pdev_osif_priv *osif_ctx; 1475 struct osif_scan_pdev *scan_priv; 1476 QDF_STATUS status; 1477 struct wlan_objmgr_vdev *vdev; 1478 1479 req = qdf_mem_malloc(sizeof(*req)); 1480 if (!req) { 1481 cfg80211_err("Failed to allocate memory"); 1482 return QDF_STATUS_E_NOMEM; 1483 } 1484 1485 /* Get NL global context from objmgr*/ 1486 osif_ctx = wlan_pdev_get_ospriv(pdev); 1487 if (!osif_ctx) { 1488 cfg80211_err("Failed to retrieve osif context"); 1489 qdf_mem_free(req); 1490 return QDF_STATUS_E_FAILURE; 1491 } 1492 if (vdev_id == INVAL_VDEV_ID) 1493 vdev = wlan_objmgr_pdev_get_first_vdev(pdev, WLAN_OSIF_ID); 1494 else 1495 vdev = wlan_objmgr_get_vdev_by_id_from_pdev(pdev, 1496 vdev_id, WLAN_OSIF_ID); 1497 1498 if (!vdev) { 1499 cfg80211_err("Failed get vdev"); 1500 qdf_mem_free(req); 1501 return QDF_STATUS_E_INVAL; 1502 } 1503 scan_priv = osif_ctx->osif_scan; 1504 req->cancel_req.requester = scan_priv->req_id; 1505 req->vdev = vdev; 1506 req->cancel_req.scan_id = scan_id; 1507 req->cancel_req.pdev_id = pdev_id; 1508 req->cancel_req.vdev_id = vdev_id; 1509 if (scan_id != INVAL_SCAN_ID) 1510 req->cancel_req.req_type = WLAN_SCAN_CANCEL_SINGLE; 1511 else if (vdev_id == INVAL_VDEV_ID) 1512 req->cancel_req.req_type = WLAN_SCAN_CANCEL_PDEV_ALL; 1513 else 1514 req->cancel_req.req_type = WLAN_SCAN_CANCEL_VDEV_ALL; 1515 1516 if (sync) 1517 status = ucfg_scan_cancel_sync(req); 1518 else 1519 status = ucfg_scan_cancel(req); 1520 if (QDF_IS_STATUS_ERROR(status)) 1521 cfg80211_err("Cancel scan request failed"); 1522 1523 wlan_objmgr_vdev_release_ref(vdev, WLAN_OSIF_ID); 1524 1525 return status; 1526 } 1527 1528 int wlan_cfg80211_abort_scan(struct wlan_objmgr_pdev *pdev) 1529 { 1530 uint8_t pdev_id; 1531 1532 pdev_id = wlan_objmgr_pdev_get_pdev_id(pdev); 1533 1534 if (ucfg_scan_get_pdev_status(pdev) != 1535 SCAN_NOT_IN_PROGRESS) 1536 wlan_abort_scan(pdev, pdev_id, 1537 INVAL_VDEV_ID, INVAL_SCAN_ID, true); 1538 1539 return 0; 1540 } 1541 1542 int wlan_vendor_abort_scan(struct wlan_objmgr_pdev *pdev, 1543 const void *data, int data_len) 1544 { 1545 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_SCAN_MAX + 1]; 1546 int ret = -EINVAL; 1547 wlan_scan_id scan_id; 1548 uint64_t cookie; 1549 uint8_t pdev_id; 1550 1551 pdev_id = wlan_objmgr_pdev_get_pdev_id(pdev); 1552 if (wlan_cfg80211_nla_parse(tb, QCA_WLAN_VENDOR_ATTR_SCAN_MAX, data, 1553 data_len, scan_policy)) { 1554 cfg80211_err("Invalid ATTR"); 1555 return ret; 1556 } 1557 1558 if (tb[QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE]) { 1559 cookie = nla_get_u64( 1560 tb[QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE]); 1561 ret = wlan_get_scanid(pdev, &scan_id, cookie); 1562 if (ret != 0) 1563 return ret; 1564 if (ucfg_scan_get_pdev_status(pdev) != 1565 SCAN_NOT_IN_PROGRESS) 1566 wlan_abort_scan(pdev, INVAL_PDEV_ID, 1567 INVAL_VDEV_ID, scan_id, true); 1568 } 1569 return 0; 1570 } 1571 1572 static inline struct ieee80211_channel * 1573 wlan_get_ieee80211_channel(struct wiphy *wiphy, 1574 struct wlan_objmgr_pdev *pdev, 1575 int chan_no) 1576 { 1577 unsigned int freq; 1578 struct ieee80211_channel *chan; 1579 1580 freq = wlan_reg_chan_to_freq(pdev, chan_no); 1581 chan = ieee80211_get_channel(wiphy, freq); 1582 if (!chan) 1583 cfg80211_err("chan is NULL, chan_no: %d freq: %d", 1584 chan_no, freq); 1585 1586 return chan; 1587 } 1588 1589 #ifdef WLAN_ENABLE_AGEIE_ON_SCAN_RESULTS 1590 static inline int wlan_get_frame_len(struct scan_cache_entry *scan_params) 1591 { 1592 return util_scan_entry_frame_len(scan_params) + sizeof(qcom_ie_age); 1593 } 1594 1595 static inline void wlan_add_age_ie(uint8_t *mgmt_frame, 1596 struct scan_cache_entry *scan_params) 1597 { 1598 qcom_ie_age *qie_age = NULL; 1599 1600 /* GPS Requirement: need age ie per entry. Using vendor specific. */ 1601 /* Assuming this is the last IE, copy at the end */ 1602 qie_age = (qcom_ie_age *) (mgmt_frame + 1603 util_scan_entry_frame_len(scan_params)); 1604 qie_age->element_id = QCOM_VENDOR_IE_ID; 1605 qie_age->len = QCOM_VENDOR_IE_AGE_LEN; 1606 qie_age->oui_1 = QCOM_OUI1; 1607 qie_age->oui_2 = QCOM_OUI2; 1608 qie_age->oui_3 = QCOM_OUI3; 1609 qie_age->type = QCOM_VENDOR_IE_AGE_TYPE; 1610 /* 1611 * Lowi expects the timestamp of bss in units of 1/10 ms. In driver 1612 * all bss related timestamp is in units of ms. Due to this when scan 1613 * results are sent to lowi the scan age is high.To address this, 1614 * send age in units of 1/10 ms. 1615 */ 1616 qie_age->age = 1617 (uint32_t)(qdf_mc_timer_get_system_time() - 1618 scan_params->scan_entry_time)/10; 1619 qie_age->tsf_delta = scan_params->tsf_delta; 1620 memcpy(&qie_age->beacon_tsf, scan_params->tsf_info.data, 1621 sizeof(qie_age->beacon_tsf)); 1622 memcpy(&qie_age->seq_ctrl, &scan_params->seq_num, 1623 sizeof(qie_age->seq_ctrl)); 1624 } 1625 #else 1626 static inline int wlan_get_frame_len(struct scan_cache_entry *scan_params) 1627 { 1628 return util_scan_entry_frame_len(scan_params); 1629 } 1630 1631 static inline void wlan_add_age_ie(uint8_t *mgmt_frame, 1632 struct scan_cache_entry *scan_params) 1633 { 1634 } 1635 #endif /* WLAN_ENABLE_AGEIE_ON_SCAN_RESULTS */ 1636 1637 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) || \ 1638 defined(CFG80211_INFORM_BSS_FRAME_DATA) 1639 /** 1640 * wlan_fill_per_chain_rssi() - fill per chain RSSI in inform bss 1641 * @data: bss data 1642 * @per_chain_snr: per chain RSSI 1643 * 1644 * Return: void 1645 */ 1646 #if defined(CFG80211_SCAN_PER_CHAIN_RSSI_SUPPORT) || \ 1647 (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)) 1648 static void wlan_fill_per_chain_rssi(struct cfg80211_inform_bss *data, 1649 struct wlan_cfg80211_inform_bss *bss) 1650 { 1651 1652 uint32_t i; 1653 1654 if (!bss || !data) { 1655 cfg80211_err("Received bss is NULL"); 1656 return; 1657 } 1658 for (i = 0; i < WLAN_MGMT_TXRX_HOST_MAX_ANTENNA; i++) { 1659 if (!bss->per_chain_rssi[i] || 1660 (bss->per_chain_rssi[i] == WLAN_INVALID_PER_CHAIN_RSSI)) 1661 continue; 1662 data->chain_signal[i] = bss->per_chain_rssi[i]; 1663 data->chains |= BIT(i); 1664 } 1665 } 1666 #else 1667 static inline void 1668 wlan_fill_per_chain_rssi(struct cfg80211_inform_bss *data, 1669 struct wlan_cfg80211_inform_bss *bss) 1670 { 1671 } 1672 #endif 1673 1674 struct cfg80211_bss * 1675 wlan_cfg80211_inform_bss_frame_data(struct wiphy *wiphy, 1676 struct wlan_cfg80211_inform_bss *bss) 1677 { 1678 struct cfg80211_inform_bss data = {0}; 1679 1680 if (!bss) { 1681 cfg80211_err("bss is null"); 1682 return NULL; 1683 } 1684 wlan_fill_per_chain_rssi(&data, bss); 1685 1686 data.chan = bss->chan; 1687 data.boottime_ns = bss->boottime_ns; 1688 data.signal = bss->rssi; 1689 return cfg80211_inform_bss_frame_data(wiphy, &data, bss->mgmt, 1690 bss->frame_len, GFP_ATOMIC); 1691 } 1692 #else 1693 struct cfg80211_bss * 1694 wlan_cfg80211_inform_bss_frame_data(struct wiphy *wiphy, 1695 struct wlan_cfg80211_inform_bss *bss) 1696 1697 { 1698 return cfg80211_inform_bss_frame(wiphy, bss->chan, bss->mgmt, 1699 bss->frame_len, 1700 bss->rssi, GFP_ATOMIC); 1701 } 1702 #endif 1703 1704 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) 1705 static inline void wlan_cfg80211_put_bss(struct wiphy *wiphy, 1706 struct cfg80211_bss *bss) 1707 { 1708 cfg80211_put_bss(wiphy, bss); 1709 } 1710 #else 1711 static inline void wlan_cfg80211_put_bss(struct wiphy *wiphy, 1712 struct cfg80211_bss *bss) 1713 { 1714 cfg80211_put_bss(bss); 1715 } 1716 #endif 1717 1718 void wlan_cfg80211_inform_bss_frame(struct wlan_objmgr_pdev *pdev, 1719 struct scan_cache_entry *scan_params) 1720 { 1721 struct pdev_osif_priv *pdev_ospriv = wlan_pdev_get_ospriv(pdev); 1722 struct wiphy *wiphy; 1723 struct cfg80211_bss *bss = NULL; 1724 struct wlan_cfg80211_inform_bss bss_data = {0}; 1725 1726 if (!pdev_ospriv) { 1727 cfg80211_err("os_priv is NULL"); 1728 return; 1729 } 1730 1731 wiphy = pdev_ospriv->wiphy; 1732 1733 bss_data.frame_len = wlan_get_frame_len(scan_params); 1734 bss_data.mgmt = qdf_mem_malloc_atomic(bss_data.frame_len); 1735 if (!bss_data.mgmt) { 1736 cfg80211_err("mem alloc failed for bss %pM seq %d", 1737 bss_data.mgmt->bssid, scan_params->seq_num); 1738 return; 1739 } 1740 qdf_mem_copy(bss_data.mgmt, 1741 util_scan_entry_frame_ptr(scan_params), 1742 util_scan_entry_frame_len(scan_params)); 1743 /* 1744 * Android does not want the timestamp from the frame. 1745 * Instead it wants a monotonic increasing value 1746 */ 1747 bss_data.mgmt->u.probe_resp.timestamp = qdf_get_monotonic_boottime(); 1748 wlan_add_age_ie((uint8_t *)bss_data.mgmt, scan_params); 1749 /* 1750 * Based on .ini configuration, raw rssi can be reported for bss. 1751 * Raw rssi is typically used for estimating power. 1752 */ 1753 bss_data.rssi = scan_params->rssi_raw; 1754 1755 bss_data.chan = wlan_get_ieee80211_channel(wiphy, pdev, 1756 scan_params->channel.chan_idx); 1757 if (!bss_data.chan) { 1758 cfg80211_err("Channel not found for bss %pM seq %d chan %d", 1759 bss_data.mgmt->bssid, scan_params->seq_num, 1760 scan_params->channel.chan_idx); 1761 qdf_mem_free(bss_data.mgmt); 1762 return; 1763 } 1764 1765 /* 1766 * Supplicant takes the signal strength in terms of 1767 * mBm (1 dBm = 100 mBm). 1768 */ 1769 bss_data.rssi = QDF_MIN(bss_data.rssi, 0) * 100; 1770 1771 bss_data.boottime_ns = scan_params->boottime_ns; 1772 1773 qdf_mem_copy(bss_data.per_chain_rssi, scan_params->per_chain_rssi, 1774 WLAN_MGMT_TXRX_HOST_MAX_ANTENNA); 1775 1776 bss = wlan_cfg80211_inform_bss_frame_data(wiphy, &bss_data); 1777 if (!bss) 1778 cfg80211_err("failed to inform bss %pM seq %d", 1779 bss_data.mgmt->bssid, scan_params->seq_num); 1780 else 1781 wlan_cfg80211_put_bss(wiphy, bss); 1782 1783 qdf_mem_free(bss_data.mgmt); 1784 } 1785