Commit | Line | Data |
---|---|---|
2a421b91 TW |
1 | /****************************************************************************** |
2 | * | |
3 | * GPL LICENSE SUMMARY | |
4 | * | |
128e63ef | 5 | * Copyright(c) 2008 - 2013 Intel Corporation. All rights reserved. |
2a421b91 TW |
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 | |
410dc5aa | 22 | * in the file called COPYING. |
2a421b91 TW |
23 | * |
24 | * Contact Information: | |
759ef89f | 25 | * Intel Linux Wireless <ilw@linux.intel.com> |
2a421b91 TW |
26 | * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 |
27 | *****************************************************************************/ | |
5a0e3ad6 | 28 | #include <linux/slab.h> |
7e272fcf | 29 | #include <linux/types.h> |
2a421b91 | 30 | #include <linux/etherdevice.h> |
7e272fcf | 31 | #include <net/mac80211.h> |
2a421b91 | 32 | |
1023fdc4 JB |
33 | #include "dev.h" |
34 | #include "agn.h" | |
2a421b91 TW |
35 | |
36 | /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after | |
37 | * sending probe req. This should be set long enough to hear probe responses | |
38 | * from more than one AP. */ | |
fe905f1d TW |
39 | #define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */ |
40 | #define IWL_ACTIVE_DWELL_TIME_52 (20) | |
41 | ||
42 | #define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3) | |
43 | #define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2) | |
2a421b91 | 44 | |
2a421b91 TW |
45 | /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel. |
46 | * Must be set longer than active dwell time. | |
47 | * For the most reliable scan, set > AP beacon interval (typically 100msec). */ | |
48 | #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */ | |
49 | #define IWL_PASSIVE_DWELL_TIME_52 (10) | |
50 | #define IWL_PASSIVE_DWELL_BASE (100) | |
51 | #define IWL_CHANNEL_TUNE_TIME 5 | |
0ed46287 | 52 | #define MAX_SCAN_CHANNEL 50 |
2a421b91 | 53 | |
0d4e0772 JB |
54 | /* For reset radio, need minimal dwell time only */ |
55 | #define IWL_RADIO_RESET_DWELL_TIME 5 | |
56 | ||
cd44600f SG |
57 | static int iwl_send_scan_abort(struct iwl_priv *priv) |
58 | { | |
59 | int ret; | |
cd44600f SG |
60 | struct iwl_host_cmd cmd = { |
61 | .id = REPLY_SCAN_ABORT_CMD, | |
e419d62d | 62 | .flags = CMD_SYNC | CMD_WANT_SKB, |
cd44600f | 63 | }; |
f8d7c1a1 | 64 | __le32 *status; |
fe905f1d | 65 | |
cd44600f SG |
66 | /* Exit instantly with error when device is not ready |
67 | * to receive scan abort command or it does not perform | |
68 | * hardware scan currently */ | |
83626404 | 69 | if (!test_bit(STATUS_READY, &priv->status) || |
83626404 | 70 | !test_bit(STATUS_SCAN_HW, &priv->status) || |
17acd0b6 | 71 | test_bit(STATUS_FW_ERROR, &priv->status)) |
cd44600f | 72 | return -EIO; |
fe905f1d | 73 | |
e10a0533 | 74 | ret = iwl_dvm_send_cmd(priv, &cmd); |
cd44600f SG |
75 | if (ret) |
76 | return ret; | |
77 | ||
f8d7c1a1 JB |
78 | status = (void *)cmd.resp_pkt->data; |
79 | if (*status != CAN_ABORT_STATUS) { | |
cd44600f SG |
80 | /* The scan abort will return 1 for success or |
81 | * 2 for "failure". A failure condition can be | |
82 | * due to simply not being in an active scan which | |
83 | * can occur if we send the scan abort before we | |
84 | * the microcode has notified us that a scan is | |
85 | * completed. */ | |
f8d7c1a1 JB |
86 | IWL_DEBUG_SCAN(priv, "SCAN_ABORT ret %d.\n", |
87 | le32_to_cpu(*status)); | |
cd44600f | 88 | ret = -EIO; |
2a421b91 TW |
89 | } |
90 | ||
65b94a4a | 91 | iwl_free_resp(&cmd); |
cd44600f SG |
92 | return ret; |
93 | } | |
2a421b91 | 94 | |
02d8c14b SG |
95 | static void iwl_complete_scan(struct iwl_priv *priv, bool aborted) |
96 | { | |
97 | /* check if scan was requested from mac80211 */ | |
98 | if (priv->scan_request) { | |
99 | IWL_DEBUG_SCAN(priv, "Complete scan in mac80211\n"); | |
100 | ieee80211_scan_completed(priv->hw, aborted); | |
101 | } | |
102 | ||
622a9268 IP |
103 | if (priv->scan_type == IWL_SCAN_ROC) |
104 | iwl_scan_roc_expired(priv); | |
c6baf7fb | 105 | |
266af4c7 | 106 | priv->scan_type = IWL_SCAN_NORMAL; |
02d8c14b SG |
107 | priv->scan_vif = NULL; |
108 | priv->scan_request = NULL; | |
109 | } | |
110 | ||
a2fa2462 JB |
111 | static void iwl_process_scan_complete(struct iwl_priv *priv) |
112 | { | |
113 | bool aborted; | |
114 | ||
b1eea297 | 115 | lockdep_assert_held(&priv->mutex); |
84b1bec6 | 116 | |
83626404 | 117 | if (!test_and_clear_bit(STATUS_SCAN_COMPLETE, &priv->status)) |
84b1bec6 JB |
118 | return; |
119 | ||
a2fa2462 JB |
120 | IWL_DEBUG_SCAN(priv, "Completed scan.\n"); |
121 | ||
122 | cancel_delayed_work(&priv->scan_check); | |
123 | ||
83626404 | 124 | aborted = test_and_clear_bit(STATUS_SCAN_ABORTING, &priv->status); |
a2fa2462 JB |
125 | if (aborted) |
126 | IWL_DEBUG_SCAN(priv, "Aborted scan completed.\n"); | |
127 | ||
83626404 | 128 | if (!test_and_clear_bit(STATUS_SCANNING, &priv->status)) { |
a2fa2462 JB |
129 | IWL_DEBUG_SCAN(priv, "Scan already completed.\n"); |
130 | goto out_settings; | |
131 | } | |
132 | ||
622a9268 IP |
133 | if (priv->scan_type == IWL_SCAN_ROC) |
134 | iwl_scan_roc_expired(priv); | |
a2fa2462 JB |
135 | |
136 | if (priv->scan_type != IWL_SCAN_NORMAL && !aborted) { | |
137 | int err; | |
138 | ||
139 | /* Check if mac80211 requested scan during our internal scan */ | |
140 | if (priv->scan_request == NULL) | |
141 | goto out_complete; | |
142 | ||
143 | /* If so request a new scan */ | |
144 | err = iwl_scan_initiate(priv, priv->scan_vif, IWL_SCAN_NORMAL, | |
145 | priv->scan_request->channels[0]->band); | |
146 | if (err) { | |
147 | IWL_DEBUG_SCAN(priv, | |
148 | "failed to initiate pending scan: %d\n", err); | |
149 | aborted = true; | |
150 | goto out_complete; | |
151 | } | |
152 | ||
153 | return; | |
154 | } | |
155 | ||
156 | out_complete: | |
157 | iwl_complete_scan(priv, aborted); | |
158 | ||
159 | out_settings: | |
160 | /* Can we still talk to firmware ? */ | |
83626404 | 161 | if (!iwl_is_ready_rf(priv)) |
a2fa2462 JB |
162 | return; |
163 | ||
164 | iwlagn_post_scan(priv); | |
165 | } | |
166 | ||
e7e16b90 | 167 | void iwl_force_scan_end(struct iwl_priv *priv) |
f5354c17 | 168 | { |
b1eea297 | 169 | lockdep_assert_held(&priv->mutex); |
e7e16b90 | 170 | |
83626404 | 171 | if (!test_bit(STATUS_SCANNING, &priv->status)) { |
e7e16b90 SG |
172 | IWL_DEBUG_SCAN(priv, "Forcing scan end while not scanning\n"); |
173 | return; | |
174 | } | |
175 | ||
f5354c17 | 176 | IWL_DEBUG_SCAN(priv, "Forcing scan end\n"); |
83626404 DF |
177 | clear_bit(STATUS_SCANNING, &priv->status); |
178 | clear_bit(STATUS_SCAN_HW, &priv->status); | |
179 | clear_bit(STATUS_SCAN_ABORTING, &priv->status); | |
180 | clear_bit(STATUS_SCAN_COMPLETE, &priv->status); | |
f5354c17 SG |
181 | iwl_complete_scan(priv, true); |
182 | } | |
183 | ||
cd44600f SG |
184 | static void iwl_do_scan_abort(struct iwl_priv *priv) |
185 | { | |
186 | int ret; | |
2a421b91 | 187 | |
b1eea297 | 188 | lockdep_assert_held(&priv->mutex); |
cd44600f | 189 | |
83626404 | 190 | if (!test_bit(STATUS_SCANNING, &priv->status)) { |
cd44600f SG |
191 | IWL_DEBUG_SCAN(priv, "Not performing scan to abort\n"); |
192 | return; | |
2a421b91 TW |
193 | } |
194 | ||
83626404 | 195 | if (test_and_set_bit(STATUS_SCAN_ABORTING, &priv->status)) { |
cd44600f SG |
196 | IWL_DEBUG_SCAN(priv, "Scan abort in progress\n"); |
197 | return; | |
198 | } | |
199 | ||
200 | ret = iwl_send_scan_abort(priv); | |
201 | if (ret) { | |
202 | IWL_DEBUG_SCAN(priv, "Send scan abort failed %d\n", ret); | |
f5354c17 | 203 | iwl_force_scan_end(priv); |
cd44600f | 204 | } else |
25985edc | 205 | IWL_DEBUG_SCAN(priv, "Successfully send scan abort\n"); |
cd44600f SG |
206 | } |
207 | ||
208 | /** | |
209 | * iwl_scan_cancel - Cancel any currently executing HW scan | |
210 | */ | |
211 | int iwl_scan_cancel(struct iwl_priv *priv) | |
212 | { | |
213 | IWL_DEBUG_SCAN(priv, "Queuing abort scan\n"); | |
1ee158d8 | 214 | queue_work(priv->workqueue, &priv->abort_scan); |
2a421b91 TW |
215 | return 0; |
216 | } | |
cd44600f | 217 | |
2a421b91 TW |
218 | /** |
219 | * iwl_scan_cancel_timeout - Cancel any currently executing HW scan | |
220 | * @ms: amount of time to wait (in milliseconds) for scan to abort | |
221 | * | |
2a421b91 | 222 | */ |
98efb4a5 | 223 | void iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms) |
2a421b91 | 224 | { |
e693a802 SG |
225 | unsigned long timeout = jiffies + msecs_to_jiffies(ms); |
226 | ||
b1eea297 | 227 | lockdep_assert_held(&priv->mutex); |
2a421b91 | 228 | |
e693a802 | 229 | IWL_DEBUG_SCAN(priv, "Scan cancel timeout\n"); |
2a421b91 | 230 | |
e693a802 SG |
231 | iwl_do_scan_abort(priv); |
232 | ||
233 | while (time_before_eq(jiffies, timeout)) { | |
83626404 | 234 | if (!test_bit(STATUS_SCAN_HW, &priv->status)) |
84b1bec6 | 235 | goto finished; |
e693a802 | 236 | msleep(20); |
2a421b91 | 237 | } |
84b1bec6 JB |
238 | |
239 | return; | |
240 | ||
241 | finished: | |
242 | /* | |
243 | * Now STATUS_SCAN_HW is clear. This means that the | |
244 | * device finished, but the background work is going | |
245 | * to execute at best as soon as we release the mutex. | |
246 | * Since we need to be able to issue a new scan right | |
247 | * after this function returns, run the complete here. | |
248 | * The STATUS_SCAN_COMPLETE bit will then be cleared | |
249 | * and prevent the background work from "completing" | |
250 | * a possible new scan. | |
251 | */ | |
252 | iwl_process_scan_complete(priv); | |
2a421b91 | 253 | } |
2a421b91 | 254 | |
2a421b91 | 255 | /* Service response to REPLY_SCAN_CMD (0x80) */ |
247c61d6 | 256 | static int iwl_rx_reply_scan(struct iwl_priv *priv, |
48a2d66f | 257 | struct iwl_rx_cmd_buffer *rxb, |
247c61d6 | 258 | struct iwl_device_cmd *cmd) |
2a421b91 TW |
259 | { |
260 | #ifdef CONFIG_IWLWIFI_DEBUG | |
2f301227 | 261 | struct iwl_rx_packet *pkt = rxb_addr(rxb); |
f8d7c1a1 | 262 | struct iwl_scanreq_notification *notif = (void *)pkt->data; |
2a421b91 | 263 | |
7cf24421 | 264 | IWL_DEBUG_SCAN(priv, "Scan request status = 0x%x\n", notif->status); |
2a421b91 | 265 | #endif |
247c61d6 | 266 | return 0; |
2a421b91 TW |
267 | } |
268 | ||
269 | /* Service SCAN_START_NOTIFICATION (0x82) */ | |
247c61d6 | 270 | static int iwl_rx_scan_start_notif(struct iwl_priv *priv, |
48a2d66f | 271 | struct iwl_rx_cmd_buffer *rxb, |
247c61d6 | 272 | struct iwl_device_cmd *cmd) |
2a421b91 | 273 | { |
2f301227 | 274 | struct iwl_rx_packet *pkt = rxb_addr(rxb); |
f8d7c1a1 JB |
275 | struct iwl_scanstart_notification *notif = (void *)pkt->data; |
276 | ||
2a421b91 | 277 | priv->scan_start_tsf = le32_to_cpu(notif->tsf_low); |
e1623446 | 278 | IWL_DEBUG_SCAN(priv, "Scan start: " |
2a421b91 TW |
279 | "%d [802.11%s] " |
280 | "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n", | |
281 | notif->channel, | |
282 | notif->band ? "bg" : "a", | |
fe905f1d TW |
283 | le32_to_cpu(notif->tsf_high), |
284 | le32_to_cpu(notif->tsf_low), | |
285 | notif->status, notif->beacon_timer); | |
c6baf7fb | 286 | |
390808db JB |
287 | if (priv->scan_type == IWL_SCAN_ROC && |
288 | !priv->hw_roc_start_notified) { | |
c6baf7fb | 289 | ieee80211_ready_on_channel(priv->hw); |
390808db JB |
290 | priv->hw_roc_start_notified = true; |
291 | } | |
247c61d6 EG |
292 | |
293 | return 0; | |
2a421b91 TW |
294 | } |
295 | ||
296 | /* Service SCAN_RESULTS_NOTIFICATION (0x83) */ | |
247c61d6 | 297 | static int iwl_rx_scan_results_notif(struct iwl_priv *priv, |
48a2d66f | 298 | struct iwl_rx_cmd_buffer *rxb, |
247c61d6 | 299 | struct iwl_device_cmd *cmd) |
2a421b91 TW |
300 | { |
301 | #ifdef CONFIG_IWLWIFI_DEBUG | |
2f301227 | 302 | struct iwl_rx_packet *pkt = rxb_addr(rxb); |
f8d7c1a1 | 303 | struct iwl_scanresults_notification *notif = (void *)pkt->data; |
2a421b91 | 304 | |
e1623446 | 305 | IWL_DEBUG_SCAN(priv, "Scan ch.res: " |
2a421b91 | 306 | "%d [802.11%s] " |
1895b36b | 307 | "probe status: %u:%u " |
2a421b91 | 308 | "(TSF: 0x%08X:%08X) - %d " |
220575f7 | 309 | "elapsed=%lu usec\n", |
2a421b91 TW |
310 | notif->channel, |
311 | notif->band ? "bg" : "a", | |
1895b36b | 312 | notif->probe_status, notif->num_probe_not_sent, |
2a421b91 TW |
313 | le32_to_cpu(notif->tsf_high), |
314 | le32_to_cpu(notif->tsf_low), | |
315 | le32_to_cpu(notif->statistics[0]), | |
220575f7 | 316 | le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf); |
2a421b91 | 317 | #endif |
247c61d6 | 318 | return 0; |
2a421b91 TW |
319 | } |
320 | ||
321 | /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */ | |
247c61d6 | 322 | static int iwl_rx_scan_complete_notif(struct iwl_priv *priv, |
48a2d66f | 323 | struct iwl_rx_cmd_buffer *rxb, |
247c61d6 | 324 | struct iwl_device_cmd *cmd) |
2a421b91 | 325 | { |
2f301227 | 326 | struct iwl_rx_packet *pkt = rxb_addr(rxb); |
f8d7c1a1 | 327 | struct iwl_scancomplete_notification *scan_notif = (void *)pkt->data; |
2a421b91 | 328 | |
e1623446 | 329 | IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n", |
2a421b91 TW |
330 | scan_notif->scanned_channels, |
331 | scan_notif->tsf_low, | |
332 | scan_notif->tsf_high, scan_notif->status); | |
333 | ||
7cf24421 | 334 | IWL_DEBUG_SCAN(priv, "Scan on %sGHz took %dms\n", |
00700ee0 | 335 | (priv->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2", |
ef1b21f7 | 336 | jiffies_to_msecs(jiffies - priv->scan_start)); |
2a421b91 | 337 | |
84b1bec6 JB |
338 | /* |
339 | * When aborting, we run the scan completed background work inline | |
340 | * and the background work must then do nothing. The SCAN_COMPLETE | |
341 | * bit helps implement that logic and thus needs to be set before | |
342 | * queueing the work. Also, since the scan abort waits for SCAN_HW | |
343 | * to clear, we need to set SCAN_COMPLETE before clearing SCAN_HW | |
344 | * to avoid a race there. | |
345 | */ | |
83626404 DF |
346 | set_bit(STATUS_SCAN_COMPLETE, &priv->status); |
347 | clear_bit(STATUS_SCAN_HW, &priv->status); | |
1ee158d8 | 348 | queue_work(priv->workqueue, &priv->scan_completed); |
2a421b91 | 349 | |
f78e5454 | 350 | if (priv->iw_mode != NL80211_IFTYPE_ADHOC && |
88e58fc5 | 351 | iwl_advanced_bt_coexist(priv) && |
d5926d9d | 352 | priv->bt_status != scan_notif->bt_status) { |
f78e5454 WYG |
353 | if (scan_notif->bt_status) { |
354 | /* BT on */ | |
355 | if (!priv->bt_ch_announce) | |
356 | priv->bt_traffic_load = | |
357 | IWL_BT_COEX_TRAFFIC_LOAD_HIGH; | |
358 | /* | |
359 | * otherwise, no traffic load information provided | |
360 | * no changes made | |
361 | */ | |
362 | } else { | |
363 | /* BT off */ | |
364 | priv->bt_traffic_load = | |
365 | IWL_BT_COEX_TRAFFIC_LOAD_NONE; | |
366 | } | |
367 | priv->bt_status = scan_notif->bt_status; | |
1ee158d8 | 368 | queue_work(priv->workqueue, |
74e28e44 | 369 | &priv->bt_traffic_change_work); |
f78e5454 | 370 | } |
247c61d6 | 371 | return 0; |
2a421b91 TW |
372 | } |
373 | ||
374 | void iwl_setup_rx_scan_handlers(struct iwl_priv *priv) | |
375 | { | |
376 | /* scan handlers */ | |
377 | priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan; | |
378 | priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif; | |
379 | priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] = | |
380 | iwl_rx_scan_results_notif; | |
381 | priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] = | |
382 | iwl_rx_scan_complete_notif; | |
383 | } | |
2a421b91 | 384 | |
6e809a16 JB |
385 | static u16 iwl_get_active_dwell_time(struct iwl_priv *priv, |
386 | enum ieee80211_band band, u8 n_probes) | |
2a421b91 TW |
387 | { |
388 | if (band == IEEE80211_BAND_5GHZ) | |
fe905f1d TW |
389 | return IWL_ACTIVE_DWELL_TIME_52 + |
390 | IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1); | |
2a421b91 | 391 | else |
fe905f1d TW |
392 | return IWL_ACTIVE_DWELL_TIME_24 + |
393 | IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1); | |
2a421b91 TW |
394 | } |
395 | ||
390808db JB |
396 | static u16 iwl_limit_dwell(struct iwl_priv *priv, u16 dwell_time) |
397 | { | |
398 | struct iwl_rxon_context *ctx; | |
b3a6d2e0 JB |
399 | int limits[NUM_IWL_RXON_CTX] = {}; |
400 | int n_active = 0; | |
401 | u16 limit; | |
402 | ||
403 | BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2); | |
390808db JB |
404 | |
405 | /* | |
406 | * If we're associated, we clamp the dwell time 98% | |
b3a6d2e0 JB |
407 | * of the beacon interval (minus 2 * channel tune time) |
408 | * If both contexts are active, we have to restrict to | |
409 | * 1/2 of the minimum of them, because they might be in | |
410 | * lock-step with the time inbetween only half of what | |
411 | * time we'd have in each of them. | |
390808db JB |
412 | */ |
413 | for_each_context(priv, ctx) { | |
2ed81710 JB |
414 | switch (ctx->staging.dev_type) { |
415 | case RXON_DEV_TYPE_P2P: | |
416 | /* no timing constraints */ | |
eb32043f | 417 | continue; |
2ed81710 JB |
418 | case RXON_DEV_TYPE_ESS: |
419 | default: | |
420 | /* timing constraints if associated */ | |
421 | if (!iwl_is_associated_ctx(ctx)) | |
422 | continue; | |
423 | break; | |
424 | case RXON_DEV_TYPE_CP: | |
425 | case RXON_DEV_TYPE_2STA: | |
426 | /* | |
427 | * These seem to always have timers for TBTT | |
428 | * active in uCode even when not associated yet. | |
429 | */ | |
430 | break; | |
431 | } | |
432 | ||
b3a6d2e0 | 433 | limits[n_active++] = ctx->beacon_int ?: IWL_PASSIVE_DWELL_BASE; |
390808db JB |
434 | } |
435 | ||
b3a6d2e0 JB |
436 | switch (n_active) { |
437 | case 0: | |
438 | return dwell_time; | |
439 | case 2: | |
440 | limit = (limits[1] * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2; | |
441 | limit /= 2; | |
442 | dwell_time = min(limit, dwell_time); | |
443 | /* fall through to limit further */ | |
444 | case 1: | |
445 | limit = (limits[0] * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2; | |
446 | limit /= n_active; | |
447 | return min(limit, dwell_time); | |
448 | default: | |
449 | WARN_ON_ONCE(1); | |
450 | return dwell_time; | |
451 | } | |
390808db JB |
452 | } |
453 | ||
6e809a16 JB |
454 | static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv, |
455 | enum ieee80211_band band) | |
2a421b91 | 456 | { |
fe905f1d | 457 | u16 passive = (band == IEEE80211_BAND_2GHZ) ? |
2a421b91 TW |
458 | IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 : |
459 | IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52; | |
460 | ||
390808db | 461 | return iwl_limit_dwell(priv, passive); |
2a421b91 TW |
462 | } |
463 | ||
c736f235 MV |
464 | /* Return valid, unused, channel for a passive scan to reset the RF */ |
465 | static u8 iwl_get_single_channel_number(struct iwl_priv *priv, | |
d9b9d940 | 466 | enum ieee80211_band band) |
c736f235 | 467 | { |
d9b9d940 | 468 | struct ieee80211_supported_band *sband = priv->hw->wiphy->bands[band]; |
c736f235 | 469 | struct iwl_rxon_context *ctx; |
d9b9d940 | 470 | int i; |
c736f235 | 471 | |
d9b9d940 | 472 | for (i = 0; i < sband->n_channels; i++) { |
c736f235 MV |
473 | bool busy = false; |
474 | ||
475 | for_each_context(priv, ctx) { | |
d9b9d940 | 476 | busy = sband->channels[i].hw_value == |
c736f235 MV |
477 | le16_to_cpu(ctx->staging.channel); |
478 | if (busy) | |
479 | break; | |
480 | } | |
481 | ||
482 | if (busy) | |
483 | continue; | |
484 | ||
d9b9d940 JB |
485 | if (!(sband->channels[i].flags & IEEE80211_CHAN_DISABLED)) |
486 | return sband->channels[i].hw_value; | |
c736f235 MV |
487 | } |
488 | ||
d9b9d940 | 489 | return 0; |
c736f235 MV |
490 | } |
491 | ||
0d4e0772 JB |
492 | static int iwl_get_channel_for_reset_scan(struct iwl_priv *priv, |
493 | struct ieee80211_vif *vif, | |
494 | enum ieee80211_band band, | |
495 | struct iwl_scan_channel *scan_ch) | |
6e809a16 JB |
496 | { |
497 | const struct ieee80211_supported_band *sband; | |
0d4e0772 | 498 | u16 channel; |
6e809a16 JB |
499 | |
500 | sband = iwl_get_hw_mode(priv, band); | |
501 | if (!sband) { | |
502 | IWL_ERR(priv, "invalid band\n"); | |
0d4e0772 | 503 | return 0; |
6e809a16 JB |
504 | } |
505 | ||
6e809a16 JB |
506 | channel = iwl_get_single_channel_number(priv, band); |
507 | if (channel) { | |
508 | scan_ch->channel = cpu_to_le16(channel); | |
509 | scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE; | |
0d4e0772 JB |
510 | scan_ch->active_dwell = |
511 | cpu_to_le16(IWL_RADIO_RESET_DWELL_TIME); | |
512 | scan_ch->passive_dwell = | |
513 | cpu_to_le16(IWL_RADIO_RESET_DWELL_TIME); | |
6e809a16 JB |
514 | /* Set txpower levels to defaults */ |
515 | scan_ch->dsp_atten = 110; | |
516 | if (band == IEEE80211_BAND_5GHZ) | |
517 | scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3; | |
518 | else | |
519 | scan_ch->tx_gain = ((1 << 5) | (5 << 3)); | |
0d4e0772 JB |
520 | return 1; |
521 | } | |
522 | ||
523 | IWL_ERR(priv, "no valid channel found\n"); | |
524 | return 0; | |
6e809a16 JB |
525 | } |
526 | ||
527 | static int iwl_get_channels_for_scan(struct iwl_priv *priv, | |
528 | struct ieee80211_vif *vif, | |
529 | enum ieee80211_band band, | |
530 | u8 is_active, u8 n_probes, | |
531 | struct iwl_scan_channel *scan_ch) | |
532 | { | |
533 | struct ieee80211_channel *chan; | |
534 | const struct ieee80211_supported_band *sband; | |
6e809a16 JB |
535 | u16 passive_dwell = 0; |
536 | u16 active_dwell = 0; | |
537 | int added, i; | |
538 | u16 channel; | |
539 | ||
540 | sband = iwl_get_hw_mode(priv, band); | |
541 | if (!sband) | |
542 | return 0; | |
543 | ||
544 | active_dwell = iwl_get_active_dwell_time(priv, band, n_probes); | |
545 | passive_dwell = iwl_get_passive_dwell_time(priv, band); | |
546 | ||
547 | if (passive_dwell <= active_dwell) | |
548 | passive_dwell = active_dwell + 1; | |
549 | ||
550 | for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) { | |
551 | chan = priv->scan_request->channels[i]; | |
552 | ||
553 | if (chan->band != band) | |
554 | continue; | |
555 | ||
556 | channel = chan->hw_value; | |
557 | scan_ch->channel = cpu_to_le16(channel); | |
558 | ||
9dc216d7 | 559 | if (!is_active || (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)) |
6e809a16 JB |
560 | scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE; |
561 | else | |
562 | scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE; | |
563 | ||
564 | if (n_probes) | |
565 | scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes); | |
566 | ||
567 | scan_ch->active_dwell = cpu_to_le16(active_dwell); | |
568 | scan_ch->passive_dwell = cpu_to_le16(passive_dwell); | |
569 | ||
570 | /* Set txpower levels to defaults */ | |
571 | scan_ch->dsp_atten = 110; | |
572 | ||
573 | /* NOTE: if we were doing 6Mb OFDM for scans we'd use | |
574 | * power level: | |
575 | * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3; | |
576 | */ | |
577 | if (band == IEEE80211_BAND_5GHZ) | |
578 | scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3; | |
579 | else | |
580 | scan_ch->tx_gain = ((1 << 5) | (5 << 3)); | |
581 | ||
582 | IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n", | |
583 | channel, le32_to_cpu(scan_ch->type), | |
584 | (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ? | |
585 | "ACTIVE" : "PASSIVE", | |
586 | (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ? | |
587 | active_dwell : passive_dwell); | |
588 | ||
589 | scan_ch++; | |
590 | added++; | |
591 | } | |
592 | ||
593 | IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added); | |
594 | return added; | |
595 | } | |
596 | ||
b443d8d8 JB |
597 | /** |
598 | * iwl_fill_probe_req - fill in all required fields and IE for probe request | |
599 | */ | |
600 | ||
601 | static u16 iwl_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta, | |
4f9bfbb1 DS |
602 | const u8 *ies, int ie_len, const u8 *ssid, |
603 | u8 ssid_len, int left) | |
b443d8d8 JB |
604 | { |
605 | int len = 0; | |
606 | u8 *pos = NULL; | |
607 | ||
608 | /* Make sure there is enough space for the probe request, | |
609 | * two mandatory IEs and the data */ | |
610 | left -= 24; | |
611 | if (left < 0) | |
612 | return 0; | |
613 | ||
614 | frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ); | |
ca42613a | 615 | eth_broadcast_addr(frame->da); |
b443d8d8 | 616 | memcpy(frame->sa, ta, ETH_ALEN); |
ca42613a | 617 | eth_broadcast_addr(frame->bssid); |
b443d8d8 JB |
618 | frame->seq_ctrl = 0; |
619 | ||
620 | len += 24; | |
621 | ||
622 | /* ...next IE... */ | |
623 | pos = &frame->u.probe_req.variable[0]; | |
624 | ||
4f9bfbb1 DS |
625 | /* fill in our SSID IE */ |
626 | left -= ssid_len + 2; | |
b443d8d8 JB |
627 | if (left < 0) |
628 | return 0; | |
629 | *pos++ = WLAN_EID_SSID; | |
4f9bfbb1 DS |
630 | *pos++ = ssid_len; |
631 | if (ssid && ssid_len) { | |
632 | memcpy(pos, ssid, ssid_len); | |
633 | pos += ssid_len; | |
634 | } | |
b443d8d8 | 635 | |
4f9bfbb1 | 636 | len += ssid_len + 2; |
b443d8d8 JB |
637 | |
638 | if (WARN_ON(left < ie_len)) | |
639 | return len; | |
640 | ||
641 | if (ies && ie_len) { | |
642 | memcpy(pos, ies, ie_len); | |
643 | len += ie_len; | |
644 | } | |
645 | ||
646 | return (u16)len; | |
647 | } | |
648 | ||
6e809a16 JB |
649 | static int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif) |
650 | { | |
651 | struct iwl_host_cmd cmd = { | |
652 | .id = REPLY_SCAN_CMD, | |
653 | .len = { sizeof(struct iwl_scan_cmd), }, | |
654 | .flags = CMD_SYNC, | |
655 | }; | |
656 | struct iwl_scan_cmd *scan; | |
657 | struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS]; | |
658 | u32 rate_flags = 0; | |
331d9301 | 659 | u16 cmd_len = 0; |
6e809a16 JB |
660 | u16 rx_chain = 0; |
661 | enum ieee80211_band band; | |
662 | u8 n_probes = 0; | |
b7998c8b | 663 | u8 rx_ant = priv->nvm_data->valid_rx_ant; |
6e809a16 JB |
664 | u8 rate; |
665 | bool is_active = false; | |
666 | int chan_mod; | |
667 | u8 active_chains; | |
b7998c8b | 668 | u8 scan_tx_antennas = priv->nvm_data->valid_tx_ant; |
6e809a16 | 669 | int ret; |
0ed46287 DS |
670 | int scan_cmd_size = sizeof(struct iwl_scan_cmd) + |
671 | MAX_SCAN_CHANNEL * sizeof(struct iwl_scan_channel) + | |
672 | priv->fw->ucode_capa.max_probe_length; | |
4f9bfbb1 DS |
673 | const u8 *ssid = NULL; |
674 | u8 ssid_len = 0; | |
0ed46287 | 675 | |
8907a6fb TG |
676 | if (WARN_ON(priv->scan_type == IWL_SCAN_NORMAL && |
677 | (!priv->scan_request || | |
678 | priv->scan_request->n_channels > MAX_SCAN_CHANNEL))) | |
0ed46287 | 679 | return -EINVAL; |
6e809a16 | 680 | |
b1eea297 | 681 | lockdep_assert_held(&priv->mutex); |
6e809a16 JB |
682 | |
683 | if (vif) | |
684 | ctx = iwl_rxon_ctx_from_vif(vif); | |
685 | ||
686 | if (!priv->scan_cmd) { | |
0ed46287 | 687 | priv->scan_cmd = kmalloc(scan_cmd_size, GFP_KERNEL); |
6e809a16 JB |
688 | if (!priv->scan_cmd) { |
689 | IWL_DEBUG_SCAN(priv, | |
690 | "fail to allocate memory for scan\n"); | |
691 | return -ENOMEM; | |
692 | } | |
693 | } | |
694 | scan = priv->scan_cmd; | |
0ed46287 | 695 | memset(scan, 0, scan_cmd_size); |
6e809a16 JB |
696 | |
697 | scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH; | |
698 | scan->quiet_time = IWL_ACTIVE_QUIET_TIME; | |
699 | ||
700 | if (priv->scan_type != IWL_SCAN_ROC && | |
701 | iwl_is_any_associated(priv)) { | |
702 | u16 interval = 0; | |
703 | u32 extra; | |
704 | u32 suspend_time = 100; | |
705 | u32 scan_suspend_time = 100; | |
706 | ||
707 | IWL_DEBUG_INFO(priv, "Scanning while associated...\n"); | |
708 | switch (priv->scan_type) { | |
709 | case IWL_SCAN_ROC: | |
710 | WARN_ON(1); | |
711 | break; | |
712 | case IWL_SCAN_RADIO_RESET: | |
713 | interval = 0; | |
714 | break; | |
715 | case IWL_SCAN_NORMAL: | |
716 | interval = vif->bss_conf.beacon_int; | |
717 | break; | |
718 | } | |
719 | ||
720 | scan->suspend_time = 0; | |
721 | scan->max_out_time = cpu_to_le32(200 * 1024); | |
722 | if (!interval) | |
723 | interval = suspend_time; | |
724 | ||
725 | extra = (suspend_time / interval) << 22; | |
726 | scan_suspend_time = (extra | | |
727 | ((suspend_time % interval) * 1024)); | |
728 | scan->suspend_time = cpu_to_le32(scan_suspend_time); | |
729 | IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n", | |
730 | scan_suspend_time, interval); | |
731 | } else if (priv->scan_type == IWL_SCAN_ROC) { | |
732 | scan->suspend_time = 0; | |
733 | scan->max_out_time = 0; | |
734 | scan->quiet_time = 0; | |
735 | scan->quiet_plcp_th = 0; | |
736 | } | |
737 | ||
738 | switch (priv->scan_type) { | |
739 | case IWL_SCAN_RADIO_RESET: | |
740 | IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n"); | |
3f8a9e76 JB |
741 | /* |
742 | * Override quiet time as firmware checks that active | |
743 | * dwell is >= quiet; since we use passive scan it'll | |
744 | * not actually be used. | |
745 | */ | |
746 | scan->quiet_time = cpu_to_le16(IWL_RADIO_RESET_DWELL_TIME); | |
6e809a16 JB |
747 | break; |
748 | case IWL_SCAN_NORMAL: | |
749 | if (priv->scan_request->n_ssids) { | |
750 | int i, p = 0; | |
751 | IWL_DEBUG_SCAN(priv, "Kicking off active scan\n"); | |
4f9bfbb1 DS |
752 | /* |
753 | * The highest priority SSID is inserted to the | |
754 | * probe request template. | |
755 | */ | |
756 | ssid_len = priv->scan_request->ssids[0].ssid_len; | |
757 | ssid = priv->scan_request->ssids[0].ssid; | |
758 | ||
65781af6 DS |
759 | /* |
760 | * Invert the order of ssids, the firmware will invert | |
761 | * it back. | |
762 | */ | |
763 | for (i = priv->scan_request->n_ssids - 1; i >= 1; i--) { | |
6e809a16 JB |
764 | scan->direct_scan[p].id = WLAN_EID_SSID; |
765 | scan->direct_scan[p].len = | |
766 | priv->scan_request->ssids[i].ssid_len; | |
767 | memcpy(scan->direct_scan[p].ssid, | |
768 | priv->scan_request->ssids[i].ssid, | |
769 | priv->scan_request->ssids[i].ssid_len); | |
770 | n_probes++; | |
771 | p++; | |
772 | } | |
773 | is_active = true; | |
774 | } else | |
775 | IWL_DEBUG_SCAN(priv, "Start passive scan.\n"); | |
776 | break; | |
777 | case IWL_SCAN_ROC: | |
778 | IWL_DEBUG_SCAN(priv, "Start ROC scan.\n"); | |
779 | break; | |
780 | } | |
781 | ||
782 | scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK; | |
783 | scan->tx_cmd.sta_id = ctx->bcast_sta_id; | |
784 | scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE; | |
785 | ||
786 | switch (priv->scan_band) { | |
787 | case IEEE80211_BAND_2GHZ: | |
788 | scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK; | |
789 | chan_mod = le32_to_cpu( | |
790 | priv->contexts[IWL_RXON_CTX_BSS].active.flags & | |
791 | RXON_FLG_CHANNEL_MODE_MSK) | |
792 | >> RXON_FLG_CHANNEL_MODE_POS; | |
3a8aea09 JB |
793 | if ((priv->scan_request && priv->scan_request->no_cck) || |
794 | chan_mod == CHANNEL_MODE_PURE_40) { | |
6e809a16 JB |
795 | rate = IWL_RATE_6M_PLCP; |
796 | } else { | |
797 | rate = IWL_RATE_1M_PLCP; | |
798 | rate_flags = RATE_MCS_CCK_MSK; | |
799 | } | |
800 | /* | |
801 | * Internal scans are passive, so we can indiscriminately set | |
802 | * the BT ignore flag on 2.4 GHz since it applies to TX only. | |
803 | */ | |
2152268f EG |
804 | if (priv->cfg->bt_params && |
805 | priv->cfg->bt_params->advanced_bt_coexist) | |
6e809a16 JB |
806 | scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT; |
807 | break; | |
808 | case IEEE80211_BAND_5GHZ: | |
809 | rate = IWL_RATE_6M_PLCP; | |
810 | break; | |
811 | default: | |
812 | IWL_WARN(priv, "Invalid scan band\n"); | |
813 | return -EIO; | |
814 | } | |
815 | ||
816 | /* | |
817 | * If active scanning is requested but a certain channel is | |
818 | * marked passive, we can do active scanning if we detect | |
819 | * transmissions. | |
820 | * | |
821 | * There is an issue with some firmware versions that triggers | |
822 | * a sysassert on a "good CRC threshold" of zero (== disabled), | |
823 | * on a radar channel even though this means that we should NOT | |
824 | * send probes. | |
825 | * | |
826 | * The "good CRC threshold" is the number of frames that we | |
827 | * need to receive during our dwell time on a channel before | |
828 | * sending out probes -- setting this to a huge value will | |
829 | * mean we never reach it, but at the same time work around | |
830 | * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER | |
831 | * here instead of IWL_GOOD_CRC_TH_DISABLED. | |
832 | * | |
833 | * This was fixed in later versions along with some other | |
834 | * scan changes, and the threshold behaves as a flag in those | |
835 | * versions. | |
836 | */ | |
837 | if (priv->new_scan_threshold_behaviour) | |
838 | scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT : | |
839 | IWL_GOOD_CRC_TH_DISABLED; | |
840 | else | |
841 | scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT : | |
842 | IWL_GOOD_CRC_TH_NEVER; | |
843 | ||
844 | band = priv->scan_band; | |
845 | ||
6e809a16 | 846 | if (band == IEEE80211_BAND_2GHZ && |
2152268f EG |
847 | priv->cfg->bt_params && |
848 | priv->cfg->bt_params->advanced_bt_coexist) { | |
6e809a16 JB |
849 | /* transmit 2.4 GHz probes only on first antenna */ |
850 | scan_tx_antennas = first_antenna(scan_tx_antennas); | |
851 | } | |
852 | ||
853 | priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv, | |
854 | priv->scan_tx_ant[band], | |
855 | scan_tx_antennas); | |
856 | rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]); | |
857 | scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags); | |
858 | ||
e5610382 JB |
859 | /* |
860 | * In power save mode while associated use one chain, | |
861 | * otherwise use all chains | |
862 | */ | |
47107e84 | 863 | if (test_bit(STATUS_POWER_PMI, &priv->status) && |
e5610382 | 864 | !(priv->hw->conf.flags & IEEE80211_CONF_IDLE)) { |
6e809a16 JB |
865 | /* rx_ant has been set to all valid chains previously */ |
866 | active_chains = rx_ant & | |
867 | ((u8)(priv->chain_noise_data.active_chains)); | |
868 | if (!active_chains) | |
869 | active_chains = rx_ant; | |
870 | ||
871 | IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n", | |
872 | priv->chain_noise_data.active_chains); | |
873 | ||
874 | rx_ant = first_antenna(active_chains); | |
875 | } | |
2152268f EG |
876 | if (priv->cfg->bt_params && |
877 | priv->cfg->bt_params->advanced_bt_coexist && | |
6e809a16 JB |
878 | priv->bt_full_concurrent) { |
879 | /* operated as 1x1 in full concurrency mode */ | |
880 | rx_ant = first_antenna(rx_ant); | |
881 | } | |
882 | ||
883 | /* MIMO is not used here, but value is required */ | |
884 | rx_chain |= | |
b7998c8b | 885 | priv->nvm_data->valid_rx_ant << RXON_RX_CHAIN_VALID_POS; |
6e809a16 JB |
886 | rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS; |
887 | rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS; | |
888 | rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS; | |
889 | scan->rx_chain = cpu_to_le16(rx_chain); | |
890 | switch (priv->scan_type) { | |
891 | case IWL_SCAN_NORMAL: | |
b443d8d8 | 892 | cmd_len = iwl_fill_probe_req( |
6e809a16 JB |
893 | (struct ieee80211_mgmt *)scan->data, |
894 | vif->addr, | |
895 | priv->scan_request->ie, | |
896 | priv->scan_request->ie_len, | |
4f9bfbb1 | 897 | ssid, ssid_len, |
0ed46287 | 898 | scan_cmd_size - sizeof(*scan)); |
6e809a16 JB |
899 | break; |
900 | case IWL_SCAN_RADIO_RESET: | |
901 | case IWL_SCAN_ROC: | |
902 | /* use bcast addr, will not be transmitted but must be valid */ | |
b443d8d8 | 903 | cmd_len = iwl_fill_probe_req( |
6e809a16 JB |
904 | (struct ieee80211_mgmt *)scan->data, |
905 | iwl_bcast_addr, NULL, 0, | |
4f9bfbb1 | 906 | NULL, 0, |
0ed46287 | 907 | scan_cmd_size - sizeof(*scan)); |
6e809a16 JB |
908 | break; |
909 | default: | |
910 | BUG(); | |
911 | } | |
912 | scan->tx_cmd.len = cpu_to_le16(cmd_len); | |
913 | ||
914 | scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK | | |
915 | RXON_FILTER_BCON_AWARE_MSK); | |
916 | ||
917 | switch (priv->scan_type) { | |
918 | case IWL_SCAN_RADIO_RESET: | |
919 | scan->channel_count = | |
0d4e0772 | 920 | iwl_get_channel_for_reset_scan(priv, vif, band, |
6e809a16 JB |
921 | (void *)&scan->data[cmd_len]); |
922 | break; | |
923 | case IWL_SCAN_NORMAL: | |
924 | scan->channel_count = | |
925 | iwl_get_channels_for_scan(priv, vif, band, | |
926 | is_active, n_probes, | |
927 | (void *)&scan->data[cmd_len]); | |
928 | break; | |
929 | case IWL_SCAN_ROC: { | |
930 | struct iwl_scan_channel *scan_ch; | |
390808db JB |
931 | int n_chan, i; |
932 | u16 dwell; | |
933 | ||
934 | dwell = iwl_limit_dwell(priv, priv->hw_roc_duration); | |
935 | n_chan = DIV_ROUND_UP(priv->hw_roc_duration, dwell); | |
6e809a16 | 936 | |
390808db | 937 | scan->channel_count = n_chan; |
6e809a16 JB |
938 | |
939 | scan_ch = (void *)&scan->data[cmd_len]; | |
6e809a16 | 940 | |
390808db JB |
941 | for (i = 0; i < n_chan; i++) { |
942 | scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE; | |
943 | scan_ch->channel = | |
944 | cpu_to_le16(priv->hw_roc_channel->hw_value); | |
6e809a16 | 945 | |
390808db JB |
946 | if (i == n_chan - 1) |
947 | dwell = priv->hw_roc_duration - i * dwell; | |
948 | ||
949 | scan_ch->active_dwell = | |
950 | scan_ch->passive_dwell = cpu_to_le16(dwell); | |
951 | ||
952 | /* Set txpower levels to defaults */ | |
953 | scan_ch->dsp_atten = 110; | |
954 | ||
955 | /* NOTE: if we were doing 6Mb OFDM for scans we'd use | |
956 | * power level: | |
957 | * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3; | |
958 | */ | |
959 | if (priv->hw_roc_channel->band == IEEE80211_BAND_5GHZ) | |
960 | scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3; | |
961 | else | |
962 | scan_ch->tx_gain = ((1 << 5) | (5 << 3)); | |
963 | ||
964 | scan_ch++; | |
6e809a16 | 965 | } |
390808db JB |
966 | } |
967 | ||
6e809a16 JB |
968 | break; |
969 | } | |
970 | ||
971 | if (scan->channel_count == 0) { | |
972 | IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count); | |
973 | return -EIO; | |
974 | } | |
975 | ||
976 | cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) + | |
977 | scan->channel_count * sizeof(struct iwl_scan_channel); | |
978 | cmd.data[0] = scan; | |
979 | cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY; | |
980 | scan->len = cpu_to_le16(cmd.len[0]); | |
981 | ||
982 | /* set scan bit here for PAN params */ | |
83626404 | 983 | set_bit(STATUS_SCAN_HW, &priv->status); |
6e809a16 JB |
984 | |
985 | ret = iwlagn_set_pan_params(priv); | |
76a3aa89 IP |
986 | if (ret) { |
987 | clear_bit(STATUS_SCAN_HW, &priv->status); | |
6e809a16 | 988 | return ret; |
76a3aa89 | 989 | } |
6e809a16 | 990 | |
e10a0533 | 991 | ret = iwl_dvm_send_cmd(priv, &cmd); |
6e809a16 | 992 | if (ret) { |
83626404 | 993 | clear_bit(STATUS_SCAN_HW, &priv->status); |
6e809a16 JB |
994 | iwlagn_set_pan_params(priv); |
995 | } | |
996 | ||
997 | return ret; | |
998 | } | |
999 | ||
f53696de TW |
1000 | void iwl_init_scan_params(struct iwl_priv *priv) |
1001 | { | |
b7998c8b | 1002 | u8 ant_idx = fls(priv->nvm_data->valid_tx_ant) - 1; |
f53696de | 1003 | if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ]) |
76eff18b | 1004 | priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx; |
f53696de | 1005 | if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ]) |
76eff18b | 1006 | priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx; |
f53696de TW |
1007 | } |
1008 | ||
266af4c7 JB |
1009 | int __must_check iwl_scan_initiate(struct iwl_priv *priv, |
1010 | struct ieee80211_vif *vif, | |
1011 | enum iwl_scan_type scan_type, | |
1012 | enum ieee80211_band band) | |
2a421b91 | 1013 | { |
3eecce52 JB |
1014 | int ret; |
1015 | ||
b1eea297 | 1016 | lockdep_assert_held(&priv->mutex); |
88be0264 | 1017 | |
3eecce52 JB |
1018 | cancel_delayed_work(&priv->scan_check); |
1019 | ||
83626404 | 1020 | if (!iwl_is_ready_rf(priv)) { |
7cf24421 | 1021 | IWL_WARN(priv, "Request scan called when driver not ready.\n"); |
3eecce52 JB |
1022 | return -EIO; |
1023 | } | |
1024 | ||
83626404 | 1025 | if (test_bit(STATUS_SCAN_HW, &priv->status)) { |
7cf24421 | 1026 | IWL_DEBUG_SCAN(priv, |
3eecce52 JB |
1027 | "Multiple concurrent scan requests in parallel.\n"); |
1028 | return -EBUSY; | |
1029 | } | |
1030 | ||
83626404 | 1031 | if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) { |
7cf24421 | 1032 | IWL_DEBUG_SCAN(priv, "Scan request while abort pending.\n"); |
3eecce52 JB |
1033 | return -EBUSY; |
1034 | } | |
1035 | ||
7cf24421 | 1036 | IWL_DEBUG_SCAN(priv, "Starting %sscan...\n", |
266af4c7 | 1037 | scan_type == IWL_SCAN_NORMAL ? "" : |
c6baf7fb | 1038 | scan_type == IWL_SCAN_ROC ? "remain-on-channel " : |
266af4c7 | 1039 | "internal short "); |
3eecce52 | 1040 | |
83626404 | 1041 | set_bit(STATUS_SCANNING, &priv->status); |
266af4c7 | 1042 | priv->scan_type = scan_type; |
2a421b91 | 1043 | priv->scan_start = jiffies; |
3eecce52 | 1044 | priv->scan_band = band; |
2a421b91 | 1045 | |
5c3d29fc | 1046 | ret = iwlagn_request_scan(priv, vif); |
3eecce52 | 1047 | if (ret) { |
83626404 | 1048 | clear_bit(STATUS_SCANNING, &priv->status); |
266af4c7 | 1049 | priv->scan_type = IWL_SCAN_NORMAL; |
3eecce52 JB |
1050 | return ret; |
1051 | } | |
b6e4c55a | 1052 | |
1ee158d8 | 1053 | queue_delayed_work(priv->workqueue, &priv->scan_check, |
3eecce52 | 1054 | IWL_SCAN_CHECK_WATCHDOG); |
2a421b91 TW |
1055 | |
1056 | return 0; | |
1057 | } | |
2a421b91 | 1058 | |
e9dde6f6 | 1059 | |
afbdd69a WYG |
1060 | /* |
1061 | * internal short scan, this function should only been called while associated. | |
1062 | * It will reset and tune the radio to prevent possible RF related problem | |
1063 | */ | |
88be0264 | 1064 | void iwl_internal_short_hw_scan(struct iwl_priv *priv) |
afbdd69a | 1065 | { |
1ee158d8 | 1066 | queue_work(priv->workqueue, &priv->start_internal_scan); |
88be0264 JB |
1067 | } |
1068 | ||
c240879f | 1069 | static void iwl_bg_start_internal_scan(struct work_struct *work) |
88be0264 JB |
1070 | { |
1071 | struct iwl_priv *priv = | |
1072 | container_of(work, struct iwl_priv, start_internal_scan); | |
1073 | ||
7cf24421 SG |
1074 | IWL_DEBUG_SCAN(priv, "Start internal scan\n"); |
1075 | ||
b1eea297 | 1076 | mutex_lock(&priv->mutex); |
afbdd69a | 1077 | |
266af4c7 | 1078 | if (priv->scan_type == IWL_SCAN_RADIO_RESET) { |
073d5eab RC |
1079 | IWL_DEBUG_SCAN(priv, "Internal scan already in progress\n"); |
1080 | goto unlock; | |
1081 | } | |
1082 | ||
83626404 | 1083 | if (test_bit(STATUS_SCANNING, &priv->status)) { |
afbdd69a | 1084 | IWL_DEBUG_SCAN(priv, "Scan already in progress.\n"); |
88be0264 | 1085 | goto unlock; |
afbdd69a | 1086 | } |
88be0264 | 1087 | |
266af4c7 | 1088 | if (iwl_scan_initiate(priv, NULL, IWL_SCAN_RADIO_RESET, priv->band)) |
3eecce52 | 1089 | IWL_DEBUG_SCAN(priv, "failed to start internal short scan\n"); |
88be0264 | 1090 | unlock: |
b1eea297 | 1091 | mutex_unlock(&priv->mutex); |
afbdd69a | 1092 | } |
afbdd69a | 1093 | |
c240879f | 1094 | static void iwl_bg_scan_check(struct work_struct *data) |
2a421b91 TW |
1095 | { |
1096 | struct iwl_priv *priv = | |
1097 | container_of(data, struct iwl_priv, scan_check.work); | |
1098 | ||
7cf24421 SG |
1099 | IWL_DEBUG_SCAN(priv, "Scan check work\n"); |
1100 | ||
e7e16b90 SG |
1101 | /* Since we are here firmware does not finish scan and |
1102 | * most likely is in bad shape, so we don't bother to | |
1103 | * send abort command, just force scan complete to mac80211 */ | |
b1eea297 | 1104 | mutex_lock(&priv->mutex); |
e7e16b90 | 1105 | iwl_force_scan_end(priv); |
b1eea297 | 1106 | mutex_unlock(&priv->mutex); |
2a421b91 | 1107 | } |
77fecfb8 | 1108 | |
c240879f | 1109 | static void iwl_bg_abort_scan(struct work_struct *work) |
2a421b91 TW |
1110 | { |
1111 | struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan); | |
1112 | ||
7cf24421 SG |
1113 | IWL_DEBUG_SCAN(priv, "Abort scan work\n"); |
1114 | ||
e7e16b90 SG |
1115 | /* We keep scan_check work queued in case when firmware will not |
1116 | * report back scan completed notification */ | |
b1eea297 | 1117 | mutex_lock(&priv->mutex); |
6bd1758d | 1118 | iwl_scan_cancel_timeout(priv, 200); |
b1eea297 | 1119 | mutex_unlock(&priv->mutex); |
2a421b91 TW |
1120 | } |
1121 | ||
f253247a JB |
1122 | static void iwl_bg_scan_completed(struct work_struct *work) |
1123 | { | |
1124 | struct iwl_priv *priv = | |
1125 | container_of(work, struct iwl_priv, scan_completed); | |
52a02d15 | 1126 | |
b1eea297 | 1127 | mutex_lock(&priv->mutex); |
f253247a | 1128 | iwl_process_scan_complete(priv); |
b1eea297 | 1129 | mutex_unlock(&priv->mutex); |
eedda367 | 1130 | } |
eedda367 | 1131 | |
2a421b91 TW |
1132 | void iwl_setup_scan_deferred_work(struct iwl_priv *priv) |
1133 | { | |
eedda367 | 1134 | INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed); |
2a421b91 | 1135 | INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan); |
88be0264 | 1136 | INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan); |
2a421b91 TW |
1137 | INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check); |
1138 | } | |
2a421b91 | 1139 | |
e7e16b90 SG |
1140 | void iwl_cancel_scan_deferred_work(struct iwl_priv *priv) |
1141 | { | |
1142 | cancel_work_sync(&priv->start_internal_scan); | |
1143 | cancel_work_sync(&priv->abort_scan); | |
1144 | cancel_work_sync(&priv->scan_completed); | |
1145 | ||
1146 | if (cancel_delayed_work_sync(&priv->scan_check)) { | |
b1eea297 | 1147 | mutex_lock(&priv->mutex); |
e7e16b90 | 1148 | iwl_force_scan_end(priv); |
b1eea297 | 1149 | mutex_unlock(&priv->mutex); |
e7e16b90 SG |
1150 | } |
1151 | } | |
622a9268 IP |
1152 | |
1153 | void iwl_scan_roc_expired(struct iwl_priv *priv) | |
1154 | { | |
1155 | /* | |
1156 | * The status bit should be set here, to prevent a race | |
1157 | * where the atomic_read returns 1, but before the execution continues | |
1158 | * iwl_scan_offchannel_skb_status() checks if the status bit is set | |
1159 | */ | |
1160 | set_bit(STATUS_SCAN_ROC_EXPIRED, &priv->status); | |
1161 | ||
1162 | if (atomic_read(&priv->num_aux_in_flight) == 0) { | |
1163 | ieee80211_remain_on_channel_expired(priv->hw); | |
1164 | priv->hw_roc_channel = NULL; | |
1165 | schedule_delayed_work(&priv->hw_roc_disable_work, | |
1166 | 10 * HZ); | |
1167 | ||
1168 | clear_bit(STATUS_SCAN_ROC_EXPIRED, &priv->status); | |
1169 | } else { | |
1170 | IWL_DEBUG_SCAN(priv, "ROC done with %d frames in aux\n", | |
1171 | atomic_read(&priv->num_aux_in_flight)); | |
1172 | } | |
1173 | } | |
1174 | ||
1175 | void iwl_scan_offchannel_skb(struct iwl_priv *priv) | |
1176 | { | |
1177 | WARN_ON(!priv->hw_roc_start_notified); | |
1178 | atomic_inc(&priv->num_aux_in_flight); | |
1179 | } | |
1180 | ||
1181 | void iwl_scan_offchannel_skb_status(struct iwl_priv *priv) | |
1182 | { | |
1183 | if (atomic_dec_return(&priv->num_aux_in_flight) == 0 && | |
1184 | test_bit(STATUS_SCAN_ROC_EXPIRED, &priv->status)) { | |
1185 | IWL_DEBUG_SCAN(priv, "0 aux frames. Calling ROC expired\n"); | |
1186 | iwl_scan_roc_expired(priv); | |
1187 | } | |
1188 | } |