iwl-scan.c 31 KB

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