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