iwl-scan.c 31 KB

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