scan.c 30 KB

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