mac80211: fix bugs in queue handling functions
[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
ZY
1608{
1609
1610 tx_sta->status.ack_signal = 0;
1611 tx_sta->status.excessive_retries = 0;
b481de9c
ZY
1612
1613 if (in_interrupt())
1614 ieee80211_tx_status_irqsafe(priv->hw,
1615 tx_sta->skb[0], &(tx_sta->status));
1616 else
1617 ieee80211_tx_status(priv->hw,
1618 tx_sta->skb[0], &(tx_sta->status));
1619
1620 tx_sta->skb[0] = NULL;
1621}
1622
1623/**
6440adb5 1624 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
b481de9c 1625 *
6440adb5
CB
1626 * When FW advances 'R' index, all entries between old and new 'R' index
1627 * need to be reclaimed. As result, some free space forms. If there is
1628 * enough free space (> low mark), wake the stack that feeds us.
b481de9c 1629 */
c79dd5b5 1630int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
b481de9c 1631{
16466903 1632 struct iwl_tx_queue *txq = &priv->txq[txq_id];
443cfd45 1633 struct iwl_queue *q = &txq->q;
b481de9c
ZY
1634 int nfreed = 0;
1635
443cfd45 1636 if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
b481de9c
ZY
1637 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
1638 "is out of range [0-%d] %d %d.\n", txq_id,
fc4b6853 1639 index, q->n_bd, q->write_ptr, q->read_ptr);
b481de9c
ZY
1640 return 0;
1641 }
1642
c54b679d 1643 for (index = iwl_queue_inc_wrap(index, q->n_bd);
fc4b6853 1644 q->read_ptr != index;
c54b679d 1645 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
b481de9c 1646 if (txq_id != IWL_CMD_QUEUE_NUM) {
bb8c093b 1647 iwl4965_txstatus_to_ieee(priv,
fc4b6853 1648 &(txq->txb[txq->q.read_ptr]));
1053d35f 1649 iwl_hw_txq_free_tfd(priv, txq);
b481de9c
ZY
1650 } else if (nfreed > 1) {
1651 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
fc4b6853 1652 q->write_ptr, q->read_ptr);
b481de9c
ZY
1653 queue_work(priv->workqueue, &priv->restart);
1654 }
1655 nfreed++;
1656 }
1657
b481de9c
ZY
1658 return nfreed;
1659}
1660
bb8c093b 1661static int iwl4965_is_tx_success(u32 status)
b481de9c
ZY
1662{
1663 status &= TX_STATUS_MSK;
1664 return (status == TX_STATUS_SUCCESS)
1665 || (status == TX_STATUS_DIRECT_DONE);
1666}
1667
1668/******************************************************************************
1669 *
1670 * Generic RX handler implementations
1671 *
1672 ******************************************************************************/
c8b0e6e1 1673#ifdef CONFIG_IWL4965_HT
b481de9c 1674
c79dd5b5 1675static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
b481de9c
ZY
1676 struct ieee80211_hdr *hdr)
1677{
1678 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
1679 return IWL_AP_ID;
1680 else {
1681 u8 *da = ieee80211_get_DA(hdr);
947b13a7 1682 return iwl_find_station(priv, da);
b481de9c
ZY
1683 }
1684}
1685
bb8c093b 1686static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
c79dd5b5 1687 struct iwl_priv *priv, int txq_id, int idx)
b481de9c
ZY
1688{
1689 if (priv->txq[txq_id].txb[idx].skb[0])
1690 return (struct ieee80211_hdr *)priv->txq[txq_id].
1691 txb[idx].skb[0]->data;
1692 return NULL;
1693}
1694
bb8c093b 1695static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
b481de9c
ZY
1696{
1697 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
1698 tx_resp->frame_count);
1699 return le32_to_cpu(*scd_ssn) & MAX_SN;
1700
1701}
6440adb5
CB
1702
1703/**
1704 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
1705 */
c79dd5b5 1706static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
6def9761 1707 struct iwl_ht_agg *agg,
fe01b477 1708 struct iwl4965_tx_resp_agg *tx_resp,
b481de9c
ZY
1709 u16 start_idx)
1710{
fe01b477
RR
1711 u16 status;
1712 struct agg_tx_status *frame_status = &tx_resp->status;
b481de9c
ZY
1713 struct ieee80211_tx_status *tx_status = NULL;
1714 struct ieee80211_hdr *hdr = NULL;
1715 int i, sh;
1716 int txq_id, idx;
1717 u16 seq;
1718
1719 if (agg->wait_for_ba)
6440adb5 1720 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
b481de9c
ZY
1721
1722 agg->frame_count = tx_resp->frame_count;
1723 agg->start_idx = start_idx;
1724 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
fe01b477 1725 agg->bitmap = 0;
b481de9c 1726
6440adb5 1727 /* # frames attempted by Tx command */
b481de9c 1728 if (agg->frame_count == 1) {
6440adb5 1729 /* Only one frame was attempted; no block-ack will arrive */
fe01b477
RR
1730 status = le16_to_cpu(frame_status[0].status);
1731 seq = le16_to_cpu(frame_status[0].sequence);
1732 idx = SEQ_TO_INDEX(seq);
1733 txq_id = SEQ_TO_QUEUE(seq);
b481de9c 1734
b481de9c 1735 /* FIXME: code repetition */
fe01b477
RR
1736 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
1737 agg->frame_count, agg->start_idx, idx);
b481de9c 1738
fe01b477 1739 tx_status = &(priv->txq[txq_id].txb[idx].status);
b481de9c 1740 tx_status->retry_count = tx_resp->failure_frame;
fe01b477 1741 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
bb8c093b 1742 tx_status->flags = iwl4965_is_tx_success(status)?
b481de9c 1743 IEEE80211_TX_STATUS_ACK : 0;
4c424e4c
RR
1744 iwl4965_hwrate_to_tx_control(priv,
1745 le32_to_cpu(tx_resp->rate_n_flags),
1746 &tx_status->control);
b481de9c
ZY
1747 /* FIXME: code repetition end */
1748
1749 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
1750 status & 0xff, tx_resp->failure_frame);
1751 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
bb8c093b 1752 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
b481de9c
ZY
1753
1754 agg->wait_for_ba = 0;
1755 } else {
6440adb5 1756 /* Two or more frames were attempted; expect block-ack */
b481de9c
ZY
1757 u64 bitmap = 0;
1758 int start = agg->start_idx;
1759
6440adb5 1760 /* Construct bit-map of pending frames within Tx window */
b481de9c
ZY
1761 for (i = 0; i < agg->frame_count; i++) {
1762 u16 sc;
fe01b477
RR
1763 status = le16_to_cpu(frame_status[i].status);
1764 seq = le16_to_cpu(frame_status[i].sequence);
b481de9c
ZY
1765 idx = SEQ_TO_INDEX(seq);
1766 txq_id = SEQ_TO_QUEUE(seq);
1767
1768 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
1769 AGG_TX_STATE_ABORT_MSK))
1770 continue;
1771
1772 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
1773 agg->frame_count, txq_id, idx);
1774
bb8c093b 1775 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
b481de9c
ZY
1776
1777 sc = le16_to_cpu(hdr->seq_ctrl);
1778 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
1779 IWL_ERROR("BUG_ON idx doesn't match seq control"
1780 " idx=%d, seq_idx=%d, seq=%d\n",
1781 idx, SEQ_TO_SN(sc),
1782 hdr->seq_ctrl);
1783 return -1;
1784 }
1785
1786 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
1787 i, idx, SEQ_TO_SN(sc));
1788
1789 sh = idx - start;
1790 if (sh > 64) {
1791 sh = (start - idx) + 0xff;
1792 bitmap = bitmap << sh;
1793 sh = 0;
1794 start = idx;
1795 } else if (sh < -64)
1796 sh = 0xff - (start - idx);
1797 else if (sh < 0) {
1798 sh = start - idx;
1799 start = idx;
1800 bitmap = bitmap << sh;
1801 sh = 0;
1802 }
1803 bitmap |= (1 << sh);
1804 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
1805 start, (u32)(bitmap & 0xFFFFFFFF));
1806 }
1807
fe01b477 1808 agg->bitmap = bitmap;
b481de9c
ZY
1809 agg->start_idx = start;
1810 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
fe01b477 1811 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
b481de9c 1812 agg->frame_count, agg->start_idx,
06501d29 1813 (unsigned long long)agg->bitmap);
b481de9c
ZY
1814
1815 if (bitmap)
1816 agg->wait_for_ba = 1;
1817 }
1818 return 0;
1819}
1820#endif
b481de9c 1821
6440adb5
CB
1822/**
1823 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
1824 */
c79dd5b5 1825static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
a55360e4 1826 struct iwl_rx_mem_buffer *rxb)
b481de9c 1827{
db11d634 1828 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
b481de9c
ZY
1829 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1830 int txq_id = SEQ_TO_QUEUE(sequence);
1831 int index = SEQ_TO_INDEX(sequence);
16466903 1832 struct iwl_tx_queue *txq = &priv->txq[txq_id];
b481de9c 1833 struct ieee80211_tx_status *tx_status;
bb8c093b 1834 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
b481de9c 1835 u32 status = le32_to_cpu(tx_resp->status);
c8b0e6e1 1836#ifdef CONFIG_IWL4965_HT
fe01b477 1837 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
54dbb525 1838 u16 fc;
fe01b477 1839 struct ieee80211_hdr *hdr;
54dbb525 1840 u8 *qc = NULL;
b481de9c
ZY
1841#endif
1842
443cfd45 1843 if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
b481de9c
ZY
1844 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
1845 "is out of range [0-%d] %d %d\n", txq_id,
fc4b6853
TW
1846 index, txq->q.n_bd, txq->q.write_ptr,
1847 txq->q.read_ptr);
b481de9c
ZY
1848 return;
1849 }
1850
c8b0e6e1 1851#ifdef CONFIG_IWL4965_HT
fe01b477 1852 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
54dbb525
TW
1853 fc = le16_to_cpu(hdr->frame_control);
1854 if (ieee80211_is_qos_data(fc)) {
1855 qc = ieee80211_get_qos_ctrl(hdr, ieee80211_get_hdrlen(fc));
1856 tid = qc[0] & 0xf;
1857 }
fe01b477
RR
1858
1859 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
1860 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
1861 IWL_ERROR("Station not known\n");
1862 return;
1863 }
1864
b481de9c 1865 if (txq->sched_retry) {
bb8c093b 1866 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
6def9761 1867 struct iwl_ht_agg *agg = NULL;
b481de9c 1868
fe01b477 1869 if (!qc)
b481de9c 1870 return;
b481de9c
ZY
1871
1872 agg = &priv->stations[sta_id].tid[tid].agg;
1873
fe01b477
RR
1874 iwl4965_tx_status_reply_tx(priv, agg,
1875 (struct iwl4965_tx_resp_agg *)tx_resp, index);
b481de9c
ZY
1876
1877 if ((tx_resp->frame_count == 1) &&
bb8c093b 1878 !iwl4965_is_tx_success(status)) {
b481de9c
ZY
1879 /* TODO: send BAR */
1880 }
1881
fe01b477 1882 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
0d0b2c1c 1883 int freed, ampdu_q;
c54b679d 1884 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
b481de9c
ZY
1885 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
1886 "%d index %d\n", scd_ssn , index);
fe01b477
RR
1887 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
1888 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
1889
443cfd45 1890 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
fe01b477 1891 txq_id >= 0 && priv->mac80211_registered &&
0d0b2c1c
RR
1892 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA) {
1893 /* calculate mac80211 ampdu sw queue to wake */
1894 ampdu_q = txq_id - IWL_BACK_QUEUE_FIRST_ID +
1895 priv->hw->queues;
1896 if (agg->state == IWL_AGG_OFF)
1897 ieee80211_wake_queue(priv->hw, txq_id);
1898 else
1899 ieee80211_wake_queue(priv->hw, ampdu_q);
1900 }
fe01b477 1901 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
b481de9c
ZY
1902 }
1903 } else {
c8b0e6e1 1904#endif /* CONFIG_IWL4965_HT */
fc4b6853 1905 tx_status = &(txq->txb[txq->q.read_ptr].status);
b481de9c
ZY
1906
1907 tx_status->retry_count = tx_resp->failure_frame;
b481de9c 1908 tx_status->flags =
bb8c093b 1909 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
4c424e4c
RR
1910 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
1911 &tx_status->control);
b481de9c 1912
b481de9c 1913 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
bb8c093b 1914 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
b481de9c
ZY
1915 status, le32_to_cpu(tx_resp->rate_n_flags),
1916 tx_resp->failure_frame);
1917
1918 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
47c5196e 1919#ifdef CONFIG_IWL4965_HT
fe01b477
RR
1920 if (index != -1) {
1921 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
fe01b477
RR
1922 if (tid != MAX_TID_COUNT)
1923 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
443cfd45 1924 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
0d0b2c1c 1925 (txq_id >= 0) && priv->mac80211_registered)
fe01b477
RR
1926 ieee80211_wake_queue(priv->hw, txq_id);
1927 if (tid != MAX_TID_COUNT)
1928 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
fe01b477 1929 }
b481de9c 1930 }
c8b0e6e1 1931#endif /* CONFIG_IWL4965_HT */
b481de9c
ZY
1932
1933 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
1934 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
1935}
1936
1937
c79dd5b5 1938static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
a55360e4 1939 struct iwl_rx_mem_buffer *rxb)
b481de9c 1940{
db11d634 1941 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
bb8c093b 1942 struct iwl4965_alive_resp *palive;
b481de9c
ZY
1943 struct delayed_work *pwork;
1944
1945 palive = &pkt->u.alive_frame;
1946
1947 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
1948 "0x%01X 0x%01X\n",
1949 palive->is_valid, palive->ver_type,
1950 palive->ver_subtype);
1951
1952 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
1953 IWL_DEBUG_INFO("Initialization Alive received.\n");
1954 memcpy(&priv->card_alive_init,
1955 &pkt->u.alive_frame,
bb8c093b 1956 sizeof(struct iwl4965_init_alive_resp));
b481de9c
ZY
1957 pwork = &priv->init_alive_start;
1958 } else {
1959 IWL_DEBUG_INFO("Runtime Alive received.\n");
1960 memcpy(&priv->card_alive, &pkt->u.alive_frame,
bb8c093b 1961 sizeof(struct iwl4965_alive_resp));
b481de9c
ZY
1962 pwork = &priv->alive_start;
1963 }
1964
1965 /* We delay the ALIVE response by 5ms to
1966 * give the HW RF Kill time to activate... */
1967 if (palive->is_valid == UCODE_VALID_OK)
1968 queue_delayed_work(priv->workqueue, pwork,
1969 msecs_to_jiffies(5));
1970 else
1971 IWL_WARNING("uCode did not respond OK.\n");
1972}
1973
c79dd5b5 1974static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
a55360e4 1975 struct iwl_rx_mem_buffer *rxb)
b481de9c 1976{
db11d634 1977 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
b481de9c
ZY
1978
1979 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
1980 return;
1981}
1982
c79dd5b5 1983static void iwl4965_rx_reply_error(struct iwl_priv *priv,
a55360e4 1984 struct iwl_rx_mem_buffer *rxb)
b481de9c 1985{
db11d634 1986 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
b481de9c
ZY
1987
1988 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
1989 "seq 0x%04X ser 0x%08X\n",
1990 le32_to_cpu(pkt->u.err_resp.error_type),
1991 get_cmd_string(pkt->u.err_resp.cmd_id),
1992 pkt->u.err_resp.cmd_id,
1993 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1994 le32_to_cpu(pkt->u.err_resp.error_info));
1995}
1996
1997#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1998
a55360e4 1999static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
b481de9c 2000{
db11d634 2001 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
c1adf9fb 2002 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
bb8c093b 2003 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
b481de9c
ZY
2004 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2005 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2006 rxon->channel = csa->channel;
2007 priv->staging_rxon.channel = csa->channel;
2008}
2009
c79dd5b5 2010static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
a55360e4 2011 struct iwl_rx_mem_buffer *rxb)
b481de9c 2012{
c8b0e6e1 2013#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
db11d634 2014 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
bb8c093b 2015 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
b481de9c
ZY
2016
2017 if (!report->state) {
f3d67999
EK
2018 IWL_DEBUG(IWL_DL_11H,
2019 "Spectrum Measure Notification: Start\n");
b481de9c
ZY
2020 return;
2021 }
2022
2023 memcpy(&priv->measure_report, report, sizeof(*report));
2024 priv->measurement_status |= MEASUREMENT_READY;
2025#endif
2026}
2027
c79dd5b5 2028static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
a55360e4 2029 struct iwl_rx_mem_buffer *rxb)
b481de9c 2030{
0a6857e7 2031#ifdef CONFIG_IWLWIFI_DEBUG
db11d634 2032 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
bb8c093b 2033 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
b481de9c
ZY
2034 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2035 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2036#endif
2037}
2038
c79dd5b5 2039static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
a55360e4 2040 struct iwl_rx_mem_buffer *rxb)
b481de9c 2041{
db11d634 2042 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
b481de9c
ZY
2043 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2044 "notification for %s:\n",
2045 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
bf403db8 2046 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
b481de9c
ZY
2047}
2048
bb8c093b 2049static void iwl4965_bg_beacon_update(struct work_struct *work)
b481de9c 2050{
c79dd5b5
TW
2051 struct iwl_priv *priv =
2052 container_of(work, struct iwl_priv, beacon_update);
b481de9c
ZY
2053 struct sk_buff *beacon;
2054
2055 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
32bfd35d 2056 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
b481de9c
ZY
2057
2058 if (!beacon) {
2059 IWL_ERROR("update beacon failed\n");
2060 return;
2061 }
2062
2063 mutex_lock(&priv->mutex);
2064 /* new beacon skb is allocated every time; dispose previous.*/
2065 if (priv->ibss_beacon)
2066 dev_kfree_skb(priv->ibss_beacon);
2067
2068 priv->ibss_beacon = beacon;
2069 mutex_unlock(&priv->mutex);
2070
bb8c093b 2071 iwl4965_send_beacon_cmd(priv);
b481de9c
ZY
2072}
2073
c79dd5b5 2074static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
a55360e4 2075 struct iwl_rx_mem_buffer *rxb)
b481de9c 2076{
0a6857e7 2077#ifdef CONFIG_IWLWIFI_DEBUG
db11d634 2078 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
bb8c093b
CH
2079 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
2080 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
b481de9c
ZY
2081
2082 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2083 "tsf %d %d rate %d\n",
2084 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2085 beacon->beacon_notify_hdr.failure_frame,
2086 le32_to_cpu(beacon->ibss_mgr_status),
2087 le32_to_cpu(beacon->high_tsf),
2088 le32_to_cpu(beacon->low_tsf), rate);
2089#endif
2090
2091 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
2092 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2093 queue_work(priv->workqueue, &priv->beacon_update);
2094}
2095
2096/* Service response to REPLY_SCAN_CMD (0x80) */
c79dd5b5 2097static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
a55360e4 2098 struct iwl_rx_mem_buffer *rxb)
b481de9c 2099{
0a6857e7 2100#ifdef CONFIG_IWLWIFI_DEBUG
db11d634 2101 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
bb8c093b
CH
2102 struct iwl4965_scanreq_notification *notif =
2103 (struct iwl4965_scanreq_notification *)pkt->u.raw;
b481de9c
ZY
2104
2105 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2106#endif
2107}
2108
2109/* Service SCAN_START_NOTIFICATION (0x82) */
c79dd5b5 2110static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
a55360e4 2111 struct iwl_rx_mem_buffer *rxb)
b481de9c 2112{
db11d634 2113 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
bb8c093b
CH
2114 struct iwl4965_scanstart_notification *notif =
2115 (struct iwl4965_scanstart_notification *)pkt->u.raw;
b481de9c
ZY
2116 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2117 IWL_DEBUG_SCAN("Scan start: "
2118 "%d [802.11%s] "
2119 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2120 notif->channel,
2121 notif->band ? "bg" : "a",
2122 notif->tsf_high,
2123 notif->tsf_low, notif->status, notif->beacon_timer);
2124}
2125
2126/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
c79dd5b5 2127static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
a55360e4 2128 struct iwl_rx_mem_buffer *rxb)
b481de9c 2129{
db11d634 2130 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
bb8c093b
CH
2131 struct iwl4965_scanresults_notification *notif =
2132 (struct iwl4965_scanresults_notification *)pkt->u.raw;
b481de9c
ZY
2133
2134 IWL_DEBUG_SCAN("Scan ch.res: "
2135 "%d [802.11%s] "
2136 "(TSF: 0x%08X:%08X) - %d "
2137 "elapsed=%lu usec (%dms since last)\n",
2138 notif->channel,
2139 notif->band ? "bg" : "a",
2140 le32_to_cpu(notif->tsf_high),
2141 le32_to_cpu(notif->tsf_low),
2142 le32_to_cpu(notif->statistics[0]),
2143 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
2144 jiffies_to_msecs(elapsed_jiffies
2145 (priv->last_scan_jiffies, jiffies)));
2146
2147 priv->last_scan_jiffies = jiffies;
7878a5a4 2148 priv->next_scan_jiffies = 0;
b481de9c
ZY
2149}
2150
2151/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
c79dd5b5 2152static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
a55360e4 2153 struct iwl_rx_mem_buffer *rxb)
b481de9c 2154{
db11d634 2155 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
bb8c093b 2156 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
b481de9c
ZY
2157
2158 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
2159 scan_notif->scanned_channels,
2160 scan_notif->tsf_low,
2161 scan_notif->tsf_high, scan_notif->status);
2162
2163 /* The HW is no longer scanning */
2164 clear_bit(STATUS_SCAN_HW, &priv->status);
2165
2166 /* The scan completion notification came in, so kill that timer... */
2167 cancel_delayed_work(&priv->scan_check);
2168
2169 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
2170 (priv->scan_bands == 2) ? "2.4" : "5.2",
2171 jiffies_to_msecs(elapsed_jiffies
2172 (priv->scan_pass_start, jiffies)));
2173
2174 /* Remove this scanned band from the list
2175 * of pending bands to scan */
2176 priv->scan_bands--;
2177
2178 /* If a request to abort was given, or the scan did not succeed
2179 * then we reset the scan state machine and terminate,
2180 * re-queuing another scan if one has been requested */
2181 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2182 IWL_DEBUG_INFO("Aborted scan completed.\n");
2183 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
2184 } else {
2185 /* If there are more bands on this scan pass reschedule */
2186 if (priv->scan_bands > 0)
2187 goto reschedule;
2188 }
2189
2190 priv->last_scan_jiffies = jiffies;
7878a5a4 2191 priv->next_scan_jiffies = 0;
b481de9c
ZY
2192 IWL_DEBUG_INFO("Setting scan to off\n");
2193
2194 clear_bit(STATUS_SCANNING, &priv->status);
2195
2196 IWL_DEBUG_INFO("Scan took %dms\n",
2197 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
2198
2199 queue_work(priv->workqueue, &priv->scan_completed);
2200
2201 return;
2202
2203reschedule:
2204 priv->scan_pass_start = jiffies;
2205 queue_work(priv->workqueue, &priv->request_scan);
2206}
2207
2208/* Handle notification from uCode that card's power state is changing
2209 * due to software, hardware, or critical temperature RFKILL */
c79dd5b5 2210static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
a55360e4 2211 struct iwl_rx_mem_buffer *rxb)
b481de9c 2212{
db11d634 2213 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
b481de9c
ZY
2214 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
2215 unsigned long status = priv->status;
2216
2217 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
2218 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
2219 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
2220
2221 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
2222 RF_CARD_DISABLED)) {
2223
3395f6e9 2224 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
b481de9c
ZY
2225 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2226
3395f6e9
TW
2227 if (!iwl_grab_nic_access(priv)) {
2228 iwl_write_direct32(
b481de9c
ZY
2229 priv, HBUS_TARG_MBX_C,
2230 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
2231
3395f6e9 2232 iwl_release_nic_access(priv);
b481de9c
ZY
2233 }
2234
2235 if (!(flags & RXON_CARD_DISABLED)) {
3395f6e9 2236 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
b481de9c 2237 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3395f6e9
TW
2238 if (!iwl_grab_nic_access(priv)) {
2239 iwl_write_direct32(
b481de9c
ZY
2240 priv, HBUS_TARG_MBX_C,
2241 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
2242
3395f6e9 2243 iwl_release_nic_access(priv);
b481de9c
ZY
2244 }
2245 }
2246
2247 if (flags & RF_CARD_DISABLED) {
3395f6e9 2248 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
b481de9c 2249 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
3395f6e9
TW
2250 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2251 if (!iwl_grab_nic_access(priv))
2252 iwl_release_nic_access(priv);
b481de9c
ZY
2253 }
2254 }
2255
2256 if (flags & HW_CARD_DISABLED)
2257 set_bit(STATUS_RF_KILL_HW, &priv->status);
2258 else
2259 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2260
2261
2262 if (flags & SW_CARD_DISABLED)
2263 set_bit(STATUS_RF_KILL_SW, &priv->status);
2264 else
2265 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2266
2267 if (!(flags & RXON_CARD_DISABLED))
bb8c093b 2268 iwl4965_scan_cancel(priv);
b481de9c
ZY
2269
2270 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
2271 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
2272 (test_bit(STATUS_RF_KILL_SW, &status) !=
2273 test_bit(STATUS_RF_KILL_SW, &priv->status)))
2274 queue_work(priv->workqueue, &priv->rf_kill);
2275 else
2276 wake_up_interruptible(&priv->wait_command_queue);
2277}
2278
2279/**
bb8c093b 2280 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
b481de9c
ZY
2281 *
2282 * Setup the RX handlers for each of the reply types sent from the uCode
2283 * to the host.
2284 *
2285 * This function chains into the hardware specific files for them to setup
2286 * any hardware specific handlers as well.
2287 */
c79dd5b5 2288static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
b481de9c 2289{
bb8c093b
CH
2290 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
2291 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
2292 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
2293 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
b481de9c 2294 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
bb8c093b
CH
2295 iwl4965_rx_spectrum_measure_notif;
2296 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
b481de9c 2297 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
bb8c093b
CH
2298 iwl4965_rx_pm_debug_statistics_notif;
2299 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
b481de9c 2300
9fbab516
BC
2301 /*
2302 * The same handler is used for both the REPLY to a discrete
2303 * statistics request from the host as well as for the periodic
2304 * statistics notifications (after received beacons) from the uCode.
b481de9c 2305 */
bb8c093b
CH
2306 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
2307 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
b481de9c 2308
bb8c093b
CH
2309 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
2310 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
b481de9c 2311 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
bb8c093b 2312 iwl4965_rx_scan_results_notif;
b481de9c 2313 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
bb8c093b
CH
2314 iwl4965_rx_scan_complete_notif;
2315 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
2316 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
b481de9c 2317
9fbab516 2318 /* Set up hardware specific Rx handlers */
d4789efe 2319 priv->cfg->ops->lib->rx_handler_setup(priv);
b481de9c
ZY
2320}
2321
2322/**
bb8c093b 2323 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
b481de9c
ZY
2324 * @rxb: Rx buffer to reclaim
2325 *
2326 * If an Rx buffer has an async callback associated with it the callback
2327 * will be executed. The attached skb (if present) will only be freed
2328 * if the callback returns 1
2329 */
c79dd5b5 2330static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
a55360e4 2331 struct iwl_rx_mem_buffer *rxb)
b481de9c 2332{
db11d634 2333 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
b481de9c
ZY
2334 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2335 int txq_id = SEQ_TO_QUEUE(sequence);
2336 int index = SEQ_TO_INDEX(sequence);
2337 int huge = sequence & SEQ_HUGE_FRAME;
2338 int cmd_index;
857485c0 2339 struct iwl_cmd *cmd;
b481de9c
ZY
2340
2341 /* If a Tx command is being handled and it isn't in the actual
2342 * command queue then there a command routing bug has been introduced
2343 * in the queue management code. */
2344 if (txq_id != IWL_CMD_QUEUE_NUM)
2345 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
2346 txq_id, pkt->hdr.cmd);
2347 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
2348
2349 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
2350 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
2351
2352 /* Input error checking is done when commands are added to queue. */
2353 if (cmd->meta.flags & CMD_WANT_SKB) {
2354 cmd->meta.source->u.skb = rxb->skb;
2355 rxb->skb = NULL;
2356 } else if (cmd->meta.u.callback &&
2357 !cmd->meta.u.callback(priv, cmd, rxb->skb))
2358 rxb->skb = NULL;
2359
bb8c093b 2360 iwl4965_tx_queue_reclaim(priv, txq_id, index);
b481de9c
ZY
2361
2362 if (!(cmd->meta.flags & CMD_ASYNC)) {
2363 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
2364 wake_up_interruptible(&priv->wait_command_queue);
2365 }
2366}
2367
5c0eef96
MA
2368/*
2369 * this should be called while priv->lock is locked
2370*/
a55360e4 2371static void __iwl_rx_replenish(struct iwl_priv *priv)
b481de9c 2372{
a55360e4
TW
2373 iwl_rx_allocate(priv);
2374 iwl_rx_queue_restock(priv);
b481de9c
ZY
2375}
2376
b481de9c
ZY
2377
2378/**
a55360e4 2379 * iwl_rx_handle - Main entry function for receiving responses from uCode
b481de9c
ZY
2380 *
2381 * Uses the priv->rx_handlers callback function array to invoke
2382 * the appropriate handlers, including command responses,
2383 * frame-received notifications, and other notifications.
2384 */
a55360e4 2385void iwl_rx_handle(struct iwl_priv *priv)
b481de9c 2386{
a55360e4 2387 struct iwl_rx_mem_buffer *rxb;
db11d634 2388 struct iwl_rx_packet *pkt;
a55360e4 2389 struct iwl_rx_queue *rxq = &priv->rxq;
b481de9c
ZY
2390 u32 r, i;
2391 int reclaim;
2392 unsigned long flags;
5c0eef96 2393 u8 fill_rx = 0;
d68ab680 2394 u32 count = 8;
b481de9c 2395
6440adb5
CB
2396 /* uCode's read index (stored in shared DRAM) indicates the last Rx
2397 * buffer that the driver may process (last buffer filled by ucode). */
d67f5489 2398 r = priv->cfg->ops->lib->shared_mem_rx_idx(priv);
b481de9c
ZY
2399 i = rxq->read;
2400
2401 /* Rx interrupt, but nothing sent from uCode */
2402 if (i == r)
f3d67999 2403 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d\n", r, i);
b481de9c 2404
a55360e4 2405 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
5c0eef96
MA
2406 fill_rx = 1;
2407
b481de9c
ZY
2408 while (i != r) {
2409 rxb = rxq->queue[i];
2410
9fbab516 2411 /* If an RXB doesn't have a Rx queue slot associated with it,
b481de9c
ZY
2412 * then a bug has been introduced in the queue refilling
2413 * routines -- catch it here */
2414 BUG_ON(rxb == NULL);
2415
2416 rxq->queue[i] = NULL;
2417
2418 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
5425e490 2419 priv->hw_params.rx_buf_size,
b481de9c 2420 PCI_DMA_FROMDEVICE);
db11d634 2421 pkt = (struct iwl_rx_packet *)rxb->skb->data;
b481de9c
ZY
2422
2423 /* Reclaim a command buffer only if this packet is a response
2424 * to a (driver-originated) command.
2425 * If the packet (e.g. Rx frame) originated from uCode,
2426 * there is no command buffer to reclaim.
2427 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
2428 * but apparently a few don't get set; catch them here. */
2429 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
2430 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
857485c0 2431 (pkt->hdr.cmd != REPLY_RX) &&
cfe01709 2432 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
b481de9c
ZY
2433 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
2434 (pkt->hdr.cmd != REPLY_TX);
2435
2436 /* Based on type of command response or notification,
2437 * handle those that need handling via function in
bb8c093b 2438 * rx_handlers table. See iwl4965_setup_rx_handlers() */
b481de9c 2439 if (priv->rx_handlers[pkt->hdr.cmd]) {
f3d67999
EK
2440 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d, %s, 0x%02x\n", r,
2441 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
b481de9c
ZY
2442 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
2443 } else {
2444 /* No handling needed */
f3d67999 2445 IWL_DEBUG(IWL_DL_RX,
b481de9c
ZY
2446 "r %d i %d No handler needed for %s, 0x%02x\n",
2447 r, i, get_cmd_string(pkt->hdr.cmd),
2448 pkt->hdr.cmd);
2449 }
2450
2451 if (reclaim) {
9fbab516 2452 /* Invoke any callbacks, transfer the skb to caller, and
857485c0 2453 * fire off the (possibly) blocking iwl_send_cmd()
b481de9c
ZY
2454 * as we reclaim the driver command queue */
2455 if (rxb && rxb->skb)
bb8c093b 2456 iwl4965_tx_cmd_complete(priv, rxb);
b481de9c
ZY
2457 else
2458 IWL_WARNING("Claim null rxb?\n");
2459 }
2460
2461 /* For now we just don't re-use anything. We can tweak this
2462 * later to try and re-use notification packets and SKBs that
2463 * fail to Rx correctly */
2464 if (rxb->skb != NULL) {
2465 priv->alloc_rxb_skb--;
2466 dev_kfree_skb_any(rxb->skb);
2467 rxb->skb = NULL;
2468 }
2469
2470 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
5425e490 2471 priv->hw_params.rx_buf_size,
9ee1ba47 2472 PCI_DMA_FROMDEVICE);
b481de9c
ZY
2473 spin_lock_irqsave(&rxq->lock, flags);
2474 list_add_tail(&rxb->list, &priv->rxq.rx_used);
2475 spin_unlock_irqrestore(&rxq->lock, flags);
2476 i = (i + 1) & RX_QUEUE_MASK;
5c0eef96
MA
2477 /* If there are a lot of unused frames,
2478 * restock the Rx queue so ucode wont assert. */
2479 if (fill_rx) {
2480 count++;
2481 if (count >= 8) {
2482 priv->rxq.read = i;
a55360e4 2483 __iwl_rx_replenish(priv);
5c0eef96
MA
2484 count = 0;
2485 }
2486 }
b481de9c
ZY
2487 }
2488
2489 /* Backtrack one entry */
2490 priv->rxq.read = i;
a55360e4
TW
2491 iwl_rx_queue_restock(priv);
2492}
2493/* Convert linear signal-to-noise ratio into dB */
2494static u8 ratio2dB[100] = {
2495/* 0 1 2 3 4 5 6 7 8 9 */
2496 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
2497 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
2498 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
2499 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
2500 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
2501 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
2502 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
2503 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
2504 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
2505 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
2506};
2507
2508/* Calculates a relative dB value from a ratio of linear
2509 * (i.e. not dB) signal levels.
2510 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
2511int iwl4965_calc_db_from_ratio(int sig_ratio)
2512{
2513 /* 1000:1 or higher just report as 60 dB */
2514 if (sig_ratio >= 1000)
2515 return 60;
2516
2517 /* 100:1 or higher, divide by 10 and use table,
2518 * add 20 dB to make up for divide by 10 */
2519 if (sig_ratio >= 100)
2520 return (20 + (int)ratio2dB[sig_ratio/10]);
2521
2522 /* We shouldn't see this */
2523 if (sig_ratio < 1)
2524 return 0;
2525
2526 /* Use table for ratios 1:1 - 99:1 */
2527 return (int)ratio2dB[sig_ratio];
2528}
2529
2530#define PERFECT_RSSI (-20) /* dBm */
2531#define WORST_RSSI (-95) /* dBm */
2532#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
2533
2534/* Calculate an indication of rx signal quality (a percentage, not dBm!).
2535 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
2536 * about formulas used below. */
2537int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
2538{
2539 int sig_qual;
2540 int degradation = PERFECT_RSSI - rssi_dbm;
2541
2542 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
2543 * as indicator; formula is (signal dbm - noise dbm).
2544 * SNR at or above 40 is a great signal (100%).
2545 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
2546 * Weakest usable signal is usually 10 - 15 dB SNR. */
2547 if (noise_dbm) {
2548 if (rssi_dbm - noise_dbm >= 40)
2549 return 100;
2550 else if (rssi_dbm < noise_dbm)
2551 return 0;
2552 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
2553
2554 /* Else use just the signal level.
2555 * This formula is a least squares fit of data points collected and
2556 * compared with a reference system that had a percentage (%) display
2557 * for signal quality. */
2558 } else
2559 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
2560 (15 * RSSI_RANGE + 62 * degradation)) /
2561 (RSSI_RANGE * RSSI_RANGE);
2562
2563 if (sig_qual > 100)
2564 sig_qual = 100;
2565 else if (sig_qual < 1)
2566 sig_qual = 0;
2567
2568 return sig_qual;
b481de9c
ZY
2569}
2570
0a6857e7 2571#ifdef CONFIG_IWLWIFI_DEBUG
bf403db8 2572static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
b481de9c 2573{
c1adf9fb 2574 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
0795af57
JP
2575 DECLARE_MAC_BUF(mac);
2576
b481de9c 2577 IWL_DEBUG_RADIO("RX CONFIG:\n");
bf403db8 2578 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
b481de9c
ZY
2579 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
2580 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
2581 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
2582 le32_to_cpu(rxon->filter_flags));
2583 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
2584 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
2585 rxon->ofdm_basic_rates);
2586 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
0795af57
JP
2587 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
2588 print_mac(mac, rxon->node_addr));
2589 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
2590 print_mac(mac, rxon->bssid_addr));
b481de9c
ZY
2591 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
2592}
2593#endif
2594
c79dd5b5 2595static void iwl4965_enable_interrupts(struct iwl_priv *priv)
b481de9c
ZY
2596{
2597 IWL_DEBUG_ISR("Enabling interrupts\n");
2598 set_bit(STATUS_INT_ENABLED, &priv->status);
3395f6e9 2599 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
b481de9c
ZY
2600}
2601
0359facc
MA
2602/* call this function to flush any scheduled tasklet */
2603static inline void iwl_synchronize_irq(struct iwl_priv *priv)
2604{
2605 /* wait to make sure we flush pedding tasklet*/
2606 synchronize_irq(priv->pci_dev->irq);
2607 tasklet_kill(&priv->irq_tasklet);
2608}
2609
c79dd5b5 2610static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
b481de9c
ZY
2611{
2612 clear_bit(STATUS_INT_ENABLED, &priv->status);
2613
2614 /* disable interrupts from uCode/NIC to host */
3395f6e9 2615 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
b481de9c
ZY
2616
2617 /* acknowledge/clear/reset any interrupts still pending
2618 * from uCode or flow handler (Rx/Tx DMA) */
3395f6e9
TW
2619 iwl_write32(priv, CSR_INT, 0xffffffff);
2620 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
b481de9c
ZY
2621 IWL_DEBUG_ISR("Disabled interrupts\n");
2622}
2623
2624static const char *desc_lookup(int i)
2625{
2626 switch (i) {
2627 case 1:
2628 return "FAIL";
2629 case 2:
2630 return "BAD_PARAM";
2631 case 3:
2632 return "BAD_CHECKSUM";
2633 case 4:
2634 return "NMI_INTERRUPT";
2635 case 5:
2636 return "SYSASSERT";
2637 case 6:
2638 return "FATAL_ERROR";
2639 }
2640
2641 return "UNKNOWN";
2642}
2643
2644#define ERROR_START_OFFSET (1 * sizeof(u32))
2645#define ERROR_ELEM_SIZE (7 * sizeof(u32))
2646
c79dd5b5 2647static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
b481de9c
ZY
2648{
2649 u32 data2, line;
2650 u32 desc, time, count, base, data1;
2651 u32 blink1, blink2, ilink1, ilink2;
2652 int rc;
2653
2654 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
2655
57aab75a 2656 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
b481de9c
ZY
2657 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
2658 return;
2659 }
2660
3395f6e9 2661 rc = iwl_grab_nic_access(priv);
b481de9c
ZY
2662 if (rc) {
2663 IWL_WARNING("Can not read from adapter at this time.\n");
2664 return;
2665 }
2666
3395f6e9 2667 count = iwl_read_targ_mem(priv, base);
b481de9c
ZY
2668
2669 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
2670 IWL_ERROR("Start IWL Error Log Dump:\n");
2acae16e 2671 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
b481de9c
ZY
2672 }
2673
3395f6e9
TW
2674 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
2675 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
2676 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
2677 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
2678 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
2679 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
2680 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
2681 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
2682 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
b481de9c
ZY
2683
2684 IWL_ERROR("Desc Time "
2685 "data1 data2 line\n");
2686 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
2687 desc_lookup(desc), desc, time, data1, data2, line);
2688 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
2689 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
2690 ilink1, ilink2);
2691
3395f6e9 2692 iwl_release_nic_access(priv);
b481de9c
ZY
2693}
2694
b481de9c 2695/**
bb8c093b 2696 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
b481de9c 2697 */
c79dd5b5 2698static void iwl4965_irq_handle_error(struct iwl_priv *priv)
b481de9c 2699{
bb8c093b 2700 /* Set the FW error flag -- cleared on iwl4965_down */
b481de9c
ZY
2701 set_bit(STATUS_FW_ERROR, &priv->status);
2702
2703 /* Cancel currently queued command. */
2704 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
2705
0a6857e7 2706#ifdef CONFIG_IWLWIFI_DEBUG
bf403db8 2707 if (priv->debug_level & IWL_DL_FW_ERRORS) {
bb8c093b 2708 iwl4965_dump_nic_error_log(priv);
189a2b59 2709 iwl_dump_nic_event_log(priv);
bf403db8 2710 iwl4965_print_rx_config_cmd(priv);
b481de9c
ZY
2711 }
2712#endif
2713
2714 wake_up_interruptible(&priv->wait_command_queue);
2715
2716 /* Keep the restart process from trying to send host
2717 * commands by clearing the INIT status bit */
2718 clear_bit(STATUS_READY, &priv->status);
2719
2720 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
f3d67999 2721 IWL_DEBUG(IWL_DL_FW_ERRORS,
b481de9c
ZY
2722 "Restarting adapter due to uCode error.\n");
2723
3109ece1 2724 if (iwl_is_associated(priv)) {
b481de9c
ZY
2725 memcpy(&priv->recovery_rxon, &priv->active_rxon,
2726 sizeof(priv->recovery_rxon));
2727 priv->error_recovering = 1;
2728 }
3a1081e8
EK
2729 if (priv->cfg->mod_params->restart_fw)
2730 queue_work(priv->workqueue, &priv->restart);
b481de9c
ZY
2731 }
2732}
2733
c79dd5b5 2734static void iwl4965_error_recovery(struct iwl_priv *priv)
b481de9c
ZY
2735{
2736 unsigned long flags;
2737
2738 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
2739 sizeof(priv->staging_rxon));
2740 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
bb8c093b 2741 iwl4965_commit_rxon(priv);
b481de9c 2742
4f40e4d9 2743 iwl_rxon_add_station(priv, priv->bssid, 1);
b481de9c
ZY
2744
2745 spin_lock_irqsave(&priv->lock, flags);
2746 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
2747 priv->error_recovering = 0;
2748 spin_unlock_irqrestore(&priv->lock, flags);
2749}
2750
c79dd5b5 2751static void iwl4965_irq_tasklet(struct iwl_priv *priv)
b481de9c
ZY
2752{
2753 u32 inta, handled = 0;
2754 u32 inta_fh;
2755 unsigned long flags;
0a6857e7 2756#ifdef CONFIG_IWLWIFI_DEBUG
b481de9c
ZY
2757 u32 inta_mask;
2758#endif
2759
2760 spin_lock_irqsave(&priv->lock, flags);
2761
2762 /* Ack/clear/reset pending uCode interrupts.
2763 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
2764 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
3395f6e9
TW
2765 inta = iwl_read32(priv, CSR_INT);
2766 iwl_write32(priv, CSR_INT, inta);
b481de9c
ZY
2767
2768 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
2769 * Any new interrupts that happen after this, either while we're
2770 * in this tasklet, or later, will show up in next ISR/tasklet. */
3395f6e9
TW
2771 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
2772 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
b481de9c 2773
0a6857e7 2774#ifdef CONFIG_IWLWIFI_DEBUG
bf403db8 2775 if (priv->debug_level & IWL_DL_ISR) {
9fbab516 2776 /* just for debug */
3395f6e9 2777 inta_mask = iwl_read32(priv, CSR_INT_MASK);
b481de9c
ZY
2778 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
2779 inta, inta_mask, inta_fh);
2780 }
2781#endif
2782
2783 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
2784 * atomic, make sure that inta covers all the interrupts that
2785 * we've discovered, even if FH interrupt came in just after
2786 * reading CSR_INT. */
6f83eaa1 2787 if (inta_fh & CSR49_FH_INT_RX_MASK)
b481de9c 2788 inta |= CSR_INT_BIT_FH_RX;
6f83eaa1 2789 if (inta_fh & CSR49_FH_INT_TX_MASK)
b481de9c
ZY
2790 inta |= CSR_INT_BIT_FH_TX;
2791
2792 /* Now service all interrupt bits discovered above. */
2793 if (inta & CSR_INT_BIT_HW_ERR) {
2794 IWL_ERROR("Microcode HW error detected. Restarting.\n");
2795
2796 /* Tell the device to stop sending interrupts */
bb8c093b 2797 iwl4965_disable_interrupts(priv);
b481de9c 2798
bb8c093b 2799 iwl4965_irq_handle_error(priv);
b481de9c
ZY
2800
2801 handled |= CSR_INT_BIT_HW_ERR;
2802
2803 spin_unlock_irqrestore(&priv->lock, flags);
2804
2805 return;
2806 }
2807
0a6857e7 2808#ifdef CONFIG_IWLWIFI_DEBUG
bf403db8 2809 if (priv->debug_level & (IWL_DL_ISR)) {
b481de9c 2810 /* NIC fires this, but we don't use it, redundant with WAKEUP */
25c03d8e
JP
2811 if (inta & CSR_INT_BIT_SCD)
2812 IWL_DEBUG_ISR("Scheduler finished to transmit "
2813 "the frame/frames.\n");
b481de9c
ZY
2814
2815 /* Alive notification via Rx interrupt will do the real work */
2816 if (inta & CSR_INT_BIT_ALIVE)
2817 IWL_DEBUG_ISR("Alive interrupt\n");
2818 }
2819#endif
2820 /* Safely ignore these bits for debug checks below */
25c03d8e 2821 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
b481de9c 2822
9fbab516 2823 /* HW RF KILL switch toggled */
b481de9c
ZY
2824 if (inta & CSR_INT_BIT_RF_KILL) {
2825 int hw_rf_kill = 0;
3395f6e9 2826 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
b481de9c
ZY
2827 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
2828 hw_rf_kill = 1;
2829
f3d67999 2830 IWL_DEBUG(IWL_DL_RF_KILL, "RF_KILL bit toggled to %s.\n",
b481de9c
ZY
2831 hw_rf_kill ? "disable radio":"enable radio");
2832
2833 /* Queue restart only if RF_KILL switch was set to "kill"
2834 * when we loaded driver, and is now set to "enable".
2835 * After we're Alive, RF_KILL gets handled by
3230455d 2836 * iwl4965_rx_card_state_notif() */
53e49093
ZY
2837 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
2838 clear_bit(STATUS_RF_KILL_HW, &priv->status);
b481de9c 2839 queue_work(priv->workqueue, &priv->restart);
53e49093 2840 }
b481de9c
ZY
2841
2842 handled |= CSR_INT_BIT_RF_KILL;
2843 }
2844
9fbab516 2845 /* Chip got too hot and stopped itself */
b481de9c
ZY
2846 if (inta & CSR_INT_BIT_CT_KILL) {
2847 IWL_ERROR("Microcode CT kill error detected.\n");
2848 handled |= CSR_INT_BIT_CT_KILL;
2849 }
2850
2851 /* Error detected by uCode */
2852 if (inta & CSR_INT_BIT_SW_ERR) {
2853 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
2854 inta);
bb8c093b 2855 iwl4965_irq_handle_error(priv);
b481de9c
ZY
2856 handled |= CSR_INT_BIT_SW_ERR;
2857 }
2858
2859 /* uCode wakes up after power-down sleep */
2860 if (inta & CSR_INT_BIT_WAKEUP) {
2861 IWL_DEBUG_ISR("Wakeup interrupt\n");
a55360e4 2862 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
babcebfa
TW
2863 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
2864 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
2865 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
2866 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
2867 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
2868 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
b481de9c
ZY
2869
2870 handled |= CSR_INT_BIT_WAKEUP;
2871 }
2872
2873 /* All uCode command responses, including Tx command responses,
2874 * Rx "responses" (frame-received notification), and other
2875 * notifications from uCode come through here*/
2876 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
a55360e4 2877 iwl_rx_handle(priv);
b481de9c
ZY
2878 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
2879 }
2880
2881 if (inta & CSR_INT_BIT_FH_TX) {
2882 IWL_DEBUG_ISR("Tx interrupt\n");
2883 handled |= CSR_INT_BIT_FH_TX;
dbb983b7
RR
2884 /* FH finished to write, send event */
2885 priv->ucode_write_complete = 1;
2886 wake_up_interruptible(&priv->wait_command_queue);
b481de9c
ZY
2887 }
2888
2889 if (inta & ~handled)
2890 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
2891
2892 if (inta & ~CSR_INI_SET_MASK) {
2893 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
2894 inta & ~CSR_INI_SET_MASK);
2895 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
2896 }
2897
2898 /* Re-enable all interrupts */
0359facc
MA
2899 /* only Re-enable if diabled by irq */
2900 if (test_bit(STATUS_INT_ENABLED, &priv->status))
2901 iwl4965_enable_interrupts(priv);
b481de9c 2902
0a6857e7 2903#ifdef CONFIG_IWLWIFI_DEBUG
bf403db8 2904 if (priv->debug_level & (IWL_DL_ISR)) {
3395f6e9
TW
2905 inta = iwl_read32(priv, CSR_INT);
2906 inta_mask = iwl_read32(priv, CSR_INT_MASK);
2907 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
b481de9c
ZY
2908 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
2909 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
2910 }
2911#endif
2912 spin_unlock_irqrestore(&priv->lock, flags);
2913}
2914
bb8c093b 2915static irqreturn_t iwl4965_isr(int irq, void *data)
b481de9c 2916{
c79dd5b5 2917 struct iwl_priv *priv = data;
b481de9c
ZY
2918 u32 inta, inta_mask;
2919 u32 inta_fh;
2920 if (!priv)
2921 return IRQ_NONE;
2922
2923 spin_lock(&priv->lock);
2924
2925 /* Disable (but don't clear!) interrupts here to avoid
2926 * back-to-back ISRs and sporadic interrupts from our NIC.
2927 * If we have something to service, the tasklet will re-enable ints.
2928 * If we *don't* have something, we'll re-enable before leaving here. */
3395f6e9
TW
2929 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
2930 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
b481de9c
ZY
2931
2932 /* Discover which interrupts are active/pending */
3395f6e9
TW
2933 inta = iwl_read32(priv, CSR_INT);
2934 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
b481de9c
ZY
2935
2936 /* Ignore interrupt if there's nothing in NIC to service.
2937 * This may be due to IRQ shared with another device,
2938 * or due to sporadic interrupts thrown from our NIC. */
2939 if (!inta && !inta_fh) {
2940 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
2941 goto none;
2942 }
2943
2944 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
66fbb541
ON
2945 /* Hardware disappeared. It might have already raised
2946 * an interrupt */
b481de9c 2947 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
66fbb541 2948 goto unplugged;
b481de9c
ZY
2949 }
2950
2951 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
2952 inta, inta_mask, inta_fh);
2953
25c03d8e
JP
2954 inta &= ~CSR_INT_BIT_SCD;
2955
bb8c093b 2956 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
25c03d8e
JP
2957 if (likely(inta || inta_fh))
2958 tasklet_schedule(&priv->irq_tasklet);
b481de9c 2959
66fbb541
ON
2960 unplugged:
2961 spin_unlock(&priv->lock);
b481de9c
ZY
2962 return IRQ_HANDLED;
2963
2964 none:
2965 /* re-enable interrupts here since we don't have anything to service. */
0359facc
MA
2966 /* only Re-enable if diabled by irq */
2967 if (test_bit(STATUS_INT_ENABLED, &priv->status))
2968 iwl4965_enable_interrupts(priv);
b481de9c
ZY
2969 spin_unlock(&priv->lock);
2970 return IRQ_NONE;
2971}
2972
b481de9c
ZY
2973/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
2974 * sending probe req. This should be set long enough to hear probe responses
2975 * from more than one AP. */
2976#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
2977#define IWL_ACTIVE_DWELL_TIME_52 (10)
2978
2979/* For faster active scanning, scan will move to the next channel if fewer than
2980 * PLCP_QUIET_THRESH packets are heard on this channel within
2981 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
2982 * time if it's a quiet channel (nothing responded to our probe, and there's
2983 * no other traffic).
2984 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
2985#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
2986#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
2987
2988/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
2989 * Must be set longer than active dwell time.
2990 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
2991#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
2992#define IWL_PASSIVE_DWELL_TIME_52 (10)
2993#define IWL_PASSIVE_DWELL_BASE (100)
2994#define IWL_CHANNEL_TUNE_TIME 5
2995
c79dd5b5 2996static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
8318d78a 2997 enum ieee80211_band band)
b481de9c 2998{
8318d78a 2999 if (band == IEEE80211_BAND_5GHZ)
b481de9c
ZY
3000 return IWL_ACTIVE_DWELL_TIME_52;
3001 else
3002 return IWL_ACTIVE_DWELL_TIME_24;
3003}
3004
c79dd5b5 3005static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
8318d78a 3006 enum ieee80211_band band)
b481de9c 3007{
8318d78a
JB
3008 u16 active = iwl4965_get_active_dwell_time(priv, band);
3009 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
b481de9c
ZY
3010 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
3011 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
3012
3109ece1 3013 if (iwl_is_associated(priv)) {
b481de9c
ZY
3014 /* If we're associated, we clamp the maximum passive
3015 * dwell time to be 98% of the beacon interval (minus
3016 * 2 * channel tune time) */
3017 passive = priv->beacon_int;
3018 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
3019 passive = IWL_PASSIVE_DWELL_BASE;
3020 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
3021 }
3022
3023 if (passive <= active)
3024 passive = active + 1;
3025
3026 return passive;
3027}
3028
c79dd5b5 3029static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
8318d78a 3030 enum ieee80211_band band,
b481de9c 3031 u8 is_active, u8 direct_mask,
bb8c093b 3032 struct iwl4965_scan_channel *scan_ch)
b481de9c
ZY
3033{
3034 const struct ieee80211_channel *channels = NULL;
8318d78a 3035 const struct ieee80211_supported_band *sband;
bf85ea4f 3036 const struct iwl_channel_info *ch_info;
b481de9c
ZY
3037 u16 passive_dwell = 0;
3038 u16 active_dwell = 0;
3039 int added, i;
3040
d1141dfb 3041 sband = iwl_get_hw_mode(priv, band);
8318d78a 3042 if (!sband)
b481de9c
ZY
3043 return 0;
3044
8318d78a 3045 channels = sband->channels;
b481de9c 3046
8318d78a
JB
3047 active_dwell = iwl4965_get_active_dwell_time(priv, band);
3048 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
b481de9c 3049
8318d78a 3050 for (i = 0, added = 0; i < sband->n_channels; i++) {
182e2e66
JB
3051 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3052 continue;
3053
8318d78a 3054 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
b481de9c 3055
8622e705 3056 ch_info = iwl_get_channel_info(priv, band,
9fbab516 3057 scan_ch->channel);
b481de9c
ZY
3058 if (!is_channel_valid(ch_info)) {
3059 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
3060 scan_ch->channel);
3061 continue;
3062 }
3063
3064 if (!is_active || is_channel_passive(ch_info) ||
8318d78a 3065 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
b481de9c
ZY
3066 scan_ch->type = 0; /* passive */
3067 else
3068 scan_ch->type = 1; /* active */
3069
3070 if (scan_ch->type & 1)
3071 scan_ch->type |= (direct_mask << 1);
3072
b481de9c
ZY
3073 scan_ch->active_dwell = cpu_to_le16(active_dwell);
3074 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
3075
9fbab516 3076 /* Set txpower levels to defaults */
b481de9c
ZY
3077 scan_ch->tpc.dsp_atten = 110;
3078 /* scan_pwr_info->tpc.dsp_atten; */
3079
3080 /*scan_pwr_info->tpc.tx_gain; */
8318d78a 3081 if (band == IEEE80211_BAND_5GHZ)
b481de9c
ZY
3082 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
3083 else {
3084 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
3085 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
9fbab516 3086 * power level:
8a1b0245 3087 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
b481de9c
ZY
3088 */
3089 }
3090
3091 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
3092 scan_ch->channel,
3093 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
3094 (scan_ch->type & 1) ?
3095 active_dwell : passive_dwell);
3096
3097 scan_ch++;
3098 added++;
3099 }
3100
3101 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
3102 return added;
3103}
3104
b481de9c
ZY
3105/******************************************************************************
3106 *
3107 * uCode download functions
3108 *
3109 ******************************************************************************/
3110
c79dd5b5 3111static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
b481de9c 3112{
98c92211
TW
3113 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
3114 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
3115 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
3116 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
3117 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
3118 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
b481de9c
ZY
3119}
3120
edcdf8b2
RR
3121static void iwl4965_nic_start(struct iwl_priv *priv)
3122{
3123 /* Remove all resets to allow NIC to operate */
3124 iwl_write32(priv, CSR_RESET, 0);
3125}
3126
3127
b481de9c 3128/**
bb8c093b 3129 * iwl4965_read_ucode - Read uCode images from disk file.
b481de9c
ZY
3130 *
3131 * Copy into buffers for card to fetch via bus-mastering
3132 */
c79dd5b5 3133static int iwl4965_read_ucode(struct iwl_priv *priv)
b481de9c 3134{
bb8c093b 3135 struct iwl4965_ucode *ucode;
90e759d1 3136 int ret;
b481de9c 3137 const struct firmware *ucode_raw;
4bf775cd 3138 const char *name = priv->cfg->fw_name;
b481de9c
ZY
3139 u8 *src;
3140 size_t len;
3141 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
3142
3143 /* Ask kernel firmware_class module to get the boot firmware off disk.
3144 * request_firmware() is synchronous, file is in memory on return. */
90e759d1
TW
3145 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
3146 if (ret < 0) {
3147 IWL_ERROR("%s firmware file req failed: Reason %d\n",
3148 name, ret);
b481de9c
ZY
3149 goto error;
3150 }
3151
3152 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
3153 name, ucode_raw->size);
3154
3155 /* Make sure that we got at least our header! */
3156 if (ucode_raw->size < sizeof(*ucode)) {
3157 IWL_ERROR("File size way too small!\n");
90e759d1 3158 ret = -EINVAL;
b481de9c
ZY
3159 goto err_release;
3160 }
3161
3162 /* Data from ucode file: header followed by uCode images */
3163 ucode = (void *)ucode_raw->data;
3164
3165 ver = le32_to_cpu(ucode->ver);
3166 inst_size = le32_to_cpu(ucode->inst_size);
3167 data_size = le32_to_cpu(ucode->data_size);
3168 init_size = le32_to_cpu(ucode->init_size);
3169 init_data_size = le32_to_cpu(ucode->init_data_size);
3170 boot_size = le32_to_cpu(ucode->boot_size);
3171
3172 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
3173 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
3174 inst_size);
3175 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
3176 data_size);
3177 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
3178 init_size);
3179 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
3180 init_data_size);
3181 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
3182 boot_size);
3183
3184 /* Verify size of file vs. image size info in file's header */
3185 if (ucode_raw->size < sizeof(*ucode) +
3186 inst_size + data_size + init_size +
3187 init_data_size + boot_size) {
3188
3189 IWL_DEBUG_INFO("uCode file size %d too small\n",
3190 (int)ucode_raw->size);
90e759d1 3191 ret = -EINVAL;
b481de9c
ZY
3192 goto err_release;
3193 }
3194
3195 /* Verify that uCode images will fit in card's SRAM */
099b40b7 3196 if (inst_size > priv->hw_params.max_inst_size) {
90e759d1
TW
3197 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
3198 inst_size);
3199 ret = -EINVAL;
b481de9c
ZY
3200 goto err_release;
3201 }
3202
099b40b7 3203 if (data_size > priv->hw_params.max_data_size) {
90e759d1
TW
3204 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
3205 data_size);
3206 ret = -EINVAL;
b481de9c
ZY
3207 goto err_release;
3208 }
099b40b7 3209 if (init_size > priv->hw_params.max_inst_size) {
b481de9c 3210 IWL_DEBUG_INFO
90e759d1
TW
3211 ("uCode init instr len %d too large to fit in\n",
3212 init_size);
3213 ret = -EINVAL;
b481de9c
ZY
3214 goto err_release;
3215 }
099b40b7 3216 if (init_data_size > priv->hw_params.max_data_size) {
b481de9c 3217 IWL_DEBUG_INFO
90e759d1
TW
3218 ("uCode init data len %d too large to fit in\n",
3219 init_data_size);
3220 ret = -EINVAL;
b481de9c
ZY
3221 goto err_release;
3222 }
099b40b7 3223 if (boot_size > priv->hw_params.max_bsm_size) {
b481de9c 3224 IWL_DEBUG_INFO
90e759d1
TW
3225 ("uCode boot instr len %d too large to fit in\n",
3226 boot_size);
3227 ret = -EINVAL;
b481de9c
ZY
3228 goto err_release;
3229 }
3230
3231 /* Allocate ucode buffers for card's bus-master loading ... */
3232
3233 /* Runtime instructions and 2 copies of data:
3234 * 1) unmodified from disk
3235 * 2) backup cache for save/restore during power-downs */
3236 priv->ucode_code.len = inst_size;
98c92211 3237 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
b481de9c
ZY
3238
3239 priv->ucode_data.len = data_size;
98c92211 3240 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
b481de9c
ZY
3241
3242 priv->ucode_data_backup.len = data_size;
98c92211 3243 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
b481de9c
ZY
3244
3245 /* Initialization instructions and data */
90e759d1
TW
3246 if (init_size && init_data_size) {
3247 priv->ucode_init.len = init_size;
98c92211 3248 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
90e759d1
TW
3249
3250 priv->ucode_init_data.len = init_data_size;
98c92211 3251 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
90e759d1
TW
3252
3253 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
3254 goto err_pci_alloc;
3255 }
b481de9c
ZY
3256
3257 /* Bootstrap (instructions only, no data) */
90e759d1
TW
3258 if (boot_size) {
3259 priv->ucode_boot.len = boot_size;
98c92211 3260 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
b481de9c 3261
90e759d1
TW
3262 if (!priv->ucode_boot.v_addr)
3263 goto err_pci_alloc;
3264 }
b481de9c
ZY
3265
3266 /* Copy images into buffers for card's bus-master reads ... */
3267
3268 /* Runtime instructions (first block of data in file) */
3269 src = &ucode->data[0];
3270 len = priv->ucode_code.len;
90e759d1 3271 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
b481de9c
ZY
3272 memcpy(priv->ucode_code.v_addr, src, len);
3273 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
3274 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
3275
3276 /* Runtime data (2nd block)
bb8c093b 3277 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
b481de9c
ZY
3278 src = &ucode->data[inst_size];
3279 len = priv->ucode_data.len;
90e759d1 3280 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
b481de9c
ZY
3281 memcpy(priv->ucode_data.v_addr, src, len);
3282 memcpy(priv->ucode_data_backup.v_addr, src, len);
3283
3284 /* Initialization instructions (3rd block) */
3285 if (init_size) {
3286 src = &ucode->data[inst_size + data_size];
3287 len = priv->ucode_init.len;
90e759d1
TW
3288 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
3289 len);
b481de9c
ZY
3290 memcpy(priv->ucode_init.v_addr, src, len);
3291 }
3292
3293 /* Initialization data (4th block) */
3294 if (init_data_size) {
3295 src = &ucode->data[inst_size + data_size + init_size];
3296 len = priv->ucode_init_data.len;
90e759d1
TW
3297 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
3298 len);
b481de9c
ZY
3299 memcpy(priv->ucode_init_data.v_addr, src, len);
3300 }
3301
3302 /* Bootstrap instructions (5th block) */
3303 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
3304 len = priv->ucode_boot.len;
90e759d1 3305 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
b481de9c
ZY
3306 memcpy(priv->ucode_boot.v_addr, src, len);
3307
3308 /* We have our copies now, allow OS release its copies */
3309 release_firmware(ucode_raw);
3310 return 0;
3311
3312 err_pci_alloc:
3313 IWL_ERROR("failed to allocate pci memory\n");
90e759d1 3314 ret = -ENOMEM;
bb8c093b 3315 iwl4965_dealloc_ucode_pci(priv);
b481de9c
ZY
3316
3317 err_release:
3318 release_firmware(ucode_raw);
3319
3320 error:
90e759d1 3321 return ret;
b481de9c
ZY
3322}
3323
b481de9c 3324/**
bb8c093b 3325 * iwl4965_alive_start - called after REPLY_ALIVE notification received
b481de9c 3326 * from protocol/runtime uCode (initialization uCode's
bb8c093b 3327 * Alive gets handled by iwl4965_init_alive_start()).
b481de9c 3328 */
c79dd5b5 3329static void iwl4965_alive_start(struct iwl_priv *priv)
b481de9c 3330{
57aab75a 3331 int ret = 0;
b481de9c
ZY
3332
3333 IWL_DEBUG_INFO("Runtime Alive received.\n");
3334
3335 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
3336 /* We had an error bringing up the hardware, so take it
3337 * all the way back down so we can try again */
3338 IWL_DEBUG_INFO("Alive failed.\n");
3339 goto restart;
3340 }
3341
3342 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
3343 * This is a paranoid check, because we would not have gotten the
3344 * "runtime" alive if code weren't properly loaded. */
b0692f2f 3345 if (iwl_verify_ucode(priv)) {
b481de9c
ZY
3346 /* Runtime instruction load was bad;
3347 * take it all the way back down so we can try again */
3348 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
3349 goto restart;
3350 }
3351
bf85ea4f 3352 iwlcore_clear_stations_table(priv);
b481de9c 3353
57aab75a
TW
3354 ret = priv->cfg->ops->lib->alive_notify(priv);
3355 if (ret) {
b481de9c 3356 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
57aab75a 3357 ret);
b481de9c
ZY
3358 goto restart;
3359 }
3360
9fbab516 3361 /* After the ALIVE response, we can send host commands to 4965 uCode */
b481de9c
ZY
3362 set_bit(STATUS_ALIVE, &priv->status);
3363
3364 /* Clear out the uCode error bit if it is set */
3365 clear_bit(STATUS_FW_ERROR, &priv->status);
3366
fee1247a 3367 if (iwl_is_rfkill(priv))
b481de9c
ZY
3368 return;
3369
5a66926a 3370 ieee80211_start_queues(priv->hw);
b481de9c
ZY
3371
3372 priv->active_rate = priv->rates_mask;
3373 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
3374
3109ece1 3375 if (iwl_is_associated(priv)) {
c1adf9fb
GG
3376 struct iwl_rxon_cmd *active_rxon =
3377 (struct iwl_rxon_cmd *)&priv->active_rxon;
b481de9c
ZY
3378
3379 memcpy(&priv->staging_rxon, &priv->active_rxon,
3380 sizeof(priv->staging_rxon));
3381 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3382 } else {
3383 /* Initialize our rx_config data */
bb8c093b 3384 iwl4965_connection_init_rx_config(priv);
b481de9c
ZY
3385 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
3386 }
3387
9fbab516 3388 /* Configure Bluetooth device coexistence support */
bb8c093b 3389 iwl4965_send_bt_config(priv);
b481de9c
ZY
3390
3391 /* Configure the adapter for unassociated operation */
bb8c093b 3392 iwl4965_commit_rxon(priv);
b481de9c
ZY
3393
3394 /* At this point, the NIC is initialized and operational */
3395 priv->notif_missed_beacons = 0;
b481de9c
ZY
3396
3397 iwl4965_rf_kill_ct_config(priv);
5a66926a 3398
fe00b5a5
RC
3399 iwl_leds_register(priv);
3400
b481de9c 3401 IWL_DEBUG_INFO("ALIVE processing complete.\n");
a9f46786 3402 set_bit(STATUS_READY, &priv->status);
5a66926a 3403 wake_up_interruptible(&priv->wait_command_queue);
b481de9c
ZY
3404
3405 if (priv->error_recovering)
bb8c093b 3406 iwl4965_error_recovery(priv);
b481de9c 3407
c8381fdc 3408 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
84363e6e 3409 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
b481de9c
ZY
3410 return;
3411
3412 restart:
3413 queue_work(priv->workqueue, &priv->restart);
3414}
3415
c79dd5b5 3416static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
b481de9c 3417
c79dd5b5 3418static void __iwl4965_down(struct iwl_priv *priv)
b481de9c
ZY
3419{
3420 unsigned long flags;
3421 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
3422 struct ieee80211_conf *conf = NULL;
3423
3424 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
3425
3426 conf = ieee80211_get_hw_conf(priv->hw);
3427
3428 if (!exit_pending)
3429 set_bit(STATUS_EXIT_PENDING, &priv->status);
3430
ab53d8af
MA
3431 iwl_leds_unregister(priv);
3432
c8381fdc
MA
3433 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
3434
bf85ea4f 3435 iwlcore_clear_stations_table(priv);
b481de9c
ZY
3436
3437 /* Unblock any waiting calls */
3438 wake_up_interruptible_all(&priv->wait_command_queue);
3439
b481de9c
ZY
3440 /* Wipe out the EXIT_PENDING status bit if we are not actually
3441 * exiting the module */
3442 if (!exit_pending)
3443 clear_bit(STATUS_EXIT_PENDING, &priv->status);
3444
3445 /* stop and reset the on-board processor */
3395f6e9 3446 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
b481de9c
ZY
3447
3448 /* tell the device to stop sending interrupts */
0359facc 3449 spin_lock_irqsave(&priv->lock, flags);
bb8c093b 3450 iwl4965_disable_interrupts(priv);
0359facc
MA
3451 spin_unlock_irqrestore(&priv->lock, flags);
3452 iwl_synchronize_irq(priv);
b481de9c
ZY
3453
3454 if (priv->mac80211_registered)
3455 ieee80211_stop_queues(priv->hw);
3456
bb8c093b 3457 /* If we have not previously called iwl4965_init() then
b481de9c 3458 * clear all bits but the RF Kill and SUSPEND bits and return */
fee1247a 3459 if (!iwl_is_init(priv)) {
b481de9c
ZY
3460 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
3461 STATUS_RF_KILL_HW |
3462 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
3463 STATUS_RF_KILL_SW |
9788864e
RC
3464 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
3465 STATUS_GEO_CONFIGURED |
b481de9c
ZY
3466 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
3467 STATUS_IN_SUSPEND;
3468 goto exit;
3469 }
3470
3471 /* ...otherwise clear out all the status bits but the RF Kill and
3472 * SUSPEND bits and continue taking the NIC down. */
3473 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
3474 STATUS_RF_KILL_HW |
3475 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
3476 STATUS_RF_KILL_SW |
9788864e
RC
3477 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
3478 STATUS_GEO_CONFIGURED |
b481de9c
ZY
3479 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
3480 STATUS_IN_SUSPEND |
3481 test_bit(STATUS_FW_ERROR, &priv->status) <<
3482 STATUS_FW_ERROR;
3483
3484 spin_lock_irqsave(&priv->lock, flags);
3395f6e9 3485 iwl_clear_bit(priv, CSR_GP_CNTRL,
9fbab516 3486 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
b481de9c
ZY
3487 spin_unlock_irqrestore(&priv->lock, flags);
3488
bb8c093b
CH
3489 iwl4965_hw_txq_ctx_stop(priv);
3490 iwl4965_hw_rxq_stop(priv);
b481de9c
ZY
3491
3492 spin_lock_irqsave(&priv->lock, flags);
3395f6e9
TW
3493 if (!iwl_grab_nic_access(priv)) {
3494 iwl_write_prph(priv, APMG_CLK_DIS_REG,
b481de9c 3495 APMG_CLK_VAL_DMA_CLK_RQT);
3395f6e9 3496 iwl_release_nic_access(priv);
b481de9c
ZY
3497 }
3498 spin_unlock_irqrestore(&priv->lock, flags);
3499
3500 udelay(5);
3501
bb8c093b 3502 iwl4965_hw_nic_stop_master(priv);
3395f6e9 3503 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
bb8c093b 3504 iwl4965_hw_nic_reset(priv);
399f4900 3505 priv->cfg->ops->lib->free_shared_mem(priv);
b481de9c
ZY
3506
3507 exit:
bb8c093b 3508 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
b481de9c
ZY
3509
3510 if (priv->ibss_beacon)
3511 dev_kfree_skb(priv->ibss_beacon);
3512 priv->ibss_beacon = NULL;
3513
3514 /* clear out any free frames */
fcab423d 3515 iwl_clear_free_frames(priv);
b481de9c
ZY
3516}
3517
c79dd5b5 3518static void iwl4965_down(struct iwl_priv *priv)
b481de9c
ZY
3519{
3520 mutex_lock(&priv->mutex);
bb8c093b 3521 __iwl4965_down(priv);
b481de9c 3522 mutex_unlock(&priv->mutex);
b24d22b1 3523
bb8c093b 3524 iwl4965_cancel_deferred_work(priv);
b481de9c
ZY
3525}
3526
3527#define MAX_HW_RESTARTS 5
3528
c79dd5b5 3529static int __iwl4965_up(struct iwl_priv *priv)
b481de9c 3530{
57aab75a
TW
3531 int i;
3532 int ret;
b481de9c
ZY
3533
3534 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3535 IWL_WARNING("Exit pending; will not bring the NIC up\n");
3536 return -EIO;
3537 }
3538
3539 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
3540 IWL_WARNING("Radio disabled by SW RF kill (module "
3541 "parameter)\n");
ad97edd2 3542 iwl_rfkill_set_hw_state(priv);
e655b9f0
ZY
3543 return -ENODEV;
3544 }
3545
e903fbd4
RC
3546 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
3547 IWL_ERROR("ucode not available for device bringup\n");
3548 return -EIO;
3549 }
3550
e655b9f0 3551 /* If platform's RF_KILL switch is NOT set to KILL */
3395f6e9 3552 if (iwl_read32(priv, CSR_GP_CNTRL) &
e655b9f0
ZY
3553 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3554 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3555 else {
3556 set_bit(STATUS_RF_KILL_HW, &priv->status);
3557 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
ad97edd2 3558 iwl_rfkill_set_hw_state(priv);
e655b9f0
ZY
3559 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
3560 return -ENODEV;
3561 }
b481de9c
ZY
3562 }
3563
ad97edd2 3564 iwl_rfkill_set_hw_state(priv);
3395f6e9 3565 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
b481de9c 3566
399f4900
RR
3567 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
3568 if (ret) {
3569 IWL_ERROR("Unable to allocate shared memory\n");
3570 return ret;
3571 }
3572
1053d35f 3573 ret = iwl_hw_nic_init(priv);
57aab75a
TW
3574 if (ret) {
3575 IWL_ERROR("Unable to init nic\n");
3576 return ret;
b481de9c
ZY
3577 }
3578
3579 /* make sure rfkill handshake bits are cleared */
3395f6e9
TW
3580 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3581 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
b481de9c
ZY
3582 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3583
3584 /* clear (again), then enable host interrupts */
3395f6e9 3585 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
bb8c093b 3586 iwl4965_enable_interrupts(priv);
b481de9c
ZY
3587
3588 /* really make sure rfkill handshake bits are cleared */
3395f6e9
TW
3589 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3590 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
b481de9c
ZY
3591
3592 /* Copy original ucode data image from disk into backup cache.
3593 * This will be used to initialize the on-board processor's
3594 * data SRAM for a clean start when the runtime program first loads. */
3595 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
5a66926a 3596 priv->ucode_data.len);
b481de9c 3597
e655b9f0
ZY
3598 /* We return success when we resume from suspend and rf_kill is on. */
3599 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
b481de9c 3600 return 0;
b481de9c
ZY
3601
3602 for (i = 0; i < MAX_HW_RESTARTS; i++) {
3603
bf85ea4f 3604 iwlcore_clear_stations_table(priv);
b481de9c
ZY
3605
3606 /* load bootstrap state machine,
3607 * load bootstrap program into processor's memory,
3608 * prepare to load the "initialize" uCode */
57aab75a 3609 ret = priv->cfg->ops->lib->load_ucode(priv);
b481de9c 3610
57aab75a
TW
3611 if (ret) {
3612 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
b481de9c
ZY
3613 continue;
3614 }
3615
3616 /* start card; "initialize" will load runtime ucode */
edcdf8b2 3617 iwl4965_nic_start(priv);
b481de9c 3618
b481de9c
ZY
3619 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
3620
3621 return 0;
3622 }
3623
3624 set_bit(STATUS_EXIT_PENDING, &priv->status);
bb8c093b 3625 __iwl4965_down(priv);
b481de9c
ZY
3626
3627 /* tried to restart and config the device for as long as our
3628 * patience could withstand */
3629 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
3630 return -EIO;
3631}
3632
3633
3634/*****************************************************************************
3635 *
3636 * Workqueue callbacks
3637 *
3638 *****************************************************************************/
3639
bb8c093b 3640static void iwl4965_bg_init_alive_start(struct work_struct *data)
b481de9c 3641{
c79dd5b5
TW
3642 struct iwl_priv *priv =
3643 container_of(data, struct iwl_priv, init_alive_start.work);
b481de9c
ZY
3644
3645 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3646 return;
3647
3648 mutex_lock(&priv->mutex);
f3ccc08c 3649 priv->cfg->ops->lib->init_alive_start(priv);
b481de9c
ZY
3650 mutex_unlock(&priv->mutex);
3651}
3652
bb8c093b 3653static void iwl4965_bg_alive_start(struct work_struct *data)
b481de9c 3654{
c79dd5b5
TW
3655 struct iwl_priv *priv =
3656 container_of(data, struct iwl_priv, alive_start.work);
b481de9c
ZY
3657
3658 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3659 return;
3660
3661 mutex_lock(&priv->mutex);
bb8c093b 3662 iwl4965_alive_start(priv);
b481de9c
ZY
3663 mutex_unlock(&priv->mutex);
3664}
3665
bb8c093b 3666static void iwl4965_bg_rf_kill(struct work_struct *work)
b481de9c 3667{
c79dd5b5 3668 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
b481de9c
ZY
3669
3670 wake_up_interruptible(&priv->wait_command_queue);
3671
3672 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3673 return;
3674
3675 mutex_lock(&priv->mutex);
3676
fee1247a 3677 if (!iwl_is_rfkill(priv)) {
f3d67999 3678 IWL_DEBUG(IWL_DL_RF_KILL,
b481de9c
ZY
3679 "HW and/or SW RF Kill no longer active, restarting "
3680 "device\n");
3681 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
3682 queue_work(priv->workqueue, &priv->restart);
3683 } else {
ad97edd2
MA
3684 /* make sure mac80211 stop sending Tx frame */
3685 if (priv->mac80211_registered)
3686 ieee80211_stop_queues(priv->hw);
b481de9c
ZY
3687
3688 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
3689 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3690 "disabled by SW switch\n");
3691 else
3692 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
3693 "Kill switch must be turned off for "
3694 "wireless networking to work.\n");
3695 }
ad97edd2
MA
3696 iwl_rfkill_set_hw_state(priv);
3697
b481de9c
ZY
3698 mutex_unlock(&priv->mutex);
3699}
3700
4419e39b
AK
3701static void iwl4965_bg_set_monitor(struct work_struct *work)
3702{
3703 struct iwl_priv *priv = container_of(work,
3704 struct iwl_priv, set_monitor);
3705
3706 IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
3707
3708 mutex_lock(&priv->mutex);
3709
3710 if (!iwl_is_ready(priv))
3711 IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
3712 else
3713 if (iwl4965_set_mode(priv, IEEE80211_IF_TYPE_MNTR) != 0)
3714 IWL_ERROR("iwl4965_set_mode() failed\n");
3715
3716 mutex_unlock(&priv->mutex);
3717}
3718
b481de9c
ZY
3719#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
3720
bb8c093b 3721static void iwl4965_bg_scan_check(struct work_struct *data)
b481de9c 3722{
c79dd5b5
TW
3723 struct iwl_priv *priv =
3724 container_of(data, struct iwl_priv, scan_check.work);
b481de9c
ZY
3725
3726 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3727 return;
3728
3729 mutex_lock(&priv->mutex);
3730 if (test_bit(STATUS_SCANNING, &priv->status) ||
3731 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
f3d67999
EK
3732 IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
3733 "adapter (%dms)\n",
3734 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
052c4b9f 3735
b481de9c 3736 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
bb8c093b 3737 iwl4965_send_scan_abort(priv);
b481de9c
ZY
3738 }
3739 mutex_unlock(&priv->mutex);
3740}
3741
bb8c093b 3742static void iwl4965_bg_request_scan(struct work_struct *data)
b481de9c 3743{
c79dd5b5
TW
3744 struct iwl_priv *priv =
3745 container_of(data, struct iwl_priv, request_scan);
857485c0 3746 struct iwl_host_cmd cmd = {
b481de9c 3747 .id = REPLY_SCAN_CMD,
bb8c093b 3748 .len = sizeof(struct iwl4965_scan_cmd),
b481de9c
ZY
3749 .meta.flags = CMD_SIZE_HUGE,
3750 };
bb8c093b 3751 struct iwl4965_scan_cmd *scan;
b481de9c 3752 struct ieee80211_conf *conf = NULL;
78330fdd 3753 u16 cmd_len;
8318d78a 3754 enum ieee80211_band band;
78330fdd 3755 u8 direct_mask;
857485c0 3756 int ret = 0;
b481de9c
ZY
3757
3758 conf = ieee80211_get_hw_conf(priv->hw);
3759
3760 mutex_lock(&priv->mutex);
3761
fee1247a 3762 if (!iwl_is_ready(priv)) {
b481de9c
ZY
3763 IWL_WARNING("request scan called when driver not ready.\n");
3764 goto done;
3765 }
3766
3767 /* Make sure the scan wasn't cancelled before this queued work
3768 * was given the chance to run... */
3769 if (!test_bit(STATUS_SCANNING, &priv->status))
3770 goto done;
3771
3772 /* This should never be called or scheduled if there is currently
3773 * a scan active in the hardware. */
3774 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
3775 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
3776 "Ignoring second request.\n");
857485c0 3777 ret = -EIO;
b481de9c
ZY
3778 goto done;
3779 }
3780
3781 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3782 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
3783 goto done;
3784 }
3785
3786 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3787 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
3788 goto done;
3789 }
3790
fee1247a 3791 if (iwl_is_rfkill(priv)) {
b481de9c
ZY
3792 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
3793 goto done;
3794 }
3795
3796 if (!test_bit(STATUS_READY, &priv->status)) {
3797 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
3798 goto done;
3799 }
3800
3801 if (!priv->scan_bands) {
3802 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
3803 goto done;
3804 }
3805
3806 if (!priv->scan) {
bb8c093b 3807 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
b481de9c
ZY
3808 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
3809 if (!priv->scan) {
857485c0 3810 ret = -ENOMEM;
b481de9c
ZY
3811 goto done;
3812 }
3813 }
3814 scan = priv->scan;
bb8c093b 3815 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
b481de9c
ZY
3816
3817 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
3818 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
3819
3109ece1 3820 if (iwl_is_associated(priv)) {
b481de9c
ZY
3821 u16 interval = 0;
3822 u32 extra;
3823 u32 suspend_time = 100;
3824 u32 scan_suspend_time = 100;
3825 unsigned long flags;
3826
3827 IWL_DEBUG_INFO("Scanning while associated...\n");
3828
3829 spin_lock_irqsave(&priv->lock, flags);
3830 interval = priv->beacon_int;
3831 spin_unlock_irqrestore(&priv->lock, flags);
3832
3833 scan->suspend_time = 0;
052c4b9f 3834 scan->max_out_time = cpu_to_le32(200 * 1024);
b481de9c
ZY
3835 if (!interval)
3836 interval = suspend_time;
3837
3838 extra = (suspend_time / interval) << 22;
3839 scan_suspend_time = (extra |
3840 ((suspend_time % interval) * 1024));
3841 scan->suspend_time = cpu_to_le32(scan_suspend_time);
3842 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
3843 scan_suspend_time, interval);
3844 }
3845
3846 /* We should add the ability for user to lock to PASSIVE ONLY */
3847 if (priv->one_direct_scan) {
3848 IWL_DEBUG_SCAN
3849 ("Kicking off one direct scan for '%s'\n",
bb8c093b 3850 iwl4965_escape_essid(priv->direct_ssid,
b481de9c
ZY
3851 priv->direct_ssid_len));
3852 scan->direct_scan[0].id = WLAN_EID_SSID;
3853 scan->direct_scan[0].len = priv->direct_ssid_len;
3854 memcpy(scan->direct_scan[0].ssid,
3855 priv->direct_ssid, priv->direct_ssid_len);
3856 direct_mask = 1;
3109ece1 3857 } else if (!iwl_is_associated(priv) && priv->essid_len) {
786b4557
BM
3858 IWL_DEBUG_SCAN
3859 ("Kicking off one direct scan for '%s' when not associated\n",
3860 iwl4965_escape_essid(priv->essid, priv->essid_len));
b481de9c
ZY
3861 scan->direct_scan[0].id = WLAN_EID_SSID;
3862 scan->direct_scan[0].len = priv->essid_len;
3863 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
3864 direct_mask = 1;
857485c0 3865 } else {
786b4557 3866 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
b481de9c 3867 direct_mask = 0;
857485c0 3868 }
b481de9c 3869
b481de9c 3870 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
5425e490 3871 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
b481de9c
ZY
3872 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
3873
b481de9c
ZY
3874
3875 switch (priv->scan_bands) {
3876 case 2:
3877 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
3878 scan->tx_cmd.rate_n_flags =
bb8c093b 3879 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
b481de9c
ZY
3880 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
3881
3882 scan->good_CRC_th = 0;
8318d78a 3883 band = IEEE80211_BAND_2GHZ;
b481de9c
ZY
3884 break;
3885
3886 case 1:
3887 scan->tx_cmd.rate_n_flags =
bb8c093b 3888 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
b481de9c
ZY
3889 RATE_MCS_ANT_B_MSK);
3890 scan->good_CRC_th = IWL_GOOD_CRC_TH;
8318d78a 3891 band = IEEE80211_BAND_5GHZ;
b481de9c
ZY
3892 break;
3893
3894 default:
3895 IWL_WARNING("Invalid scan band count\n");
3896 goto done;
3897 }
3898
78330fdd
TW
3899 /* We don't build a direct scan probe request; the uCode will do
3900 * that based on the direct_mask added to each channel entry */
3901 cmd_len = iwl4965_fill_probe_req(priv, band,
3902 (struct ieee80211_mgmt *)scan->data,
3903 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
3904
3905 scan->tx_cmd.len = cpu_to_le16(cmd_len);
b481de9c
ZY
3906 /* select Rx chains */
3907
3908 /* Force use of chains B and C (0x6) for scan Rx.
3909 * Avoid A (0x1) because of its off-channel reception on A-band.
3910 * MIMO is not used here, but value is required to make uCode happy. */
3911 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
3912 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
3913 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
3914 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
3915
3916 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
3917 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
3918
786b4557 3919 if (direct_mask)
26c0f03f
RC
3920 scan->channel_count =
3921 iwl4965_get_channels_for_scan(
3922 priv, band, 1, /* active */
3923 direct_mask,
3924 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
786b4557 3925 else
26c0f03f
RC
3926 scan->channel_count =
3927 iwl4965_get_channels_for_scan(
3928 priv, band, 0, /* passive */
3929 direct_mask,
3930 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
b481de9c 3931
5da4b55f
MA
3932 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
3933 RXON_FILTER_BCON_AWARE_MSK);
b481de9c 3934 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
bb8c093b 3935 scan->channel_count * sizeof(struct iwl4965_scan_channel);
b481de9c
ZY
3936 cmd.data = scan;
3937 scan->len = cpu_to_le16(cmd.len);
3938
3939 set_bit(STATUS_SCAN_HW, &priv->status);
857485c0
TW
3940 ret = iwl_send_cmd_sync(priv, &cmd);
3941 if (ret)
b481de9c
ZY
3942 goto done;
3943
3944 queue_delayed_work(priv->workqueue, &priv->scan_check,
3945 IWL_SCAN_CHECK_WATCHDOG);
3946
3947 mutex_unlock(&priv->mutex);
3948 return;
3949
3950 done:
01ebd063 3951 /* inform mac80211 scan aborted */
b481de9c
ZY
3952 queue_work(priv->workqueue, &priv->scan_completed);
3953 mutex_unlock(&priv->mutex);
3954}
3955
bb8c093b 3956static void iwl4965_bg_up(struct work_struct *data)
b481de9c 3957{
c79dd5b5 3958 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
b481de9c
ZY
3959
3960 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3961 return;
3962
3963 mutex_lock(&priv->mutex);
bb8c093b 3964 __iwl4965_up(priv);
b481de9c
ZY
3965 mutex_unlock(&priv->mutex);
3966}
3967
bb8c093b 3968static void iwl4965_bg_restart(struct work_struct *data)
b481de9c 3969{
c79dd5b5 3970 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
b481de9c
ZY
3971
3972 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3973 return;
3974
bb8c093b 3975 iwl4965_down(priv);
b481de9c
ZY
3976 queue_work(priv->workqueue, &priv->up);
3977}
3978
bb8c093b 3979static void iwl4965_bg_rx_replenish(struct work_struct *data)
b481de9c 3980{
c79dd5b5
TW
3981 struct iwl_priv *priv =
3982 container_of(data, struct iwl_priv, rx_replenish);
b481de9c
ZY
3983
3984 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3985 return;
3986
3987 mutex_lock(&priv->mutex);
a55360e4 3988 iwl_rx_replenish(priv);
b481de9c
ZY
3989 mutex_unlock(&priv->mutex);
3990}
3991
7878a5a4
MA
3992#define IWL_DELAY_NEXT_SCAN (HZ*2)
3993
508e32e1 3994static void iwl4965_post_associate(struct iwl_priv *priv)
b481de9c 3995{
b481de9c 3996 struct ieee80211_conf *conf = NULL;
857485c0 3997 int ret = 0;
0795af57 3998 DECLARE_MAC_BUF(mac);
b481de9c
ZY
3999
4000 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
4001 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
4002 return;
4003 }
4004
0795af57
JP
4005 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
4006 priv->assoc_id,
4007 print_mac(mac, priv->active_rxon.bssid_addr));
b481de9c
ZY
4008
4009
4010 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4011 return;
4012
b481de9c 4013
508e32e1 4014 if (!priv->vif || !priv->is_open)
948c171c 4015 return;
508e32e1 4016
bb8c093b 4017 iwl4965_scan_cancel_timeout(priv, 200);
052c4b9f 4018
b481de9c
ZY
4019 conf = ieee80211_get_hw_conf(priv->hw);
4020
4021 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
bb8c093b 4022 iwl4965_commit_rxon(priv);
b481de9c 4023
bb8c093b
CH
4024 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
4025 iwl4965_setup_rxon_timing(priv);
857485c0 4026 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
b481de9c 4027 sizeof(priv->rxon_timing), &priv->rxon_timing);
857485c0 4028 if (ret)
b481de9c
ZY
4029 IWL_WARNING("REPLY_RXON_TIMING failed - "
4030 "Attempting to continue.\n");
4031
4032 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
4033
c8b0e6e1 4034#ifdef CONFIG_IWL4965_HT
fd105e79 4035 if (priv->current_ht_config.is_ht)
47c5196e 4036 iwl_set_rxon_ht(priv, &priv->current_ht_config);
c8b0e6e1 4037#endif /* CONFIG_IWL4965_HT*/
c7de35cd 4038 iwl_set_rxon_chain(priv);
b481de9c
ZY
4039 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
4040
4041 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
4042 priv->assoc_id, priv->beacon_int);
4043
4044 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
4045 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
4046 else
4047 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
4048
4049 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
4050 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
4051 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
4052 else
4053 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
4054
4055 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4056 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
4057
4058 }
4059
bb8c093b 4060 iwl4965_commit_rxon(priv);
b481de9c
ZY
4061
4062 switch (priv->iw_mode) {
4063 case IEEE80211_IF_TYPE_STA:
bb8c093b 4064 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
b481de9c
ZY
4065 break;
4066
4067 case IEEE80211_IF_TYPE_IBSS:
4068
4069 /* clear out the station table */
bf85ea4f 4070 iwlcore_clear_stations_table(priv);
b481de9c 4071
4f40e4d9
TW
4072 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
4073 iwl_rxon_add_station(priv, priv->bssid, 0);
bb8c093b
CH
4074 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
4075 iwl4965_send_beacon_cmd(priv);
b481de9c
ZY
4076
4077 break;
4078
4079 default:
4080 IWL_ERROR("%s Should not be called in %d mode\n",
4081 __FUNCTION__, priv->iw_mode);
4082 break;
4083 }
4084
bb8c093b 4085 iwl4965_sequence_reset(priv);
b481de9c 4086
b481de9c 4087 /* Enable Rx differential gain and sensitivity calibrations */
f0832f13 4088 iwl_chain_noise_reset(priv);
b481de9c 4089 priv->start_calib = 1;
b481de9c
ZY
4090
4091 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4092 priv->assoc_station_added = 1;
4093
bb8c093b 4094 iwl4965_activate_qos(priv, 0);
292ae174 4095
5da4b55f 4096 iwl_power_update_mode(priv, 0);
7878a5a4
MA
4097 /* we have just associated, don't start scan too early */
4098 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
508e32e1
RC
4099}
4100
4101
4102static void iwl4965_bg_post_associate(struct work_struct *data)
4103{
4104 struct iwl_priv *priv = container_of(data, struct iwl_priv,
4105 post_associate.work);
4106
4107 mutex_lock(&priv->mutex);
4108 iwl4965_post_associate(priv);
b481de9c 4109 mutex_unlock(&priv->mutex);
508e32e1 4110
b481de9c
ZY
4111}
4112
bb8c093b 4113static void iwl4965_bg_abort_scan(struct work_struct *work)
b481de9c 4114{
c79dd5b5 4115 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
b481de9c 4116
fee1247a 4117 if (!iwl_is_ready(priv))
b481de9c
ZY
4118 return;
4119
4120 mutex_lock(&priv->mutex);
4121
4122 set_bit(STATUS_SCAN_ABORTING, &priv->status);
bb8c093b 4123 iwl4965_send_scan_abort(priv);
b481de9c
ZY
4124
4125 mutex_unlock(&priv->mutex);
4126}
4127
76bb77e0
ZY
4128static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
4129
bb8c093b 4130static void iwl4965_bg_scan_completed(struct work_struct *work)
b481de9c 4131{
c79dd5b5
TW
4132 struct iwl_priv *priv =
4133 container_of(work, struct iwl_priv, scan_completed);
b481de9c 4134
f3d67999 4135 IWL_DEBUG(IWL_DL_SCAN, "SCAN complete scan\n");
b481de9c
ZY
4136
4137 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4138 return;
4139
a0646470
ZY
4140 if (test_bit(STATUS_CONF_PENDING, &priv->status))
4141 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
76bb77e0 4142
b481de9c
ZY
4143 ieee80211_scan_completed(priv->hw);
4144
4145 /* Since setting the TXPOWER may have been deferred while
4146 * performing the scan, fire one off */
4147 mutex_lock(&priv->mutex);
bb8c093b 4148 iwl4965_hw_reg_send_txpower(priv);
b481de9c
ZY
4149 mutex_unlock(&priv->mutex);
4150}
4151
4152/*****************************************************************************
4153 *
4154 * mac80211 entry point functions
4155 *
4156 *****************************************************************************/
4157
5a66926a
ZY
4158#define UCODE_READY_TIMEOUT (2 * HZ)
4159
bb8c093b 4160static int iwl4965_mac_start(struct ieee80211_hw *hw)
b481de9c 4161{
c79dd5b5 4162 struct iwl_priv *priv = hw->priv;
5a66926a 4163 int ret;
b481de9c
ZY
4164
4165 IWL_DEBUG_MAC80211("enter\n");
4166
5a66926a
ZY
4167 if (pci_enable_device(priv->pci_dev)) {
4168 IWL_ERROR("Fail to pci_enable_device\n");
4169 return -ENODEV;
4170 }
4171 pci_restore_state(priv->pci_dev);
4172 pci_enable_msi(priv->pci_dev);
4173
4174 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
4175 DRV_NAME, priv);
4176 if (ret) {
4177 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
4178 goto out_disable_msi;
4179 }
4180
b481de9c
ZY
4181 /* we should be verifying the device is ready to be opened */
4182 mutex_lock(&priv->mutex);
4183
c1adf9fb 4184 memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
5a66926a
ZY
4185 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
4186 * ucode filename and max sizes are card-specific. */
b481de9c 4187
5a66926a
ZY
4188 if (!priv->ucode_code.len) {
4189 ret = iwl4965_read_ucode(priv);
4190 if (ret) {
4191 IWL_ERROR("Could not read microcode: %d\n", ret);
4192 mutex_unlock(&priv->mutex);
4193 goto out_release_irq;
4194 }
4195 }
b481de9c 4196
e655b9f0 4197 ret = __iwl4965_up(priv);
5a66926a 4198
b481de9c 4199 mutex_unlock(&priv->mutex);
5a66926a 4200
e655b9f0
ZY
4201 if (ret)
4202 goto out_release_irq;
4203
4204 IWL_DEBUG_INFO("Start UP work done.\n");
4205
4206 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
4207 return 0;
4208
5a66926a
ZY
4209 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
4210 * mac80211 will not be run successfully. */
4211 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
4212 test_bit(STATUS_READY, &priv->status),
4213 UCODE_READY_TIMEOUT);
4214 if (!ret) {
4215 if (!test_bit(STATUS_READY, &priv->status)) {
4216 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
4217 jiffies_to_msecs(UCODE_READY_TIMEOUT));
4218 ret = -ETIMEDOUT;
4219 goto out_release_irq;
4220 }
4221 }
4222
e655b9f0 4223 priv->is_open = 1;
b481de9c
ZY
4224 IWL_DEBUG_MAC80211("leave\n");
4225 return 0;
5a66926a
ZY
4226
4227out_release_irq:
4228 free_irq(priv->pci_dev->irq, priv);
4229out_disable_msi:
4230 pci_disable_msi(priv->pci_dev);
e655b9f0
ZY
4231 pci_disable_device(priv->pci_dev);
4232 priv->is_open = 0;
4233 IWL_DEBUG_MAC80211("leave - failed\n");
5a66926a 4234 return ret;
b481de9c
ZY
4235}
4236
bb8c093b 4237static void iwl4965_mac_stop(struct ieee80211_hw *hw)
b481de9c 4238{
c79dd5b5 4239 struct iwl_priv *priv = hw->priv;
b481de9c
ZY
4240
4241 IWL_DEBUG_MAC80211("enter\n");
948c171c 4242
e655b9f0
ZY
4243 if (!priv->is_open) {
4244 IWL_DEBUG_MAC80211("leave - skip\n");
4245 return;
4246 }
4247
b481de9c 4248 priv->is_open = 0;
5a66926a 4249
fee1247a 4250 if (iwl_is_ready_rf(priv)) {
e655b9f0
ZY
4251 /* stop mac, cancel any scan request and clear
4252 * RXON_FILTER_ASSOC_MSK BIT
4253 */
5a66926a
ZY
4254 mutex_lock(&priv->mutex);
4255 iwl4965_scan_cancel_timeout(priv, 100);
4256 cancel_delayed_work(&priv->post_associate);
fde3571f 4257 mutex_unlock(&priv->mutex);
fde3571f
MA
4258 }
4259
5a66926a
ZY
4260 iwl4965_down(priv);
4261
4262 flush_workqueue(priv->workqueue);
4263 free_irq(priv->pci_dev->irq, priv);
4264 pci_disable_msi(priv->pci_dev);
4265 pci_save_state(priv->pci_dev);
4266 pci_disable_device(priv->pci_dev);
948c171c 4267
b481de9c 4268 IWL_DEBUG_MAC80211("leave\n");
b481de9c
ZY
4269}
4270
bb8c093b 4271static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
b481de9c
ZY
4272 struct ieee80211_tx_control *ctl)
4273{
c79dd5b5 4274 struct iwl_priv *priv = hw->priv;
b481de9c
ZY
4275
4276 IWL_DEBUG_MAC80211("enter\n");
4277
4278 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
4279 IWL_DEBUG_MAC80211("leave - monitor\n");
4280 return -1;
4281 }
4282
4283 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
8318d78a 4284 ctl->tx_rate->bitrate);
b481de9c 4285
fd4abac5 4286 if (iwl_tx_skb(priv, skb, ctl))
b481de9c
ZY
4287 dev_kfree_skb_any(skb);
4288
4289 IWL_DEBUG_MAC80211("leave\n");
4290 return 0;
4291}
4292
bb8c093b 4293static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
b481de9c
ZY
4294 struct ieee80211_if_init_conf *conf)
4295{
c79dd5b5 4296 struct iwl_priv *priv = hw->priv;
b481de9c 4297 unsigned long flags;
0795af57 4298 DECLARE_MAC_BUF(mac);
b481de9c 4299
32bfd35d 4300 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
b481de9c 4301
32bfd35d
JB
4302 if (priv->vif) {
4303 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
75849d28 4304 return -EOPNOTSUPP;
b481de9c
ZY
4305 }
4306
4307 spin_lock_irqsave(&priv->lock, flags);
32bfd35d 4308 priv->vif = conf->vif;
b481de9c
ZY
4309
4310 spin_unlock_irqrestore(&priv->lock, flags);
4311
4312 mutex_lock(&priv->mutex);
864792e3
TW
4313
4314 if (conf->mac_addr) {
4315 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
4316 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
4317 }
b481de9c 4318
fee1247a 4319 if (iwl_is_ready(priv))
5a66926a
ZY
4320 iwl4965_set_mode(priv, conf->type);
4321
b481de9c
ZY
4322 mutex_unlock(&priv->mutex);
4323
5a66926a 4324 IWL_DEBUG_MAC80211("leave\n");
b481de9c
ZY
4325 return 0;
4326}
4327
4328/**
bb8c093b 4329 * iwl4965_mac_config - mac80211 config callback
b481de9c
ZY
4330 *
4331 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
4332 * be set inappropriately and the driver currently sets the hardware up to
4333 * use it whenever needed.
4334 */
bb8c093b 4335static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
b481de9c 4336{
c79dd5b5 4337 struct iwl_priv *priv = hw->priv;
bf85ea4f 4338 const struct iwl_channel_info *ch_info;
b481de9c 4339 unsigned long flags;
76bb77e0 4340 int ret = 0;
b481de9c
ZY
4341
4342 mutex_lock(&priv->mutex);
8318d78a 4343 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
b481de9c 4344
12342c47
ZY
4345 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
4346
fee1247a 4347 if (!iwl_is_ready(priv)) {
b481de9c 4348 IWL_DEBUG_MAC80211("leave - not ready\n");
76bb77e0
ZY
4349 ret = -EIO;
4350 goto out;
b481de9c
ZY
4351 }
4352
1ea87396 4353 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
b481de9c 4354 test_bit(STATUS_SCANNING, &priv->status))) {
a0646470
ZY
4355 IWL_DEBUG_MAC80211("leave - scanning\n");
4356 set_bit(STATUS_CONF_PENDING, &priv->status);
b481de9c 4357 mutex_unlock(&priv->mutex);
a0646470 4358 return 0;
b481de9c
ZY
4359 }
4360
4361 spin_lock_irqsave(&priv->lock, flags);
4362
8622e705 4363 ch_info = iwl_get_channel_info(priv, conf->channel->band,
8318d78a 4364 ieee80211_frequency_to_channel(conf->channel->center_freq));
b481de9c 4365 if (!is_channel_valid(ch_info)) {
b481de9c
ZY
4366 IWL_DEBUG_MAC80211("leave - invalid channel\n");
4367 spin_unlock_irqrestore(&priv->lock, flags);
76bb77e0
ZY
4368 ret = -EINVAL;
4369 goto out;
b481de9c
ZY
4370 }
4371
c8b0e6e1 4372#ifdef CONFIG_IWL4965_HT
78330fdd 4373 /* if we are switching from ht to 2.4 clear flags
b481de9c
ZY
4374 * from any ht related info since 2.4 does not
4375 * support ht */
78330fdd 4376 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
b481de9c
ZY
4377#ifdef IEEE80211_CONF_CHANNEL_SWITCH
4378 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
4379#endif
4380 )
4381 priv->staging_rxon.flags = 0;
c8b0e6e1 4382#endif /* CONFIG_IWL4965_HT */
b481de9c 4383
c7de35cd 4384 iwl_set_rxon_channel(priv, conf->channel->band,
8318d78a 4385 ieee80211_frequency_to_channel(conf->channel->center_freq));
b481de9c 4386
8318d78a 4387 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
b481de9c
ZY
4388
4389 /* The list of supported rates and rate mask can be different
8318d78a 4390 * for each band; since the band may have changed, reset
b481de9c 4391 * the rate mask to what mac80211 lists */
bb8c093b 4392 iwl4965_set_rate(priv);
b481de9c
ZY
4393
4394 spin_unlock_irqrestore(&priv->lock, flags);
4395
4396#ifdef IEEE80211_CONF_CHANNEL_SWITCH
4397 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
bb8c093b 4398 iwl4965_hw_channel_switch(priv, conf->channel);
76bb77e0 4399 goto out;
b481de9c
ZY
4400 }
4401#endif
4402
ad97edd2
MA
4403 if (priv->cfg->ops->lib->radio_kill_sw)
4404 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
b481de9c
ZY
4405
4406 if (!conf->radio_enabled) {
4407 IWL_DEBUG_MAC80211("leave - radio disabled\n");
76bb77e0 4408 goto out;
b481de9c
ZY
4409 }
4410
fee1247a 4411 if (iwl_is_rfkill(priv)) {
b481de9c 4412 IWL_DEBUG_MAC80211("leave - RF kill\n");
76bb77e0
ZY
4413 ret = -EIO;
4414 goto out;
b481de9c
ZY
4415 }
4416
bb8c093b 4417 iwl4965_set_rate(priv);
b481de9c
ZY
4418
4419 if (memcmp(&priv->active_rxon,
4420 &priv->staging_rxon, sizeof(priv->staging_rxon)))
bb8c093b 4421 iwl4965_commit_rxon(priv);
b481de9c
ZY
4422 else
4423 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
4424
4425 IWL_DEBUG_MAC80211("leave\n");
4426
a0646470
ZY
4427out:
4428 clear_bit(STATUS_CONF_PENDING, &priv->status);
5a66926a 4429 mutex_unlock(&priv->mutex);
76bb77e0 4430 return ret;
b481de9c
ZY
4431}
4432
c79dd5b5 4433static void iwl4965_config_ap(struct iwl_priv *priv)
b481de9c 4434{
857485c0 4435 int ret = 0;
b481de9c 4436
d986bcd1 4437 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
b481de9c
ZY
4438 return;
4439
4440 /* The following should be done only at AP bring up */
4441 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
4442
4443 /* RXON - unassoc (to set timing command) */
4444 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
bb8c093b 4445 iwl4965_commit_rxon(priv);
b481de9c
ZY
4446
4447 /* RXON Timing */
bb8c093b
CH
4448 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
4449 iwl4965_setup_rxon_timing(priv);
857485c0 4450 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
b481de9c 4451 sizeof(priv->rxon_timing), &priv->rxon_timing);
857485c0 4452 if (ret)
b481de9c
ZY
4453 IWL_WARNING("REPLY_RXON_TIMING failed - "
4454 "Attempting to continue.\n");
4455
c7de35cd 4456 iwl_set_rxon_chain(priv);
b481de9c
ZY
4457
4458 /* FIXME: what should be the assoc_id for AP? */
4459 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
4460 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
4461 priv->staging_rxon.flags |=
4462 RXON_FLG_SHORT_PREAMBLE_MSK;
4463 else
4464 priv->staging_rxon.flags &=
4465 ~RXON_FLG_SHORT_PREAMBLE_MSK;
4466
4467 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
4468 if (priv->assoc_capability &
4469 WLAN_CAPABILITY_SHORT_SLOT_TIME)
4470 priv->staging_rxon.flags |=
4471 RXON_FLG_SHORT_SLOT_MSK;
4472 else
4473 priv->staging_rxon.flags &=
4474 ~RXON_FLG_SHORT_SLOT_MSK;
4475
4476 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4477 priv->staging_rxon.flags &=
4478 ~RXON_FLG_SHORT_SLOT_MSK;
4479 }
4480 /* restore RXON assoc */
4481 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
bb8c093b 4482 iwl4965_commit_rxon(priv);
bb8c093b 4483 iwl4965_activate_qos(priv, 1);
4f40e4d9 4484 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
e1493deb 4485 }
bb8c093b 4486 iwl4965_send_beacon_cmd(priv);
b481de9c
ZY
4487
4488 /* FIXME - we need to add code here to detect a totally new
4489 * configuration, reset the AP, unassoc, rxon timing, assoc,
4490 * clear sta table, add BCAST sta... */
4491}
4492
32bfd35d
JB
4493static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
4494 struct ieee80211_vif *vif,
b481de9c
ZY
4495 struct ieee80211_if_conf *conf)
4496{
c79dd5b5 4497 struct iwl_priv *priv = hw->priv;
0795af57 4498 DECLARE_MAC_BUF(mac);
b481de9c
ZY
4499 unsigned long flags;
4500 int rc;
4501
4502 if (conf == NULL)
4503 return -EIO;
4504
b716bb91
EG
4505 if (priv->vif != vif) {
4506 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
b716bb91
EG
4507 return 0;
4508 }
4509
b481de9c
ZY
4510 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
4511 (!conf->beacon || !conf->ssid_len)) {
4512 IWL_DEBUG_MAC80211
4513 ("Leaving in AP mode because HostAPD is not ready.\n");
4514 return 0;
4515 }
4516
fee1247a 4517 if (!iwl_is_alive(priv))
5a66926a
ZY
4518 return -EAGAIN;
4519
b481de9c
ZY
4520 mutex_lock(&priv->mutex);
4521
b481de9c 4522 if (conf->bssid)
0795af57
JP
4523 IWL_DEBUG_MAC80211("bssid: %s\n",
4524 print_mac(mac, conf->bssid));
b481de9c 4525
4150c572
JB
4526/*
4527 * very dubious code was here; the probe filtering flag is never set:
4528 *
b481de9c
ZY
4529 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
4530 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
4150c572 4531 */
b481de9c
ZY
4532
4533 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
4534 if (!conf->bssid) {
4535 conf->bssid = priv->mac_addr;
4536 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
0795af57
JP
4537 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
4538 print_mac(mac, conf->bssid));
b481de9c
ZY
4539 }
4540 if (priv->ibss_beacon)
4541 dev_kfree_skb(priv->ibss_beacon);
4542
4543 priv->ibss_beacon = conf->beacon;
4544 }
4545
fee1247a 4546 if (iwl_is_rfkill(priv))
fde3571f
MA
4547 goto done;
4548
b481de9c
ZY
4549 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
4550 !is_multicast_ether_addr(conf->bssid)) {
4551 /* If there is currently a HW scan going on in the background
4552 * then we need to cancel it else the RXON below will fail. */
bb8c093b 4553 if (iwl4965_scan_cancel_timeout(priv, 100)) {
b481de9c
ZY
4554 IWL_WARNING("Aborted scan still in progress "
4555 "after 100ms\n");
4556 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
4557 mutex_unlock(&priv->mutex);
4558 return -EAGAIN;
4559 }
4560 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
4561
4562 /* TODO: Audit driver for usage of these members and see
4563 * if mac80211 deprecates them (priv->bssid looks like it
4564 * shouldn't be there, but I haven't scanned the IBSS code
4565 * to verify) - jpk */
4566 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
4567
4568 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
bb8c093b 4569 iwl4965_config_ap(priv);
b481de9c 4570 else {
bb8c093b 4571 rc = iwl4965_commit_rxon(priv);
b481de9c 4572 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
4f40e4d9 4573 iwl_rxon_add_station(
b481de9c
ZY
4574 priv, priv->active_rxon.bssid_addr, 1);
4575 }
4576
4577 } else {
bb8c093b 4578 iwl4965_scan_cancel_timeout(priv, 100);
b481de9c 4579 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
bb8c093b 4580 iwl4965_commit_rxon(priv);
b481de9c
ZY
4581 }
4582
fde3571f 4583 done:
b481de9c
ZY
4584 spin_lock_irqsave(&priv->lock, flags);
4585 if (!conf->ssid_len)
4586 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
4587 else
4588 memcpy(priv->essid, conf->ssid, conf->ssid_len);
4589
4590 priv->essid_len = conf->ssid_len;
4591 spin_unlock_irqrestore(&priv->lock, flags);
4592
4593 IWL_DEBUG_MAC80211("leave\n");
4594 mutex_unlock(&priv->mutex);
4595
4596 return 0;
4597}
4598
bb8c093b 4599static void iwl4965_configure_filter(struct ieee80211_hw *hw,
4150c572
JB
4600 unsigned int changed_flags,
4601 unsigned int *total_flags,
4602 int mc_count, struct dev_addr_list *mc_list)
4603{
4604 /*
4605 * XXX: dummy
bb8c093b 4606 * see also iwl4965_connection_init_rx_config
4150c572 4607 */
4419e39b
AK
4608 struct iwl_priv *priv = hw->priv;
4609 int new_flags = 0;
4610 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
4611 if (*total_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
4612 IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
4613 IEEE80211_IF_TYPE_MNTR,
4614 changed_flags, *total_flags);
4615 /* queue work 'cuz mac80211 is holding a lock which
4616 * prevents us from issuing (synchronous) f/w cmds */
4617 queue_work(priv->workqueue, &priv->set_monitor);
4618 new_flags &= FIF_PROMISC_IN_BSS |
4619 FIF_OTHER_BSS |
4620 FIF_ALLMULTI;
4621 }
4622 }
4623 *total_flags = new_flags;
4150c572
JB
4624}
4625
bb8c093b 4626static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
b481de9c
ZY
4627 struct ieee80211_if_init_conf *conf)
4628{
c79dd5b5 4629 struct iwl_priv *priv = hw->priv;
b481de9c
ZY
4630
4631 IWL_DEBUG_MAC80211("enter\n");
4632
4633 mutex_lock(&priv->mutex);
948c171c 4634
fee1247a 4635 if (iwl_is_ready_rf(priv)) {
fde3571f
MA
4636 iwl4965_scan_cancel_timeout(priv, 100);
4637 cancel_delayed_work(&priv->post_associate);
4638 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4639 iwl4965_commit_rxon(priv);
4640 }
32bfd35d
JB
4641 if (priv->vif == conf->vif) {
4642 priv->vif = NULL;
b481de9c
ZY
4643 memset(priv->bssid, 0, ETH_ALEN);
4644 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
4645 priv->essid_len = 0;
4646 }
4647 mutex_unlock(&priv->mutex);
4648
4649 IWL_DEBUG_MAC80211("leave\n");
4650
4651}
471b3efd 4652
3109ece1 4653#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
471b3efd
JB
4654static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
4655 struct ieee80211_vif *vif,
4656 struct ieee80211_bss_conf *bss_conf,
4657 u32 changes)
220173b0 4658{
c79dd5b5 4659 struct iwl_priv *priv = hw->priv;
220173b0 4660
3109ece1
TW
4661 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
4662
471b3efd 4663 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
3109ece1
TW
4664 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
4665 bss_conf->use_short_preamble);
471b3efd 4666 if (bss_conf->use_short_preamble)
220173b0
TW
4667 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
4668 else
4669 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
4670 }
4671
471b3efd 4672 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
3109ece1 4673 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
8318d78a 4674 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
220173b0
TW
4675 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
4676 else
4677 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
4678 }
4679
98952d5d 4680 if (changes & BSS_CHANGED_HT) {
3109ece1 4681 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
98952d5d 4682 iwl4965_ht_conf(priv, bss_conf);
c7de35cd 4683 iwl_set_rxon_chain(priv);
98952d5d
TW
4684 }
4685
471b3efd 4686 if (changes & BSS_CHANGED_ASSOC) {
3109ece1 4687 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
508e32e1
RC
4688 /* This should never happen as this function should
4689 * never be called from interrupt context. */
4690 if (WARN_ON_ONCE(in_interrupt()))
4691 return;
3109ece1
TW
4692 if (bss_conf->assoc) {
4693 priv->assoc_id = bss_conf->aid;
4694 priv->beacon_int = bss_conf->beacon_int;
4695 priv->timestamp = bss_conf->timestamp;
4696 priv->assoc_capability = bss_conf->assoc_capability;
4697 priv->next_scan_jiffies = jiffies +
4698 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
508e32e1
RC
4699 mutex_lock(&priv->mutex);
4700 iwl4965_post_associate(priv);
4701 mutex_unlock(&priv->mutex);
3109ece1
TW
4702 } else {
4703 priv->assoc_id = 0;
4704 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
4705 }
4706 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
4707 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
7e8c519e 4708 iwl_send_rxon_assoc(priv);
471b3efd
JB
4709 }
4710
220173b0 4711}
b481de9c 4712
bb8c093b 4713static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
b481de9c
ZY
4714{
4715 int rc = 0;
4716 unsigned long flags;
c79dd5b5 4717 struct iwl_priv *priv = hw->priv;
b481de9c
ZY
4718
4719 IWL_DEBUG_MAC80211("enter\n");
4720
052c4b9f 4721 mutex_lock(&priv->mutex);
b481de9c
ZY
4722 spin_lock_irqsave(&priv->lock, flags);
4723
fee1247a 4724 if (!iwl_is_ready_rf(priv)) {
b481de9c
ZY
4725 rc = -EIO;
4726 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
4727 goto out_unlock;
4728 }
4729
4730 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
4731 rc = -EIO;
4732 IWL_ERROR("ERROR: APs don't scan\n");
4733 goto out_unlock;
4734 }
4735
7878a5a4
MA
4736 /* we don't schedule scan within next_scan_jiffies period */
4737 if (priv->next_scan_jiffies &&
4738 time_after(priv->next_scan_jiffies, jiffies)) {
4739 rc = -EAGAIN;
4740 goto out_unlock;
4741 }
b481de9c 4742 /* if we just finished scan ask for delay */
7878a5a4
MA
4743 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
4744 IWL_DELAY_NEXT_SCAN, jiffies)) {
b481de9c
ZY
4745 rc = -EAGAIN;
4746 goto out_unlock;
4747 }
4748 if (len) {
7878a5a4 4749 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
bb8c093b 4750 iwl4965_escape_essid(ssid, len), (int)len);
b481de9c
ZY
4751
4752 priv->one_direct_scan = 1;
4753 priv->direct_ssid_len = (u8)
4754 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
4755 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
948c171c
MA
4756 } else
4757 priv->one_direct_scan = 0;
b481de9c 4758
bb8c093b 4759 rc = iwl4965_scan_initiate(priv);
b481de9c
ZY
4760
4761 IWL_DEBUG_MAC80211("leave\n");
4762
4763out_unlock:
4764 spin_unlock_irqrestore(&priv->lock, flags);
052c4b9f 4765 mutex_unlock(&priv->mutex);
b481de9c
ZY
4766
4767 return rc;
4768}
4769
ab885f8c
EG
4770static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
4771 struct ieee80211_key_conf *keyconf, const u8 *addr,
4772 u32 iv32, u16 *phase1key)
4773{
4774 struct iwl_priv *priv = hw->priv;
4775 u8 sta_id = IWL_INVALID_STATION;
4776 unsigned long flags;
4777 __le16 key_flags = 0;
4778 int i;
4779 DECLARE_MAC_BUF(mac);
4780
4781 IWL_DEBUG_MAC80211("enter\n");
4782
947b13a7 4783 sta_id = iwl_find_station(priv, addr);
ab885f8c
EG
4784 if (sta_id == IWL_INVALID_STATION) {
4785 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
4786 print_mac(mac, addr));
4787 return;
4788 }
4789
4790 iwl4965_scan_cancel_timeout(priv, 100);
4791
4792 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
4793 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
4794 key_flags &= ~STA_KEY_FLG_INVALID;
4795
5425e490 4796 if (sta_id == priv->hw_params.bcast_sta_id)
ab885f8c
EG
4797 key_flags |= STA_KEY_MULTICAST_MSK;
4798
4799 spin_lock_irqsave(&priv->sta_lock, flags);
4800
ab885f8c
EG
4801 priv->stations[sta_id].sta.key.key_flags = key_flags;
4802 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
4803
4804 for (i = 0; i < 5; i++)
4805 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
4806 cpu_to_le16(phase1key[i]);
4807
4808 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
4809 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
4810
133636de 4811 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
ab885f8c
EG
4812
4813 spin_unlock_irqrestore(&priv->sta_lock, flags);
4814
4815 IWL_DEBUG_MAC80211("leave\n");
4816}
4817
bb8c093b 4818static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
b481de9c
ZY
4819 const u8 *local_addr, const u8 *addr,
4820 struct ieee80211_key_conf *key)
4821{
c79dd5b5 4822 struct iwl_priv *priv = hw->priv;
0795af57 4823 DECLARE_MAC_BUF(mac);
deb09c43
EG
4824 int ret = 0;
4825 u8 sta_id = IWL_INVALID_STATION;
6974e363 4826 u8 is_default_wep_key = 0;
b481de9c
ZY
4827
4828 IWL_DEBUG_MAC80211("enter\n");
4829
099b40b7 4830 if (priv->hw_params.sw_crypto) {
b481de9c
ZY
4831 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
4832 return -EOPNOTSUPP;
4833 }
4834
4835 if (is_zero_ether_addr(addr))
4836 /* only support pairwise keys */
4837 return -EOPNOTSUPP;
4838
947b13a7 4839 sta_id = iwl_find_station(priv, addr);
6974e363
EG
4840 if (sta_id == IWL_INVALID_STATION) {
4841 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
4842 print_mac(mac, addr));
4843 return -EINVAL;
b481de9c 4844
deb09c43 4845 }
b481de9c 4846
6974e363 4847 mutex_lock(&priv->mutex);
bb8c093b 4848 iwl4965_scan_cancel_timeout(priv, 100);
6974e363
EG
4849 mutex_unlock(&priv->mutex);
4850
4851 /* If we are getting WEP group key and we didn't receive any key mapping
4852 * so far, we are in legacy wep mode (group key only), otherwise we are
4853 * in 1X mode.
4854 * In legacy wep mode, we use another host command to the uCode */
5425e490 4855 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
6974e363
EG
4856 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
4857 if (cmd == SET_KEY)
4858 is_default_wep_key = !priv->key_mapping_key;
4859 else
ccc038ab
EG
4860 is_default_wep_key =
4861 (key->hw_key_idx == HW_KEY_DEFAULT);
6974e363 4862 }
052c4b9f 4863
b481de9c 4864 switch (cmd) {
deb09c43 4865 case SET_KEY:
6974e363
EG
4866 if (is_default_wep_key)
4867 ret = iwl_set_default_wep_key(priv, key);
deb09c43 4868 else
7480513f 4869 ret = iwl_set_dynamic_key(priv, key, sta_id);
deb09c43
EG
4870
4871 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
b481de9c
ZY
4872 break;
4873 case DISABLE_KEY:
6974e363
EG
4874 if (is_default_wep_key)
4875 ret = iwl_remove_default_wep_key(priv, key);
deb09c43 4876 else
3ec47732 4877 ret = iwl_remove_dynamic_key(priv, key, sta_id);
deb09c43
EG
4878
4879 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
b481de9c
ZY
4880 break;
4881 default:
deb09c43 4882 ret = -EINVAL;
b481de9c
ZY
4883 }
4884
4885 IWL_DEBUG_MAC80211("leave\n");
b481de9c 4886
deb09c43 4887 return ret;
b481de9c
ZY
4888}
4889
e100bb64 4890static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
b481de9c
ZY
4891 const struct ieee80211_tx_queue_params *params)
4892{
c79dd5b5 4893 struct iwl_priv *priv = hw->priv;
b481de9c
ZY
4894 unsigned long flags;
4895 int q;
b481de9c
ZY
4896
4897 IWL_DEBUG_MAC80211("enter\n");
4898
fee1247a 4899 if (!iwl_is_ready_rf(priv)) {
b481de9c
ZY
4900 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4901 return -EIO;
4902 }
4903
4904 if (queue >= AC_NUM) {
4905 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
4906 return 0;
4907 }
4908
b481de9c
ZY
4909 if (!priv->qos_data.qos_enable) {
4910 priv->qos_data.qos_active = 0;
4911 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
4912 return 0;
4913 }
4914 q = AC_NUM - 1 - queue;
4915
4916 spin_lock_irqsave(&priv->lock, flags);
4917
4918 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
4919 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
4920 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
4921 priv->qos_data.def_qos_parm.ac[q].edca_txop =
3330d7be 4922 cpu_to_le16((params->txop * 32));
b481de9c
ZY
4923
4924 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
4925 priv->qos_data.qos_active = 1;
4926
4927 spin_unlock_irqrestore(&priv->lock, flags);
4928
4929 mutex_lock(&priv->mutex);
4930 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
bb8c093b 4931 iwl4965_activate_qos(priv, 1);
3109ece1 4932 else if (priv->assoc_id && iwl_is_associated(priv))
bb8c093b 4933 iwl4965_activate_qos(priv, 0);
b481de9c
ZY
4934
4935 mutex_unlock(&priv->mutex);
4936
b481de9c
ZY
4937 IWL_DEBUG_MAC80211("leave\n");
4938 return 0;
4939}
4940
bb8c093b 4941static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
b481de9c
ZY
4942 struct ieee80211_tx_queue_stats *stats)
4943{
c79dd5b5 4944 struct iwl_priv *priv = hw->priv;
b481de9c 4945 int i, avail;
16466903 4946 struct iwl_tx_queue *txq;
443cfd45 4947 struct iwl_queue *q;
b481de9c
ZY
4948 unsigned long flags;
4949
4950 IWL_DEBUG_MAC80211("enter\n");
4951
fee1247a 4952 if (!iwl_is_ready_rf(priv)) {
b481de9c
ZY
4953 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4954 return -EIO;
4955 }
4956
4957 spin_lock_irqsave(&priv->lock, flags);
4958
4959 for (i = 0; i < AC_NUM; i++) {
4960 txq = &priv->txq[i];
4961 q = &txq->q;
443cfd45 4962 avail = iwl_queue_space(q);
b481de9c 4963
57ffc589
JB
4964 stats[i].len = q->n_window - avail;
4965 stats[i].limit = q->n_window - q->high_mark;
4966 stats[i].count = q->n_window;
b481de9c
ZY
4967
4968 }
4969 spin_unlock_irqrestore(&priv->lock, flags);
4970
4971 IWL_DEBUG_MAC80211("leave\n");
4972
4973 return 0;
4974}
4975
bb8c093b 4976static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
b481de9c
ZY
4977 struct ieee80211_low_level_stats *stats)
4978{
bf403db8
EK
4979 struct iwl_priv *priv = hw->priv;
4980
4981 priv = hw->priv;
b481de9c
ZY
4982 IWL_DEBUG_MAC80211("enter\n");
4983 IWL_DEBUG_MAC80211("leave\n");
4984
4985 return 0;
4986}
4987
bb8c093b 4988static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
b481de9c 4989{
bf403db8
EK
4990 struct iwl_priv *priv;
4991
4992 priv = hw->priv;
b481de9c
ZY
4993 IWL_DEBUG_MAC80211("enter\n");
4994 IWL_DEBUG_MAC80211("leave\n");
4995
4996 return 0;
4997}
4998
bb8c093b 4999static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
b481de9c 5000{
c79dd5b5 5001 struct iwl_priv *priv = hw->priv;
b481de9c
ZY
5002 unsigned long flags;
5003
5004 mutex_lock(&priv->mutex);
5005 IWL_DEBUG_MAC80211("enter\n");
5006
5007 priv->lq_mngr.lq_ready = 0;
c8b0e6e1 5008#ifdef CONFIG_IWL4965_HT
b481de9c 5009 spin_lock_irqsave(&priv->lock, flags);
fd105e79 5010 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
b481de9c 5011 spin_unlock_irqrestore(&priv->lock, flags);
c8b0e6e1 5012#endif /* CONFIG_IWL4965_HT */
b481de9c 5013
c7de35cd 5014 iwl_reset_qos(priv);
b481de9c
ZY
5015
5016 cancel_delayed_work(&priv->post_associate);
5017
5018 spin_lock_irqsave(&priv->lock, flags);
5019 priv->assoc_id = 0;
5020 priv->assoc_capability = 0;
b481de9c
ZY
5021 priv->assoc_station_added = 0;
5022
5023 /* new association get rid of ibss beacon skb */
5024 if (priv->ibss_beacon)
5025 dev_kfree_skb(priv->ibss_beacon);
5026
5027 priv->ibss_beacon = NULL;
5028
5029 priv->beacon_int = priv->hw->conf.beacon_int;
3109ece1 5030 priv->timestamp = 0;
b481de9c
ZY
5031 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
5032 priv->beacon_int = 0;
5033
5034 spin_unlock_irqrestore(&priv->lock, flags);
5035
fee1247a 5036 if (!iwl_is_ready_rf(priv)) {
fde3571f
MA
5037 IWL_DEBUG_MAC80211("leave - not ready\n");
5038 mutex_unlock(&priv->mutex);
5039 return;
5040 }
5041
052c4b9f 5042 /* we are restarting association process
5043 * clear RXON_FILTER_ASSOC_MSK bit
5044 */
5045 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
bb8c093b 5046 iwl4965_scan_cancel_timeout(priv, 100);
052c4b9f 5047 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
bb8c093b 5048 iwl4965_commit_rxon(priv);
052c4b9f 5049 }
5050
5da4b55f
MA
5051 iwl_power_update_mode(priv, 0);
5052
b481de9c
ZY
5053 /* Per mac80211.h: This is only used in IBSS mode... */
5054 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
052c4b9f 5055
b481de9c
ZY
5056 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
5057 mutex_unlock(&priv->mutex);
5058 return;
5059 }
5060
bb8c093b 5061 iwl4965_set_rate(priv);
b481de9c
ZY
5062
5063 mutex_unlock(&priv->mutex);
5064
5065 IWL_DEBUG_MAC80211("leave\n");
b481de9c
ZY
5066}
5067
bb8c093b 5068static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
b481de9c
ZY
5069 struct ieee80211_tx_control *control)
5070{
c79dd5b5 5071 struct iwl_priv *priv = hw->priv;
b481de9c
ZY
5072 unsigned long flags;
5073
5074 mutex_lock(&priv->mutex);
5075 IWL_DEBUG_MAC80211("enter\n");
5076
fee1247a 5077 if (!iwl_is_ready_rf(priv)) {
b481de9c
ZY
5078 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5079 mutex_unlock(&priv->mutex);
5080 return -EIO;
5081 }
5082
5083 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
5084 IWL_DEBUG_MAC80211("leave - not IBSS\n");
5085 mutex_unlock(&priv->mutex);
5086 return -EIO;
5087 }
5088
5089 spin_lock_irqsave(&priv->lock, flags);
5090
5091 if (priv->ibss_beacon)
5092 dev_kfree_skb(priv->ibss_beacon);
5093
5094 priv->ibss_beacon = skb;
5095
5096 priv->assoc_id = 0;
5097
5098 IWL_DEBUG_MAC80211("leave\n");
5099 spin_unlock_irqrestore(&priv->lock, flags);
5100
c7de35cd 5101 iwl_reset_qos(priv);
b481de9c
ZY
5102
5103 queue_work(priv->workqueue, &priv->post_associate.work);
5104
5105 mutex_unlock(&priv->mutex);
5106
5107 return 0;
5108}
5109
b481de9c
ZY
5110/*****************************************************************************
5111 *
5112 * sysfs attributes
5113 *
5114 *****************************************************************************/
5115
0a6857e7 5116#ifdef CONFIG_IWLWIFI_DEBUG
b481de9c
ZY
5117
5118/*
5119 * The following adds a new attribute to the sysfs representation
5120 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
5121 * used for controlling the debug level.
5122 *
5123 * See the level definitions in iwl for details.
5124 */
5125
8cf769c6
EK
5126static ssize_t show_debug_level(struct device *d,
5127 struct device_attribute *attr, char *buf)
b481de9c 5128{
8cf769c6
EK
5129 struct iwl_priv *priv = d->driver_data;
5130
5131 return sprintf(buf, "0x%08X\n", priv->debug_level);
b481de9c 5132}
8cf769c6
EK
5133static ssize_t store_debug_level(struct device *d,
5134 struct device_attribute *attr,
b481de9c
ZY
5135 const char *buf, size_t count)
5136{
8cf769c6 5137 struct iwl_priv *priv = d->driver_data;
b481de9c
ZY
5138 char *p = (char *)buf;
5139 u32 val;
5140
5141 val = simple_strtoul(p, &p, 0);
5142 if (p == buf)
5143 printk(KERN_INFO DRV_NAME
5144 ": %s is not in hex or decimal form.\n", buf);
5145 else
8cf769c6 5146 priv->debug_level = val;
b481de9c
ZY
5147
5148 return strnlen(buf, count);
5149}
5150
8cf769c6
EK
5151static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
5152 show_debug_level, store_debug_level);
5153
b481de9c 5154
0a6857e7 5155#endif /* CONFIG_IWLWIFI_DEBUG */
b481de9c 5156
b481de9c 5157
bc6f59bc
TW
5158static ssize_t show_version(struct device *d,
5159 struct device_attribute *attr, char *buf)
5160{
5161 struct iwl_priv *priv = d->driver_data;
5162 struct iwl4965_alive_resp *palive = &priv->card_alive;
5163
5164 if (palive->is_valid)
5165 return sprintf(buf, "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
5166 "fw type: 0x%01X 0x%01X\n",
5167 palive->ucode_major, palive->ucode_minor,
5168 palive->sw_rev[0], palive->sw_rev[1],
5169 palive->ver_type, palive->ver_subtype);
5170
5171 else
5172 return sprintf(buf, "fw not loaded\n");
5173}
5174
5175static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
5176
b481de9c
ZY
5177static ssize_t show_temperature(struct device *d,
5178 struct device_attribute *attr, char *buf)
5179{
c79dd5b5 5180 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
b481de9c 5181
fee1247a 5182 if (!iwl_is_alive(priv))
b481de9c
ZY
5183 return -EAGAIN;
5184
bb8c093b 5185 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
b481de9c
ZY
5186}
5187
5188static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
5189
5190static ssize_t show_rs_window(struct device *d,
5191 struct device_attribute *attr,
5192 char *buf)
5193{
c79dd5b5 5194 struct iwl_priv *priv = d->driver_data;
bb8c093b 5195 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
b481de9c
ZY
5196}
5197static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
5198
5199static ssize_t show_tx_power(struct device *d,
5200 struct device_attribute *attr, char *buf)
5201{
c79dd5b5 5202 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
b481de9c
ZY
5203 return sprintf(buf, "%d\n", priv->user_txpower_limit);
5204}
5205
5206static ssize_t store_tx_power(struct device *d,
5207 struct device_attribute *attr,
5208 const char *buf, size_t count)
5209{
c79dd5b5 5210 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
b481de9c
ZY
5211 char *p = (char *)buf;
5212 u32 val;
5213
5214 val = simple_strtoul(p, &p, 10);
5215 if (p == buf)
5216 printk(KERN_INFO DRV_NAME
5217 ": %s is not in decimal form.\n", buf);
5218 else
bb8c093b 5219 iwl4965_hw_reg_set_txpower(priv, val);
b481de9c
ZY
5220
5221 return count;
5222}
5223
5224static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
5225
5226static ssize_t show_flags(struct device *d,
5227 struct device_attribute *attr, char *buf)
5228{
c79dd5b5 5229 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
b481de9c
ZY
5230
5231 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
5232}
5233
5234static ssize_t store_flags(struct device *d,
5235 struct device_attribute *attr,
5236 const char *buf, size_t count)
5237{
c79dd5b5 5238 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
b481de9c
ZY
5239 u32 flags = simple_strtoul(buf, NULL, 0);
5240
5241 mutex_lock(&priv->mutex);
5242 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
5243 /* Cancel any currently running scans... */
bb8c093b 5244 if (iwl4965_scan_cancel_timeout(priv, 100))
b481de9c
ZY
5245 IWL_WARNING("Could not cancel scan.\n");
5246 else {
5247 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
5248 flags);
5249 priv->staging_rxon.flags = cpu_to_le32(flags);
bb8c093b 5250 iwl4965_commit_rxon(priv);
b481de9c
ZY
5251 }
5252 }
5253 mutex_unlock(&priv->mutex);
5254
5255 return count;
5256}
5257
5258static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
5259
5260static ssize_t show_filter_flags(struct device *d,
5261 struct device_attribute *attr, char *buf)
5262{
c79dd5b5 5263 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
b481de9c
ZY
5264
5265 return sprintf(buf, "0x%04X\n",
5266 le32_to_cpu(priv->active_rxon.filter_flags));
5267}
5268
5269static ssize_t store_filter_flags(struct device *d,
5270 struct device_attribute *attr,
5271 const char *buf, size_t count)
5272{
c79dd5b5 5273 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
b481de9c
ZY
5274 u32 filter_flags = simple_strtoul(buf, NULL, 0);
5275
5276 mutex_lock(&priv->mutex);
5277 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
5278 /* Cancel any currently running scans... */
bb8c093b 5279 if (iwl4965_scan_cancel_timeout(priv, 100))
b481de9c
ZY
5280 IWL_WARNING("Could not cancel scan.\n");
5281 else {
5282 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
5283 "0x%04X\n", filter_flags);
5284 priv->staging_rxon.filter_flags =
5285 cpu_to_le32(filter_flags);
bb8c093b 5286 iwl4965_commit_rxon(priv);
b481de9c
ZY
5287 }
5288 }
5289 mutex_unlock(&priv->mutex);
5290
5291 return count;
5292}
5293
5294static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
5295 store_filter_flags);
5296
c8b0e6e1 5297#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
b481de9c
ZY
5298
5299static ssize_t show_measurement(struct device *d,
5300 struct device_attribute *attr, char *buf)
5301{
c79dd5b5 5302 struct iwl_priv *priv = dev_get_drvdata(d);
bb8c093b 5303 struct iwl4965_spectrum_notification measure_report;
b481de9c
ZY
5304 u32 size = sizeof(measure_report), len = 0, ofs = 0;
5305 u8 *data = (u8 *) & measure_report;
5306 unsigned long flags;
5307
5308 spin_lock_irqsave(&priv->lock, flags);
5309 if (!(priv->measurement_status & MEASUREMENT_READY)) {
5310 spin_unlock_irqrestore(&priv->lock, flags);
5311 return 0;
5312 }
5313 memcpy(&measure_report, &priv->measure_report, size);
5314 priv->measurement_status = 0;
5315 spin_unlock_irqrestore(&priv->lock, flags);
5316
5317 while (size && (PAGE_SIZE - len)) {
5318 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
5319 PAGE_SIZE - len, 1);
5320 len = strlen(buf);
5321 if (PAGE_SIZE - len)
5322 buf[len++] = '\n';
5323
5324 ofs += 16;
5325 size -= min(size, 16U);
5326 }
5327
5328 return len;
5329}
5330
5331static ssize_t store_measurement(struct device *d,
5332 struct device_attribute *attr,
5333 const char *buf, size_t count)
5334{
c79dd5b5 5335 struct iwl_priv *priv = dev_get_drvdata(d);
b481de9c
ZY
5336 struct ieee80211_measurement_params params = {
5337 .channel = le16_to_cpu(priv->active_rxon.channel),
5338 .start_time = cpu_to_le64(priv->last_tsf),
5339 .duration = cpu_to_le16(1),
5340 };
5341 u8 type = IWL_MEASURE_BASIC;
5342 u8 buffer[32];
5343 u8 channel;
5344
5345 if (count) {
5346 char *p = buffer;
5347 strncpy(buffer, buf, min(sizeof(buffer), count));
5348 channel = simple_strtoul(p, NULL, 0);
5349 if (channel)
5350 params.channel = channel;
5351
5352 p = buffer;
5353 while (*p && *p != ' ')
5354 p++;
5355 if (*p)
5356 type = simple_strtoul(p + 1, NULL, 0);
5357 }
5358
5359 IWL_DEBUG_INFO("Invoking measurement of type %d on "
5360 "channel %d (for '%s')\n", type, params.channel, buf);
bb8c093b 5361 iwl4965_get_measurement(priv, &params, type);
b481de9c
ZY
5362
5363 return count;
5364}
5365
5366static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
5367 show_measurement, store_measurement);
c8b0e6e1 5368#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
b481de9c
ZY
5369
5370static ssize_t store_retry_rate(struct device *d,
5371 struct device_attribute *attr,
5372 const char *buf, size_t count)
5373{
c79dd5b5 5374 struct iwl_priv *priv = dev_get_drvdata(d);
b481de9c
ZY
5375
5376 priv->retry_rate = simple_strtoul(buf, NULL, 0);
5377 if (priv->retry_rate <= 0)
5378 priv->retry_rate = 1;
5379
5380 return count;
5381}
5382
5383static ssize_t show_retry_rate(struct device *d,
5384 struct device_attribute *attr, char *buf)
5385{
c79dd5b5 5386 struct iwl_priv *priv = dev_get_drvdata(d);
b481de9c
ZY
5387 return sprintf(buf, "%d", priv->retry_rate);
5388}
5389
5390static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
5391 store_retry_rate);
5392
5393static ssize_t store_power_level(struct device *d,
5394 struct device_attribute *attr,
5395 const char *buf, size_t count)
5396{
c79dd5b5 5397 struct iwl_priv *priv = dev_get_drvdata(d);
b481de9c
ZY
5398 int rc;
5399 int mode;
5400
5401 mode = simple_strtoul(buf, NULL, 0);
5402 mutex_lock(&priv->mutex);
5403
fee1247a 5404 if (!iwl_is_ready(priv)) {
b481de9c
ZY
5405 rc = -EAGAIN;
5406 goto out;
5407 }
5408
5da4b55f
MA
5409 rc = iwl_power_set_user_mode(priv, mode);
5410 if (rc) {
5411 IWL_DEBUG_MAC80211("failed setting power mode.\n");
5412 goto out;
b481de9c 5413 }
b481de9c
ZY
5414 rc = count;
5415
5416 out:
5417 mutex_unlock(&priv->mutex);
5418 return rc;
5419}
5420
5421#define MAX_WX_STRING 80
5422
5423/* Values are in microsecond */
5424static const s32 timeout_duration[] = {
5425 350000,
5426 250000,
5427 75000,
5428 37000,
5429 25000,
5430};
5431static const s32 period_duration[] = {
5432 400000,
5433 700000,
5434 1000000,
5435 1000000,
5436 1000000
5437};
5438
5439static ssize_t show_power_level(struct device *d,
5440 struct device_attribute *attr, char *buf)
5441{
c79dd5b5 5442 struct iwl_priv *priv = dev_get_drvdata(d);
5da4b55f 5443 int level = priv->power_data.power_mode;
b481de9c
ZY
5444 char *p = buf;
5445
5446 p += sprintf(p, "%d ", level);
5447 switch (level) {
5448 case IWL_POWER_MODE_CAM:
5449 case IWL_POWER_AC:
5450 p += sprintf(p, "(AC)");
5451 break;
5452 case IWL_POWER_BATTERY:
5453 p += sprintf(p, "(BATTERY)");
5454 break;
5455 default:
5456 p += sprintf(p,
5457 "(Timeout %dms, Period %dms)",
5458 timeout_duration[level - 1] / 1000,
5459 period_duration[level - 1] / 1000);
5460 }
5da4b55f 5461/*
b481de9c
ZY
5462 if (!(priv->power_mode & IWL_POWER_ENABLED))
5463 p += sprintf(p, " OFF\n");
5464 else
5465 p += sprintf(p, " \n");
5da4b55f
MA
5466*/
5467 p += sprintf(p, " \n");
b481de9c 5468 return (p - buf + 1);
b481de9c
ZY
5469}
5470
5471static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
5472 store_power_level);
5473
5474static ssize_t show_channels(struct device *d,
5475 struct device_attribute *attr, char *buf)
5476{
8318d78a
JB
5477 /* all this shit doesn't belong into sysfs anyway */
5478 return 0;
b481de9c
ZY
5479}
5480
5481static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
5482
5483static ssize_t show_statistics(struct device *d,
5484 struct device_attribute *attr, char *buf)
5485{
c79dd5b5 5486 struct iwl_priv *priv = dev_get_drvdata(d);
bb8c093b 5487 u32 size = sizeof(struct iwl4965_notif_statistics);
b481de9c
ZY
5488 u32 len = 0, ofs = 0;
5489 u8 *data = (u8 *) & priv->statistics;
5490 int rc = 0;
5491
fee1247a 5492 if (!iwl_is_alive(priv))
b481de9c
ZY
5493 return -EAGAIN;
5494
5495 mutex_lock(&priv->mutex);
49ea8596 5496 rc = iwl_send_statistics_request(priv, 0);
b481de9c
ZY
5497 mutex_unlock(&priv->mutex);
5498
5499 if (rc) {
5500 len = sprintf(buf,
5501 "Error sending statistics request: 0x%08X\n", rc);
5502 return len;
5503 }
5504
5505 while (size && (PAGE_SIZE - len)) {
5506 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
5507 PAGE_SIZE - len, 1);
5508 len = strlen(buf);
5509 if (PAGE_SIZE - len)
5510 buf[len++] = '\n';
5511
5512 ofs += 16;
5513 size -= min(size, 16U);
5514 }
5515
5516 return len;
5517}
5518
5519static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
5520
b481de9c
ZY
5521static ssize_t show_status(struct device *d,
5522 struct device_attribute *attr, char *buf)
5523{
c79dd5b5 5524 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
fee1247a 5525 if (!iwl_is_alive(priv))
b481de9c
ZY
5526 return -EAGAIN;
5527 return sprintf(buf, "0x%08x\n", (int)priv->status);
5528}
5529
5530static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
5531
5532static ssize_t dump_error_log(struct device *d,
5533 struct device_attribute *attr,
5534 const char *buf, size_t count)
5535{
5536 char *p = (char *)buf;
5537
5538 if (p[0] == '1')
c79dd5b5 5539 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
b481de9c
ZY
5540
5541 return strnlen(buf, count);
5542}
5543
5544static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
5545
b481de9c
ZY
5546/*****************************************************************************
5547 *
5548 * driver setup and teardown
5549 *
5550 *****************************************************************************/
5551
c79dd5b5 5552static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
b481de9c
ZY
5553{
5554 priv->workqueue = create_workqueue(DRV_NAME);
5555
5556 init_waitqueue_head(&priv->wait_command_queue);
5557
bb8c093b
CH
5558 INIT_WORK(&priv->up, iwl4965_bg_up);
5559 INIT_WORK(&priv->restart, iwl4965_bg_restart);
5560 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
5561 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
5562 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
5563 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
5564 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
5565 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
4419e39b 5566 INIT_WORK(&priv->set_monitor, iwl4965_bg_set_monitor);
bb8c093b
CH
5567 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
5568 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
5569 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
5570 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
5571
5572 iwl4965_hw_setup_deferred_work(priv);
b481de9c
ZY
5573
5574 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
bb8c093b 5575 iwl4965_irq_tasklet, (unsigned long)priv);
b481de9c
ZY
5576}
5577
c79dd5b5 5578static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
b481de9c 5579{
bb8c093b 5580 iwl4965_hw_cancel_deferred_work(priv);
b481de9c 5581
3ae6a054 5582 cancel_delayed_work_sync(&priv->init_alive_start);
b481de9c
ZY
5583 cancel_delayed_work(&priv->scan_check);
5584 cancel_delayed_work(&priv->alive_start);
5585 cancel_delayed_work(&priv->post_associate);
5586 cancel_work_sync(&priv->beacon_update);
5587}
5588
bb8c093b 5589static struct attribute *iwl4965_sysfs_entries[] = {
b481de9c
ZY
5590 &dev_attr_channels.attr,
5591 &dev_attr_dump_errors.attr,
b481de9c
ZY
5592 &dev_attr_flags.attr,
5593 &dev_attr_filter_flags.attr,
c8b0e6e1 5594#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
b481de9c
ZY
5595 &dev_attr_measurement.attr,
5596#endif
5597 &dev_attr_power_level.attr,
5598 &dev_attr_retry_rate.attr,
b481de9c
ZY
5599 &dev_attr_rs_window.attr,
5600 &dev_attr_statistics.attr,
5601 &dev_attr_status.attr,
5602 &dev_attr_temperature.attr,
b481de9c 5603 &dev_attr_tx_power.attr,
8cf769c6
EK
5604#ifdef CONFIG_IWLWIFI_DEBUG
5605 &dev_attr_debug_level.attr,
5606#endif
bc6f59bc 5607 &dev_attr_version.attr,
b481de9c
ZY
5608
5609 NULL
5610};
5611
bb8c093b 5612static struct attribute_group iwl4965_attribute_group = {
b481de9c 5613 .name = NULL, /* put in device directory */
bb8c093b 5614 .attrs = iwl4965_sysfs_entries,
b481de9c
ZY
5615};
5616
bb8c093b
CH
5617static struct ieee80211_ops iwl4965_hw_ops = {
5618 .tx = iwl4965_mac_tx,
5619 .start = iwl4965_mac_start,
5620 .stop = iwl4965_mac_stop,
5621 .add_interface = iwl4965_mac_add_interface,
5622 .remove_interface = iwl4965_mac_remove_interface,
5623 .config = iwl4965_mac_config,
5624 .config_interface = iwl4965_mac_config_interface,
5625 .configure_filter = iwl4965_configure_filter,
5626 .set_key = iwl4965_mac_set_key,
ab885f8c 5627 .update_tkip_key = iwl4965_mac_update_tkip_key,
bb8c093b
CH
5628 .get_stats = iwl4965_mac_get_stats,
5629 .get_tx_stats = iwl4965_mac_get_tx_stats,
5630 .conf_tx = iwl4965_mac_conf_tx,
5631 .get_tsf = iwl4965_mac_get_tsf,
5632 .reset_tsf = iwl4965_mac_reset_tsf,
5633 .beacon_update = iwl4965_mac_beacon_update,
471b3efd 5634 .bss_info_changed = iwl4965_bss_info_changed,
c8b0e6e1 5635#ifdef CONFIG_IWL4965_HT
9ab46173 5636 .ampdu_action = iwl4965_mac_ampdu_action,
c8b0e6e1 5637#endif /* CONFIG_IWL4965_HT */
bb8c093b 5638 .hw_scan = iwl4965_mac_hw_scan
b481de9c
ZY
5639};
5640
bb8c093b 5641static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
b481de9c
ZY
5642{
5643 int err = 0;
c79dd5b5 5644 struct iwl_priv *priv;
b481de9c 5645 struct ieee80211_hw *hw;
82b9a121 5646 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
0359facc 5647 unsigned long flags;
5a66926a 5648 DECLARE_MAC_BUF(mac);
b481de9c 5649
316c30d9
AK
5650 /************************
5651 * 1. Allocating HW data
5652 ************************/
5653
6440adb5
CB
5654 /* Disabling hardware scan means that mac80211 will perform scans
5655 * "the hard way", rather than using device's scan. */
1ea87396 5656 if (cfg->mod_params->disable_hw_scan) {
bf403db8
EK
5657 if (cfg->mod_params->debug & IWL_DL_INFO)
5658 dev_printk(KERN_DEBUG, &(pdev->dev),
5659 "Disabling hw_scan\n");
bb8c093b 5660 iwl4965_hw_ops.hw_scan = NULL;
b481de9c
ZY
5661 }
5662
1d0a082d
AK
5663 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
5664 if (!hw) {
b481de9c
ZY
5665 err = -ENOMEM;
5666 goto out;
5667 }
1d0a082d
AK
5668 priv = hw->priv;
5669 /* At this point both hw and priv are allocated. */
5670
b481de9c
ZY
5671 SET_IEEE80211_DEV(hw, &pdev->dev);
5672
5673 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
82b9a121 5674 priv->cfg = cfg;
b481de9c 5675 priv->pci_dev = pdev;
316c30d9 5676
0a6857e7 5677#ifdef CONFIG_IWLWIFI_DEBUG
bf403db8 5678 priv->debug_level = priv->cfg->mod_params->debug;
b481de9c
ZY
5679 atomic_set(&priv->restrict_refcnt, 0);
5680#endif
b481de9c 5681
316c30d9
AK
5682 /**************************
5683 * 2. Initializing PCI bus
5684 **************************/
5685 if (pci_enable_device(pdev)) {
5686 err = -ENODEV;
5687 goto out_ieee80211_free_hw;
5688 }
5689
5690 pci_set_master(pdev);
5691
cc2a8ea8 5692 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
316c30d9 5693 if (!err)
cc2a8ea8
RR
5694 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
5695 if (err) {
5696 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
5697 if (!err)
5698 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
5699 /* both attempts failed: */
316c30d9 5700 if (err) {
cc2a8ea8
RR
5701 printk(KERN_WARNING "%s: No suitable DMA available.\n",
5702 DRV_NAME);
316c30d9 5703 goto out_pci_disable_device;
cc2a8ea8 5704 }
316c30d9
AK
5705 }
5706
5707 err = pci_request_regions(pdev, DRV_NAME);
5708 if (err)
5709 goto out_pci_disable_device;
5710
5711 pci_set_drvdata(pdev, priv);
5712
5713 /* We disable the RETRY_TIMEOUT register (0x41) to keep
5714 * PCI Tx retries from interfering with C3 CPU state */
5715 pci_write_config_byte(pdev, 0x41, 0x00);
5716
5717 /***********************
5718 * 3. Read REV register
5719 ***********************/
5720 priv->hw_base = pci_iomap(pdev, 0, 0);
5721 if (!priv->hw_base) {
5722 err = -ENODEV;
5723 goto out_pci_release_regions;
5724 }
5725
5726 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
5727 (unsigned long long) pci_resource_len(pdev, 0));
5728 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
5729
b661c819 5730 iwl_hw_detect(priv);
316c30d9 5731 printk(KERN_INFO DRV_NAME
b661c819
TW
5732 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
5733 priv->cfg->name, priv->hw_rev);
316c30d9 5734
91238714
TW
5735 /* amp init */
5736 err = priv->cfg->ops->lib->apm_ops.init(priv);
316c30d9 5737 if (err < 0) {
91238714 5738 IWL_DEBUG_INFO("Failed to init APMG\n");
316c30d9
AK
5739 goto out_iounmap;
5740 }
91238714
TW
5741 /*****************
5742 * 4. Read EEPROM
5743 *****************/
316c30d9
AK
5744 /* Read the EEPROM */
5745 err = iwl_eeprom_init(priv);
5746 if (err) {
5747 IWL_ERROR("Unable to init EEPROM\n");
5748 goto out_iounmap;
5749 }
8614f360
TW
5750 err = iwl_eeprom_check_version(priv);
5751 if (err)
5752 goto out_iounmap;
5753
02883017 5754 /* extract MAC Address */
316c30d9
AK
5755 iwl_eeprom_get_mac(priv, priv->mac_addr);
5756 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
5757 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
5758
5759 /************************
5760 * 5. Setup HW constants
5761 ************************/
5762 /* Device-specific setup */
5425e490
TW
5763 if (priv->cfg->ops->lib->set_hw_params(priv)) {
5764 IWL_ERROR("failed to set hw parameters\n");
073d3f5f 5765 goto out_free_eeprom;
316c30d9
AK
5766 }
5767
5768 /*******************
6ba87956 5769 * 6. Setup priv
316c30d9 5770 *******************/
b481de9c 5771
6ba87956 5772 err = iwl_init_drv(priv);
bf85ea4f 5773 if (err)
399f4900 5774 goto out_free_eeprom;
bf85ea4f 5775 /* At this point both hw and priv are initialized. */
316c30d9
AK
5776
5777 /**********************************
5778 * 7. Initialize module parameters
5779 **********************************/
5780
5781 /* Disable radio (SW RF KILL) via parameter when loading driver */
1ea87396 5782 if (priv->cfg->mod_params->disable) {
316c30d9
AK
5783 set_bit(STATUS_RF_KILL_SW, &priv->status);
5784 IWL_DEBUG_INFO("Radio disabled.\n");
5785 }
5786
316c30d9
AK
5787 /********************
5788 * 8. Setup services
5789 ********************/
0359facc 5790 spin_lock_irqsave(&priv->lock, flags);
316c30d9 5791 iwl4965_disable_interrupts(priv);
0359facc 5792 spin_unlock_irqrestore(&priv->lock, flags);
316c30d9
AK
5793
5794 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
5795 if (err) {
5796 IWL_ERROR("failed to create sysfs device attributes\n");
6ba87956 5797 goto out_uninit_drv;
316c30d9
AK
5798 }
5799
316c30d9
AK
5800
5801 iwl4965_setup_deferred_work(priv);
5802 iwl4965_setup_rx_handlers(priv);
5803
5804 /********************
5805 * 9. Conclude
5806 ********************/
5a66926a
ZY
5807 pci_save_state(pdev);
5808 pci_disable_device(pdev);
b481de9c 5809
6ba87956
TW
5810 /**********************************
5811 * 10. Setup and register mac80211
5812 **********************************/
5813
5814 err = iwl_setup_mac(priv);
5815 if (err)
5816 goto out_remove_sysfs;
5817
5818 err = iwl_dbgfs_register(priv, DRV_NAME);
5819 if (err)
5820 IWL_ERROR("failed to create debugfs files\n");
5821
c8381fdc
MA
5822 /* notify iwlcore to init */
5823 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
b481de9c
ZY
5824 return 0;
5825
316c30d9
AK
5826 out_remove_sysfs:
5827 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
6ba87956
TW
5828 out_uninit_drv:
5829 iwl_uninit_drv(priv);
073d3f5f
TW
5830 out_free_eeprom:
5831 iwl_eeprom_free(priv);
b481de9c
ZY
5832 out_iounmap:
5833 pci_iounmap(pdev, priv->hw_base);
5834 out_pci_release_regions:
5835 pci_release_regions(pdev);
316c30d9 5836 pci_set_drvdata(pdev, NULL);
b481de9c
ZY
5837 out_pci_disable_device:
5838 pci_disable_device(pdev);
b481de9c
ZY
5839 out_ieee80211_free_hw:
5840 ieee80211_free_hw(priv->hw);
5841 out:
5842 return err;
5843}
5844
c83dbf68 5845static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
b481de9c 5846{
c79dd5b5 5847 struct iwl_priv *priv = pci_get_drvdata(pdev);
b481de9c
ZY
5848 struct list_head *p, *q;
5849 int i;
0359facc 5850 unsigned long flags;
b481de9c
ZY
5851
5852 if (!priv)
5853 return;
5854
5855 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
5856
c4f55232
RR
5857 if (priv->mac80211_registered) {
5858 ieee80211_unregister_hw(priv->hw);
5859 priv->mac80211_registered = 0;
5860 }
5861
b481de9c 5862 set_bit(STATUS_EXIT_PENDING, &priv->status);
b24d22b1 5863
bb8c093b 5864 iwl4965_down(priv);
b481de9c 5865
0359facc
MA
5866 /* make sure we flush any pending irq or
5867 * tasklet for the driver
5868 */
5869 spin_lock_irqsave(&priv->lock, flags);
5870 iwl4965_disable_interrupts(priv);
5871 spin_unlock_irqrestore(&priv->lock, flags);
5872
5873 iwl_synchronize_irq(priv);
5874
b481de9c
ZY
5875 /* Free MAC hash list for ADHOC */
5876 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
5877 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
5878 list_del(p);
bb8c093b 5879 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
b481de9c
ZY
5880 }
5881 }
5882
c8381fdc 5883 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
712b6cf5 5884 iwl_dbgfs_unregister(priv);
bb8c093b 5885 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
b481de9c 5886
bb8c093b 5887 iwl4965_dealloc_ucode_pci(priv);
b481de9c
ZY
5888
5889 if (priv->rxq.bd)
a55360e4 5890 iwl_rx_queue_free(priv, &priv->rxq);
1053d35f 5891 iwl_hw_txq_ctx_free(priv);
b481de9c 5892
bf85ea4f 5893 iwlcore_clear_stations_table(priv);
073d3f5f 5894 iwl_eeprom_free(priv);
b481de9c 5895
b481de9c 5896
948c171c
MA
5897 /*netif_stop_queue(dev); */
5898 flush_workqueue(priv->workqueue);
5899
bb8c093b 5900 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
b481de9c
ZY
5901 * priv->workqueue... so we can't take down the workqueue
5902 * until now... */
5903 destroy_workqueue(priv->workqueue);
5904 priv->workqueue = NULL;
5905
b481de9c
ZY
5906 pci_iounmap(pdev, priv->hw_base);
5907 pci_release_regions(pdev);
5908 pci_disable_device(pdev);
5909 pci_set_drvdata(pdev, NULL);
5910
6ba87956 5911 iwl_uninit_drv(priv);
b481de9c
ZY
5912
5913 if (priv->ibss_beacon)
5914 dev_kfree_skb(priv->ibss_beacon);
5915
5916 ieee80211_free_hw(priv->hw);
5917}
5918
5919#ifdef CONFIG_PM
5920
bb8c093b 5921static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
b481de9c 5922{
c79dd5b5 5923 struct iwl_priv *priv = pci_get_drvdata(pdev);
b481de9c 5924
e655b9f0
ZY
5925 if (priv->is_open) {
5926 set_bit(STATUS_IN_SUSPEND, &priv->status);
5927 iwl4965_mac_stop(priv->hw);
5928 priv->is_open = 1;
5929 }
b481de9c 5930
b481de9c
ZY
5931 pci_set_power_state(pdev, PCI_D3hot);
5932
b481de9c
ZY
5933 return 0;
5934}
5935
bb8c093b 5936static int iwl4965_pci_resume(struct pci_dev *pdev)
b481de9c 5937{
c79dd5b5 5938 struct iwl_priv *priv = pci_get_drvdata(pdev);
b481de9c 5939
b481de9c 5940 pci_set_power_state(pdev, PCI_D0);
b481de9c 5941
e655b9f0
ZY
5942 if (priv->is_open)
5943 iwl4965_mac_start(priv->hw);
b481de9c 5944
e655b9f0 5945 clear_bit(STATUS_IN_SUSPEND, &priv->status);
b481de9c
ZY
5946 return 0;
5947}
5948
5949#endif /* CONFIG_PM */
5950
5951/*****************************************************************************
5952 *
5953 * driver and module entry point
5954 *
5955 *****************************************************************************/
5956
fed9017e
RR
5957/* Hardware specific file defines the PCI IDs table for that hardware module */
5958static struct pci_device_id iwl_hw_card_ids[] = {
5959 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
5960 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
5a6a256e
TW
5961#ifdef CONFIG_IWL5000
5962 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
5963 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
5964 {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
5965#endif /* CONFIG_IWL5000 */
fed9017e
RR
5966 {0}
5967};
5968MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
5969
5970static struct pci_driver iwl_driver = {
b481de9c 5971 .name = DRV_NAME,
fed9017e 5972 .id_table = iwl_hw_card_ids,
bb8c093b
CH
5973 .probe = iwl4965_pci_probe,
5974 .remove = __devexit_p(iwl4965_pci_remove),
b481de9c 5975#ifdef CONFIG_PM
bb8c093b
CH
5976 .suspend = iwl4965_pci_suspend,
5977 .resume = iwl4965_pci_resume,
b481de9c
ZY
5978#endif
5979};
5980
bb8c093b 5981static int __init iwl4965_init(void)
b481de9c
ZY
5982{
5983
5984 int ret;
5985 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
5986 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
897e1cf2
RC
5987
5988 ret = iwl4965_rate_control_register();
5989 if (ret) {
5990 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
5991 return ret;
5992 }
5993
fed9017e 5994 ret = pci_register_driver(&iwl_driver);
b481de9c
ZY
5995 if (ret) {
5996 IWL_ERROR("Unable to initialize PCI module\n");
897e1cf2 5997 goto error_register;
b481de9c 5998 }
b481de9c
ZY
5999
6000 return ret;
897e1cf2 6001
897e1cf2
RC
6002error_register:
6003 iwl4965_rate_control_unregister();
6004 return ret;
b481de9c
ZY
6005}
6006
bb8c093b 6007static void __exit iwl4965_exit(void)
b481de9c 6008{
fed9017e 6009 pci_unregister_driver(&iwl_driver);
897e1cf2 6010 iwl4965_rate_control_unregister();
b481de9c
ZY
6011}
6012
bb8c093b
CH
6013module_exit(iwl4965_exit);
6014module_init(iwl4965_init);
This page took 0.661968 seconds and 5 git commands to generate.