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