iwlwifi: fix in-column rate scaling
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / iwl4965-base.c
CommitLineData
b481de9c
ZY
1/******************************************************************************
2 *
eb7ae89c 3 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
b481de9c
ZY
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
b481de9c
ZY
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/version.h>
33#include <linux/init.h>
34#include <linux/pci.h>
35#include <linux/dma-mapping.h>
36#include <linux/delay.h>
37#include <linux/skbuff.h>
38#include <linux/netdevice.h>
39#include <linux/wireless.h>
40#include <linux/firmware.h>
b481de9c
ZY
41#include <linux/etherdevice.h>
42#include <linux/if_arp.h>
43
b481de9c
ZY
44#include <net/mac80211.h>
45
46#include <asm/div64.h>
47
6bc913bd 48#include "iwl-eeprom.h"
3e0d4cb1 49#include "iwl-dev.h"
fee1247a 50#include "iwl-core.h"
3395f6e9 51#include "iwl-io.h"
b481de9c 52#include "iwl-helpers.h"
6974e363 53#include "iwl-sta.h"
f0832f13 54#include "iwl-calib.h"
b481de9c 55
416e1438 56
b481de9c
ZY
57/******************************************************************************
58 *
59 * module boiler plate
60 *
61 ******************************************************************************/
62
b481de9c
ZY
63/*
64 * module name, copyright, version, etc.
65 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
66 */
67
68#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
69
0a6857e7 70#ifdef CONFIG_IWLWIFI_DEBUG
b481de9c
ZY
71#define VD "d"
72#else
73#define VD
74#endif
75
c8b0e6e1 76#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
b481de9c
ZY
77#define VS "s"
78#else
79#define VS
80#endif
81
df48c323 82#define DRV_VERSION IWLWIFI_VERSION VD VS
b481de9c 83
b481de9c
ZY
84
85MODULE_DESCRIPTION(DRV_DESCRIPTION);
86MODULE_VERSION(DRV_VERSION);
87MODULE_AUTHOR(DRV_COPYRIGHT);
88MODULE_LICENSE("GPL");
89
d1141dfb 90static const struct ieee80211_supported_band *iwl_get_hw_mode(
c79dd5b5 91 struct iwl_priv *priv, enum ieee80211_band band)
b481de9c 92{
8318d78a 93 return priv->hw->wiphy->bands[band];
b481de9c
ZY
94}
95
bb8c093b 96static int iwl4965_is_empty_essid(const char *essid, int essid_len)
b481de9c
ZY
97{
98 /* Single white space is for Linksys APs */
99 if (essid_len == 1 && essid[0] == ' ')
100 return 1;
101
102 /* Otherwise, if the entire essid is 0, we assume it is hidden */
103 while (essid_len) {
104 essid_len--;
105 if (essid[essid_len] != '\0')
106 return 0;
107 }
108
109 return 1;
110}
111
bb8c093b 112static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
b481de9c
ZY
113{
114 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
115 const char *s = essid;
116 char *d = escaped;
117
bb8c093b 118 if (iwl4965_is_empty_essid(essid, essid_len)) {
b481de9c
ZY
119 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
120 return escaped;
121 }
122
123 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
124 while (essid_len--) {
125 if (*s == '\0') {
126 *d++ = '\\';
127 *d++ = '0';
128 s++;
129 } else
130 *d++ = *s++;
131 }
132 *d = '\0';
133 return escaped;
134}
135
b481de9c 136/*************** STATION TABLE MANAGEMENT ****
9fbab516 137 * mac80211 should be examined to determine if sta_info is duplicating
b481de9c
ZY
138 * the functionality provided here
139 */
140
141/**************************************************************/
142
b481de9c 143
b481de9c 144
deb09c43
EG
145static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
146{
c1adf9fb 147 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
deb09c43
EG
148
149 if (hw_decrypt)
150 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
151 else
152 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
153
154}
155
b481de9c 156/**
bb8c093b 157 * iwl4965_check_rxon_cmd - validate RXON structure is valid
b481de9c
ZY
158 *
159 * NOTE: This is really only useful during development and can eventually
160 * be #ifdef'd out once the driver is stable and folks aren't actively
161 * making changes
162 */
c1adf9fb 163static int iwl4965_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
b481de9c
ZY
164{
165 int error = 0;
166 int counter = 1;
167
168 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
169 error |= le32_to_cpu(rxon->flags &
170 (RXON_FLG_TGJ_NARROW_BAND_MSK |
171 RXON_FLG_RADAR_DETECT_MSK));
172 if (error)
173 IWL_WARNING("check 24G fields %d | %d\n",
174 counter++, error);
175 } else {
176 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
177 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
178 if (error)
179 IWL_WARNING("check 52 fields %d | %d\n",
180 counter++, error);
181 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
182 if (error)
183 IWL_WARNING("check 52 CCK %d | %d\n",
184 counter++, error);
185 }
186 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
187 if (error)
188 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
189
190 /* make sure basic rates 6Mbps and 1Mbps are supported */
191 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
192 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
193 if (error)
194 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
195
196 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
197 if (error)
198 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
199
200 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
201 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
202 if (error)
203 IWL_WARNING("check CCK and short slot %d | %d\n",
204 counter++, error);
205
206 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
207 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
208 if (error)
209 IWL_WARNING("check CCK & auto detect %d | %d\n",
210 counter++, error);
211
212 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
213 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
214 if (error)
215 IWL_WARNING("check TGG and auto detect %d | %d\n",
216 counter++, error);
217
218 if (error)
219 IWL_WARNING("Tuning to channel %d\n",
220 le16_to_cpu(rxon->channel));
221
222 if (error) {
bb8c093b 223 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
b481de9c
ZY
224 return -1;
225 }
226 return 0;
227}
228
229/**
9fbab516 230 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
01ebd063 231 * @priv: staging_rxon is compared to active_rxon
b481de9c 232 *
9fbab516
BC
233 * If the RXON structure is changing enough to require a new tune,
234 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
235 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
b481de9c 236 */
c79dd5b5 237static int iwl4965_full_rxon_required(struct iwl_priv *priv)
b481de9c
ZY
238{
239
240 /* These items are only settable from the full RXON command */
241 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
242 compare_ether_addr(priv->staging_rxon.bssid_addr,
243 priv->active_rxon.bssid_addr) ||
244 compare_ether_addr(priv->staging_rxon.node_addr,
245 priv->active_rxon.node_addr) ||
246 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
247 priv->active_rxon.wlap_bssid_addr) ||
248 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
249 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
250 (priv->staging_rxon.air_propagation !=
251 priv->active_rxon.air_propagation) ||
252 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
253 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
254 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
255 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
256 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
257 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
258 return 1;
259
260 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
261 * be updated with the RXON_ASSOC command -- however only some
262 * flag transitions are allowed using RXON_ASSOC */
263
264 /* Check if we are not switching bands */
265 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
266 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
267 return 1;
268
269 /* Check if we are switching association toggle */
270 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
271 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
272 return 1;
273
274 return 0;
275}
276
b481de9c 277/**
bb8c093b 278 * iwl4965_commit_rxon - commit staging_rxon to hardware
b481de9c 279 *
01ebd063 280 * The RXON command in staging_rxon is committed to the hardware and
b481de9c
ZY
281 * the active_rxon structure is updated with the new data. This
282 * function correctly transitions out of the RXON_ASSOC_MSK state if
283 * a HW tune is required based on the RXON structure changes.
284 */
c79dd5b5 285static int iwl4965_commit_rxon(struct iwl_priv *priv)
b481de9c
ZY
286{
287 /* cast away the const for active_rxon in this function */
c1adf9fb 288 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
0795af57 289 DECLARE_MAC_BUF(mac);
b481de9c
ZY
290 int rc = 0;
291
fee1247a 292 if (!iwl_is_alive(priv))
b481de9c
ZY
293 return -1;
294
295 /* always get timestamp with Rx frame */
296 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
297
bb8c093b 298 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
b481de9c
ZY
299 if (rc) {
300 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
301 return -EINVAL;
302 }
303
304 /* If we don't need to send a full RXON, we can use
bb8c093b 305 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
b481de9c 306 * and other flags for the current radio configuration. */
bb8c093b 307 if (!iwl4965_full_rxon_required(priv)) {
7e8c519e 308 rc = iwl_send_rxon_assoc(priv);
b481de9c
ZY
309 if (rc) {
310 IWL_ERROR("Error setting RXON_ASSOC "
311 "configuration (%d).\n", rc);
312 return rc;
313 }
314
315 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
316
317 return 0;
318 }
319
320 /* station table will be cleared */
321 priv->assoc_station_added = 0;
322
b481de9c
ZY
323 /* If we are currently associated and the new config requires
324 * an RXON_ASSOC and the new config wants the associated mask enabled,
325 * we must clear the associated from the active configuration
326 * before we apply the new config */
3109ece1 327 if (iwl_is_associated(priv) &&
b481de9c
ZY
328 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
329 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
330 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
331
857485c0 332 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
c1adf9fb 333 sizeof(struct iwl_rxon_cmd),
b481de9c
ZY
334 &priv->active_rxon);
335
336 /* If the mask clearing failed then we set
337 * active_rxon back to what it was previously */
338 if (rc) {
339 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
340 IWL_ERROR("Error clearing ASSOC_MSK on current "
341 "configuration (%d).\n", rc);
342 return rc;
343 }
b481de9c
ZY
344 }
345
346 IWL_DEBUG_INFO("Sending RXON\n"
347 "* with%s RXON_FILTER_ASSOC_MSK\n"
348 "* channel = %d\n"
0795af57 349 "* bssid = %s\n",
b481de9c
ZY
350 ((priv->staging_rxon.filter_flags &
351 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
352 le16_to_cpu(priv->staging_rxon.channel),
0795af57 353 print_mac(mac, priv->staging_rxon.bssid_addr));
b481de9c 354
099b40b7 355 iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
b481de9c 356 /* Apply the new configuration */
857485c0 357 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
c1adf9fb 358 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
b481de9c
ZY
359 if (rc) {
360 IWL_ERROR("Error setting new configuration (%d).\n", rc);
361 return rc;
362 }
363
7a999bf0 364 iwl_remove_station(priv, iwl_bcast_addr, 0);
bf85ea4f 365 iwlcore_clear_stations_table(priv);
556f8db7 366
b481de9c
ZY
367 if (!priv->error_recovering)
368 priv->start_calib = 0;
369
f0832f13 370 iwl_init_sensitivity(priv);
b481de9c
ZY
371
372 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
373
374 /* If we issue a new RXON command which required a tune then we must
375 * send a new TXPOWER command or we won't be able to Tx any frames */
bb8c093b 376 rc = iwl4965_hw_reg_send_txpower(priv);
b481de9c
ZY
377 if (rc) {
378 IWL_ERROR("Error setting Tx power (%d).\n", rc);
379 return rc;
380 }
381
382 /* Add the broadcast address so we can send broadcast frames */
4f40e4d9 383 if (iwl_rxon_add_station(priv, iwl_bcast_addr, 0) ==
b481de9c
ZY
384 IWL_INVALID_STATION) {
385 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
386 return -EIO;
387 }
388
389 /* If we have set the ASSOC_MSK and we are in BSS mode then
390 * add the IWL_AP_ID to the station rate table */
3109ece1 391 if (iwl_is_associated(priv) &&
b481de9c 392 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
4f40e4d9 393 if (iwl_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
b481de9c
ZY
394 == IWL_INVALID_STATION) {
395 IWL_ERROR("Error adding AP address for transmit.\n");
396 return -EIO;
397 }
398 priv->assoc_station_added = 1;
6974e363
EG
399 if (priv->default_wep_key &&
400 iwl_send_static_wepkey_cmd(priv, 0))
401 IWL_ERROR("Could not send WEP static key.\n");
b481de9c
ZY
402 }
403
404 return 0;
405}
406
5da4b55f
MA
407void iwl4965_update_chain_flags(struct iwl_priv *priv)
408{
409
c7de35cd 410 iwl_set_rxon_chain(priv);
5da4b55f
MA
411 iwl4965_commit_rxon(priv);
412}
413
c79dd5b5 414static int iwl4965_send_bt_config(struct iwl_priv *priv)
b481de9c 415{
bb8c093b 416 struct iwl4965_bt_cmd bt_cmd = {
b481de9c
ZY
417 .flags = 3,
418 .lead_time = 0xAA,
419 .max_kill = 1,
420 .kill_ack_mask = 0,
421 .kill_cts_mask = 0,
422 };
423
857485c0 424 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
bb8c093b 425 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
b481de9c
ZY
426}
427
c79dd5b5 428static int iwl4965_send_scan_abort(struct iwl_priv *priv)
b481de9c 429{
db11d634
TW
430 int ret = 0;
431 struct iwl_rx_packet *res;
857485c0 432 struct iwl_host_cmd cmd = {
b481de9c
ZY
433 .id = REPLY_SCAN_ABORT_CMD,
434 .meta.flags = CMD_WANT_SKB,
435 };
436
437 /* If there isn't a scan actively going on in the hardware
438 * then we are in between scan bands and not actually
439 * actively scanning, so don't send the abort command */
440 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
441 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
442 return 0;
443 }
444
db11d634
TW
445 ret = iwl_send_cmd_sync(priv, &cmd);
446 if (ret) {
b481de9c 447 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
db11d634 448 return ret;
b481de9c
ZY
449 }
450
db11d634 451 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
b481de9c
ZY
452 if (res->u.status != CAN_ABORT_STATUS) {
453 /* The scan abort will return 1 for success or
454 * 2 for "failure". A failure condition can be
455 * due to simply not being in an active scan which
456 * can occur if we send the scan abort before we
457 * the microcode has notified us that a scan is
458 * completed. */
459 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
460 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
461 clear_bit(STATUS_SCAN_HW, &priv->status);
462 }
463
464 dev_kfree_skb_any(cmd.meta.u.skb);
465
db11d634 466 return ret;
b481de9c
ZY
467}
468
b481de9c
ZY
469/*
470 * CARD_STATE_CMD
471 *
9fbab516 472 * Use: Sets the device's internal card state to enable, disable, or halt
b481de9c
ZY
473 *
474 * When in the 'enable' state the card operates as normal.
475 * When in the 'disable' state, the card enters into a low power mode.
476 * When in the 'halt' state, the card is shut down and must be fully
477 * restarted to come back on.
478 */
c79dd5b5 479static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
b481de9c 480{
857485c0 481 struct iwl_host_cmd cmd = {
b481de9c
ZY
482 .id = REPLY_CARD_STATE_CMD,
483 .len = sizeof(u32),
484 .data = &flags,
485 .meta.flags = meta_flag,
486 };
487
857485c0 488 return iwl_send_cmd(priv, &cmd);
b481de9c
ZY
489}
490
fcab423d 491static void iwl_clear_free_frames(struct iwl_priv *priv)
b481de9c
ZY
492{
493 struct list_head *element;
494
495 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
496 priv->frames_count);
497
498 while (!list_empty(&priv->free_frames)) {
499 element = priv->free_frames.next;
500 list_del(element);
fcab423d 501 kfree(list_entry(element, struct iwl_frame, list));
b481de9c
ZY
502 priv->frames_count--;
503 }
504
505 if (priv->frames_count) {
506 IWL_WARNING("%d frames still in use. Did we lose one?\n",
507 priv->frames_count);
508 priv->frames_count = 0;
509 }
510}
511
fcab423d 512static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
b481de9c 513{
fcab423d 514 struct iwl_frame *frame;
b481de9c
ZY
515 struct list_head *element;
516 if (list_empty(&priv->free_frames)) {
517 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
518 if (!frame) {
519 IWL_ERROR("Could not allocate frame!\n");
520 return NULL;
521 }
522
523 priv->frames_count++;
524 return frame;
525 }
526
527 element = priv->free_frames.next;
528 list_del(element);
fcab423d 529 return list_entry(element, struct iwl_frame, list);
b481de9c
ZY
530}
531
fcab423d 532static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
b481de9c
ZY
533{
534 memset(frame, 0, sizeof(*frame));
535 list_add(&frame->list, &priv->free_frames);
536}
537
c79dd5b5 538unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
b481de9c
ZY
539 struct ieee80211_hdr *hdr,
540 const u8 *dest, int left)
541{
542
3109ece1 543 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
b481de9c
ZY
544 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
545 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
546 return 0;
547
548 if (priv->ibss_beacon->len > left)
549 return 0;
550
551 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
552
553 return priv->ibss_beacon->len;
554}
555
39e88504 556static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
b481de9c 557{
39e88504
GC
558 int i;
559 int rate_mask;
560
561 /* Set rate mask*/
562 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
563 rate_mask = priv->active_rate_basic & 0xF;
564 else
565 rate_mask = priv->active_rate_basic & 0xFF0;
b481de9c 566
39e88504 567 /* Find lowest valid rate */
b481de9c 568 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
1826dcc0 569 i = iwl_rates[i].next_ieee) {
b481de9c 570 if (rate_mask & (1 << i))
1826dcc0 571 return iwl_rates[i].plcp;
b481de9c
ZY
572 }
573
39e88504
GC
574 /* No valid rate was found. Assign the lowest one */
575 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
576 return IWL_RATE_1M_PLCP;
577 else
578 return IWL_RATE_6M_PLCP;
b481de9c
ZY
579}
580
c79dd5b5 581static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
b481de9c 582{
fcab423d 583 struct iwl_frame *frame;
b481de9c
ZY
584 unsigned int frame_size;
585 int rc;
586 u8 rate;
587
fcab423d 588 frame = iwl_get_free_frame(priv);
b481de9c
ZY
589
590 if (!frame) {
591 IWL_ERROR("Could not obtain free frame buffer for beacon "
592 "command.\n");
593 return -ENOMEM;
594 }
595
39e88504 596 rate = iwl4965_rate_get_lowest_plcp(priv);
b481de9c 597
bb8c093b 598 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
b481de9c 599
857485c0 600 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
b481de9c
ZY
601 &frame->u.cmd[0]);
602
fcab423d 603 iwl_free_frame(priv, frame);
b481de9c
ZY
604
605 return rc;
606}
607
b481de9c
ZY
608/******************************************************************************
609 *
610 * Misc. internal state and helper functions
611 *
612 ******************************************************************************/
b481de9c 613
b481de9c 614/**
bb8c093b 615 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
b481de9c
ZY
616 *
617 * return : set the bit for each supported rate insert in ie
618 */
bb8c093b 619static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
c7c46676 620 u16 basic_rate, int *left)
b481de9c
ZY
621{
622 u16 ret_rates = 0, bit;
623 int i;
c7c46676
TW
624 u8 *cnt = ie;
625 u8 *rates = ie + 1;
b481de9c
ZY
626
627 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
628 if (bit & supported_rate) {
629 ret_rates |= bit;
1826dcc0 630 rates[*cnt] = iwl_rates[i].ieee |
c7c46676
TW
631 ((bit & basic_rate) ? 0x80 : 0x00);
632 (*cnt)++;
633 (*left)--;
634 if ((*left <= 0) ||
635 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
b481de9c
ZY
636 break;
637 }
638 }
639
640 return ret_rates;
641}
642
d1141dfb
EG
643#ifdef CONFIG_IWL4965_HT
644static void iwl4965_ht_conf(struct iwl_priv *priv,
645 struct ieee80211_bss_conf *bss_conf)
646{
647 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
648 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
649 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
650
651 IWL_DEBUG_MAC80211("enter: \n");
652
653 iwl_conf->is_ht = bss_conf->assoc_ht;
654
655 if (!iwl_conf->is_ht)
656 return;
657
658 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
659
660 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
a9841013 661 iwl_conf->sgf |= HT_SHORT_GI_20MHZ;
d1141dfb 662 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
a9841013 663 iwl_conf->sgf |= HT_SHORT_GI_40MHZ;
d1141dfb
EG
664
665 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
666 iwl_conf->max_amsdu_size =
667 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
668
669 iwl_conf->supported_chan_width =
670 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
671 iwl_conf->extension_chan_offset =
672 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
673 /* If no above or below channel supplied disable FAT channel */
674 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
675 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
676 iwl_conf->supported_chan_width = 0;
677
678 iwl_conf->tx_mimo_ps_mode =
679 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
680 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
681
682 iwl_conf->control_channel = ht_bss_conf->primary_channel;
683 iwl_conf->tx_chan_width =
684 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
685 iwl_conf->ht_protection =
686 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
687 iwl_conf->non_GF_STA_present =
688 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
689
690 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
691 IWL_DEBUG_MAC80211("leave\n");
692}
693
694static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
695 u8 *pos, int *left)
696{
697 struct ieee80211_ht_cap *ht_cap;
698
699 if (!sband || !sband->ht_info.ht_supported)
700 return;
701
702 if (*left < sizeof(struct ieee80211_ht_cap))
703 return;
704
705 *pos++ = sizeof(struct ieee80211_ht_cap);
706 ht_cap = (struct ieee80211_ht_cap *) pos;
707
708 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
709 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
710 ht_cap->ampdu_params_info =
711 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
712 ((sband->ht_info.ampdu_density << 2) &
713 IEEE80211_HT_CAP_AMPDU_DENSITY);
714 *left -= sizeof(struct ieee80211_ht_cap);
715}
716#else
717static inline void iwl4965_ht_conf(struct iwl_priv *priv,
718 struct ieee80211_bss_conf *bss_conf)
719{
720}
721static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
722 u8 *pos, int *left)
723{
724}
725#endif
726
727
b481de9c 728/**
bb8c093b 729 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
b481de9c 730 */
c79dd5b5 731static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
78330fdd
TW
732 enum ieee80211_band band,
733 struct ieee80211_mgmt *frame,
734 int left, int is_direct)
b481de9c
ZY
735{
736 int len = 0;
737 u8 *pos = NULL;
bee488db 738 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
78330fdd 739 const struct ieee80211_supported_band *sband =
d1141dfb 740 iwl_get_hw_mode(priv, band);
b481de9c
ZY
741
742 /* Make sure there is enough space for the probe request,
743 * two mandatory IEs and the data */
744 left -= 24;
745 if (left < 0)
746 return 0;
747 len += 24;
748
749 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
57bd1bea 750 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
b481de9c 751 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
57bd1bea 752 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
b481de9c
ZY
753 frame->seq_ctrl = 0;
754
755 /* fill in our indirect SSID IE */
756 /* ...next IE... */
757
758 left -= 2;
759 if (left < 0)
760 return 0;
761 len += 2;
762 pos = &(frame->u.probe_req.variable[0]);
763 *pos++ = WLAN_EID_SSID;
764 *pos++ = 0;
765
766 /* fill in our direct SSID IE... */
767 if (is_direct) {
768 /* ...next IE... */
769 left -= 2 + priv->essid_len;
770 if (left < 0)
771 return 0;
772 /* ... fill it in... */
773 *pos++ = WLAN_EID_SSID;
774 *pos++ = priv->essid_len;
775 memcpy(pos, priv->essid, priv->essid_len);
776 pos += priv->essid_len;
777 len += 2 + priv->essid_len;
778 }
779
780 /* fill in supported rate */
781 /* ...next IE... */
782 left -= 2;
783 if (left < 0)
784 return 0;
c7c46676 785
b481de9c
ZY
786 /* ... fill it in... */
787 *pos++ = WLAN_EID_SUPP_RATES;
788 *pos = 0;
c7c46676 789
bee488db 790 /* exclude 60M rate */
791 active_rates = priv->rates_mask;
792 active_rates &= ~IWL_RATE_60M_MASK;
793
794 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
b481de9c 795
c7c46676 796 cck_rates = IWL_CCK_RATES_MASK & active_rates;
bb8c093b 797 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
bee488db 798 active_rate_basic, &left);
c7c46676
TW
799 active_rates &= ~ret_rates;
800
bb8c093b 801 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
bee488db 802 active_rate_basic, &left);
c7c46676
TW
803 active_rates &= ~ret_rates;
804
b481de9c
ZY
805 len += 2 + *pos;
806 pos += (*pos) + 1;
c7c46676 807 if (active_rates == 0)
b481de9c
ZY
808 goto fill_end;
809
810 /* fill in supported extended rate */
811 /* ...next IE... */
812 left -= 2;
813 if (left < 0)
814 return 0;
815 /* ... fill it in... */
816 *pos++ = WLAN_EID_EXT_SUPP_RATES;
817 *pos = 0;
bb8c093b 818 iwl4965_supported_rate_to_ie(pos, active_rates,
bee488db 819 active_rate_basic, &left);
b481de9c
ZY
820 if (*pos > 0)
821 len += 2 + *pos;
822
b481de9c 823 fill_end:
d1141dfb
EG
824 /* fill in HT IE */
825 left -= 2;
826 if (left < 0)
827 return 0;
828
829 *pos++ = WLAN_EID_HT_CAPABILITY;
830 *pos = 0;
831
832 iwl_ht_cap_to_ie(sband, pos, &left);
833
834 if (*pos > 0)
835 len += 2 + *pos;
b481de9c
ZY
836 return (u16)len;
837}
838
839/*
840 * QoS support
841*/
c79dd5b5 842static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
bb8c093b 843 struct iwl4965_qosparam_cmd *qos)
b481de9c
ZY
844{
845
857485c0 846 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
bb8c093b 847 sizeof(struct iwl4965_qosparam_cmd), qos);
b481de9c
ZY
848}
849
c79dd5b5 850static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
b481de9c
ZY
851{
852 unsigned long flags;
853
b481de9c
ZY
854 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
855 return;
856
857 if (!priv->qos_data.qos_enable)
858 return;
859
860 spin_lock_irqsave(&priv->lock, flags);
861 priv->qos_data.def_qos_parm.qos_flags = 0;
862
863 if (priv->qos_data.qos_cap.q_AP.queue_request &&
864 !priv->qos_data.qos_cap.q_AP.txop_request)
865 priv->qos_data.def_qos_parm.qos_flags |=
866 QOS_PARAM_FLG_TXOP_TYPE_MSK;
b481de9c
ZY
867 if (priv->qos_data.qos_active)
868 priv->qos_data.def_qos_parm.qos_flags |=
869 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
870
c8b0e6e1 871#ifdef CONFIG_IWL4965_HT
fd105e79 872 if (priv->current_ht_config.is_ht)
f1f1f5c7 873 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
c8b0e6e1 874#endif /* CONFIG_IWL4965_HT */
f1f1f5c7 875
b481de9c
ZY
876 spin_unlock_irqrestore(&priv->lock, flags);
877
3109ece1 878 if (force || iwl_is_associated(priv)) {
f1f1f5c7
TW
879 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
880 priv->qos_data.qos_active,
881 priv->qos_data.def_qos_parm.qos_flags);
b481de9c 882
bb8c093b 883 iwl4965_send_qos_params_command(priv,
b481de9c
ZY
884 &(priv->qos_data.def_qos_parm));
885 }
886}
887
c79dd5b5 888int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
b481de9c
ZY
889{
890 /* Filter incoming packets to determine if they are targeted toward
891 * this network, discarding packets coming from ourselves */
892 switch (priv->iw_mode) {
893 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
894 /* packets from our adapter are dropped (echo) */
895 if (!compare_ether_addr(header->addr2, priv->mac_addr))
896 return 0;
897 /* {broad,multi}cast packets to our IBSS go through */
898 if (is_multicast_ether_addr(header->addr1))
899 return !compare_ether_addr(header->addr3, priv->bssid);
900 /* packets to our adapter go through */
901 return !compare_ether_addr(header->addr1, priv->mac_addr);
902 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
903 /* packets from our adapter are dropped (echo) */
904 if (!compare_ether_addr(header->addr3, priv->mac_addr))
905 return 0;
906 /* {broad,multi}cast packets to our BSS go through */
907 if (is_multicast_ether_addr(header->addr1))
908 return !compare_ether_addr(header->addr2, priv->bssid);
909 /* packets to our adapter go through */
910 return !compare_ether_addr(header->addr1, priv->mac_addr);
69dc5d9d
TW
911 default:
912 break;
b481de9c
ZY
913 }
914
915 return 1;
916}
917
b481de9c 918
b481de9c
ZY
919
920/**
bb8c093b 921 * iwl4965_scan_cancel - Cancel any currently executing HW scan
b481de9c
ZY
922 *
923 * NOTE: priv->mutex is not required before calling this function
924 */
c79dd5b5 925static int iwl4965_scan_cancel(struct iwl_priv *priv)
b481de9c
ZY
926{
927 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
928 clear_bit(STATUS_SCANNING, &priv->status);
929 return 0;
930 }
931
932 if (test_bit(STATUS_SCANNING, &priv->status)) {
933 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
934 IWL_DEBUG_SCAN("Queuing scan abort.\n");
935 set_bit(STATUS_SCAN_ABORTING, &priv->status);
936 queue_work(priv->workqueue, &priv->abort_scan);
937
938 } else
939 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
940
941 return test_bit(STATUS_SCANNING, &priv->status);
942 }
943
944 return 0;
945}
946
947/**
bb8c093b 948 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
b481de9c
ZY
949 * @ms: amount of time to wait (in milliseconds) for scan to abort
950 *
951 * NOTE: priv->mutex must be held before calling this function
952 */
c79dd5b5 953static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
b481de9c
ZY
954{
955 unsigned long now = jiffies;
956 int ret;
957
bb8c093b 958 ret = iwl4965_scan_cancel(priv);
b481de9c
ZY
959 if (ret && ms) {
960 mutex_unlock(&priv->mutex);
961 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
962 test_bit(STATUS_SCANNING, &priv->status))
963 msleep(1);
964 mutex_lock(&priv->mutex);
965
966 return test_bit(STATUS_SCANNING, &priv->status);
967 }
968
969 return ret;
970}
971
c79dd5b5 972static void iwl4965_sequence_reset(struct iwl_priv *priv)
b481de9c
ZY
973{
974 /* Reset ieee stats */
975
976 /* We don't reset the net_device_stats (ieee->stats) on
977 * re-association */
978
979 priv->last_seq_num = -1;
980 priv->last_frag_num = -1;
981 priv->last_packet_time = 0;
982
bb8c093b 983 iwl4965_scan_cancel(priv);
b481de9c
ZY
984}
985
986#define MAX_UCODE_BEACON_INTERVAL 4096
987#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
988
bb8c093b 989static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
b481de9c
ZY
990{
991 u16 new_val = 0;
992 u16 beacon_factor = 0;
993
994 beacon_factor =
995 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
996 / MAX_UCODE_BEACON_INTERVAL;
997 new_val = beacon_val / beacon_factor;
998
999 return cpu_to_le16(new_val);
1000}
1001
c79dd5b5 1002static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
b481de9c
ZY
1003{
1004 u64 interval_tm_unit;
1005 u64 tsf, result;
1006 unsigned long flags;
1007 struct ieee80211_conf *conf = NULL;
1008 u16 beacon_int = 0;
1009
1010 conf = ieee80211_get_hw_conf(priv->hw);
1011
1012 spin_lock_irqsave(&priv->lock, flags);
3109ece1
TW
1013 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1014 priv->rxon_timing.timestamp.dw[0] =
1015 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
b481de9c
ZY
1016
1017 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1018
3109ece1 1019 tsf = priv->timestamp;
b481de9c
ZY
1020
1021 beacon_int = priv->beacon_int;
1022 spin_unlock_irqrestore(&priv->lock, flags);
1023
1024 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1025 if (beacon_int == 0) {
1026 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1027 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1028 } else {
1029 priv->rxon_timing.beacon_interval =
1030 cpu_to_le16(beacon_int);
1031 priv->rxon_timing.beacon_interval =
bb8c093b 1032 iwl4965_adjust_beacon_interval(
b481de9c
ZY
1033 le16_to_cpu(priv->rxon_timing.beacon_interval));
1034 }
1035
1036 priv->rxon_timing.atim_window = 0;
1037 } else {
1038 priv->rxon_timing.beacon_interval =
bb8c093b 1039 iwl4965_adjust_beacon_interval(conf->beacon_int);
b481de9c
ZY
1040 /* TODO: we need to get atim_window from upper stack
1041 * for now we set to 0 */
1042 priv->rxon_timing.atim_window = 0;
1043 }
1044
1045 interval_tm_unit =
1046 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1047 result = do_div(tsf, interval_tm_unit);
1048 priv->rxon_timing.beacon_init_val =
1049 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1050
1051 IWL_DEBUG_ASSOC
1052 ("beacon interval %d beacon timer %d beacon tim %d\n",
1053 le16_to_cpu(priv->rxon_timing.beacon_interval),
1054 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1055 le16_to_cpu(priv->rxon_timing.atim_window));
1056}
1057
c79dd5b5 1058static int iwl4965_scan_initiate(struct iwl_priv *priv)
b481de9c
ZY
1059{
1060 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1061 IWL_ERROR("APs don't scan.\n");
1062 return 0;
1063 }
1064
fee1247a 1065 if (!iwl_is_ready_rf(priv)) {
b481de9c
ZY
1066 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1067 return -EIO;
1068 }
1069
1070 if (test_bit(STATUS_SCANNING, &priv->status)) {
1071 IWL_DEBUG_SCAN("Scan already in progress.\n");
1072 return -EAGAIN;
1073 }
1074
1075 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1076 IWL_DEBUG_SCAN("Scan request while abort pending. "
1077 "Queuing.\n");
1078 return -EAGAIN;
1079 }
1080
1081 IWL_DEBUG_INFO("Starting scan...\n");
1082 priv->scan_bands = 2;
1083 set_bit(STATUS_SCANNING, &priv->status);
1084 priv->scan_start = jiffies;
1085 priv->scan_pass_start = priv->scan_start;
1086
1087 queue_work(priv->workqueue, &priv->request_scan);
1088
1089 return 0;
1090}
1091
b481de9c 1092
c79dd5b5 1093static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
8318d78a 1094 enum ieee80211_band band)
b481de9c 1095{
8318d78a 1096 if (band == IEEE80211_BAND_5GHZ) {
b481de9c
ZY
1097 priv->staging_rxon.flags &=
1098 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1099 | RXON_FLG_CCK_MSK);
1100 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1101 } else {
508e32e1 1102 /* Copied from iwl4965_post_associate() */
b481de9c
ZY
1103 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1104 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1105 else
1106 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1107
1108 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1109 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1110
1111 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1112 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1113 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1114 }
1115}
1116
1117/*
01ebd063 1118 * initialize rxon structure with default values from eeprom
b481de9c 1119 */
c79dd5b5 1120static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
b481de9c 1121{
bf85ea4f 1122 const struct iwl_channel_info *ch_info;
b481de9c
ZY
1123
1124 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1125
1126 switch (priv->iw_mode) {
1127 case IEEE80211_IF_TYPE_AP:
1128 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1129 break;
1130
1131 case IEEE80211_IF_TYPE_STA:
1132 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1133 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1134 break;
1135
1136 case IEEE80211_IF_TYPE_IBSS:
1137 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1138 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1139 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1140 RXON_FILTER_ACCEPT_GRP_MSK;
1141 break;
1142
1143 case IEEE80211_IF_TYPE_MNTR:
1144 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1145 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1146 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1147 break;
69dc5d9d
TW
1148 default:
1149 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1150 break;
b481de9c
ZY
1151 }
1152
1153#if 0
1154 /* TODO: Figure out when short_preamble would be set and cache from
1155 * that */
1156 if (!hw_to_local(priv->hw)->short_preamble)
1157 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1158 else
1159 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1160#endif
1161
8622e705 1162 ch_info = iwl_get_channel_info(priv, priv->band,
b481de9c
ZY
1163 le16_to_cpu(priv->staging_rxon.channel));
1164
1165 if (!ch_info)
1166 ch_info = &priv->channel_info[0];
1167
1168 /*
1169 * in some case A channels are all non IBSS
1170 * in this case force B/G channel
1171 */
1172 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1173 !(is_channel_ibss(ch_info)))
1174 ch_info = &priv->channel_info[0];
1175
1176 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
8318d78a 1177 priv->band = ch_info->band;
b481de9c 1178
8318d78a 1179 iwl4965_set_flags_for_phymode(priv, priv->band);
b481de9c
ZY
1180
1181 priv->staging_rxon.ofdm_basic_rates =
1182 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1183 priv->staging_rxon.cck_basic_rates =
1184 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1185
1186 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1187 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1188 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1189 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1190 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1191 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
c7de35cd 1192 iwl_set_rxon_chain(priv);
b481de9c
ZY
1193}
1194
c79dd5b5 1195static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
b481de9c 1196{
b481de9c 1197 if (mode == IEEE80211_IF_TYPE_IBSS) {
bf85ea4f 1198 const struct iwl_channel_info *ch_info;
b481de9c 1199
8622e705 1200 ch_info = iwl_get_channel_info(priv,
8318d78a 1201 priv->band,
b481de9c
ZY
1202 le16_to_cpu(priv->staging_rxon.channel));
1203
1204 if (!ch_info || !is_channel_ibss(ch_info)) {
1205 IWL_ERROR("channel %d not IBSS channel\n",
1206 le16_to_cpu(priv->staging_rxon.channel));
1207 return -EINVAL;
1208 }
1209 }
1210
b481de9c
ZY
1211 priv->iw_mode = mode;
1212
bb8c093b 1213 iwl4965_connection_init_rx_config(priv);
b481de9c
ZY
1214 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1215
bf85ea4f 1216 iwlcore_clear_stations_table(priv);
b481de9c 1217
fde3571f 1218 /* dont commit rxon if rf-kill is on*/
fee1247a 1219 if (!iwl_is_ready_rf(priv))
fde3571f
MA
1220 return -EAGAIN;
1221
1222 cancel_delayed_work(&priv->scan_check);
1223 if (iwl4965_scan_cancel_timeout(priv, 100)) {
1224 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1225 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1226 return -EAGAIN;
1227 }
1228
bb8c093b 1229 iwl4965_commit_rxon(priv);
b481de9c
ZY
1230
1231 return 0;
1232}
1233
c79dd5b5 1234static void iwl4965_set_rate(struct iwl_priv *priv)
b481de9c 1235{
8318d78a 1236 const struct ieee80211_supported_band *hw = NULL;
b481de9c
ZY
1237 struct ieee80211_rate *rate;
1238 int i;
1239
d1141dfb 1240 hw = iwl_get_hw_mode(priv, priv->band);
c4ba9621
SA
1241 if (!hw) {
1242 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
1243 return;
1244 }
b481de9c
ZY
1245
1246 priv->active_rate = 0;
1247 priv->active_rate_basic = 0;
1248
8318d78a
JB
1249 for (i = 0; i < hw->n_bitrates; i++) {
1250 rate = &(hw->bitrates[i]);
1251 if (rate->hw_value < IWL_RATE_COUNT)
1252 priv->active_rate |= (1 << rate->hw_value);
b481de9c
ZY
1253 }
1254
1255 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
1256 priv->active_rate, priv->active_rate_basic);
1257
1258 /*
1259 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
1260 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
1261 * OFDM
1262 */
1263 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
1264 priv->staging_rxon.cck_basic_rates =
1265 ((priv->active_rate_basic &
1266 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
1267 else
1268 priv->staging_rxon.cck_basic_rates =
1269 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1270
1271 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
1272 priv->staging_rxon.ofdm_basic_rates =
1273 ((priv->active_rate_basic &
1274 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
1275 IWL_FIRST_OFDM_RATE) & 0xFF;
1276 else
1277 priv->staging_rxon.ofdm_basic_rates =
1278 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1279}
1280
ad97edd2 1281void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
b481de9c
ZY
1282{
1283 unsigned long flags;
1284
1285 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
1286 return;
1287
1288 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
1289 disable_radio ? "OFF" : "ON");
1290
1291 if (disable_radio) {
bb8c093b 1292 iwl4965_scan_cancel(priv);
b481de9c
ZY
1293 /* FIXME: This is a workaround for AP */
1294 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
1295 spin_lock_irqsave(&priv->lock, flags);
3395f6e9 1296 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
b481de9c
ZY
1297 CSR_UCODE_SW_BIT_RFKILL);
1298 spin_unlock_irqrestore(&priv->lock, flags);
ad97edd2 1299 /* call the host command only if no hw rf-kill set */
59003835
MA
1300 if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
1301 iwl_is_ready(priv))
ad97edd2
MA
1302 iwl4965_send_card_state(priv,
1303 CARD_STATE_CMD_DISABLE,
1304 0);
b481de9c 1305 set_bit(STATUS_RF_KILL_SW, &priv->status);
ad97edd2
MA
1306
1307 /* make sure mac80211 stop sending Tx frame */
1308 if (priv->mac80211_registered)
1309 ieee80211_stop_queues(priv->hw);
b481de9c
ZY
1310 }
1311 return;
1312 }
1313
1314 spin_lock_irqsave(&priv->lock, flags);
3395f6e9 1315 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
b481de9c
ZY
1316
1317 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1318 spin_unlock_irqrestore(&priv->lock, flags);
1319
1320 /* wake up ucode */
1321 msleep(10);
1322
1323 spin_lock_irqsave(&priv->lock, flags);
3395f6e9
TW
1324 iwl_read32(priv, CSR_UCODE_DRV_GP1);
1325 if (!iwl_grab_nic_access(priv))
1326 iwl_release_nic_access(priv);
b481de9c
ZY
1327 spin_unlock_irqrestore(&priv->lock, flags);
1328
1329 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
1330 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
1331 "disabled by HW switch\n");
1332 return;
1333 }
1334
1335 queue_work(priv->workqueue, &priv->restart);
1336 return;
1337}
1338
b481de9c
ZY
1339#define IWL_PACKET_RETRY_TIME HZ
1340
c79dd5b5 1341int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
b481de9c
ZY
1342{
1343 u16 sc = le16_to_cpu(header->seq_ctrl);
1344 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1345 u16 frag = sc & IEEE80211_SCTL_FRAG;
1346 u16 *last_seq, *last_frag;
1347 unsigned long *last_time;
1348
1349 switch (priv->iw_mode) {
1350 case IEEE80211_IF_TYPE_IBSS:{
1351 struct list_head *p;
bb8c093b 1352 struct iwl4965_ibss_seq *entry = NULL;
b481de9c
ZY
1353 u8 *mac = header->addr2;
1354 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
1355
1356 __list_for_each(p, &priv->ibss_mac_hash[index]) {
bb8c093b 1357 entry = list_entry(p, struct iwl4965_ibss_seq, list);
b481de9c
ZY
1358 if (!compare_ether_addr(entry->mac, mac))
1359 break;
1360 }
1361 if (p == &priv->ibss_mac_hash[index]) {
1362 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1363 if (!entry) {
bc434dd2 1364 IWL_ERROR("Cannot malloc new mac entry\n");
b481de9c
ZY
1365 return 0;
1366 }
1367 memcpy(entry->mac, mac, ETH_ALEN);
1368 entry->seq_num = seq;
1369 entry->frag_num = frag;
1370 entry->packet_time = jiffies;
bc434dd2 1371 list_add(&entry->list, &priv->ibss_mac_hash[index]);
b481de9c
ZY
1372 return 0;
1373 }
1374 last_seq = &entry->seq_num;
1375 last_frag = &entry->frag_num;
1376 last_time = &entry->packet_time;
1377 break;
1378 }
1379 case IEEE80211_IF_TYPE_STA:
1380 last_seq = &priv->last_seq_num;
1381 last_frag = &priv->last_frag_num;
1382 last_time = &priv->last_packet_time;
1383 break;
1384 default:
1385 return 0;
1386 }
1387 if ((*last_seq == seq) &&
1388 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
1389 if (*last_frag == frag)
1390 goto drop;
1391 if (*last_frag + 1 != frag)
1392 /* out-of-order fragment */
1393 goto drop;
1394 } else
1395 *last_seq = seq;
1396
1397 *last_frag = frag;
1398 *last_time = jiffies;
1399 return 0;
1400
1401 drop:
1402 return 1;
1403}
1404
c8b0e6e1 1405#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
b481de9c
ZY
1406
1407#include "iwl-spectrum.h"
1408
1409#define BEACON_TIME_MASK_LOW 0x00FFFFFF
1410#define BEACON_TIME_MASK_HIGH 0xFF000000
1411#define TIME_UNIT 1024
1412
1413/*
1414 * extended beacon time format
1415 * time in usec will be changed into a 32-bit value in 8:24 format
1416 * the high 1 byte is the beacon counts
1417 * the lower 3 bytes is the time in usec within one beacon interval
1418 */
1419
bb8c093b 1420static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
b481de9c
ZY
1421{
1422 u32 quot;
1423 u32 rem;
1424 u32 interval = beacon_interval * 1024;
1425
1426 if (!interval || !usec)
1427 return 0;
1428
1429 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
1430 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
1431
1432 return (quot << 24) + rem;
1433}
1434
1435/* base is usually what we get from ucode with each received frame,
1436 * the same as HW timer counter counting down
1437 */
1438
bb8c093b 1439static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
b481de9c
ZY
1440{
1441 u32 base_low = base & BEACON_TIME_MASK_LOW;
1442 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
1443 u32 interval = beacon_interval * TIME_UNIT;
1444 u32 res = (base & BEACON_TIME_MASK_HIGH) +
1445 (addon & BEACON_TIME_MASK_HIGH);
1446
1447 if (base_low > addon_low)
1448 res += base_low - addon_low;
1449 else if (base_low < addon_low) {
1450 res += interval + base_low - addon_low;
1451 res += (1 << 24);
1452 } else
1453 res += (1 << 24);
1454
1455 return cpu_to_le32(res);
1456}
1457
c79dd5b5 1458static int iwl4965_get_measurement(struct iwl_priv *priv,
b481de9c
ZY
1459 struct ieee80211_measurement_params *params,
1460 u8 type)
1461{
bb8c093b 1462 struct iwl4965_spectrum_cmd spectrum;
db11d634 1463 struct iwl_rx_packet *res;
857485c0 1464 struct iwl_host_cmd cmd = {
b481de9c
ZY
1465 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
1466 .data = (void *)&spectrum,
1467 .meta.flags = CMD_WANT_SKB,
1468 };
1469 u32 add_time = le64_to_cpu(params->start_time);
1470 int rc;
1471 int spectrum_resp_status;
1472 int duration = le16_to_cpu(params->duration);
1473
3109ece1 1474 if (iwl_is_associated(priv))
b481de9c 1475 add_time =
bb8c093b 1476 iwl4965_usecs_to_beacons(
b481de9c
ZY
1477 le64_to_cpu(params->start_time) - priv->last_tsf,
1478 le16_to_cpu(priv->rxon_timing.beacon_interval));
1479
1480 memset(&spectrum, 0, sizeof(spectrum));
1481
1482 spectrum.channel_count = cpu_to_le16(1);
1483 spectrum.flags =
1484 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
1485 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
1486 cmd.len = sizeof(spectrum);
1487 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
1488
3109ece1 1489 if (iwl_is_associated(priv))
b481de9c 1490 spectrum.start_time =
bb8c093b 1491 iwl4965_add_beacon_time(priv->last_beacon_time,
b481de9c
ZY
1492 add_time,
1493 le16_to_cpu(priv->rxon_timing.beacon_interval));
1494 else
1495 spectrum.start_time = 0;
1496
1497 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
1498 spectrum.channels[0].channel = params->channel;
1499 spectrum.channels[0].type = type;
1500 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
1501 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
1502 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
1503
857485c0 1504 rc = iwl_send_cmd_sync(priv, &cmd);
b481de9c
ZY
1505 if (rc)
1506 return rc;
1507
db11d634 1508 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
b481de9c
ZY
1509 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1510 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
1511 rc = -EIO;
1512 }
1513
1514 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
1515 switch (spectrum_resp_status) {
1516 case 0: /* Command will be handled */
1517 if (res->u.spectrum.id != 0xff) {
1518 IWL_DEBUG_INFO
1519 ("Replaced existing measurement: %d\n",
1520 res->u.spectrum.id);
1521 priv->measurement_status &= ~MEASUREMENT_READY;
1522 }
1523 priv->measurement_status |= MEASUREMENT_ACTIVE;
1524 rc = 0;
1525 break;
1526
1527 case 1: /* Command will not be handled */
1528 rc = -EAGAIN;
1529 break;
1530 }
1531
1532 dev_kfree_skb_any(cmd.meta.u.skb);
1533
1534 return rc;
1535}
1536#endif
1537
c79dd5b5 1538static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
8567c63e 1539 struct iwl_tx_info *tx_sta)
b481de9c 1540{
e039fa4a 1541 ieee80211_tx_status_irqsafe(priv->hw, tx_sta->skb[0]);
b481de9c
ZY
1542 tx_sta->skb[0] = NULL;
1543}
1544
1545/**
6440adb5 1546 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
b481de9c 1547 *
6440adb5
CB
1548 * When FW advances 'R' index, all entries between old and new 'R' index
1549 * need to be reclaimed. As result, some free space forms. If there is
1550 * enough free space (> low mark), wake the stack that feeds us.
b481de9c 1551 */
c79dd5b5 1552int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
b481de9c 1553{
16466903 1554 struct iwl_tx_queue *txq = &priv->txq[txq_id];
443cfd45 1555 struct iwl_queue *q = &txq->q;
b481de9c
ZY
1556 int nfreed = 0;
1557
443cfd45 1558 if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
b481de9c
ZY
1559 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
1560 "is out of range [0-%d] %d %d.\n", txq_id,
fc4b6853 1561 index, q->n_bd, q->write_ptr, q->read_ptr);
b481de9c
ZY
1562 return 0;
1563 }
1564
c54b679d 1565 for (index = iwl_queue_inc_wrap(index, q->n_bd);
fc4b6853 1566 q->read_ptr != index;
c54b679d 1567 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
b481de9c 1568 if (txq_id != IWL_CMD_QUEUE_NUM) {
bb8c093b 1569 iwl4965_txstatus_to_ieee(priv,
fc4b6853 1570 &(txq->txb[txq->q.read_ptr]));
1053d35f 1571 iwl_hw_txq_free_tfd(priv, txq);
b481de9c
ZY
1572 } else if (nfreed > 1) {
1573 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
fc4b6853 1574 q->write_ptr, q->read_ptr);
b481de9c
ZY
1575 queue_work(priv->workqueue, &priv->restart);
1576 }
1577 nfreed++;
1578 }
1579
b481de9c
ZY
1580 return nfreed;
1581}
1582
b481de9c
ZY
1583/******************************************************************************
1584 *
1585 * Generic RX handler implementations
1586 *
1587 ******************************************************************************/
885ba202
TW
1588static void iwl_rx_reply_alive(struct iwl_priv *priv,
1589 struct iwl_rx_mem_buffer *rxb)
b481de9c 1590{
db11d634 1591 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
885ba202 1592 struct iwl_alive_resp *palive;
b481de9c
ZY
1593 struct delayed_work *pwork;
1594
1595 palive = &pkt->u.alive_frame;
1596
1597 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
1598 "0x%01X 0x%01X\n",
1599 palive->is_valid, palive->ver_type,
1600 palive->ver_subtype);
1601
1602 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
1603 IWL_DEBUG_INFO("Initialization Alive received.\n");
1604 memcpy(&priv->card_alive_init,
1605 &pkt->u.alive_frame,
885ba202 1606 sizeof(struct iwl_init_alive_resp));
b481de9c
ZY
1607 pwork = &priv->init_alive_start;
1608 } else {
1609 IWL_DEBUG_INFO("Runtime Alive received.\n");
1610 memcpy(&priv->card_alive, &pkt->u.alive_frame,
885ba202 1611 sizeof(struct iwl_alive_resp));
b481de9c
ZY
1612 pwork = &priv->alive_start;
1613 }
1614
1615 /* We delay the ALIVE response by 5ms to
1616 * give the HW RF Kill time to activate... */
1617 if (palive->is_valid == UCODE_VALID_OK)
1618 queue_delayed_work(priv->workqueue, pwork,
1619 msecs_to_jiffies(5));
1620 else
1621 IWL_WARNING("uCode did not respond OK.\n");
1622}
1623
c79dd5b5 1624static void iwl4965_rx_reply_error(struct iwl_priv *priv,
a55360e4 1625 struct iwl_rx_mem_buffer *rxb)
b481de9c 1626{
db11d634 1627 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
b481de9c
ZY
1628
1629 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
1630 "seq 0x%04X ser 0x%08X\n",
1631 le32_to_cpu(pkt->u.err_resp.error_type),
1632 get_cmd_string(pkt->u.err_resp.cmd_id),
1633 pkt->u.err_resp.cmd_id,
1634 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1635 le32_to_cpu(pkt->u.err_resp.error_info));
1636}
1637
1638#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1639
a55360e4 1640static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
b481de9c 1641{
db11d634 1642 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
c1adf9fb 1643 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
bb8c093b 1644 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
b481de9c
ZY
1645 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
1646 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
1647 rxon->channel = csa->channel;
1648 priv->staging_rxon.channel = csa->channel;
1649}
1650
c79dd5b5 1651static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
a55360e4 1652 struct iwl_rx_mem_buffer *rxb)
b481de9c 1653{
c8b0e6e1 1654#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
db11d634 1655 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
bb8c093b 1656 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
b481de9c
ZY
1657
1658 if (!report->state) {
f3d67999
EK
1659 IWL_DEBUG(IWL_DL_11H,
1660 "Spectrum Measure Notification: Start\n");
b481de9c
ZY
1661 return;
1662 }
1663
1664 memcpy(&priv->measure_report, report, sizeof(*report));
1665 priv->measurement_status |= MEASUREMENT_READY;
1666#endif
1667}
1668
c79dd5b5 1669static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
a55360e4 1670 struct iwl_rx_mem_buffer *rxb)
b481de9c 1671{
0a6857e7 1672#ifdef CONFIG_IWLWIFI_DEBUG
db11d634 1673 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
bb8c093b 1674 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
b481de9c
ZY
1675 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
1676 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1677#endif
1678}
1679
c79dd5b5 1680static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
a55360e4 1681 struct iwl_rx_mem_buffer *rxb)
b481de9c 1682{
db11d634 1683 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
b481de9c
ZY
1684 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
1685 "notification for %s:\n",
1686 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
bf403db8 1687 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
b481de9c
ZY
1688}
1689
bb8c093b 1690static void iwl4965_bg_beacon_update(struct work_struct *work)
b481de9c 1691{
c79dd5b5
TW
1692 struct iwl_priv *priv =
1693 container_of(work, struct iwl_priv, beacon_update);
b481de9c
ZY
1694 struct sk_buff *beacon;
1695
1696 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
e039fa4a 1697 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
b481de9c
ZY
1698
1699 if (!beacon) {
1700 IWL_ERROR("update beacon failed\n");
1701 return;
1702 }
1703
1704 mutex_lock(&priv->mutex);
1705 /* new beacon skb is allocated every time; dispose previous.*/
1706 if (priv->ibss_beacon)
1707 dev_kfree_skb(priv->ibss_beacon);
1708
1709 priv->ibss_beacon = beacon;
1710 mutex_unlock(&priv->mutex);
1711
bb8c093b 1712 iwl4965_send_beacon_cmd(priv);
b481de9c
ZY
1713}
1714
c79dd5b5 1715static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
a55360e4 1716 struct iwl_rx_mem_buffer *rxb)
b481de9c 1717{
0a6857e7 1718#ifdef CONFIG_IWLWIFI_DEBUG
db11d634 1719 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
bb8c093b
CH
1720 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
1721 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
b481de9c
ZY
1722
1723 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
1724 "tsf %d %d rate %d\n",
1725 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
1726 beacon->beacon_notify_hdr.failure_frame,
1727 le32_to_cpu(beacon->ibss_mgr_status),
1728 le32_to_cpu(beacon->high_tsf),
1729 le32_to_cpu(beacon->low_tsf), rate);
1730#endif
1731
1732 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
1733 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
1734 queue_work(priv->workqueue, &priv->beacon_update);
1735}
1736
1737/* Service response to REPLY_SCAN_CMD (0x80) */
c79dd5b5 1738static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
a55360e4 1739 struct iwl_rx_mem_buffer *rxb)
b481de9c 1740{
0a6857e7 1741#ifdef CONFIG_IWLWIFI_DEBUG
db11d634 1742 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
bb8c093b
CH
1743 struct iwl4965_scanreq_notification *notif =
1744 (struct iwl4965_scanreq_notification *)pkt->u.raw;
b481de9c
ZY
1745
1746 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
1747#endif
1748}
1749
1750/* Service SCAN_START_NOTIFICATION (0x82) */
c79dd5b5 1751static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
a55360e4 1752 struct iwl_rx_mem_buffer *rxb)
b481de9c 1753{
db11d634 1754 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
bb8c093b
CH
1755 struct iwl4965_scanstart_notification *notif =
1756 (struct iwl4965_scanstart_notification *)pkt->u.raw;
b481de9c
ZY
1757 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
1758 IWL_DEBUG_SCAN("Scan start: "
1759 "%d [802.11%s] "
1760 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
1761 notif->channel,
1762 notif->band ? "bg" : "a",
1763 notif->tsf_high,
1764 notif->tsf_low, notif->status, notif->beacon_timer);
1765}
1766
1767/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
c79dd5b5 1768static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
a55360e4 1769 struct iwl_rx_mem_buffer *rxb)
b481de9c 1770{
db11d634 1771 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
bb8c093b
CH
1772 struct iwl4965_scanresults_notification *notif =
1773 (struct iwl4965_scanresults_notification *)pkt->u.raw;
b481de9c
ZY
1774
1775 IWL_DEBUG_SCAN("Scan ch.res: "
1776 "%d [802.11%s] "
1777 "(TSF: 0x%08X:%08X) - %d "
1778 "elapsed=%lu usec (%dms since last)\n",
1779 notif->channel,
1780 notif->band ? "bg" : "a",
1781 le32_to_cpu(notif->tsf_high),
1782 le32_to_cpu(notif->tsf_low),
1783 le32_to_cpu(notif->statistics[0]),
1784 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
1785 jiffies_to_msecs(elapsed_jiffies
1786 (priv->last_scan_jiffies, jiffies)));
1787
1788 priv->last_scan_jiffies = jiffies;
7878a5a4 1789 priv->next_scan_jiffies = 0;
b481de9c
ZY
1790}
1791
1792/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
c79dd5b5 1793static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
a55360e4 1794 struct iwl_rx_mem_buffer *rxb)
b481de9c 1795{
db11d634 1796 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
bb8c093b 1797 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
b481de9c
ZY
1798
1799 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
1800 scan_notif->scanned_channels,
1801 scan_notif->tsf_low,
1802 scan_notif->tsf_high, scan_notif->status);
1803
1804 /* The HW is no longer scanning */
1805 clear_bit(STATUS_SCAN_HW, &priv->status);
1806
1807 /* The scan completion notification came in, so kill that timer... */
1808 cancel_delayed_work(&priv->scan_check);
1809
1810 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
1811 (priv->scan_bands == 2) ? "2.4" : "5.2",
1812 jiffies_to_msecs(elapsed_jiffies
1813 (priv->scan_pass_start, jiffies)));
1814
1815 /* Remove this scanned band from the list
1816 * of pending bands to scan */
1817 priv->scan_bands--;
1818
1819 /* If a request to abort was given, or the scan did not succeed
1820 * then we reset the scan state machine and terminate,
1821 * re-queuing another scan if one has been requested */
1822 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1823 IWL_DEBUG_INFO("Aborted scan completed.\n");
1824 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1825 } else {
1826 /* If there are more bands on this scan pass reschedule */
1827 if (priv->scan_bands > 0)
1828 goto reschedule;
1829 }
1830
1831 priv->last_scan_jiffies = jiffies;
7878a5a4 1832 priv->next_scan_jiffies = 0;
b481de9c
ZY
1833 IWL_DEBUG_INFO("Setting scan to off\n");
1834
1835 clear_bit(STATUS_SCANNING, &priv->status);
1836
1837 IWL_DEBUG_INFO("Scan took %dms\n",
1838 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
1839
1840 queue_work(priv->workqueue, &priv->scan_completed);
1841
1842 return;
1843
1844reschedule:
1845 priv->scan_pass_start = jiffies;
1846 queue_work(priv->workqueue, &priv->request_scan);
1847}
1848
1849/* Handle notification from uCode that card's power state is changing
1850 * due to software, hardware, or critical temperature RFKILL */
c79dd5b5 1851static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
a55360e4 1852 struct iwl_rx_mem_buffer *rxb)
b481de9c 1853{
db11d634 1854 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
b481de9c
ZY
1855 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
1856 unsigned long status = priv->status;
1857
1858 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
1859 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1860 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
1861
1862 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
1863 RF_CARD_DISABLED)) {
1864
3395f6e9 1865 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
b481de9c
ZY
1866 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1867
3395f6e9
TW
1868 if (!iwl_grab_nic_access(priv)) {
1869 iwl_write_direct32(
b481de9c
ZY
1870 priv, HBUS_TARG_MBX_C,
1871 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1872
3395f6e9 1873 iwl_release_nic_access(priv);
b481de9c
ZY
1874 }
1875
1876 if (!(flags & RXON_CARD_DISABLED)) {
3395f6e9 1877 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
b481de9c 1878 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3395f6e9
TW
1879 if (!iwl_grab_nic_access(priv)) {
1880 iwl_write_direct32(
b481de9c
ZY
1881 priv, HBUS_TARG_MBX_C,
1882 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1883
3395f6e9 1884 iwl_release_nic_access(priv);
b481de9c
ZY
1885 }
1886 }
1887
1888 if (flags & RF_CARD_DISABLED) {
3395f6e9 1889 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
b481de9c 1890 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
3395f6e9
TW
1891 iwl_read32(priv, CSR_UCODE_DRV_GP1);
1892 if (!iwl_grab_nic_access(priv))
1893 iwl_release_nic_access(priv);
b481de9c
ZY
1894 }
1895 }
1896
1897 if (flags & HW_CARD_DISABLED)
1898 set_bit(STATUS_RF_KILL_HW, &priv->status);
1899 else
1900 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1901
1902
1903 if (flags & SW_CARD_DISABLED)
1904 set_bit(STATUS_RF_KILL_SW, &priv->status);
1905 else
1906 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1907
1908 if (!(flags & RXON_CARD_DISABLED))
bb8c093b 1909 iwl4965_scan_cancel(priv);
b481de9c
ZY
1910
1911 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
1912 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
1913 (test_bit(STATUS_RF_KILL_SW, &status) !=
1914 test_bit(STATUS_RF_KILL_SW, &priv->status)))
1915 queue_work(priv->workqueue, &priv->rf_kill);
1916 else
1917 wake_up_interruptible(&priv->wait_command_queue);
1918}
1919
1920/**
bb8c093b 1921 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
b481de9c
ZY
1922 *
1923 * Setup the RX handlers for each of the reply types sent from the uCode
1924 * to the host.
1925 *
1926 * This function chains into the hardware specific files for them to setup
1927 * any hardware specific handlers as well.
1928 */
c79dd5b5 1929static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
b481de9c 1930{
885ba202 1931 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
bb8c093b
CH
1932 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
1933 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
b481de9c 1934 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
bb8c093b
CH
1935 iwl4965_rx_spectrum_measure_notif;
1936 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
b481de9c 1937 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
bb8c093b
CH
1938 iwl4965_rx_pm_debug_statistics_notif;
1939 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
b481de9c 1940
9fbab516
BC
1941 /*
1942 * The same handler is used for both the REPLY to a discrete
1943 * statistics request from the host as well as for the periodic
1944 * statistics notifications (after received beacons) from the uCode.
b481de9c 1945 */
bb8c093b
CH
1946 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
1947 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
b481de9c 1948
bb8c093b
CH
1949 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
1950 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
b481de9c 1951 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
bb8c093b 1952 iwl4965_rx_scan_results_notif;
b481de9c 1953 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
bb8c093b
CH
1954 iwl4965_rx_scan_complete_notif;
1955 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
b481de9c 1956
c1354754
TW
1957 priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
1958 iwl_rx_missed_beacon_notif;
1959
9fbab516 1960 /* Set up hardware specific Rx handlers */
d4789efe 1961 priv->cfg->ops->lib->rx_handler_setup(priv);
b481de9c
ZY
1962}
1963
1964/**
bb8c093b 1965 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
b481de9c
ZY
1966 * @rxb: Rx buffer to reclaim
1967 *
1968 * If an Rx buffer has an async callback associated with it the callback
1969 * will be executed. The attached skb (if present) will only be freed
1970 * if the callback returns 1
1971 */
c79dd5b5 1972static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
a55360e4 1973 struct iwl_rx_mem_buffer *rxb)
b481de9c 1974{
db11d634 1975 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
b481de9c
ZY
1976 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1977 int txq_id = SEQ_TO_QUEUE(sequence);
1978 int index = SEQ_TO_INDEX(sequence);
1979 int huge = sequence & SEQ_HUGE_FRAME;
1980 int cmd_index;
857485c0 1981 struct iwl_cmd *cmd;
b481de9c
ZY
1982
1983 /* If a Tx command is being handled and it isn't in the actual
1984 * command queue then there a command routing bug has been introduced
1985 * in the queue management code. */
1986 if (txq_id != IWL_CMD_QUEUE_NUM)
1987 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
1988 txq_id, pkt->hdr.cmd);
1989 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
1990
1991 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
1992 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
1993
1994 /* Input error checking is done when commands are added to queue. */
1995 if (cmd->meta.flags & CMD_WANT_SKB) {
1996 cmd->meta.source->u.skb = rxb->skb;
1997 rxb->skb = NULL;
1998 } else if (cmd->meta.u.callback &&
1999 !cmd->meta.u.callback(priv, cmd, rxb->skb))
2000 rxb->skb = NULL;
2001
bb8c093b 2002 iwl4965_tx_queue_reclaim(priv, txq_id, index);
b481de9c
ZY
2003
2004 if (!(cmd->meta.flags & CMD_ASYNC)) {
2005 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
2006 wake_up_interruptible(&priv->wait_command_queue);
2007 }
2008}
2009
5c0eef96
MA
2010/*
2011 * this should be called while priv->lock is locked
2012*/
a55360e4 2013static void __iwl_rx_replenish(struct iwl_priv *priv)
b481de9c 2014{
a55360e4
TW
2015 iwl_rx_allocate(priv);
2016 iwl_rx_queue_restock(priv);
b481de9c
ZY
2017}
2018
b481de9c
ZY
2019
2020/**
a55360e4 2021 * iwl_rx_handle - Main entry function for receiving responses from uCode
b481de9c
ZY
2022 *
2023 * Uses the priv->rx_handlers callback function array to invoke
2024 * the appropriate handlers, including command responses,
2025 * frame-received notifications, and other notifications.
2026 */
a55360e4 2027void iwl_rx_handle(struct iwl_priv *priv)
b481de9c 2028{
a55360e4 2029 struct iwl_rx_mem_buffer *rxb;
db11d634 2030 struct iwl_rx_packet *pkt;
a55360e4 2031 struct iwl_rx_queue *rxq = &priv->rxq;
b481de9c
ZY
2032 u32 r, i;
2033 int reclaim;
2034 unsigned long flags;
5c0eef96 2035 u8 fill_rx = 0;
d68ab680 2036 u32 count = 8;
b481de9c 2037
6440adb5
CB
2038 /* uCode's read index (stored in shared DRAM) indicates the last Rx
2039 * buffer that the driver may process (last buffer filled by ucode). */
d67f5489 2040 r = priv->cfg->ops->lib->shared_mem_rx_idx(priv);
b481de9c
ZY
2041 i = rxq->read;
2042
2043 /* Rx interrupt, but nothing sent from uCode */
2044 if (i == r)
f3d67999 2045 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d\n", r, i);
b481de9c 2046
a55360e4 2047 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
5c0eef96
MA
2048 fill_rx = 1;
2049
b481de9c
ZY
2050 while (i != r) {
2051 rxb = rxq->queue[i];
2052
9fbab516 2053 /* If an RXB doesn't have a Rx queue slot associated with it,
b481de9c
ZY
2054 * then a bug has been introduced in the queue refilling
2055 * routines -- catch it here */
2056 BUG_ON(rxb == NULL);
2057
2058 rxq->queue[i] = NULL;
2059
2060 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
5425e490 2061 priv->hw_params.rx_buf_size,
b481de9c 2062 PCI_DMA_FROMDEVICE);
db11d634 2063 pkt = (struct iwl_rx_packet *)rxb->skb->data;
b481de9c
ZY
2064
2065 /* Reclaim a command buffer only if this packet is a response
2066 * to a (driver-originated) command.
2067 * If the packet (e.g. Rx frame) originated from uCode,
2068 * there is no command buffer to reclaim.
2069 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
2070 * but apparently a few don't get set; catch them here. */
2071 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
2072 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
857485c0 2073 (pkt->hdr.cmd != REPLY_RX) &&
cfe01709 2074 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
b481de9c
ZY
2075 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
2076 (pkt->hdr.cmd != REPLY_TX);
2077
2078 /* Based on type of command response or notification,
2079 * handle those that need handling via function in
bb8c093b 2080 * rx_handlers table. See iwl4965_setup_rx_handlers() */
b481de9c 2081 if (priv->rx_handlers[pkt->hdr.cmd]) {
f3d67999
EK
2082 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d, %s, 0x%02x\n", r,
2083 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
b481de9c
ZY
2084 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
2085 } else {
2086 /* No handling needed */
f3d67999 2087 IWL_DEBUG(IWL_DL_RX,
b481de9c
ZY
2088 "r %d i %d No handler needed for %s, 0x%02x\n",
2089 r, i, get_cmd_string(pkt->hdr.cmd),
2090 pkt->hdr.cmd);
2091 }
2092
2093 if (reclaim) {
9fbab516 2094 /* Invoke any callbacks, transfer the skb to caller, and
857485c0 2095 * fire off the (possibly) blocking iwl_send_cmd()
b481de9c
ZY
2096 * as we reclaim the driver command queue */
2097 if (rxb && rxb->skb)
bb8c093b 2098 iwl4965_tx_cmd_complete(priv, rxb);
b481de9c
ZY
2099 else
2100 IWL_WARNING("Claim null rxb?\n");
2101 }
2102
2103 /* For now we just don't re-use anything. We can tweak this
2104 * later to try and re-use notification packets and SKBs that
2105 * fail to Rx correctly */
2106 if (rxb->skb != NULL) {
2107 priv->alloc_rxb_skb--;
2108 dev_kfree_skb_any(rxb->skb);
2109 rxb->skb = NULL;
2110 }
2111
2112 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
5425e490 2113 priv->hw_params.rx_buf_size,
9ee1ba47 2114 PCI_DMA_FROMDEVICE);
b481de9c
ZY
2115 spin_lock_irqsave(&rxq->lock, flags);
2116 list_add_tail(&rxb->list, &priv->rxq.rx_used);
2117 spin_unlock_irqrestore(&rxq->lock, flags);
2118 i = (i + 1) & RX_QUEUE_MASK;
5c0eef96
MA
2119 /* If there are a lot of unused frames,
2120 * restock the Rx queue so ucode wont assert. */
2121 if (fill_rx) {
2122 count++;
2123 if (count >= 8) {
2124 priv->rxq.read = i;
a55360e4 2125 __iwl_rx_replenish(priv);
5c0eef96
MA
2126 count = 0;
2127 }
2128 }
b481de9c
ZY
2129 }
2130
2131 /* Backtrack one entry */
2132 priv->rxq.read = i;
a55360e4
TW
2133 iwl_rx_queue_restock(priv);
2134}
2135/* Convert linear signal-to-noise ratio into dB */
2136static u8 ratio2dB[100] = {
2137/* 0 1 2 3 4 5 6 7 8 9 */
2138 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
2139 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
2140 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
2141 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
2142 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
2143 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
2144 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
2145 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
2146 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
2147 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
2148};
2149
2150/* Calculates a relative dB value from a ratio of linear
2151 * (i.e. not dB) signal levels.
2152 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
2153int iwl4965_calc_db_from_ratio(int sig_ratio)
2154{
2155 /* 1000:1 or higher just report as 60 dB */
2156 if (sig_ratio >= 1000)
2157 return 60;
2158
2159 /* 100:1 or higher, divide by 10 and use table,
2160 * add 20 dB to make up for divide by 10 */
2161 if (sig_ratio >= 100)
2162 return (20 + (int)ratio2dB[sig_ratio/10]);
2163
2164 /* We shouldn't see this */
2165 if (sig_ratio < 1)
2166 return 0;
2167
2168 /* Use table for ratios 1:1 - 99:1 */
2169 return (int)ratio2dB[sig_ratio];
2170}
2171
2172#define PERFECT_RSSI (-20) /* dBm */
2173#define WORST_RSSI (-95) /* dBm */
2174#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
2175
2176/* Calculate an indication of rx signal quality (a percentage, not dBm!).
2177 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
2178 * about formulas used below. */
2179int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
2180{
2181 int sig_qual;
2182 int degradation = PERFECT_RSSI - rssi_dbm;
2183
2184 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
2185 * as indicator; formula is (signal dbm - noise dbm).
2186 * SNR at or above 40 is a great signal (100%).
2187 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
2188 * Weakest usable signal is usually 10 - 15 dB SNR. */
2189 if (noise_dbm) {
2190 if (rssi_dbm - noise_dbm >= 40)
2191 return 100;
2192 else if (rssi_dbm < noise_dbm)
2193 return 0;
2194 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
2195
2196 /* Else use just the signal level.
2197 * This formula is a least squares fit of data points collected and
2198 * compared with a reference system that had a percentage (%) display
2199 * for signal quality. */
2200 } else
2201 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
2202 (15 * RSSI_RANGE + 62 * degradation)) /
2203 (RSSI_RANGE * RSSI_RANGE);
2204
2205 if (sig_qual > 100)
2206 sig_qual = 100;
2207 else if (sig_qual < 1)
2208 sig_qual = 0;
2209
2210 return sig_qual;
b481de9c
ZY
2211}
2212
0a6857e7 2213#ifdef CONFIG_IWLWIFI_DEBUG
bf403db8 2214static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
b481de9c 2215{
c1adf9fb 2216 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
0795af57
JP
2217 DECLARE_MAC_BUF(mac);
2218
b481de9c 2219 IWL_DEBUG_RADIO("RX CONFIG:\n");
bf403db8 2220 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
b481de9c
ZY
2221 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
2222 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
2223 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
2224 le32_to_cpu(rxon->filter_flags));
2225 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
2226 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
2227 rxon->ofdm_basic_rates);
2228 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
0795af57
JP
2229 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
2230 print_mac(mac, rxon->node_addr));
2231 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
2232 print_mac(mac, rxon->bssid_addr));
b481de9c
ZY
2233 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
2234}
2235#endif
2236
c79dd5b5 2237static void iwl4965_enable_interrupts(struct iwl_priv *priv)
b481de9c
ZY
2238{
2239 IWL_DEBUG_ISR("Enabling interrupts\n");
2240 set_bit(STATUS_INT_ENABLED, &priv->status);
3395f6e9 2241 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
b481de9c
ZY
2242}
2243
0359facc
MA
2244/* call this function to flush any scheduled tasklet */
2245static inline void iwl_synchronize_irq(struct iwl_priv *priv)
2246{
2247 /* wait to make sure we flush pedding tasklet*/
2248 synchronize_irq(priv->pci_dev->irq);
2249 tasklet_kill(&priv->irq_tasklet);
2250}
2251
c79dd5b5 2252static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
b481de9c
ZY
2253{
2254 clear_bit(STATUS_INT_ENABLED, &priv->status);
2255
2256 /* disable interrupts from uCode/NIC to host */
3395f6e9 2257 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
b481de9c
ZY
2258
2259 /* acknowledge/clear/reset any interrupts still pending
2260 * from uCode or flow handler (Rx/Tx DMA) */
3395f6e9
TW
2261 iwl_write32(priv, CSR_INT, 0xffffffff);
2262 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
b481de9c
ZY
2263 IWL_DEBUG_ISR("Disabled interrupts\n");
2264}
2265
b481de9c 2266
b481de9c 2267/**
bb8c093b 2268 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
b481de9c 2269 */
c79dd5b5 2270static void iwl4965_irq_handle_error(struct iwl_priv *priv)
b481de9c 2271{
bb8c093b 2272 /* Set the FW error flag -- cleared on iwl4965_down */
b481de9c
ZY
2273 set_bit(STATUS_FW_ERROR, &priv->status);
2274
2275 /* Cancel currently queued command. */
2276 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
2277
0a6857e7 2278#ifdef CONFIG_IWLWIFI_DEBUG
bf403db8 2279 if (priv->debug_level & IWL_DL_FW_ERRORS) {
ede0cba4 2280 iwl_dump_nic_error_log(priv);
189a2b59 2281 iwl_dump_nic_event_log(priv);
bf403db8 2282 iwl4965_print_rx_config_cmd(priv);
b481de9c
ZY
2283 }
2284#endif
2285
2286 wake_up_interruptible(&priv->wait_command_queue);
2287
2288 /* Keep the restart process from trying to send host
2289 * commands by clearing the INIT status bit */
2290 clear_bit(STATUS_READY, &priv->status);
2291
2292 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
f3d67999 2293 IWL_DEBUG(IWL_DL_FW_ERRORS,
b481de9c
ZY
2294 "Restarting adapter due to uCode error.\n");
2295
3109ece1 2296 if (iwl_is_associated(priv)) {
b481de9c
ZY
2297 memcpy(&priv->recovery_rxon, &priv->active_rxon,
2298 sizeof(priv->recovery_rxon));
2299 priv->error_recovering = 1;
2300 }
3a1081e8
EK
2301 if (priv->cfg->mod_params->restart_fw)
2302 queue_work(priv->workqueue, &priv->restart);
b481de9c
ZY
2303 }
2304}
2305
c79dd5b5 2306static void iwl4965_error_recovery(struct iwl_priv *priv)
b481de9c
ZY
2307{
2308 unsigned long flags;
2309
2310 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
2311 sizeof(priv->staging_rxon));
2312 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
bb8c093b 2313 iwl4965_commit_rxon(priv);
b481de9c 2314
4f40e4d9 2315 iwl_rxon_add_station(priv, priv->bssid, 1);
b481de9c
ZY
2316
2317 spin_lock_irqsave(&priv->lock, flags);
2318 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
2319 priv->error_recovering = 0;
2320 spin_unlock_irqrestore(&priv->lock, flags);
2321}
2322
c79dd5b5 2323static void iwl4965_irq_tasklet(struct iwl_priv *priv)
b481de9c
ZY
2324{
2325 u32 inta, handled = 0;
2326 u32 inta_fh;
2327 unsigned long flags;
0a6857e7 2328#ifdef CONFIG_IWLWIFI_DEBUG
b481de9c
ZY
2329 u32 inta_mask;
2330#endif
2331
2332 spin_lock_irqsave(&priv->lock, flags);
2333
2334 /* Ack/clear/reset pending uCode interrupts.
2335 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
2336 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
3395f6e9
TW
2337 inta = iwl_read32(priv, CSR_INT);
2338 iwl_write32(priv, CSR_INT, inta);
b481de9c
ZY
2339
2340 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
2341 * Any new interrupts that happen after this, either while we're
2342 * in this tasklet, or later, will show up in next ISR/tasklet. */
3395f6e9
TW
2343 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
2344 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
b481de9c 2345
0a6857e7 2346#ifdef CONFIG_IWLWIFI_DEBUG
bf403db8 2347 if (priv->debug_level & IWL_DL_ISR) {
9fbab516 2348 /* just for debug */
3395f6e9 2349 inta_mask = iwl_read32(priv, CSR_INT_MASK);
b481de9c
ZY
2350 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
2351 inta, inta_mask, inta_fh);
2352 }
2353#endif
2354
2355 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
2356 * atomic, make sure that inta covers all the interrupts that
2357 * we've discovered, even if FH interrupt came in just after
2358 * reading CSR_INT. */
6f83eaa1 2359 if (inta_fh & CSR49_FH_INT_RX_MASK)
b481de9c 2360 inta |= CSR_INT_BIT_FH_RX;
6f83eaa1 2361 if (inta_fh & CSR49_FH_INT_TX_MASK)
b481de9c
ZY
2362 inta |= CSR_INT_BIT_FH_TX;
2363
2364 /* Now service all interrupt bits discovered above. */
2365 if (inta & CSR_INT_BIT_HW_ERR) {
2366 IWL_ERROR("Microcode HW error detected. Restarting.\n");
2367
2368 /* Tell the device to stop sending interrupts */
bb8c093b 2369 iwl4965_disable_interrupts(priv);
b481de9c 2370
bb8c093b 2371 iwl4965_irq_handle_error(priv);
b481de9c
ZY
2372
2373 handled |= CSR_INT_BIT_HW_ERR;
2374
2375 spin_unlock_irqrestore(&priv->lock, flags);
2376
2377 return;
2378 }
2379
0a6857e7 2380#ifdef CONFIG_IWLWIFI_DEBUG
bf403db8 2381 if (priv->debug_level & (IWL_DL_ISR)) {
b481de9c 2382 /* NIC fires this, but we don't use it, redundant with WAKEUP */
25c03d8e
JP
2383 if (inta & CSR_INT_BIT_SCD)
2384 IWL_DEBUG_ISR("Scheduler finished to transmit "
2385 "the frame/frames.\n");
b481de9c
ZY
2386
2387 /* Alive notification via Rx interrupt will do the real work */
2388 if (inta & CSR_INT_BIT_ALIVE)
2389 IWL_DEBUG_ISR("Alive interrupt\n");
2390 }
2391#endif
2392 /* Safely ignore these bits for debug checks below */
25c03d8e 2393 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
b481de9c 2394
9fbab516 2395 /* HW RF KILL switch toggled */
b481de9c
ZY
2396 if (inta & CSR_INT_BIT_RF_KILL) {
2397 int hw_rf_kill = 0;
3395f6e9 2398 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
b481de9c
ZY
2399 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
2400 hw_rf_kill = 1;
2401
f3d67999 2402 IWL_DEBUG(IWL_DL_RF_KILL, "RF_KILL bit toggled to %s.\n",
b481de9c
ZY
2403 hw_rf_kill ? "disable radio":"enable radio");
2404
2405 /* Queue restart only if RF_KILL switch was set to "kill"
2406 * when we loaded driver, and is now set to "enable".
2407 * After we're Alive, RF_KILL gets handled by
3230455d 2408 * iwl4965_rx_card_state_notif() */
53e49093
ZY
2409 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
2410 clear_bit(STATUS_RF_KILL_HW, &priv->status);
b481de9c 2411 queue_work(priv->workqueue, &priv->restart);
53e49093 2412 }
b481de9c
ZY
2413
2414 handled |= CSR_INT_BIT_RF_KILL;
2415 }
2416
9fbab516 2417 /* Chip got too hot and stopped itself */
b481de9c
ZY
2418 if (inta & CSR_INT_BIT_CT_KILL) {
2419 IWL_ERROR("Microcode CT kill error detected.\n");
2420 handled |= CSR_INT_BIT_CT_KILL;
2421 }
2422
2423 /* Error detected by uCode */
2424 if (inta & CSR_INT_BIT_SW_ERR) {
2425 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
2426 inta);
bb8c093b 2427 iwl4965_irq_handle_error(priv);
b481de9c
ZY
2428 handled |= CSR_INT_BIT_SW_ERR;
2429 }
2430
2431 /* uCode wakes up after power-down sleep */
2432 if (inta & CSR_INT_BIT_WAKEUP) {
2433 IWL_DEBUG_ISR("Wakeup interrupt\n");
a55360e4 2434 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
babcebfa
TW
2435 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
2436 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
2437 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
2438 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
2439 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
2440 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
b481de9c
ZY
2441
2442 handled |= CSR_INT_BIT_WAKEUP;
2443 }
2444
2445 /* All uCode command responses, including Tx command responses,
2446 * Rx "responses" (frame-received notification), and other
2447 * notifications from uCode come through here*/
2448 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
a55360e4 2449 iwl_rx_handle(priv);
b481de9c
ZY
2450 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
2451 }
2452
2453 if (inta & CSR_INT_BIT_FH_TX) {
2454 IWL_DEBUG_ISR("Tx interrupt\n");
2455 handled |= CSR_INT_BIT_FH_TX;
dbb983b7
RR
2456 /* FH finished to write, send event */
2457 priv->ucode_write_complete = 1;
2458 wake_up_interruptible(&priv->wait_command_queue);
b481de9c
ZY
2459 }
2460
2461 if (inta & ~handled)
2462 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
2463
2464 if (inta & ~CSR_INI_SET_MASK) {
2465 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
2466 inta & ~CSR_INI_SET_MASK);
2467 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
2468 }
2469
2470 /* Re-enable all interrupts */
0359facc
MA
2471 /* only Re-enable if diabled by irq */
2472 if (test_bit(STATUS_INT_ENABLED, &priv->status))
2473 iwl4965_enable_interrupts(priv);
b481de9c 2474
0a6857e7 2475#ifdef CONFIG_IWLWIFI_DEBUG
bf403db8 2476 if (priv->debug_level & (IWL_DL_ISR)) {
3395f6e9
TW
2477 inta = iwl_read32(priv, CSR_INT);
2478 inta_mask = iwl_read32(priv, CSR_INT_MASK);
2479 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
b481de9c
ZY
2480 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
2481 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
2482 }
2483#endif
2484 spin_unlock_irqrestore(&priv->lock, flags);
2485}
2486
bb8c093b 2487static irqreturn_t iwl4965_isr(int irq, void *data)
b481de9c 2488{
c79dd5b5 2489 struct iwl_priv *priv = data;
b481de9c
ZY
2490 u32 inta, inta_mask;
2491 u32 inta_fh;
2492 if (!priv)
2493 return IRQ_NONE;
2494
2495 spin_lock(&priv->lock);
2496
2497 /* Disable (but don't clear!) interrupts here to avoid
2498 * back-to-back ISRs and sporadic interrupts from our NIC.
2499 * If we have something to service, the tasklet will re-enable ints.
2500 * If we *don't* have something, we'll re-enable before leaving here. */
3395f6e9
TW
2501 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
2502 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
b481de9c
ZY
2503
2504 /* Discover which interrupts are active/pending */
3395f6e9
TW
2505 inta = iwl_read32(priv, CSR_INT);
2506 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
b481de9c
ZY
2507
2508 /* Ignore interrupt if there's nothing in NIC to service.
2509 * This may be due to IRQ shared with another device,
2510 * or due to sporadic interrupts thrown from our NIC. */
2511 if (!inta && !inta_fh) {
2512 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
2513 goto none;
2514 }
2515
2516 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
66fbb541
ON
2517 /* Hardware disappeared. It might have already raised
2518 * an interrupt */
b481de9c 2519 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
66fbb541 2520 goto unplugged;
b481de9c
ZY
2521 }
2522
2523 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
2524 inta, inta_mask, inta_fh);
2525
25c03d8e
JP
2526 inta &= ~CSR_INT_BIT_SCD;
2527
bb8c093b 2528 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
25c03d8e
JP
2529 if (likely(inta || inta_fh))
2530 tasklet_schedule(&priv->irq_tasklet);
b481de9c 2531
66fbb541
ON
2532 unplugged:
2533 spin_unlock(&priv->lock);
b481de9c
ZY
2534 return IRQ_HANDLED;
2535
2536 none:
2537 /* re-enable interrupts here since we don't have anything to service. */
0359facc
MA
2538 /* only Re-enable if diabled by irq */
2539 if (test_bit(STATUS_INT_ENABLED, &priv->status))
2540 iwl4965_enable_interrupts(priv);
b481de9c
ZY
2541 spin_unlock(&priv->lock);
2542 return IRQ_NONE;
2543}
2544
b481de9c
ZY
2545/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
2546 * sending probe req. This should be set long enough to hear probe responses
2547 * from more than one AP. */
2548#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
2549#define IWL_ACTIVE_DWELL_TIME_52 (10)
2550
2551/* For faster active scanning, scan will move to the next channel if fewer than
2552 * PLCP_QUIET_THRESH packets are heard on this channel within
2553 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
2554 * time if it's a quiet channel (nothing responded to our probe, and there's
2555 * no other traffic).
2556 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
2557#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
2558#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
2559
2560/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
2561 * Must be set longer than active dwell time.
2562 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
2563#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
2564#define IWL_PASSIVE_DWELL_TIME_52 (10)
2565#define IWL_PASSIVE_DWELL_BASE (100)
2566#define IWL_CHANNEL_TUNE_TIME 5
2567
c79dd5b5 2568static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
8318d78a 2569 enum ieee80211_band band)
b481de9c 2570{
8318d78a 2571 if (band == IEEE80211_BAND_5GHZ)
b481de9c
ZY
2572 return IWL_ACTIVE_DWELL_TIME_52;
2573 else
2574 return IWL_ACTIVE_DWELL_TIME_24;
2575}
2576
c79dd5b5 2577static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
8318d78a 2578 enum ieee80211_band band)
b481de9c 2579{
8318d78a
JB
2580 u16 active = iwl4965_get_active_dwell_time(priv, band);
2581 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
b481de9c
ZY
2582 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
2583 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
2584
3109ece1 2585 if (iwl_is_associated(priv)) {
b481de9c
ZY
2586 /* If we're associated, we clamp the maximum passive
2587 * dwell time to be 98% of the beacon interval (minus
2588 * 2 * channel tune time) */
2589 passive = priv->beacon_int;
2590 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
2591 passive = IWL_PASSIVE_DWELL_BASE;
2592 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
2593 }
2594
2595 if (passive <= active)
2596 passive = active + 1;
2597
2598 return passive;
2599}
2600
c79dd5b5 2601static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
8318d78a 2602 enum ieee80211_band band,
b481de9c 2603 u8 is_active, u8 direct_mask,
bb8c093b 2604 struct iwl4965_scan_channel *scan_ch)
b481de9c
ZY
2605{
2606 const struct ieee80211_channel *channels = NULL;
8318d78a 2607 const struct ieee80211_supported_band *sband;
bf85ea4f 2608 const struct iwl_channel_info *ch_info;
b481de9c
ZY
2609 u16 passive_dwell = 0;
2610 u16 active_dwell = 0;
2611 int added, i;
2612
d1141dfb 2613 sband = iwl_get_hw_mode(priv, band);
8318d78a 2614 if (!sband)
b481de9c
ZY
2615 return 0;
2616
8318d78a 2617 channels = sband->channels;
b481de9c 2618
8318d78a
JB
2619 active_dwell = iwl4965_get_active_dwell_time(priv, band);
2620 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
b481de9c 2621
8318d78a 2622 for (i = 0, added = 0; i < sband->n_channels; i++) {
182e2e66
JB
2623 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
2624 continue;
2625
8318d78a 2626 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
b481de9c 2627
8622e705 2628 ch_info = iwl_get_channel_info(priv, band,
9fbab516 2629 scan_ch->channel);
b481de9c
ZY
2630 if (!is_channel_valid(ch_info)) {
2631 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
2632 scan_ch->channel);
2633 continue;
2634 }
2635
2636 if (!is_active || is_channel_passive(ch_info) ||
8318d78a 2637 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
b481de9c
ZY
2638 scan_ch->type = 0; /* passive */
2639 else
2640 scan_ch->type = 1; /* active */
2641
2642 if (scan_ch->type & 1)
2643 scan_ch->type |= (direct_mask << 1);
2644
b481de9c
ZY
2645 scan_ch->active_dwell = cpu_to_le16(active_dwell);
2646 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
2647
9fbab516 2648 /* Set txpower levels to defaults */
b481de9c
ZY
2649 scan_ch->tpc.dsp_atten = 110;
2650 /* scan_pwr_info->tpc.dsp_atten; */
2651
2652 /*scan_pwr_info->tpc.tx_gain; */
8318d78a 2653 if (band == IEEE80211_BAND_5GHZ)
b481de9c
ZY
2654 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
2655 else {
2656 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
2657 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
9fbab516 2658 * power level:
8a1b0245 2659 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
b481de9c
ZY
2660 */
2661 }
2662
2663 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
2664 scan_ch->channel,
2665 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
2666 (scan_ch->type & 1) ?
2667 active_dwell : passive_dwell);
2668
2669 scan_ch++;
2670 added++;
2671 }
2672
2673 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
2674 return added;
2675}
2676
b481de9c
ZY
2677/******************************************************************************
2678 *
2679 * uCode download functions
2680 *
2681 ******************************************************************************/
2682
c79dd5b5 2683static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
b481de9c 2684{
98c92211
TW
2685 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
2686 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
2687 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
2688 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
2689 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
2690 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
b481de9c
ZY
2691}
2692
edcdf8b2
RR
2693static void iwl4965_nic_start(struct iwl_priv *priv)
2694{
2695 /* Remove all resets to allow NIC to operate */
2696 iwl_write32(priv, CSR_RESET, 0);
2697}
2698
2699
b481de9c 2700/**
bb8c093b 2701 * iwl4965_read_ucode - Read uCode images from disk file.
b481de9c
ZY
2702 *
2703 * Copy into buffers for card to fetch via bus-mastering
2704 */
c79dd5b5 2705static int iwl4965_read_ucode(struct iwl_priv *priv)
b481de9c 2706{
bb8c093b 2707 struct iwl4965_ucode *ucode;
90e759d1 2708 int ret;
b481de9c 2709 const struct firmware *ucode_raw;
4bf775cd 2710 const char *name = priv->cfg->fw_name;
b481de9c
ZY
2711 u8 *src;
2712 size_t len;
2713 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
2714
2715 /* Ask kernel firmware_class module to get the boot firmware off disk.
2716 * request_firmware() is synchronous, file is in memory on return. */
90e759d1
TW
2717 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
2718 if (ret < 0) {
2719 IWL_ERROR("%s firmware file req failed: Reason %d\n",
2720 name, ret);
b481de9c
ZY
2721 goto error;
2722 }
2723
2724 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
2725 name, ucode_raw->size);
2726
2727 /* Make sure that we got at least our header! */
2728 if (ucode_raw->size < sizeof(*ucode)) {
2729 IWL_ERROR("File size way too small!\n");
90e759d1 2730 ret = -EINVAL;
b481de9c
ZY
2731 goto err_release;
2732 }
2733
2734 /* Data from ucode file: header followed by uCode images */
2735 ucode = (void *)ucode_raw->data;
2736
2737 ver = le32_to_cpu(ucode->ver);
2738 inst_size = le32_to_cpu(ucode->inst_size);
2739 data_size = le32_to_cpu(ucode->data_size);
2740 init_size = le32_to_cpu(ucode->init_size);
2741 init_data_size = le32_to_cpu(ucode->init_data_size);
2742 boot_size = le32_to_cpu(ucode->boot_size);
2743
2744 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
2745 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
2746 inst_size);
2747 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
2748 data_size);
2749 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
2750 init_size);
2751 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
2752 init_data_size);
2753 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
2754 boot_size);
2755
2756 /* Verify size of file vs. image size info in file's header */
2757 if (ucode_raw->size < sizeof(*ucode) +
2758 inst_size + data_size + init_size +
2759 init_data_size + boot_size) {
2760
2761 IWL_DEBUG_INFO("uCode file size %d too small\n",
2762 (int)ucode_raw->size);
90e759d1 2763 ret = -EINVAL;
b481de9c
ZY
2764 goto err_release;
2765 }
2766
2767 /* Verify that uCode images will fit in card's SRAM */
099b40b7 2768 if (inst_size > priv->hw_params.max_inst_size) {
90e759d1
TW
2769 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
2770 inst_size);
2771 ret = -EINVAL;
b481de9c
ZY
2772 goto err_release;
2773 }
2774
099b40b7 2775 if (data_size > priv->hw_params.max_data_size) {
90e759d1
TW
2776 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
2777 data_size);
2778 ret = -EINVAL;
b481de9c
ZY
2779 goto err_release;
2780 }
099b40b7 2781 if (init_size > priv->hw_params.max_inst_size) {
b481de9c 2782 IWL_DEBUG_INFO
90e759d1
TW
2783 ("uCode init instr len %d too large to fit in\n",
2784 init_size);
2785 ret = -EINVAL;
b481de9c
ZY
2786 goto err_release;
2787 }
099b40b7 2788 if (init_data_size > priv->hw_params.max_data_size) {
b481de9c 2789 IWL_DEBUG_INFO
90e759d1
TW
2790 ("uCode init data len %d too large to fit in\n",
2791 init_data_size);
2792 ret = -EINVAL;
b481de9c
ZY
2793 goto err_release;
2794 }
099b40b7 2795 if (boot_size > priv->hw_params.max_bsm_size) {
b481de9c 2796 IWL_DEBUG_INFO
90e759d1
TW
2797 ("uCode boot instr len %d too large to fit in\n",
2798 boot_size);
2799 ret = -EINVAL;
b481de9c
ZY
2800 goto err_release;
2801 }
2802
2803 /* Allocate ucode buffers for card's bus-master loading ... */
2804
2805 /* Runtime instructions and 2 copies of data:
2806 * 1) unmodified from disk
2807 * 2) backup cache for save/restore during power-downs */
2808 priv->ucode_code.len = inst_size;
98c92211 2809 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
b481de9c
ZY
2810
2811 priv->ucode_data.len = data_size;
98c92211 2812 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
b481de9c
ZY
2813
2814 priv->ucode_data_backup.len = data_size;
98c92211 2815 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
b481de9c
ZY
2816
2817 /* Initialization instructions and data */
90e759d1
TW
2818 if (init_size && init_data_size) {
2819 priv->ucode_init.len = init_size;
98c92211 2820 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
90e759d1
TW
2821
2822 priv->ucode_init_data.len = init_data_size;
98c92211 2823 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
90e759d1
TW
2824
2825 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2826 goto err_pci_alloc;
2827 }
b481de9c
ZY
2828
2829 /* Bootstrap (instructions only, no data) */
90e759d1
TW
2830 if (boot_size) {
2831 priv->ucode_boot.len = boot_size;
98c92211 2832 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
b481de9c 2833
90e759d1
TW
2834 if (!priv->ucode_boot.v_addr)
2835 goto err_pci_alloc;
2836 }
b481de9c
ZY
2837
2838 /* Copy images into buffers for card's bus-master reads ... */
2839
2840 /* Runtime instructions (first block of data in file) */
2841 src = &ucode->data[0];
2842 len = priv->ucode_code.len;
90e759d1 2843 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
b481de9c
ZY
2844 memcpy(priv->ucode_code.v_addr, src, len);
2845 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
2846 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2847
2848 /* Runtime data (2nd block)
bb8c093b 2849 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
b481de9c
ZY
2850 src = &ucode->data[inst_size];
2851 len = priv->ucode_data.len;
90e759d1 2852 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
b481de9c
ZY
2853 memcpy(priv->ucode_data.v_addr, src, len);
2854 memcpy(priv->ucode_data_backup.v_addr, src, len);
2855
2856 /* Initialization instructions (3rd block) */
2857 if (init_size) {
2858 src = &ucode->data[inst_size + data_size];
2859 len = priv->ucode_init.len;
90e759d1
TW
2860 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
2861 len);
b481de9c
ZY
2862 memcpy(priv->ucode_init.v_addr, src, len);
2863 }
2864
2865 /* Initialization data (4th block) */
2866 if (init_data_size) {
2867 src = &ucode->data[inst_size + data_size + init_size];
2868 len = priv->ucode_init_data.len;
90e759d1
TW
2869 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
2870 len);
b481de9c
ZY
2871 memcpy(priv->ucode_init_data.v_addr, src, len);
2872 }
2873
2874 /* Bootstrap instructions (5th block) */
2875 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
2876 len = priv->ucode_boot.len;
90e759d1 2877 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
b481de9c
ZY
2878 memcpy(priv->ucode_boot.v_addr, src, len);
2879
2880 /* We have our copies now, allow OS release its copies */
2881 release_firmware(ucode_raw);
2882 return 0;
2883
2884 err_pci_alloc:
2885 IWL_ERROR("failed to allocate pci memory\n");
90e759d1 2886 ret = -ENOMEM;
bb8c093b 2887 iwl4965_dealloc_ucode_pci(priv);
b481de9c
ZY
2888
2889 err_release:
2890 release_firmware(ucode_raw);
2891
2892 error:
90e759d1 2893 return ret;
b481de9c
ZY
2894}
2895
b481de9c 2896/**
4a4a9e81 2897 * iwl_alive_start - called after REPLY_ALIVE notification received
b481de9c 2898 * from protocol/runtime uCode (initialization uCode's
4a4a9e81 2899 * Alive gets handled by iwl_init_alive_start()).
b481de9c 2900 */
4a4a9e81 2901static void iwl_alive_start(struct iwl_priv *priv)
b481de9c 2902{
57aab75a 2903 int ret = 0;
b481de9c
ZY
2904
2905 IWL_DEBUG_INFO("Runtime Alive received.\n");
2906
2907 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2908 /* We had an error bringing up the hardware, so take it
2909 * all the way back down so we can try again */
2910 IWL_DEBUG_INFO("Alive failed.\n");
2911 goto restart;
2912 }
2913
2914 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2915 * This is a paranoid check, because we would not have gotten the
2916 * "runtime" alive if code weren't properly loaded. */
b0692f2f 2917 if (iwl_verify_ucode(priv)) {
b481de9c
ZY
2918 /* Runtime instruction load was bad;
2919 * take it all the way back down so we can try again */
2920 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
2921 goto restart;
2922 }
2923
bf85ea4f 2924 iwlcore_clear_stations_table(priv);
57aab75a
TW
2925 ret = priv->cfg->ops->lib->alive_notify(priv);
2926 if (ret) {
b481de9c 2927 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
57aab75a 2928 ret);
b481de9c
ZY
2929 goto restart;
2930 }
2931
9fbab516 2932 /* After the ALIVE response, we can send host commands to 4965 uCode */
b481de9c
ZY
2933 set_bit(STATUS_ALIVE, &priv->status);
2934
2935 /* Clear out the uCode error bit if it is set */
2936 clear_bit(STATUS_FW_ERROR, &priv->status);
2937
fee1247a 2938 if (iwl_is_rfkill(priv))
b481de9c
ZY
2939 return;
2940
36d6825b 2941 ieee80211_wake_queues(priv->hw);
b481de9c
ZY
2942
2943 priv->active_rate = priv->rates_mask;
2944 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2945
3109ece1 2946 if (iwl_is_associated(priv)) {
c1adf9fb
GG
2947 struct iwl_rxon_cmd *active_rxon =
2948 (struct iwl_rxon_cmd *)&priv->active_rxon;
b481de9c
ZY
2949
2950 memcpy(&priv->staging_rxon, &priv->active_rxon,
2951 sizeof(priv->staging_rxon));
2952 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2953 } else {
2954 /* Initialize our rx_config data */
bb8c093b 2955 iwl4965_connection_init_rx_config(priv);
b481de9c
ZY
2956 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2957 }
2958
9fbab516 2959 /* Configure Bluetooth device coexistence support */
bb8c093b 2960 iwl4965_send_bt_config(priv);
b481de9c 2961
4a4a9e81
TW
2962 iwl_reset_run_time_calib(priv);
2963
b481de9c 2964 /* Configure the adapter for unassociated operation */
bb8c093b 2965 iwl4965_commit_rxon(priv);
b481de9c
ZY
2966
2967 /* At this point, the NIC is initialized and operational */
b481de9c 2968 iwl4965_rf_kill_ct_config(priv);
5a66926a 2969
fe00b5a5
RC
2970 iwl_leds_register(priv);
2971
b481de9c 2972 IWL_DEBUG_INFO("ALIVE processing complete.\n");
a9f46786 2973 set_bit(STATUS_READY, &priv->status);
5a66926a 2974 wake_up_interruptible(&priv->wait_command_queue);
b481de9c
ZY
2975
2976 if (priv->error_recovering)
bb8c093b 2977 iwl4965_error_recovery(priv);
b481de9c 2978
c8381fdc 2979 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
84363e6e 2980 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
b481de9c
ZY
2981 return;
2982
2983 restart:
2984 queue_work(priv->workqueue, &priv->restart);
2985}
2986
c79dd5b5 2987static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
b481de9c 2988
c79dd5b5 2989static void __iwl4965_down(struct iwl_priv *priv)
b481de9c
ZY
2990{
2991 unsigned long flags;
2992 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
b481de9c
ZY
2993
2994 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
2995
b481de9c
ZY
2996 if (!exit_pending)
2997 set_bit(STATUS_EXIT_PENDING, &priv->status);
2998
ab53d8af
MA
2999 iwl_leds_unregister(priv);
3000
c8381fdc
MA
3001 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
3002
bf85ea4f 3003 iwlcore_clear_stations_table(priv);
b481de9c
ZY
3004
3005 /* Unblock any waiting calls */
3006 wake_up_interruptible_all(&priv->wait_command_queue);
3007
b481de9c
ZY
3008 /* Wipe out the EXIT_PENDING status bit if we are not actually
3009 * exiting the module */
3010 if (!exit_pending)
3011 clear_bit(STATUS_EXIT_PENDING, &priv->status);
3012
3013 /* stop and reset the on-board processor */
3395f6e9 3014 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
b481de9c
ZY
3015
3016 /* tell the device to stop sending interrupts */
0359facc 3017 spin_lock_irqsave(&priv->lock, flags);
bb8c093b 3018 iwl4965_disable_interrupts(priv);
0359facc
MA
3019 spin_unlock_irqrestore(&priv->lock, flags);
3020 iwl_synchronize_irq(priv);
b481de9c
ZY
3021
3022 if (priv->mac80211_registered)
3023 ieee80211_stop_queues(priv->hw);
3024
bb8c093b 3025 /* If we have not previously called iwl4965_init() then
b481de9c 3026 * clear all bits but the RF Kill and SUSPEND bits and return */
fee1247a 3027 if (!iwl_is_init(priv)) {
b481de9c
ZY
3028 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
3029 STATUS_RF_KILL_HW |
3030 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
3031 STATUS_RF_KILL_SW |
9788864e
RC
3032 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
3033 STATUS_GEO_CONFIGURED |
b481de9c
ZY
3034 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
3035 STATUS_IN_SUSPEND;
3036 goto exit;
3037 }
3038
3039 /* ...otherwise clear out all the status bits but the RF Kill and
3040 * SUSPEND bits and continue taking the NIC down. */
3041 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
3042 STATUS_RF_KILL_HW |
3043 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
3044 STATUS_RF_KILL_SW |
9788864e
RC
3045 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
3046 STATUS_GEO_CONFIGURED |
b481de9c
ZY
3047 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
3048 STATUS_IN_SUSPEND |
3049 test_bit(STATUS_FW_ERROR, &priv->status) <<
3050 STATUS_FW_ERROR;
3051
3052 spin_lock_irqsave(&priv->lock, flags);
3395f6e9 3053 iwl_clear_bit(priv, CSR_GP_CNTRL,
9fbab516 3054 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
b481de9c
ZY
3055 spin_unlock_irqrestore(&priv->lock, flags);
3056
da1bc453 3057 iwl_txq_ctx_stop(priv);
b3bbacb7 3058 iwl_rxq_stop(priv);
b481de9c
ZY
3059
3060 spin_lock_irqsave(&priv->lock, flags);
3395f6e9
TW
3061 if (!iwl_grab_nic_access(priv)) {
3062 iwl_write_prph(priv, APMG_CLK_DIS_REG,
b481de9c 3063 APMG_CLK_VAL_DMA_CLK_RQT);
3395f6e9 3064 iwl_release_nic_access(priv);
b481de9c
ZY
3065 }
3066 spin_unlock_irqrestore(&priv->lock, flags);
3067
3068 udelay(5);
3069
7f066108
TW
3070 /* FIXME: apm_ops.suspend(priv) */
3071 priv->cfg->ops->lib->apm_ops.reset(priv);
399f4900 3072 priv->cfg->ops->lib->free_shared_mem(priv);
b481de9c
ZY
3073
3074 exit:
885ba202 3075 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
b481de9c
ZY
3076
3077 if (priv->ibss_beacon)
3078 dev_kfree_skb(priv->ibss_beacon);
3079 priv->ibss_beacon = NULL;
3080
3081 /* clear out any free frames */
fcab423d 3082 iwl_clear_free_frames(priv);
b481de9c
ZY
3083}
3084
c79dd5b5 3085static void iwl4965_down(struct iwl_priv *priv)
b481de9c
ZY
3086{
3087 mutex_lock(&priv->mutex);
bb8c093b 3088 __iwl4965_down(priv);
b481de9c 3089 mutex_unlock(&priv->mutex);
b24d22b1 3090
bb8c093b 3091 iwl4965_cancel_deferred_work(priv);
b481de9c
ZY
3092}
3093
3094#define MAX_HW_RESTARTS 5
3095
c79dd5b5 3096static int __iwl4965_up(struct iwl_priv *priv)
b481de9c 3097{
57aab75a
TW
3098 int i;
3099 int ret;
b481de9c
ZY
3100
3101 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3102 IWL_WARNING("Exit pending; will not bring the NIC up\n");
3103 return -EIO;
3104 }
3105
3106 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
3107 IWL_WARNING("Radio disabled by SW RF kill (module "
3108 "parameter)\n");
ad97edd2 3109 iwl_rfkill_set_hw_state(priv);
e655b9f0
ZY
3110 return -ENODEV;
3111 }
3112
e903fbd4
RC
3113 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
3114 IWL_ERROR("ucode not available for device bringup\n");
3115 return -EIO;
3116 }
3117
e655b9f0 3118 /* If platform's RF_KILL switch is NOT set to KILL */
3395f6e9 3119 if (iwl_read32(priv, CSR_GP_CNTRL) &
e655b9f0
ZY
3120 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3121 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3122 else {
3123 set_bit(STATUS_RF_KILL_HW, &priv->status);
3124 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
ad97edd2 3125 iwl_rfkill_set_hw_state(priv);
e655b9f0
ZY
3126 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
3127 return -ENODEV;
3128 }
b481de9c
ZY
3129 }
3130
ad97edd2 3131 iwl_rfkill_set_hw_state(priv);
3395f6e9 3132 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
b481de9c 3133
399f4900
RR
3134 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
3135 if (ret) {
3136 IWL_ERROR("Unable to allocate shared memory\n");
3137 return ret;
3138 }
3139
1053d35f 3140 ret = iwl_hw_nic_init(priv);
57aab75a
TW
3141 if (ret) {
3142 IWL_ERROR("Unable to init nic\n");
3143 return ret;
b481de9c
ZY
3144 }
3145
3146 /* make sure rfkill handshake bits are cleared */
3395f6e9
TW
3147 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3148 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
b481de9c
ZY
3149 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3150
3151 /* clear (again), then enable host interrupts */
3395f6e9 3152 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
bb8c093b 3153 iwl4965_enable_interrupts(priv);
b481de9c
ZY
3154
3155 /* really make sure rfkill handshake bits are cleared */
3395f6e9
TW
3156 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3157 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
b481de9c
ZY
3158
3159 /* Copy original ucode data image from disk into backup cache.
3160 * This will be used to initialize the on-board processor's
3161 * data SRAM for a clean start when the runtime program first loads. */
3162 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
5a66926a 3163 priv->ucode_data.len);
b481de9c 3164
e655b9f0
ZY
3165 /* We return success when we resume from suspend and rf_kill is on. */
3166 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
b481de9c 3167 return 0;
b481de9c
ZY
3168
3169 for (i = 0; i < MAX_HW_RESTARTS; i++) {
3170
bf85ea4f 3171 iwlcore_clear_stations_table(priv);
b481de9c
ZY
3172
3173 /* load bootstrap state machine,
3174 * load bootstrap program into processor's memory,
3175 * prepare to load the "initialize" uCode */
57aab75a 3176 ret = priv->cfg->ops->lib->load_ucode(priv);
b481de9c 3177
57aab75a
TW
3178 if (ret) {
3179 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
b481de9c
ZY
3180 continue;
3181 }
3182
3183 /* start card; "initialize" will load runtime ucode */
edcdf8b2 3184 iwl4965_nic_start(priv);
b481de9c 3185
b481de9c
ZY
3186 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
3187
3188 return 0;
3189 }
3190
3191 set_bit(STATUS_EXIT_PENDING, &priv->status);
bb8c093b 3192 __iwl4965_down(priv);
b481de9c
ZY
3193
3194 /* tried to restart and config the device for as long as our
3195 * patience could withstand */
3196 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
3197 return -EIO;
3198}
3199
3200
3201/*****************************************************************************
3202 *
3203 * Workqueue callbacks
3204 *
3205 *****************************************************************************/
3206
4a4a9e81 3207static void iwl_bg_init_alive_start(struct work_struct *data)
b481de9c 3208{
c79dd5b5
TW
3209 struct iwl_priv *priv =
3210 container_of(data, struct iwl_priv, init_alive_start.work);
b481de9c
ZY
3211
3212 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3213 return;
3214
3215 mutex_lock(&priv->mutex);
f3ccc08c 3216 priv->cfg->ops->lib->init_alive_start(priv);
b481de9c
ZY
3217 mutex_unlock(&priv->mutex);
3218}
3219
4a4a9e81 3220static void iwl_bg_alive_start(struct work_struct *data)
b481de9c 3221{
c79dd5b5
TW
3222 struct iwl_priv *priv =
3223 container_of(data, struct iwl_priv, alive_start.work);
b481de9c
ZY
3224
3225 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3226 return;
3227
3228 mutex_lock(&priv->mutex);
4a4a9e81 3229 iwl_alive_start(priv);
b481de9c
ZY
3230 mutex_unlock(&priv->mutex);
3231}
3232
bb8c093b 3233static void iwl4965_bg_rf_kill(struct work_struct *work)
b481de9c 3234{
c79dd5b5 3235 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
b481de9c
ZY
3236
3237 wake_up_interruptible(&priv->wait_command_queue);
3238
3239 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3240 return;
3241
3242 mutex_lock(&priv->mutex);
3243
fee1247a 3244 if (!iwl_is_rfkill(priv)) {
f3d67999 3245 IWL_DEBUG(IWL_DL_RF_KILL,
b481de9c
ZY
3246 "HW and/or SW RF Kill no longer active, restarting "
3247 "device\n");
3248 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
3249 queue_work(priv->workqueue, &priv->restart);
3250 } else {
ad97edd2
MA
3251 /* make sure mac80211 stop sending Tx frame */
3252 if (priv->mac80211_registered)
3253 ieee80211_stop_queues(priv->hw);
b481de9c
ZY
3254
3255 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
3256 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3257 "disabled by SW switch\n");
3258 else
3259 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
3260 "Kill switch must be turned off for "
3261 "wireless networking to work.\n");
3262 }
ad97edd2
MA
3263 iwl_rfkill_set_hw_state(priv);
3264
b481de9c
ZY
3265 mutex_unlock(&priv->mutex);
3266}
3267
4419e39b
AK
3268static void iwl4965_bg_set_monitor(struct work_struct *work)
3269{
3270 struct iwl_priv *priv = container_of(work,
3271 struct iwl_priv, set_monitor);
3272
3273 IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
3274
3275 mutex_lock(&priv->mutex);
3276
3277 if (!iwl_is_ready(priv))
3278 IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
3279 else
3280 if (iwl4965_set_mode(priv, IEEE80211_IF_TYPE_MNTR) != 0)
3281 IWL_ERROR("iwl4965_set_mode() failed\n");
3282
3283 mutex_unlock(&priv->mutex);
3284}
3285
b481de9c
ZY
3286#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
3287
bb8c093b 3288static void iwl4965_bg_scan_check(struct work_struct *data)
b481de9c 3289{
c79dd5b5
TW
3290 struct iwl_priv *priv =
3291 container_of(data, struct iwl_priv, scan_check.work);
b481de9c
ZY
3292
3293 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3294 return;
3295
3296 mutex_lock(&priv->mutex);
3297 if (test_bit(STATUS_SCANNING, &priv->status) ||
3298 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
f3d67999
EK
3299 IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
3300 "adapter (%dms)\n",
3301 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
052c4b9f 3302
b481de9c 3303 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
bb8c093b 3304 iwl4965_send_scan_abort(priv);
b481de9c
ZY
3305 }
3306 mutex_unlock(&priv->mutex);
3307}
3308
bb8c093b 3309static void iwl4965_bg_request_scan(struct work_struct *data)
b481de9c 3310{
c79dd5b5
TW
3311 struct iwl_priv *priv =
3312 container_of(data, struct iwl_priv, request_scan);
857485c0 3313 struct iwl_host_cmd cmd = {
b481de9c 3314 .id = REPLY_SCAN_CMD,
bb8c093b 3315 .len = sizeof(struct iwl4965_scan_cmd),
b481de9c
ZY
3316 .meta.flags = CMD_SIZE_HUGE,
3317 };
bb8c093b 3318 struct iwl4965_scan_cmd *scan;
b481de9c 3319 struct ieee80211_conf *conf = NULL;
78330fdd 3320 u16 cmd_len;
8318d78a 3321 enum ieee80211_band band;
78330fdd 3322 u8 direct_mask;
857485c0 3323 int ret = 0;
b481de9c
ZY
3324
3325 conf = ieee80211_get_hw_conf(priv->hw);
3326
3327 mutex_lock(&priv->mutex);
3328
fee1247a 3329 if (!iwl_is_ready(priv)) {
b481de9c
ZY
3330 IWL_WARNING("request scan called when driver not ready.\n");
3331 goto done;
3332 }
3333
3334 /* Make sure the scan wasn't cancelled before this queued work
3335 * was given the chance to run... */
3336 if (!test_bit(STATUS_SCANNING, &priv->status))
3337 goto done;
3338
3339 /* This should never be called or scheduled if there is currently
3340 * a scan active in the hardware. */
3341 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
3342 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
3343 "Ignoring second request.\n");
857485c0 3344 ret = -EIO;
b481de9c
ZY
3345 goto done;
3346 }
3347
3348 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3349 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
3350 goto done;
3351 }
3352
3353 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3354 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
3355 goto done;
3356 }
3357
fee1247a 3358 if (iwl_is_rfkill(priv)) {
b481de9c
ZY
3359 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
3360 goto done;
3361 }
3362
3363 if (!test_bit(STATUS_READY, &priv->status)) {
3364 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
3365 goto done;
3366 }
3367
3368 if (!priv->scan_bands) {
3369 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
3370 goto done;
3371 }
3372
3373 if (!priv->scan) {
bb8c093b 3374 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
b481de9c
ZY
3375 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
3376 if (!priv->scan) {
857485c0 3377 ret = -ENOMEM;
b481de9c
ZY
3378 goto done;
3379 }
3380 }
3381 scan = priv->scan;
bb8c093b 3382 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
b481de9c
ZY
3383
3384 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
3385 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
3386
3109ece1 3387 if (iwl_is_associated(priv)) {
b481de9c
ZY
3388 u16 interval = 0;
3389 u32 extra;
3390 u32 suspend_time = 100;
3391 u32 scan_suspend_time = 100;
3392 unsigned long flags;
3393
3394 IWL_DEBUG_INFO("Scanning while associated...\n");
3395
3396 spin_lock_irqsave(&priv->lock, flags);
3397 interval = priv->beacon_int;
3398 spin_unlock_irqrestore(&priv->lock, flags);
3399
3400 scan->suspend_time = 0;
052c4b9f 3401 scan->max_out_time = cpu_to_le32(200 * 1024);
b481de9c
ZY
3402 if (!interval)
3403 interval = suspend_time;
3404
3405 extra = (suspend_time / interval) << 22;
3406 scan_suspend_time = (extra |
3407 ((suspend_time % interval) * 1024));
3408 scan->suspend_time = cpu_to_le32(scan_suspend_time);
3409 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
3410 scan_suspend_time, interval);
3411 }
3412
3413 /* We should add the ability for user to lock to PASSIVE ONLY */
3414 if (priv->one_direct_scan) {
3415 IWL_DEBUG_SCAN
3416 ("Kicking off one direct scan for '%s'\n",
bb8c093b 3417 iwl4965_escape_essid(priv->direct_ssid,
b481de9c
ZY
3418 priv->direct_ssid_len));
3419 scan->direct_scan[0].id = WLAN_EID_SSID;
3420 scan->direct_scan[0].len = priv->direct_ssid_len;
3421 memcpy(scan->direct_scan[0].ssid,
3422 priv->direct_ssid, priv->direct_ssid_len);
3423 direct_mask = 1;
3109ece1 3424 } else if (!iwl_is_associated(priv) && priv->essid_len) {
786b4557
BM
3425 IWL_DEBUG_SCAN
3426 ("Kicking off one direct scan for '%s' when not associated\n",
3427 iwl4965_escape_essid(priv->essid, priv->essid_len));
b481de9c
ZY
3428 scan->direct_scan[0].id = WLAN_EID_SSID;
3429 scan->direct_scan[0].len = priv->essid_len;
3430 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
3431 direct_mask = 1;
857485c0 3432 } else {
786b4557 3433 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
b481de9c 3434 direct_mask = 0;
857485c0 3435 }
b481de9c 3436
b481de9c 3437 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
5425e490 3438 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
b481de9c
ZY
3439 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
3440
b481de9c
ZY
3441
3442 switch (priv->scan_bands) {
3443 case 2:
3444 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
3445 scan->tx_cmd.rate_n_flags =
bb8c093b 3446 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
b481de9c
ZY
3447 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
3448
3449 scan->good_CRC_th = 0;
8318d78a 3450 band = IEEE80211_BAND_2GHZ;
b481de9c
ZY
3451 break;
3452
3453 case 1:
3454 scan->tx_cmd.rate_n_flags =
bb8c093b 3455 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
b481de9c
ZY
3456 RATE_MCS_ANT_B_MSK);
3457 scan->good_CRC_th = IWL_GOOD_CRC_TH;
8318d78a 3458 band = IEEE80211_BAND_5GHZ;
b481de9c
ZY
3459 break;
3460
3461 default:
3462 IWL_WARNING("Invalid scan band count\n");
3463 goto done;
3464 }
3465
78330fdd
TW
3466 /* We don't build a direct scan probe request; the uCode will do
3467 * that based on the direct_mask added to each channel entry */
3468 cmd_len = iwl4965_fill_probe_req(priv, band,
3469 (struct ieee80211_mgmt *)scan->data,
3470 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
3471
3472 scan->tx_cmd.len = cpu_to_le16(cmd_len);
b481de9c
ZY
3473 /* select Rx chains */
3474
3475 /* Force use of chains B and C (0x6) for scan Rx.
3476 * Avoid A (0x1) because of its off-channel reception on A-band.
3477 * MIMO is not used here, but value is required to make uCode happy. */
3478 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
3479 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
3480 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
3481 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
3482
3483 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
3484 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
3485
786b4557 3486 if (direct_mask)
26c0f03f
RC
3487 scan->channel_count =
3488 iwl4965_get_channels_for_scan(
3489 priv, band, 1, /* active */
3490 direct_mask,
3491 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
786b4557 3492 else
26c0f03f
RC
3493 scan->channel_count =
3494 iwl4965_get_channels_for_scan(
3495 priv, band, 0, /* passive */
3496 direct_mask,
3497 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
b481de9c 3498
5da4b55f
MA
3499 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
3500 RXON_FILTER_BCON_AWARE_MSK);
b481de9c 3501 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
bb8c093b 3502 scan->channel_count * sizeof(struct iwl4965_scan_channel);
b481de9c
ZY
3503 cmd.data = scan;
3504 scan->len = cpu_to_le16(cmd.len);
3505
3506 set_bit(STATUS_SCAN_HW, &priv->status);
857485c0
TW
3507 ret = iwl_send_cmd_sync(priv, &cmd);
3508 if (ret)
b481de9c
ZY
3509 goto done;
3510
3511 queue_delayed_work(priv->workqueue, &priv->scan_check,
3512 IWL_SCAN_CHECK_WATCHDOG);
3513
3514 mutex_unlock(&priv->mutex);
3515 return;
3516
3517 done:
01ebd063 3518 /* inform mac80211 scan aborted */
b481de9c
ZY
3519 queue_work(priv->workqueue, &priv->scan_completed);
3520 mutex_unlock(&priv->mutex);
3521}
3522
bb8c093b 3523static void iwl4965_bg_up(struct work_struct *data)
b481de9c 3524{
c79dd5b5 3525 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
b481de9c
ZY
3526
3527 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3528 return;
3529
3530 mutex_lock(&priv->mutex);
bb8c093b 3531 __iwl4965_up(priv);
b481de9c
ZY
3532 mutex_unlock(&priv->mutex);
3533}
3534
bb8c093b 3535static void iwl4965_bg_restart(struct work_struct *data)
b481de9c 3536{
c79dd5b5 3537 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
b481de9c
ZY
3538
3539 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3540 return;
3541
bb8c093b 3542 iwl4965_down(priv);
b481de9c
ZY
3543 queue_work(priv->workqueue, &priv->up);
3544}
3545
bb8c093b 3546static void iwl4965_bg_rx_replenish(struct work_struct *data)
b481de9c 3547{
c79dd5b5
TW
3548 struct iwl_priv *priv =
3549 container_of(data, struct iwl_priv, rx_replenish);
b481de9c
ZY
3550
3551 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3552 return;
3553
3554 mutex_lock(&priv->mutex);
a55360e4 3555 iwl_rx_replenish(priv);
b481de9c
ZY
3556 mutex_unlock(&priv->mutex);
3557}
3558
7878a5a4
MA
3559#define IWL_DELAY_NEXT_SCAN (HZ*2)
3560
508e32e1 3561static void iwl4965_post_associate(struct iwl_priv *priv)
b481de9c 3562{
b481de9c 3563 struct ieee80211_conf *conf = NULL;
857485c0 3564 int ret = 0;
0795af57 3565 DECLARE_MAC_BUF(mac);
b481de9c
ZY
3566
3567 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
3568 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
3569 return;
3570 }
3571
0795af57
JP
3572 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
3573 priv->assoc_id,
3574 print_mac(mac, priv->active_rxon.bssid_addr));
b481de9c
ZY
3575
3576
3577 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3578 return;
3579
b481de9c 3580
508e32e1 3581 if (!priv->vif || !priv->is_open)
948c171c 3582 return;
508e32e1 3583
bb8c093b 3584 iwl4965_scan_cancel_timeout(priv, 200);
052c4b9f 3585
b481de9c
ZY
3586 conf = ieee80211_get_hw_conf(priv->hw);
3587
3588 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
bb8c093b 3589 iwl4965_commit_rxon(priv);
b481de9c 3590
bb8c093b
CH
3591 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
3592 iwl4965_setup_rxon_timing(priv);
857485c0 3593 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
b481de9c 3594 sizeof(priv->rxon_timing), &priv->rxon_timing);
857485c0 3595 if (ret)
b481de9c
ZY
3596 IWL_WARNING("REPLY_RXON_TIMING failed - "
3597 "Attempting to continue.\n");
3598
3599 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
3600
c8b0e6e1 3601#ifdef CONFIG_IWL4965_HT
fd105e79 3602 if (priv->current_ht_config.is_ht)
47c5196e 3603 iwl_set_rxon_ht(priv, &priv->current_ht_config);
c8b0e6e1 3604#endif /* CONFIG_IWL4965_HT*/
c7de35cd 3605 iwl_set_rxon_chain(priv);
b481de9c
ZY
3606 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
3607
3608 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
3609 priv->assoc_id, priv->beacon_int);
3610
3611 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3612 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
3613 else
3614 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
3615
3616 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
3617 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
3618 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
3619 else
3620 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3621
3622 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
3623 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3624
3625 }
3626
bb8c093b 3627 iwl4965_commit_rxon(priv);
b481de9c
ZY
3628
3629 switch (priv->iw_mode) {
3630 case IEEE80211_IF_TYPE_STA:
bb8c093b 3631 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
b481de9c
ZY
3632 break;
3633
3634 case IEEE80211_IF_TYPE_IBSS:
3635
3636 /* clear out the station table */
bf85ea4f 3637 iwlcore_clear_stations_table(priv);
b481de9c 3638
4f40e4d9
TW
3639 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
3640 iwl_rxon_add_station(priv, priv->bssid, 0);
bb8c093b
CH
3641 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
3642 iwl4965_send_beacon_cmd(priv);
b481de9c
ZY
3643
3644 break;
3645
3646 default:
3647 IWL_ERROR("%s Should not be called in %d mode\n",
3648 __FUNCTION__, priv->iw_mode);
3649 break;
3650 }
3651
bb8c093b 3652 iwl4965_sequence_reset(priv);
b481de9c 3653
b481de9c 3654 /* Enable Rx differential gain and sensitivity calibrations */
f0832f13 3655 iwl_chain_noise_reset(priv);
b481de9c 3656 priv->start_calib = 1;
b481de9c
ZY
3657
3658 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
3659 priv->assoc_station_added = 1;
3660
bb8c093b 3661 iwl4965_activate_qos(priv, 0);
292ae174 3662
5da4b55f 3663 iwl_power_update_mode(priv, 0);
7878a5a4
MA
3664 /* we have just associated, don't start scan too early */
3665 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
508e32e1
RC
3666}
3667
3668
3669static void iwl4965_bg_post_associate(struct work_struct *data)
3670{
3671 struct iwl_priv *priv = container_of(data, struct iwl_priv,
3672 post_associate.work);
3673
3674 mutex_lock(&priv->mutex);
3675 iwl4965_post_associate(priv);
b481de9c 3676 mutex_unlock(&priv->mutex);
508e32e1 3677
b481de9c
ZY
3678}
3679
bb8c093b 3680static void iwl4965_bg_abort_scan(struct work_struct *work)
b481de9c 3681{
c79dd5b5 3682 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
b481de9c 3683
fee1247a 3684 if (!iwl_is_ready(priv))
b481de9c
ZY
3685 return;
3686
3687 mutex_lock(&priv->mutex);
3688
3689 set_bit(STATUS_SCAN_ABORTING, &priv->status);
bb8c093b 3690 iwl4965_send_scan_abort(priv);
b481de9c
ZY
3691
3692 mutex_unlock(&priv->mutex);
3693}
3694
76bb77e0
ZY
3695static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
3696
bb8c093b 3697static void iwl4965_bg_scan_completed(struct work_struct *work)
b481de9c 3698{
c79dd5b5
TW
3699 struct iwl_priv *priv =
3700 container_of(work, struct iwl_priv, scan_completed);
b481de9c 3701
f3d67999 3702 IWL_DEBUG(IWL_DL_SCAN, "SCAN complete scan\n");
b481de9c
ZY
3703
3704 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3705 return;
3706
a0646470
ZY
3707 if (test_bit(STATUS_CONF_PENDING, &priv->status))
3708 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
76bb77e0 3709
b481de9c
ZY
3710 ieee80211_scan_completed(priv->hw);
3711
3712 /* Since setting the TXPOWER may have been deferred while
3713 * performing the scan, fire one off */
3714 mutex_lock(&priv->mutex);
bb8c093b 3715 iwl4965_hw_reg_send_txpower(priv);
b481de9c
ZY
3716 mutex_unlock(&priv->mutex);
3717}
3718
3719/*****************************************************************************
3720 *
3721 * mac80211 entry point functions
3722 *
3723 *****************************************************************************/
3724
5a66926a
ZY
3725#define UCODE_READY_TIMEOUT (2 * HZ)
3726
bb8c093b 3727static int iwl4965_mac_start(struct ieee80211_hw *hw)
b481de9c 3728{
c79dd5b5 3729 struct iwl_priv *priv = hw->priv;
5a66926a 3730 int ret;
b481de9c
ZY
3731
3732 IWL_DEBUG_MAC80211("enter\n");
3733
5a66926a
ZY
3734 if (pci_enable_device(priv->pci_dev)) {
3735 IWL_ERROR("Fail to pci_enable_device\n");
3736 return -ENODEV;
3737 }
3738 pci_restore_state(priv->pci_dev);
3739 pci_enable_msi(priv->pci_dev);
3740
3741 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
3742 DRV_NAME, priv);
3743 if (ret) {
3744 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
3745 goto out_disable_msi;
3746 }
3747
b481de9c
ZY
3748 /* we should be verifying the device is ready to be opened */
3749 mutex_lock(&priv->mutex);
3750
c1adf9fb 3751 memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
5a66926a
ZY
3752 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3753 * ucode filename and max sizes are card-specific. */
b481de9c 3754
5a66926a
ZY
3755 if (!priv->ucode_code.len) {
3756 ret = iwl4965_read_ucode(priv);
3757 if (ret) {
3758 IWL_ERROR("Could not read microcode: %d\n", ret);
3759 mutex_unlock(&priv->mutex);
3760 goto out_release_irq;
3761 }
3762 }
b481de9c 3763
e655b9f0 3764 ret = __iwl4965_up(priv);
5a66926a 3765
b481de9c 3766 mutex_unlock(&priv->mutex);
5a66926a 3767
e655b9f0
ZY
3768 if (ret)
3769 goto out_release_irq;
3770
3771 IWL_DEBUG_INFO("Start UP work done.\n");
3772
3773 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
3774 return 0;
3775
fe9b6b72 3776 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
5a66926a 3777 * mac80211 will not be run successfully. */
fe9b6b72
RR
3778 if (priv->ucode_type == UCODE_RT) {
3779 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3780 test_bit(STATUS_READY, &priv->status),
3781 UCODE_READY_TIMEOUT);
3782 if (!ret) {
3783 if (!test_bit(STATUS_READY, &priv->status)) {
3784 IWL_ERROR("START_ALIVE timeout after %dms.\n",
3785 jiffies_to_msecs(UCODE_READY_TIMEOUT));
3786 ret = -ETIMEDOUT;
3787 goto out_release_irq;
3788 }
5a66926a 3789 }
5a66926a 3790
fe9b6b72
RR
3791 priv->is_open = 1;
3792 }
b481de9c
ZY
3793 IWL_DEBUG_MAC80211("leave\n");
3794 return 0;
5a66926a
ZY
3795
3796out_release_irq:
3797 free_irq(priv->pci_dev->irq, priv);
3798out_disable_msi:
3799 pci_disable_msi(priv->pci_dev);
e655b9f0
ZY
3800 pci_disable_device(priv->pci_dev);
3801 priv->is_open = 0;
3802 IWL_DEBUG_MAC80211("leave - failed\n");
5a66926a 3803 return ret;
b481de9c
ZY
3804}
3805
bb8c093b 3806static void iwl4965_mac_stop(struct ieee80211_hw *hw)
b481de9c 3807{
c79dd5b5 3808 struct iwl_priv *priv = hw->priv;
b481de9c
ZY
3809
3810 IWL_DEBUG_MAC80211("enter\n");
948c171c 3811
e655b9f0
ZY
3812 if (!priv->is_open) {
3813 IWL_DEBUG_MAC80211("leave - skip\n");
3814 return;
3815 }
3816
b481de9c 3817 priv->is_open = 0;
5a66926a 3818
fee1247a 3819 if (iwl_is_ready_rf(priv)) {
e655b9f0
ZY
3820 /* stop mac, cancel any scan request and clear
3821 * RXON_FILTER_ASSOC_MSK BIT
3822 */
5a66926a
ZY
3823 mutex_lock(&priv->mutex);
3824 iwl4965_scan_cancel_timeout(priv, 100);
3825 cancel_delayed_work(&priv->post_associate);
fde3571f 3826 mutex_unlock(&priv->mutex);
fde3571f
MA
3827 }
3828
5a66926a
ZY
3829 iwl4965_down(priv);
3830
3831 flush_workqueue(priv->workqueue);
3832 free_irq(priv->pci_dev->irq, priv);
3833 pci_disable_msi(priv->pci_dev);
3834 pci_save_state(priv->pci_dev);
3835 pci_disable_device(priv->pci_dev);
948c171c 3836
b481de9c 3837 IWL_DEBUG_MAC80211("leave\n");
b481de9c
ZY
3838}
3839
e039fa4a 3840static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
b481de9c 3841{
c79dd5b5 3842 struct iwl_priv *priv = hw->priv;
b481de9c
ZY
3843
3844 IWL_DEBUG_MAC80211("enter\n");
3845
3846 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
3847 IWL_DEBUG_MAC80211("leave - monitor\n");
3848 return -1;
3849 }
3850
3851 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
e039fa4a 3852 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
b481de9c 3853
e039fa4a 3854 if (iwl_tx_skb(priv, skb))
b481de9c
ZY
3855 dev_kfree_skb_any(skb);
3856
3857 IWL_DEBUG_MAC80211("leave\n");
3858 return 0;
3859}
3860
bb8c093b 3861static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
b481de9c
ZY
3862 struct ieee80211_if_init_conf *conf)
3863{
c79dd5b5 3864 struct iwl_priv *priv = hw->priv;
b481de9c 3865 unsigned long flags;
0795af57 3866 DECLARE_MAC_BUF(mac);
b481de9c 3867
32bfd35d 3868 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
b481de9c 3869
32bfd35d
JB
3870 if (priv->vif) {
3871 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
75849d28 3872 return -EOPNOTSUPP;
b481de9c
ZY
3873 }
3874
3875 spin_lock_irqsave(&priv->lock, flags);
32bfd35d 3876 priv->vif = conf->vif;
b481de9c
ZY
3877
3878 spin_unlock_irqrestore(&priv->lock, flags);
3879
3880 mutex_lock(&priv->mutex);
864792e3
TW
3881
3882 if (conf->mac_addr) {
3883 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
3884 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
3885 }
b481de9c 3886
fee1247a 3887 if (iwl_is_ready(priv))
5a66926a
ZY
3888 iwl4965_set_mode(priv, conf->type);
3889
b481de9c
ZY
3890 mutex_unlock(&priv->mutex);
3891
5a66926a 3892 IWL_DEBUG_MAC80211("leave\n");
b481de9c
ZY
3893 return 0;
3894}
3895
3896/**
bb8c093b 3897 * iwl4965_mac_config - mac80211 config callback
b481de9c
ZY
3898 *
3899 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
3900 * be set inappropriately and the driver currently sets the hardware up to
3901 * use it whenever needed.
3902 */
bb8c093b 3903static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
b481de9c 3904{
c79dd5b5 3905 struct iwl_priv *priv = hw->priv;
bf85ea4f 3906 const struct iwl_channel_info *ch_info;
b481de9c 3907 unsigned long flags;
76bb77e0 3908 int ret = 0;
b481de9c
ZY
3909
3910 mutex_lock(&priv->mutex);
8318d78a 3911 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
b481de9c 3912
12342c47
ZY
3913 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
3914
fee1247a 3915 if (!iwl_is_ready(priv)) {
b481de9c 3916 IWL_DEBUG_MAC80211("leave - not ready\n");
76bb77e0
ZY
3917 ret = -EIO;
3918 goto out;
b481de9c
ZY
3919 }
3920
1ea87396 3921 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
b481de9c 3922 test_bit(STATUS_SCANNING, &priv->status))) {
a0646470
ZY
3923 IWL_DEBUG_MAC80211("leave - scanning\n");
3924 set_bit(STATUS_CONF_PENDING, &priv->status);
b481de9c 3925 mutex_unlock(&priv->mutex);
a0646470 3926 return 0;
b481de9c
ZY
3927 }
3928
3929 spin_lock_irqsave(&priv->lock, flags);
3930
8622e705 3931 ch_info = iwl_get_channel_info(priv, conf->channel->band,
8318d78a 3932 ieee80211_frequency_to_channel(conf->channel->center_freq));
b481de9c 3933 if (!is_channel_valid(ch_info)) {
b481de9c
ZY
3934 IWL_DEBUG_MAC80211("leave - invalid channel\n");
3935 spin_unlock_irqrestore(&priv->lock, flags);
76bb77e0
ZY
3936 ret = -EINVAL;
3937 goto out;
b481de9c
ZY
3938 }
3939
c8b0e6e1 3940#ifdef CONFIG_IWL4965_HT
78330fdd 3941 /* if we are switching from ht to 2.4 clear flags
b481de9c
ZY
3942 * from any ht related info since 2.4 does not
3943 * support ht */
78330fdd 3944 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
b481de9c
ZY
3945#ifdef IEEE80211_CONF_CHANNEL_SWITCH
3946 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
3947#endif
3948 )
3949 priv->staging_rxon.flags = 0;
c8b0e6e1 3950#endif /* CONFIG_IWL4965_HT */
b481de9c 3951
c7de35cd 3952 iwl_set_rxon_channel(priv, conf->channel->band,
8318d78a 3953 ieee80211_frequency_to_channel(conf->channel->center_freq));
b481de9c 3954
8318d78a 3955 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
b481de9c
ZY
3956
3957 /* The list of supported rates and rate mask can be different
8318d78a 3958 * for each band; since the band may have changed, reset
b481de9c 3959 * the rate mask to what mac80211 lists */
bb8c093b 3960 iwl4965_set_rate(priv);
b481de9c
ZY
3961
3962 spin_unlock_irqrestore(&priv->lock, flags);
3963
3964#ifdef IEEE80211_CONF_CHANNEL_SWITCH
3965 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
bb8c093b 3966 iwl4965_hw_channel_switch(priv, conf->channel);
76bb77e0 3967 goto out;
b481de9c
ZY
3968 }
3969#endif
3970
ad97edd2
MA
3971 if (priv->cfg->ops->lib->radio_kill_sw)
3972 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
b481de9c
ZY
3973
3974 if (!conf->radio_enabled) {
3975 IWL_DEBUG_MAC80211("leave - radio disabled\n");
76bb77e0 3976 goto out;
b481de9c
ZY
3977 }
3978
fee1247a 3979 if (iwl_is_rfkill(priv)) {
b481de9c 3980 IWL_DEBUG_MAC80211("leave - RF kill\n");
76bb77e0
ZY
3981 ret = -EIO;
3982 goto out;
b481de9c
ZY
3983 }
3984
bb8c093b 3985 iwl4965_set_rate(priv);
b481de9c
ZY
3986
3987 if (memcmp(&priv->active_rxon,
3988 &priv->staging_rxon, sizeof(priv->staging_rxon)))
bb8c093b 3989 iwl4965_commit_rxon(priv);
b481de9c
ZY
3990 else
3991 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
3992
3993 IWL_DEBUG_MAC80211("leave\n");
3994
a0646470
ZY
3995out:
3996 clear_bit(STATUS_CONF_PENDING, &priv->status);
5a66926a 3997 mutex_unlock(&priv->mutex);
76bb77e0 3998 return ret;
b481de9c
ZY
3999}
4000
c79dd5b5 4001static void iwl4965_config_ap(struct iwl_priv *priv)
b481de9c 4002{
857485c0 4003 int ret = 0;
b481de9c 4004
d986bcd1 4005 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
b481de9c
ZY
4006 return;
4007
4008 /* The following should be done only at AP bring up */
4009 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
4010
4011 /* RXON - unassoc (to set timing command) */
4012 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
bb8c093b 4013 iwl4965_commit_rxon(priv);
b481de9c
ZY
4014
4015 /* RXON Timing */
bb8c093b
CH
4016 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
4017 iwl4965_setup_rxon_timing(priv);
857485c0 4018 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
b481de9c 4019 sizeof(priv->rxon_timing), &priv->rxon_timing);
857485c0 4020 if (ret)
b481de9c
ZY
4021 IWL_WARNING("REPLY_RXON_TIMING failed - "
4022 "Attempting to continue.\n");
4023
c7de35cd 4024 iwl_set_rxon_chain(priv);
b481de9c
ZY
4025
4026 /* FIXME: what should be the assoc_id for AP? */
4027 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
4028 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
4029 priv->staging_rxon.flags |=
4030 RXON_FLG_SHORT_PREAMBLE_MSK;
4031 else
4032 priv->staging_rxon.flags &=
4033 ~RXON_FLG_SHORT_PREAMBLE_MSK;
4034
4035 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
4036 if (priv->assoc_capability &
4037 WLAN_CAPABILITY_SHORT_SLOT_TIME)
4038 priv->staging_rxon.flags |=
4039 RXON_FLG_SHORT_SLOT_MSK;
4040 else
4041 priv->staging_rxon.flags &=
4042 ~RXON_FLG_SHORT_SLOT_MSK;
4043
4044 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4045 priv->staging_rxon.flags &=
4046 ~RXON_FLG_SHORT_SLOT_MSK;
4047 }
4048 /* restore RXON assoc */
4049 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
bb8c093b 4050 iwl4965_commit_rxon(priv);
bb8c093b 4051 iwl4965_activate_qos(priv, 1);
4f40e4d9 4052 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
e1493deb 4053 }
bb8c093b 4054 iwl4965_send_beacon_cmd(priv);
b481de9c
ZY
4055
4056 /* FIXME - we need to add code here to detect a totally new
4057 * configuration, reset the AP, unassoc, rxon timing, assoc,
4058 * clear sta table, add BCAST sta... */
4059}
4060
32bfd35d
JB
4061static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
4062 struct ieee80211_vif *vif,
b481de9c
ZY
4063 struct ieee80211_if_conf *conf)
4064{
c79dd5b5 4065 struct iwl_priv *priv = hw->priv;
0795af57 4066 DECLARE_MAC_BUF(mac);
b481de9c
ZY
4067 unsigned long flags;
4068 int rc;
4069
4070 if (conf == NULL)
4071 return -EIO;
4072
b716bb91
EG
4073 if (priv->vif != vif) {
4074 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
b716bb91
EG
4075 return 0;
4076 }
4077
b481de9c
ZY
4078 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
4079 (!conf->beacon || !conf->ssid_len)) {
4080 IWL_DEBUG_MAC80211
4081 ("Leaving in AP mode because HostAPD is not ready.\n");
4082 return 0;
4083 }
4084
fee1247a 4085 if (!iwl_is_alive(priv))
5a66926a
ZY
4086 return -EAGAIN;
4087
b481de9c
ZY
4088 mutex_lock(&priv->mutex);
4089
b481de9c 4090 if (conf->bssid)
0795af57
JP
4091 IWL_DEBUG_MAC80211("bssid: %s\n",
4092 print_mac(mac, conf->bssid));
b481de9c 4093
4150c572
JB
4094/*
4095 * very dubious code was here; the probe filtering flag is never set:
4096 *
b481de9c
ZY
4097 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
4098 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
4150c572 4099 */
b481de9c
ZY
4100
4101 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
4102 if (!conf->bssid) {
4103 conf->bssid = priv->mac_addr;
4104 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
0795af57
JP
4105 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
4106 print_mac(mac, conf->bssid));
b481de9c
ZY
4107 }
4108 if (priv->ibss_beacon)
4109 dev_kfree_skb(priv->ibss_beacon);
4110
4111 priv->ibss_beacon = conf->beacon;
4112 }
4113
fee1247a 4114 if (iwl_is_rfkill(priv))
fde3571f
MA
4115 goto done;
4116
b481de9c
ZY
4117 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
4118 !is_multicast_ether_addr(conf->bssid)) {
4119 /* If there is currently a HW scan going on in the background
4120 * then we need to cancel it else the RXON below will fail. */
bb8c093b 4121 if (iwl4965_scan_cancel_timeout(priv, 100)) {
b481de9c
ZY
4122 IWL_WARNING("Aborted scan still in progress "
4123 "after 100ms\n");
4124 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
4125 mutex_unlock(&priv->mutex);
4126 return -EAGAIN;
4127 }
4128 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
4129
4130 /* TODO: Audit driver for usage of these members and see
4131 * if mac80211 deprecates them (priv->bssid looks like it
4132 * shouldn't be there, but I haven't scanned the IBSS code
4133 * to verify) - jpk */
4134 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
4135
4136 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
bb8c093b 4137 iwl4965_config_ap(priv);
b481de9c 4138 else {
bb8c093b 4139 rc = iwl4965_commit_rxon(priv);
b481de9c 4140 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
4f40e4d9 4141 iwl_rxon_add_station(
b481de9c
ZY
4142 priv, priv->active_rxon.bssid_addr, 1);
4143 }
4144
4145 } else {
bb8c093b 4146 iwl4965_scan_cancel_timeout(priv, 100);
b481de9c 4147 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
bb8c093b 4148 iwl4965_commit_rxon(priv);
b481de9c
ZY
4149 }
4150
fde3571f 4151 done:
b481de9c
ZY
4152 spin_lock_irqsave(&priv->lock, flags);
4153 if (!conf->ssid_len)
4154 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
4155 else
4156 memcpy(priv->essid, conf->ssid, conf->ssid_len);
4157
4158 priv->essid_len = conf->ssid_len;
4159 spin_unlock_irqrestore(&priv->lock, flags);
4160
4161 IWL_DEBUG_MAC80211("leave\n");
4162 mutex_unlock(&priv->mutex);
4163
4164 return 0;
4165}
4166
bb8c093b 4167static void iwl4965_configure_filter(struct ieee80211_hw *hw,
4150c572
JB
4168 unsigned int changed_flags,
4169 unsigned int *total_flags,
4170 int mc_count, struct dev_addr_list *mc_list)
4171{
4172 /*
4173 * XXX: dummy
bb8c093b 4174 * see also iwl4965_connection_init_rx_config
4150c572 4175 */
4419e39b
AK
4176 struct iwl_priv *priv = hw->priv;
4177 int new_flags = 0;
4178 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
4179 if (*total_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
4180 IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
4181 IEEE80211_IF_TYPE_MNTR,
4182 changed_flags, *total_flags);
4183 /* queue work 'cuz mac80211 is holding a lock which
4184 * prevents us from issuing (synchronous) f/w cmds */
4185 queue_work(priv->workqueue, &priv->set_monitor);
4186 new_flags &= FIF_PROMISC_IN_BSS |
4187 FIF_OTHER_BSS |
4188 FIF_ALLMULTI;
4189 }
4190 }
4191 *total_flags = new_flags;
4150c572
JB
4192}
4193
bb8c093b 4194static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
b481de9c
ZY
4195 struct ieee80211_if_init_conf *conf)
4196{
c79dd5b5 4197 struct iwl_priv *priv = hw->priv;
b481de9c
ZY
4198
4199 IWL_DEBUG_MAC80211("enter\n");
4200
4201 mutex_lock(&priv->mutex);
948c171c 4202
fee1247a 4203 if (iwl_is_ready_rf(priv)) {
fde3571f
MA
4204 iwl4965_scan_cancel_timeout(priv, 100);
4205 cancel_delayed_work(&priv->post_associate);
4206 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4207 iwl4965_commit_rxon(priv);
4208 }
32bfd35d
JB
4209 if (priv->vif == conf->vif) {
4210 priv->vif = NULL;
b481de9c
ZY
4211 memset(priv->bssid, 0, ETH_ALEN);
4212 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
4213 priv->essid_len = 0;
4214 }
4215 mutex_unlock(&priv->mutex);
4216
4217 IWL_DEBUG_MAC80211("leave\n");
4218
4219}
471b3efd 4220
3109ece1 4221#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
471b3efd
JB
4222static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
4223 struct ieee80211_vif *vif,
4224 struct ieee80211_bss_conf *bss_conf,
4225 u32 changes)
220173b0 4226{
c79dd5b5 4227 struct iwl_priv *priv = hw->priv;
220173b0 4228
3109ece1
TW
4229 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
4230
471b3efd 4231 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
3109ece1
TW
4232 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
4233 bss_conf->use_short_preamble);
471b3efd 4234 if (bss_conf->use_short_preamble)
220173b0
TW
4235 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
4236 else
4237 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
4238 }
4239
471b3efd 4240 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
3109ece1 4241 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
8318d78a 4242 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
220173b0
TW
4243 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
4244 else
4245 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
4246 }
4247
98952d5d 4248 if (changes & BSS_CHANGED_HT) {
3109ece1 4249 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
98952d5d 4250 iwl4965_ht_conf(priv, bss_conf);
c7de35cd 4251 iwl_set_rxon_chain(priv);
98952d5d
TW
4252 }
4253
471b3efd 4254 if (changes & BSS_CHANGED_ASSOC) {
3109ece1 4255 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
508e32e1
RC
4256 /* This should never happen as this function should
4257 * never be called from interrupt context. */
4258 if (WARN_ON_ONCE(in_interrupt()))
4259 return;
3109ece1
TW
4260 if (bss_conf->assoc) {
4261 priv->assoc_id = bss_conf->aid;
4262 priv->beacon_int = bss_conf->beacon_int;
4263 priv->timestamp = bss_conf->timestamp;
4264 priv->assoc_capability = bss_conf->assoc_capability;
4265 priv->next_scan_jiffies = jiffies +
4266 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
508e32e1
RC
4267 mutex_lock(&priv->mutex);
4268 iwl4965_post_associate(priv);
4269 mutex_unlock(&priv->mutex);
3109ece1
TW
4270 } else {
4271 priv->assoc_id = 0;
4272 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
4273 }
4274 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
4275 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
7e8c519e 4276 iwl_send_rxon_assoc(priv);
471b3efd
JB
4277 }
4278
220173b0 4279}
b481de9c 4280
bb8c093b 4281static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
b481de9c
ZY
4282{
4283 int rc = 0;
4284 unsigned long flags;
c79dd5b5 4285 struct iwl_priv *priv = hw->priv;
b481de9c
ZY
4286
4287 IWL_DEBUG_MAC80211("enter\n");
4288
052c4b9f 4289 mutex_lock(&priv->mutex);
b481de9c
ZY
4290 spin_lock_irqsave(&priv->lock, flags);
4291
fee1247a 4292 if (!iwl_is_ready_rf(priv)) {
b481de9c
ZY
4293 rc = -EIO;
4294 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
4295 goto out_unlock;
4296 }
4297
4298 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
4299 rc = -EIO;
4300 IWL_ERROR("ERROR: APs don't scan\n");
4301 goto out_unlock;
4302 }
4303
7878a5a4
MA
4304 /* we don't schedule scan within next_scan_jiffies period */
4305 if (priv->next_scan_jiffies &&
4306 time_after(priv->next_scan_jiffies, jiffies)) {
4307 rc = -EAGAIN;
4308 goto out_unlock;
4309 }
b481de9c 4310 /* if we just finished scan ask for delay */
7878a5a4
MA
4311 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
4312 IWL_DELAY_NEXT_SCAN, jiffies)) {
b481de9c
ZY
4313 rc = -EAGAIN;
4314 goto out_unlock;
4315 }
4316 if (len) {
7878a5a4 4317 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
bb8c093b 4318 iwl4965_escape_essid(ssid, len), (int)len);
b481de9c
ZY
4319
4320 priv->one_direct_scan = 1;
4321 priv->direct_ssid_len = (u8)
4322 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
4323 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
948c171c
MA
4324 } else
4325 priv->one_direct_scan = 0;
b481de9c 4326
bb8c093b 4327 rc = iwl4965_scan_initiate(priv);
b481de9c
ZY
4328
4329 IWL_DEBUG_MAC80211("leave\n");
4330
4331out_unlock:
4332 spin_unlock_irqrestore(&priv->lock, flags);
052c4b9f 4333 mutex_unlock(&priv->mutex);
b481de9c
ZY
4334
4335 return rc;
4336}
4337
ab885f8c
EG
4338static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
4339 struct ieee80211_key_conf *keyconf, const u8 *addr,
4340 u32 iv32, u16 *phase1key)
4341{
4342 struct iwl_priv *priv = hw->priv;
4343 u8 sta_id = IWL_INVALID_STATION;
4344 unsigned long flags;
4345 __le16 key_flags = 0;
4346 int i;
4347 DECLARE_MAC_BUF(mac);
4348
4349 IWL_DEBUG_MAC80211("enter\n");
4350
947b13a7 4351 sta_id = iwl_find_station(priv, addr);
ab885f8c
EG
4352 if (sta_id == IWL_INVALID_STATION) {
4353 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
4354 print_mac(mac, addr));
4355 return;
4356 }
4357
4358 iwl4965_scan_cancel_timeout(priv, 100);
4359
4360 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
4361 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
4362 key_flags &= ~STA_KEY_FLG_INVALID;
4363
5425e490 4364 if (sta_id == priv->hw_params.bcast_sta_id)
ab885f8c
EG
4365 key_flags |= STA_KEY_MULTICAST_MSK;
4366
4367 spin_lock_irqsave(&priv->sta_lock, flags);
4368
ab885f8c
EG
4369 priv->stations[sta_id].sta.key.key_flags = key_flags;
4370 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
4371
4372 for (i = 0; i < 5; i++)
4373 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
4374 cpu_to_le16(phase1key[i]);
4375
4376 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
4377 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
4378
133636de 4379 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
ab885f8c
EG
4380
4381 spin_unlock_irqrestore(&priv->sta_lock, flags);
4382
4383 IWL_DEBUG_MAC80211("leave\n");
4384}
4385
bb8c093b 4386static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
b481de9c
ZY
4387 const u8 *local_addr, const u8 *addr,
4388 struct ieee80211_key_conf *key)
4389{
c79dd5b5 4390 struct iwl_priv *priv = hw->priv;
0795af57 4391 DECLARE_MAC_BUF(mac);
deb09c43
EG
4392 int ret = 0;
4393 u8 sta_id = IWL_INVALID_STATION;
6974e363 4394 u8 is_default_wep_key = 0;
b481de9c
ZY
4395
4396 IWL_DEBUG_MAC80211("enter\n");
4397
099b40b7 4398 if (priv->hw_params.sw_crypto) {
b481de9c
ZY
4399 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
4400 return -EOPNOTSUPP;
4401 }
4402
4403 if (is_zero_ether_addr(addr))
4404 /* only support pairwise keys */
4405 return -EOPNOTSUPP;
4406
947b13a7 4407 sta_id = iwl_find_station(priv, addr);
6974e363
EG
4408 if (sta_id == IWL_INVALID_STATION) {
4409 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
4410 print_mac(mac, addr));
4411 return -EINVAL;
b481de9c 4412
deb09c43 4413 }
b481de9c 4414
6974e363 4415 mutex_lock(&priv->mutex);
bb8c093b 4416 iwl4965_scan_cancel_timeout(priv, 100);
6974e363
EG
4417 mutex_unlock(&priv->mutex);
4418
4419 /* If we are getting WEP group key and we didn't receive any key mapping
4420 * so far, we are in legacy wep mode (group key only), otherwise we are
4421 * in 1X mode.
4422 * In legacy wep mode, we use another host command to the uCode */
5425e490 4423 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
6974e363
EG
4424 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
4425 if (cmd == SET_KEY)
4426 is_default_wep_key = !priv->key_mapping_key;
4427 else
ccc038ab
EG
4428 is_default_wep_key =
4429 (key->hw_key_idx == HW_KEY_DEFAULT);
6974e363 4430 }
052c4b9f 4431
b481de9c 4432 switch (cmd) {
deb09c43 4433 case SET_KEY:
6974e363
EG
4434 if (is_default_wep_key)
4435 ret = iwl_set_default_wep_key(priv, key);
deb09c43 4436 else
7480513f 4437 ret = iwl_set_dynamic_key(priv, key, sta_id);
deb09c43
EG
4438
4439 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
b481de9c
ZY
4440 break;
4441 case DISABLE_KEY:
6974e363
EG
4442 if (is_default_wep_key)
4443 ret = iwl_remove_default_wep_key(priv, key);
deb09c43 4444 else
3ec47732 4445 ret = iwl_remove_dynamic_key(priv, key, sta_id);
deb09c43
EG
4446
4447 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
b481de9c
ZY
4448 break;
4449 default:
deb09c43 4450 ret = -EINVAL;
b481de9c
ZY
4451 }
4452
4453 IWL_DEBUG_MAC80211("leave\n");
b481de9c 4454
deb09c43 4455 return ret;
b481de9c
ZY
4456}
4457
e100bb64 4458static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
b481de9c
ZY
4459 const struct ieee80211_tx_queue_params *params)
4460{
c79dd5b5 4461 struct iwl_priv *priv = hw->priv;
b481de9c
ZY
4462 unsigned long flags;
4463 int q;
b481de9c
ZY
4464
4465 IWL_DEBUG_MAC80211("enter\n");
4466
fee1247a 4467 if (!iwl_is_ready_rf(priv)) {
b481de9c
ZY
4468 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4469 return -EIO;
4470 }
4471
4472 if (queue >= AC_NUM) {
4473 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
4474 return 0;
4475 }
4476
b481de9c
ZY
4477 if (!priv->qos_data.qos_enable) {
4478 priv->qos_data.qos_active = 0;
4479 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
4480 return 0;
4481 }
4482 q = AC_NUM - 1 - queue;
4483
4484 spin_lock_irqsave(&priv->lock, flags);
4485
4486 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
4487 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
4488 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
4489 priv->qos_data.def_qos_parm.ac[q].edca_txop =
3330d7be 4490 cpu_to_le16((params->txop * 32));
b481de9c
ZY
4491
4492 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
4493 priv->qos_data.qos_active = 1;
4494
4495 spin_unlock_irqrestore(&priv->lock, flags);
4496
4497 mutex_lock(&priv->mutex);
4498 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
bb8c093b 4499 iwl4965_activate_qos(priv, 1);
3109ece1 4500 else if (priv->assoc_id && iwl_is_associated(priv))
bb8c093b 4501 iwl4965_activate_qos(priv, 0);
b481de9c
ZY
4502
4503 mutex_unlock(&priv->mutex);
4504
b481de9c
ZY
4505 IWL_DEBUG_MAC80211("leave\n");
4506 return 0;
4507}
4508
bb8c093b 4509static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
b481de9c
ZY
4510 struct ieee80211_tx_queue_stats *stats)
4511{
c79dd5b5 4512 struct iwl_priv *priv = hw->priv;
b481de9c 4513 int i, avail;
16466903 4514 struct iwl_tx_queue *txq;
443cfd45 4515 struct iwl_queue *q;
b481de9c
ZY
4516 unsigned long flags;
4517
4518 IWL_DEBUG_MAC80211("enter\n");
4519
fee1247a 4520 if (!iwl_is_ready_rf(priv)) {
b481de9c
ZY
4521 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4522 return -EIO;
4523 }
4524
4525 spin_lock_irqsave(&priv->lock, flags);
4526
4527 for (i = 0; i < AC_NUM; i++) {
4528 txq = &priv->txq[i];
4529 q = &txq->q;
443cfd45 4530 avail = iwl_queue_space(q);
b481de9c 4531
57ffc589
JB
4532 stats[i].len = q->n_window - avail;
4533 stats[i].limit = q->n_window - q->high_mark;
4534 stats[i].count = q->n_window;
b481de9c
ZY
4535
4536 }
4537 spin_unlock_irqrestore(&priv->lock, flags);
4538
4539 IWL_DEBUG_MAC80211("leave\n");
4540
4541 return 0;
4542}
4543
bb8c093b 4544static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
b481de9c
ZY
4545 struct ieee80211_low_level_stats *stats)
4546{
bf403db8
EK
4547 struct iwl_priv *priv = hw->priv;
4548
4549 priv = hw->priv;
b481de9c
ZY
4550 IWL_DEBUG_MAC80211("enter\n");
4551 IWL_DEBUG_MAC80211("leave\n");
4552
4553 return 0;
4554}
4555
bb8c093b 4556static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
b481de9c 4557{
bf403db8
EK
4558 struct iwl_priv *priv;
4559
4560 priv = hw->priv;
b481de9c
ZY
4561 IWL_DEBUG_MAC80211("enter\n");
4562 IWL_DEBUG_MAC80211("leave\n");
4563
4564 return 0;
4565}
4566
bb8c093b 4567static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
b481de9c 4568{
c79dd5b5 4569 struct iwl_priv *priv = hw->priv;
b481de9c
ZY
4570 unsigned long flags;
4571
4572 mutex_lock(&priv->mutex);
4573 IWL_DEBUG_MAC80211("enter\n");
4574
4575 priv->lq_mngr.lq_ready = 0;
c8b0e6e1 4576#ifdef CONFIG_IWL4965_HT
b481de9c 4577 spin_lock_irqsave(&priv->lock, flags);
fd105e79 4578 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
b481de9c 4579 spin_unlock_irqrestore(&priv->lock, flags);
c8b0e6e1 4580#endif /* CONFIG_IWL4965_HT */
b481de9c 4581
c7de35cd 4582 iwl_reset_qos(priv);
b481de9c
ZY
4583
4584 cancel_delayed_work(&priv->post_associate);
4585
4586 spin_lock_irqsave(&priv->lock, flags);
4587 priv->assoc_id = 0;
4588 priv->assoc_capability = 0;
b481de9c
ZY
4589 priv->assoc_station_added = 0;
4590
4591 /* new association get rid of ibss beacon skb */
4592 if (priv->ibss_beacon)
4593 dev_kfree_skb(priv->ibss_beacon);
4594
4595 priv->ibss_beacon = NULL;
4596
4597 priv->beacon_int = priv->hw->conf.beacon_int;
3109ece1 4598 priv->timestamp = 0;
b481de9c
ZY
4599 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
4600 priv->beacon_int = 0;
4601
4602 spin_unlock_irqrestore(&priv->lock, flags);
4603
fee1247a 4604 if (!iwl_is_ready_rf(priv)) {
fde3571f
MA
4605 IWL_DEBUG_MAC80211("leave - not ready\n");
4606 mutex_unlock(&priv->mutex);
4607 return;
4608 }
4609
052c4b9f 4610 /* we are restarting association process
4611 * clear RXON_FILTER_ASSOC_MSK bit
4612 */
4613 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
bb8c093b 4614 iwl4965_scan_cancel_timeout(priv, 100);
052c4b9f 4615 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
bb8c093b 4616 iwl4965_commit_rxon(priv);
052c4b9f 4617 }
4618
5da4b55f
MA
4619 iwl_power_update_mode(priv, 0);
4620
b481de9c
ZY
4621 /* Per mac80211.h: This is only used in IBSS mode... */
4622 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
052c4b9f 4623
b481de9c
ZY
4624 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
4625 mutex_unlock(&priv->mutex);
4626 return;
4627 }
4628
bb8c093b 4629 iwl4965_set_rate(priv);
b481de9c
ZY
4630
4631 mutex_unlock(&priv->mutex);
4632
4633 IWL_DEBUG_MAC80211("leave\n");
b481de9c
ZY
4634}
4635
e039fa4a 4636static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
b481de9c 4637{
c79dd5b5 4638 struct iwl_priv *priv = hw->priv;
b481de9c
ZY
4639 unsigned long flags;
4640
4641 mutex_lock(&priv->mutex);
4642 IWL_DEBUG_MAC80211("enter\n");
4643
fee1247a 4644 if (!iwl_is_ready_rf(priv)) {
b481de9c
ZY
4645 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4646 mutex_unlock(&priv->mutex);
4647 return -EIO;
4648 }
4649
4650 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
4651 IWL_DEBUG_MAC80211("leave - not IBSS\n");
4652 mutex_unlock(&priv->mutex);
4653 return -EIO;
4654 }
4655
4656 spin_lock_irqsave(&priv->lock, flags);
4657
4658 if (priv->ibss_beacon)
4659 dev_kfree_skb(priv->ibss_beacon);
4660
4661 priv->ibss_beacon = skb;
4662
4663 priv->assoc_id = 0;
4664
4665 IWL_DEBUG_MAC80211("leave\n");
4666 spin_unlock_irqrestore(&priv->lock, flags);
4667
c7de35cd 4668 iwl_reset_qos(priv);
b481de9c
ZY
4669
4670 queue_work(priv->workqueue, &priv->post_associate.work);
4671
4672 mutex_unlock(&priv->mutex);
4673
4674 return 0;
4675}
4676
b481de9c
ZY
4677/*****************************************************************************
4678 *
4679 * sysfs attributes
4680 *
4681 *****************************************************************************/
4682
0a6857e7 4683#ifdef CONFIG_IWLWIFI_DEBUG
b481de9c
ZY
4684
4685/*
4686 * The following adds a new attribute to the sysfs representation
4687 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
4688 * used for controlling the debug level.
4689 *
4690 * See the level definitions in iwl for details.
4691 */
4692
8cf769c6
EK
4693static ssize_t show_debug_level(struct device *d,
4694 struct device_attribute *attr, char *buf)
b481de9c 4695{
8cf769c6
EK
4696 struct iwl_priv *priv = d->driver_data;
4697
4698 return sprintf(buf, "0x%08X\n", priv->debug_level);
b481de9c 4699}
8cf769c6
EK
4700static ssize_t store_debug_level(struct device *d,
4701 struct device_attribute *attr,
b481de9c
ZY
4702 const char *buf, size_t count)
4703{
8cf769c6 4704 struct iwl_priv *priv = d->driver_data;
b481de9c
ZY
4705 char *p = (char *)buf;
4706 u32 val;
4707
4708 val = simple_strtoul(p, &p, 0);
4709 if (p == buf)
4710 printk(KERN_INFO DRV_NAME
4711 ": %s is not in hex or decimal form.\n", buf);
4712 else
8cf769c6 4713 priv->debug_level = val;
b481de9c
ZY
4714
4715 return strnlen(buf, count);
4716}
4717
8cf769c6
EK
4718static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
4719 show_debug_level, store_debug_level);
4720
b481de9c 4721
0a6857e7 4722#endif /* CONFIG_IWLWIFI_DEBUG */
b481de9c 4723
b481de9c 4724
bc6f59bc
TW
4725static ssize_t show_version(struct device *d,
4726 struct device_attribute *attr, char *buf)
4727{
4728 struct iwl_priv *priv = d->driver_data;
885ba202 4729 struct iwl_alive_resp *palive = &priv->card_alive;
bc6f59bc
TW
4730
4731 if (palive->is_valid)
4732 return sprintf(buf, "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
4733 "fw type: 0x%01X 0x%01X\n",
4734 palive->ucode_major, palive->ucode_minor,
4735 palive->sw_rev[0], palive->sw_rev[1],
4736 palive->ver_type, palive->ver_subtype);
4737
4738 else
4739 return sprintf(buf, "fw not loaded\n");
4740}
4741
4742static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
4743
b481de9c
ZY
4744static ssize_t show_temperature(struct device *d,
4745 struct device_attribute *attr, char *buf)
4746{
c79dd5b5 4747 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
b481de9c 4748
fee1247a 4749 if (!iwl_is_alive(priv))
b481de9c
ZY
4750 return -EAGAIN;
4751
bb8c093b 4752 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
b481de9c
ZY
4753}
4754
4755static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
4756
4757static ssize_t show_rs_window(struct device *d,
4758 struct device_attribute *attr,
4759 char *buf)
4760{
c79dd5b5 4761 struct iwl_priv *priv = d->driver_data;
bb8c093b 4762 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
b481de9c
ZY
4763}
4764static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
4765
4766static ssize_t show_tx_power(struct device *d,
4767 struct device_attribute *attr, char *buf)
4768{
c79dd5b5 4769 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
b481de9c
ZY
4770 return sprintf(buf, "%d\n", priv->user_txpower_limit);
4771}
4772
4773static ssize_t store_tx_power(struct device *d,
4774 struct device_attribute *attr,
4775 const char *buf, size_t count)
4776{
c79dd5b5 4777 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
b481de9c
ZY
4778 char *p = (char *)buf;
4779 u32 val;
4780
4781 val = simple_strtoul(p, &p, 10);
4782 if (p == buf)
4783 printk(KERN_INFO DRV_NAME
4784 ": %s is not in decimal form.\n", buf);
4785 else
bb8c093b 4786 iwl4965_hw_reg_set_txpower(priv, val);
b481de9c
ZY
4787
4788 return count;
4789}
4790
4791static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
4792
4793static ssize_t show_flags(struct device *d,
4794 struct device_attribute *attr, char *buf)
4795{
c79dd5b5 4796 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
b481de9c
ZY
4797
4798 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
4799}
4800
4801static ssize_t store_flags(struct device *d,
4802 struct device_attribute *attr,
4803 const char *buf, size_t count)
4804{
c79dd5b5 4805 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
b481de9c
ZY
4806 u32 flags = simple_strtoul(buf, NULL, 0);
4807
4808 mutex_lock(&priv->mutex);
4809 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
4810 /* Cancel any currently running scans... */
bb8c093b 4811 if (iwl4965_scan_cancel_timeout(priv, 100))
b481de9c
ZY
4812 IWL_WARNING("Could not cancel scan.\n");
4813 else {
4814 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
4815 flags);
4816 priv->staging_rxon.flags = cpu_to_le32(flags);
bb8c093b 4817 iwl4965_commit_rxon(priv);
b481de9c
ZY
4818 }
4819 }
4820 mutex_unlock(&priv->mutex);
4821
4822 return count;
4823}
4824
4825static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
4826
4827static ssize_t show_filter_flags(struct device *d,
4828 struct device_attribute *attr, char *buf)
4829{
c79dd5b5 4830 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
b481de9c
ZY
4831
4832 return sprintf(buf, "0x%04X\n",
4833 le32_to_cpu(priv->active_rxon.filter_flags));
4834}
4835
4836static ssize_t store_filter_flags(struct device *d,
4837 struct device_attribute *attr,
4838 const char *buf, size_t count)
4839{
c79dd5b5 4840 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
b481de9c
ZY
4841 u32 filter_flags = simple_strtoul(buf, NULL, 0);
4842
4843 mutex_lock(&priv->mutex);
4844 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
4845 /* Cancel any currently running scans... */
bb8c093b 4846 if (iwl4965_scan_cancel_timeout(priv, 100))
b481de9c
ZY
4847 IWL_WARNING("Could not cancel scan.\n");
4848 else {
4849 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
4850 "0x%04X\n", filter_flags);
4851 priv->staging_rxon.filter_flags =
4852 cpu_to_le32(filter_flags);
bb8c093b 4853 iwl4965_commit_rxon(priv);
b481de9c
ZY
4854 }
4855 }
4856 mutex_unlock(&priv->mutex);
4857
4858 return count;
4859}
4860
4861static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
4862 store_filter_flags);
4863
c8b0e6e1 4864#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
b481de9c
ZY
4865
4866static ssize_t show_measurement(struct device *d,
4867 struct device_attribute *attr, char *buf)
4868{
c79dd5b5 4869 struct iwl_priv *priv = dev_get_drvdata(d);
bb8c093b 4870 struct iwl4965_spectrum_notification measure_report;
b481de9c
ZY
4871 u32 size = sizeof(measure_report), len = 0, ofs = 0;
4872 u8 *data = (u8 *) & measure_report;
4873 unsigned long flags;
4874
4875 spin_lock_irqsave(&priv->lock, flags);
4876 if (!(priv->measurement_status & MEASUREMENT_READY)) {
4877 spin_unlock_irqrestore(&priv->lock, flags);
4878 return 0;
4879 }
4880 memcpy(&measure_report, &priv->measure_report, size);
4881 priv->measurement_status = 0;
4882 spin_unlock_irqrestore(&priv->lock, flags);
4883
4884 while (size && (PAGE_SIZE - len)) {
4885 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4886 PAGE_SIZE - len, 1);
4887 len = strlen(buf);
4888 if (PAGE_SIZE - len)
4889 buf[len++] = '\n';
4890
4891 ofs += 16;
4892 size -= min(size, 16U);
4893 }
4894
4895 return len;
4896}
4897
4898static ssize_t store_measurement(struct device *d,
4899 struct device_attribute *attr,
4900 const char *buf, size_t count)
4901{
c79dd5b5 4902 struct iwl_priv *priv = dev_get_drvdata(d);
b481de9c
ZY
4903 struct ieee80211_measurement_params params = {
4904 .channel = le16_to_cpu(priv->active_rxon.channel),
4905 .start_time = cpu_to_le64(priv->last_tsf),
4906 .duration = cpu_to_le16(1),
4907 };
4908 u8 type = IWL_MEASURE_BASIC;
4909 u8 buffer[32];
4910 u8 channel;
4911
4912 if (count) {
4913 char *p = buffer;
4914 strncpy(buffer, buf, min(sizeof(buffer), count));
4915 channel = simple_strtoul(p, NULL, 0);
4916 if (channel)
4917 params.channel = channel;
4918
4919 p = buffer;
4920 while (*p && *p != ' ')
4921 p++;
4922 if (*p)
4923 type = simple_strtoul(p + 1, NULL, 0);
4924 }
4925
4926 IWL_DEBUG_INFO("Invoking measurement of type %d on "
4927 "channel %d (for '%s')\n", type, params.channel, buf);
bb8c093b 4928 iwl4965_get_measurement(priv, &params, type);
b481de9c
ZY
4929
4930 return count;
4931}
4932
4933static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
4934 show_measurement, store_measurement);
c8b0e6e1 4935#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
b481de9c
ZY
4936
4937static ssize_t store_retry_rate(struct device *d,
4938 struct device_attribute *attr,
4939 const char *buf, size_t count)
4940{
c79dd5b5 4941 struct iwl_priv *priv = dev_get_drvdata(d);
b481de9c
ZY
4942
4943 priv->retry_rate = simple_strtoul(buf, NULL, 0);
4944 if (priv->retry_rate <= 0)
4945 priv->retry_rate = 1;
4946
4947 return count;
4948}
4949
4950static ssize_t show_retry_rate(struct device *d,
4951 struct device_attribute *attr, char *buf)
4952{
c79dd5b5 4953 struct iwl_priv *priv = dev_get_drvdata(d);
b481de9c
ZY
4954 return sprintf(buf, "%d", priv->retry_rate);
4955}
4956
4957static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
4958 store_retry_rate);
4959
4960static ssize_t store_power_level(struct device *d,
4961 struct device_attribute *attr,
4962 const char *buf, size_t count)
4963{
c79dd5b5 4964 struct iwl_priv *priv = dev_get_drvdata(d);
b481de9c
ZY
4965 int rc;
4966 int mode;
4967
4968 mode = simple_strtoul(buf, NULL, 0);
4969 mutex_lock(&priv->mutex);
4970
fee1247a 4971 if (!iwl_is_ready(priv)) {
b481de9c
ZY
4972 rc = -EAGAIN;
4973 goto out;
4974 }
4975
5da4b55f
MA
4976 rc = iwl_power_set_user_mode(priv, mode);
4977 if (rc) {
4978 IWL_DEBUG_MAC80211("failed setting power mode.\n");
4979 goto out;
b481de9c 4980 }
b481de9c
ZY
4981 rc = count;
4982
4983 out:
4984 mutex_unlock(&priv->mutex);
4985 return rc;
4986}
4987
4988#define MAX_WX_STRING 80
4989
4990/* Values are in microsecond */
4991static const s32 timeout_duration[] = {
4992 350000,
4993 250000,
4994 75000,
4995 37000,
4996 25000,
4997};
4998static const s32 period_duration[] = {
4999 400000,
5000 700000,
5001 1000000,
5002 1000000,
5003 1000000
5004};
5005
5006static ssize_t show_power_level(struct device *d,
5007 struct device_attribute *attr, char *buf)
5008{
c79dd5b5 5009 struct iwl_priv *priv = dev_get_drvdata(d);
5da4b55f 5010 int level = priv->power_data.power_mode;
b481de9c
ZY
5011 char *p = buf;
5012
5013 p += sprintf(p, "%d ", level);
5014 switch (level) {
5015 case IWL_POWER_MODE_CAM:
5016 case IWL_POWER_AC:
5017 p += sprintf(p, "(AC)");
5018 break;
5019 case IWL_POWER_BATTERY:
5020 p += sprintf(p, "(BATTERY)");
5021 break;
5022 default:
5023 p += sprintf(p,
5024 "(Timeout %dms, Period %dms)",
5025 timeout_duration[level - 1] / 1000,
5026 period_duration[level - 1] / 1000);
5027 }
5da4b55f 5028/*
b481de9c
ZY
5029 if (!(priv->power_mode & IWL_POWER_ENABLED))
5030 p += sprintf(p, " OFF\n");
5031 else
5032 p += sprintf(p, " \n");
5da4b55f
MA
5033*/
5034 p += sprintf(p, " \n");
b481de9c 5035 return (p - buf + 1);
b481de9c
ZY
5036}
5037
5038static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
5039 store_power_level);
5040
5041static ssize_t show_channels(struct device *d,
5042 struct device_attribute *attr, char *buf)
5043{
8318d78a
JB
5044 /* all this shit doesn't belong into sysfs anyway */
5045 return 0;
b481de9c
ZY
5046}
5047
5048static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
5049
5050static ssize_t show_statistics(struct device *d,
5051 struct device_attribute *attr, char *buf)
5052{
c79dd5b5 5053 struct iwl_priv *priv = dev_get_drvdata(d);
bb8c093b 5054 u32 size = sizeof(struct iwl4965_notif_statistics);
b481de9c
ZY
5055 u32 len = 0, ofs = 0;
5056 u8 *data = (u8 *) & priv->statistics;
5057 int rc = 0;
5058
fee1247a 5059 if (!iwl_is_alive(priv))
b481de9c
ZY
5060 return -EAGAIN;
5061
5062 mutex_lock(&priv->mutex);
49ea8596 5063 rc = iwl_send_statistics_request(priv, 0);
b481de9c
ZY
5064 mutex_unlock(&priv->mutex);
5065
5066 if (rc) {
5067 len = sprintf(buf,
5068 "Error sending statistics request: 0x%08X\n", rc);
5069 return len;
5070 }
5071
5072 while (size && (PAGE_SIZE - len)) {
5073 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
5074 PAGE_SIZE - len, 1);
5075 len = strlen(buf);
5076 if (PAGE_SIZE - len)
5077 buf[len++] = '\n';
5078
5079 ofs += 16;
5080 size -= min(size, 16U);
5081 }
5082
5083 return len;
5084}
5085
5086static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
5087
b481de9c
ZY
5088static ssize_t show_status(struct device *d,
5089 struct device_attribute *attr, char *buf)
5090{
c79dd5b5 5091 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
fee1247a 5092 if (!iwl_is_alive(priv))
b481de9c
ZY
5093 return -EAGAIN;
5094 return sprintf(buf, "0x%08x\n", (int)priv->status);
5095}
5096
5097static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
5098
b481de9c
ZY
5099/*****************************************************************************
5100 *
5101 * driver setup and teardown
5102 *
5103 *****************************************************************************/
5104
c79dd5b5 5105static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
b481de9c
ZY
5106{
5107 priv->workqueue = create_workqueue(DRV_NAME);
5108
5109 init_waitqueue_head(&priv->wait_command_queue);
5110
bb8c093b
CH
5111 INIT_WORK(&priv->up, iwl4965_bg_up);
5112 INIT_WORK(&priv->restart, iwl4965_bg_restart);
5113 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
5114 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
5115 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
5116 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
5117 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
5118 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
4419e39b 5119 INIT_WORK(&priv->set_monitor, iwl4965_bg_set_monitor);
bb8c093b 5120 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
4a4a9e81
TW
5121 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
5122 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
bb8c093b
CH
5123 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
5124
5125 iwl4965_hw_setup_deferred_work(priv);
b481de9c
ZY
5126
5127 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
bb8c093b 5128 iwl4965_irq_tasklet, (unsigned long)priv);
b481de9c
ZY
5129}
5130
c79dd5b5 5131static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
b481de9c 5132{
bb8c093b 5133 iwl4965_hw_cancel_deferred_work(priv);
b481de9c 5134
3ae6a054 5135 cancel_delayed_work_sync(&priv->init_alive_start);
b481de9c
ZY
5136 cancel_delayed_work(&priv->scan_check);
5137 cancel_delayed_work(&priv->alive_start);
5138 cancel_delayed_work(&priv->post_associate);
5139 cancel_work_sync(&priv->beacon_update);
5140}
5141
bb8c093b 5142static struct attribute *iwl4965_sysfs_entries[] = {
b481de9c 5143 &dev_attr_channels.attr,
b481de9c
ZY
5144 &dev_attr_flags.attr,
5145 &dev_attr_filter_flags.attr,
c8b0e6e1 5146#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
b481de9c
ZY
5147 &dev_attr_measurement.attr,
5148#endif
5149 &dev_attr_power_level.attr,
5150 &dev_attr_retry_rate.attr,
b481de9c
ZY
5151 &dev_attr_rs_window.attr,
5152 &dev_attr_statistics.attr,
5153 &dev_attr_status.attr,
5154 &dev_attr_temperature.attr,
b481de9c 5155 &dev_attr_tx_power.attr,
8cf769c6
EK
5156#ifdef CONFIG_IWLWIFI_DEBUG
5157 &dev_attr_debug_level.attr,
5158#endif
bc6f59bc 5159 &dev_attr_version.attr,
b481de9c
ZY
5160
5161 NULL
5162};
5163
bb8c093b 5164static struct attribute_group iwl4965_attribute_group = {
b481de9c 5165 .name = NULL, /* put in device directory */
bb8c093b 5166 .attrs = iwl4965_sysfs_entries,
b481de9c
ZY
5167};
5168
bb8c093b
CH
5169static struct ieee80211_ops iwl4965_hw_ops = {
5170 .tx = iwl4965_mac_tx,
5171 .start = iwl4965_mac_start,
5172 .stop = iwl4965_mac_stop,
5173 .add_interface = iwl4965_mac_add_interface,
5174 .remove_interface = iwl4965_mac_remove_interface,
5175 .config = iwl4965_mac_config,
5176 .config_interface = iwl4965_mac_config_interface,
5177 .configure_filter = iwl4965_configure_filter,
5178 .set_key = iwl4965_mac_set_key,
ab885f8c 5179 .update_tkip_key = iwl4965_mac_update_tkip_key,
bb8c093b
CH
5180 .get_stats = iwl4965_mac_get_stats,
5181 .get_tx_stats = iwl4965_mac_get_tx_stats,
5182 .conf_tx = iwl4965_mac_conf_tx,
5183 .get_tsf = iwl4965_mac_get_tsf,
5184 .reset_tsf = iwl4965_mac_reset_tsf,
5185 .beacon_update = iwl4965_mac_beacon_update,
471b3efd 5186 .bss_info_changed = iwl4965_bss_info_changed,
c8b0e6e1 5187#ifdef CONFIG_IWL4965_HT
9ab46173 5188 .ampdu_action = iwl4965_mac_ampdu_action,
c8b0e6e1 5189#endif /* CONFIG_IWL4965_HT */
bb8c093b 5190 .hw_scan = iwl4965_mac_hw_scan
b481de9c
ZY
5191};
5192
bb8c093b 5193static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
b481de9c
ZY
5194{
5195 int err = 0;
c79dd5b5 5196 struct iwl_priv *priv;
b481de9c 5197 struct ieee80211_hw *hw;
82b9a121 5198 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
0359facc 5199 unsigned long flags;
5a66926a 5200 DECLARE_MAC_BUF(mac);
b481de9c 5201
316c30d9
AK
5202 /************************
5203 * 1. Allocating HW data
5204 ************************/
5205
6440adb5
CB
5206 /* Disabling hardware scan means that mac80211 will perform scans
5207 * "the hard way", rather than using device's scan. */
1ea87396 5208 if (cfg->mod_params->disable_hw_scan) {
bf403db8
EK
5209 if (cfg->mod_params->debug & IWL_DL_INFO)
5210 dev_printk(KERN_DEBUG, &(pdev->dev),
5211 "Disabling hw_scan\n");
bb8c093b 5212 iwl4965_hw_ops.hw_scan = NULL;
b481de9c
ZY
5213 }
5214
1d0a082d
AK
5215 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
5216 if (!hw) {
b481de9c
ZY
5217 err = -ENOMEM;
5218 goto out;
5219 }
1d0a082d
AK
5220 priv = hw->priv;
5221 /* At this point both hw and priv are allocated. */
5222
b481de9c
ZY
5223 SET_IEEE80211_DEV(hw, &pdev->dev);
5224
5225 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
82b9a121 5226 priv->cfg = cfg;
b481de9c 5227 priv->pci_dev = pdev;
316c30d9 5228
0a6857e7 5229#ifdef CONFIG_IWLWIFI_DEBUG
bf403db8 5230 priv->debug_level = priv->cfg->mod_params->debug;
b481de9c
ZY
5231 atomic_set(&priv->restrict_refcnt, 0);
5232#endif
b481de9c 5233
316c30d9
AK
5234 /**************************
5235 * 2. Initializing PCI bus
5236 **************************/
5237 if (pci_enable_device(pdev)) {
5238 err = -ENODEV;
5239 goto out_ieee80211_free_hw;
5240 }
5241
5242 pci_set_master(pdev);
5243
cc2a8ea8 5244 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
316c30d9 5245 if (!err)
cc2a8ea8
RR
5246 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
5247 if (err) {
5248 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
5249 if (!err)
5250 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
5251 /* both attempts failed: */
316c30d9 5252 if (err) {
cc2a8ea8
RR
5253 printk(KERN_WARNING "%s: No suitable DMA available.\n",
5254 DRV_NAME);
316c30d9 5255 goto out_pci_disable_device;
cc2a8ea8 5256 }
316c30d9
AK
5257 }
5258
5259 err = pci_request_regions(pdev, DRV_NAME);
5260 if (err)
5261 goto out_pci_disable_device;
5262
5263 pci_set_drvdata(pdev, priv);
5264
5265 /* We disable the RETRY_TIMEOUT register (0x41) to keep
5266 * PCI Tx retries from interfering with C3 CPU state */
5267 pci_write_config_byte(pdev, 0x41, 0x00);
5268
5269 /***********************
5270 * 3. Read REV register
5271 ***********************/
5272 priv->hw_base = pci_iomap(pdev, 0, 0);
5273 if (!priv->hw_base) {
5274 err = -ENODEV;
5275 goto out_pci_release_regions;
5276 }
5277
5278 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
5279 (unsigned long long) pci_resource_len(pdev, 0));
5280 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
5281
b661c819 5282 iwl_hw_detect(priv);
316c30d9 5283 printk(KERN_INFO DRV_NAME
b661c819
TW
5284 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
5285 priv->cfg->name, priv->hw_rev);
316c30d9 5286
91238714
TW
5287 /* amp init */
5288 err = priv->cfg->ops->lib->apm_ops.init(priv);
316c30d9 5289 if (err < 0) {
91238714 5290 IWL_DEBUG_INFO("Failed to init APMG\n");
316c30d9
AK
5291 goto out_iounmap;
5292 }
91238714
TW
5293 /*****************
5294 * 4. Read EEPROM
5295 *****************/
316c30d9
AK
5296 /* Read the EEPROM */
5297 err = iwl_eeprom_init(priv);
5298 if (err) {
5299 IWL_ERROR("Unable to init EEPROM\n");
5300 goto out_iounmap;
5301 }
8614f360
TW
5302 err = iwl_eeprom_check_version(priv);
5303 if (err)
5304 goto out_iounmap;
5305
02883017 5306 /* extract MAC Address */
316c30d9
AK
5307 iwl_eeprom_get_mac(priv, priv->mac_addr);
5308 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
5309 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
5310
5311 /************************
5312 * 5. Setup HW constants
5313 ************************/
5314 /* Device-specific setup */
5425e490
TW
5315 if (priv->cfg->ops->lib->set_hw_params(priv)) {
5316 IWL_ERROR("failed to set hw parameters\n");
073d3f5f 5317 goto out_free_eeprom;
316c30d9
AK
5318 }
5319
5320 /*******************
6ba87956 5321 * 6. Setup priv
316c30d9 5322 *******************/
b481de9c 5323
6ba87956 5324 err = iwl_init_drv(priv);
bf85ea4f 5325 if (err)
399f4900 5326 goto out_free_eeprom;
bf85ea4f 5327 /* At this point both hw and priv are initialized. */
316c30d9
AK
5328
5329 /**********************************
5330 * 7. Initialize module parameters
5331 **********************************/
5332
5333 /* Disable radio (SW RF KILL) via parameter when loading driver */
1ea87396 5334 if (priv->cfg->mod_params->disable) {
316c30d9
AK
5335 set_bit(STATUS_RF_KILL_SW, &priv->status);
5336 IWL_DEBUG_INFO("Radio disabled.\n");
5337 }
5338
316c30d9
AK
5339 /********************
5340 * 8. Setup services
5341 ********************/
0359facc 5342 spin_lock_irqsave(&priv->lock, flags);
316c30d9 5343 iwl4965_disable_interrupts(priv);
0359facc 5344 spin_unlock_irqrestore(&priv->lock, flags);
316c30d9
AK
5345
5346 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
5347 if (err) {
5348 IWL_ERROR("failed to create sysfs device attributes\n");
6ba87956 5349 goto out_uninit_drv;
316c30d9
AK
5350 }
5351
316c30d9
AK
5352
5353 iwl4965_setup_deferred_work(priv);
5354 iwl4965_setup_rx_handlers(priv);
5355
5356 /********************
5357 * 9. Conclude
5358 ********************/
5a66926a
ZY
5359 pci_save_state(pdev);
5360 pci_disable_device(pdev);
b481de9c 5361
6ba87956
TW
5362 /**********************************
5363 * 10. Setup and register mac80211
5364 **********************************/
5365
5366 err = iwl_setup_mac(priv);
5367 if (err)
5368 goto out_remove_sysfs;
5369
5370 err = iwl_dbgfs_register(priv, DRV_NAME);
5371 if (err)
5372 IWL_ERROR("failed to create debugfs files\n");
5373
c8381fdc
MA
5374 /* notify iwlcore to init */
5375 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
b481de9c
ZY
5376 return 0;
5377
316c30d9
AK
5378 out_remove_sysfs:
5379 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
6ba87956
TW
5380 out_uninit_drv:
5381 iwl_uninit_drv(priv);
073d3f5f
TW
5382 out_free_eeprom:
5383 iwl_eeprom_free(priv);
b481de9c
ZY
5384 out_iounmap:
5385 pci_iounmap(pdev, priv->hw_base);
5386 out_pci_release_regions:
5387 pci_release_regions(pdev);
316c30d9 5388 pci_set_drvdata(pdev, NULL);
b481de9c
ZY
5389 out_pci_disable_device:
5390 pci_disable_device(pdev);
b481de9c
ZY
5391 out_ieee80211_free_hw:
5392 ieee80211_free_hw(priv->hw);
5393 out:
5394 return err;
5395}
5396
c83dbf68 5397static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
b481de9c 5398{
c79dd5b5 5399 struct iwl_priv *priv = pci_get_drvdata(pdev);
b481de9c
ZY
5400 struct list_head *p, *q;
5401 int i;
0359facc 5402 unsigned long flags;
b481de9c
ZY
5403
5404 if (!priv)
5405 return;
5406
5407 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
5408
c4f55232
RR
5409 if (priv->mac80211_registered) {
5410 ieee80211_unregister_hw(priv->hw);
5411 priv->mac80211_registered = 0;
5412 }
5413
b481de9c 5414 set_bit(STATUS_EXIT_PENDING, &priv->status);
b24d22b1 5415
bb8c093b 5416 iwl4965_down(priv);
b481de9c 5417
0359facc
MA
5418 /* make sure we flush any pending irq or
5419 * tasklet for the driver
5420 */
5421 spin_lock_irqsave(&priv->lock, flags);
5422 iwl4965_disable_interrupts(priv);
5423 spin_unlock_irqrestore(&priv->lock, flags);
5424
5425 iwl_synchronize_irq(priv);
5426
b481de9c
ZY
5427 /* Free MAC hash list for ADHOC */
5428 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
5429 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
5430 list_del(p);
bb8c093b 5431 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
b481de9c
ZY
5432 }
5433 }
5434
c8381fdc 5435 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
712b6cf5 5436 iwl_dbgfs_unregister(priv);
bb8c093b 5437 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
b481de9c 5438
bb8c093b 5439 iwl4965_dealloc_ucode_pci(priv);
b481de9c
ZY
5440
5441 if (priv->rxq.bd)
a55360e4 5442 iwl_rx_queue_free(priv, &priv->rxq);
1053d35f 5443 iwl_hw_txq_ctx_free(priv);
b481de9c 5444
bf85ea4f 5445 iwlcore_clear_stations_table(priv);
073d3f5f 5446 iwl_eeprom_free(priv);
b481de9c 5447
b481de9c 5448
948c171c
MA
5449 /*netif_stop_queue(dev); */
5450 flush_workqueue(priv->workqueue);
5451
bb8c093b 5452 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
b481de9c
ZY
5453 * priv->workqueue... so we can't take down the workqueue
5454 * until now... */
5455 destroy_workqueue(priv->workqueue);
5456 priv->workqueue = NULL;
5457
b481de9c
ZY
5458 pci_iounmap(pdev, priv->hw_base);
5459 pci_release_regions(pdev);
5460 pci_disable_device(pdev);
5461 pci_set_drvdata(pdev, NULL);
5462
6ba87956 5463 iwl_uninit_drv(priv);
b481de9c
ZY
5464
5465 if (priv->ibss_beacon)
5466 dev_kfree_skb(priv->ibss_beacon);
5467
5468 ieee80211_free_hw(priv->hw);
5469}
5470
5471#ifdef CONFIG_PM
5472
bb8c093b 5473static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
b481de9c 5474{
c79dd5b5 5475 struct iwl_priv *priv = pci_get_drvdata(pdev);
b481de9c 5476
e655b9f0
ZY
5477 if (priv->is_open) {
5478 set_bit(STATUS_IN_SUSPEND, &priv->status);
5479 iwl4965_mac_stop(priv->hw);
5480 priv->is_open = 1;
5481 }
b481de9c 5482
b481de9c
ZY
5483 pci_set_power_state(pdev, PCI_D3hot);
5484
b481de9c
ZY
5485 return 0;
5486}
5487
bb8c093b 5488static int iwl4965_pci_resume(struct pci_dev *pdev)
b481de9c 5489{
c79dd5b5 5490 struct iwl_priv *priv = pci_get_drvdata(pdev);
b481de9c 5491
b481de9c 5492 pci_set_power_state(pdev, PCI_D0);
b481de9c 5493
e655b9f0
ZY
5494 if (priv->is_open)
5495 iwl4965_mac_start(priv->hw);
b481de9c 5496
e655b9f0 5497 clear_bit(STATUS_IN_SUSPEND, &priv->status);
b481de9c
ZY
5498 return 0;
5499}
5500
5501#endif /* CONFIG_PM */
5502
5503/*****************************************************************************
5504 *
5505 * driver and module entry point
5506 *
5507 *****************************************************************************/
5508
fed9017e
RR
5509/* Hardware specific file defines the PCI IDs table for that hardware module */
5510static struct pci_device_id iwl_hw_card_ids[] = {
5511 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
5512 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
5a6a256e
TW
5513#ifdef CONFIG_IWL5000
5514 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
5515 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
5516 {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
5517#endif /* CONFIG_IWL5000 */
fed9017e
RR
5518 {0}
5519};
5520MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
5521
5522static struct pci_driver iwl_driver = {
b481de9c 5523 .name = DRV_NAME,
fed9017e 5524 .id_table = iwl_hw_card_ids,
bb8c093b
CH
5525 .probe = iwl4965_pci_probe,
5526 .remove = __devexit_p(iwl4965_pci_remove),
b481de9c 5527#ifdef CONFIG_PM
bb8c093b
CH
5528 .suspend = iwl4965_pci_suspend,
5529 .resume = iwl4965_pci_resume,
b481de9c
ZY
5530#endif
5531};
5532
bb8c093b 5533static int __init iwl4965_init(void)
b481de9c
ZY
5534{
5535
5536 int ret;
5537 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
5538 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
897e1cf2
RC
5539
5540 ret = iwl4965_rate_control_register();
5541 if (ret) {
5542 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
5543 return ret;
5544 }
5545
fed9017e 5546 ret = pci_register_driver(&iwl_driver);
b481de9c
ZY
5547 if (ret) {
5548 IWL_ERROR("Unable to initialize PCI module\n");
897e1cf2 5549 goto error_register;
b481de9c 5550 }
b481de9c
ZY
5551
5552 return ret;
897e1cf2 5553
897e1cf2
RC
5554error_register:
5555 iwl4965_rate_control_unregister();
5556 return ret;
b481de9c
ZY
5557}
5558
bb8c093b 5559static void __exit iwl4965_exit(void)
b481de9c 5560{
fed9017e 5561 pci_unregister_driver(&iwl_driver);
897e1cf2 5562 iwl4965_rate_control_unregister();
b481de9c
ZY
5563}
5564
bb8c093b
CH
5565module_exit(iwl4965_exit);
5566module_init(iwl4965_init);
This page took 0.70887 seconds and 5 git commands to generate.