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