blob: 0acd42bd43c571ca9a0f403d9eb5e846e50482d8 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatreeb7ae892008-03-11 16:17:17 -07003 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
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
Zhu Yib481de92007-09-25 17:54:57 -070030#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>
Zhu Yib481de92007-09-25 17:54:57 -070041#include <linux/etherdevice.h>
42#include <linux/if_arp.h>
43
Zhu Yib481de92007-09-25 17:54:57 -070044#include <net/mac80211.h>
45
46#include <asm/div64.h>
47
Assaf Krauss6bc913b2008-03-11 16:17:18 -070048#include "iwl-eeprom.h"
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070049#include "iwl-dev.h"
Tomas Winklerfee12472008-04-03 16:05:21 -070050#include "iwl-core.h"
Tomas Winkler3395f6e2008-03-25 16:33:37 -070051#include "iwl-io.h"
Zhu Yib481de92007-09-25 17:54:57 -070052#include "iwl-helpers.h"
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070053#include "iwl-sta.h"
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070054#include "iwl-calib.h"
Zhu Yib481de92007-09-25 17:54:57 -070055
Christoph Hellwig416e1432007-10-25 17:15:49 +080056
Zhu Yib481de92007-09-25 17:54:57 -070057/******************************************************************************
58 *
59 * module boiler plate
60 *
61 ******************************************************************************/
62
Zhu Yib481de92007-09-25 17:54:57 -070063/*
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
Tomas Winkler0a6857e2008-03-12 16:58:49 -070070#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070071#define VD "d"
72#else
73#define VD
74#endif
75
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080076#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070077#define VS "s"
78#else
79#define VS
80#endif
81
Tomas Winklerdf48c322008-03-06 10:40:19 -080082#define DRV_VERSION IWLWIFI_VERSION VD VS
Zhu Yib481de92007-09-25 17:54:57 -070083
Zhu Yib481de92007-09-25 17:54:57 -070084
85MODULE_DESCRIPTION(DRV_DESCRIPTION);
86MODULE_VERSION(DRV_VERSION);
87MODULE_AUTHOR(DRV_COPYRIGHT);
88MODULE_LICENSE("GPL");
89
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080090static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -070091{
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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800106static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700107{
108 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
109 const char *s = essid;
110 char *d = escaped;
111
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800112 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700113 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
Zhu Yib481de92007-09-25 17:54:57 -0700130/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800131 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700132 * the functionality provided here
133 */
134
135/**************************************************************/
136
Zhu Yib481de92007-09-25 17:54:57 -0700137
Zhu Yib481de92007-09-25 17:54:57 -0700138
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700139static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
140{
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800141 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700142
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
Zhu Yib481de92007-09-25 17:54:57 -0700150/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800151 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700152 *
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 */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800157static int iwl4965_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700158{
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) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800217 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700218 return -1;
219 }
220 return 0;
221}
222
223/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800224 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800225 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700226 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800227 * 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.
Zhu Yib481de92007-09-25 17:54:57 -0700230 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700231static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700232{
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
Zhu Yib481de92007-09-25 17:54:57 -0700271/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800272 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700273 *
Ian Schram01ebd062007-10-25 17:15:22 +0800274 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700275 * 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 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700279static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700280{
281 /* cast away the const for active_rxon in this function */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800282 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -0700283 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700284 int rc = 0;
285
Tomas Winklerfee12472008-04-03 16:05:21 -0700286 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700287 return -1;
288
289 /* always get timestamp with Rx frame */
290 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
291
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800292 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700293 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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800299 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700300 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800301 if (!iwl4965_full_rxon_required(priv)) {
Tomas Winkler7e8c5192008-04-15 16:01:43 -0700302 rc = iwl_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700303 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
Zhu Yib481de92007-09-25 17:54:57 -0700317 /* 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 */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700321 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700322 (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
Tomas Winkler857485c2008-03-21 13:53:44 -0700326 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800327 sizeof(struct iwl_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700328 &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 }
Zhu Yib481de92007-09-25 17:54:57 -0700338 }
339
340 IWL_DEBUG_INFO("Sending RXON\n"
341 "* with%s RXON_FILTER_ASSOC_MSK\n"
342 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -0700343 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700344 ((priv->staging_rxon.filter_flags &
345 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
346 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -0700347 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -0700348
Ron Rindjunsky099b40b2008-04-21 15:41:53 -0700349 iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
Zhu Yib481de92007-09-25 17:54:57 -0700350 /* Apply the new configuration */
Tomas Winkler857485c2008-03-21 13:53:44 -0700351 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800352 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700353 if (rc) {
354 IWL_ERROR("Error setting new configuration (%d).\n", rc);
355 return rc;
356 }
357
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800358 iwl_remove_station(priv, iwl_bcast_addr, 0);
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700359 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800360
Zhu Yib481de92007-09-25 17:54:57 -0700361 if (!priv->error_recovering)
362 priv->start_calib = 0;
363
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700364 iwl_init_sensitivity(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700365
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 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800370 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700371 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 */
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800377 if (iwl_rxon_add_station(priv, iwl_bcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700378 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 */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700385 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700386 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800387 if (iwl_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700388 == IWL_INVALID_STATION) {
389 IWL_ERROR("Error adding AP address for transmit.\n");
390 return -EIO;
391 }
392 priv->assoc_station_added = 1;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700393 if (priv->default_wep_key &&
394 iwl_send_static_wepkey_cmd(priv, 0))
395 IWL_ERROR("Could not send WEP static key.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700396 }
397
398 return 0;
399}
400
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700401void iwl4965_update_chain_flags(struct iwl_priv *priv)
402{
403
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -0700404 iwl_set_rxon_chain(priv);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700405 iwl4965_commit_rxon(priv);
406}
407
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700408static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700409{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800410 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700411 .flags = 3,
412 .lead_time = 0xAA,
413 .max_kill = 1,
414 .kill_ack_mask = 0,
415 .kill_cts_mask = 0,
416 };
417
Tomas Winkler857485c2008-03-21 13:53:44 -0700418 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800419 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700420}
421
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700422static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700423{
Tomas Winklerdb11d632008-05-05 10:22:33 +0800424 int ret = 0;
425 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -0700426 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700427 .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
Tomas Winklerdb11d632008-05-05 10:22:33 +0800439 ret = iwl_send_cmd_sync(priv, &cmd);
440 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700441 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
Tomas Winklerdb11d632008-05-05 10:22:33 +0800442 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700443 }
444
Tomas Winklerdb11d632008-05-05 10:22:33 +0800445 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700446 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
Tomas Winklerdb11d632008-05-05 10:22:33 +0800460 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700461}
462
Zhu Yib481de92007-09-25 17:54:57 -0700463/*
464 * CARD_STATE_CMD
465 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800466 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -0700467 *
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 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700473static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -0700474{
Tomas Winkler857485c2008-03-21 13:53:44 -0700475 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700476 .id = REPLY_CARD_STATE_CMD,
477 .len = sizeof(u32),
478 .data = &flags,
479 .meta.flags = meta_flag,
480 };
481
Tomas Winkler857485c2008-03-21 13:53:44 -0700482 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700483}
484
Tomas Winklerfcab4232008-05-15 13:54:01 +0800485static void iwl_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700486{
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);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800495 kfree(list_entry(element, struct iwl_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700496 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
Tomas Winklerfcab4232008-05-15 13:54:01 +0800506static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700507{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800508 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700509 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);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800523 return list_entry(element, struct iwl_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700524}
525
Tomas Winklerfcab4232008-05-15 13:54:01 +0800526static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700527{
528 memset(frame, 0, sizeof(*frame));
529 list_add(&frame->list, &priv->free_frames);
530}
531
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700532unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700533 struct ieee80211_hdr *hdr,
534 const u8 *dest, int left)
535{
536
Tomas Winkler3109ece2008-03-28 16:33:35 -0700537 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -0700538 ((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
Guy Cohen39e88502008-04-23 17:14:57 -0700550static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700551{
Guy Cohen39e88502008-04-23 17:14:57 -0700552 int i;
553 int rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -0700554
Guy Cohen39e88502008-04-23 17:14:57 -0700555 /* 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;
560
561 /* Find lowest valid rate */
Zhu Yib481de92007-09-25 17:54:57 -0700562 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800563 i = iwl_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -0700564 if (rate_mask & (1 << i))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800565 return iwl_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700566 }
567
Guy Cohen39e88502008-04-23 17:14:57 -0700568 /* 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;
Zhu Yib481de92007-09-25 17:54:57 -0700573}
574
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700575static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700576{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800577 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700578 unsigned int frame_size;
579 int rc;
580 u8 rate;
581
Tomas Winklerfcab4232008-05-15 13:54:01 +0800582 frame = iwl_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700583
584 if (!frame) {
585 IWL_ERROR("Could not obtain free frame buffer for beacon "
586 "command.\n");
587 return -ENOMEM;
588 }
589
Guy Cohen39e88502008-04-23 17:14:57 -0700590 rate = iwl4965_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700591
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800592 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700593
Tomas Winkler857485c2008-03-21 13:53:44 -0700594 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700595 &frame->u.cmd[0]);
596
Tomas Winklerfcab4232008-05-15 13:54:01 +0800597 iwl_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700598
599 return rc;
600}
601
602/******************************************************************************
603 *
Zhu Yib481de92007-09-25 17:54:57 -0700604 * Misc. internal state and helper functions
605 *
606 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -0700607
Zhu Yib481de92007-09-25 17:54:57 -0700608/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800609 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -0700610 *
611 * return : set the bit for each supported rate insert in ie
612 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800613static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +0200614 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -0700615{
616 u16 ret_rates = 0, bit;
617 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200618 u8 *cnt = ie;
619 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -0700620
621 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
622 if (bit & supported_rate) {
623 ret_rates |= bit;
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800624 rates[*cnt] = iwl_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +0200625 ((bit & basic_rate) ? 0x80 : 0x00);
626 (*cnt)++;
627 (*left)--;
628 if ((*left <= 0) ||
629 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -0700630 break;
631 }
632 }
633
634 return ret_rates;
635}
636
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700637static 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)
Emmanuel Grumbacha9841012008-05-15 13:54:08 +0800654 iwl_conf->sgf |= HT_SHORT_GI_20MHZ;
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700655 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
Emmanuel Grumbacha9841012008-05-15 13:54:08 +0800656 iwl_conf->sgf |= HT_SHORT_GI_40MHZ;
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700657
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}
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700709
Zhu Yib481de92007-09-25 17:54:57 -0700710/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800711 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -0700712 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700713static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +0200714 enum ieee80211_band band,
715 struct ieee80211_mgmt *frame,
716 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -0700717{
718 int len = 0;
719 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +0800720 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Tomas Winkler78330fd2008-02-06 02:37:18 +0200721 const struct ieee80211_supported_band *sband =
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700722 iwl_get_hw_mode(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -0700723
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);
Tomas Winkler57bd1be2008-05-15 13:54:03 +0800732 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -0700733 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Tomas Winkler57bd1be2008-05-15 13:54:03 +0800734 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -0700735 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;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200767
Zhu Yib481de92007-09-25 17:54:57 -0700768 /* ... fill it in... */
769 *pos++ = WLAN_EID_SUPP_RATES;
770 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200771
mabbasbee488d2007-10-25 17:15:42 +0800772 /* 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;
Zhu Yib481de92007-09-25 17:54:57 -0700777
Tomas Winklerc7c46672007-10-18 02:04:15 +0200778 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800779 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +0800780 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +0200781 active_rates &= ~ret_rates;
782
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800783 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +0800784 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +0200785 active_rates &= ~ret_rates;
786
Zhu Yib481de92007-09-25 17:54:57 -0700787 len += 2 + *pos;
788 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200789 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -0700790 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;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800800 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +0800801 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -0700802 if (*pos > 0)
803 len += 2 + *pos;
804
Zhu Yib481de92007-09-25 17:54:57 -0700805 fill_end:
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700806 /* 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;
Zhu Yib481de92007-09-25 17:54:57 -0700818 return (u16)len;
819}
820
821/*
822 * QoS support
823*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700824static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800825 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -0700826{
827
Tomas Winkler857485c2008-03-21 13:53:44 -0700828 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800829 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -0700830}
831
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700832static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -0700833{
834 unsigned long flags;
835
Zhu Yib481de92007-09-25 17:54:57 -0700836 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;
Zhu Yib481de92007-09-25 17:54:57 -0700849 if (priv->qos_data.qos_active)
850 priv->qos_data.def_qos_parm.qos_flags |=
851 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
852
Ron Rindjunskyfd105e72007-11-26 16:14:39 +0200853 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800854 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800855
Zhu Yib481de92007-09-25 17:54:57 -0700856 spin_unlock_irqrestore(&priv->lock, flags);
857
Tomas Winkler3109ece2008-03-28 16:33:35 -0700858 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800859 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);
Zhu Yib481de92007-09-25 17:54:57 -0700862
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800863 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -0700864 &(priv->qos_data.def_qos_parm));
865 }
866}
867
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700868int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -0700869{
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);
Tomas Winkler69dc5d92008-03-25 16:33:41 -0700891 default:
892 break;
Zhu Yib481de92007-09-25 17:54:57 -0700893 }
894
895 return 1;
896}
897
Zhu Yib481de92007-09-25 17:54:57 -0700898
Zhu Yib481de92007-09-25 17:54:57 -0700899
900/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800901 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -0700902 *
903 * NOTE: priv->mutex is not required before calling this function
904 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700905static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700906{
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/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800928 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -0700929 * @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 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700933static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -0700934{
935 unsigned long now = jiffies;
936 int ret;
937
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800938 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700939 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
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700952static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700953{
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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800963 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700964}
965
966#define MAX_UCODE_BEACON_INTERVAL 4096
967#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
968
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800969static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -0700970{
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
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700982static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700983{
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);
Tomas Winkler3109ece2008-03-28 16:33:35 -0700993 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);
Zhu Yib481de92007-09-25 17:54:57 -0700996
997 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
998
Tomas Winkler3109ece2008-03-28 16:33:35 -0700999 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001000
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 =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001012 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001013 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 =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001019 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001020 /* 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
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001038static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001039{
1040 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1041 IWL_ERROR("APs don't scan.\n");
1042 return 0;
1043 }
1044
Tomas Winklerfee12472008-04-03 16:05:21 -07001045 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001046 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
Zhu Yib481de92007-09-25 17:54:57 -07001072
Tomas Winkler82a66bb2008-05-29 16:35:28 +08001073static void iwl_set_flags_for_band(struct iwl_priv *priv,
1074 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001075{
Johannes Berg8318d782008-01-24 19:38:38 +01001076 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07001077 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 {
Reinette Chatre508e32e2008-04-14 21:16:13 -07001082 /* Copied from iwl4965_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001083 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/*
Ian Schram01ebd062007-10-25 17:15:22 +08001098 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001099 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001100static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001101{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001102 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001103
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;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001128 default:
1129 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1130 break;
Zhu Yib481de92007-09-25 17:54:57 -07001131 }
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
Assaf Krauss8622e702008-03-21 13:53:43 -07001142 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001143 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);
Johannes Berg8318d782008-01-24 19:38:38 +01001157 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07001158
Tomas Winkler82a66bb2008-05-29 16:35:28 +08001159 iwl_set_flags_for_band(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001160
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;
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07001172 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001173}
1174
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001175static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001176{
Zhu Yib481de92007-09-25 17:54:57 -07001177 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001178 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001179
Assaf Krauss8622e702008-03-21 13:53:43 -07001180 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001181 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001182 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
Zhu Yib481de92007-09-25 17:54:57 -07001191 priv->iw_mode = mode;
1192
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001193 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001194 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1195
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001196 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001197
Mohamed Abbasfde35712007-11-29 11:10:15 +08001198 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -07001199 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08001200 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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001209 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001210
1211 return 0;
1212}
1213
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001214static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001215{
Johannes Berg8318d782008-01-24 19:38:38 +01001216 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001217 struct ieee80211_rate *rate;
1218 int i;
1219
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001220 hw = iwl_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08001221 if (!hw) {
1222 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
1223 return;
1224 }
Zhu Yib481de92007-09-25 17:54:57 -07001225
1226 priv->active_rate = 0;
1227 priv->active_rate_basic = 0;
1228
Johannes Berg8318d782008-01-24 19:38:38 +01001229 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);
Zhu Yib481de92007-09-25 17:54:57 -07001233 }
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
Mohamed Abbasad97edd2008-03-28 16:21:06 -07001261void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07001262{
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) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001272 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001273 /* FIXME: This is a workaround for AP */
1274 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
1275 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001276 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001277 CSR_UCODE_SW_BIT_RFKILL);
1278 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07001279 /* call the host command only if no hw rf-kill set */
Mohamed Abbas59003832008-04-15 16:01:46 -07001280 if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
1281 iwl_is_ready(priv))
Mohamed Abbasad97edd2008-03-28 16:21:06 -07001282 iwl4965_send_card_state(priv,
1283 CARD_STATE_CMD_DISABLE,
1284 0);
Zhu Yib481de92007-09-25 17:54:57 -07001285 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07001286
1287 /* make sure mac80211 stop sending Tx frame */
1288 if (priv->mac80211_registered)
1289 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07001290 }
1291 return;
1292 }
1293
1294 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001295 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07001296
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);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001304 iwl_read32(priv, CSR_UCODE_DRV_GP1);
1305 if (!iwl_grab_nic_access(priv))
1306 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001307 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
Zhu Yib481de92007-09-25 17:54:57 -07001319#define IWL_PACKET_RETRY_TIME HZ
1320
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001321int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001322{
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;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001332 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001333 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]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001337 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07001338 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) {
Ian Schrambc434dd2007-10-25 17:15:29 +08001344 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07001345 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;
Ian Schrambc434dd2007-10-25 17:15:29 +08001351 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07001352 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
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001385#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07001386
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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001400static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07001401{
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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001419static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07001420{
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
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001438static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001439 struct ieee80211_measurement_params *params,
1440 u8 type)
1441{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001442 struct iwl4965_spectrum_cmd spectrum;
Tomas Winklerdb11d632008-05-05 10:22:33 +08001443 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07001444 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001445 .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
Tomas Winkler3109ece2008-03-28 16:33:35 -07001454 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001455 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001456 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07001457 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
Tomas Winkler3109ece2008-03-28 16:33:35 -07001469 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001470 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001471 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07001472 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
Tomas Winkler857485c2008-03-21 13:53:44 -07001484 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001485 if (rc)
1486 return rc;
1487
Tomas Winklerdb11d632008-05-05 10:22:33 +08001488 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001489 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
Zhu Yib481de92007-09-25 17:54:57 -07001518/******************************************************************************
1519 *
1520 * Generic RX handler implementations
1521 *
1522 ******************************************************************************/
Tomas Winkler885ba202008-05-29 16:34:55 +08001523static void iwl_rx_reply_alive(struct iwl_priv *priv,
1524 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001525{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001526 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Tomas Winkler885ba202008-05-29 16:34:55 +08001527 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07001528 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,
Tomas Winkler885ba202008-05-29 16:34:55 +08001541 sizeof(struct iwl_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001542 pwork = &priv->init_alive_start;
1543 } else {
1544 IWL_DEBUG_INFO("Runtime Alive received.\n");
1545 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler885ba202008-05-29 16:34:55 +08001546 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001547 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
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001559static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001560 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001561{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001562 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001563
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
Tomas Winklera55360e2008-05-05 10:22:28 +08001575static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001576{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001577 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08001578 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001579 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07001580 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
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001586static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001587 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001588{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001589#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Tomas Winklerdb11d632008-05-05 10:22:33 +08001590 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001591 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07001592
1593 if (!report->state) {
Ester Kummerf3d67992008-05-06 11:05:12 +08001594 IWL_DEBUG(IWL_DL_11H,
1595 "Spectrum Measure Notification: Start\n");
Zhu Yib481de92007-09-25 17:54:57 -07001596 return;
1597 }
1598
1599 memcpy(&priv->measure_report, report, sizeof(*report));
1600 priv->measurement_status |= MEASUREMENT_READY;
1601#endif
1602}
1603
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001604static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001605 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001606{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001607#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08001608 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001609 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07001610 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
1611 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1612#endif
1613}
1614
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001615static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001616 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001617{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001618 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001619 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));
Ester Kummerbf403db2008-05-05 10:22:40 +08001622 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07001623}
1624
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001625static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07001626{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001627 struct iwl_priv *priv =
1628 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07001629 struct sk_buff *beacon;
1630
1631 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02001632 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07001633
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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001647 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001648}
1649
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08001650/**
1651 * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
1652 *
1653 * This callback is provided in order to send a statistics request.
1654 *
1655 * This timer function is continually reset to execute within
1656 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
1657 * was received. We need to ensure we receive the statistics in order
1658 * to update the temperature used for calibrating the TXPOWER.
1659 */
1660static void iwl4965_bg_statistics_periodic(unsigned long data)
1661{
1662 struct iwl_priv *priv = (struct iwl_priv *)data;
1663
1664 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1665 return;
1666
1667 iwl_send_statistics_request(priv, CMD_ASYNC);
1668}
1669
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001670static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001671 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001672{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001673#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08001674 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001675 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
1676 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001677
1678 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
1679 "tsf %d %d rate %d\n",
1680 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
1681 beacon->beacon_notify_hdr.failure_frame,
1682 le32_to_cpu(beacon->ibss_mgr_status),
1683 le32_to_cpu(beacon->high_tsf),
1684 le32_to_cpu(beacon->low_tsf), rate);
1685#endif
1686
1687 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
1688 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
1689 queue_work(priv->workqueue, &priv->beacon_update);
1690}
1691
1692/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001693static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001694 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001695{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001696#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08001697 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001698 struct iwl4965_scanreq_notification *notif =
1699 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07001700
1701 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
1702#endif
1703}
1704
1705/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001706static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001707 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001708{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001709 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001710 struct iwl4965_scanstart_notification *notif =
1711 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07001712 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
1713 IWL_DEBUG_SCAN("Scan start: "
1714 "%d [802.11%s] "
1715 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
1716 notif->channel,
1717 notif->band ? "bg" : "a",
1718 notif->tsf_high,
1719 notif->tsf_low, notif->status, notif->beacon_timer);
1720}
1721
1722/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001723static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001724 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001725{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001726 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001727 struct iwl4965_scanresults_notification *notif =
1728 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07001729
1730 IWL_DEBUG_SCAN("Scan ch.res: "
1731 "%d [802.11%s] "
1732 "(TSF: 0x%08X:%08X) - %d "
1733 "elapsed=%lu usec (%dms since last)\n",
1734 notif->channel,
1735 notif->band ? "bg" : "a",
1736 le32_to_cpu(notif->tsf_high),
1737 le32_to_cpu(notif->tsf_low),
1738 le32_to_cpu(notif->statistics[0]),
1739 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
1740 jiffies_to_msecs(elapsed_jiffies
1741 (priv->last_scan_jiffies, jiffies)));
1742
1743 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08001744 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001745}
1746
1747/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001748static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001749 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001750{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001751 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001752 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07001753
1754 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
1755 scan_notif->scanned_channels,
1756 scan_notif->tsf_low,
1757 scan_notif->tsf_high, scan_notif->status);
1758
1759 /* The HW is no longer scanning */
1760 clear_bit(STATUS_SCAN_HW, &priv->status);
1761
1762 /* The scan completion notification came in, so kill that timer... */
1763 cancel_delayed_work(&priv->scan_check);
1764
1765 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
1766 (priv->scan_bands == 2) ? "2.4" : "5.2",
1767 jiffies_to_msecs(elapsed_jiffies
1768 (priv->scan_pass_start, jiffies)));
1769
1770 /* Remove this scanned band from the list
1771 * of pending bands to scan */
1772 priv->scan_bands--;
1773
1774 /* If a request to abort was given, or the scan did not succeed
1775 * then we reset the scan state machine and terminate,
1776 * re-queuing another scan if one has been requested */
1777 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1778 IWL_DEBUG_INFO("Aborted scan completed.\n");
1779 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1780 } else {
1781 /* If there are more bands on this scan pass reschedule */
1782 if (priv->scan_bands > 0)
1783 goto reschedule;
1784 }
1785
1786 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08001787 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001788 IWL_DEBUG_INFO("Setting scan to off\n");
1789
1790 clear_bit(STATUS_SCANNING, &priv->status);
1791
1792 IWL_DEBUG_INFO("Scan took %dms\n",
1793 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
1794
1795 queue_work(priv->workqueue, &priv->scan_completed);
1796
1797 return;
1798
1799reschedule:
1800 priv->scan_pass_start = jiffies;
1801 queue_work(priv->workqueue, &priv->request_scan);
1802}
1803
1804/* Handle notification from uCode that card's power state is changing
1805 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001806static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001807 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001808{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001809 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001810 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
1811 unsigned long status = priv->status;
1812
1813 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
1814 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1815 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
1816
1817 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
1818 RF_CARD_DISABLED)) {
1819
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001820 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001821 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1822
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001823 if (!iwl_grab_nic_access(priv)) {
1824 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07001825 priv, HBUS_TARG_MBX_C,
1826 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1827
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001828 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001829 }
1830
1831 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001832 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07001833 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001834 if (!iwl_grab_nic_access(priv)) {
1835 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07001836 priv, HBUS_TARG_MBX_C,
1837 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1838
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001839 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001840 }
1841 }
1842
1843 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001844 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001845 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001846 iwl_read32(priv, CSR_UCODE_DRV_GP1);
1847 if (!iwl_grab_nic_access(priv))
1848 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001849 }
1850 }
1851
1852 if (flags & HW_CARD_DISABLED)
1853 set_bit(STATUS_RF_KILL_HW, &priv->status);
1854 else
1855 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1856
1857
1858 if (flags & SW_CARD_DISABLED)
1859 set_bit(STATUS_RF_KILL_SW, &priv->status);
1860 else
1861 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1862
1863 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001864 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001865
1866 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
1867 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
1868 (test_bit(STATUS_RF_KILL_SW, &status) !=
1869 test_bit(STATUS_RF_KILL_SW, &priv->status)))
1870 queue_work(priv->workqueue, &priv->rf_kill);
1871 else
1872 wake_up_interruptible(&priv->wait_command_queue);
1873}
1874
Ron Rindjunsky37a44212008-05-29 16:35:18 +08001875/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
1876 * This will be used later in iwl4965_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
1877static void iwl4965_rx_reply_rx_phy(struct iwl_priv *priv,
1878 struct iwl_rx_mem_buffer *rxb)
1879{
1880 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1881 priv->last_phy_res[0] = 1;
1882 memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
1883 sizeof(struct iwl4965_rx_phy_res));
1884}
1885
Zhu Yib481de92007-09-25 17:54:57 -07001886/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001887 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07001888 *
1889 * Setup the RX handlers for each of the reply types sent from the uCode
1890 * to the host.
1891 *
1892 * This function chains into the hardware specific files for them to setup
1893 * any hardware specific handlers as well.
1894 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001895static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001896{
Tomas Winkler885ba202008-05-29 16:34:55 +08001897 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001898 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
1899 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07001900 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001901 iwl4965_rx_spectrum_measure_notif;
1902 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001903 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001904 iwl4965_rx_pm_debug_statistics_notif;
1905 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001906
Ben Cahill9fbab512007-11-29 11:09:47 +08001907 /*
1908 * The same handler is used for both the REPLY to a discrete
1909 * statistics request from the host as well as for the periodic
1910 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07001911 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001912 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
1913 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Ron Rindjunsky37a44212008-05-29 16:35:18 +08001914 /* scan handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001915 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
1916 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001917 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001918 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001919 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001920 iwl4965_rx_scan_complete_notif;
Ron Rindjunsky37a44212008-05-29 16:35:18 +08001921 /* status change handler */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001922 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001923
Tomas Winklerc1354752008-05-29 16:35:04 +08001924 priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
1925 iwl_rx_missed_beacon_notif;
Ron Rindjunsky37a44212008-05-29 16:35:18 +08001926 /* Rx handlers */
1927 priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
1928 priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl4965_rx_reply_rx;
Ben Cahill9fbab512007-11-29 11:09:47 +08001929 /* Set up hardware specific Rx handlers */
Emmanuel Grumbachd4789ef2008-04-24 11:55:20 -07001930 priv->cfg->ops->lib->rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001931}
1932
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001933/*
1934 * this should be called while priv->lock is locked
1935*/
Tomas Winklera55360e2008-05-05 10:22:28 +08001936static void __iwl_rx_replenish(struct iwl_priv *priv)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001937{
Tomas Winklera55360e2008-05-05 10:22:28 +08001938 iwl_rx_allocate(priv);
1939 iwl_rx_queue_restock(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001940}
1941
1942
Zhu Yib481de92007-09-25 17:54:57 -07001943/**
Tomas Winklera55360e2008-05-05 10:22:28 +08001944 * iwl_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07001945 *
1946 * Uses the priv->rx_handlers callback function array to invoke
1947 * the appropriate handlers, including command responses,
1948 * frame-received notifications, and other notifications.
1949 */
Tomas Winklera55360e2008-05-05 10:22:28 +08001950void iwl_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001951{
Tomas Winklera55360e2008-05-05 10:22:28 +08001952 struct iwl_rx_mem_buffer *rxb;
Tomas Winklerdb11d632008-05-05 10:22:33 +08001953 struct iwl_rx_packet *pkt;
Tomas Winklera55360e2008-05-05 10:22:28 +08001954 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001955 u32 r, i;
1956 int reclaim;
1957 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001958 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08001959 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07001960
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001961 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1962 * buffer that the driver may process (last buffer filled by ucode). */
Ron Rindjunskyd67f5482008-05-05 10:22:49 +08001963 r = priv->cfg->ops->lib->shared_mem_rx_idx(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001964 i = rxq->read;
1965
1966 /* Rx interrupt, but nothing sent from uCode */
1967 if (i == r)
Ester Kummerf3d67992008-05-06 11:05:12 +08001968 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07001969
Tomas Winklera55360e2008-05-05 10:22:28 +08001970 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001971 fill_rx = 1;
1972
Zhu Yib481de92007-09-25 17:54:57 -07001973 while (i != r) {
1974 rxb = rxq->queue[i];
1975
Ben Cahill9fbab512007-11-29 11:09:47 +08001976 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07001977 * then a bug has been introduced in the queue refilling
1978 * routines -- catch it here */
1979 BUG_ON(rxb == NULL);
1980
1981 rxq->queue[i] = NULL;
1982
1983 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07001984 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07001985 PCI_DMA_FROMDEVICE);
Tomas Winklerdb11d632008-05-05 10:22:33 +08001986 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001987
1988 /* Reclaim a command buffer only if this packet is a response
1989 * to a (driver-originated) command.
1990 * If the packet (e.g. Rx frame) originated from uCode,
1991 * there is no command buffer to reclaim.
1992 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1993 * but apparently a few don't get set; catch them here. */
1994 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1995 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07001996 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08001997 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07001998 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1999 (pkt->hdr.cmd != REPLY_TX);
2000
2001 /* Based on type of command response or notification,
2002 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002003 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07002004 if (priv->rx_handlers[pkt->hdr.cmd]) {
Ester Kummerf3d67992008-05-06 11:05:12 +08002005 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d, %s, 0x%02x\n", r,
2006 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002007 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
2008 } else {
2009 /* No handling needed */
Ester Kummerf3d67992008-05-06 11:05:12 +08002010 IWL_DEBUG(IWL_DL_RX,
Zhu Yib481de92007-09-25 17:54:57 -07002011 "r %d i %d No handler needed for %s, 0x%02x\n",
2012 r, i, get_cmd_string(pkt->hdr.cmd),
2013 pkt->hdr.cmd);
2014 }
2015
2016 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08002017 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07002018 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07002019 * as we reclaim the driver command queue */
2020 if (rxb && rxb->skb)
Tomas Winkler17b88922008-05-29 16:35:12 +08002021 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07002022 else
2023 IWL_WARNING("Claim null rxb?\n");
2024 }
2025
2026 /* For now we just don't re-use anything. We can tweak this
2027 * later to try and re-use notification packets and SKBs that
2028 * fail to Rx correctly */
2029 if (rxb->skb != NULL) {
2030 priv->alloc_rxb_skb--;
2031 dev_kfree_skb_any(rxb->skb);
2032 rxb->skb = NULL;
2033 }
2034
2035 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07002036 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02002037 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07002038 spin_lock_irqsave(&rxq->lock, flags);
2039 list_add_tail(&rxb->list, &priv->rxq.rx_used);
2040 spin_unlock_irqrestore(&rxq->lock, flags);
2041 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002042 /* If there are a lot of unused frames,
2043 * restock the Rx queue so ucode wont assert. */
2044 if (fill_rx) {
2045 count++;
2046 if (count >= 8) {
2047 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08002048 __iwl_rx_replenish(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002049 count = 0;
2050 }
2051 }
Zhu Yib481de92007-09-25 17:54:57 -07002052 }
2053
2054 /* Backtrack one entry */
2055 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08002056 iwl_rx_queue_restock(priv);
2057}
2058/* Convert linear signal-to-noise ratio into dB */
2059static u8 ratio2dB[100] = {
2060/* 0 1 2 3 4 5 6 7 8 9 */
2061 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
2062 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
2063 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
2064 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
2065 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
2066 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
2067 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
2068 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
2069 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
2070 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
2071};
2072
2073/* Calculates a relative dB value from a ratio of linear
2074 * (i.e. not dB) signal levels.
2075 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
2076int iwl4965_calc_db_from_ratio(int sig_ratio)
2077{
2078 /* 1000:1 or higher just report as 60 dB */
2079 if (sig_ratio >= 1000)
2080 return 60;
2081
2082 /* 100:1 or higher, divide by 10 and use table,
2083 * add 20 dB to make up for divide by 10 */
2084 if (sig_ratio >= 100)
2085 return (20 + (int)ratio2dB[sig_ratio/10]);
2086
2087 /* We shouldn't see this */
2088 if (sig_ratio < 1)
2089 return 0;
2090
2091 /* Use table for ratios 1:1 - 99:1 */
2092 return (int)ratio2dB[sig_ratio];
2093}
2094
2095#define PERFECT_RSSI (-20) /* dBm */
2096#define WORST_RSSI (-95) /* dBm */
2097#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
2098
2099/* Calculate an indication of rx signal quality (a percentage, not dBm!).
2100 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
2101 * about formulas used below. */
2102int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
2103{
2104 int sig_qual;
2105 int degradation = PERFECT_RSSI - rssi_dbm;
2106
2107 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
2108 * as indicator; formula is (signal dbm - noise dbm).
2109 * SNR at or above 40 is a great signal (100%).
2110 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
2111 * Weakest usable signal is usually 10 - 15 dB SNR. */
2112 if (noise_dbm) {
2113 if (rssi_dbm - noise_dbm >= 40)
2114 return 100;
2115 else if (rssi_dbm < noise_dbm)
2116 return 0;
2117 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
2118
2119 /* Else use just the signal level.
2120 * This formula is a least squares fit of data points collected and
2121 * compared with a reference system that had a percentage (%) display
2122 * for signal quality. */
2123 } else
2124 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
2125 (15 * RSSI_RANGE + 62 * degradation)) /
2126 (RSSI_RANGE * RSSI_RANGE);
2127
2128 if (sig_qual > 100)
2129 sig_qual = 100;
2130 else if (sig_qual < 1)
2131 sig_qual = 0;
2132
2133 return sig_qual;
Zhu Yib481de92007-09-25 17:54:57 -07002134}
2135
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002136#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08002137static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002138{
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08002139 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07002140 DECLARE_MAC_BUF(mac);
2141
Zhu Yib481de92007-09-25 17:54:57 -07002142 IWL_DEBUG_RADIO("RX CONFIG:\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002143 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07002144 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
2145 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
2146 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
2147 le32_to_cpu(rxon->filter_flags));
2148 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
2149 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
2150 rxon->ofdm_basic_rates);
2151 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07002152 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
2153 print_mac(mac, rxon->node_addr));
2154 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
2155 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07002156 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
2157}
2158#endif
2159
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002160static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002161{
2162 IWL_DEBUG_ISR("Enabling interrupts\n");
2163 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002164 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07002165}
2166
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002167/* call this function to flush any scheduled tasklet */
2168static inline void iwl_synchronize_irq(struct iwl_priv *priv)
2169{
2170 /* wait to make sure we flush pedding tasklet*/
2171 synchronize_irq(priv->pci_dev->irq);
2172 tasklet_kill(&priv->irq_tasklet);
2173}
2174
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002175static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002176{
2177 clear_bit(STATUS_INT_ENABLED, &priv->status);
2178
2179 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002180 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07002181
2182 /* acknowledge/clear/reset any interrupts still pending
2183 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002184 iwl_write32(priv, CSR_INT, 0xffffffff);
2185 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07002186 IWL_DEBUG_ISR("Disabled interrupts\n");
2187}
2188
Zhu Yib481de92007-09-25 17:54:57 -07002189
Zhu Yib481de92007-09-25 17:54:57 -07002190/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002191 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07002192 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002193static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002194{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002195 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07002196 set_bit(STATUS_FW_ERROR, &priv->status);
2197
2198 /* Cancel currently queued command. */
2199 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
2200
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002201#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08002202 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Ester Kummerede0cba2008-05-29 16:34:46 +08002203 iwl_dump_nic_error_log(priv);
Ester Kummer189a2b52008-05-15 13:54:18 +08002204 iwl_dump_nic_event_log(priv);
Ester Kummerbf403db2008-05-05 10:22:40 +08002205 iwl4965_print_rx_config_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002206 }
2207#endif
2208
2209 wake_up_interruptible(&priv->wait_command_queue);
2210
2211 /* Keep the restart process from trying to send host
2212 * commands by clearing the INIT status bit */
2213 clear_bit(STATUS_READY, &priv->status);
2214
2215 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08002216 IWL_DEBUG(IWL_DL_FW_ERRORS,
Zhu Yib481de92007-09-25 17:54:57 -07002217 "Restarting adapter due to uCode error.\n");
2218
Tomas Winkler3109ece2008-03-28 16:33:35 -07002219 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002220 memcpy(&priv->recovery_rxon, &priv->active_rxon,
2221 sizeof(priv->recovery_rxon));
2222 priv->error_recovering = 1;
2223 }
Ester Kummer3a1081e2008-05-06 11:05:14 +08002224 if (priv->cfg->mod_params->restart_fw)
2225 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07002226 }
2227}
2228
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002229static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002230{
2231 unsigned long flags;
2232
2233 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
2234 sizeof(priv->staging_rxon));
2235 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002236 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002237
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08002238 iwl_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07002239
2240 spin_lock_irqsave(&priv->lock, flags);
2241 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
2242 priv->error_recovering = 0;
2243 spin_unlock_irqrestore(&priv->lock, flags);
2244}
2245
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002246static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002247{
2248 u32 inta, handled = 0;
2249 u32 inta_fh;
2250 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002251#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002252 u32 inta_mask;
2253#endif
2254
2255 spin_lock_irqsave(&priv->lock, flags);
2256
2257 /* Ack/clear/reset pending uCode interrupts.
2258 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
2259 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002260 inta = iwl_read32(priv, CSR_INT);
2261 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07002262
2263 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
2264 * Any new interrupts that happen after this, either while we're
2265 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002266 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
2267 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07002268
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002269#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08002270 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08002271 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002272 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07002273 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
2274 inta, inta_mask, inta_fh);
2275 }
2276#endif
2277
2278 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
2279 * atomic, make sure that inta covers all the interrupts that
2280 * we've discovered, even if FH interrupt came in just after
2281 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08002282 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07002283 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08002284 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07002285 inta |= CSR_INT_BIT_FH_TX;
2286
2287 /* Now service all interrupt bits discovered above. */
2288 if (inta & CSR_INT_BIT_HW_ERR) {
2289 IWL_ERROR("Microcode HW error detected. Restarting.\n");
2290
2291 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002292 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002293
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002294 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002295
2296 handled |= CSR_INT_BIT_HW_ERR;
2297
2298 spin_unlock_irqrestore(&priv->lock, flags);
2299
2300 return;
2301 }
2302
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002303#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08002304 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07002305 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08002306 if (inta & CSR_INT_BIT_SCD)
2307 IWL_DEBUG_ISR("Scheduler finished to transmit "
2308 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002309
2310 /* Alive notification via Rx interrupt will do the real work */
2311 if (inta & CSR_INT_BIT_ALIVE)
2312 IWL_DEBUG_ISR("Alive interrupt\n");
2313 }
2314#endif
2315 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08002316 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07002317
Ben Cahill9fbab512007-11-29 11:09:47 +08002318 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07002319 if (inta & CSR_INT_BIT_RF_KILL) {
2320 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002321 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07002322 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
2323 hw_rf_kill = 1;
2324
Ester Kummerf3d67992008-05-06 11:05:12 +08002325 IWL_DEBUG(IWL_DL_RF_KILL, "RF_KILL bit toggled to %s.\n",
Zhu Yib481de92007-09-25 17:54:57 -07002326 hw_rf_kill ? "disable radio":"enable radio");
2327
2328 /* Queue restart only if RF_KILL switch was set to "kill"
2329 * when we loaded driver, and is now set to "enable".
2330 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08002331 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08002332 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
2333 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002334 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08002335 }
Zhu Yib481de92007-09-25 17:54:57 -07002336
2337 handled |= CSR_INT_BIT_RF_KILL;
2338 }
2339
Ben Cahill9fbab512007-11-29 11:09:47 +08002340 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07002341 if (inta & CSR_INT_BIT_CT_KILL) {
2342 IWL_ERROR("Microcode CT kill error detected.\n");
2343 handled |= CSR_INT_BIT_CT_KILL;
2344 }
2345
2346 /* Error detected by uCode */
2347 if (inta & CSR_INT_BIT_SW_ERR) {
2348 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
2349 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002350 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002351 handled |= CSR_INT_BIT_SW_ERR;
2352 }
2353
2354 /* uCode wakes up after power-down sleep */
2355 if (inta & CSR_INT_BIT_WAKEUP) {
2356 IWL_DEBUG_ISR("Wakeup interrupt\n");
Tomas Winklera55360e2008-05-05 10:22:28 +08002357 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Tomas Winklerbabcebf2008-05-15 13:54:00 +08002358 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
2359 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
2360 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
2361 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
2362 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
2363 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07002364
2365 handled |= CSR_INT_BIT_WAKEUP;
2366 }
2367
2368 /* All uCode command responses, including Tx command responses,
2369 * Rx "responses" (frame-received notification), and other
2370 * notifications from uCode come through here*/
2371 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Tomas Winklera55360e2008-05-05 10:22:28 +08002372 iwl_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002373 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
2374 }
2375
2376 if (inta & CSR_INT_BIT_FH_TX) {
2377 IWL_DEBUG_ISR("Tx interrupt\n");
2378 handled |= CSR_INT_BIT_FH_TX;
Ron Rindjunskydbb983b2008-05-15 13:54:12 +08002379 /* FH finished to write, send event */
2380 priv->ucode_write_complete = 1;
2381 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002382 }
2383
2384 if (inta & ~handled)
2385 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
2386
2387 if (inta & ~CSR_INI_SET_MASK) {
2388 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
2389 inta & ~CSR_INI_SET_MASK);
2390 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
2391 }
2392
2393 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002394 /* only Re-enable if diabled by irq */
2395 if (test_bit(STATUS_INT_ENABLED, &priv->status))
2396 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002397
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002398#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08002399 if (priv->debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002400 inta = iwl_read32(priv, CSR_INT);
2401 inta_mask = iwl_read32(priv, CSR_INT_MASK);
2402 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07002403 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
2404 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
2405 }
2406#endif
2407 spin_unlock_irqrestore(&priv->lock, flags);
2408}
2409
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002410static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07002411{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002412 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07002413 u32 inta, inta_mask;
2414 u32 inta_fh;
2415 if (!priv)
2416 return IRQ_NONE;
2417
2418 spin_lock(&priv->lock);
2419
2420 /* Disable (but don't clear!) interrupts here to avoid
2421 * back-to-back ISRs and sporadic interrupts from our NIC.
2422 * If we have something to service, the tasklet will re-enable ints.
2423 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002424 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
2425 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07002426
2427 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002428 inta = iwl_read32(priv, CSR_INT);
2429 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07002430
2431 /* Ignore interrupt if there's nothing in NIC to service.
2432 * This may be due to IRQ shared with another device,
2433 * or due to sporadic interrupts thrown from our NIC. */
2434 if (!inta && !inta_fh) {
2435 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
2436 goto none;
2437 }
2438
2439 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08002440 /* Hardware disappeared. It might have already raised
2441 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07002442 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08002443 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07002444 }
2445
2446 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
2447 inta, inta_mask, inta_fh);
2448
Joonwoo Park25c03d82008-01-23 10:15:20 -08002449 inta &= ~CSR_INT_BIT_SCD;
2450
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002451 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08002452 if (likely(inta || inta_fh))
2453 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07002454
Oliver Neukum66fbb542007-11-15 09:31:10 +08002455 unplugged:
2456 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07002457 return IRQ_HANDLED;
2458
2459 none:
2460 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002461 /* only Re-enable if diabled by irq */
2462 if (test_bit(STATUS_INT_ENABLED, &priv->status))
2463 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002464 spin_unlock(&priv->lock);
2465 return IRQ_NONE;
2466}
2467
Zhu Yib481de92007-09-25 17:54:57 -07002468/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
2469 * sending probe req. This should be set long enough to hear probe responses
2470 * from more than one AP. */
2471#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
2472#define IWL_ACTIVE_DWELL_TIME_52 (10)
2473
2474/* For faster active scanning, scan will move to the next channel if fewer than
2475 * PLCP_QUIET_THRESH packets are heard on this channel within
2476 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
2477 * time if it's a quiet channel (nothing responded to our probe, and there's
2478 * no other traffic).
2479 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
2480#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
2481#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
2482
2483/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
2484 * Must be set longer than active dwell time.
2485 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
2486#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
2487#define IWL_PASSIVE_DWELL_TIME_52 (10)
2488#define IWL_PASSIVE_DWELL_BASE (100)
2489#define IWL_CHANNEL_TUNE_TIME 5
2490
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002491static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002492 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002493{
Johannes Berg8318d782008-01-24 19:38:38 +01002494 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07002495 return IWL_ACTIVE_DWELL_TIME_52;
2496 else
2497 return IWL_ACTIVE_DWELL_TIME_24;
2498}
2499
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002500static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002501 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002502{
Johannes Berg8318d782008-01-24 19:38:38 +01002503 u16 active = iwl4965_get_active_dwell_time(priv, band);
2504 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07002505 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
2506 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
2507
Tomas Winkler3109ece2008-03-28 16:33:35 -07002508 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002509 /* If we're associated, we clamp the maximum passive
2510 * dwell time to be 98% of the beacon interval (minus
2511 * 2 * channel tune time) */
2512 passive = priv->beacon_int;
2513 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
2514 passive = IWL_PASSIVE_DWELL_BASE;
2515 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
2516 }
2517
2518 if (passive <= active)
2519 passive = active + 1;
2520
2521 return passive;
2522}
2523
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002524static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002525 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07002526 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002527 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07002528{
2529 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01002530 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002531 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002532 u16 passive_dwell = 0;
2533 u16 active_dwell = 0;
2534 int added, i;
2535
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07002536 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01002537 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07002538 return 0;
2539
Johannes Berg8318d782008-01-24 19:38:38 +01002540 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07002541
Johannes Berg8318d782008-01-24 19:38:38 +01002542 active_dwell = iwl4965_get_active_dwell_time(priv, band);
2543 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07002544
Johannes Berg8318d782008-01-24 19:38:38 +01002545 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02002546 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
2547 continue;
2548
Johannes Berg8318d782008-01-24 19:38:38 +01002549 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07002550
Assaf Krauss8622e702008-03-21 13:53:43 -07002551 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08002552 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07002553 if (!is_channel_valid(ch_info)) {
2554 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
2555 scan_ch->channel);
2556 continue;
2557 }
2558
2559 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01002560 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07002561 scan_ch->type = 0; /* passive */
2562 else
2563 scan_ch->type = 1; /* active */
2564
2565 if (scan_ch->type & 1)
2566 scan_ch->type |= (direct_mask << 1);
2567
Zhu Yib481de92007-09-25 17:54:57 -07002568 scan_ch->active_dwell = cpu_to_le16(active_dwell);
2569 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
2570
Ben Cahill9fbab512007-11-29 11:09:47 +08002571 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07002572 scan_ch->tpc.dsp_atten = 110;
2573 /* scan_pwr_info->tpc.dsp_atten; */
2574
2575 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01002576 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07002577 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
2578 else {
2579 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
2580 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08002581 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08002582 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07002583 */
2584 }
2585
2586 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
2587 scan_ch->channel,
2588 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
2589 (scan_ch->type & 1) ?
2590 active_dwell : passive_dwell);
2591
2592 scan_ch++;
2593 added++;
2594 }
2595
2596 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
2597 return added;
2598}
2599
Zhu Yib481de92007-09-25 17:54:57 -07002600/******************************************************************************
2601 *
2602 * uCode download functions
2603 *
2604 ******************************************************************************/
2605
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002606static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002607{
Tomas Winkler98c92212008-01-14 17:46:20 -08002608 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
2609 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
2610 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
2611 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
2612 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
2613 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07002614}
2615
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08002616static void iwl4965_nic_start(struct iwl_priv *priv)
2617{
2618 /* Remove all resets to allow NIC to operate */
2619 iwl_write32(priv, CSR_RESET, 0);
2620}
2621
2622
Zhu Yib481de92007-09-25 17:54:57 -07002623/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002624 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07002625 *
2626 * Copy into buffers for card to fetch via bus-mastering
2627 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002628static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002629{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002630 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002631 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002632 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08002633 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07002634 u8 *src;
2635 size_t len;
2636 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
2637
2638 /* Ask kernel firmware_class module to get the boot firmware off disk.
2639 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08002640 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
2641 if (ret < 0) {
2642 IWL_ERROR("%s firmware file req failed: Reason %d\n",
2643 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07002644 goto error;
2645 }
2646
2647 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
2648 name, ucode_raw->size);
2649
2650 /* Make sure that we got at least our header! */
2651 if (ucode_raw->size < sizeof(*ucode)) {
2652 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002653 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002654 goto err_release;
2655 }
2656
2657 /* Data from ucode file: header followed by uCode images */
2658 ucode = (void *)ucode_raw->data;
2659
2660 ver = le32_to_cpu(ucode->ver);
2661 inst_size = le32_to_cpu(ucode->inst_size);
2662 data_size = le32_to_cpu(ucode->data_size);
2663 init_size = le32_to_cpu(ucode->init_size);
2664 init_data_size = le32_to_cpu(ucode->init_data_size);
2665 boot_size = le32_to_cpu(ucode->boot_size);
2666
2667 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
2668 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
2669 inst_size);
2670 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
2671 data_size);
2672 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
2673 init_size);
2674 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
2675 init_data_size);
2676 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
2677 boot_size);
2678
2679 /* Verify size of file vs. image size info in file's header */
2680 if (ucode_raw->size < sizeof(*ucode) +
2681 inst_size + data_size + init_size +
2682 init_data_size + boot_size) {
2683
2684 IWL_DEBUG_INFO("uCode file size %d too small\n",
2685 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002686 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002687 goto err_release;
2688 }
2689
2690 /* Verify that uCode images will fit in card's SRAM */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002691 if (inst_size > priv->hw_params.max_inst_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08002692 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
2693 inst_size);
2694 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002695 goto err_release;
2696 }
2697
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002698 if (data_size > priv->hw_params.max_data_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08002699 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
2700 data_size);
2701 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002702 goto err_release;
2703 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002704 if (init_size > priv->hw_params.max_inst_size) {
Zhu Yib481de92007-09-25 17:54:57 -07002705 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08002706 ("uCode init instr len %d too large to fit in\n",
2707 init_size);
2708 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002709 goto err_release;
2710 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002711 if (init_data_size > priv->hw_params.max_data_size) {
Zhu Yib481de92007-09-25 17:54:57 -07002712 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08002713 ("uCode init data len %d too large to fit in\n",
2714 init_data_size);
2715 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002716 goto err_release;
2717 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002718 if (boot_size > priv->hw_params.max_bsm_size) {
Zhu Yib481de92007-09-25 17:54:57 -07002719 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08002720 ("uCode boot instr len %d too large to fit in\n",
2721 boot_size);
2722 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002723 goto err_release;
2724 }
2725
2726 /* Allocate ucode buffers for card's bus-master loading ... */
2727
2728 /* Runtime instructions and 2 copies of data:
2729 * 1) unmodified from disk
2730 * 2) backup cache for save/restore during power-downs */
2731 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002732 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07002733
2734 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002735 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07002736
2737 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002738 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07002739
2740 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08002741 if (init_size && init_data_size) {
2742 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002743 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07002744
Tomas Winkler90e759d2007-11-29 11:09:41 +08002745 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002746 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002747
2748 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2749 goto err_pci_alloc;
2750 }
Zhu Yib481de92007-09-25 17:54:57 -07002751
2752 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08002753 if (boot_size) {
2754 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002755 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07002756
Tomas Winkler90e759d2007-11-29 11:09:41 +08002757 if (!priv->ucode_boot.v_addr)
2758 goto err_pci_alloc;
2759 }
Zhu Yib481de92007-09-25 17:54:57 -07002760
2761 /* Copy images into buffers for card's bus-master reads ... */
2762
2763 /* Runtime instructions (first block of data in file) */
2764 src = &ucode->data[0];
2765 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002766 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002767 memcpy(priv->ucode_code.v_addr, src, len);
2768 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
2769 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2770
2771 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002772 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07002773 src = &ucode->data[inst_size];
2774 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002775 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002776 memcpy(priv->ucode_data.v_addr, src, len);
2777 memcpy(priv->ucode_data_backup.v_addr, src, len);
2778
2779 /* Initialization instructions (3rd block) */
2780 if (init_size) {
2781 src = &ucode->data[inst_size + data_size];
2782 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002783 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
2784 len);
Zhu Yib481de92007-09-25 17:54:57 -07002785 memcpy(priv->ucode_init.v_addr, src, len);
2786 }
2787
2788 /* Initialization data (4th block) */
2789 if (init_data_size) {
2790 src = &ucode->data[inst_size + data_size + init_size];
2791 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002792 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
2793 len);
Zhu Yib481de92007-09-25 17:54:57 -07002794 memcpy(priv->ucode_init_data.v_addr, src, len);
2795 }
2796
2797 /* Bootstrap instructions (5th block) */
2798 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
2799 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002800 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002801 memcpy(priv->ucode_boot.v_addr, src, len);
2802
2803 /* We have our copies now, allow OS release its copies */
2804 release_firmware(ucode_raw);
2805 return 0;
2806
2807 err_pci_alloc:
2808 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002809 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002810 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002811
2812 err_release:
2813 release_firmware(ucode_raw);
2814
2815 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08002816 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002817}
2818
Zhu Yib481de92007-09-25 17:54:57 -07002819/**
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002820 * iwl_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002821 * from protocol/runtime uCode (initialization uCode's
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002822 * Alive gets handled by iwl_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002823 */
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002824static void iwl_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002825{
Tomas Winkler57aab752008-04-14 21:16:03 -07002826 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002827
2828 IWL_DEBUG_INFO("Runtime Alive received.\n");
2829
2830 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2831 /* We had an error bringing up the hardware, so take it
2832 * all the way back down so we can try again */
2833 IWL_DEBUG_INFO("Alive failed.\n");
2834 goto restart;
2835 }
2836
2837 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2838 * This is a paranoid check, because we would not have gotten the
2839 * "runtime" alive if code weren't properly loaded. */
Emmanuel Grumbachb0692f22008-04-24 11:55:18 -07002840 if (iwl_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002841 /* Runtime instruction load was bad;
2842 * take it all the way back down so we can try again */
2843 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
2844 goto restart;
2845 }
2846
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002847 iwlcore_clear_stations_table(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07002848 ret = priv->cfg->ops->lib->alive_notify(priv);
2849 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07002850 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07002851 ret);
Zhu Yib481de92007-09-25 17:54:57 -07002852 goto restart;
2853 }
2854
Ben Cahill9fbab512007-11-29 11:09:47 +08002855 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002856 set_bit(STATUS_ALIVE, &priv->status);
2857
2858 /* Clear out the uCode error bit if it is set */
2859 clear_bit(STATUS_FW_ERROR, &priv->status);
2860
Tomas Winklerfee12472008-04-03 16:05:21 -07002861 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002862 return;
2863
Johannes Berg36d68252008-05-15 12:55:26 +02002864 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002865
2866 priv->active_rate = priv->rates_mask;
2867 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2868
Tomas Winkler3109ece2008-03-28 16:33:35 -07002869 if (iwl_is_associated(priv)) {
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08002870 struct iwl_rxon_cmd *active_rxon =
2871 (struct iwl_rxon_cmd *)&priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002872
2873 memcpy(&priv->staging_rxon, &priv->active_rxon,
2874 sizeof(priv->staging_rxon));
2875 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2876 } else {
2877 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002878 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002879 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2880 }
2881
Ben Cahill9fbab512007-11-29 11:09:47 +08002882 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002883 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002884
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002885 iwl_reset_run_time_calib(priv);
2886
Zhu Yib481de92007-09-25 17:54:57 -07002887 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002888 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002889
2890 /* At this point, the NIC is initialized and operational */
Zhu Yib481de92007-09-25 17:54:57 -07002891 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002892
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002893 iwl_leds_register(priv);
2894
Zhu Yib481de92007-09-25 17:54:57 -07002895 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002896 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002897 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002898
2899 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002900 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002901
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07002902 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Mohamed Abbas84363e62008-04-04 16:59:58 -07002903 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Zhu Yib481de92007-09-25 17:54:57 -07002904 return;
2905
2906 restart:
2907 queue_work(priv->workqueue, &priv->restart);
2908}
2909
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08002910static void iwl_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002911
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002912static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002913{
2914 unsigned long flags;
2915 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002916
2917 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
2918
Zhu Yib481de92007-09-25 17:54:57 -07002919 if (!exit_pending)
2920 set_bit(STATUS_EXIT_PENDING, &priv->status);
2921
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002922 iwl_leds_unregister(priv);
2923
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07002924 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
2925
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002926 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002927
2928 /* Unblock any waiting calls */
2929 wake_up_interruptible_all(&priv->wait_command_queue);
2930
Zhu Yib481de92007-09-25 17:54:57 -07002931 /* Wipe out the EXIT_PENDING status bit if we are not actually
2932 * exiting the module */
2933 if (!exit_pending)
2934 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2935
2936 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002937 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002938
2939 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002940 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002941 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002942 spin_unlock_irqrestore(&priv->lock, flags);
2943 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002944
2945 if (priv->mac80211_registered)
2946 ieee80211_stop_queues(priv->hw);
2947
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002948 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07002949 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07002950 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002951 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2952 STATUS_RF_KILL_HW |
2953 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2954 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08002955 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2956 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002957 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
2958 STATUS_IN_SUSPEND;
2959 goto exit;
2960 }
2961
2962 /* ...otherwise clear out all the status bits but the RF Kill and
2963 * SUSPEND bits and continue taking the NIC down. */
2964 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2965 STATUS_RF_KILL_HW |
2966 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2967 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08002968 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2969 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002970 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
2971 STATUS_IN_SUSPEND |
2972 test_bit(STATUS_FW_ERROR, &priv->status) <<
2973 STATUS_FW_ERROR;
2974
2975 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002976 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08002977 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07002978 spin_unlock_irqrestore(&priv->lock, flags);
2979
Tomas Winklerda1bc452008-05-29 16:35:00 +08002980 iwl_txq_ctx_stop(priv);
Tomas Winklerb3bbacb2008-05-29 16:35:01 +08002981 iwl_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002982
2983 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002984 if (!iwl_grab_nic_access(priv)) {
2985 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002986 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002987 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002988 }
2989 spin_unlock_irqrestore(&priv->lock, flags);
2990
2991 udelay(5);
2992
Tomas Winkler7f066102008-05-29 16:34:57 +08002993 /* FIXME: apm_ops.suspend(priv) */
2994 priv->cfg->ops->lib->apm_ops.reset(priv);
Ron Rindjunsky399f4902008-04-23 17:14:56 -07002995 priv->cfg->ops->lib->free_shared_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002996
2997 exit:
Tomas Winkler885ba202008-05-29 16:34:55 +08002998 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002999
3000 if (priv->ibss_beacon)
3001 dev_kfree_skb(priv->ibss_beacon);
3002 priv->ibss_beacon = NULL;
3003
3004 /* clear out any free frames */
Tomas Winklerfcab4232008-05-15 13:54:01 +08003005 iwl_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003006}
3007
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003008static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003009{
3010 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003011 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003012 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08003013
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003014 iwl_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003015}
3016
3017#define MAX_HW_RESTARTS 5
3018
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003019static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003020{
Tomas Winkler57aab752008-04-14 21:16:03 -07003021 int i;
3022 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003023
3024 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3025 IWL_WARNING("Exit pending; will not bring the NIC up\n");
3026 return -EIO;
3027 }
3028
3029 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
3030 IWL_WARNING("Radio disabled by SW RF kill (module "
3031 "parameter)\n");
Mohamed Abbasad97edd2008-03-28 16:21:06 -07003032 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08003033 return -ENODEV;
3034 }
3035
Reinette Chatree903fbd2008-01-30 22:05:15 -08003036 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
3037 IWL_ERROR("ucode not available for device bringup\n");
3038 return -EIO;
3039 }
3040
Zhu Yie655b9f2008-01-24 02:19:38 -08003041 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003042 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08003043 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3044 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3045 else {
3046 set_bit(STATUS_RF_KILL_HW, &priv->status);
3047 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07003048 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08003049 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
3050 return -ENODEV;
3051 }
Zhu Yib481de92007-09-25 17:54:57 -07003052 }
3053
Mohamed Abbasad97edd2008-03-28 16:21:06 -07003054 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003055 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07003056
Ron Rindjunsky399f4902008-04-23 17:14:56 -07003057 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
3058 if (ret) {
3059 IWL_ERROR("Unable to allocate shared memory\n");
3060 return ret;
3061 }
3062
Ron Rindjunsky1053d352008-05-05 10:22:43 +08003063 ret = iwl_hw_nic_init(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07003064 if (ret) {
3065 IWL_ERROR("Unable to init nic\n");
3066 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003067 }
3068
3069 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003070 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3071 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003072 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3073
3074 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003075 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003076 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003077
3078 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003079 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3080 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07003081
3082 /* Copy original ucode data image from disk into backup cache.
3083 * This will be used to initialize the on-board processor's
3084 * data SRAM for a clean start when the runtime program first loads. */
3085 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08003086 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07003087
Zhu Yie655b9f2008-01-24 02:19:38 -08003088 /* We return success when we resume from suspend and rf_kill is on. */
3089 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003090 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003091
3092 for (i = 0; i < MAX_HW_RESTARTS; i++) {
3093
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003094 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003095
3096 /* load bootstrap state machine,
3097 * load bootstrap program into processor's memory,
3098 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07003099 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003100
Tomas Winkler57aab752008-04-14 21:16:03 -07003101 if (ret) {
3102 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07003103 continue;
3104 }
3105
3106 /* start card; "initialize" will load runtime ucode */
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08003107 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003108
Zhu Yib481de92007-09-25 17:54:57 -07003109 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
3110
3111 return 0;
3112 }
3113
3114 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003115 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003116
3117 /* tried to restart and config the device for as long as our
3118 * patience could withstand */
3119 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
3120 return -EIO;
3121}
3122
3123
3124/*****************************************************************************
3125 *
3126 * Workqueue callbacks
3127 *
3128 *****************************************************************************/
3129
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08003130static void iwl_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003131{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003132 struct iwl_priv *priv =
3133 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07003134
3135 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3136 return;
3137
3138 mutex_lock(&priv->mutex);
Emmanuel Grumbachf3ccc082008-05-05 10:22:45 +08003139 priv->cfg->ops->lib->init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003140 mutex_unlock(&priv->mutex);
3141}
3142
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08003143static void iwl_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003144{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003145 struct iwl_priv *priv =
3146 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07003147
3148 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3149 return;
3150
3151 mutex_lock(&priv->mutex);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08003152 iwl_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003153 mutex_unlock(&priv->mutex);
3154}
3155
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003156static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003157{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003158 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07003159
3160 wake_up_interruptible(&priv->wait_command_queue);
3161
3162 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3163 return;
3164
3165 mutex_lock(&priv->mutex);
3166
Tomas Winklerfee12472008-04-03 16:05:21 -07003167 if (!iwl_is_rfkill(priv)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08003168 IWL_DEBUG(IWL_DL_RF_KILL,
Zhu Yib481de92007-09-25 17:54:57 -07003169 "HW and/or SW RF Kill no longer active, restarting "
3170 "device\n");
3171 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
3172 queue_work(priv->workqueue, &priv->restart);
3173 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07003174 /* make sure mac80211 stop sending Tx frame */
3175 if (priv->mac80211_registered)
3176 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07003177
3178 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
3179 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3180 "disabled by SW switch\n");
3181 else
3182 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
3183 "Kill switch must be turned off for "
3184 "wireless networking to work.\n");
3185 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07003186 iwl_rfkill_set_hw_state(priv);
3187
Zhu Yib481de92007-09-25 17:54:57 -07003188 mutex_unlock(&priv->mutex);
3189}
3190
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08003191static void iwl4965_bg_set_monitor(struct work_struct *work)
3192{
3193 struct iwl_priv *priv = container_of(work,
3194 struct iwl_priv, set_monitor);
3195
3196 IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
3197
3198 mutex_lock(&priv->mutex);
3199
3200 if (!iwl_is_ready(priv))
3201 IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
3202 else
3203 if (iwl4965_set_mode(priv, IEEE80211_IF_TYPE_MNTR) != 0)
3204 IWL_ERROR("iwl4965_set_mode() failed\n");
3205
3206 mutex_unlock(&priv->mutex);
3207}
3208
Zhu Yib481de92007-09-25 17:54:57 -07003209#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
3210
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003211static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003212{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003213 struct iwl_priv *priv =
3214 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07003215
3216 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3217 return;
3218
3219 mutex_lock(&priv->mutex);
3220 if (test_bit(STATUS_SCANNING, &priv->status) ||
3221 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08003222 IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
3223 "adapter (%dms)\n",
3224 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08003225
Zhu Yib481de92007-09-25 17:54:57 -07003226 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003227 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003228 }
3229 mutex_unlock(&priv->mutex);
3230}
3231
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003232static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003233{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003234 struct iwl_priv *priv =
3235 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07003236 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003237 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003238 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07003239 .meta.flags = CMD_SIZE_HUGE,
3240 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003241 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07003242 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02003243 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01003244 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02003245 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07003246 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003247
3248 conf = ieee80211_get_hw_conf(priv->hw);
3249
3250 mutex_lock(&priv->mutex);
3251
Tomas Winklerfee12472008-04-03 16:05:21 -07003252 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003253 IWL_WARNING("request scan called when driver not ready.\n");
3254 goto done;
3255 }
3256
3257 /* Make sure the scan wasn't cancelled before this queued work
3258 * was given the chance to run... */
3259 if (!test_bit(STATUS_SCANNING, &priv->status))
3260 goto done;
3261
3262 /* This should never be called or scheduled if there is currently
3263 * a scan active in the hardware. */
3264 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
3265 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
3266 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07003267 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07003268 goto done;
3269 }
3270
3271 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3272 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
3273 goto done;
3274 }
3275
3276 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3277 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
3278 goto done;
3279 }
3280
Tomas Winklerfee12472008-04-03 16:05:21 -07003281 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003282 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
3283 goto done;
3284 }
3285
3286 if (!test_bit(STATUS_READY, &priv->status)) {
3287 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
3288 goto done;
3289 }
3290
3291 if (!priv->scan_bands) {
3292 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
3293 goto done;
3294 }
3295
3296 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003297 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07003298 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
3299 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07003300 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07003301 goto done;
3302 }
3303 }
3304 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003305 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07003306
3307 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
3308 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
3309
Tomas Winkler3109ece2008-03-28 16:33:35 -07003310 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003311 u16 interval = 0;
3312 u32 extra;
3313 u32 suspend_time = 100;
3314 u32 scan_suspend_time = 100;
3315 unsigned long flags;
3316
3317 IWL_DEBUG_INFO("Scanning while associated...\n");
3318
3319 spin_lock_irqsave(&priv->lock, flags);
3320 interval = priv->beacon_int;
3321 spin_unlock_irqrestore(&priv->lock, flags);
3322
3323 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08003324 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07003325 if (!interval)
3326 interval = suspend_time;
3327
3328 extra = (suspend_time / interval) << 22;
3329 scan_suspend_time = (extra |
3330 ((suspend_time % interval) * 1024));
3331 scan->suspend_time = cpu_to_le32(scan_suspend_time);
3332 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
3333 scan_suspend_time, interval);
3334 }
3335
3336 /* We should add the ability for user to lock to PASSIVE ONLY */
3337 if (priv->one_direct_scan) {
3338 IWL_DEBUG_SCAN
3339 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003340 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07003341 priv->direct_ssid_len));
3342 scan->direct_scan[0].id = WLAN_EID_SSID;
3343 scan->direct_scan[0].len = priv->direct_ssid_len;
3344 memcpy(scan->direct_scan[0].ssid,
3345 priv->direct_ssid, priv->direct_ssid_len);
3346 direct_mask = 1;
Tomas Winkler3109ece2008-03-28 16:33:35 -07003347 } else if (!iwl_is_associated(priv) && priv->essid_len) {
Bill Moss786b4552008-04-17 16:03:40 -07003348 IWL_DEBUG_SCAN
3349 ("Kicking off one direct scan for '%s' when not associated\n",
3350 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07003351 scan->direct_scan[0].id = WLAN_EID_SSID;
3352 scan->direct_scan[0].len = priv->essid_len;
3353 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
3354 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07003355 } else {
Bill Moss786b4552008-04-17 16:03:40 -07003356 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003357 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07003358 }
Zhu Yib481de92007-09-25 17:54:57 -07003359
Zhu Yib481de92007-09-25 17:54:57 -07003360 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler5425e492008-04-15 16:01:38 -07003361 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07003362 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
3363
Zhu Yib481de92007-09-25 17:54:57 -07003364
3365 switch (priv->scan_bands) {
3366 case 2:
3367 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
3368 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003369 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07003370 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
3371
3372 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003373 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07003374 break;
3375
3376 case 1:
3377 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003378 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07003379 RATE_MCS_ANT_B_MSK);
3380 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01003381 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07003382 break;
3383
3384 default:
3385 IWL_WARNING("Invalid scan band count\n");
3386 goto done;
3387 }
3388
Tomas Winkler78330fd2008-02-06 02:37:18 +02003389 /* We don't build a direct scan probe request; the uCode will do
3390 * that based on the direct_mask added to each channel entry */
3391 cmd_len = iwl4965_fill_probe_req(priv, band,
3392 (struct ieee80211_mgmt *)scan->data,
3393 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
3394
3395 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07003396 /* select Rx chains */
3397
3398 /* Force use of chains B and C (0x6) for scan Rx.
3399 * Avoid A (0x1) because of its off-channel reception on A-band.
3400 * MIMO is not used here, but value is required to make uCode happy. */
3401 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
3402 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
3403 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
3404 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
3405
3406 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
3407 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
3408
Bill Moss786b4552008-04-17 16:03:40 -07003409 if (direct_mask)
Reinette Chatre26c0f032008-03-11 16:17:15 -07003410 scan->channel_count =
3411 iwl4965_get_channels_for_scan(
3412 priv, band, 1, /* active */
3413 direct_mask,
3414 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Bill Moss786b4552008-04-17 16:03:40 -07003415 else
Reinette Chatre26c0f032008-03-11 16:17:15 -07003416 scan->channel_count =
3417 iwl4965_get_channels_for_scan(
3418 priv, band, 0, /* passive */
3419 direct_mask,
3420 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07003421
Mohamed Abbas5da4b552008-04-21 15:41:51 -07003422 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
3423 RXON_FILTER_BCON_AWARE_MSK);
Zhu Yib481de92007-09-25 17:54:57 -07003424 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003425 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07003426 cmd.data = scan;
3427 scan->len = cpu_to_le16(cmd.len);
3428
3429 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07003430 ret = iwl_send_cmd_sync(priv, &cmd);
3431 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07003432 goto done;
3433
3434 queue_delayed_work(priv->workqueue, &priv->scan_check,
3435 IWL_SCAN_CHECK_WATCHDOG);
3436
3437 mutex_unlock(&priv->mutex);
3438 return;
3439
3440 done:
Ian Schram01ebd062007-10-25 17:15:22 +08003441 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07003442 queue_work(priv->workqueue, &priv->scan_completed);
3443 mutex_unlock(&priv->mutex);
3444}
3445
Emmanuel Grumbach16e727e2008-06-12 09:46:52 +08003446static void iwl_bg_run_time_calib_work(struct work_struct *work)
3447{
3448 struct iwl_priv *priv = container_of(work, struct iwl_priv,
3449 run_time_calib_work);
3450
3451 mutex_lock(&priv->mutex);
3452
3453 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
3454 test_bit(STATUS_SCANNING, &priv->status)) {
3455 mutex_unlock(&priv->mutex);
3456 return;
3457 }
3458
3459 if (priv->start_calib) {
3460 iwl_chain_noise_calibration(priv, &priv->statistics);
3461
3462 iwl_sensitivity_calibration(priv, &priv->statistics);
3463 }
3464
3465 mutex_unlock(&priv->mutex);
3466 return;
3467}
3468
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003469static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003470{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003471 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07003472
3473 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3474 return;
3475
3476 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003477 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003478 mutex_unlock(&priv->mutex);
3479}
3480
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003481static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003482{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003483 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07003484
3485 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3486 return;
3487
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003488 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003489 queue_work(priv->workqueue, &priv->up);
3490}
3491
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003492static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003493{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003494 struct iwl_priv *priv =
3495 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07003496
3497 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3498 return;
3499
3500 mutex_lock(&priv->mutex);
Tomas Winklera55360e2008-05-05 10:22:28 +08003501 iwl_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003502 mutex_unlock(&priv->mutex);
3503}
3504
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003505#define IWL_DELAY_NEXT_SCAN (HZ*2)
3506
Reinette Chatre508e32e2008-04-14 21:16:13 -07003507static void iwl4965_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003508{
Zhu Yib481de92007-09-25 17:54:57 -07003509 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07003510 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07003511 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07003512
3513 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
3514 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
3515 return;
3516 }
3517
Joe Perches0795af52007-10-03 17:59:30 -07003518 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
3519 priv->assoc_id,
3520 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07003521
3522
3523 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3524 return;
3525
Zhu Yib481de92007-09-25 17:54:57 -07003526
Reinette Chatre508e32e2008-04-14 21:16:13 -07003527 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08003528 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07003529
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003530 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08003531
Zhu Yib481de92007-09-25 17:54:57 -07003532 conf = ieee80211_get_hw_conf(priv->hw);
3533
3534 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003535 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003536
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003537 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
3538 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07003539 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07003540 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07003541 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07003542 IWL_WARNING("REPLY_RXON_TIMING failed - "
3543 "Attempting to continue.\n");
3544
3545 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
3546
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02003547 if (priv->current_ht_config.is_ht)
Tomas Winkler47c51962008-05-05 10:22:41 +08003548 iwl_set_rxon_ht(priv, &priv->current_ht_config);
Ron Rindjunsky4f85f5b2008-06-09 22:54:35 +03003549
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07003550 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003551 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
3552
3553 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
3554 priv->assoc_id, priv->beacon_int);
3555
3556 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3557 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
3558 else
3559 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
3560
3561 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
3562 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
3563 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
3564 else
3565 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3566
3567 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
3568 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3569
3570 }
3571
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003572 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003573
3574 switch (priv->iw_mode) {
3575 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003576 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003577 break;
3578
3579 case IEEE80211_IF_TYPE_IBSS:
3580
3581 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003582 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003583
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08003584 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
3585 iwl_rxon_add_station(priv, priv->bssid, 0);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003586 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
3587 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003588
3589 break;
3590
3591 default:
3592 IWL_ERROR("%s Should not be called in %d mode\n",
3593 __FUNCTION__, priv->iw_mode);
3594 break;
3595 }
3596
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003597 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003598
Zhu Yib481de92007-09-25 17:54:57 -07003599 /* Enable Rx differential gain and sensitivity calibrations */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07003600 iwl_chain_noise_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003601 priv->start_calib = 1;
Zhu Yib481de92007-09-25 17:54:57 -07003602
3603 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
3604 priv->assoc_station_added = 1;
3605
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003606 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08003607
Mohamed Abbas5da4b552008-04-21 15:41:51 -07003608 iwl_power_update_mode(priv, 0);
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003609 /* we have just associated, don't start scan too early */
3610 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Reinette Chatre508e32e2008-04-14 21:16:13 -07003611}
3612
3613
3614static void iwl4965_bg_post_associate(struct work_struct *data)
3615{
3616 struct iwl_priv *priv = container_of(data, struct iwl_priv,
3617 post_associate.work);
3618
3619 mutex_lock(&priv->mutex);
3620 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003621 mutex_unlock(&priv->mutex);
Reinette Chatre508e32e2008-04-14 21:16:13 -07003622
Zhu Yib481de92007-09-25 17:54:57 -07003623}
3624
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003625static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003626{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003627 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07003628
Tomas Winklerfee12472008-04-03 16:05:21 -07003629 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003630 return;
3631
3632 mutex_lock(&priv->mutex);
3633
3634 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003635 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003636
3637 mutex_unlock(&priv->mutex);
3638}
3639
Zhu Yi76bb77e2007-11-22 10:53:22 +08003640static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
3641
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003642static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003643{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003644 struct iwl_priv *priv =
3645 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07003646
Ester Kummerf3d67992008-05-06 11:05:12 +08003647 IWL_DEBUG(IWL_DL_SCAN, "SCAN complete scan\n");
Zhu Yib481de92007-09-25 17:54:57 -07003648
3649 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3650 return;
3651
Zhu Yia0646472007-12-20 14:10:01 +08003652 if (test_bit(STATUS_CONF_PENDING, &priv->status))
3653 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08003654
Zhu Yib481de92007-09-25 17:54:57 -07003655 ieee80211_scan_completed(priv->hw);
3656
3657 /* Since setting the TXPOWER may have been deferred while
3658 * performing the scan, fire one off */
3659 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003660 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003661 mutex_unlock(&priv->mutex);
3662}
3663
3664/*****************************************************************************
3665 *
3666 * mac80211 entry point functions
3667 *
3668 *****************************************************************************/
3669
Zhu Yi5a66926a2008-01-14 17:46:18 -08003670#define UCODE_READY_TIMEOUT (2 * HZ)
3671
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003672static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003673{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003674 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003675 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003676
3677 IWL_DEBUG_MAC80211("enter\n");
3678
Zhu Yi5a66926a2008-01-14 17:46:18 -08003679 if (pci_enable_device(priv->pci_dev)) {
3680 IWL_ERROR("Fail to pci_enable_device\n");
3681 return -ENODEV;
3682 }
3683 pci_restore_state(priv->pci_dev);
3684 pci_enable_msi(priv->pci_dev);
3685
3686 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
3687 DRV_NAME, priv);
3688 if (ret) {
3689 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
3690 goto out_disable_msi;
3691 }
3692
Zhu Yib481de92007-09-25 17:54:57 -07003693 /* we should be verifying the device is ready to be opened */
3694 mutex_lock(&priv->mutex);
3695
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08003696 memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
Zhu Yi5a66926a2008-01-14 17:46:18 -08003697 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3698 * ucode filename and max sizes are card-specific. */
3699
3700 if (!priv->ucode_code.len) {
3701 ret = iwl4965_read_ucode(priv);
3702 if (ret) {
3703 IWL_ERROR("Could not read microcode: %d\n", ret);
3704 mutex_unlock(&priv->mutex);
3705 goto out_release_irq;
3706 }
3707 }
3708
Zhu Yie655b9f2008-01-24 02:19:38 -08003709 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003710
Zhu Yib481de92007-09-25 17:54:57 -07003711 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003712
Zhu Yie655b9f2008-01-24 02:19:38 -08003713 if (ret)
3714 goto out_release_irq;
3715
3716 IWL_DEBUG_INFO("Start UP work done.\n");
3717
3718 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
3719 return 0;
3720
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08003721 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
Zhu Yi5a66926a2008-01-14 17:46:18 -08003722 * mac80211 will not be run successfully. */
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08003723 if (priv->ucode_type == UCODE_RT) {
3724 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3725 test_bit(STATUS_READY, &priv->status),
3726 UCODE_READY_TIMEOUT);
3727 if (!ret) {
3728 if (!test_bit(STATUS_READY, &priv->status)) {
3729 IWL_ERROR("START_ALIVE timeout after %dms.\n",
3730 jiffies_to_msecs(UCODE_READY_TIMEOUT));
3731 ret = -ETIMEDOUT;
3732 goto out_release_irq;
3733 }
Zhu Yi5a66926a2008-01-14 17:46:18 -08003734 }
Zhu Yi5a66926a2008-01-14 17:46:18 -08003735
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08003736 priv->is_open = 1;
3737 }
Zhu Yib481de92007-09-25 17:54:57 -07003738 IWL_DEBUG_MAC80211("leave\n");
3739 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003740
3741out_release_irq:
3742 free_irq(priv->pci_dev->irq, priv);
3743out_disable_msi:
3744 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08003745 pci_disable_device(priv->pci_dev);
3746 priv->is_open = 0;
3747 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08003748 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003749}
3750
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003751static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003752{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003753 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003754
3755 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08003756
Zhu Yie655b9f2008-01-24 02:19:38 -08003757 if (!priv->is_open) {
3758 IWL_DEBUG_MAC80211("leave - skip\n");
3759 return;
3760 }
3761
Zhu Yib481de92007-09-25 17:54:57 -07003762 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003763
Tomas Winklerfee12472008-04-03 16:05:21 -07003764 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08003765 /* stop mac, cancel any scan request and clear
3766 * RXON_FILTER_ASSOC_MSK BIT
3767 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08003768 mutex_lock(&priv->mutex);
3769 iwl4965_scan_cancel_timeout(priv, 100);
3770 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003771 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003772 }
3773
Zhu Yi5a66926a2008-01-14 17:46:18 -08003774 iwl4965_down(priv);
3775
3776 flush_workqueue(priv->workqueue);
3777 free_irq(priv->pci_dev->irq, priv);
3778 pci_disable_msi(priv->pci_dev);
3779 pci_save_state(priv->pci_dev);
3780 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08003781
Zhu Yib481de92007-09-25 17:54:57 -07003782 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003783}
3784
Johannes Berge039fa42008-05-15 12:55:29 +02003785static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003786{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003787 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003788
3789 IWL_DEBUG_MAC80211("enter\n");
3790
3791 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
3792 IWL_DEBUG_MAC80211("leave - monitor\n");
3793 return -1;
3794 }
3795
3796 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02003797 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07003798
Johannes Berge039fa42008-05-15 12:55:29 +02003799 if (iwl_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07003800 dev_kfree_skb_any(skb);
3801
3802 IWL_DEBUG_MAC80211("leave\n");
3803 return 0;
3804}
3805
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003806static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003807 struct ieee80211_if_init_conf *conf)
3808{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003809 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003810 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07003811 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07003812
Johannes Berg32bfd352007-12-19 01:31:26 +01003813 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07003814
Johannes Berg32bfd352007-12-19 01:31:26 +01003815 if (priv->vif) {
3816 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08003817 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07003818 }
3819
3820 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01003821 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07003822
3823 spin_unlock_irqrestore(&priv->lock, flags);
3824
3825 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02003826
3827 if (conf->mac_addr) {
3828 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
3829 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
3830 }
Zhu Yib481de92007-09-25 17:54:57 -07003831
Tomas Winklerfee12472008-04-03 16:05:21 -07003832 if (iwl_is_ready(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08003833 iwl4965_set_mode(priv, conf->type);
3834
Zhu Yib481de92007-09-25 17:54:57 -07003835 mutex_unlock(&priv->mutex);
3836
Zhu Yi5a66926a2008-01-14 17:46:18 -08003837 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003838 return 0;
3839}
3840
3841/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003842 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07003843 *
3844 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
3845 * be set inappropriately and the driver currently sets the hardware up to
3846 * use it whenever needed.
3847 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003848static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07003849{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003850 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003851 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07003852 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08003853 int ret = 0;
Tomas Winkler82a66bb2008-05-29 16:35:28 +08003854 u16 channel;
Zhu Yib481de92007-09-25 17:54:57 -07003855
3856 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01003857 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07003858
Zhu Yi12342c42007-12-20 11:27:32 +08003859 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
3860
Tomas Winklerfee12472008-04-03 16:05:21 -07003861 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003862 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08003863 ret = -EIO;
3864 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003865 }
3866
Assaf Krauss1ea87392008-03-18 14:57:50 -07003867 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07003868 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08003869 IWL_DEBUG_MAC80211("leave - scanning\n");
3870 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003871 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08003872 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003873 }
3874
Tomas Winkler82a66bb2008-05-29 16:35:28 +08003875 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
3876 ch_info = iwl_get_channel_info(priv, conf->channel->band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07003877 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07003878 IWL_DEBUG_MAC80211("leave - invalid channel\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08003879 ret = -EINVAL;
3880 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003881 }
3882
Tomas Winkler82a66bb2008-05-29 16:35:28 +08003883 spin_lock_irqsave(&priv->lock, flags);
3884
Tomas Winkler78330fd2008-02-06 02:37:18 +02003885 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07003886 * from any ht related info since 2.4 does not
3887 * support ht */
Tomas Winkler82a66bb2008-05-29 16:35:28 +08003888 if ((le16_to_cpu(priv->staging_rxon.channel) != channel)
Zhu Yib481de92007-09-25 17:54:57 -07003889#ifdef IEEE80211_CONF_CHANNEL_SWITCH
3890 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
3891#endif
3892 )
3893 priv->staging_rxon.flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003894
Tomas Winkler82a66bb2008-05-29 16:35:28 +08003895 iwl_set_rxon_channel(priv, conf->channel->band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07003896
Tomas Winkler82a66bb2008-05-29 16:35:28 +08003897 iwl_set_flags_for_band(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07003898
3899 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01003900 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07003901 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003902 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003903
3904 spin_unlock_irqrestore(&priv->lock, flags);
3905
3906#ifdef IEEE80211_CONF_CHANNEL_SWITCH
3907 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003908 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08003909 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003910 }
3911#endif
3912
Mohamed Abbasad97edd2008-03-28 16:21:06 -07003913 if (priv->cfg->ops->lib->radio_kill_sw)
3914 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07003915
3916 if (!conf->radio_enabled) {
3917 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08003918 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003919 }
3920
Tomas Winklerfee12472008-04-03 16:05:21 -07003921 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003922 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08003923 ret = -EIO;
3924 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003925 }
3926
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003927 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003928
3929 if (memcmp(&priv->active_rxon,
3930 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003931 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003932 else
3933 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
3934
3935 IWL_DEBUG_MAC80211("leave\n");
3936
Zhu Yia0646472007-12-20 14:10:01 +08003937out:
3938 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003939 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08003940 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003941}
3942
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003943static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003944{
Tomas Winkler857485c2008-03-21 13:53:44 -07003945 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003946
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003947 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003948 return;
3949
3950 /* The following should be done only at AP bring up */
3951 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
3952
3953 /* RXON - unassoc (to set timing command) */
3954 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003955 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003956
3957 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003958 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
3959 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07003960 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07003961 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07003962 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07003963 IWL_WARNING("REPLY_RXON_TIMING failed - "
3964 "Attempting to continue.\n");
3965
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07003966 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003967
3968 /* FIXME: what should be the assoc_id for AP? */
3969 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
3970 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3971 priv->staging_rxon.flags |=
3972 RXON_FLG_SHORT_PREAMBLE_MSK;
3973 else
3974 priv->staging_rxon.flags &=
3975 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3976
3977 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
3978 if (priv->assoc_capability &
3979 WLAN_CAPABILITY_SHORT_SLOT_TIME)
3980 priv->staging_rxon.flags |=
3981 RXON_FLG_SHORT_SLOT_MSK;
3982 else
3983 priv->staging_rxon.flags &=
3984 ~RXON_FLG_SHORT_SLOT_MSK;
3985
3986 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
3987 priv->staging_rxon.flags &=
3988 ~RXON_FLG_SHORT_SLOT_MSK;
3989 }
3990 /* restore RXON assoc */
3991 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003992 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003993 iwl4965_activate_qos(priv, 1);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08003994 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08003995 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003996 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003997
3998 /* FIXME - we need to add code here to detect a totally new
3999 * configuration, reset the AP, unassoc, rxon timing, assoc,
4000 * clear sta table, add BCAST sta... */
4001}
4002
Johannes Berg32bfd352007-12-19 01:31:26 +01004003static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
4004 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07004005 struct ieee80211_if_conf *conf)
4006{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004007 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07004008 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07004009 unsigned long flags;
4010 int rc;
4011
4012 if (conf == NULL)
4013 return -EIO;
4014
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08004015 if (priv->vif != vif) {
4016 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08004017 return 0;
4018 }
4019
Zhu Yib481de92007-09-25 17:54:57 -07004020 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
4021 (!conf->beacon || !conf->ssid_len)) {
4022 IWL_DEBUG_MAC80211
4023 ("Leaving in AP mode because HostAPD is not ready.\n");
4024 return 0;
4025 }
4026
Tomas Winklerfee12472008-04-03 16:05:21 -07004027 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08004028 return -EAGAIN;
4029
Zhu Yib481de92007-09-25 17:54:57 -07004030 mutex_lock(&priv->mutex);
4031
Zhu Yib481de92007-09-25 17:54:57 -07004032 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07004033 IWL_DEBUG_MAC80211("bssid: %s\n",
4034 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07004035
Johannes Berg4150c572007-09-17 01:29:23 -04004036/*
4037 * very dubious code was here; the probe filtering flag is never set:
4038 *
Zhu Yib481de92007-09-25 17:54:57 -07004039 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
4040 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04004041 */
Zhu Yib481de92007-09-25 17:54:57 -07004042
4043 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
4044 if (!conf->bssid) {
4045 conf->bssid = priv->mac_addr;
4046 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07004047 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
4048 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07004049 }
4050 if (priv->ibss_beacon)
4051 dev_kfree_skb(priv->ibss_beacon);
4052
4053 priv->ibss_beacon = conf->beacon;
4054 }
4055
Tomas Winklerfee12472008-04-03 16:05:21 -07004056 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08004057 goto done;
4058
Zhu Yib481de92007-09-25 17:54:57 -07004059 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
4060 !is_multicast_ether_addr(conf->bssid)) {
4061 /* If there is currently a HW scan going on in the background
4062 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004063 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07004064 IWL_WARNING("Aborted scan still in progress "
4065 "after 100ms\n");
4066 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
4067 mutex_unlock(&priv->mutex);
4068 return -EAGAIN;
4069 }
4070 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
4071
4072 /* TODO: Audit driver for usage of these members and see
4073 * if mac80211 deprecates them (priv->bssid looks like it
4074 * shouldn't be there, but I haven't scanned the IBSS code
4075 * to verify) - jpk */
4076 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
4077
4078 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004079 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004080 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004081 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004082 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08004083 iwl_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07004084 priv, priv->active_rxon.bssid_addr, 1);
4085 }
4086
4087 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004088 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07004089 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004090 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004091 }
4092
Mohamed Abbasfde35712007-11-29 11:10:15 +08004093 done:
Zhu Yib481de92007-09-25 17:54:57 -07004094 spin_lock_irqsave(&priv->lock, flags);
4095 if (!conf->ssid_len)
4096 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
4097 else
4098 memcpy(priv->essid, conf->ssid, conf->ssid_len);
4099
4100 priv->essid_len = conf->ssid_len;
4101 spin_unlock_irqrestore(&priv->lock, flags);
4102
4103 IWL_DEBUG_MAC80211("leave\n");
4104 mutex_unlock(&priv->mutex);
4105
4106 return 0;
4107}
4108
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004109static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04004110 unsigned int changed_flags,
4111 unsigned int *total_flags,
4112 int mc_count, struct dev_addr_list *mc_list)
4113{
4114 /*
4115 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004116 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04004117 */
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08004118 struct iwl_priv *priv = hw->priv;
4119 int new_flags = 0;
4120 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
4121 if (*total_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
4122 IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
4123 IEEE80211_IF_TYPE_MNTR,
4124 changed_flags, *total_flags);
4125 /* queue work 'cuz mac80211 is holding a lock which
4126 * prevents us from issuing (synchronous) f/w cmds */
4127 queue_work(priv->workqueue, &priv->set_monitor);
4128 new_flags &= FIF_PROMISC_IN_BSS |
4129 FIF_OTHER_BSS |
4130 FIF_ALLMULTI;
4131 }
4132 }
4133 *total_flags = new_flags;
Johannes Berg4150c572007-09-17 01:29:23 -04004134}
4135
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004136static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07004137 struct ieee80211_if_init_conf *conf)
4138{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004139 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004140
4141 IWL_DEBUG_MAC80211("enter\n");
4142
4143 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08004144
Tomas Winklerfee12472008-04-03 16:05:21 -07004145 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08004146 iwl4965_scan_cancel_timeout(priv, 100);
4147 cancel_delayed_work(&priv->post_associate);
4148 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4149 iwl4965_commit_rxon(priv);
4150 }
Johannes Berg32bfd352007-12-19 01:31:26 +01004151 if (priv->vif == conf->vif) {
4152 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07004153 memset(priv->bssid, 0, ETH_ALEN);
4154 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
4155 priv->essid_len = 0;
4156 }
4157 mutex_unlock(&priv->mutex);
4158
4159 IWL_DEBUG_MAC80211("leave\n");
4160
4161}
Johannes Berg471b3ef2007-12-28 14:32:58 +01004162
Tomas Winkler3109ece2008-03-28 16:33:35 -07004163#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01004164static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
4165 struct ieee80211_vif *vif,
4166 struct ieee80211_bss_conf *bss_conf,
4167 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02004168{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004169 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02004170
Tomas Winkler3109ece2008-03-28 16:33:35 -07004171 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
4172
Johannes Berg471b3ef2007-12-28 14:32:58 +01004173 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07004174 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
4175 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01004176 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02004177 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
4178 else
4179 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
4180 }
4181
Johannes Berg471b3ef2007-12-28 14:32:58 +01004182 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07004183 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01004184 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02004185 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
4186 else
4187 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
4188 }
4189
Tomas Winkler98952d52008-03-28 16:33:33 -07004190 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07004191 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07004192 iwl4965_ht_conf(priv, bss_conf);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07004193 iwl_set_rxon_chain(priv);
Tomas Winkler98952d52008-03-28 16:33:33 -07004194 }
4195
Johannes Berg471b3ef2007-12-28 14:32:58 +01004196 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07004197 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
Reinette Chatre508e32e2008-04-14 21:16:13 -07004198 /* This should never happen as this function should
4199 * never be called from interrupt context. */
4200 if (WARN_ON_ONCE(in_interrupt()))
4201 return;
Tomas Winkler3109ece2008-03-28 16:33:35 -07004202 if (bss_conf->assoc) {
4203 priv->assoc_id = bss_conf->aid;
4204 priv->beacon_int = bss_conf->beacon_int;
4205 priv->timestamp = bss_conf->timestamp;
4206 priv->assoc_capability = bss_conf->assoc_capability;
4207 priv->next_scan_jiffies = jiffies +
4208 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
Reinette Chatre508e32e2008-04-14 21:16:13 -07004209 mutex_lock(&priv->mutex);
4210 iwl4965_post_associate(priv);
4211 mutex_unlock(&priv->mutex);
Tomas Winkler3109ece2008-03-28 16:33:35 -07004212 } else {
4213 priv->assoc_id = 0;
4214 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
4215 }
4216 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
4217 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
Tomas Winkler7e8c5192008-04-15 16:01:43 -07004218 iwl_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01004219 }
4220
Tomas Winkler220173b2007-10-16 00:50:25 +02004221}
Zhu Yib481de92007-09-25 17:54:57 -07004222
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004223static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07004224{
4225 int rc = 0;
4226 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004227 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004228
4229 IWL_DEBUG_MAC80211("enter\n");
4230
mabbas052c4b92007-10-25 17:15:43 +08004231 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07004232 spin_lock_irqsave(&priv->lock, flags);
4233
Tomas Winklerfee12472008-04-03 16:05:21 -07004234 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004235 rc = -EIO;
4236 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
4237 goto out_unlock;
4238 }
4239
4240 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
4241 rc = -EIO;
4242 IWL_ERROR("ERROR: APs don't scan\n");
4243 goto out_unlock;
4244 }
4245
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004246 /* we don't schedule scan within next_scan_jiffies period */
4247 if (priv->next_scan_jiffies &&
4248 time_after(priv->next_scan_jiffies, jiffies)) {
4249 rc = -EAGAIN;
4250 goto out_unlock;
4251 }
Zhu Yib481de92007-09-25 17:54:57 -07004252 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004253 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
4254 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07004255 rc = -EAGAIN;
4256 goto out_unlock;
4257 }
4258 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004259 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004260 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07004261
4262 priv->one_direct_scan = 1;
4263 priv->direct_ssid_len = (u8)
4264 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
4265 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08004266 } else
4267 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004268
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004269 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004270
4271 IWL_DEBUG_MAC80211("leave\n");
4272
4273out_unlock:
4274 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08004275 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07004276
4277 return rc;
4278}
4279
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004280static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
4281 struct ieee80211_key_conf *keyconf, const u8 *addr,
4282 u32 iv32, u16 *phase1key)
4283{
4284 struct iwl_priv *priv = hw->priv;
4285 u8 sta_id = IWL_INVALID_STATION;
4286 unsigned long flags;
4287 __le16 key_flags = 0;
4288 int i;
4289 DECLARE_MAC_BUF(mac);
4290
4291 IWL_DEBUG_MAC80211("enter\n");
4292
Tomas Winkler947b13a2008-04-16 16:34:48 -07004293 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004294 if (sta_id == IWL_INVALID_STATION) {
4295 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
4296 print_mac(mac, addr));
4297 return;
4298 }
4299
4300 iwl4965_scan_cancel_timeout(priv, 100);
4301
4302 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
4303 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
4304 key_flags &= ~STA_KEY_FLG_INVALID;
4305
Tomas Winkler5425e492008-04-15 16:01:38 -07004306 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004307 key_flags |= STA_KEY_MULTICAST_MSK;
4308
4309 spin_lock_irqsave(&priv->sta_lock, flags);
4310
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004311 priv->stations[sta_id].sta.key.key_flags = key_flags;
4312 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
4313
4314 for (i = 0; i < 5; i++)
4315 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
4316 cpu_to_le16(phase1key[i]);
4317
4318 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
4319 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
4320
Tomas Winkler133636d2008-05-05 10:22:34 +08004321 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004322
4323 spin_unlock_irqrestore(&priv->sta_lock, flags);
4324
4325 IWL_DEBUG_MAC80211("leave\n");
4326}
4327
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004328static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07004329 const u8 *local_addr, const u8 *addr,
4330 struct ieee80211_key_conf *key)
4331{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004332 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07004333 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004334 int ret = 0;
4335 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004336 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004337
4338 IWL_DEBUG_MAC80211("enter\n");
4339
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004340 if (priv->hw_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07004341 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
4342 return -EOPNOTSUPP;
4343 }
4344
4345 if (is_zero_ether_addr(addr))
4346 /* only support pairwise keys */
4347 return -EOPNOTSUPP;
4348
Tomas Winkler947b13a2008-04-16 16:34:48 -07004349 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004350 if (sta_id == IWL_INVALID_STATION) {
4351 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
4352 print_mac(mac, addr));
4353 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004354
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004355 }
Zhu Yib481de92007-09-25 17:54:57 -07004356
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004357 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004358 iwl4965_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004359 mutex_unlock(&priv->mutex);
4360
4361 /* If we are getting WEP group key and we didn't receive any key mapping
4362 * so far, we are in legacy wep mode (group key only), otherwise we are
4363 * in 1X mode.
4364 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07004365 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004366 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
4367 if (cmd == SET_KEY)
4368 is_default_wep_key = !priv->key_mapping_key;
4369 else
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08004370 is_default_wep_key =
4371 (key->hw_key_idx == HW_KEY_DEFAULT);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004372 }
mabbas052c4b92007-10-25 17:15:43 +08004373
Zhu Yib481de92007-09-25 17:54:57 -07004374 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004375 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004376 if (is_default_wep_key)
4377 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004378 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07004379 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004380
4381 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07004382 break;
4383 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004384 if (is_default_wep_key)
4385 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004386 else
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07004387 ret = iwl_remove_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004388
4389 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07004390 break;
4391 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004392 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004393 }
4394
4395 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07004396
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004397 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004398}
4399
Johannes Berge100bb62008-04-30 18:51:21 +02004400static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07004401 const struct ieee80211_tx_queue_params *params)
4402{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004403 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004404 unsigned long flags;
4405 int q;
Zhu Yib481de92007-09-25 17:54:57 -07004406
4407 IWL_DEBUG_MAC80211("enter\n");
4408
Tomas Winklerfee12472008-04-03 16:05:21 -07004409 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004410 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4411 return -EIO;
4412 }
4413
4414 if (queue >= AC_NUM) {
4415 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
4416 return 0;
4417 }
4418
Zhu Yib481de92007-09-25 17:54:57 -07004419 if (!priv->qos_data.qos_enable) {
4420 priv->qos_data.qos_active = 0;
4421 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
4422 return 0;
4423 }
4424 q = AC_NUM - 1 - queue;
4425
4426 spin_lock_irqsave(&priv->lock, flags);
4427
4428 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
4429 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
4430 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
4431 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01004432 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07004433
4434 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
4435 priv->qos_data.qos_active = 1;
4436
4437 spin_unlock_irqrestore(&priv->lock, flags);
4438
4439 mutex_lock(&priv->mutex);
4440 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004441 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07004442 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004443 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004444
4445 mutex_unlock(&priv->mutex);
4446
Zhu Yib481de92007-09-25 17:54:57 -07004447 IWL_DEBUG_MAC80211("leave\n");
4448 return 0;
4449}
4450
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004451static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07004452 struct ieee80211_tx_queue_stats *stats)
4453{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004454 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004455 int i, avail;
Ron Rindjunsky16466902008-05-05 10:22:50 +08004456 struct iwl_tx_queue *txq;
Tomas Winkler443cfd42008-05-15 13:53:57 +08004457 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07004458 unsigned long flags;
4459
4460 IWL_DEBUG_MAC80211("enter\n");
4461
Tomas Winklerfee12472008-04-03 16:05:21 -07004462 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004463 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4464 return -EIO;
4465 }
4466
4467 spin_lock_irqsave(&priv->lock, flags);
4468
4469 for (i = 0; i < AC_NUM; i++) {
4470 txq = &priv->txq[i];
4471 q = &txq->q;
Tomas Winkler443cfd42008-05-15 13:53:57 +08004472 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07004473
Johannes Berg57ffc582008-04-29 17:18:59 +02004474 stats[i].len = q->n_window - avail;
4475 stats[i].limit = q->n_window - q->high_mark;
4476 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07004477
4478 }
4479 spin_unlock_irqrestore(&priv->lock, flags);
4480
4481 IWL_DEBUG_MAC80211("leave\n");
4482
4483 return 0;
4484}
4485
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004486static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07004487 struct ieee80211_low_level_stats *stats)
4488{
Ester Kummerbf403db2008-05-05 10:22:40 +08004489 struct iwl_priv *priv = hw->priv;
4490
4491 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004492 IWL_DEBUG_MAC80211("enter\n");
4493 IWL_DEBUG_MAC80211("leave\n");
4494
4495 return 0;
4496}
4497
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004498static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07004499{
Ester Kummerbf403db2008-05-05 10:22:40 +08004500 struct iwl_priv *priv;
4501
4502 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004503 IWL_DEBUG_MAC80211("enter\n");
4504 IWL_DEBUG_MAC80211("leave\n");
4505
4506 return 0;
4507}
4508
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004509static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07004510{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004511 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004512 unsigned long flags;
4513
4514 mutex_lock(&priv->mutex);
4515 IWL_DEBUG_MAC80211("enter\n");
4516
4517 priv->lq_mngr.lq_ready = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004518 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02004519 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07004520 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -07004521
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07004522 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004523
4524 cancel_delayed_work(&priv->post_associate);
4525
4526 spin_lock_irqsave(&priv->lock, flags);
4527 priv->assoc_id = 0;
4528 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004529 priv->assoc_station_added = 0;
4530
4531 /* new association get rid of ibss beacon skb */
4532 if (priv->ibss_beacon)
4533 dev_kfree_skb(priv->ibss_beacon);
4534
4535 priv->ibss_beacon = NULL;
4536
4537 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07004538 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004539 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
4540 priv->beacon_int = 0;
4541
4542 spin_unlock_irqrestore(&priv->lock, flags);
4543
Tomas Winklerfee12472008-04-03 16:05:21 -07004544 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08004545 IWL_DEBUG_MAC80211("leave - not ready\n");
4546 mutex_unlock(&priv->mutex);
4547 return;
4548 }
4549
mabbas052c4b92007-10-25 17:15:43 +08004550 /* we are restarting association process
4551 * clear RXON_FILTER_ASSOC_MSK bit
4552 */
4553 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004554 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08004555 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004556 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08004557 }
4558
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004559 iwl_power_update_mode(priv, 0);
4560
Zhu Yib481de92007-09-25 17:54:57 -07004561 /* Per mac80211.h: This is only used in IBSS mode... */
4562 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08004563
Zhu Yib481de92007-09-25 17:54:57 -07004564 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
4565 mutex_unlock(&priv->mutex);
4566 return;
4567 }
4568
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004569 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004570
4571 mutex_unlock(&priv->mutex);
4572
4573 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07004574}
4575
Johannes Berge039fa42008-05-15 12:55:29 +02004576static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07004577{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004578 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004579 unsigned long flags;
4580
4581 mutex_lock(&priv->mutex);
4582 IWL_DEBUG_MAC80211("enter\n");
4583
Tomas Winklerfee12472008-04-03 16:05:21 -07004584 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004585 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4586 mutex_unlock(&priv->mutex);
4587 return -EIO;
4588 }
4589
4590 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
4591 IWL_DEBUG_MAC80211("leave - not IBSS\n");
4592 mutex_unlock(&priv->mutex);
4593 return -EIO;
4594 }
4595
4596 spin_lock_irqsave(&priv->lock, flags);
4597
4598 if (priv->ibss_beacon)
4599 dev_kfree_skb(priv->ibss_beacon);
4600
4601 priv->ibss_beacon = skb;
4602
4603 priv->assoc_id = 0;
4604
4605 IWL_DEBUG_MAC80211("leave\n");
4606 spin_unlock_irqrestore(&priv->lock, flags);
4607
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07004608 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004609
4610 queue_work(priv->workqueue, &priv->post_associate.work);
4611
4612 mutex_unlock(&priv->mutex);
4613
4614 return 0;
4615}
4616
Zhu Yib481de92007-09-25 17:54:57 -07004617/*****************************************************************************
4618 *
4619 * sysfs attributes
4620 *
4621 *****************************************************************************/
4622
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004623#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004624
4625/*
4626 * The following adds a new attribute to the sysfs representation
4627 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
4628 * used for controlling the debug level.
4629 *
4630 * See the level definitions in iwl for details.
4631 */
4632
Ester Kummer8cf769c2008-05-06 11:05:11 +08004633static ssize_t show_debug_level(struct device *d,
4634 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07004635{
Ester Kummer8cf769c2008-05-06 11:05:11 +08004636 struct iwl_priv *priv = d->driver_data;
4637
4638 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07004639}
Ester Kummer8cf769c2008-05-06 11:05:11 +08004640static ssize_t store_debug_level(struct device *d,
4641 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07004642 const char *buf, size_t count)
4643{
Ester Kummer8cf769c2008-05-06 11:05:11 +08004644 struct iwl_priv *priv = d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004645 char *p = (char *)buf;
4646 u32 val;
4647
4648 val = simple_strtoul(p, &p, 0);
4649 if (p == buf)
4650 printk(KERN_INFO DRV_NAME
4651 ": %s is not in hex or decimal form.\n", buf);
4652 else
Ester Kummer8cf769c2008-05-06 11:05:11 +08004653 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07004654
4655 return strnlen(buf, count);
4656}
4657
Ester Kummer8cf769c2008-05-06 11:05:11 +08004658static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
4659 show_debug_level, store_debug_level);
4660
Zhu Yib481de92007-09-25 17:54:57 -07004661
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004662#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07004663
Zhu Yib481de92007-09-25 17:54:57 -07004664
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08004665static ssize_t show_version(struct device *d,
4666 struct device_attribute *attr, char *buf)
4667{
4668 struct iwl_priv *priv = d->driver_data;
Tomas Winkler885ba202008-05-29 16:34:55 +08004669 struct iwl_alive_resp *palive = &priv->card_alive;
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08004670
4671 if (palive->is_valid)
4672 return sprintf(buf, "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
4673 "fw type: 0x%01X 0x%01X\n",
4674 palive->ucode_major, palive->ucode_minor,
4675 palive->sw_rev[0], palive->sw_rev[1],
4676 palive->ver_type, palive->ver_subtype);
4677
4678 else
4679 return sprintf(buf, "fw not loaded\n");
4680}
4681
4682static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
4683
Zhu Yib481de92007-09-25 17:54:57 -07004684static ssize_t show_temperature(struct device *d,
4685 struct device_attribute *attr, char *buf)
4686{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004687 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004688
Tomas Winklerfee12472008-04-03 16:05:21 -07004689 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004690 return -EAGAIN;
4691
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004692 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07004693}
4694
4695static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
4696
4697static ssize_t show_rs_window(struct device *d,
4698 struct device_attribute *attr,
4699 char *buf)
4700{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004701 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004702 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07004703}
4704static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
4705
4706static ssize_t show_tx_power(struct device *d,
4707 struct device_attribute *attr, char *buf)
4708{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004709 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004710 return sprintf(buf, "%d\n", priv->user_txpower_limit);
4711}
4712
4713static ssize_t store_tx_power(struct device *d,
4714 struct device_attribute *attr,
4715 const char *buf, size_t count)
4716{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004717 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004718 char *p = (char *)buf;
4719 u32 val;
4720
4721 val = simple_strtoul(p, &p, 10);
4722 if (p == buf)
4723 printk(KERN_INFO DRV_NAME
4724 ": %s is not in decimal form.\n", buf);
4725 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004726 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07004727
4728 return count;
4729}
4730
4731static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
4732
4733static ssize_t show_flags(struct device *d,
4734 struct device_attribute *attr, char *buf)
4735{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004736 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004737
4738 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
4739}
4740
4741static ssize_t store_flags(struct device *d,
4742 struct device_attribute *attr,
4743 const char *buf, size_t count)
4744{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004745 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004746 u32 flags = simple_strtoul(buf, NULL, 0);
4747
4748 mutex_lock(&priv->mutex);
4749 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
4750 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004751 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07004752 IWL_WARNING("Could not cancel scan.\n");
4753 else {
4754 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
4755 flags);
4756 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004757 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004758 }
4759 }
4760 mutex_unlock(&priv->mutex);
4761
4762 return count;
4763}
4764
4765static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
4766
4767static ssize_t show_filter_flags(struct device *d,
4768 struct device_attribute *attr, char *buf)
4769{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004770 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004771
4772 return sprintf(buf, "0x%04X\n",
4773 le32_to_cpu(priv->active_rxon.filter_flags));
4774}
4775
4776static ssize_t store_filter_flags(struct device *d,
4777 struct device_attribute *attr,
4778 const char *buf, size_t count)
4779{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004780 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004781 u32 filter_flags = simple_strtoul(buf, NULL, 0);
4782
4783 mutex_lock(&priv->mutex);
4784 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
4785 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004786 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07004787 IWL_WARNING("Could not cancel scan.\n");
4788 else {
4789 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
4790 "0x%04X\n", filter_flags);
4791 priv->staging_rxon.filter_flags =
4792 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004793 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004794 }
4795 }
4796 mutex_unlock(&priv->mutex);
4797
4798 return count;
4799}
4800
4801static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
4802 store_filter_flags);
4803
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004804#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07004805
4806static ssize_t show_measurement(struct device *d,
4807 struct device_attribute *attr, char *buf)
4808{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004809 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004810 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07004811 u32 size = sizeof(measure_report), len = 0, ofs = 0;
4812 u8 *data = (u8 *) & measure_report;
4813 unsigned long flags;
4814
4815 spin_lock_irqsave(&priv->lock, flags);
4816 if (!(priv->measurement_status & MEASUREMENT_READY)) {
4817 spin_unlock_irqrestore(&priv->lock, flags);
4818 return 0;
4819 }
4820 memcpy(&measure_report, &priv->measure_report, size);
4821 priv->measurement_status = 0;
4822 spin_unlock_irqrestore(&priv->lock, flags);
4823
4824 while (size && (PAGE_SIZE - len)) {
4825 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4826 PAGE_SIZE - len, 1);
4827 len = strlen(buf);
4828 if (PAGE_SIZE - len)
4829 buf[len++] = '\n';
4830
4831 ofs += 16;
4832 size -= min(size, 16U);
4833 }
4834
4835 return len;
4836}
4837
4838static ssize_t store_measurement(struct device *d,
4839 struct device_attribute *attr,
4840 const char *buf, size_t count)
4841{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004842 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004843 struct ieee80211_measurement_params params = {
4844 .channel = le16_to_cpu(priv->active_rxon.channel),
4845 .start_time = cpu_to_le64(priv->last_tsf),
4846 .duration = cpu_to_le16(1),
4847 };
4848 u8 type = IWL_MEASURE_BASIC;
4849 u8 buffer[32];
4850 u8 channel;
4851
4852 if (count) {
4853 char *p = buffer;
4854 strncpy(buffer, buf, min(sizeof(buffer), count));
4855 channel = simple_strtoul(p, NULL, 0);
4856 if (channel)
4857 params.channel = channel;
4858
4859 p = buffer;
4860 while (*p && *p != ' ')
4861 p++;
4862 if (*p)
4863 type = simple_strtoul(p + 1, NULL, 0);
4864 }
4865
4866 IWL_DEBUG_INFO("Invoking measurement of type %d on "
4867 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004868 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07004869
4870 return count;
4871}
4872
4873static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
4874 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004875#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07004876
4877static ssize_t store_retry_rate(struct device *d,
4878 struct device_attribute *attr,
4879 const char *buf, size_t count)
4880{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004881 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004882
4883 priv->retry_rate = simple_strtoul(buf, NULL, 0);
4884 if (priv->retry_rate <= 0)
4885 priv->retry_rate = 1;
4886
4887 return count;
4888}
4889
4890static ssize_t show_retry_rate(struct device *d,
4891 struct device_attribute *attr, char *buf)
4892{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004893 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004894 return sprintf(buf, "%d", priv->retry_rate);
4895}
4896
4897static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
4898 store_retry_rate);
4899
4900static ssize_t store_power_level(struct device *d,
4901 struct device_attribute *attr,
4902 const char *buf, size_t count)
4903{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004904 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004905 int rc;
4906 int mode;
4907
4908 mode = simple_strtoul(buf, NULL, 0);
4909 mutex_lock(&priv->mutex);
4910
Tomas Winklerfee12472008-04-03 16:05:21 -07004911 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004912 rc = -EAGAIN;
4913 goto out;
4914 }
4915
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004916 rc = iwl_power_set_user_mode(priv, mode);
4917 if (rc) {
4918 IWL_DEBUG_MAC80211("failed setting power mode.\n");
4919 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004920 }
Zhu Yib481de92007-09-25 17:54:57 -07004921 rc = count;
4922
4923 out:
4924 mutex_unlock(&priv->mutex);
4925 return rc;
4926}
4927
4928#define MAX_WX_STRING 80
4929
4930/* Values are in microsecond */
4931static const s32 timeout_duration[] = {
4932 350000,
4933 250000,
4934 75000,
4935 37000,
4936 25000,
4937};
4938static const s32 period_duration[] = {
4939 400000,
4940 700000,
4941 1000000,
4942 1000000,
4943 1000000
4944};
4945
4946static ssize_t show_power_level(struct device *d,
4947 struct device_attribute *attr, char *buf)
4948{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004949 struct iwl_priv *priv = dev_get_drvdata(d);
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004950 int level = priv->power_data.power_mode;
Zhu Yib481de92007-09-25 17:54:57 -07004951 char *p = buf;
4952
4953 p += sprintf(p, "%d ", level);
4954 switch (level) {
4955 case IWL_POWER_MODE_CAM:
4956 case IWL_POWER_AC:
4957 p += sprintf(p, "(AC)");
4958 break;
4959 case IWL_POWER_BATTERY:
4960 p += sprintf(p, "(BATTERY)");
4961 break;
4962 default:
4963 p += sprintf(p,
4964 "(Timeout %dms, Period %dms)",
4965 timeout_duration[level - 1] / 1000,
4966 period_duration[level - 1] / 1000);
4967 }
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004968/*
Zhu Yib481de92007-09-25 17:54:57 -07004969 if (!(priv->power_mode & IWL_POWER_ENABLED))
4970 p += sprintf(p, " OFF\n");
4971 else
4972 p += sprintf(p, " \n");
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004973*/
4974 p += sprintf(p, " \n");
Zhu Yib481de92007-09-25 17:54:57 -07004975 return (p - buf + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004976}
4977
4978static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
4979 store_power_level);
4980
4981static ssize_t show_channels(struct device *d,
4982 struct device_attribute *attr, char *buf)
4983{
Johannes Berg8318d782008-01-24 19:38:38 +01004984 /* all this shit doesn't belong into sysfs anyway */
4985 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07004986}
4987
4988static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
4989
4990static ssize_t show_statistics(struct device *d,
4991 struct device_attribute *attr, char *buf)
4992{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004993 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004994 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07004995 u32 len = 0, ofs = 0;
4996 u8 *data = (u8 *) & priv->statistics;
4997 int rc = 0;
4998
Tomas Winklerfee12472008-04-03 16:05:21 -07004999 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005000 return -EAGAIN;
5001
5002 mutex_lock(&priv->mutex);
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07005003 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005004 mutex_unlock(&priv->mutex);
5005
5006 if (rc) {
5007 len = sprintf(buf,
5008 "Error sending statistics request: 0x%08X\n", rc);
5009 return len;
5010 }
5011
5012 while (size && (PAGE_SIZE - len)) {
5013 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
5014 PAGE_SIZE - len, 1);
5015 len = strlen(buf);
5016 if (PAGE_SIZE - len)
5017 buf[len++] = '\n';
5018
5019 ofs += 16;
5020 size -= min(size, 16U);
5021 }
5022
5023 return len;
5024}
5025
5026static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
5027
Zhu Yib481de92007-09-25 17:54:57 -07005028static ssize_t show_status(struct device *d,
5029 struct device_attribute *attr, char *buf)
5030{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005031 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07005032 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005033 return -EAGAIN;
5034 return sprintf(buf, "0x%08x\n", (int)priv->status);
5035}
5036
5037static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
5038
Zhu Yib481de92007-09-25 17:54:57 -07005039/*****************************************************************************
5040 *
5041 * driver setup and teardown
5042 *
5043 *****************************************************************************/
5044
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08005045static void iwl_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005046{
5047 priv->workqueue = create_workqueue(DRV_NAME);
5048
5049 init_waitqueue_head(&priv->wait_command_queue);
5050
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005051 INIT_WORK(&priv->up, iwl4965_bg_up);
5052 INIT_WORK(&priv->restart, iwl4965_bg_restart);
5053 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
5054 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
5055 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
5056 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
5057 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
5058 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08005059 INIT_WORK(&priv->set_monitor, iwl4965_bg_set_monitor);
Emmanuel Grumbach16e727e2008-06-12 09:46:52 +08005060 INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005061 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08005062 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
5063 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005064 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07005065
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08005066 if (priv->cfg->ops->lib->setup_deferred_work)
5067 priv->cfg->ops->lib->setup_deferred_work(priv);
5068
5069 init_timer(&priv->statistics_periodic);
5070 priv->statistics_periodic.data = (unsigned long)priv;
5071 priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
Zhu Yib481de92007-09-25 17:54:57 -07005072
5073 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005074 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07005075}
5076
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08005077static void iwl_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005078{
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08005079 if (priv->cfg->ops->lib->cancel_deferred_work)
5080 priv->cfg->ops->lib->cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005081
Joonwoo Park3ae6a052007-11-29 10:43:16 +09005082 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07005083 cancel_delayed_work(&priv->scan_check);
5084 cancel_delayed_work(&priv->alive_start);
5085 cancel_delayed_work(&priv->post_associate);
5086 cancel_work_sync(&priv->beacon_update);
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08005087 del_timer_sync(&priv->statistics_periodic);
Zhu Yib481de92007-09-25 17:54:57 -07005088}
5089
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005090static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07005091 &dev_attr_channels.attr,
Zhu Yib481de92007-09-25 17:54:57 -07005092 &dev_attr_flags.attr,
5093 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005094#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07005095 &dev_attr_measurement.attr,
5096#endif
5097 &dev_attr_power_level.attr,
5098 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07005099 &dev_attr_rs_window.attr,
5100 &dev_attr_statistics.attr,
5101 &dev_attr_status.attr,
5102 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07005103 &dev_attr_tx_power.attr,
Ester Kummer8cf769c2008-05-06 11:05:11 +08005104#ifdef CONFIG_IWLWIFI_DEBUG
5105 &dev_attr_debug_level.attr,
5106#endif
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08005107 &dev_attr_version.attr,
Zhu Yib481de92007-09-25 17:54:57 -07005108
5109 NULL
5110};
5111
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005112static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07005113 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005114 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07005115};
5116
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005117static struct ieee80211_ops iwl4965_hw_ops = {
5118 .tx = iwl4965_mac_tx,
5119 .start = iwl4965_mac_start,
5120 .stop = iwl4965_mac_stop,
5121 .add_interface = iwl4965_mac_add_interface,
5122 .remove_interface = iwl4965_mac_remove_interface,
5123 .config = iwl4965_mac_config,
5124 .config_interface = iwl4965_mac_config_interface,
5125 .configure_filter = iwl4965_configure_filter,
5126 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005127 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005128 .get_stats = iwl4965_mac_get_stats,
5129 .get_tx_stats = iwl4965_mac_get_tx_stats,
5130 .conf_tx = iwl4965_mac_conf_tx,
5131 .get_tsf = iwl4965_mac_get_tsf,
5132 .reset_tsf = iwl4965_mac_reset_tsf,
5133 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01005134 .bss_info_changed = iwl4965_bss_info_changed,
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02005135 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005136 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07005137};
5138
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005139static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07005140{
5141 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005142 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07005143 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08005144 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005145 unsigned long flags;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005146 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005147
Assaf Krauss316c30d2008-03-14 10:38:46 -07005148 /************************
5149 * 1. Allocating HW data
5150 ************************/
5151
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005152 /* Disabling hardware scan means that mac80211 will perform scans
5153 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07005154 if (cfg->mod_params->disable_hw_scan) {
Ester Kummerbf403db2008-05-05 10:22:40 +08005155 if (cfg->mod_params->debug & IWL_DL_INFO)
5156 dev_printk(KERN_DEBUG, &(pdev->dev),
5157 "Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005158 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005159 }
5160
Assaf Krauss1d0a0822008-03-14 10:38:48 -07005161 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
5162 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07005163 err = -ENOMEM;
5164 goto out;
5165 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07005166 priv = hw->priv;
5167 /* At this point both hw and priv are allocated. */
5168
Zhu Yib481de92007-09-25 17:54:57 -07005169 SET_IEEE80211_DEV(hw, &pdev->dev);
5170
5171 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08005172 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07005173 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07005174
Tomas Winkler0a6857e2008-03-12 16:58:49 -07005175#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08005176 priv->debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07005177 atomic_set(&priv->restrict_refcnt, 0);
5178#endif
Zhu Yib481de92007-09-25 17:54:57 -07005179
Assaf Krauss316c30d2008-03-14 10:38:46 -07005180 /**************************
5181 * 2. Initializing PCI bus
5182 **************************/
5183 if (pci_enable_device(pdev)) {
5184 err = -ENODEV;
5185 goto out_ieee80211_free_hw;
5186 }
5187
5188 pci_set_master(pdev);
5189
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07005190 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005191 if (!err)
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07005192 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
5193 if (err) {
5194 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
5195 if (!err)
5196 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
5197 /* both attempts failed: */
Assaf Krauss316c30d2008-03-14 10:38:46 -07005198 if (err) {
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07005199 printk(KERN_WARNING "%s: No suitable DMA available.\n",
5200 DRV_NAME);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005201 goto out_pci_disable_device;
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07005202 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07005203 }
5204
5205 err = pci_request_regions(pdev, DRV_NAME);
5206 if (err)
5207 goto out_pci_disable_device;
5208
5209 pci_set_drvdata(pdev, priv);
5210
5211 /* We disable the RETRY_TIMEOUT register (0x41) to keep
5212 * PCI Tx retries from interfering with C3 CPU state */
5213 pci_write_config_byte(pdev, 0x41, 0x00);
5214
5215 /***********************
5216 * 3. Read REV register
5217 ***********************/
5218 priv->hw_base = pci_iomap(pdev, 0, 0);
5219 if (!priv->hw_base) {
5220 err = -ENODEV;
5221 goto out_pci_release_regions;
5222 }
5223
5224 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
5225 (unsigned long long) pci_resource_len(pdev, 0));
5226 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
5227
Tomas Winklerb661c812008-04-23 17:14:54 -07005228 iwl_hw_detect(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005229 printk(KERN_INFO DRV_NAME
Tomas Winklerb661c812008-04-23 17:14:54 -07005230 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
5231 priv->cfg->name, priv->hw_rev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005232
Tomas Winkler91238712008-04-23 17:14:53 -07005233 /* amp init */
5234 err = priv->cfg->ops->lib->apm_ops.init(priv);
5235 if (err < 0) {
5236 IWL_DEBUG_INFO("Failed to init APMG\n");
5237 goto out_iounmap;
5238 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07005239 /*****************
5240 * 4. Read EEPROM
5241 *****************/
Assaf Krauss316c30d2008-03-14 10:38:46 -07005242 /* Read the EEPROM */
5243 err = iwl_eeprom_init(priv);
5244 if (err) {
5245 IWL_ERROR("Unable to init EEPROM\n");
5246 goto out_iounmap;
5247 }
Tomas Winkler8614f362008-04-23 17:14:55 -07005248 err = iwl_eeprom_check_version(priv);
5249 if (err)
5250 goto out_iounmap;
5251
Ron Rindjunsky02883012008-05-15 13:53:53 +08005252 /* extract MAC Address */
Assaf Krauss316c30d2008-03-14 10:38:46 -07005253 iwl_eeprom_get_mac(priv, priv->mac_addr);
5254 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
5255 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
5256
5257 /************************
5258 * 5. Setup HW constants
5259 ************************/
5260 /* Device-specific setup */
Tomas Winkler5425e492008-04-15 16:01:38 -07005261 if (priv->cfg->ops->lib->set_hw_params(priv)) {
5262 IWL_ERROR("failed to set hw parameters\n");
Tomas Winkler073d3f52008-04-21 15:41:52 -07005263 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07005264 }
5265
5266 /*******************
Tomas Winkler6ba87952008-05-15 13:54:17 +08005267 * 6. Setup priv
Assaf Krauss316c30d2008-03-14 10:38:46 -07005268 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07005269
Tomas Winkler6ba87952008-05-15 13:54:17 +08005270 err = iwl_init_drv(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005271 if (err)
Ron Rindjunsky399f4902008-04-23 17:14:56 -07005272 goto out_free_eeprom;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005273 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07005274
5275 /**********************************
5276 * 7. Initialize module parameters
5277 **********************************/
5278
5279 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07005280 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07005281 set_bit(STATUS_RF_KILL_SW, &priv->status);
5282 IWL_DEBUG_INFO("Radio disabled.\n");
5283 }
5284
Assaf Krauss316c30d2008-03-14 10:38:46 -07005285 /********************
5286 * 8. Setup services
5287 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005288 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005289 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005290 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005291
5292 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
5293 if (err) {
5294 IWL_ERROR("failed to create sysfs device attributes\n");
Tomas Winkler6ba87952008-05-15 13:54:17 +08005295 goto out_uninit_drv;
Assaf Krauss316c30d2008-03-14 10:38:46 -07005296 }
5297
Assaf Krauss316c30d2008-03-14 10:38:46 -07005298
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08005299 iwl_setup_deferred_work(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005300 iwl4965_setup_rx_handlers(priv);
5301
5302 /********************
5303 * 9. Conclude
5304 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08005305 pci_save_state(pdev);
5306 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07005307
Tomas Winkler6ba87952008-05-15 13:54:17 +08005308 /**********************************
5309 * 10. Setup and register mac80211
5310 **********************************/
5311
5312 err = iwl_setup_mac(priv);
5313 if (err)
5314 goto out_remove_sysfs;
5315
5316 err = iwl_dbgfs_register(priv, DRV_NAME);
5317 if (err)
5318 IWL_ERROR("failed to create debugfs files\n");
5319
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005320 /* notify iwlcore to init */
5321 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07005322 return 0;
5323
Assaf Krauss316c30d2008-03-14 10:38:46 -07005324 out_remove_sysfs:
5325 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Tomas Winkler6ba87952008-05-15 13:54:17 +08005326 out_uninit_drv:
5327 iwl_uninit_drv(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07005328 out_free_eeprom:
5329 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005330 out_iounmap:
5331 pci_iounmap(pdev, priv->hw_base);
5332 out_pci_release_regions:
5333 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005334 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07005335 out_pci_disable_device:
5336 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07005337 out_ieee80211_free_hw:
5338 ieee80211_free_hw(priv->hw);
5339 out:
5340 return err;
5341}
5342
Reinette Chatrec83dbf62008-03-21 13:53:41 -07005343static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07005344{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005345 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07005346 struct list_head *p, *q;
5347 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005348 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07005349
5350 if (!priv)
5351 return;
5352
5353 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
5354
Emmanuel Grumbach67249622008-05-29 16:35:26 +08005355 iwl_dbgfs_unregister(priv);
5356 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
5357
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07005358 if (priv->mac80211_registered) {
5359 ieee80211_unregister_hw(priv->hw);
5360 priv->mac80211_registered = 0;
5361 }
5362
Zhu Yib481de92007-09-25 17:54:57 -07005363 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08005364
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005365 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005366
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005367 /* make sure we flush any pending irq or
5368 * tasklet for the driver
5369 */
5370 spin_lock_irqsave(&priv->lock, flags);
5371 iwl4965_disable_interrupts(priv);
5372 spin_unlock_irqrestore(&priv->lock, flags);
5373
5374 iwl_synchronize_irq(priv);
5375
Zhu Yib481de92007-09-25 17:54:57 -07005376 /* Free MAC hash list for ADHOC */
5377 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
5378 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
5379 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005380 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07005381 }
5382 }
5383
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005384 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07005385
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005386 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005387
5388 if (priv->rxq.bd)
Tomas Winklera55360e2008-05-05 10:22:28 +08005389 iwl_rx_queue_free(priv, &priv->rxq);
Ron Rindjunsky1053d352008-05-05 10:22:43 +08005390 iwl_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005391
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005392 iwlcore_clear_stations_table(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07005393 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005394
Zhu Yib481de92007-09-25 17:54:57 -07005395
Mohamed Abbas948c1712007-10-25 17:15:45 +08005396 /*netif_stop_queue(dev); */
5397 flush_workqueue(priv->workqueue);
5398
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005399 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07005400 * priv->workqueue... so we can't take down the workqueue
5401 * until now... */
5402 destroy_workqueue(priv->workqueue);
5403 priv->workqueue = NULL;
5404
Zhu Yib481de92007-09-25 17:54:57 -07005405 pci_iounmap(pdev, priv->hw_base);
5406 pci_release_regions(pdev);
5407 pci_disable_device(pdev);
5408 pci_set_drvdata(pdev, NULL);
5409
Tomas Winkler6ba87952008-05-15 13:54:17 +08005410 iwl_uninit_drv(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005411
5412 if (priv->ibss_beacon)
5413 dev_kfree_skb(priv->ibss_beacon);
5414
5415 ieee80211_free_hw(priv->hw);
5416}
5417
5418#ifdef CONFIG_PM
5419
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005420static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07005421{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005422 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07005423
Zhu Yie655b9f2008-01-24 02:19:38 -08005424 if (priv->is_open) {
5425 set_bit(STATUS_IN_SUSPEND, &priv->status);
5426 iwl4965_mac_stop(priv->hw);
5427 priv->is_open = 1;
5428 }
Zhu Yib481de92007-09-25 17:54:57 -07005429
Zhu Yib481de92007-09-25 17:54:57 -07005430 pci_set_power_state(pdev, PCI_D3hot);
5431
Zhu Yib481de92007-09-25 17:54:57 -07005432 return 0;
5433}
5434
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005435static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07005436{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005437 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07005438
Zhu Yib481de92007-09-25 17:54:57 -07005439 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07005440
Zhu Yie655b9f2008-01-24 02:19:38 -08005441 if (priv->is_open)
5442 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005443
Zhu Yie655b9f2008-01-24 02:19:38 -08005444 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005445 return 0;
5446}
5447
5448#endif /* CONFIG_PM */
5449
5450/*****************************************************************************
5451 *
5452 * driver and module entry point
5453 *
5454 *****************************************************************************/
5455
Ron Rindjunskyfed90172008-04-15 16:01:41 -07005456/* Hardware specific file defines the PCI IDs table for that hardware module */
5457static struct pci_device_id iwl_hw_card_ids[] = {
5458 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
5459 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
Tomas Winkler5a6a2562008-04-24 11:55:23 -07005460#ifdef CONFIG_IWL5000
5461 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
5462 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
5463 {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
5464#endif /* CONFIG_IWL5000 */
Ron Rindjunskyfed90172008-04-15 16:01:41 -07005465 {0}
5466};
5467MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
5468
5469static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07005470 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07005471 .id_table = iwl_hw_card_ids,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005472 .probe = iwl4965_pci_probe,
5473 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07005474#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005475 .suspend = iwl4965_pci_suspend,
5476 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07005477#endif
5478};
5479
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005480static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07005481{
5482
5483 int ret;
5484 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
5485 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07005486
5487 ret = iwl4965_rate_control_register();
5488 if (ret) {
5489 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
5490 return ret;
5491 }
5492
Ron Rindjunskyfed90172008-04-15 16:01:41 -07005493 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07005494 if (ret) {
5495 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07005496 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07005497 }
Zhu Yib481de92007-09-25 17:54:57 -07005498
5499 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07005500
Reinette Chatre897e1cf2008-03-28 16:21:09 -07005501error_register:
5502 iwl4965_rate_control_unregister();
5503 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005504}
5505
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005506static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07005507{
Ron Rindjunskyfed90172008-04-15 16:01:41 -07005508 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07005509 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07005510}
5511
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005512module_exit(iwl4965_exit);
5513module_init(iwl4965_init);