iwl-scan.c 30 KB

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