iwl-scan.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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. void 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. }
  162. /* Service response to REPLY_SCAN_CMD (0x80) */
  163. static int iwl_rx_reply_scan(struct iwl_priv *priv,
  164. struct iwl_rx_mem_buffer *rxb,
  165. struct iwl_device_cmd *cmd)
  166. {
  167. #ifdef CONFIG_IWLWIFI_DEBUG
  168. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  169. struct iwl_scanreq_notification *notif =
  170. (struct iwl_scanreq_notification *)pkt->u.raw;
  171. IWL_DEBUG_SCAN(priv, "Scan request status = 0x%x\n", notif->status);
  172. #endif
  173. return 0;
  174. }
  175. /* Service SCAN_START_NOTIFICATION (0x82) */
  176. static int iwl_rx_scan_start_notif(struct iwl_priv *priv,
  177. struct iwl_rx_mem_buffer *rxb,
  178. struct iwl_device_cmd *cmd)
  179. {
  180. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  181. struct iwl_scanstart_notification *notif =
  182. (struct iwl_scanstart_notification *)pkt->u.raw;
  183. priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
  184. IWL_DEBUG_SCAN(priv, "Scan start: "
  185. "%d [802.11%s] "
  186. "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
  187. notif->channel,
  188. notif->band ? "bg" : "a",
  189. le32_to_cpu(notif->tsf_high),
  190. le32_to_cpu(notif->tsf_low),
  191. notif->status, notif->beacon_timer);
  192. if (priv->scan_type == IWL_SCAN_ROC &&
  193. !priv->hw_roc_start_notified) {
  194. ieee80211_ready_on_channel(priv->hw);
  195. priv->hw_roc_start_notified = true;
  196. }
  197. return 0;
  198. }
  199. /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
  200. static int iwl_rx_scan_results_notif(struct iwl_priv *priv,
  201. struct iwl_rx_mem_buffer *rxb,
  202. struct iwl_device_cmd *cmd)
  203. {
  204. #ifdef CONFIG_IWLWIFI_DEBUG
  205. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  206. struct iwl_scanresults_notification *notif =
  207. (struct iwl_scanresults_notification *)pkt->u.raw;
  208. IWL_DEBUG_SCAN(priv, "Scan ch.res: "
  209. "%d [802.11%s] "
  210. "probe status: %u:%u "
  211. "(TSF: 0x%08X:%08X) - %d "
  212. "elapsed=%lu usec\n",
  213. notif->channel,
  214. notif->band ? "bg" : "a",
  215. notif->probe_status, notif->num_probe_not_sent,
  216. le32_to_cpu(notif->tsf_high),
  217. le32_to_cpu(notif->tsf_low),
  218. le32_to_cpu(notif->statistics[0]),
  219. le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf);
  220. #endif
  221. return 0;
  222. }
  223. /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
  224. static int iwl_rx_scan_complete_notif(struct iwl_priv *priv,
  225. struct iwl_rx_mem_buffer *rxb,
  226. struct iwl_device_cmd *cmd)
  227. {
  228. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  229. struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
  230. IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
  231. scan_notif->scanned_channels,
  232. scan_notif->tsf_low,
  233. scan_notif->tsf_high, scan_notif->status);
  234. /* The HW is no longer scanning */
  235. clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
  236. IWL_DEBUG_SCAN(priv, "Scan on %sGHz took %dms\n",
  237. (priv->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
  238. jiffies_to_msecs(jiffies - priv->scan_start));
  239. queue_work(priv->shrd->workqueue, &priv->scan_completed);
  240. if (priv->iw_mode != NL80211_IFTYPE_ADHOC &&
  241. iwl_advanced_bt_coexist(priv) &&
  242. priv->bt_status != scan_notif->bt_status) {
  243. if (scan_notif->bt_status) {
  244. /* BT on */
  245. if (!priv->bt_ch_announce)
  246. priv->bt_traffic_load =
  247. IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  248. /*
  249. * otherwise, no traffic load information provided
  250. * no changes made
  251. */
  252. } else {
  253. /* BT off */
  254. priv->bt_traffic_load =
  255. IWL_BT_COEX_TRAFFIC_LOAD_NONE;
  256. }
  257. priv->bt_status = scan_notif->bt_status;
  258. queue_work(priv->shrd->workqueue,
  259. &priv->bt_traffic_change_work);
  260. }
  261. return 0;
  262. }
  263. void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
  264. {
  265. /* scan handlers */
  266. priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
  267. priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
  268. priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
  269. iwl_rx_scan_results_notif;
  270. priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
  271. iwl_rx_scan_complete_notif;
  272. }
  273. static u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
  274. enum ieee80211_band band, u8 n_probes)
  275. {
  276. if (band == IEEE80211_BAND_5GHZ)
  277. return IWL_ACTIVE_DWELL_TIME_52 +
  278. IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
  279. else
  280. return IWL_ACTIVE_DWELL_TIME_24 +
  281. IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
  282. }
  283. static u16 iwl_limit_dwell(struct iwl_priv *priv, u16 dwell_time)
  284. {
  285. struct iwl_rxon_context *ctx;
  286. /*
  287. * If we're associated, we clamp the dwell time 98%
  288. * of the smallest beacon interval (minus 2 * channel
  289. * tune time)
  290. */
  291. for_each_context(priv, ctx) {
  292. u16 value;
  293. if (!iwl_is_associated_ctx(ctx))
  294. continue;
  295. value = ctx->beacon_int;
  296. if (!value)
  297. value = IWL_PASSIVE_DWELL_BASE;
  298. value = (value * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
  299. dwell_time = min(value, dwell_time);
  300. }
  301. return dwell_time;
  302. }
  303. static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
  304. enum ieee80211_band band)
  305. {
  306. u16 passive = (band == IEEE80211_BAND_2GHZ) ?
  307. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
  308. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
  309. return iwl_limit_dwell(priv, passive);
  310. }
  311. static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
  312. struct ieee80211_vif *vif,
  313. enum ieee80211_band band,
  314. struct iwl_scan_channel *scan_ch)
  315. {
  316. const struct ieee80211_supported_band *sband;
  317. u16 passive_dwell = 0;
  318. u16 active_dwell = 0;
  319. int added = 0;
  320. u16 channel = 0;
  321. sband = iwl_get_hw_mode(priv, band);
  322. if (!sband) {
  323. IWL_ERR(priv, "invalid band\n");
  324. return added;
  325. }
  326. active_dwell = iwl_get_active_dwell_time(priv, band, 0);
  327. passive_dwell = iwl_get_passive_dwell_time(priv, band);
  328. if (passive_dwell <= active_dwell)
  329. passive_dwell = active_dwell + 1;
  330. channel = iwl_get_single_channel_number(priv, band);
  331. if (channel) {
  332. scan_ch->channel = cpu_to_le16(channel);
  333. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  334. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  335. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  336. /* Set txpower levels to defaults */
  337. scan_ch->dsp_atten = 110;
  338. if (band == IEEE80211_BAND_5GHZ)
  339. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  340. else
  341. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  342. added++;
  343. } else
  344. IWL_ERR(priv, "no valid channel found\n");
  345. return added;
  346. }
  347. static int iwl_get_channels_for_scan(struct iwl_priv *priv,
  348. struct ieee80211_vif *vif,
  349. enum ieee80211_band band,
  350. u8 is_active, u8 n_probes,
  351. struct iwl_scan_channel *scan_ch)
  352. {
  353. struct ieee80211_channel *chan;
  354. const struct ieee80211_supported_band *sband;
  355. const struct iwl_channel_info *ch_info;
  356. u16 passive_dwell = 0;
  357. u16 active_dwell = 0;
  358. int added, i;
  359. u16 channel;
  360. sband = iwl_get_hw_mode(priv, band);
  361. if (!sband)
  362. return 0;
  363. active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
  364. passive_dwell = iwl_get_passive_dwell_time(priv, band);
  365. if (passive_dwell <= active_dwell)
  366. passive_dwell = active_dwell + 1;
  367. for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
  368. chan = priv->scan_request->channels[i];
  369. if (chan->band != band)
  370. continue;
  371. channel = chan->hw_value;
  372. scan_ch->channel = cpu_to_le16(channel);
  373. ch_info = iwl_get_channel_info(priv, band, channel);
  374. if (!is_channel_valid(ch_info)) {
  375. IWL_DEBUG_SCAN(priv,
  376. "Channel %d is INVALID for this band.\n",
  377. channel);
  378. continue;
  379. }
  380. if (!is_active || is_channel_passive(ch_info) ||
  381. (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
  382. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  383. else
  384. scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
  385. if (n_probes)
  386. scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
  387. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  388. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  389. /* Set txpower levels to defaults */
  390. scan_ch->dsp_atten = 110;
  391. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  392. * power level:
  393. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  394. */
  395. if (band == IEEE80211_BAND_5GHZ)
  396. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  397. else
  398. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  399. IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
  400. channel, le32_to_cpu(scan_ch->type),
  401. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  402. "ACTIVE" : "PASSIVE",
  403. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  404. active_dwell : passive_dwell);
  405. scan_ch++;
  406. added++;
  407. }
  408. IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
  409. return added;
  410. }
  411. static int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
  412. {
  413. struct iwl_host_cmd cmd = {
  414. .id = REPLY_SCAN_CMD,
  415. .len = { sizeof(struct iwl_scan_cmd), },
  416. .flags = CMD_SYNC,
  417. };
  418. struct iwl_scan_cmd *scan;
  419. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  420. u32 rate_flags = 0;
  421. u16 cmd_len;
  422. u16 rx_chain = 0;
  423. enum ieee80211_band band;
  424. u8 n_probes = 0;
  425. u8 rx_ant = hw_params(priv).valid_rx_ant;
  426. u8 rate;
  427. bool is_active = false;
  428. int chan_mod;
  429. u8 active_chains;
  430. u8 scan_tx_antennas = hw_params(priv).valid_tx_ant;
  431. int ret;
  432. lockdep_assert_held(&priv->shrd->mutex);
  433. if (vif)
  434. ctx = iwl_rxon_ctx_from_vif(vif);
  435. if (!priv->scan_cmd) {
  436. priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
  437. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  438. if (!priv->scan_cmd) {
  439. IWL_DEBUG_SCAN(priv,
  440. "fail to allocate memory for scan\n");
  441. return -ENOMEM;
  442. }
  443. }
  444. scan = priv->scan_cmd;
  445. memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
  446. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  447. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  448. if (priv->scan_type != IWL_SCAN_ROC &&
  449. iwl_is_any_associated(priv)) {
  450. u16 interval = 0;
  451. u32 extra;
  452. u32 suspend_time = 100;
  453. u32 scan_suspend_time = 100;
  454. IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
  455. switch (priv->scan_type) {
  456. case IWL_SCAN_ROC:
  457. WARN_ON(1);
  458. break;
  459. case IWL_SCAN_RADIO_RESET:
  460. interval = 0;
  461. break;
  462. case IWL_SCAN_NORMAL:
  463. interval = vif->bss_conf.beacon_int;
  464. break;
  465. }
  466. scan->suspend_time = 0;
  467. scan->max_out_time = cpu_to_le32(200 * 1024);
  468. if (!interval)
  469. interval = suspend_time;
  470. extra = (suspend_time / interval) << 22;
  471. scan_suspend_time = (extra |
  472. ((suspend_time % interval) * 1024));
  473. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  474. IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
  475. scan_suspend_time, interval);
  476. } else if (priv->scan_type == IWL_SCAN_ROC) {
  477. scan->suspend_time = 0;
  478. scan->max_out_time = 0;
  479. scan->quiet_time = 0;
  480. scan->quiet_plcp_th = 0;
  481. }
  482. switch (priv->scan_type) {
  483. case IWL_SCAN_RADIO_RESET:
  484. IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
  485. break;
  486. case IWL_SCAN_NORMAL:
  487. if (priv->scan_request->n_ssids) {
  488. int i, p = 0;
  489. IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
  490. for (i = 0; i < priv->scan_request->n_ssids; i++) {
  491. /* always does wildcard anyway */
  492. if (!priv->scan_request->ssids[i].ssid_len)
  493. continue;
  494. scan->direct_scan[p].id = WLAN_EID_SSID;
  495. scan->direct_scan[p].len =
  496. priv->scan_request->ssids[i].ssid_len;
  497. memcpy(scan->direct_scan[p].ssid,
  498. priv->scan_request->ssids[i].ssid,
  499. priv->scan_request->ssids[i].ssid_len);
  500. n_probes++;
  501. p++;
  502. }
  503. is_active = true;
  504. } else
  505. IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
  506. break;
  507. case IWL_SCAN_ROC:
  508. IWL_DEBUG_SCAN(priv, "Start ROC scan.\n");
  509. break;
  510. }
  511. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  512. scan->tx_cmd.sta_id = ctx->bcast_sta_id;
  513. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  514. switch (priv->scan_band) {
  515. case IEEE80211_BAND_2GHZ:
  516. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  517. chan_mod = le32_to_cpu(
  518. priv->contexts[IWL_RXON_CTX_BSS].active.flags &
  519. RXON_FLG_CHANNEL_MODE_MSK)
  520. >> RXON_FLG_CHANNEL_MODE_POS;
  521. if (chan_mod == CHANNEL_MODE_PURE_40) {
  522. rate = IWL_RATE_6M_PLCP;
  523. } else {
  524. rate = IWL_RATE_1M_PLCP;
  525. rate_flags = RATE_MCS_CCK_MSK;
  526. }
  527. /*
  528. * Internal scans are passive, so we can indiscriminately set
  529. * the BT ignore flag on 2.4 GHz since it applies to TX only.
  530. */
  531. if (priv->cfg->bt_params &&
  532. priv->cfg->bt_params->advanced_bt_coexist)
  533. scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
  534. break;
  535. case IEEE80211_BAND_5GHZ:
  536. rate = IWL_RATE_6M_PLCP;
  537. break;
  538. default:
  539. IWL_WARN(priv, "Invalid scan band\n");
  540. return -EIO;
  541. }
  542. /*
  543. * If active scanning is requested but a certain channel is
  544. * marked passive, we can do active scanning if we detect
  545. * transmissions.
  546. *
  547. * There is an issue with some firmware versions that triggers
  548. * a sysassert on a "good CRC threshold" of zero (== disabled),
  549. * on a radar channel even though this means that we should NOT
  550. * send probes.
  551. *
  552. * The "good CRC threshold" is the number of frames that we
  553. * need to receive during our dwell time on a channel before
  554. * sending out probes -- setting this to a huge value will
  555. * mean we never reach it, but at the same time work around
  556. * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
  557. * here instead of IWL_GOOD_CRC_TH_DISABLED.
  558. *
  559. * This was fixed in later versions along with some other
  560. * scan changes, and the threshold behaves as a flag in those
  561. * versions.
  562. */
  563. if (priv->new_scan_threshold_behaviour)
  564. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  565. IWL_GOOD_CRC_TH_DISABLED;
  566. else
  567. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  568. IWL_GOOD_CRC_TH_NEVER;
  569. band = priv->scan_band;
  570. if (priv->cfg->scan_rx_antennas[band])
  571. rx_ant = priv->cfg->scan_rx_antennas[band];
  572. if (band == IEEE80211_BAND_2GHZ &&
  573. priv->cfg->bt_params &&
  574. priv->cfg->bt_params->advanced_bt_coexist) {
  575. /* transmit 2.4 GHz probes only on first antenna */
  576. scan_tx_antennas = first_antenna(scan_tx_antennas);
  577. }
  578. priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv,
  579. priv->scan_tx_ant[band],
  580. scan_tx_antennas);
  581. rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
  582. scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
  583. /* In power save mode use one chain, otherwise use all chains */
  584. if (test_bit(STATUS_POWER_PMI, &priv->shrd->status)) {
  585. /* rx_ant has been set to all valid chains previously */
  586. active_chains = rx_ant &
  587. ((u8)(priv->chain_noise_data.active_chains));
  588. if (!active_chains)
  589. active_chains = rx_ant;
  590. IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
  591. priv->chain_noise_data.active_chains);
  592. rx_ant = first_antenna(active_chains);
  593. }
  594. if (priv->cfg->bt_params &&
  595. priv->cfg->bt_params->advanced_bt_coexist &&
  596. priv->bt_full_concurrent) {
  597. /* operated as 1x1 in full concurrency mode */
  598. rx_ant = first_antenna(rx_ant);
  599. }
  600. /* MIMO is not used here, but value is required */
  601. rx_chain |=
  602. hw_params(priv).valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
  603. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
  604. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
  605. rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
  606. scan->rx_chain = cpu_to_le16(rx_chain);
  607. switch (priv->scan_type) {
  608. case IWL_SCAN_NORMAL:
  609. cmd_len = iwl_fill_probe_req(priv,
  610. (struct ieee80211_mgmt *)scan->data,
  611. vif->addr,
  612. priv->scan_request->ie,
  613. priv->scan_request->ie_len,
  614. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  615. break;
  616. case IWL_SCAN_RADIO_RESET:
  617. case IWL_SCAN_ROC:
  618. /* use bcast addr, will not be transmitted but must be valid */
  619. cmd_len = iwl_fill_probe_req(priv,
  620. (struct ieee80211_mgmt *)scan->data,
  621. iwl_bcast_addr, NULL, 0,
  622. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  623. break;
  624. default:
  625. BUG();
  626. }
  627. scan->tx_cmd.len = cpu_to_le16(cmd_len);
  628. scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
  629. RXON_FILTER_BCON_AWARE_MSK);
  630. switch (priv->scan_type) {
  631. case IWL_SCAN_RADIO_RESET:
  632. scan->channel_count =
  633. iwl_get_single_channel_for_scan(priv, vif, band,
  634. (void *)&scan->data[cmd_len]);
  635. break;
  636. case IWL_SCAN_NORMAL:
  637. scan->channel_count =
  638. iwl_get_channels_for_scan(priv, vif, band,
  639. is_active, n_probes,
  640. (void *)&scan->data[cmd_len]);
  641. break;
  642. case IWL_SCAN_ROC: {
  643. struct iwl_scan_channel *scan_ch;
  644. int n_chan, i;
  645. u16 dwell;
  646. dwell = iwl_limit_dwell(priv, priv->hw_roc_duration);
  647. n_chan = DIV_ROUND_UP(priv->hw_roc_duration, dwell);
  648. scan->channel_count = n_chan;
  649. scan_ch = (void *)&scan->data[cmd_len];
  650. for (i = 0; i < n_chan; i++) {
  651. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  652. scan_ch->channel =
  653. cpu_to_le16(priv->hw_roc_channel->hw_value);
  654. if (i == n_chan - 1)
  655. dwell = priv->hw_roc_duration - i * dwell;
  656. scan_ch->active_dwell =
  657. scan_ch->passive_dwell = cpu_to_le16(dwell);
  658. /* Set txpower levels to defaults */
  659. scan_ch->dsp_atten = 110;
  660. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  661. * power level:
  662. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  663. */
  664. if (priv->hw_roc_channel->band == IEEE80211_BAND_5GHZ)
  665. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  666. else
  667. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  668. scan_ch++;
  669. }
  670. }
  671. break;
  672. }
  673. if (scan->channel_count == 0) {
  674. IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
  675. return -EIO;
  676. }
  677. cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
  678. scan->channel_count * sizeof(struct iwl_scan_channel);
  679. cmd.data[0] = scan;
  680. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  681. scan->len = cpu_to_le16(cmd.len[0]);
  682. /* set scan bit here for PAN params */
  683. set_bit(STATUS_SCAN_HW, &priv->shrd->status);
  684. ret = iwlagn_set_pan_params(priv);
  685. if (ret)
  686. return ret;
  687. ret = iwl_trans_send_cmd(trans(priv), &cmd);
  688. if (ret) {
  689. clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
  690. iwlagn_set_pan_params(priv);
  691. }
  692. return ret;
  693. }
  694. void iwl_init_scan_params(struct iwl_priv *priv)
  695. {
  696. u8 ant_idx = fls(hw_params(priv).valid_tx_ant) - 1;
  697. if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
  698. priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
  699. if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
  700. priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
  701. }
  702. int __must_check iwl_scan_initiate(struct iwl_priv *priv,
  703. struct ieee80211_vif *vif,
  704. enum iwl_scan_type scan_type,
  705. enum ieee80211_band band)
  706. {
  707. int ret;
  708. lockdep_assert_held(&priv->shrd->mutex);
  709. cancel_delayed_work(&priv->scan_check);
  710. if (!iwl_is_ready_rf(priv->shrd)) {
  711. IWL_WARN(priv, "Request scan called when driver not ready.\n");
  712. return -EIO;
  713. }
  714. if (test_bit(STATUS_SCAN_HW, &priv->shrd->status)) {
  715. IWL_DEBUG_SCAN(priv,
  716. "Multiple concurrent scan requests in parallel.\n");
  717. return -EBUSY;
  718. }
  719. if (test_bit(STATUS_SCAN_ABORTING, &priv->shrd->status)) {
  720. IWL_DEBUG_SCAN(priv, "Scan request while abort pending.\n");
  721. return -EBUSY;
  722. }
  723. IWL_DEBUG_SCAN(priv, "Starting %sscan...\n",
  724. scan_type == IWL_SCAN_NORMAL ? "" :
  725. scan_type == IWL_SCAN_ROC ? "remain-on-channel " :
  726. "internal short ");
  727. set_bit(STATUS_SCANNING, &priv->shrd->status);
  728. priv->scan_type = scan_type;
  729. priv->scan_start = jiffies;
  730. priv->scan_band = band;
  731. ret = iwlagn_request_scan(priv, vif);
  732. if (ret) {
  733. clear_bit(STATUS_SCANNING, &priv->shrd->status);
  734. priv->scan_type = IWL_SCAN_NORMAL;
  735. return ret;
  736. }
  737. queue_delayed_work(priv->shrd->workqueue, &priv->scan_check,
  738. IWL_SCAN_CHECK_WATCHDOG);
  739. return 0;
  740. }
  741. int iwl_mac_hw_scan(struct ieee80211_hw *hw,
  742. struct ieee80211_vif *vif,
  743. struct cfg80211_scan_request *req)
  744. {
  745. struct iwl_priv *priv = hw->priv;
  746. int ret;
  747. IWL_DEBUG_MAC80211(priv, "enter\n");
  748. if (req->n_channels == 0)
  749. return -EINVAL;
  750. mutex_lock(&priv->shrd->mutex);
  751. /*
  752. * If an internal scan is in progress, just set
  753. * up the scan_request as per above.
  754. */
  755. if (priv->scan_type != IWL_SCAN_NORMAL) {
  756. IWL_DEBUG_SCAN(priv,
  757. "SCAN request during internal scan - defer\n");
  758. priv->scan_request = req;
  759. priv->scan_vif = vif;
  760. ret = 0;
  761. } else {
  762. priv->scan_request = req;
  763. priv->scan_vif = vif;
  764. /*
  765. * mac80211 will only ask for one band at a time
  766. * so using channels[0] here is ok
  767. */
  768. ret = iwl_scan_initiate(priv, vif, IWL_SCAN_NORMAL,
  769. req->channels[0]->band);
  770. if (ret) {
  771. priv->scan_request = NULL;
  772. priv->scan_vif = NULL;
  773. }
  774. }
  775. IWL_DEBUG_MAC80211(priv, "leave\n");
  776. mutex_unlock(&priv->shrd->mutex);
  777. return ret;
  778. }
  779. /*
  780. * internal short scan, this function should only been called while associated.
  781. * It will reset and tune the radio to prevent possible RF related problem
  782. */
  783. void iwl_internal_short_hw_scan(struct iwl_priv *priv)
  784. {
  785. queue_work(priv->shrd->workqueue, &priv->start_internal_scan);
  786. }
  787. static void iwl_bg_start_internal_scan(struct work_struct *work)
  788. {
  789. struct iwl_priv *priv =
  790. container_of(work, struct iwl_priv, start_internal_scan);
  791. IWL_DEBUG_SCAN(priv, "Start internal scan\n");
  792. mutex_lock(&priv->shrd->mutex);
  793. if (priv->scan_type == IWL_SCAN_RADIO_RESET) {
  794. IWL_DEBUG_SCAN(priv, "Internal scan already in progress\n");
  795. goto unlock;
  796. }
  797. if (test_bit(STATUS_SCANNING, &priv->shrd->status)) {
  798. IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
  799. goto unlock;
  800. }
  801. if (iwl_scan_initiate(priv, NULL, IWL_SCAN_RADIO_RESET, priv->band))
  802. IWL_DEBUG_SCAN(priv, "failed to start internal short scan\n");
  803. unlock:
  804. mutex_unlock(&priv->shrd->mutex);
  805. }
  806. static void iwl_bg_scan_check(struct work_struct *data)
  807. {
  808. struct iwl_priv *priv =
  809. container_of(data, struct iwl_priv, scan_check.work);
  810. IWL_DEBUG_SCAN(priv, "Scan check work\n");
  811. /* Since we are here firmware does not finish scan and
  812. * most likely is in bad shape, so we don't bother to
  813. * send abort command, just force scan complete to mac80211 */
  814. mutex_lock(&priv->shrd->mutex);
  815. iwl_force_scan_end(priv);
  816. mutex_unlock(&priv->shrd->mutex);
  817. }
  818. /**
  819. * iwl_fill_probe_req - fill in all required fields and IE for probe request
  820. */
  821. u16 iwl_fill_probe_req(struct iwl_priv *priv, struct ieee80211_mgmt *frame,
  822. const u8 *ta, const u8 *ies, int ie_len, int left)
  823. {
  824. int len = 0;
  825. u8 *pos = NULL;
  826. /* Make sure there is enough space for the probe request,
  827. * two mandatory IEs and the data */
  828. left -= 24;
  829. if (left < 0)
  830. return 0;
  831. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  832. memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
  833. memcpy(frame->sa, ta, ETH_ALEN);
  834. memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
  835. frame->seq_ctrl = 0;
  836. len += 24;
  837. /* ...next IE... */
  838. pos = &frame->u.probe_req.variable[0];
  839. /* fill in our indirect SSID IE */
  840. left -= 2;
  841. if (left < 0)
  842. return 0;
  843. *pos++ = WLAN_EID_SSID;
  844. *pos++ = 0;
  845. len += 2;
  846. if (WARN_ON(left < ie_len))
  847. return len;
  848. if (ies && ie_len) {
  849. memcpy(pos, ies, ie_len);
  850. len += ie_len;
  851. }
  852. return (u16)len;
  853. }
  854. static void iwl_bg_abort_scan(struct work_struct *work)
  855. {
  856. struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
  857. IWL_DEBUG_SCAN(priv, "Abort scan work\n");
  858. /* We keep scan_check work queued in case when firmware will not
  859. * report back scan completed notification */
  860. mutex_lock(&priv->shrd->mutex);
  861. iwl_scan_cancel_timeout(priv, 200);
  862. mutex_unlock(&priv->shrd->mutex);
  863. }
  864. static void iwl_process_scan_complete(struct iwl_priv *priv)
  865. {
  866. bool aborted;
  867. IWL_DEBUG_SCAN(priv, "Completed scan.\n");
  868. cancel_delayed_work(&priv->scan_check);
  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. return;
  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. return;
  903. iwlagn_post_scan(priv);
  904. }
  905. static void iwl_bg_scan_completed(struct work_struct *work)
  906. {
  907. struct iwl_priv *priv =
  908. container_of(work, struct iwl_priv, scan_completed);
  909. mutex_lock(&priv->shrd->mutex);
  910. iwl_process_scan_complete(priv);
  911. mutex_unlock(&priv->shrd->mutex);
  912. }
  913. void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
  914. {
  915. INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
  916. INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
  917. INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan);
  918. INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
  919. }
  920. void iwl_cancel_scan_deferred_work(struct iwl_priv *priv)
  921. {
  922. cancel_work_sync(&priv->start_internal_scan);
  923. cancel_work_sync(&priv->abort_scan);
  924. cancel_work_sync(&priv->scan_completed);
  925. if (cancel_delayed_work_sync(&priv->scan_check)) {
  926. mutex_lock(&priv->shrd->mutex);
  927. iwl_force_scan_end(priv);
  928. mutex_unlock(&priv->shrd->mutex);
  929. }
  930. }