iwl-scan.c 30 KB

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