Commit | Line | Data |
---|---|---|
2a421b91 TW |
1 | /****************************************************************************** |
2 | * | |
3 | * GPL LICENSE SUMMARY | |
4 | * | |
01f8162a | 5 | * Copyright(c) 2008 - 2009 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 | |
22 | * in the file called LICENSE.GPL. | |
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 | *****************************************************************************/ | |
7e272fcf | 28 | #include <linux/types.h> |
2a421b91 | 29 | #include <linux/etherdevice.h> |
7e272fcf JL |
30 | #include <net/lib80211.h> |
31 | #include <net/mac80211.h> | |
2a421b91 TW |
32 | |
33 | #include "iwl-eeprom.h" | |
34 | #include "iwl-dev.h" | |
35 | #include "iwl-core.h" | |
36 | #include "iwl-sta.h" | |
37 | #include "iwl-io.h" | |
38 | #include "iwl-helpers.h" | |
39 | ||
40 | /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after | |
41 | * sending probe req. This should be set long enough to hear probe responses | |
42 | * from more than one AP. */ | |
fe905f1d TW |
43 | #define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */ |
44 | #define IWL_ACTIVE_DWELL_TIME_52 (20) | |
45 | ||
46 | #define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3) | |
47 | #define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2) | |
2a421b91 TW |
48 | |
49 | /* For faster active scanning, scan will move to the next channel if fewer than | |
50 | * PLCP_QUIET_THRESH packets are heard on this channel within | |
51 | * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell | |
52 | * time if it's a quiet channel (nothing responded to our probe, and there's | |
53 | * no other traffic). | |
54 | * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */ | |
55 | #define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */ | |
fe905f1d | 56 | #define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(10) /* msec */ |
2a421b91 TW |
57 | |
58 | /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel. | |
59 | * Must be set longer than active dwell time. | |
60 | * For the most reliable scan, set > AP beacon interval (typically 100msec). */ | |
61 | #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */ | |
62 | #define IWL_PASSIVE_DWELL_TIME_52 (10) | |
63 | #define IWL_PASSIVE_DWELL_BASE (100) | |
64 | #define IWL_CHANNEL_TUNE_TIME 5 | |
65 | ||
fe905f1d TW |
66 | #define IWL_SCAN_PROBE_MASK(n) cpu_to_le32((BIT(n) | (BIT(n) - BIT(1)))) |
67 | ||
68 | ||
2a421b91 TW |
69 | /** |
70 | * iwl_scan_cancel - Cancel any currently executing HW scan | |
71 | * | |
72 | * NOTE: priv->mutex is not required before calling this function | |
73 | */ | |
74 | int iwl_scan_cancel(struct iwl_priv *priv) | |
75 | { | |
76 | if (!test_bit(STATUS_SCAN_HW, &priv->status)) { | |
77 | clear_bit(STATUS_SCANNING, &priv->status); | |
78 | return 0; | |
79 | } | |
80 | ||
81 | if (test_bit(STATUS_SCANNING, &priv->status)) { | |
82 | if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) { | |
83 | IWL_DEBUG_SCAN("Queuing scan abort.\n"); | |
84 | set_bit(STATUS_SCAN_ABORTING, &priv->status); | |
85 | queue_work(priv->workqueue, &priv->abort_scan); | |
86 | ||
87 | } else | |
88 | IWL_DEBUG_SCAN("Scan abort already in progress.\n"); | |
89 | ||
90 | return test_bit(STATUS_SCANNING, &priv->status); | |
91 | } | |
92 | ||
93 | return 0; | |
94 | } | |
95 | EXPORT_SYMBOL(iwl_scan_cancel); | |
96 | /** | |
97 | * iwl_scan_cancel_timeout - Cancel any currently executing HW scan | |
98 | * @ms: amount of time to wait (in milliseconds) for scan to abort | |
99 | * | |
100 | * NOTE: priv->mutex must be held before calling this function | |
101 | */ | |
102 | int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms) | |
103 | { | |
104 | unsigned long now = jiffies; | |
105 | int ret; | |
106 | ||
107 | ret = iwl_scan_cancel(priv); | |
108 | if (ret && ms) { | |
109 | mutex_unlock(&priv->mutex); | |
110 | while (!time_after(jiffies, now + msecs_to_jiffies(ms)) && | |
111 | test_bit(STATUS_SCANNING, &priv->status)) | |
112 | msleep(1); | |
113 | mutex_lock(&priv->mutex); | |
114 | ||
115 | return test_bit(STATUS_SCANNING, &priv->status); | |
116 | } | |
117 | ||
118 | return ret; | |
119 | } | |
120 | EXPORT_SYMBOL(iwl_scan_cancel_timeout); | |
121 | ||
122 | static int iwl_send_scan_abort(struct iwl_priv *priv) | |
123 | { | |
124 | int ret = 0; | |
125 | struct iwl_rx_packet *res; | |
126 | struct iwl_host_cmd cmd = { | |
127 | .id = REPLY_SCAN_ABORT_CMD, | |
128 | .meta.flags = CMD_WANT_SKB, | |
129 | }; | |
130 | ||
131 | /* If there isn't a scan actively going on in the hardware | |
132 | * then we are in between scan bands and not actually | |
133 | * actively scanning, so don't send the abort command */ | |
134 | if (!test_bit(STATUS_SCAN_HW, &priv->status)) { | |
135 | clear_bit(STATUS_SCAN_ABORTING, &priv->status); | |
136 | return 0; | |
137 | } | |
138 | ||
139 | ret = iwl_send_cmd_sync(priv, &cmd); | |
140 | if (ret) { | |
141 | clear_bit(STATUS_SCAN_ABORTING, &priv->status); | |
142 | return ret; | |
143 | } | |
144 | ||
145 | res = (struct iwl_rx_packet *)cmd.meta.u.skb->data; | |
146 | if (res->u.status != CAN_ABORT_STATUS) { | |
147 | /* The scan abort will return 1 for success or | |
148 | * 2 for "failure". A failure condition can be | |
149 | * due to simply not being in an active scan which | |
150 | * can occur if we send the scan abort before we | |
151 | * the microcode has notified us that a scan is | |
152 | * completed. */ | |
153 | IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status); | |
154 | clear_bit(STATUS_SCAN_ABORTING, &priv->status); | |
155 | clear_bit(STATUS_SCAN_HW, &priv->status); | |
156 | } | |
157 | ||
14652562 | 158 | priv->alloc_rxb_skb--; |
2a421b91 TW |
159 | dev_kfree_skb_any(cmd.meta.u.skb); |
160 | ||
161 | return ret; | |
162 | } | |
163 | ||
164 | ||
165 | /* Service response to REPLY_SCAN_CMD (0x80) */ | |
166 | static void iwl_rx_reply_scan(struct iwl_priv *priv, | |
167 | struct iwl_rx_mem_buffer *rxb) | |
168 | { | |
169 | #ifdef CONFIG_IWLWIFI_DEBUG | |
170 | struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data; | |
171 | struct iwl_scanreq_notification *notif = | |
172 | (struct iwl_scanreq_notification *)pkt->u.raw; | |
173 | ||
174 | IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status); | |
175 | #endif | |
176 | } | |
177 | ||
178 | /* Service SCAN_START_NOTIFICATION (0x82) */ | |
179 | static void iwl_rx_scan_start_notif(struct iwl_priv *priv, | |
180 | struct iwl_rx_mem_buffer *rxb) | |
181 | { | |
182 | struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data; | |
183 | struct iwl_scanstart_notification *notif = | |
184 | (struct iwl_scanstart_notification *)pkt->u.raw; | |
185 | priv->scan_start_tsf = le32_to_cpu(notif->tsf_low); | |
186 | IWL_DEBUG_SCAN("Scan start: " | |
187 | "%d [802.11%s] " | |
188 | "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n", | |
189 | notif->channel, | |
190 | notif->band ? "bg" : "a", | |
fe905f1d TW |
191 | le32_to_cpu(notif->tsf_high), |
192 | le32_to_cpu(notif->tsf_low), | |
193 | notif->status, notif->beacon_timer); | |
2a421b91 TW |
194 | } |
195 | ||
196 | /* Service SCAN_RESULTS_NOTIFICATION (0x83) */ | |
197 | static void iwl_rx_scan_results_notif(struct iwl_priv *priv, | |
198 | struct iwl_rx_mem_buffer *rxb) | |
199 | { | |
200 | #ifdef CONFIG_IWLWIFI_DEBUG | |
201 | struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data; | |
202 | struct iwl_scanresults_notification *notif = | |
203 | (struct iwl_scanresults_notification *)pkt->u.raw; | |
204 | ||
205 | IWL_DEBUG_SCAN("Scan ch.res: " | |
206 | "%d [802.11%s] " | |
207 | "(TSF: 0x%08X:%08X) - %d " | |
208 | "elapsed=%lu usec (%dms since last)\n", | |
209 | notif->channel, | |
210 | notif->band ? "bg" : "a", | |
211 | le32_to_cpu(notif->tsf_high), | |
212 | le32_to_cpu(notif->tsf_low), | |
213 | le32_to_cpu(notif->statistics[0]), | |
214 | le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf, | |
215 | jiffies_to_msecs(elapsed_jiffies | |
216 | (priv->last_scan_jiffies, jiffies))); | |
217 | #endif | |
218 | ||
219 | priv->last_scan_jiffies = jiffies; | |
220 | priv->next_scan_jiffies = 0; | |
221 | } | |
222 | ||
223 | /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */ | |
224 | static void iwl_rx_scan_complete_notif(struct iwl_priv *priv, | |
225 | struct iwl_rx_mem_buffer *rxb) | |
226 | { | |
cb9289cb | 227 | #ifdef CONFIG_IWLWIFI_DEBUG |
2a421b91 TW |
228 | struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data; |
229 | struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw; | |
230 | ||
231 | IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n", | |
232 | scan_notif->scanned_channels, | |
233 | scan_notif->tsf_low, | |
234 | scan_notif->tsf_high, scan_notif->status); | |
cb9289cb | 235 | #endif |
2a421b91 TW |
236 | |
237 | /* The HW is no longer scanning */ | |
238 | clear_bit(STATUS_SCAN_HW, &priv->status); | |
239 | ||
240 | /* The scan completion notification came in, so kill that timer... */ | |
241 | cancel_delayed_work(&priv->scan_check); | |
242 | ||
243 | IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n", | |
1b63ba8a DM |
244 | (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ? |
245 | "2.4" : "5.2", | |
2a421b91 TW |
246 | jiffies_to_msecs(elapsed_jiffies |
247 | (priv->scan_pass_start, jiffies))); | |
248 | ||
1b63ba8a DM |
249 | /* Remove this scanned band from the list of pending |
250 | * bands to scan, band G precedes A in order of scanning | |
251 | * as seen in iwl_bg_request_scan */ | |
252 | if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) | |
253 | priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ); | |
254 | else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) | |
255 | priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ); | |
2a421b91 TW |
256 | |
257 | /* If a request to abort was given, or the scan did not succeed | |
258 | * then we reset the scan state machine and terminate, | |
259 | * re-queuing another scan if one has been requested */ | |
260 | if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) { | |
261 | IWL_DEBUG_INFO("Aborted scan completed.\n"); | |
262 | clear_bit(STATUS_SCAN_ABORTING, &priv->status); | |
263 | } else { | |
264 | /* If there are more bands on this scan pass reschedule */ | |
1b63ba8a | 265 | if (priv->scan_bands) |
2a421b91 TW |
266 | goto reschedule; |
267 | } | |
268 | ||
269 | priv->last_scan_jiffies = jiffies; | |
270 | priv->next_scan_jiffies = 0; | |
271 | IWL_DEBUG_INFO("Setting scan to off\n"); | |
272 | ||
273 | clear_bit(STATUS_SCANNING, &priv->status); | |
274 | ||
275 | IWL_DEBUG_INFO("Scan took %dms\n", | |
276 | jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies))); | |
277 | ||
278 | queue_work(priv->workqueue, &priv->scan_completed); | |
279 | ||
280 | return; | |
281 | ||
282 | reschedule: | |
283 | priv->scan_pass_start = jiffies; | |
284 | queue_work(priv->workqueue, &priv->request_scan); | |
285 | } | |
286 | ||
287 | void iwl_setup_rx_scan_handlers(struct iwl_priv *priv) | |
288 | { | |
289 | /* scan handlers */ | |
290 | priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan; | |
291 | priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif; | |
292 | priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] = | |
293 | iwl_rx_scan_results_notif; | |
294 | priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] = | |
295 | iwl_rx_scan_complete_notif; | |
296 | } | |
297 | EXPORT_SYMBOL(iwl_setup_rx_scan_handlers); | |
298 | ||
299 | static inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv, | |
fe905f1d TW |
300 | enum ieee80211_band band, |
301 | u8 n_probes) | |
2a421b91 TW |
302 | { |
303 | if (band == IEEE80211_BAND_5GHZ) | |
fe905f1d TW |
304 | return IWL_ACTIVE_DWELL_TIME_52 + |
305 | IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1); | |
2a421b91 | 306 | else |
fe905f1d TW |
307 | return IWL_ACTIVE_DWELL_TIME_24 + |
308 | IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1); | |
2a421b91 TW |
309 | } |
310 | ||
311 | static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv, | |
fe905f1d | 312 | enum ieee80211_band band) |
2a421b91 | 313 | { |
fe905f1d | 314 | u16 passive = (band == IEEE80211_BAND_2GHZ) ? |
2a421b91 TW |
315 | IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 : |
316 | IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52; | |
317 | ||
318 | if (iwl_is_associated(priv)) { | |
319 | /* If we're associated, we clamp the maximum passive | |
320 | * dwell time to be 98% of the beacon interval (minus | |
321 | * 2 * channel tune time) */ | |
322 | passive = priv->beacon_int; | |
323 | if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive) | |
324 | passive = IWL_PASSIVE_DWELL_BASE; | |
325 | passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2; | |
326 | } | |
327 | ||
2a421b91 TW |
328 | return passive; |
329 | } | |
330 | ||
331 | static int iwl_get_channels_for_scan(struct iwl_priv *priv, | |
332 | enum ieee80211_band band, | |
fe905f1d | 333 | u8 is_active, u8 n_probes, |
2a421b91 TW |
334 | struct iwl_scan_channel *scan_ch) |
335 | { | |
336 | const struct ieee80211_channel *channels = NULL; | |
337 | const struct ieee80211_supported_band *sband; | |
338 | const struct iwl_channel_info *ch_info; | |
339 | u16 passive_dwell = 0; | |
340 | u16 active_dwell = 0; | |
341 | int added, i; | |
d16dc48a | 342 | u16 channel; |
2a421b91 TW |
343 | |
344 | sband = iwl_get_hw_mode(priv, band); | |
345 | if (!sband) | |
346 | return 0; | |
347 | ||
348 | channels = sband->channels; | |
349 | ||
fe905f1d | 350 | active_dwell = iwl_get_active_dwell_time(priv, band, n_probes); |
2a421b91 TW |
351 | passive_dwell = iwl_get_passive_dwell_time(priv, band); |
352 | ||
fe905f1d TW |
353 | if (passive_dwell <= active_dwell) |
354 | passive_dwell = active_dwell + 1; | |
355 | ||
2a421b91 TW |
356 | for (i = 0, added = 0; i < sband->n_channels; i++) { |
357 | if (channels[i].flags & IEEE80211_CHAN_DISABLED) | |
358 | continue; | |
359 | ||
d16dc48a | 360 | channel = |
2a421b91 | 361 | ieee80211_frequency_to_channel(channels[i].center_freq); |
d16dc48a | 362 | scan_ch->channel = cpu_to_le16(channel); |
2a421b91 | 363 | |
d16dc48a | 364 | ch_info = iwl_get_channel_info(priv, band, channel); |
2a421b91 | 365 | if (!is_channel_valid(ch_info)) { |
1b63ba8a | 366 | IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n", |
d16dc48a | 367 | channel); |
2a421b91 TW |
368 | continue; |
369 | } | |
370 | ||
371 | if (!is_active || is_channel_passive(ch_info) || | |
372 | (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) | |
d16dc48a | 373 | scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE; |
2a421b91 | 374 | else |
d16dc48a | 375 | scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE; |
2a421b91 | 376 | |
0ffe014a | 377 | if (n_probes) |
fe905f1d | 378 | scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes); |
2a421b91 TW |
379 | |
380 | scan_ch->active_dwell = cpu_to_le16(active_dwell); | |
381 | scan_ch->passive_dwell = cpu_to_le16(passive_dwell); | |
382 | ||
383 | /* Set txpower levels to defaults */ | |
f53696de | 384 | scan_ch->dsp_atten = 110; |
2a421b91 | 385 | |
fe905f1d TW |
386 | /* NOTE: if we were doing 6Mb OFDM for scans we'd use |
387 | * power level: | |
388 | * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3; | |
389 | */ | |
2a421b91 | 390 | if (band == IEEE80211_BAND_5GHZ) |
f53696de | 391 | scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3; |
fe905f1d | 392 | else |
f53696de | 393 | scan_ch->tx_gain = ((1 << 5) | (5 << 3)); |
2a421b91 | 394 | |
fe905f1d TW |
395 | IWL_DEBUG_SCAN("Scanning ch=%d prob=0x%X [%s %d]\n", |
396 | channel, le32_to_cpu(scan_ch->type), | |
d16dc48a TW |
397 | (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ? |
398 | "ACTIVE" : "PASSIVE", | |
399 | (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ? | |
2a421b91 TW |
400 | active_dwell : passive_dwell); |
401 | ||
402 | scan_ch++; | |
403 | added++; | |
404 | } | |
405 | ||
406 | IWL_DEBUG_SCAN("total channels to scan %d \n", added); | |
407 | return added; | |
408 | } | |
409 | ||
f53696de TW |
410 | void iwl_init_scan_params(struct iwl_priv *priv) |
411 | { | |
76eff18b | 412 | u8 ant_idx = fls(priv->hw_params.valid_tx_ant) - 1; |
f53696de | 413 | if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ]) |
76eff18b | 414 | priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx; |
f53696de | 415 | if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ]) |
76eff18b | 416 | priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx; |
f53696de TW |
417 | } |
418 | ||
2a421b91 TW |
419 | int iwl_scan_initiate(struct iwl_priv *priv) |
420 | { | |
2a421b91 TW |
421 | if (!iwl_is_ready_rf(priv)) { |
422 | IWL_DEBUG_SCAN("Aborting scan due to not ready.\n"); | |
423 | return -EIO; | |
424 | } | |
425 | ||
426 | if (test_bit(STATUS_SCANNING, &priv->status)) { | |
427 | IWL_DEBUG_SCAN("Scan already in progress.\n"); | |
428 | return -EAGAIN; | |
429 | } | |
430 | ||
431 | if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) { | |
8d09a5e1 | 432 | IWL_DEBUG_SCAN("Scan request while abort pending\n"); |
2a421b91 TW |
433 | return -EAGAIN; |
434 | } | |
435 | ||
436 | IWL_DEBUG_INFO("Starting scan...\n"); | |
1b63ba8a DM |
437 | if (priv->cfg->sku & IWL_SKU_G) |
438 | priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ); | |
439 | if (priv->cfg->sku & IWL_SKU_A) | |
440 | priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ); | |
2a421b91 TW |
441 | set_bit(STATUS_SCANNING, &priv->status); |
442 | priv->scan_start = jiffies; | |
443 | priv->scan_pass_start = priv->scan_start; | |
444 | ||
445 | queue_work(priv->workqueue, &priv->request_scan); | |
446 | ||
447 | return 0; | |
448 | } | |
449 | EXPORT_SYMBOL(iwl_scan_initiate); | |
450 | ||
451 | #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ) | |
452 | ||
453 | static void iwl_bg_scan_check(struct work_struct *data) | |
454 | { | |
455 | struct iwl_priv *priv = | |
456 | container_of(data, struct iwl_priv, scan_check.work); | |
457 | ||
458 | if (test_bit(STATUS_EXIT_PENDING, &priv->status)) | |
459 | return; | |
460 | ||
461 | mutex_lock(&priv->mutex); | |
462 | if (test_bit(STATUS_SCANNING, &priv->status) || | |
463 | test_bit(STATUS_SCAN_ABORTING, &priv->status)) { | |
464 | IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting " | |
465 | "adapter (%dms)\n", | |
466 | jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG)); | |
467 | ||
468 | if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) | |
469 | iwl_send_scan_abort(priv); | |
470 | } | |
471 | mutex_unlock(&priv->mutex); | |
472 | } | |
473 | /** | |
474 | * iwl_supported_rate_to_ie - fill in the supported rate in IE field | |
475 | * | |
476 | * return : set the bit for each supported rate insert in ie | |
477 | */ | |
478 | static u16 iwl_supported_rate_to_ie(u8 *ie, u16 supported_rate, | |
479 | u16 basic_rate, int *left) | |
480 | { | |
481 | u16 ret_rates = 0, bit; | |
482 | int i; | |
483 | u8 *cnt = ie; | |
484 | u8 *rates = ie + 1; | |
485 | ||
486 | for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) { | |
487 | if (bit & supported_rate) { | |
488 | ret_rates |= bit; | |
489 | rates[*cnt] = iwl_rates[i].ieee | | |
490 | ((bit & basic_rate) ? 0x80 : 0x00); | |
491 | (*cnt)++; | |
492 | (*left)--; | |
493 | if ((*left <= 0) || | |
494 | (*cnt >= IWL_SUPPORTED_RATES_IE_LEN)) | |
495 | break; | |
496 | } | |
497 | } | |
498 | ||
499 | return ret_rates; | |
500 | } | |
501 | ||
502 | ||
503 | static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband, | |
f53696de | 504 | u8 *pos, int *left) |
2a421b91 TW |
505 | { |
506 | struct ieee80211_ht_cap *ht_cap; | |
507 | ||
d9fe60de | 508 | if (!sband || !sband->ht_cap.ht_supported) |
2a421b91 TW |
509 | return; |
510 | ||
511 | if (*left < sizeof(struct ieee80211_ht_cap)) | |
512 | return; | |
513 | ||
514 | *pos++ = sizeof(struct ieee80211_ht_cap); | |
515 | ht_cap = (struct ieee80211_ht_cap *) pos; | |
516 | ||
d9fe60de JB |
517 | ht_cap->cap_info = cpu_to_le16(sband->ht_cap.cap); |
518 | memcpy(&ht_cap->mcs, &sband->ht_cap.mcs, 16); | |
2a421b91 | 519 | ht_cap->ampdu_params_info = |
d9fe60de JB |
520 | (sband->ht_cap.ampdu_factor & IEEE80211_HT_AMPDU_PARM_FACTOR) | |
521 | ((sband->ht_cap.ampdu_density << 2) & | |
522 | IEEE80211_HT_AMPDU_PARM_DENSITY); | |
2a421b91 TW |
523 | *left -= sizeof(struct ieee80211_ht_cap); |
524 | } | |
525 | ||
526 | /** | |
527 | * iwl_fill_probe_req - fill in all required fields and IE for probe request | |
528 | */ | |
f53696de | 529 | |
2a421b91 TW |
530 | static u16 iwl_fill_probe_req(struct iwl_priv *priv, |
531 | enum ieee80211_band band, | |
532 | struct ieee80211_mgmt *frame, | |
f53696de | 533 | int left) |
2a421b91 TW |
534 | { |
535 | int len = 0; | |
536 | u8 *pos = NULL; | |
537 | u16 active_rates, ret_rates, cck_rates, active_rate_basic; | |
538 | const struct ieee80211_supported_band *sband = | |
539 | iwl_get_hw_mode(priv, band); | |
540 | ||
f53696de | 541 | |
2a421b91 TW |
542 | /* Make sure there is enough space for the probe request, |
543 | * two mandatory IEs and the data */ | |
544 | left -= 24; | |
545 | if (left < 0) | |
546 | return 0; | |
2a421b91 TW |
547 | |
548 | frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ); | |
549 | memcpy(frame->da, iwl_bcast_addr, ETH_ALEN); | |
550 | memcpy(frame->sa, priv->mac_addr, ETH_ALEN); | |
551 | memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN); | |
552 | frame->seq_ctrl = 0; | |
553 | ||
f53696de TW |
554 | len += 24; |
555 | ||
2a421b91 | 556 | /* ...next IE... */ |
f53696de | 557 | pos = &frame->u.probe_req.variable[0]; |
2a421b91 | 558 | |
f53696de | 559 | /* fill in our indirect SSID IE */ |
2a421b91 TW |
560 | left -= 2; |
561 | if (left < 0) | |
562 | return 0; | |
2a421b91 TW |
563 | *pos++ = WLAN_EID_SSID; |
564 | *pos++ = 0; | |
565 | ||
f53696de | 566 | len += 2; |
2a421b91 TW |
567 | |
568 | /* fill in supported rate */ | |
2a421b91 TW |
569 | left -= 2; |
570 | if (left < 0) | |
571 | return 0; | |
572 | ||
2a421b91 TW |
573 | *pos++ = WLAN_EID_SUPP_RATES; |
574 | *pos = 0; | |
575 | ||
576 | /* exclude 60M rate */ | |
577 | active_rates = priv->rates_mask; | |
578 | active_rates &= ~IWL_RATE_60M_MASK; | |
579 | ||
580 | active_rate_basic = active_rates & IWL_BASIC_RATES_MASK; | |
581 | ||
582 | cck_rates = IWL_CCK_RATES_MASK & active_rates; | |
583 | ret_rates = iwl_supported_rate_to_ie(pos, cck_rates, | |
f53696de | 584 | active_rate_basic, &left); |
2a421b91 TW |
585 | active_rates &= ~ret_rates; |
586 | ||
587 | ret_rates = iwl_supported_rate_to_ie(pos, active_rates, | |
f53696de | 588 | active_rate_basic, &left); |
2a421b91 TW |
589 | active_rates &= ~ret_rates; |
590 | ||
591 | len += 2 + *pos; | |
592 | pos += (*pos) + 1; | |
f53696de | 593 | |
2a421b91 TW |
594 | if (active_rates == 0) |
595 | goto fill_end; | |
596 | ||
597 | /* fill in supported extended rate */ | |
598 | /* ...next IE... */ | |
599 | left -= 2; | |
600 | if (left < 0) | |
601 | return 0; | |
602 | /* ... fill it in... */ | |
603 | *pos++ = WLAN_EID_EXT_SUPP_RATES; | |
604 | *pos = 0; | |
f53696de TW |
605 | iwl_supported_rate_to_ie(pos, active_rates, active_rate_basic, &left); |
606 | if (*pos > 0) { | |
2a421b91 | 607 | len += 2 + *pos; |
f53696de TW |
608 | pos += (*pos) + 1; |
609 | } else { | |
610 | pos--; | |
611 | } | |
2a421b91 TW |
612 | |
613 | fill_end: | |
f53696de | 614 | |
2a421b91 TW |
615 | left -= 2; |
616 | if (left < 0) | |
617 | return 0; | |
618 | ||
619 | *pos++ = WLAN_EID_HT_CAPABILITY; | |
620 | *pos = 0; | |
2a421b91 | 621 | iwl_ht_cap_to_ie(sband, pos, &left); |
2a421b91 TW |
622 | if (*pos > 0) |
623 | len += 2 + *pos; | |
f53696de | 624 | |
2a421b91 TW |
625 | return (u16)len; |
626 | } | |
627 | ||
628 | static void iwl_bg_request_scan(struct work_struct *data) | |
629 | { | |
630 | struct iwl_priv *priv = | |
631 | container_of(data, struct iwl_priv, request_scan); | |
632 | struct iwl_host_cmd cmd = { | |
633 | .id = REPLY_SCAN_CMD, | |
634 | .len = sizeof(struct iwl_scan_cmd), | |
635 | .meta.flags = CMD_SIZE_HUGE, | |
636 | }; | |
637 | struct iwl_scan_cmd *scan; | |
638 | struct ieee80211_conf *conf = NULL; | |
f53696de | 639 | int ret = 0; |
76eff18b | 640 | u32 rate_flags = 0; |
2a421b91 TW |
641 | u16 cmd_len; |
642 | enum ieee80211_band band; | |
fe905f1d | 643 | u8 n_probes = 2; |
d588be6b | 644 | u8 rx_chain = priv->hw_params.valid_rx_ant; |
76eff18b | 645 | u8 rate; |
9387b7ca | 646 | DECLARE_SSID_BUF(ssid); |
2a421b91 TW |
647 | |
648 | conf = ieee80211_get_hw_conf(priv->hw); | |
649 | ||
650 | mutex_lock(&priv->mutex); | |
651 | ||
652 | if (!iwl_is_ready(priv)) { | |
39aadf8c | 653 | IWL_WARN(priv, "request scan called when driver not ready.\n"); |
2a421b91 TW |
654 | goto done; |
655 | } | |
656 | ||
a96a27f9 | 657 | /* Make sure the scan wasn't canceled before this queued work |
2a421b91 TW |
658 | * was given the chance to run... */ |
659 | if (!test_bit(STATUS_SCANNING, &priv->status)) | |
660 | goto done; | |
661 | ||
662 | /* This should never be called or scheduled if there is currently | |
663 | * a scan active in the hardware. */ | |
664 | if (test_bit(STATUS_SCAN_HW, &priv->status)) { | |
665 | IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. " | |
666 | "Ignoring second request.\n"); | |
667 | ret = -EIO; | |
668 | goto done; | |
669 | } | |
670 | ||
671 | if (test_bit(STATUS_EXIT_PENDING, &priv->status)) { | |
672 | IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n"); | |
673 | goto done; | |
674 | } | |
675 | ||
676 | if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) { | |
677 | IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n"); | |
678 | goto done; | |
679 | } | |
680 | ||
681 | if (iwl_is_rfkill(priv)) { | |
682 | IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n"); | |
683 | goto done; | |
684 | } | |
685 | ||
686 | if (!test_bit(STATUS_READY, &priv->status)) { | |
687 | IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n"); | |
688 | goto done; | |
689 | } | |
690 | ||
691 | if (!priv->scan_bands) { | |
692 | IWL_DEBUG_HC("Aborting scan due to no requested bands\n"); | |
693 | goto done; | |
694 | } | |
695 | ||
696 | if (!priv->scan) { | |
697 | priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) + | |
698 | IWL_MAX_SCAN_SIZE, GFP_KERNEL); | |
699 | if (!priv->scan) { | |
700 | ret = -ENOMEM; | |
701 | goto done; | |
702 | } | |
703 | } | |
704 | scan = priv->scan; | |
705 | memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE); | |
706 | ||
707 | scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH; | |
708 | scan->quiet_time = IWL_ACTIVE_QUIET_TIME; | |
709 | ||
710 | if (iwl_is_associated(priv)) { | |
711 | u16 interval = 0; | |
712 | u32 extra; | |
713 | u32 suspend_time = 100; | |
714 | u32 scan_suspend_time = 100; | |
715 | unsigned long flags; | |
716 | ||
717 | IWL_DEBUG_INFO("Scanning while associated...\n"); | |
718 | ||
719 | spin_lock_irqsave(&priv->lock, flags); | |
720 | interval = priv->beacon_int; | |
721 | spin_unlock_irqrestore(&priv->lock, flags); | |
722 | ||
723 | scan->suspend_time = 0; | |
724 | scan->max_out_time = cpu_to_le32(200 * 1024); | |
725 | if (!interval) | |
726 | interval = suspend_time; | |
727 | ||
728 | extra = (suspend_time / interval) << 22; | |
729 | scan_suspend_time = (extra | | |
730 | ((suspend_time % interval) * 1024)); | |
731 | scan->suspend_time = cpu_to_le32(scan_suspend_time); | |
732 | IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n", | |
733 | scan_suspend_time, interval); | |
734 | } | |
735 | ||
736 | /* We should add the ability for user to lock to PASSIVE ONLY */ | |
737 | if (priv->one_direct_scan) { | |
f53696de | 738 | IWL_DEBUG_SCAN("Start direct scan for '%s'\n", |
9387b7ca JL |
739 | print_ssid(ssid, priv->direct_ssid, |
740 | priv->direct_ssid_len)); | |
2a421b91 TW |
741 | scan->direct_scan[0].id = WLAN_EID_SSID; |
742 | scan->direct_scan[0].len = priv->direct_ssid_len; | |
743 | memcpy(scan->direct_scan[0].ssid, | |
744 | priv->direct_ssid, priv->direct_ssid_len); | |
fe905f1d | 745 | n_probes++; |
2a421b91 | 746 | } else { |
f53696de | 747 | IWL_DEBUG_SCAN("Start indirect scan.\n"); |
2a421b91 TW |
748 | } |
749 | ||
750 | scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK; | |
751 | scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id; | |
752 | scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE; | |
753 | ||
754 | ||
1b63ba8a | 755 | if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) { |
f53696de | 756 | band = IEEE80211_BAND_2GHZ; |
2a421b91 | 757 | scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK; |
76eff18b TW |
758 | if (priv->active_rxon.flags & RXON_FLG_CHANNEL_MODE_PURE_40_MSK) { |
759 | rate = IWL_RATE_6M_PLCP; | |
760 | } else { | |
761 | rate = IWL_RATE_1M_PLCP; | |
762 | rate_flags = RATE_MCS_CCK_MSK; | |
763 | } | |
2a421b91 | 764 | scan->good_CRC_th = 0; |
1b63ba8a | 765 | } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) { |
f53696de | 766 | band = IEEE80211_BAND_5GHZ; |
76eff18b | 767 | rate = IWL_RATE_6M_PLCP; |
2a421b91 | 768 | scan->good_CRC_th = IWL_GOOD_CRC_TH; |
2a421b91 | 769 | |
f53696de TW |
770 | /* Force use of chains B and C (0x6) for scan Rx for 4965 |
771 | * Avoid A (0x1) because of its off-channel reception on A-band. | |
5118303f | 772 | */ |
f53696de TW |
773 | if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965) |
774 | rx_chain = 0x6; | |
1b63ba8a | 775 | } else { |
39aadf8c | 776 | IWL_WARN(priv, "Invalid scan band count\n"); |
2a421b91 TW |
777 | goto done; |
778 | } | |
779 | ||
76eff18b TW |
780 | priv->scan_tx_ant[band] = |
781 | iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band]); | |
782 | rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]); | |
783 | scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags); | |
784 | ||
5118303f | 785 | /* MIMO is not used here, but value is required */ |
f53696de TW |
786 | scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK | |
787 | cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) | | |
788 | (rx_chain << RXON_RX_CHAIN_FORCE_SEL_POS) | | |
789 | (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS)); | |
790 | ||
2a421b91 | 791 | cmd_len = iwl_fill_probe_req(priv, band, |
f53696de TW |
792 | (struct ieee80211_mgmt *)scan->data, |
793 | IWL_MAX_SCAN_SIZE - sizeof(*scan)); | |
2a421b91 TW |
794 | |
795 | scan->tx_cmd.len = cpu_to_le16(cmd_len); | |
2a421b91 | 796 | |
05c914fe | 797 | if (priv->iw_mode == NL80211_IFTYPE_MONITOR) |
2a421b91 TW |
798 | scan->filter_flags = RXON_FILTER_PROMISC_MSK; |
799 | ||
f53696de TW |
800 | scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK | |
801 | RXON_FILTER_BCON_AWARE_MSK); | |
802 | ||
fe905f1d TW |
803 | scan->channel_count = |
804 | iwl_get_channels_for_scan(priv, band, 1, /* active */ | |
805 | n_probes, | |
806 | (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]); | |
807 | ||
f53696de TW |
808 | if (scan->channel_count == 0) { |
809 | IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count); | |
810 | goto done; | |
811 | } | |
2a421b91 | 812 | |
2a421b91 TW |
813 | cmd.len += le16_to_cpu(scan->tx_cmd.len) + |
814 | scan->channel_count * sizeof(struct iwl_scan_channel); | |
815 | cmd.data = scan; | |
816 | scan->len = cpu_to_le16(cmd.len); | |
817 | ||
818 | set_bit(STATUS_SCAN_HW, &priv->status); | |
819 | ret = iwl_send_cmd_sync(priv, &cmd); | |
820 | if (ret) | |
821 | goto done; | |
822 | ||
823 | queue_delayed_work(priv->workqueue, &priv->scan_check, | |
824 | IWL_SCAN_CHECK_WATCHDOG); | |
825 | ||
826 | mutex_unlock(&priv->mutex); | |
827 | return; | |
828 | ||
829 | done: | |
951891c7 MA |
830 | /* Cannot perform scan. Make sure we clear scanning |
831 | * bits from status so next scan request can be performed. | |
832 | * If we don't clear scanning status bit here all next scan | |
833 | * will fail | |
834 | */ | |
835 | clear_bit(STATUS_SCAN_HW, &priv->status); | |
836 | clear_bit(STATUS_SCANNING, &priv->status); | |
2a421b91 TW |
837 | /* inform mac80211 scan aborted */ |
838 | queue_work(priv->workqueue, &priv->scan_completed); | |
839 | mutex_unlock(&priv->mutex); | |
840 | } | |
841 | ||
842 | static void iwl_bg_abort_scan(struct work_struct *work) | |
843 | { | |
844 | struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan); | |
845 | ||
846 | if (!iwl_is_ready(priv)) | |
847 | return; | |
848 | ||
849 | mutex_lock(&priv->mutex); | |
850 | ||
851 | set_bit(STATUS_SCAN_ABORTING, &priv->status); | |
852 | iwl_send_scan_abort(priv); | |
853 | ||
854 | mutex_unlock(&priv->mutex); | |
855 | } | |
856 | ||
eedda367 TW |
857 | static void iwl_bg_scan_completed(struct work_struct *work) |
858 | { | |
859 | struct iwl_priv *priv = | |
860 | container_of(work, struct iwl_priv, scan_completed); | |
861 | ||
862 | IWL_DEBUG_SCAN("SCAN complete scan\n"); | |
863 | ||
864 | if (test_bit(STATUS_EXIT_PENDING, &priv->status)) | |
865 | return; | |
866 | ||
867 | ieee80211_scan_completed(priv->hw); | |
868 | ||
869 | /* Since setting the TXPOWER may have been deferred while | |
870 | * performing the scan, fire one off */ | |
871 | mutex_lock(&priv->mutex); | |
872 | iwl_set_tx_power(priv, priv->tx_power_user_lmt, true); | |
873 | mutex_unlock(&priv->mutex); | |
874 | } | |
875 | ||
876 | ||
2a421b91 TW |
877 | void iwl_setup_scan_deferred_work(struct iwl_priv *priv) |
878 | { | |
eedda367 | 879 | INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed); |
2a421b91 TW |
880 | INIT_WORK(&priv->request_scan, iwl_bg_request_scan); |
881 | INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan); | |
882 | INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check); | |
883 | } | |
884 | EXPORT_SYMBOL(iwl_setup_scan_deferred_work); | |
885 |