scan.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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 <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_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_NO_IR))
  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. };
  543. struct iwl_scan_cmd *scan;
  544. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  545. u32 rate_flags = 0;
  546. u16 cmd_len = 0;
  547. u16 rx_chain = 0;
  548. enum ieee80211_band band;
  549. u8 n_probes = 0;
  550. u8 rx_ant = priv->nvm_data->valid_rx_ant;
  551. u8 rate;
  552. bool is_active = false;
  553. int chan_mod;
  554. u8 active_chains;
  555. u8 scan_tx_antennas = priv->nvm_data->valid_tx_ant;
  556. int ret;
  557. int scan_cmd_size = sizeof(struct iwl_scan_cmd) +
  558. MAX_SCAN_CHANNEL * sizeof(struct iwl_scan_channel) +
  559. priv->fw->ucode_capa.max_probe_length;
  560. const u8 *ssid = NULL;
  561. u8 ssid_len = 0;
  562. if (WARN_ON(priv->scan_type == IWL_SCAN_NORMAL &&
  563. (!priv->scan_request ||
  564. priv->scan_request->n_channels > MAX_SCAN_CHANNEL)))
  565. return -EINVAL;
  566. lockdep_assert_held(&priv->mutex);
  567. if (vif)
  568. ctx = iwl_rxon_ctx_from_vif(vif);
  569. if (!priv->scan_cmd) {
  570. priv->scan_cmd = kmalloc(scan_cmd_size, GFP_KERNEL);
  571. if (!priv->scan_cmd) {
  572. IWL_DEBUG_SCAN(priv,
  573. "fail to allocate memory for scan\n");
  574. return -ENOMEM;
  575. }
  576. }
  577. scan = priv->scan_cmd;
  578. memset(scan, 0, scan_cmd_size);
  579. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  580. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  581. if (iwl_is_any_associated(priv)) {
  582. u16 interval = 0;
  583. u32 extra;
  584. u32 suspend_time = 100;
  585. u32 scan_suspend_time = 100;
  586. IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
  587. switch (priv->scan_type) {
  588. case IWL_SCAN_RADIO_RESET:
  589. interval = 0;
  590. break;
  591. case IWL_SCAN_NORMAL:
  592. interval = vif->bss_conf.beacon_int;
  593. break;
  594. }
  595. scan->suspend_time = 0;
  596. scan->max_out_time = cpu_to_le32(200 * 1024);
  597. if (!interval)
  598. interval = suspend_time;
  599. extra = (suspend_time / interval) << 22;
  600. scan_suspend_time = (extra |
  601. ((suspend_time % interval) * 1024));
  602. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  603. IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
  604. scan_suspend_time, interval);
  605. }
  606. switch (priv->scan_type) {
  607. case IWL_SCAN_RADIO_RESET:
  608. IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
  609. /*
  610. * Override quiet time as firmware checks that active
  611. * dwell is >= quiet; since we use passive scan it'll
  612. * not actually be used.
  613. */
  614. scan->quiet_time = cpu_to_le16(IWL_RADIO_RESET_DWELL_TIME);
  615. break;
  616. case IWL_SCAN_NORMAL:
  617. if (priv->scan_request->n_ssids) {
  618. int i, p = 0;
  619. IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
  620. /*
  621. * The highest priority SSID is inserted to the
  622. * probe request template.
  623. */
  624. ssid_len = priv->scan_request->ssids[0].ssid_len;
  625. ssid = priv->scan_request->ssids[0].ssid;
  626. /*
  627. * Invert the order of ssids, the firmware will invert
  628. * it back.
  629. */
  630. for (i = priv->scan_request->n_ssids - 1; i >= 1; i--) {
  631. scan->direct_scan[p].id = WLAN_EID_SSID;
  632. scan->direct_scan[p].len =
  633. priv->scan_request->ssids[i].ssid_len;
  634. memcpy(scan->direct_scan[p].ssid,
  635. priv->scan_request->ssids[i].ssid,
  636. priv->scan_request->ssids[i].ssid_len);
  637. n_probes++;
  638. p++;
  639. }
  640. is_active = true;
  641. } else
  642. IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
  643. break;
  644. }
  645. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  646. scan->tx_cmd.sta_id = ctx->bcast_sta_id;
  647. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  648. switch (priv->scan_band) {
  649. case IEEE80211_BAND_2GHZ:
  650. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  651. chan_mod = le32_to_cpu(
  652. priv->contexts[IWL_RXON_CTX_BSS].active.flags &
  653. RXON_FLG_CHANNEL_MODE_MSK)
  654. >> RXON_FLG_CHANNEL_MODE_POS;
  655. if ((priv->scan_request && priv->scan_request->no_cck) ||
  656. chan_mod == CHANNEL_MODE_PURE_40) {
  657. rate = IWL_RATE_6M_PLCP;
  658. } else {
  659. rate = IWL_RATE_1M_PLCP;
  660. rate_flags = RATE_MCS_CCK_MSK;
  661. }
  662. /*
  663. * Internal scans are passive, so we can indiscriminately set
  664. * the BT ignore flag on 2.4 GHz since it applies to TX only.
  665. */
  666. if (priv->lib->bt_params &&
  667. priv->lib->bt_params->advanced_bt_coexist)
  668. scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
  669. break;
  670. case IEEE80211_BAND_5GHZ:
  671. rate = IWL_RATE_6M_PLCP;
  672. break;
  673. default:
  674. IWL_WARN(priv, "Invalid scan band\n");
  675. return -EIO;
  676. }
  677. /*
  678. * If active scanning is requested but a certain channel is
  679. * marked passive, we can do active scanning if we detect
  680. * transmissions.
  681. *
  682. * There is an issue with some firmware versions that triggers
  683. * a sysassert on a "good CRC threshold" of zero (== disabled),
  684. * on a radar channel even though this means that we should NOT
  685. * send probes.
  686. *
  687. * The "good CRC threshold" is the number of frames that we
  688. * need to receive during our dwell time on a channel before
  689. * sending out probes -- setting this to a huge value will
  690. * mean we never reach it, but at the same time work around
  691. * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
  692. * here instead of IWL_GOOD_CRC_TH_DISABLED.
  693. *
  694. * This was fixed in later versions along with some other
  695. * scan changes, and the threshold behaves as a flag in those
  696. * versions.
  697. */
  698. if (priv->new_scan_threshold_behaviour)
  699. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  700. IWL_GOOD_CRC_TH_DISABLED;
  701. else
  702. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  703. IWL_GOOD_CRC_TH_NEVER;
  704. band = priv->scan_band;
  705. if (band == IEEE80211_BAND_2GHZ &&
  706. priv->lib->bt_params &&
  707. priv->lib->bt_params->advanced_bt_coexist) {
  708. /* transmit 2.4 GHz probes only on first antenna */
  709. scan_tx_antennas = first_antenna(scan_tx_antennas);
  710. }
  711. priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv,
  712. priv->scan_tx_ant[band],
  713. scan_tx_antennas);
  714. rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
  715. scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
  716. /*
  717. * In power save mode while associated use one chain,
  718. * otherwise use all chains
  719. */
  720. if (test_bit(STATUS_POWER_PMI, &priv->status) &&
  721. !(priv->hw->conf.flags & IEEE80211_CONF_IDLE)) {
  722. /* rx_ant has been set to all valid chains previously */
  723. active_chains = rx_ant &
  724. ((u8)(priv->chain_noise_data.active_chains));
  725. if (!active_chains)
  726. active_chains = rx_ant;
  727. IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
  728. priv->chain_noise_data.active_chains);
  729. rx_ant = first_antenna(active_chains);
  730. }
  731. if (priv->lib->bt_params &&
  732. priv->lib->bt_params->advanced_bt_coexist &&
  733. priv->bt_full_concurrent) {
  734. /* operated as 1x1 in full concurrency mode */
  735. rx_ant = first_antenna(rx_ant);
  736. }
  737. /* MIMO is not used here, but value is required */
  738. rx_chain |=
  739. priv->nvm_data->valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
  740. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
  741. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
  742. rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
  743. scan->rx_chain = cpu_to_le16(rx_chain);
  744. switch (priv->scan_type) {
  745. case IWL_SCAN_NORMAL:
  746. cmd_len = iwl_fill_probe_req(
  747. (struct ieee80211_mgmt *)scan->data,
  748. vif->addr,
  749. priv->scan_request->ie,
  750. priv->scan_request->ie_len,
  751. ssid, ssid_len,
  752. scan_cmd_size - sizeof(*scan));
  753. break;
  754. case IWL_SCAN_RADIO_RESET:
  755. /* use bcast addr, will not be transmitted but must be valid */
  756. cmd_len = iwl_fill_probe_req(
  757. (struct ieee80211_mgmt *)scan->data,
  758. iwl_bcast_addr, NULL, 0,
  759. NULL, 0,
  760. scan_cmd_size - sizeof(*scan));
  761. break;
  762. default:
  763. BUG();
  764. }
  765. scan->tx_cmd.len = cpu_to_le16(cmd_len);
  766. scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
  767. RXON_FILTER_BCON_AWARE_MSK);
  768. switch (priv->scan_type) {
  769. case IWL_SCAN_RADIO_RESET:
  770. scan->channel_count =
  771. iwl_get_channel_for_reset_scan(priv, vif, band,
  772. (void *)&scan->data[cmd_len]);
  773. break;
  774. case IWL_SCAN_NORMAL:
  775. scan->channel_count =
  776. iwl_get_channels_for_scan(priv, vif, band,
  777. is_active, n_probes,
  778. (void *)&scan->data[cmd_len]);
  779. break;
  780. }
  781. if (scan->channel_count == 0) {
  782. IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
  783. return -EIO;
  784. }
  785. cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
  786. scan->channel_count * sizeof(struct iwl_scan_channel);
  787. cmd.data[0] = scan;
  788. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  789. scan->len = cpu_to_le16(cmd.len[0]);
  790. /* set scan bit here for PAN params */
  791. set_bit(STATUS_SCAN_HW, &priv->status);
  792. ret = iwlagn_set_pan_params(priv);
  793. if (ret) {
  794. clear_bit(STATUS_SCAN_HW, &priv->status);
  795. return ret;
  796. }
  797. ret = iwl_dvm_send_cmd(priv, &cmd);
  798. if (ret) {
  799. clear_bit(STATUS_SCAN_HW, &priv->status);
  800. iwlagn_set_pan_params(priv);
  801. }
  802. return ret;
  803. }
  804. void iwl_init_scan_params(struct iwl_priv *priv)
  805. {
  806. u8 ant_idx = fls(priv->nvm_data->valid_tx_ant) - 1;
  807. if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
  808. priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
  809. if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
  810. priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
  811. }
  812. int __must_check iwl_scan_initiate(struct iwl_priv *priv,
  813. struct ieee80211_vif *vif,
  814. enum iwl_scan_type scan_type,
  815. enum ieee80211_band band)
  816. {
  817. int ret;
  818. lockdep_assert_held(&priv->mutex);
  819. cancel_delayed_work(&priv->scan_check);
  820. if (!iwl_is_ready_rf(priv)) {
  821. IWL_WARN(priv, "Request scan called when driver not ready.\n");
  822. return -EIO;
  823. }
  824. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  825. IWL_DEBUG_SCAN(priv,
  826. "Multiple concurrent scan requests in parallel.\n");
  827. return -EBUSY;
  828. }
  829. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  830. IWL_DEBUG_SCAN(priv, "Scan request while abort pending.\n");
  831. return -EBUSY;
  832. }
  833. IWL_DEBUG_SCAN(priv, "Starting %sscan...\n",
  834. scan_type == IWL_SCAN_NORMAL ? "" :
  835. "internal short ");
  836. set_bit(STATUS_SCANNING, &priv->status);
  837. priv->scan_type = scan_type;
  838. priv->scan_start = jiffies;
  839. priv->scan_band = band;
  840. ret = iwlagn_request_scan(priv, vif);
  841. if (ret) {
  842. clear_bit(STATUS_SCANNING, &priv->status);
  843. priv->scan_type = IWL_SCAN_NORMAL;
  844. return ret;
  845. }
  846. queue_delayed_work(priv->workqueue, &priv->scan_check,
  847. IWL_SCAN_CHECK_WATCHDOG);
  848. return 0;
  849. }
  850. /*
  851. * internal short scan, this function should only been called while associated.
  852. * It will reset and tune the radio to prevent possible RF related problem
  853. */
  854. void iwl_internal_short_hw_scan(struct iwl_priv *priv)
  855. {
  856. queue_work(priv->workqueue, &priv->start_internal_scan);
  857. }
  858. static void iwl_bg_start_internal_scan(struct work_struct *work)
  859. {
  860. struct iwl_priv *priv =
  861. container_of(work, struct iwl_priv, start_internal_scan);
  862. IWL_DEBUG_SCAN(priv, "Start internal scan\n");
  863. mutex_lock(&priv->mutex);
  864. if (priv->scan_type == IWL_SCAN_RADIO_RESET) {
  865. IWL_DEBUG_SCAN(priv, "Internal scan already in progress\n");
  866. goto unlock;
  867. }
  868. if (test_bit(STATUS_SCANNING, &priv->status)) {
  869. IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
  870. goto unlock;
  871. }
  872. if (iwl_scan_initiate(priv, NULL, IWL_SCAN_RADIO_RESET, priv->band))
  873. IWL_DEBUG_SCAN(priv, "failed to start internal short scan\n");
  874. unlock:
  875. mutex_unlock(&priv->mutex);
  876. }
  877. static void iwl_bg_scan_check(struct work_struct *data)
  878. {
  879. struct iwl_priv *priv =
  880. container_of(data, struct iwl_priv, scan_check.work);
  881. IWL_DEBUG_SCAN(priv, "Scan check work\n");
  882. /* Since we are here firmware does not finish scan and
  883. * most likely is in bad shape, so we don't bother to
  884. * send abort command, just force scan complete to mac80211 */
  885. mutex_lock(&priv->mutex);
  886. iwl_force_scan_end(priv);
  887. mutex_unlock(&priv->mutex);
  888. }
  889. static void iwl_bg_abort_scan(struct work_struct *work)
  890. {
  891. struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
  892. IWL_DEBUG_SCAN(priv, "Abort scan work\n");
  893. /* We keep scan_check work queued in case when firmware will not
  894. * report back scan completed notification */
  895. mutex_lock(&priv->mutex);
  896. iwl_scan_cancel_timeout(priv, 200);
  897. mutex_unlock(&priv->mutex);
  898. }
  899. static void iwl_bg_scan_completed(struct work_struct *work)
  900. {
  901. struct iwl_priv *priv =
  902. container_of(work, struct iwl_priv, scan_completed);
  903. mutex_lock(&priv->mutex);
  904. iwl_process_scan_complete(priv);
  905. mutex_unlock(&priv->mutex);
  906. }
  907. void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
  908. {
  909. INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
  910. INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
  911. INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan);
  912. INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
  913. }
  914. void iwl_cancel_scan_deferred_work(struct iwl_priv *priv)
  915. {
  916. cancel_work_sync(&priv->start_internal_scan);
  917. cancel_work_sync(&priv->abort_scan);
  918. cancel_work_sync(&priv->scan_completed);
  919. if (cancel_delayed_work_sync(&priv->scan_check)) {
  920. mutex_lock(&priv->mutex);
  921. iwl_force_scan_end(priv);
  922. mutex_unlock(&priv->mutex);
  923. }
  924. }