1 /****************************************************************************** 2 * 3 * GPL LICENSE SUMMARY 4 * 5 * Copyright(c) 2008 - 2014 Intel Corporation. All rights reserved. 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of version 2 of the GNU General Public License as 9 * published by the Free Software Foundation. 10 * 11 * This program is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * General Public License for more details. 15 * 16 * The full GNU General Public License is included in this distribution 17 * in the file called COPYING. 18 * 19 * Contact Information: 20 * Intel Linux Wireless <linuxwifi@intel.com> 21 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 22 *****************************************************************************/ 23 #include <linux/slab.h> 24 #include <linux/types.h> 25 #include <linux/etherdevice.h> 26 #include <net/mac80211.h> 27 28 #include "dev.h" 29 #include "agn.h" 30 31 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after 32 * sending probe req. This should be set long enough to hear probe responses 33 * from more than one AP. */ 34 #define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */ 35 #define IWL_ACTIVE_DWELL_TIME_52 (20) 36 37 #define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3) 38 #define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2) 39 40 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel. 41 * Must be set longer than active dwell time. 42 * For the most reliable scan, set > AP beacon interval (typically 100msec). */ 43 #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */ 44 #define IWL_PASSIVE_DWELL_TIME_52 (10) 45 #define IWL_PASSIVE_DWELL_BASE (100) 46 #define IWL_CHANNEL_TUNE_TIME 5 47 #define MAX_SCAN_CHANNEL 50 48 49 /* For reset radio, need minimal dwell time only */ 50 #define IWL_RADIO_RESET_DWELL_TIME 5 51 52 static int iwl_send_scan_abort(struct iwl_priv *priv) 53 { 54 int ret; 55 struct iwl_host_cmd cmd = { 56 .id = REPLY_SCAN_ABORT_CMD, 57 .flags = CMD_WANT_SKB, 58 }; 59 __le32 *status; 60 61 /* Exit instantly with error when device is not ready 62 * to receive scan abort command or it does not perform 63 * hardware scan currently */ 64 if (!test_bit(STATUS_READY, &priv->status) || 65 !test_bit(STATUS_SCAN_HW, &priv->status) || 66 test_bit(STATUS_FW_ERROR, &priv->status)) 67 return -EIO; 68 69 ret = iwl_dvm_send_cmd(priv, &cmd); 70 if (ret) 71 return ret; 72 73 status = (void *)cmd.resp_pkt->data; 74 if (*status != CAN_ABORT_STATUS) { 75 /* The scan abort will return 1 for success or 76 * 2 for "failure". A failure condition can be 77 * due to simply not being in an active scan which 78 * can occur if we send the scan abort before we 79 * the microcode has notified us that a scan is 80 * completed. */ 81 IWL_DEBUG_SCAN(priv, "SCAN_ABORT ret %d.\n", 82 le32_to_cpu(*status)); 83 ret = -EIO; 84 } 85 86 iwl_free_resp(&cmd); 87 return ret; 88 } 89 90 static void iwl_complete_scan(struct iwl_priv *priv, bool aborted) 91 { 92 struct cfg80211_scan_info info = { 93 .aborted = aborted, 94 }; 95 96 /* check if scan was requested from mac80211 */ 97 if (priv->scan_request) { 98 IWL_DEBUG_SCAN(priv, "Complete scan in mac80211\n"); 99 ieee80211_scan_completed(priv->hw, &info); 100 } 101 102 priv->scan_type = IWL_SCAN_NORMAL; 103 priv->scan_vif = NULL; 104 priv->scan_request = NULL; 105 } 106 107 static void iwl_process_scan_complete(struct iwl_priv *priv) 108 { 109 bool aborted; 110 111 lockdep_assert_held(&priv->mutex); 112 113 if (!test_and_clear_bit(STATUS_SCAN_COMPLETE, &priv->status)) 114 return; 115 116 IWL_DEBUG_SCAN(priv, "Completed scan.\n"); 117 118 cancel_delayed_work(&priv->scan_check); 119 120 aborted = test_and_clear_bit(STATUS_SCAN_ABORTING, &priv->status); 121 if (aborted) 122 IWL_DEBUG_SCAN(priv, "Aborted scan completed.\n"); 123 124 if (!test_and_clear_bit(STATUS_SCANNING, &priv->status)) { 125 IWL_DEBUG_SCAN(priv, "Scan already completed.\n"); 126 goto out_settings; 127 } 128 129 if (priv->scan_type != IWL_SCAN_NORMAL && !aborted) { 130 int err; 131 132 /* Check if mac80211 requested scan during our internal scan */ 133 if (priv->scan_request == NULL) 134 goto out_complete; 135 136 /* If so request a new scan */ 137 err = iwl_scan_initiate(priv, priv->scan_vif, IWL_SCAN_NORMAL, 138 priv->scan_request->channels[0]->band); 139 if (err) { 140 IWL_DEBUG_SCAN(priv, 141 "failed to initiate pending scan: %d\n", err); 142 aborted = true; 143 goto out_complete; 144 } 145 146 return; 147 } 148 149 out_complete: 150 iwl_complete_scan(priv, aborted); 151 152 out_settings: 153 /* Can we still talk to firmware ? */ 154 if (!iwl_is_ready_rf(priv)) 155 return; 156 157 iwlagn_post_scan(priv); 158 } 159 160 void iwl_force_scan_end(struct iwl_priv *priv) 161 { 162 lockdep_assert_held(&priv->mutex); 163 164 if (!test_bit(STATUS_SCANNING, &priv->status)) { 165 IWL_DEBUG_SCAN(priv, "Forcing scan end while not scanning\n"); 166 return; 167 } 168 169 IWL_DEBUG_SCAN(priv, "Forcing scan end\n"); 170 clear_bit(STATUS_SCANNING, &priv->status); 171 clear_bit(STATUS_SCAN_HW, &priv->status); 172 clear_bit(STATUS_SCAN_ABORTING, &priv->status); 173 clear_bit(STATUS_SCAN_COMPLETE, &priv->status); 174 iwl_complete_scan(priv, true); 175 } 176 177 static void iwl_do_scan_abort(struct iwl_priv *priv) 178 { 179 int ret; 180 181 lockdep_assert_held(&priv->mutex); 182 183 if (!test_bit(STATUS_SCANNING, &priv->status)) { 184 IWL_DEBUG_SCAN(priv, "Not performing scan to abort\n"); 185 return; 186 } 187 188 if (test_and_set_bit(STATUS_SCAN_ABORTING, &priv->status)) { 189 IWL_DEBUG_SCAN(priv, "Scan abort in progress\n"); 190 return; 191 } 192 193 ret = iwl_send_scan_abort(priv); 194 if (ret) { 195 IWL_DEBUG_SCAN(priv, "Send scan abort failed %d\n", ret); 196 iwl_force_scan_end(priv); 197 } else 198 IWL_DEBUG_SCAN(priv, "Successfully send scan abort\n"); 199 } 200 201 /** 202 * iwl_scan_cancel - Cancel any currently executing HW scan 203 */ 204 int iwl_scan_cancel(struct iwl_priv *priv) 205 { 206 IWL_DEBUG_SCAN(priv, "Queuing abort scan\n"); 207 queue_work(priv->workqueue, &priv->abort_scan); 208 return 0; 209 } 210 211 /** 212 * iwl_scan_cancel_timeout - Cancel any currently executing HW scan 213 * @ms: amount of time to wait (in milliseconds) for scan to abort 214 * 215 */ 216 void iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms) 217 { 218 unsigned long timeout = jiffies + msecs_to_jiffies(ms); 219 220 lockdep_assert_held(&priv->mutex); 221 222 IWL_DEBUG_SCAN(priv, "Scan cancel timeout\n"); 223 224 iwl_do_scan_abort(priv); 225 226 while (time_before_eq(jiffies, timeout)) { 227 if (!test_bit(STATUS_SCAN_HW, &priv->status)) 228 goto finished; 229 msleep(20); 230 } 231 232 return; 233 234 finished: 235 /* 236 * Now STATUS_SCAN_HW is clear. This means that the 237 * device finished, but the background work is going 238 * to execute at best as soon as we release the mutex. 239 * Since we need to be able to issue a new scan right 240 * after this function returns, run the complete here. 241 * The STATUS_SCAN_COMPLETE bit will then be cleared 242 * and prevent the background work from "completing" 243 * a possible new scan. 244 */ 245 iwl_process_scan_complete(priv); 246 } 247 248 /* Service response to REPLY_SCAN_CMD (0x80) */ 249 static void iwl_rx_reply_scan(struct iwl_priv *priv, 250 struct iwl_rx_cmd_buffer *rxb) 251 { 252 #ifdef CONFIG_IWLWIFI_DEBUG 253 struct iwl_rx_packet *pkt = rxb_addr(rxb); 254 struct iwl_scanreq_notification *notif = (void *)pkt->data; 255 256 IWL_DEBUG_SCAN(priv, "Scan request status = 0x%x\n", notif->status); 257 #endif 258 } 259 260 /* Service SCAN_START_NOTIFICATION (0x82) */ 261 static void iwl_rx_scan_start_notif(struct iwl_priv *priv, 262 struct iwl_rx_cmd_buffer *rxb) 263 { 264 struct iwl_rx_packet *pkt = rxb_addr(rxb); 265 struct iwl_scanstart_notification *notif = (void *)pkt->data; 266 267 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low); 268 IWL_DEBUG_SCAN(priv, "Scan start: " 269 "%d [802.11%s] " 270 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n", 271 notif->channel, 272 notif->band ? "bg" : "a", 273 le32_to_cpu(notif->tsf_high), 274 le32_to_cpu(notif->tsf_low), 275 notif->status, notif->beacon_timer); 276 } 277 278 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */ 279 static void iwl_rx_scan_results_notif(struct iwl_priv *priv, 280 struct iwl_rx_cmd_buffer *rxb) 281 { 282 #ifdef CONFIG_IWLWIFI_DEBUG 283 struct iwl_rx_packet *pkt = rxb_addr(rxb); 284 struct iwl_scanresults_notification *notif = (void *)pkt->data; 285 286 IWL_DEBUG_SCAN(priv, "Scan ch.res: " 287 "%d [802.11%s] " 288 "probe status: %u:%u " 289 "(TSF: 0x%08X:%08X) - %d " 290 "elapsed=%lu usec\n", 291 notif->channel, 292 notif->band ? "bg" : "a", 293 notif->probe_status, notif->num_probe_not_sent, 294 le32_to_cpu(notif->tsf_high), 295 le32_to_cpu(notif->tsf_low), 296 le32_to_cpu(notif->statistics[0]), 297 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf); 298 #endif 299 } 300 301 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */ 302 static void iwl_rx_scan_complete_notif(struct iwl_priv *priv, 303 struct iwl_rx_cmd_buffer *rxb) 304 { 305 struct iwl_rx_packet *pkt = rxb_addr(rxb); 306 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->data; 307 308 IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n", 309 scan_notif->scanned_channels, 310 scan_notif->tsf_low, 311 scan_notif->tsf_high, scan_notif->status); 312 313 IWL_DEBUG_SCAN(priv, "Scan on %sGHz took %dms\n", 314 (priv->scan_band == NL80211_BAND_2GHZ) ? "2.4" : "5.2", 315 jiffies_to_msecs(jiffies - priv->scan_start)); 316 317 /* 318 * When aborting, we run the scan completed background work inline 319 * and the background work must then do nothing. The SCAN_COMPLETE 320 * bit helps implement that logic and thus needs to be set before 321 * queueing the work. Also, since the scan abort waits for SCAN_HW 322 * to clear, we need to set SCAN_COMPLETE before clearing SCAN_HW 323 * to avoid a race there. 324 */ 325 set_bit(STATUS_SCAN_COMPLETE, &priv->status); 326 clear_bit(STATUS_SCAN_HW, &priv->status); 327 queue_work(priv->workqueue, &priv->scan_completed); 328 329 if (priv->iw_mode != NL80211_IFTYPE_ADHOC && 330 iwl_advanced_bt_coexist(priv) && 331 priv->bt_status != scan_notif->bt_status) { 332 if (scan_notif->bt_status) { 333 /* BT on */ 334 if (!priv->bt_ch_announce) 335 priv->bt_traffic_load = 336 IWL_BT_COEX_TRAFFIC_LOAD_HIGH; 337 /* 338 * otherwise, no traffic load information provided 339 * no changes made 340 */ 341 } else { 342 /* BT off */ 343 priv->bt_traffic_load = 344 IWL_BT_COEX_TRAFFIC_LOAD_NONE; 345 } 346 priv->bt_status = scan_notif->bt_status; 347 queue_work(priv->workqueue, 348 &priv->bt_traffic_change_work); 349 } 350 } 351 352 void iwl_setup_rx_scan_handlers(struct iwl_priv *priv) 353 { 354 /* scan handlers */ 355 priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan; 356 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif; 357 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] = 358 iwl_rx_scan_results_notif; 359 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] = 360 iwl_rx_scan_complete_notif; 361 } 362 363 static u16 iwl_get_active_dwell_time(struct iwl_priv *priv, 364 enum nl80211_band band, u8 n_probes) 365 { 366 if (band == NL80211_BAND_5GHZ) 367 return IWL_ACTIVE_DWELL_TIME_52 + 368 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1); 369 else 370 return IWL_ACTIVE_DWELL_TIME_24 + 371 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1); 372 } 373 374 static u16 iwl_limit_dwell(struct iwl_priv *priv, u16 dwell_time) 375 { 376 struct iwl_rxon_context *ctx; 377 int limits[NUM_IWL_RXON_CTX] = {}; 378 int n_active = 0; 379 u16 limit; 380 381 BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2); 382 383 /* 384 * If we're associated, we clamp the dwell time 98% 385 * of the beacon interval (minus 2 * channel tune time) 386 * If both contexts are active, we have to restrict to 387 * 1/2 of the minimum of them, because they might be in 388 * lock-step with the time inbetween only half of what 389 * time we'd have in each of them. 390 */ 391 for_each_context(priv, ctx) { 392 switch (ctx->staging.dev_type) { 393 case RXON_DEV_TYPE_P2P: 394 /* no timing constraints */ 395 continue; 396 case RXON_DEV_TYPE_ESS: 397 default: 398 /* timing constraints if associated */ 399 if (!iwl_is_associated_ctx(ctx)) 400 continue; 401 break; 402 case RXON_DEV_TYPE_CP: 403 case RXON_DEV_TYPE_2STA: 404 /* 405 * These seem to always have timers for TBTT 406 * active in uCode even when not associated yet. 407 */ 408 break; 409 } 410 411 limits[n_active++] = ctx->beacon_int ?: IWL_PASSIVE_DWELL_BASE; 412 } 413 414 switch (n_active) { 415 case 0: 416 return dwell_time; 417 case 2: 418 limit = (limits[1] * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2; 419 limit /= 2; 420 dwell_time = min(limit, dwell_time); 421 /* fall through to limit further */ 422 case 1: 423 limit = (limits[0] * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2; 424 limit /= n_active; 425 return min(limit, dwell_time); 426 default: 427 WARN_ON_ONCE(1); 428 return dwell_time; 429 } 430 } 431 432 static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv, 433 enum nl80211_band band) 434 { 435 u16 passive = (band == NL80211_BAND_2GHZ) ? 436 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 : 437 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52; 438 439 return iwl_limit_dwell(priv, passive); 440 } 441 442 /* Return valid, unused, channel for a passive scan to reset the RF */ 443 static u8 iwl_get_single_channel_number(struct iwl_priv *priv, 444 enum nl80211_band band) 445 { 446 struct ieee80211_supported_band *sband = priv->hw->wiphy->bands[band]; 447 struct iwl_rxon_context *ctx; 448 int i; 449 450 for (i = 0; i < sband->n_channels; i++) { 451 bool busy = false; 452 453 for_each_context(priv, ctx) { 454 busy = sband->channels[i].hw_value == 455 le16_to_cpu(ctx->staging.channel); 456 if (busy) 457 break; 458 } 459 460 if (busy) 461 continue; 462 463 if (!(sband->channels[i].flags & IEEE80211_CHAN_DISABLED)) 464 return sband->channels[i].hw_value; 465 } 466 467 return 0; 468 } 469 470 static int iwl_get_channel_for_reset_scan(struct iwl_priv *priv, 471 struct ieee80211_vif *vif, 472 enum nl80211_band band, 473 struct iwl_scan_channel *scan_ch) 474 { 475 const struct ieee80211_supported_band *sband; 476 u16 channel; 477 478 sband = iwl_get_hw_mode(priv, band); 479 if (!sband) { 480 IWL_ERR(priv, "invalid band\n"); 481 return 0; 482 } 483 484 channel = iwl_get_single_channel_number(priv, band); 485 if (channel) { 486 scan_ch->channel = cpu_to_le16(channel); 487 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE; 488 scan_ch->active_dwell = 489 cpu_to_le16(IWL_RADIO_RESET_DWELL_TIME); 490 scan_ch->passive_dwell = 491 cpu_to_le16(IWL_RADIO_RESET_DWELL_TIME); 492 /* Set txpower levels to defaults */ 493 scan_ch->dsp_atten = 110; 494 if (band == NL80211_BAND_5GHZ) 495 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3; 496 else 497 scan_ch->tx_gain = ((1 << 5) | (5 << 3)); 498 return 1; 499 } 500 501 IWL_ERR(priv, "no valid channel found\n"); 502 return 0; 503 } 504 505 static int iwl_get_channels_for_scan(struct iwl_priv *priv, 506 struct ieee80211_vif *vif, 507 enum nl80211_band band, 508 u8 is_active, u8 n_probes, 509 struct iwl_scan_channel *scan_ch) 510 { 511 struct ieee80211_channel *chan; 512 const struct ieee80211_supported_band *sband; 513 u16 passive_dwell = 0; 514 u16 active_dwell = 0; 515 int added, i; 516 u16 channel; 517 518 sband = iwl_get_hw_mode(priv, band); 519 if (!sband) 520 return 0; 521 522 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes); 523 passive_dwell = iwl_get_passive_dwell_time(priv, band); 524 525 if (passive_dwell <= active_dwell) 526 passive_dwell = active_dwell + 1; 527 528 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) { 529 chan = priv->scan_request->channels[i]; 530 531 if (chan->band != band) 532 continue; 533 534 channel = chan->hw_value; 535 scan_ch->channel = cpu_to_le16(channel); 536 537 if (!is_active || (chan->flags & IEEE80211_CHAN_NO_IR)) 538 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE; 539 else 540 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE; 541 542 if (n_probes) 543 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes); 544 545 scan_ch->active_dwell = cpu_to_le16(active_dwell); 546 scan_ch->passive_dwell = cpu_to_le16(passive_dwell); 547 548 /* Set txpower levels to defaults */ 549 scan_ch->dsp_atten = 110; 550 551 /* NOTE: if we were doing 6Mb OFDM for scans we'd use 552 * power level: 553 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3; 554 */ 555 if (band == NL80211_BAND_5GHZ) 556 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3; 557 else 558 scan_ch->tx_gain = ((1 << 5) | (5 << 3)); 559 560 IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n", 561 channel, le32_to_cpu(scan_ch->type), 562 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ? 563 "ACTIVE" : "PASSIVE", 564 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ? 565 active_dwell : passive_dwell); 566 567 scan_ch++; 568 added++; 569 } 570 571 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added); 572 return added; 573 } 574 575 /** 576 * iwl_fill_probe_req - fill in all required fields and IE for probe request 577 */ 578 579 static u16 iwl_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta, 580 const u8 *ies, int ie_len, const u8 *ssid, 581 u8 ssid_len, int left) 582 { 583 int len = 0; 584 u8 *pos = NULL; 585 586 /* Make sure there is enough space for the probe request, 587 * two mandatory IEs and the data */ 588 left -= 24; 589 if (left < 0) 590 return 0; 591 592 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ); 593 eth_broadcast_addr(frame->da); 594 memcpy(frame->sa, ta, ETH_ALEN); 595 eth_broadcast_addr(frame->bssid); 596 frame->seq_ctrl = 0; 597 598 len += 24; 599 600 /* ...next IE... */ 601 pos = &frame->u.probe_req.variable[0]; 602 603 /* fill in our SSID IE */ 604 left -= ssid_len + 2; 605 if (left < 0) 606 return 0; 607 *pos++ = WLAN_EID_SSID; 608 *pos++ = ssid_len; 609 if (ssid && ssid_len) { 610 memcpy(pos, ssid, ssid_len); 611 pos += ssid_len; 612 } 613 614 len += ssid_len + 2; 615 616 if (WARN_ON(left < ie_len)) 617 return len; 618 619 if (ies && ie_len) { 620 memcpy(pos, ies, ie_len); 621 len += ie_len; 622 } 623 624 return (u16)len; 625 } 626 627 static int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif) 628 { 629 struct iwl_host_cmd cmd = { 630 .id = REPLY_SCAN_CMD, 631 .len = { sizeof(struct iwl_scan_cmd), }, 632 }; 633 struct iwl_scan_cmd *scan; 634 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS]; 635 u32 rate_flags = 0; 636 u16 cmd_len = 0; 637 u16 rx_chain = 0; 638 enum nl80211_band band; 639 u8 n_probes = 0; 640 u8 rx_ant = priv->nvm_data->valid_rx_ant; 641 u8 rate; 642 bool is_active = false; 643 int chan_mod; 644 u8 active_chains; 645 u8 scan_tx_antennas = priv->nvm_data->valid_tx_ant; 646 int ret; 647 int scan_cmd_size = sizeof(struct iwl_scan_cmd) + 648 MAX_SCAN_CHANNEL * sizeof(struct iwl_scan_channel) + 649 priv->fw->ucode_capa.max_probe_length; 650 const u8 *ssid = NULL; 651 u8 ssid_len = 0; 652 653 if (WARN_ON(priv->scan_type == IWL_SCAN_NORMAL && 654 (!priv->scan_request || 655 priv->scan_request->n_channels > MAX_SCAN_CHANNEL))) 656 return -EINVAL; 657 658 lockdep_assert_held(&priv->mutex); 659 660 if (vif) 661 ctx = iwl_rxon_ctx_from_vif(vif); 662 663 if (!priv->scan_cmd) { 664 priv->scan_cmd = kmalloc(scan_cmd_size, GFP_KERNEL); 665 if (!priv->scan_cmd) { 666 IWL_DEBUG_SCAN(priv, 667 "fail to allocate memory for scan\n"); 668 return -ENOMEM; 669 } 670 } 671 scan = priv->scan_cmd; 672 memset(scan, 0, scan_cmd_size); 673 674 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH; 675 scan->quiet_time = IWL_ACTIVE_QUIET_TIME; 676 677 if (iwl_is_any_associated(priv)) { 678 u16 interval = 0; 679 u32 extra; 680 u32 suspend_time = 100; 681 u32 scan_suspend_time = 100; 682 683 IWL_DEBUG_INFO(priv, "Scanning while associated...\n"); 684 switch (priv->scan_type) { 685 case IWL_SCAN_RADIO_RESET: 686 interval = 0; 687 break; 688 case IWL_SCAN_NORMAL: 689 interval = vif->bss_conf.beacon_int; 690 break; 691 } 692 693 scan->suspend_time = 0; 694 scan->max_out_time = cpu_to_le32(200 * 1024); 695 if (!interval) 696 interval = suspend_time; 697 698 extra = (suspend_time / interval) << 22; 699 scan_suspend_time = (extra | 700 ((suspend_time % interval) * 1024)); 701 scan->suspend_time = cpu_to_le32(scan_suspend_time); 702 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n", 703 scan_suspend_time, interval); 704 } 705 706 switch (priv->scan_type) { 707 case IWL_SCAN_RADIO_RESET: 708 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n"); 709 /* 710 * Override quiet time as firmware checks that active 711 * dwell is >= quiet; since we use passive scan it'll 712 * not actually be used. 713 */ 714 scan->quiet_time = cpu_to_le16(IWL_RADIO_RESET_DWELL_TIME); 715 break; 716 case IWL_SCAN_NORMAL: 717 if (priv->scan_request->n_ssids) { 718 int i, p = 0; 719 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n"); 720 /* 721 * The highest priority SSID is inserted to the 722 * probe request template. 723 */ 724 ssid_len = priv->scan_request->ssids[0].ssid_len; 725 ssid = priv->scan_request->ssids[0].ssid; 726 727 /* 728 * Invert the order of ssids, the firmware will invert 729 * it back. 730 */ 731 for (i = priv->scan_request->n_ssids - 1; i >= 1; i--) { 732 scan->direct_scan[p].id = WLAN_EID_SSID; 733 scan->direct_scan[p].len = 734 priv->scan_request->ssids[i].ssid_len; 735 memcpy(scan->direct_scan[p].ssid, 736 priv->scan_request->ssids[i].ssid, 737 priv->scan_request->ssids[i].ssid_len); 738 n_probes++; 739 p++; 740 } 741 is_active = true; 742 } else 743 IWL_DEBUG_SCAN(priv, "Start passive scan.\n"); 744 break; 745 } 746 747 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK; 748 scan->tx_cmd.sta_id = ctx->bcast_sta_id; 749 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE; 750 751 switch (priv->scan_band) { 752 case NL80211_BAND_2GHZ: 753 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK; 754 chan_mod = le32_to_cpu( 755 priv->contexts[IWL_RXON_CTX_BSS].active.flags & 756 RXON_FLG_CHANNEL_MODE_MSK) 757 >> RXON_FLG_CHANNEL_MODE_POS; 758 if ((priv->scan_request && priv->scan_request->no_cck) || 759 chan_mod == CHANNEL_MODE_PURE_40) { 760 rate = IWL_RATE_6M_PLCP; 761 } else { 762 rate = IWL_RATE_1M_PLCP; 763 rate_flags = RATE_MCS_CCK_MSK; 764 } 765 /* 766 * Internal scans are passive, so we can indiscriminately set 767 * the BT ignore flag on 2.4 GHz since it applies to TX only. 768 */ 769 if (priv->lib->bt_params && 770 priv->lib->bt_params->advanced_bt_coexist) 771 scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT; 772 break; 773 case NL80211_BAND_5GHZ: 774 rate = IWL_RATE_6M_PLCP; 775 break; 776 default: 777 IWL_WARN(priv, "Invalid scan band\n"); 778 return -EIO; 779 } 780 781 /* 782 * If active scanning is requested but a certain channel is 783 * marked passive, we can do active scanning if we detect 784 * transmissions. 785 * 786 * There is an issue with some firmware versions that triggers 787 * a sysassert on a "good CRC threshold" of zero (== disabled), 788 * on a radar channel even though this means that we should NOT 789 * send probes. 790 * 791 * The "good CRC threshold" is the number of frames that we 792 * need to receive during our dwell time on a channel before 793 * sending out probes -- setting this to a huge value will 794 * mean we never reach it, but at the same time work around 795 * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER 796 * here instead of IWL_GOOD_CRC_TH_DISABLED. 797 * 798 * This was fixed in later versions along with some other 799 * scan changes, and the threshold behaves as a flag in those 800 * versions. 801 */ 802 if (priv->new_scan_threshold_behaviour) 803 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT : 804 IWL_GOOD_CRC_TH_DISABLED; 805 else 806 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT : 807 IWL_GOOD_CRC_TH_NEVER; 808 809 band = priv->scan_band; 810 811 if (band == NL80211_BAND_2GHZ && 812 priv->lib->bt_params && 813 priv->lib->bt_params->advanced_bt_coexist) { 814 /* transmit 2.4 GHz probes only on first antenna */ 815 scan_tx_antennas = first_antenna(scan_tx_antennas); 816 } 817 818 priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv, 819 priv->scan_tx_ant[band], 820 scan_tx_antennas); 821 rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]); 822 scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags); 823 824 /* 825 * In power save mode while associated use one chain, 826 * otherwise use all chains 827 */ 828 if (test_bit(STATUS_POWER_PMI, &priv->status) && 829 !(priv->hw->conf.flags & IEEE80211_CONF_IDLE)) { 830 /* rx_ant has been set to all valid chains previously */ 831 active_chains = rx_ant & 832 ((u8)(priv->chain_noise_data.active_chains)); 833 if (!active_chains) 834 active_chains = rx_ant; 835 836 IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n", 837 priv->chain_noise_data.active_chains); 838 839 rx_ant = first_antenna(active_chains); 840 } 841 if (priv->lib->bt_params && 842 priv->lib->bt_params->advanced_bt_coexist && 843 priv->bt_full_concurrent) { 844 /* operated as 1x1 in full concurrency mode */ 845 rx_ant = first_antenna(rx_ant); 846 } 847 848 /* MIMO is not used here, but value is required */ 849 rx_chain |= 850 priv->nvm_data->valid_rx_ant << RXON_RX_CHAIN_VALID_POS; 851 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS; 852 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS; 853 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS; 854 scan->rx_chain = cpu_to_le16(rx_chain); 855 switch (priv->scan_type) { 856 case IWL_SCAN_NORMAL: 857 cmd_len = iwl_fill_probe_req( 858 (struct ieee80211_mgmt *)scan->data, 859 vif->addr, 860 priv->scan_request->ie, 861 priv->scan_request->ie_len, 862 ssid, ssid_len, 863 scan_cmd_size - sizeof(*scan)); 864 break; 865 case IWL_SCAN_RADIO_RESET: 866 /* use bcast addr, will not be transmitted but must be valid */ 867 cmd_len = iwl_fill_probe_req( 868 (struct ieee80211_mgmt *)scan->data, 869 iwl_bcast_addr, NULL, 0, 870 NULL, 0, 871 scan_cmd_size - sizeof(*scan)); 872 break; 873 default: 874 BUG(); 875 } 876 scan->tx_cmd.len = cpu_to_le16(cmd_len); 877 878 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK | 879 RXON_FILTER_BCON_AWARE_MSK); 880 881 switch (priv->scan_type) { 882 case IWL_SCAN_RADIO_RESET: 883 scan->channel_count = 884 iwl_get_channel_for_reset_scan(priv, vif, band, 885 (void *)&scan->data[cmd_len]); 886 break; 887 case IWL_SCAN_NORMAL: 888 scan->channel_count = 889 iwl_get_channels_for_scan(priv, vif, band, 890 is_active, n_probes, 891 (void *)&scan->data[cmd_len]); 892 break; 893 } 894 895 if (scan->channel_count == 0) { 896 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count); 897 return -EIO; 898 } 899 900 cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) + 901 scan->channel_count * sizeof(struct iwl_scan_channel); 902 cmd.data[0] = scan; 903 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY; 904 scan->len = cpu_to_le16(cmd.len[0]); 905 906 /* set scan bit here for PAN params */ 907 set_bit(STATUS_SCAN_HW, &priv->status); 908 909 ret = iwlagn_set_pan_params(priv); 910 if (ret) { 911 clear_bit(STATUS_SCAN_HW, &priv->status); 912 return ret; 913 } 914 915 ret = iwl_dvm_send_cmd(priv, &cmd); 916 if (ret) { 917 clear_bit(STATUS_SCAN_HW, &priv->status); 918 iwlagn_set_pan_params(priv); 919 } 920 921 return ret; 922 } 923 924 void iwl_init_scan_params(struct iwl_priv *priv) 925 { 926 u8 ant_idx = fls(priv->nvm_data->valid_tx_ant) - 1; 927 if (!priv->scan_tx_ant[NL80211_BAND_5GHZ]) 928 priv->scan_tx_ant[NL80211_BAND_5GHZ] = ant_idx; 929 if (!priv->scan_tx_ant[NL80211_BAND_2GHZ]) 930 priv->scan_tx_ant[NL80211_BAND_2GHZ] = ant_idx; 931 } 932 933 int __must_check iwl_scan_initiate(struct iwl_priv *priv, 934 struct ieee80211_vif *vif, 935 enum iwl_scan_type scan_type, 936 enum nl80211_band band) 937 { 938 int ret; 939 940 lockdep_assert_held(&priv->mutex); 941 942 cancel_delayed_work(&priv->scan_check); 943 944 if (!iwl_is_ready_rf(priv)) { 945 IWL_WARN(priv, "Request scan called when driver not ready.\n"); 946 return -EIO; 947 } 948 949 if (test_bit(STATUS_SCAN_HW, &priv->status)) { 950 IWL_DEBUG_SCAN(priv, 951 "Multiple concurrent scan requests in parallel.\n"); 952 return -EBUSY; 953 } 954 955 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) { 956 IWL_DEBUG_SCAN(priv, "Scan request while abort pending.\n"); 957 return -EBUSY; 958 } 959 960 IWL_DEBUG_SCAN(priv, "Starting %sscan...\n", 961 scan_type == IWL_SCAN_NORMAL ? "" : 962 "internal short "); 963 964 set_bit(STATUS_SCANNING, &priv->status); 965 priv->scan_type = scan_type; 966 priv->scan_start = jiffies; 967 priv->scan_band = band; 968 969 ret = iwlagn_request_scan(priv, vif); 970 if (ret) { 971 clear_bit(STATUS_SCANNING, &priv->status); 972 priv->scan_type = IWL_SCAN_NORMAL; 973 return ret; 974 } 975 976 queue_delayed_work(priv->workqueue, &priv->scan_check, 977 IWL_SCAN_CHECK_WATCHDOG); 978 979 return 0; 980 } 981 982 983 /* 984 * internal short scan, this function should only been called while associated. 985 * It will reset and tune the radio to prevent possible RF related problem 986 */ 987 void iwl_internal_short_hw_scan(struct iwl_priv *priv) 988 { 989 queue_work(priv->workqueue, &priv->start_internal_scan); 990 } 991 992 static void iwl_bg_start_internal_scan(struct work_struct *work) 993 { 994 struct iwl_priv *priv = 995 container_of(work, struct iwl_priv, start_internal_scan); 996 997 IWL_DEBUG_SCAN(priv, "Start internal scan\n"); 998 999 mutex_lock(&priv->mutex); 1000 1001 if (priv->scan_type == IWL_SCAN_RADIO_RESET) { 1002 IWL_DEBUG_SCAN(priv, "Internal scan already in progress\n"); 1003 goto unlock; 1004 } 1005 1006 if (test_bit(STATUS_SCANNING, &priv->status)) { 1007 IWL_DEBUG_SCAN(priv, "Scan already in progress.\n"); 1008 goto unlock; 1009 } 1010 1011 if (iwl_scan_initiate(priv, NULL, IWL_SCAN_RADIO_RESET, priv->band)) 1012 IWL_DEBUG_SCAN(priv, "failed to start internal short scan\n"); 1013 unlock: 1014 mutex_unlock(&priv->mutex); 1015 } 1016 1017 static void iwl_bg_scan_check(struct work_struct *data) 1018 { 1019 struct iwl_priv *priv = 1020 container_of(data, struct iwl_priv, scan_check.work); 1021 1022 IWL_DEBUG_SCAN(priv, "Scan check work\n"); 1023 1024 /* Since we are here firmware does not finish scan and 1025 * most likely is in bad shape, so we don't bother to 1026 * send abort command, just force scan complete to mac80211 */ 1027 mutex_lock(&priv->mutex); 1028 iwl_force_scan_end(priv); 1029 mutex_unlock(&priv->mutex); 1030 } 1031 1032 static void iwl_bg_abort_scan(struct work_struct *work) 1033 { 1034 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan); 1035 1036 IWL_DEBUG_SCAN(priv, "Abort scan work\n"); 1037 1038 /* We keep scan_check work queued in case when firmware will not 1039 * report back scan completed notification */ 1040 mutex_lock(&priv->mutex); 1041 iwl_scan_cancel_timeout(priv, 200); 1042 mutex_unlock(&priv->mutex); 1043 } 1044 1045 static void iwl_bg_scan_completed(struct work_struct *work) 1046 { 1047 struct iwl_priv *priv = 1048 container_of(work, struct iwl_priv, scan_completed); 1049 1050 mutex_lock(&priv->mutex); 1051 iwl_process_scan_complete(priv); 1052 mutex_unlock(&priv->mutex); 1053 } 1054 1055 void iwl_setup_scan_deferred_work(struct iwl_priv *priv) 1056 { 1057 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed); 1058 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan); 1059 INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan); 1060 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check); 1061 } 1062 1063 void iwl_cancel_scan_deferred_work(struct iwl_priv *priv) 1064 { 1065 cancel_work_sync(&priv->start_internal_scan); 1066 cancel_work_sync(&priv->abort_scan); 1067 cancel_work_sync(&priv->scan_completed); 1068 1069 if (cancel_delayed_work_sync(&priv->scan_check)) { 1070 mutex_lock(&priv->mutex); 1071 iwl_force_scan_end(priv); 1072 mutex_unlock(&priv->mutex); 1073 } 1074 } 1075