iwl-scan.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2010 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/types.h>
  29. #include <linux/etherdevice.h>
  30. #include <net/mac80211.h>
  31. #include "iwl-eeprom.h"
  32. #include "iwl-dev.h"
  33. #include "iwl-core.h"
  34. #include "iwl-sta.h"
  35. #include "iwl-io.h"
  36. #include "iwl-helpers.h"
  37. /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
  38. * sending probe req. This should be set long enough to hear probe responses
  39. * from more than one AP. */
  40. #define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
  41. #define IWL_ACTIVE_DWELL_TIME_52 (20)
  42. #define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
  43. #define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
  44. /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
  45. * Must be set longer than active dwell time.
  46. * For the most reliable scan, set > AP beacon interval (typically 100msec). */
  47. #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
  48. #define IWL_PASSIVE_DWELL_TIME_52 (10)
  49. #define IWL_PASSIVE_DWELL_BASE (100)
  50. #define IWL_CHANNEL_TUNE_TIME 5
  51. /**
  52. * iwl_scan_cancel - Cancel any currently executing HW scan
  53. *
  54. * NOTE: priv->mutex is not required before calling this function
  55. */
  56. int iwl_scan_cancel(struct iwl_priv *priv)
  57. {
  58. if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
  59. clear_bit(STATUS_SCANNING, &priv->status);
  60. return 0;
  61. }
  62. if (test_bit(STATUS_SCANNING, &priv->status)) {
  63. if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  64. IWL_DEBUG_SCAN(priv, "Queuing scan abort.\n");
  65. set_bit(STATUS_SCAN_ABORTING, &priv->status);
  66. queue_work(priv->workqueue, &priv->abort_scan);
  67. } else
  68. IWL_DEBUG_SCAN(priv, "Scan abort already in progress.\n");
  69. return test_bit(STATUS_SCANNING, &priv->status);
  70. }
  71. return 0;
  72. }
  73. EXPORT_SYMBOL(iwl_scan_cancel);
  74. /**
  75. * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
  76. * @ms: amount of time to wait (in milliseconds) for scan to abort
  77. *
  78. * NOTE: priv->mutex must be held before calling this function
  79. */
  80. int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
  81. {
  82. unsigned long now = jiffies;
  83. int ret;
  84. ret = iwl_scan_cancel(priv);
  85. if (ret && ms) {
  86. mutex_unlock(&priv->mutex);
  87. while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
  88. test_bit(STATUS_SCANNING, &priv->status))
  89. msleep(1);
  90. mutex_lock(&priv->mutex);
  91. return test_bit(STATUS_SCANNING, &priv->status);
  92. }
  93. return ret;
  94. }
  95. EXPORT_SYMBOL(iwl_scan_cancel_timeout);
  96. static int iwl_send_scan_abort(struct iwl_priv *priv)
  97. {
  98. int ret = 0;
  99. struct iwl_rx_packet *pkt;
  100. struct iwl_host_cmd cmd = {
  101. .id = REPLY_SCAN_ABORT_CMD,
  102. .flags = CMD_WANT_SKB,
  103. };
  104. /* If there isn't a scan actively going on in the hardware
  105. * then we are in between scan bands and not actually
  106. * actively scanning, so don't send the abort command */
  107. if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
  108. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  109. return 0;
  110. }
  111. ret = iwl_send_cmd_sync(priv, &cmd);
  112. if (ret) {
  113. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  114. return ret;
  115. }
  116. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  117. if (pkt->u.status != CAN_ABORT_STATUS) {
  118. /* The scan abort will return 1 for success or
  119. * 2 for "failure". A failure condition can be
  120. * due to simply not being in an active scan which
  121. * can occur if we send the scan abort before we
  122. * the microcode has notified us that a scan is
  123. * completed. */
  124. IWL_DEBUG_INFO(priv, "SCAN_ABORT returned %d.\n", pkt->u.status);
  125. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  126. clear_bit(STATUS_SCAN_HW, &priv->status);
  127. }
  128. iwl_free_pages(priv, cmd.reply_page);
  129. return ret;
  130. }
  131. /* Service response to REPLY_SCAN_CMD (0x80) */
  132. static void iwl_rx_reply_scan(struct iwl_priv *priv,
  133. struct iwl_rx_mem_buffer *rxb)
  134. {
  135. #ifdef CONFIG_IWLWIFI_DEBUG
  136. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  137. struct iwl_scanreq_notification *notif =
  138. (struct iwl_scanreq_notification *)pkt->u.raw;
  139. IWL_DEBUG_RX(priv, "Scan request status = 0x%x\n", notif->status);
  140. #endif
  141. }
  142. /* Service SCAN_START_NOTIFICATION (0x82) */
  143. static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
  144. struct iwl_rx_mem_buffer *rxb)
  145. {
  146. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  147. struct iwl_scanstart_notification *notif =
  148. (struct iwl_scanstart_notification *)pkt->u.raw;
  149. priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
  150. IWL_DEBUG_SCAN(priv, "Scan start: "
  151. "%d [802.11%s] "
  152. "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
  153. notif->channel,
  154. notif->band ? "bg" : "a",
  155. le32_to_cpu(notif->tsf_high),
  156. le32_to_cpu(notif->tsf_low),
  157. notif->status, notif->beacon_timer);
  158. }
  159. /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
  160. static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
  161. struct iwl_rx_mem_buffer *rxb)
  162. {
  163. #ifdef CONFIG_IWLWIFI_DEBUG
  164. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  165. struct iwl_scanresults_notification *notif =
  166. (struct iwl_scanresults_notification *)pkt->u.raw;
  167. IWL_DEBUG_SCAN(priv, "Scan ch.res: "
  168. "%d [802.11%s] "
  169. "(TSF: 0x%08X:%08X) - %d "
  170. "elapsed=%lu usec\n",
  171. notif->channel,
  172. notif->band ? "bg" : "a",
  173. le32_to_cpu(notif->tsf_high),
  174. le32_to_cpu(notif->tsf_low),
  175. le32_to_cpu(notif->statistics[0]),
  176. le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf);
  177. #endif
  178. if (!priv->is_internal_short_scan)
  179. priv->next_scan_jiffies = 0;
  180. }
  181. /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
  182. static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
  183. struct iwl_rx_mem_buffer *rxb)
  184. {
  185. #ifdef CONFIG_IWLWIFI_DEBUG
  186. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  187. struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
  188. IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
  189. scan_notif->scanned_channels,
  190. scan_notif->tsf_low,
  191. scan_notif->tsf_high, scan_notif->status);
  192. #endif
  193. /* The HW is no longer scanning */
  194. clear_bit(STATUS_SCAN_HW, &priv->status);
  195. IWL_DEBUG_INFO(priv, "Scan pass on %sGHz took %dms\n",
  196. (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
  197. "2.4" : "5.2",
  198. jiffies_to_msecs(elapsed_jiffies
  199. (priv->scan_pass_start, jiffies)));
  200. /* Remove this scanned band from the list of pending
  201. * bands to scan, band G precedes A in order of scanning
  202. * as seen in iwl_bg_request_scan */
  203. if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
  204. priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
  205. else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
  206. priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
  207. /* If a request to abort was given, or the scan did not succeed
  208. * then we reset the scan state machine and terminate,
  209. * re-queuing another scan if one has been requested */
  210. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  211. IWL_DEBUG_INFO(priv, "Aborted scan completed.\n");
  212. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  213. } else {
  214. /* If there are more bands on this scan pass reschedule */
  215. if (priv->scan_bands)
  216. goto reschedule;
  217. }
  218. if (!priv->is_internal_short_scan)
  219. priv->next_scan_jiffies = 0;
  220. else
  221. priv->last_internal_scan_jiffies = jiffies;
  222. IWL_DEBUG_INFO(priv, "Setting scan to off\n");
  223. clear_bit(STATUS_SCANNING, &priv->status);
  224. IWL_DEBUG_INFO(priv, "Scan took %dms\n",
  225. jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
  226. queue_work(priv->workqueue, &priv->scan_completed);
  227. return;
  228. reschedule:
  229. priv->scan_pass_start = jiffies;
  230. queue_work(priv->workqueue, &priv->request_scan);
  231. }
  232. void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
  233. {
  234. /* scan handlers */
  235. priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
  236. priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
  237. priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
  238. iwl_rx_scan_results_notif;
  239. priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
  240. iwl_rx_scan_complete_notif;
  241. }
  242. EXPORT_SYMBOL(iwl_setup_rx_scan_handlers);
  243. inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
  244. enum ieee80211_band band,
  245. u8 n_probes)
  246. {
  247. if (band == IEEE80211_BAND_5GHZ)
  248. return IWL_ACTIVE_DWELL_TIME_52 +
  249. IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
  250. else
  251. return IWL_ACTIVE_DWELL_TIME_24 +
  252. IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
  253. }
  254. EXPORT_SYMBOL(iwl_get_active_dwell_time);
  255. u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
  256. enum ieee80211_band band)
  257. {
  258. u16 passive = (band == IEEE80211_BAND_2GHZ) ?
  259. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
  260. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
  261. if (iwl_is_associated(priv)) {
  262. /* If we're associated, we clamp the maximum passive
  263. * dwell time to be 98% of the beacon interval (minus
  264. * 2 * channel tune time) */
  265. passive = priv->beacon_int;
  266. if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
  267. passive = IWL_PASSIVE_DWELL_BASE;
  268. passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
  269. }
  270. return passive;
  271. }
  272. EXPORT_SYMBOL(iwl_get_passive_dwell_time);
  273. static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
  274. enum ieee80211_band band,
  275. struct iwl_scan_channel *scan_ch)
  276. {
  277. const struct ieee80211_supported_band *sband;
  278. const struct iwl_channel_info *ch_info;
  279. u16 passive_dwell = 0;
  280. u16 active_dwell = 0;
  281. int i, added = 0;
  282. u16 channel = 0;
  283. sband = iwl_get_hw_mode(priv, band);
  284. if (!sband) {
  285. IWL_ERR(priv, "invalid band\n");
  286. return added;
  287. }
  288. active_dwell = iwl_get_active_dwell_time(priv, band, 0);
  289. passive_dwell = iwl_get_passive_dwell_time(priv, band);
  290. if (passive_dwell <= active_dwell)
  291. passive_dwell = active_dwell + 1;
  292. /* only scan single channel, good enough to reset the RF */
  293. /* pick the first valid not in-use channel */
  294. if (band == IEEE80211_BAND_5GHZ) {
  295. for (i = 14; i < priv->channel_count; i++) {
  296. if (priv->channel_info[i].channel !=
  297. le16_to_cpu(priv->staging_rxon.channel)) {
  298. channel = priv->channel_info[i].channel;
  299. ch_info = iwl_get_channel_info(priv,
  300. band, channel);
  301. if (is_channel_valid(ch_info))
  302. break;
  303. }
  304. }
  305. } else {
  306. for (i = 0; i < 14; i++) {
  307. if (priv->channel_info[i].channel !=
  308. le16_to_cpu(priv->staging_rxon.channel)) {
  309. channel =
  310. priv->channel_info[i].channel;
  311. ch_info = iwl_get_channel_info(priv,
  312. band, channel);
  313. if (is_channel_valid(ch_info))
  314. break;
  315. }
  316. }
  317. }
  318. if (channel) {
  319. scan_ch->channel = cpu_to_le16(channel);
  320. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  321. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  322. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  323. /* Set txpower levels to defaults */
  324. scan_ch->dsp_atten = 110;
  325. if (band == IEEE80211_BAND_5GHZ)
  326. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  327. else
  328. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  329. added++;
  330. } else
  331. IWL_ERR(priv, "no valid channel found\n");
  332. return added;
  333. }
  334. static int iwl_get_channels_for_scan(struct iwl_priv *priv,
  335. enum ieee80211_band band,
  336. u8 is_active, u8 n_probes,
  337. struct iwl_scan_channel *scan_ch)
  338. {
  339. struct ieee80211_channel *chan;
  340. const struct ieee80211_supported_band *sband;
  341. const struct iwl_channel_info *ch_info;
  342. u16 passive_dwell = 0;
  343. u16 active_dwell = 0;
  344. int added, i;
  345. u16 channel;
  346. sband = iwl_get_hw_mode(priv, band);
  347. if (!sband)
  348. return 0;
  349. active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
  350. passive_dwell = iwl_get_passive_dwell_time(priv, band);
  351. if (passive_dwell <= active_dwell)
  352. passive_dwell = active_dwell + 1;
  353. for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
  354. chan = priv->scan_request->channels[i];
  355. if (chan->band != band)
  356. continue;
  357. channel = ieee80211_frequency_to_channel(chan->center_freq);
  358. scan_ch->channel = cpu_to_le16(channel);
  359. ch_info = iwl_get_channel_info(priv, band, channel);
  360. if (!is_channel_valid(ch_info)) {
  361. IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
  362. channel);
  363. continue;
  364. }
  365. if (!is_active || is_channel_passive(ch_info) ||
  366. (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
  367. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  368. else
  369. scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
  370. if (n_probes)
  371. scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
  372. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  373. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  374. /* Set txpower levels to defaults */
  375. scan_ch->dsp_atten = 110;
  376. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  377. * power level:
  378. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  379. */
  380. if (band == IEEE80211_BAND_5GHZ)
  381. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  382. else
  383. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  384. IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
  385. channel, le32_to_cpu(scan_ch->type),
  386. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  387. "ACTIVE" : "PASSIVE",
  388. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  389. active_dwell : passive_dwell);
  390. scan_ch++;
  391. added++;
  392. }
  393. IWL_DEBUG_SCAN(priv, "total channels to scan %d \n", added);
  394. return added;
  395. }
  396. void iwl_init_scan_params(struct iwl_priv *priv)
  397. {
  398. u8 ant_idx = fls(priv->hw_params.valid_tx_ant) - 1;
  399. if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
  400. priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
  401. if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
  402. priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
  403. }
  404. EXPORT_SYMBOL(iwl_init_scan_params);
  405. static int iwl_scan_initiate(struct iwl_priv *priv)
  406. {
  407. if (!iwl_is_ready_rf(priv)) {
  408. IWL_DEBUG_SCAN(priv, "Aborting scan due to not ready.\n");
  409. return -EIO;
  410. }
  411. if (test_bit(STATUS_SCANNING, &priv->status)) {
  412. IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
  413. return -EAGAIN;
  414. }
  415. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  416. IWL_DEBUG_SCAN(priv, "Scan request while abort pending\n");
  417. return -EAGAIN;
  418. }
  419. IWL_DEBUG_INFO(priv, "Starting scan...\n");
  420. set_bit(STATUS_SCANNING, &priv->status);
  421. priv->is_internal_short_scan = false;
  422. priv->scan_start = jiffies;
  423. priv->scan_pass_start = priv->scan_start;
  424. queue_work(priv->workqueue, &priv->request_scan);
  425. return 0;
  426. }
  427. #define IWL_DELAY_NEXT_SCAN (HZ*2)
  428. int iwl_mac_hw_scan(struct ieee80211_hw *hw,
  429. struct cfg80211_scan_request *req)
  430. {
  431. unsigned long flags;
  432. struct iwl_priv *priv = hw->priv;
  433. int ret, i;
  434. IWL_DEBUG_MAC80211(priv, "enter\n");
  435. mutex_lock(&priv->mutex);
  436. spin_lock_irqsave(&priv->lock, flags);
  437. if (!iwl_is_ready_rf(priv)) {
  438. ret = -EIO;
  439. IWL_DEBUG_MAC80211(priv, "leave - not ready or exit pending\n");
  440. goto out_unlock;
  441. }
  442. /* We don't schedule scan within next_scan_jiffies period.
  443. * Avoid scanning during possible EAPOL exchange, return
  444. * success immediately.
  445. */
  446. if (priv->next_scan_jiffies &&
  447. time_after(priv->next_scan_jiffies, jiffies)) {
  448. IWL_DEBUG_SCAN(priv, "scan rejected: within next scan period\n");
  449. queue_work(priv->workqueue, &priv->scan_completed);
  450. ret = 0;
  451. goto out_unlock;
  452. }
  453. priv->scan_bands = 0;
  454. for (i = 0; i < req->n_channels; i++)
  455. priv->scan_bands |= BIT(req->channels[i]->band);
  456. priv->scan_request = req;
  457. ret = iwl_scan_initiate(priv);
  458. IWL_DEBUG_MAC80211(priv, "leave\n");
  459. out_unlock:
  460. spin_unlock_irqrestore(&priv->lock, flags);
  461. mutex_unlock(&priv->mutex);
  462. return ret;
  463. }
  464. EXPORT_SYMBOL(iwl_mac_hw_scan);
  465. /*
  466. * internal short scan, this function should only been called while associated.
  467. * It will reset and tune the radio to prevent possible RF related problem
  468. */
  469. #define IWL_DELAY_NEXT_INTERNAL_SCAN (HZ*1)
  470. int iwl_internal_short_hw_scan(struct iwl_priv *priv)
  471. {
  472. int ret = 0;
  473. if (!iwl_is_ready_rf(priv)) {
  474. ret = -EIO;
  475. IWL_DEBUG_SCAN(priv, "not ready or exit pending\n");
  476. goto out;
  477. }
  478. if (test_bit(STATUS_SCANNING, &priv->status)) {
  479. IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
  480. ret = -EAGAIN;
  481. goto out;
  482. }
  483. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  484. IWL_DEBUG_SCAN(priv, "Scan request while abort pending\n");
  485. ret = -EAGAIN;
  486. goto out;
  487. }
  488. if (priv->last_internal_scan_jiffies &&
  489. time_after(priv->last_internal_scan_jiffies +
  490. IWL_DELAY_NEXT_INTERNAL_SCAN, jiffies)) {
  491. IWL_DEBUG_SCAN(priv, "internal scan rejected\n");
  492. goto out;
  493. }
  494. priv->scan_bands = 0;
  495. if (priv->band == IEEE80211_BAND_5GHZ)
  496. priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
  497. else
  498. priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
  499. IWL_DEBUG_SCAN(priv, "Start internal short scan...\n");
  500. set_bit(STATUS_SCANNING, &priv->status);
  501. priv->is_internal_short_scan = true;
  502. queue_work(priv->workqueue, &priv->request_scan);
  503. out:
  504. return ret;
  505. }
  506. EXPORT_SYMBOL(iwl_internal_short_hw_scan);
  507. #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
  508. void iwl_bg_scan_check(struct work_struct *data)
  509. {
  510. struct iwl_priv *priv =
  511. container_of(data, struct iwl_priv, scan_check.work);
  512. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  513. return;
  514. mutex_lock(&priv->mutex);
  515. if (test_bit(STATUS_SCANNING, &priv->status) ||
  516. test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  517. IWL_DEBUG_SCAN(priv, "Scan completion watchdog resetting "
  518. "adapter (%dms)\n",
  519. jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
  520. if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
  521. iwl_send_scan_abort(priv);
  522. }
  523. mutex_unlock(&priv->mutex);
  524. }
  525. EXPORT_SYMBOL(iwl_bg_scan_check);
  526. /**
  527. * iwl_fill_probe_req - fill in all required fields and IE for probe request
  528. */
  529. u16 iwl_fill_probe_req(struct iwl_priv *priv, struct ieee80211_mgmt *frame,
  530. const u8 *ies, int ie_len, int left)
  531. {
  532. int len = 0;
  533. u8 *pos = NULL;
  534. /* Make sure there is enough space for the probe request,
  535. * two mandatory IEs and the data */
  536. left -= 24;
  537. if (left < 0)
  538. return 0;
  539. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  540. memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
  541. memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
  542. memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
  543. frame->seq_ctrl = 0;
  544. len += 24;
  545. /* ...next IE... */
  546. pos = &frame->u.probe_req.variable[0];
  547. /* fill in our indirect SSID IE */
  548. left -= 2;
  549. if (left < 0)
  550. return 0;
  551. *pos++ = WLAN_EID_SSID;
  552. if (!priv->is_internal_short_scan &&
  553. priv->scan_request->n_ssids) {
  554. struct cfg80211_ssid *ssid =
  555. priv->scan_request->ssids;
  556. /* Broadcast if ssid_len is 0 */
  557. *pos++ = ssid->ssid_len;
  558. memcpy(pos, ssid->ssid, ssid->ssid_len);
  559. pos += ssid->ssid_len;
  560. len += 2 + ssid->ssid_len;
  561. } else {
  562. *pos++ = 0;
  563. len += 2;
  564. }
  565. if (WARN_ON(left < ie_len))
  566. return len;
  567. if (ies)
  568. memcpy(pos, ies, ie_len);
  569. len += ie_len;
  570. left -= ie_len;
  571. return (u16)len;
  572. }
  573. EXPORT_SYMBOL(iwl_fill_probe_req);
  574. static void iwl_bg_request_scan(struct work_struct *data)
  575. {
  576. struct iwl_priv *priv =
  577. container_of(data, struct iwl_priv, request_scan);
  578. struct iwl_host_cmd cmd = {
  579. .id = REPLY_SCAN_CMD,
  580. .len = sizeof(struct iwl_scan_cmd),
  581. .flags = CMD_SIZE_HUGE,
  582. };
  583. struct iwl_scan_cmd *scan;
  584. struct ieee80211_conf *conf = NULL;
  585. int ret = 0;
  586. u32 rate_flags = 0;
  587. u16 cmd_len;
  588. u16 rx_chain = 0;
  589. enum ieee80211_band band;
  590. u8 n_probes = 0;
  591. u8 rx_ant = priv->hw_params.valid_rx_ant;
  592. u8 rate;
  593. bool is_active = false;
  594. int chan_mod;
  595. u8 active_chains;
  596. conf = ieee80211_get_hw_conf(priv->hw);
  597. mutex_lock(&priv->mutex);
  598. cancel_delayed_work(&priv->scan_check);
  599. if (!iwl_is_ready(priv)) {
  600. IWL_WARN(priv, "request scan called when driver not ready.\n");
  601. goto done;
  602. }
  603. /* Make sure the scan wasn't canceled before this queued work
  604. * was given the chance to run... */
  605. if (!test_bit(STATUS_SCANNING, &priv->status))
  606. goto done;
  607. /* This should never be called or scheduled if there is currently
  608. * a scan active in the hardware. */
  609. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  610. IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests in parallel. "
  611. "Ignoring second request.\n");
  612. ret = -EIO;
  613. goto done;
  614. }
  615. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  616. IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
  617. goto done;
  618. }
  619. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  620. IWL_DEBUG_HC(priv, "Scan request while abort pending. Queuing.\n");
  621. goto done;
  622. }
  623. if (iwl_is_rfkill(priv)) {
  624. IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
  625. goto done;
  626. }
  627. if (!test_bit(STATUS_READY, &priv->status)) {
  628. IWL_DEBUG_HC(priv, "Scan request while uninitialized. Queuing.\n");
  629. goto done;
  630. }
  631. if (!priv->scan_bands) {
  632. IWL_DEBUG_HC(priv, "Aborting scan due to no requested bands\n");
  633. goto done;
  634. }
  635. if (!priv->scan) {
  636. priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
  637. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  638. if (!priv->scan) {
  639. ret = -ENOMEM;
  640. goto done;
  641. }
  642. }
  643. scan = priv->scan;
  644. memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
  645. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  646. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  647. if (iwl_is_associated(priv)) {
  648. u16 interval = 0;
  649. u32 extra;
  650. u32 suspend_time = 100;
  651. u32 scan_suspend_time = 100;
  652. unsigned long flags;
  653. IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
  654. spin_lock_irqsave(&priv->lock, flags);
  655. interval = priv->beacon_int;
  656. spin_unlock_irqrestore(&priv->lock, flags);
  657. scan->suspend_time = 0;
  658. scan->max_out_time = cpu_to_le32(200 * 1024);
  659. if (!interval)
  660. interval = suspend_time;
  661. extra = (suspend_time / interval) << 22;
  662. scan_suspend_time = (extra |
  663. ((suspend_time % interval) * 1024));
  664. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  665. IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
  666. scan_suspend_time, interval);
  667. }
  668. if (priv->is_internal_short_scan) {
  669. IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
  670. } else if (priv->scan_request->n_ssids) {
  671. IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
  672. /*
  673. * The first SSID to scan is stuffed into the probe request
  674. * template and the remaining ones are handled through the
  675. * direct_scan array.
  676. */
  677. if (priv->scan_request->n_ssids > 1) {
  678. int i, p = 0;
  679. for (i = 1; i < priv->scan_request->n_ssids; i++) {
  680. if (!priv->scan_request->ssids[i].ssid_len)
  681. continue;
  682. scan->direct_scan[p].id = WLAN_EID_SSID;
  683. scan->direct_scan[p].len =
  684. priv->scan_request->ssids[i].ssid_len;
  685. memcpy(scan->direct_scan[p].ssid,
  686. priv->scan_request->ssids[i].ssid,
  687. priv->scan_request->ssids[i].ssid_len);
  688. n_probes++;
  689. p++;
  690. }
  691. }
  692. is_active = true;
  693. } else
  694. IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
  695. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  696. scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
  697. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  698. if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
  699. band = IEEE80211_BAND_2GHZ;
  700. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  701. chan_mod = le32_to_cpu(priv->active_rxon.flags & RXON_FLG_CHANNEL_MODE_MSK)
  702. >> RXON_FLG_CHANNEL_MODE_POS;
  703. if (chan_mod == CHANNEL_MODE_PURE_40) {
  704. rate = IWL_RATE_6M_PLCP;
  705. } else {
  706. rate = IWL_RATE_1M_PLCP;
  707. rate_flags = RATE_MCS_CCK_MSK;
  708. }
  709. scan->good_CRC_th = 0;
  710. } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
  711. band = IEEE80211_BAND_5GHZ;
  712. rate = IWL_RATE_6M_PLCP;
  713. /*
  714. * If active scaning is requested but a certain channel
  715. * is marked passive, we can do active scanning if we
  716. * detect transmissions.
  717. */
  718. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH : 0;
  719. /* Force use of chains B and C (0x6) for scan Rx for 4965
  720. * Avoid A (0x1) because of its off-channel reception on A-band.
  721. */
  722. if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
  723. rx_ant = ANT_BC;
  724. } else {
  725. IWL_WARN(priv, "Invalid scan band count\n");
  726. goto done;
  727. }
  728. priv->scan_tx_ant[band] =
  729. iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band]);
  730. rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
  731. scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
  732. /* In power save mode use one chain, otherwise use all chains */
  733. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  734. /* rx_ant has been set to all valid chains previously */
  735. active_chains = rx_ant &
  736. ((u8)(priv->chain_noise_data.active_chains));
  737. if (!active_chains)
  738. active_chains = rx_ant;
  739. IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
  740. priv->chain_noise_data.active_chains);
  741. rx_ant = first_antenna(active_chains);
  742. }
  743. /* MIMO is not used here, but value is required */
  744. rx_chain |= priv->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
  745. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
  746. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
  747. rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
  748. scan->rx_chain = cpu_to_le16(rx_chain);
  749. if (!priv->is_internal_short_scan) {
  750. cmd_len = iwl_fill_probe_req(priv,
  751. (struct ieee80211_mgmt *)scan->data,
  752. priv->scan_request->ie,
  753. priv->scan_request->ie_len,
  754. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  755. } else {
  756. cmd_len = iwl_fill_probe_req(priv,
  757. (struct ieee80211_mgmt *)scan->data,
  758. NULL, 0,
  759. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  760. }
  761. scan->tx_cmd.len = cpu_to_le16(cmd_len);
  762. if (iwl_is_monitor_mode(priv))
  763. scan->filter_flags = RXON_FILTER_PROMISC_MSK;
  764. scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
  765. RXON_FILTER_BCON_AWARE_MSK);
  766. if (priv->is_internal_short_scan) {
  767. scan->channel_count =
  768. iwl_get_single_channel_for_scan(priv, band,
  769. (void *)&scan->data[le16_to_cpu(
  770. scan->tx_cmd.len)]);
  771. } else {
  772. scan->channel_count =
  773. iwl_get_channels_for_scan(priv, band,
  774. is_active, n_probes,
  775. (void *)&scan->data[le16_to_cpu(
  776. scan->tx_cmd.len)]);
  777. }
  778. if (scan->channel_count == 0) {
  779. IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
  780. goto done;
  781. }
  782. cmd.len += le16_to_cpu(scan->tx_cmd.len) +
  783. scan->channel_count * sizeof(struct iwl_scan_channel);
  784. cmd.data = scan;
  785. scan->len = cpu_to_le16(cmd.len);
  786. set_bit(STATUS_SCAN_HW, &priv->status);
  787. ret = iwl_send_cmd_sync(priv, &cmd);
  788. if (ret)
  789. goto done;
  790. queue_delayed_work(priv->workqueue, &priv->scan_check,
  791. IWL_SCAN_CHECK_WATCHDOG);
  792. mutex_unlock(&priv->mutex);
  793. return;
  794. done:
  795. /* Cannot perform scan. Make sure we clear scanning
  796. * bits from status so next scan request can be performed.
  797. * If we don't clear scanning status bit here all next scan
  798. * will fail
  799. */
  800. clear_bit(STATUS_SCAN_HW, &priv->status);
  801. clear_bit(STATUS_SCANNING, &priv->status);
  802. /* inform mac80211 scan aborted */
  803. queue_work(priv->workqueue, &priv->scan_completed);
  804. mutex_unlock(&priv->mutex);
  805. }
  806. void iwl_bg_abort_scan(struct work_struct *work)
  807. {
  808. struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
  809. if (!test_bit(STATUS_READY, &priv->status) ||
  810. !test_bit(STATUS_GEO_CONFIGURED, &priv->status))
  811. return;
  812. mutex_lock(&priv->mutex);
  813. set_bit(STATUS_SCAN_ABORTING, &priv->status);
  814. iwl_send_scan_abort(priv);
  815. mutex_unlock(&priv->mutex);
  816. }
  817. EXPORT_SYMBOL(iwl_bg_abort_scan);
  818. void iwl_bg_scan_completed(struct work_struct *work)
  819. {
  820. struct iwl_priv *priv =
  821. container_of(work, struct iwl_priv, scan_completed);
  822. IWL_DEBUG_SCAN(priv, "SCAN complete scan\n");
  823. cancel_delayed_work(&priv->scan_check);
  824. if (!priv->is_internal_short_scan)
  825. ieee80211_scan_completed(priv->hw, false);
  826. else {
  827. priv->is_internal_short_scan = false;
  828. IWL_DEBUG_SCAN(priv, "internal short scan completed\n");
  829. }
  830. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  831. return;
  832. /* Since setting the TXPOWER may have been deferred while
  833. * performing the scan, fire one off */
  834. mutex_lock(&priv->mutex);
  835. iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
  836. mutex_unlock(&priv->mutex);
  837. }
  838. EXPORT_SYMBOL(iwl_bg_scan_completed);
  839. void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
  840. {
  841. INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
  842. INIT_WORK(&priv->request_scan, iwl_bg_request_scan);
  843. INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
  844. INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
  845. }
  846. EXPORT_SYMBOL(iwl_setup_scan_deferred_work);