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