scan.c 30 KB

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