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