blob: 13d53f3009d83f264cc3420b1cdab0c375b977a4 [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
Zhu Yib481de92007-09-25 17:54:57 -070090/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +080091 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -070092 * the functionality provided here
93 */
94
95/**************************************************************/
96
Zhu Yib481de92007-09-25 17:54:57 -070097
Zhu Yib481de92007-09-25 17:54:57 -070098
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -070099static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
100{
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800101 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700102
103 if (hw_decrypt)
104 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
105 else
106 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
107
108}
109
Zhu Yib481de92007-09-25 17:54:57 -0700110/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800111 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700112 *
113 * NOTE: This is really only useful during development and can eventually
114 * be #ifdef'd out once the driver is stable and folks aren't actively
115 * making changes
116 */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800117static int iwl4965_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700118{
119 int error = 0;
120 int counter = 1;
121
122 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
123 error |= le32_to_cpu(rxon->flags &
124 (RXON_FLG_TGJ_NARROW_BAND_MSK |
125 RXON_FLG_RADAR_DETECT_MSK));
126 if (error)
127 IWL_WARNING("check 24G fields %d | %d\n",
128 counter++, error);
129 } else {
130 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
131 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
132 if (error)
133 IWL_WARNING("check 52 fields %d | %d\n",
134 counter++, error);
135 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
136 if (error)
137 IWL_WARNING("check 52 CCK %d | %d\n",
138 counter++, error);
139 }
140 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
141 if (error)
142 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
143
144 /* make sure basic rates 6Mbps and 1Mbps are supported */
145 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
146 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
147 if (error)
148 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
149
150 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
151 if (error)
152 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
153
154 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
155 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
156 if (error)
157 IWL_WARNING("check CCK and short slot %d | %d\n",
158 counter++, error);
159
160 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
161 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
162 if (error)
163 IWL_WARNING("check CCK & auto detect %d | %d\n",
164 counter++, error);
165
166 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
167 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
168 if (error)
169 IWL_WARNING("check TGG and auto detect %d | %d\n",
170 counter++, error);
171
172 if (error)
173 IWL_WARNING("Tuning to channel %d\n",
174 le16_to_cpu(rxon->channel));
175
176 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800177 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700178 return -1;
179 }
180 return 0;
181}
182
183/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800184 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800185 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700186 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800187 * If the RXON structure is changing enough to require a new tune,
188 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
189 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700190 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700191static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700192{
193
194 /* These items are only settable from the full RXON command */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +0800195 if (!(iwl_is_associated(priv)) ||
Zhu Yib481de92007-09-25 17:54:57 -0700196 compare_ether_addr(priv->staging_rxon.bssid_addr,
197 priv->active_rxon.bssid_addr) ||
198 compare_ether_addr(priv->staging_rxon.node_addr,
199 priv->active_rxon.node_addr) ||
200 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
201 priv->active_rxon.wlap_bssid_addr) ||
202 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
203 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
204 (priv->staging_rxon.air_propagation !=
205 priv->active_rxon.air_propagation) ||
206 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
207 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
208 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
209 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
210 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
211 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
212 return 1;
213
214 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
215 * be updated with the RXON_ASSOC command -- however only some
216 * flag transitions are allowed using RXON_ASSOC */
217
218 /* Check if we are not switching bands */
219 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
220 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
221 return 1;
222
223 /* Check if we are switching association toggle */
224 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
225 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
226 return 1;
227
228 return 0;
229}
230
Zhu Yib481de92007-09-25 17:54:57 -0700231/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800232 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700233 *
Ian Schram01ebd062007-10-25 17:15:22 +0800234 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700235 * the active_rxon structure is updated with the new data. This
236 * function correctly transitions out of the RXON_ASSOC_MSK state if
237 * a HW tune is required based on the RXON structure changes.
238 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700239static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700240{
241 /* cast away the const for active_rxon in this function */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800242 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -0700243 DECLARE_MAC_BUF(mac);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800244 int ret;
245 bool new_assoc =
246 !!(priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700247
Tomas Winklerfee12472008-04-03 16:05:21 -0700248 if (!iwl_is_alive(priv))
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800249 return -EBUSY;
Zhu Yib481de92007-09-25 17:54:57 -0700250
251 /* always get timestamp with Rx frame */
252 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
253
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800254 ret = iwl4965_check_rxon_cmd(&priv->staging_rxon);
255 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700256 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
257 return -EINVAL;
258 }
259
260 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800261 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700262 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800263 if (!iwl4965_full_rxon_required(priv)) {
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800264 ret = iwl_send_rxon_assoc(priv);
265 if (ret) {
266 IWL_ERROR("Error setting RXON_ASSOC (%d)\n", ret);
267 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700268 }
269
270 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700271 return 0;
272 }
273
274 /* station table will be cleared */
275 priv->assoc_station_added = 0;
276
Zhu Yib481de92007-09-25 17:54:57 -0700277 /* If we are currently associated and the new config requires
278 * an RXON_ASSOC and the new config wants the associated mask enabled,
279 * we must clear the associated from the active configuration
280 * before we apply the new config */
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800281 if (iwl_is_associated(priv) && new_assoc) {
Zhu Yib481de92007-09-25 17:54:57 -0700282 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
283 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
284
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800285 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800286 sizeof(struct iwl_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700287 &priv->active_rxon);
288
289 /* If the mask clearing failed then we set
290 * active_rxon back to what it was previously */
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800291 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700292 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800293 IWL_ERROR("Error clearing ASSOC_MSK (%d)\n", ret);
294 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700295 }
Zhu Yib481de92007-09-25 17:54:57 -0700296 }
297
298 IWL_DEBUG_INFO("Sending RXON\n"
299 "* with%s RXON_FILTER_ASSOC_MSK\n"
300 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -0700301 "* bssid = %s\n",
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800302 (new_assoc ? "" : "out"),
Zhu Yib481de92007-09-25 17:54:57 -0700303 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -0700304 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -0700305
Ron Rindjunsky099b40b2008-04-21 15:41:53 -0700306 iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800307
308 /* Apply the new configuration
309 * RXON unassoc clears the station table in uCode, send it before
310 * we add the bcast station. If assoc bit is set, we will send RXON
311 * after having added the bcast and bssid station.
312 */
313 if (!new_assoc) {
314 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800315 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800316 if (ret) {
317 IWL_ERROR("Error setting new RXON (%d)\n", ret);
318 return ret;
319 }
320 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700321 }
322
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700323 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800324
Zhu Yib481de92007-09-25 17:54:57 -0700325 if (!priv->error_recovering)
326 priv->start_calib = 0;
327
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700328 iwl_init_sensitivity(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700329
Zhu Yib481de92007-09-25 17:54:57 -0700330 /* If we issue a new RXON command which required a tune then we must
331 * send a new TXPOWER command or we won't be able to Tx any frames */
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800332 ret = iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
333 if (ret) {
334 IWL_ERROR("Error sending TX power (%d)\n", ret);
335 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700336 }
337
338 /* Add the broadcast address so we can send broadcast frames */
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800339 if (iwl_rxon_add_station(priv, iwl_bcast_addr, 0) ==
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800340 IWL_INVALID_STATION) {
Zhu Yib481de92007-09-25 17:54:57 -0700341 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
342 return -EIO;
343 }
344
345 /* If we have set the ASSOC_MSK and we are in BSS mode then
346 * add the IWL_AP_ID to the station rate table */
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800347 if (new_assoc && (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800348 if (iwl_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700349 == IWL_INVALID_STATION) {
350 IWL_ERROR("Error adding AP address for transmit.\n");
351 return -EIO;
352 }
353 priv->assoc_station_added = 1;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700354 if (priv->default_wep_key &&
355 iwl_send_static_wepkey_cmd(priv, 0))
356 IWL_ERROR("Could not send WEP static key.\n");
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800357
358 /* Apply the new configuration
359 * RXON assoc doesn't clear the station table in uCode,
360 */
361 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
362 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
363 if (ret) {
364 IWL_ERROR("Error setting new RXON (%d)\n", ret);
365 return ret;
366 }
367 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700368 }
369
370 return 0;
371}
372
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700373void iwl4965_update_chain_flags(struct iwl_priv *priv)
374{
375
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -0700376 iwl_set_rxon_chain(priv);
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700377 iwl4965_commit_rxon(priv);
378}
379
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700380static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700381{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800382 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700383 .flags = 3,
384 .lead_time = 0xAA,
385 .max_kill = 1,
386 .kill_ack_mask = 0,
387 .kill_cts_mask = 0,
388 };
389
Tomas Winkler857485c2008-03-21 13:53:44 -0700390 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800391 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700392}
393
Tomas Winklerfcab4232008-05-15 13:54:01 +0800394static void iwl_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700395{
396 struct list_head *element;
397
398 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
399 priv->frames_count);
400
401 while (!list_empty(&priv->free_frames)) {
402 element = priv->free_frames.next;
403 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800404 kfree(list_entry(element, struct iwl_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700405 priv->frames_count--;
406 }
407
408 if (priv->frames_count) {
409 IWL_WARNING("%d frames still in use. Did we lose one?\n",
410 priv->frames_count);
411 priv->frames_count = 0;
412 }
413}
414
Tomas Winklerfcab4232008-05-15 13:54:01 +0800415static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700416{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800417 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700418 struct list_head *element;
419 if (list_empty(&priv->free_frames)) {
420 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
421 if (!frame) {
422 IWL_ERROR("Could not allocate frame!\n");
423 return NULL;
424 }
425
426 priv->frames_count++;
427 return frame;
428 }
429
430 element = priv->free_frames.next;
431 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800432 return list_entry(element, struct iwl_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700433}
434
Tomas Winklerfcab4232008-05-15 13:54:01 +0800435static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700436{
437 memset(frame, 0, sizeof(*frame));
438 list_add(&frame->list, &priv->free_frames);
439}
440
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700441unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700442 struct ieee80211_hdr *hdr,
443 const u8 *dest, int left)
444{
445
Tomas Winkler3109ece2008-03-28 16:33:35 -0700446 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -0700447 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
448 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
449 return 0;
450
451 if (priv->ibss_beacon->len > left)
452 return 0;
453
454 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
455
456 return priv->ibss_beacon->len;
457}
458
Guy Cohen39e88502008-04-23 17:14:57 -0700459static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700460{
Guy Cohen39e88502008-04-23 17:14:57 -0700461 int i;
462 int rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -0700463
Guy Cohen39e88502008-04-23 17:14:57 -0700464 /* Set rate mask*/
465 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
466 rate_mask = priv->active_rate_basic & 0xF;
467 else
468 rate_mask = priv->active_rate_basic & 0xFF0;
469
470 /* Find lowest valid rate */
Zhu Yib481de92007-09-25 17:54:57 -0700471 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800472 i = iwl_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -0700473 if (rate_mask & (1 << i))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800474 return iwl_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700475 }
476
Guy Cohen39e88502008-04-23 17:14:57 -0700477 /* No valid rate was found. Assign the lowest one */
478 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
479 return IWL_RATE_1M_PLCP;
480 else
481 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -0700482}
483
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700484static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700485{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800486 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700487 unsigned int frame_size;
488 int rc;
489 u8 rate;
490
Tomas Winklerfcab4232008-05-15 13:54:01 +0800491 frame = iwl_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700492
493 if (!frame) {
494 IWL_ERROR("Could not obtain free frame buffer for beacon "
495 "command.\n");
496 return -ENOMEM;
497 }
498
Guy Cohen39e88502008-04-23 17:14:57 -0700499 rate = iwl4965_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700500
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800501 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700502
Tomas Winkler857485c2008-03-21 13:53:44 -0700503 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700504 &frame->u.cmd[0]);
505
Tomas Winklerfcab4232008-05-15 13:54:01 +0800506 iwl_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700507
508 return rc;
509}
510
511/******************************************************************************
512 *
Zhu Yib481de92007-09-25 17:54:57 -0700513 * Misc. internal state and helper functions
514 *
515 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -0700516
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700517static void iwl4965_ht_conf(struct iwl_priv *priv,
518 struct ieee80211_bss_conf *bss_conf)
519{
520 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
521 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
522 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
523
524 IWL_DEBUG_MAC80211("enter: \n");
525
526 iwl_conf->is_ht = bss_conf->assoc_ht;
527
528 if (!iwl_conf->is_ht)
529 return;
530
531 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
532
533 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
Emmanuel Grumbacha9841012008-05-15 13:54:08 +0800534 iwl_conf->sgf |= HT_SHORT_GI_20MHZ;
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700535 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
Emmanuel Grumbacha9841012008-05-15 13:54:08 +0800536 iwl_conf->sgf |= HT_SHORT_GI_40MHZ;
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700537
538 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
539 iwl_conf->max_amsdu_size =
540 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
541
542 iwl_conf->supported_chan_width =
543 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
544 iwl_conf->extension_chan_offset =
545 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
546 /* If no above or below channel supplied disable FAT channel */
Emmanuel Grumbach963f5512008-06-12 09:47:00 +0800547 if (iwl_conf->extension_chan_offset != IEEE80211_HT_IE_CHA_SEC_ABOVE &&
548 iwl_conf->extension_chan_offset != IEEE80211_HT_IE_CHA_SEC_BELOW) {
549 iwl_conf->extension_chan_offset = IEEE80211_HT_IE_CHA_SEC_NONE;
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700550 iwl_conf->supported_chan_width = 0;
Emmanuel Grumbach963f5512008-06-12 09:47:00 +0800551 }
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700552
553 iwl_conf->tx_mimo_ps_mode =
554 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
555 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
556
557 iwl_conf->control_channel = ht_bss_conf->primary_channel;
558 iwl_conf->tx_chan_width =
559 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
560 iwl_conf->ht_protection =
561 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
562 iwl_conf->non_GF_STA_present =
563 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
564
565 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
566 IWL_DEBUG_MAC80211("leave\n");
567}
568
Zhu Yib481de92007-09-25 17:54:57 -0700569/*
570 * QoS support
571*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700572static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800573 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -0700574{
575
Tomas Winkler857485c2008-03-21 13:53:44 -0700576 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800577 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -0700578}
579
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700580static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -0700581{
582 unsigned long flags;
583
Zhu Yib481de92007-09-25 17:54:57 -0700584 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
585 return;
586
587 if (!priv->qos_data.qos_enable)
588 return;
589
590 spin_lock_irqsave(&priv->lock, flags);
591 priv->qos_data.def_qos_parm.qos_flags = 0;
592
593 if (priv->qos_data.qos_cap.q_AP.queue_request &&
594 !priv->qos_data.qos_cap.q_AP.txop_request)
595 priv->qos_data.def_qos_parm.qos_flags |=
596 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700597 if (priv->qos_data.qos_active)
598 priv->qos_data.def_qos_parm.qos_flags |=
599 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
600
Ron Rindjunskyfd105e72007-11-26 16:14:39 +0200601 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800602 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800603
Zhu Yib481de92007-09-25 17:54:57 -0700604 spin_unlock_irqrestore(&priv->lock, flags);
605
Tomas Winkler3109ece2008-03-28 16:33:35 -0700606 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800607 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
608 priv->qos_data.qos_active,
609 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700610
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800611 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -0700612 &(priv->qos_data.def_qos_parm));
613 }
614}
615
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700616int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -0700617{
618 /* Filter incoming packets to determine if they are targeted toward
619 * this network, discarding packets coming from ourselves */
620 switch (priv->iw_mode) {
621 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
622 /* packets from our adapter are dropped (echo) */
623 if (!compare_ether_addr(header->addr2, priv->mac_addr))
624 return 0;
625 /* {broad,multi}cast packets to our IBSS go through */
626 if (is_multicast_ether_addr(header->addr1))
627 return !compare_ether_addr(header->addr3, priv->bssid);
628 /* packets to our adapter go through */
629 return !compare_ether_addr(header->addr1, priv->mac_addr);
630 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
631 /* packets from our adapter are dropped (echo) */
632 if (!compare_ether_addr(header->addr3, priv->mac_addr))
633 return 0;
634 /* {broad,multi}cast packets to our BSS go through */
635 if (is_multicast_ether_addr(header->addr1))
636 return !compare_ether_addr(header->addr2, priv->bssid);
637 /* packets to our adapter go through */
638 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -0700639 default:
640 break;
Zhu Yib481de92007-09-25 17:54:57 -0700641 }
642
643 return 1;
644}
645
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700646static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700647{
648 /* Reset ieee stats */
649
650 /* We don't reset the net_device_stats (ieee->stats) on
651 * re-association */
652
653 priv->last_seq_num = -1;
654 priv->last_frag_num = -1;
655 priv->last_packet_time = 0;
656
Tomas Winkler2a421b92008-06-12 09:47:10 +0800657 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700658}
659
660#define MAX_UCODE_BEACON_INTERVAL 4096
661#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
662
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800663static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -0700664{
665 u16 new_val = 0;
666 u16 beacon_factor = 0;
667
668 beacon_factor =
669 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
670 / MAX_UCODE_BEACON_INTERVAL;
671 new_val = beacon_val / beacon_factor;
672
673 return cpu_to_le16(new_val);
674}
675
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700676static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700677{
678 u64 interval_tm_unit;
679 u64 tsf, result;
680 unsigned long flags;
681 struct ieee80211_conf *conf = NULL;
682 u16 beacon_int = 0;
683
684 conf = ieee80211_get_hw_conf(priv->hw);
685
686 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -0700687 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
688 priv->rxon_timing.timestamp.dw[0] =
689 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -0700690
691 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
692
Tomas Winkler3109ece2008-03-28 16:33:35 -0700693 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -0700694
695 beacon_int = priv->beacon_int;
696 spin_unlock_irqrestore(&priv->lock, flags);
697
698 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
699 if (beacon_int == 0) {
700 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
701 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
702 } else {
703 priv->rxon_timing.beacon_interval =
704 cpu_to_le16(beacon_int);
705 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800706 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -0700707 le16_to_cpu(priv->rxon_timing.beacon_interval));
708 }
709
710 priv->rxon_timing.atim_window = 0;
711 } else {
712 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800713 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -0700714 /* TODO: we need to get atim_window from upper stack
715 * for now we set to 0 */
716 priv->rxon_timing.atim_window = 0;
717 }
718
719 interval_tm_unit =
720 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
721 result = do_div(tsf, interval_tm_unit);
722 priv->rxon_timing.beacon_init_val =
723 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
724
725 IWL_DEBUG_ASSOC
726 ("beacon interval %d beacon timer %d beacon tim %d\n",
727 le16_to_cpu(priv->rxon_timing.beacon_interval),
728 le32_to_cpu(priv->rxon_timing.beacon_init_val),
729 le16_to_cpu(priv->rxon_timing.atim_window));
730}
731
Tomas Winkler82a66bb2008-05-29 16:35:28 +0800732static void iwl_set_flags_for_band(struct iwl_priv *priv,
733 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700734{
Johannes Berg8318d782008-01-24 19:38:38 +0100735 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -0700736 priv->staging_rxon.flags &=
737 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
738 | RXON_FLG_CCK_MSK);
739 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
740 } else {
Reinette Chatre508e32e2008-04-14 21:16:13 -0700741 /* Copied from iwl4965_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -0700742 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
743 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
744 else
745 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
746
747 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
748 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
749
750 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
751 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
752 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
753 }
754}
755
756/*
Ian Schram01ebd062007-10-25 17:15:22 +0800757 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -0700758 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700759static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700760{
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700761 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -0700762
763 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
764
765 switch (priv->iw_mode) {
766 case IEEE80211_IF_TYPE_AP:
767 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
768 break;
769
770 case IEEE80211_IF_TYPE_STA:
771 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
772 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
773 break;
774
775 case IEEE80211_IF_TYPE_IBSS:
776 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
777 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
778 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
779 RXON_FILTER_ACCEPT_GRP_MSK;
780 break;
781
782 case IEEE80211_IF_TYPE_MNTR:
783 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
784 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
785 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
786 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -0700787 default:
788 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
789 break;
Zhu Yib481de92007-09-25 17:54:57 -0700790 }
791
792#if 0
793 /* TODO: Figure out when short_preamble would be set and cache from
794 * that */
795 if (!hw_to_local(priv->hw)->short_preamble)
796 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
797 else
798 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
799#endif
800
Assaf Krauss8622e702008-03-21 13:53:43 -0700801 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700802 le16_to_cpu(priv->staging_rxon.channel));
803
804 if (!ch_info)
805 ch_info = &priv->channel_info[0];
806
807 /*
808 * in some case A channels are all non IBSS
809 * in this case force B/G channel
810 */
811 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
812 !(is_channel_ibss(ch_info)))
813 ch_info = &priv->channel_info[0];
814
815 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100816 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -0700817
Tomas Winkler82a66bb2008-05-29 16:35:28 +0800818 iwl_set_flags_for_band(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -0700819
820 priv->staging_rxon.ofdm_basic_rates =
821 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
822 priv->staging_rxon.cck_basic_rates =
823 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
824
825 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
826 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
827 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
828 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
829 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
830 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -0700831 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700832}
833
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700834static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -0700835{
Zhu Yib481de92007-09-25 17:54:57 -0700836 priv->iw_mode = mode;
837
Assaf Krauss398f9e72008-06-12 09:47:06 +0800838 /* init channel/phymode to values given at driver init */
839 iwl_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
840
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800841 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700842 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
843
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700844 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700845
Mohamed Abbasfde35712007-11-29 11:10:15 +0800846 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -0700847 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +0800848 return -EAGAIN;
849
850 cancel_delayed_work(&priv->scan_check);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800851 if (iwl_scan_cancel_timeout(priv, 100)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +0800852 IWL_WARNING("Aborted scan still in progress after 100ms\n");
853 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
854 return -EAGAIN;
855 }
856
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800857 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700858
859 return 0;
860}
861
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700862static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700863{
Johannes Berg8318d782008-01-24 19:38:38 +0100864 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700865 struct ieee80211_rate *rate;
866 int i;
867
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700868 hw = iwl_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -0800869 if (!hw) {
870 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
871 return;
872 }
Zhu Yib481de92007-09-25 17:54:57 -0700873
874 priv->active_rate = 0;
875 priv->active_rate_basic = 0;
876
Johannes Berg8318d782008-01-24 19:38:38 +0100877 for (i = 0; i < hw->n_bitrates; i++) {
878 rate = &(hw->bitrates[i]);
879 if (rate->hw_value < IWL_RATE_COUNT)
880 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -0700881 }
882
883 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
884 priv->active_rate, priv->active_rate_basic);
885
886 /*
887 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
888 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
889 * OFDM
890 */
891 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
892 priv->staging_rxon.cck_basic_rates =
893 ((priv->active_rate_basic &
894 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
895 else
896 priv->staging_rxon.cck_basic_rates =
897 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
898
899 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
900 priv->staging_rxon.ofdm_basic_rates =
901 ((priv->active_rate_basic &
902 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
903 IWL_FIRST_OFDM_RATE) & 0xFF;
904 else
905 priv->staging_rxon.ofdm_basic_rates =
906 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
907}
908
Zhu Yib481de92007-09-25 17:54:57 -0700909#define IWL_PACKET_RETRY_TIME HZ
910
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700911int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -0700912{
913 u16 sc = le16_to_cpu(header->seq_ctrl);
914 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
915 u16 frag = sc & IEEE80211_SCTL_FRAG;
916 u16 *last_seq, *last_frag;
917 unsigned long *last_time;
918
919 switch (priv->iw_mode) {
920 case IEEE80211_IF_TYPE_IBSS:{
921 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800922 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700923 u8 *mac = header->addr2;
924 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
925
926 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800927 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -0700928 if (!compare_ether_addr(entry->mac, mac))
929 break;
930 }
931 if (p == &priv->ibss_mac_hash[index]) {
932 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
933 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +0800934 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -0700935 return 0;
936 }
937 memcpy(entry->mac, mac, ETH_ALEN);
938 entry->seq_num = seq;
939 entry->frag_num = frag;
940 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +0800941 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -0700942 return 0;
943 }
944 last_seq = &entry->seq_num;
945 last_frag = &entry->frag_num;
946 last_time = &entry->packet_time;
947 break;
948 }
949 case IEEE80211_IF_TYPE_STA:
950 last_seq = &priv->last_seq_num;
951 last_frag = &priv->last_frag_num;
952 last_time = &priv->last_packet_time;
953 break;
954 default:
955 return 0;
956 }
957 if ((*last_seq == seq) &&
958 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
959 if (*last_frag == frag)
960 goto drop;
961 if (*last_frag + 1 != frag)
962 /* out-of-order fragment */
963 goto drop;
964 } else
965 *last_seq = seq;
966
967 *last_frag = frag;
968 *last_time = jiffies;
969 return 0;
970
971 drop:
972 return 1;
973}
974
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800975#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -0700976
977#include "iwl-spectrum.h"
978
979#define BEACON_TIME_MASK_LOW 0x00FFFFFF
980#define BEACON_TIME_MASK_HIGH 0xFF000000
981#define TIME_UNIT 1024
982
983/*
984 * extended beacon time format
985 * time in usec will be changed into a 32-bit value in 8:24 format
986 * the high 1 byte is the beacon counts
987 * the lower 3 bytes is the time in usec within one beacon interval
988 */
989
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800990static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -0700991{
992 u32 quot;
993 u32 rem;
994 u32 interval = beacon_interval * 1024;
995
996 if (!interval || !usec)
997 return 0;
998
999 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
1000 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
1001
1002 return (quot << 24) + rem;
1003}
1004
1005/* base is usually what we get from ucode with each received frame,
1006 * the same as HW timer counter counting down
1007 */
1008
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001009static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07001010{
1011 u32 base_low = base & BEACON_TIME_MASK_LOW;
1012 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
1013 u32 interval = beacon_interval * TIME_UNIT;
1014 u32 res = (base & BEACON_TIME_MASK_HIGH) +
1015 (addon & BEACON_TIME_MASK_HIGH);
1016
1017 if (base_low > addon_low)
1018 res += base_low - addon_low;
1019 else if (base_low < addon_low) {
1020 res += interval + base_low - addon_low;
1021 res += (1 << 24);
1022 } else
1023 res += (1 << 24);
1024
1025 return cpu_to_le32(res);
1026}
1027
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001028static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001029 struct ieee80211_measurement_params *params,
1030 u8 type)
1031{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001032 struct iwl4965_spectrum_cmd spectrum;
Tomas Winklerdb11d632008-05-05 10:22:33 +08001033 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07001034 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001035 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
1036 .data = (void *)&spectrum,
1037 .meta.flags = CMD_WANT_SKB,
1038 };
1039 u32 add_time = le64_to_cpu(params->start_time);
1040 int rc;
1041 int spectrum_resp_status;
1042 int duration = le16_to_cpu(params->duration);
1043
Tomas Winkler3109ece2008-03-28 16:33:35 -07001044 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001045 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001046 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07001047 le64_to_cpu(params->start_time) - priv->last_tsf,
1048 le16_to_cpu(priv->rxon_timing.beacon_interval));
1049
1050 memset(&spectrum, 0, sizeof(spectrum));
1051
1052 spectrum.channel_count = cpu_to_le16(1);
1053 spectrum.flags =
1054 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
1055 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
1056 cmd.len = sizeof(spectrum);
1057 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
1058
Tomas Winkler3109ece2008-03-28 16:33:35 -07001059 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001060 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001061 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07001062 add_time,
1063 le16_to_cpu(priv->rxon_timing.beacon_interval));
1064 else
1065 spectrum.start_time = 0;
1066
1067 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
1068 spectrum.channels[0].channel = params->channel;
1069 spectrum.channels[0].type = type;
1070 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
1071 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
1072 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
1073
Tomas Winkler857485c2008-03-21 13:53:44 -07001074 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001075 if (rc)
1076 return rc;
1077
Tomas Winklerdb11d632008-05-05 10:22:33 +08001078 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001079 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1080 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
1081 rc = -EIO;
1082 }
1083
1084 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
1085 switch (spectrum_resp_status) {
1086 case 0: /* Command will be handled */
1087 if (res->u.spectrum.id != 0xff) {
1088 IWL_DEBUG_INFO
1089 ("Replaced existing measurement: %d\n",
1090 res->u.spectrum.id);
1091 priv->measurement_status &= ~MEASUREMENT_READY;
1092 }
1093 priv->measurement_status |= MEASUREMENT_ACTIVE;
1094 rc = 0;
1095 break;
1096
1097 case 1: /* Command will not be handled */
1098 rc = -EAGAIN;
1099 break;
1100 }
1101
1102 dev_kfree_skb_any(cmd.meta.u.skb);
1103
1104 return rc;
1105}
1106#endif
1107
Zhu Yib481de92007-09-25 17:54:57 -07001108/******************************************************************************
1109 *
1110 * Generic RX handler implementations
1111 *
1112 ******************************************************************************/
Tomas Winkler885ba202008-05-29 16:34:55 +08001113static void iwl_rx_reply_alive(struct iwl_priv *priv,
1114 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001115{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001116 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Tomas Winkler885ba202008-05-29 16:34:55 +08001117 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07001118 struct delayed_work *pwork;
1119
1120 palive = &pkt->u.alive_frame;
1121
1122 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
1123 "0x%01X 0x%01X\n",
1124 palive->is_valid, palive->ver_type,
1125 palive->ver_subtype);
1126
1127 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
1128 IWL_DEBUG_INFO("Initialization Alive received.\n");
1129 memcpy(&priv->card_alive_init,
1130 &pkt->u.alive_frame,
Tomas Winkler885ba202008-05-29 16:34:55 +08001131 sizeof(struct iwl_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001132 pwork = &priv->init_alive_start;
1133 } else {
1134 IWL_DEBUG_INFO("Runtime Alive received.\n");
1135 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler885ba202008-05-29 16:34:55 +08001136 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001137 pwork = &priv->alive_start;
1138 }
1139
1140 /* We delay the ALIVE response by 5ms to
1141 * give the HW RF Kill time to activate... */
1142 if (palive->is_valid == UCODE_VALID_OK)
1143 queue_delayed_work(priv->workqueue, pwork,
1144 msecs_to_jiffies(5));
1145 else
1146 IWL_WARNING("uCode did not respond OK.\n");
1147}
1148
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001149static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001150 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001151{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001152 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001153
1154 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
1155 "seq 0x%04X ser 0x%08X\n",
1156 le32_to_cpu(pkt->u.err_resp.error_type),
1157 get_cmd_string(pkt->u.err_resp.cmd_id),
1158 pkt->u.err_resp.cmd_id,
1159 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1160 le32_to_cpu(pkt->u.err_resp.error_info));
1161}
1162
1163#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1164
Tomas Winklera55360e2008-05-05 10:22:28 +08001165static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001166{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001167 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08001168 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001169 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07001170 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
1171 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
1172 rxon->channel = csa->channel;
1173 priv->staging_rxon.channel = csa->channel;
1174}
1175
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001176static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001177 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001178{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001179#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Tomas Winklerdb11d632008-05-05 10:22:33 +08001180 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001181 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07001182
1183 if (!report->state) {
Ester Kummerf3d67992008-05-06 11:05:12 +08001184 IWL_DEBUG(IWL_DL_11H,
1185 "Spectrum Measure Notification: Start\n");
Zhu Yib481de92007-09-25 17:54:57 -07001186 return;
1187 }
1188
1189 memcpy(&priv->measure_report, report, sizeof(*report));
1190 priv->measurement_status |= MEASUREMENT_READY;
1191#endif
1192}
1193
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001194static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001195 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001196{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001197#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08001198 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001199 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07001200 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
1201 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1202#endif
1203}
1204
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001205static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001206 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001207{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001208 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001209 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
1210 "notification for %s:\n",
1211 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Ester Kummerbf403db2008-05-05 10:22:40 +08001212 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07001213}
1214
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001215static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07001216{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001217 struct iwl_priv *priv =
1218 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07001219 struct sk_buff *beacon;
1220
1221 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02001222 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07001223
1224 if (!beacon) {
1225 IWL_ERROR("update beacon failed\n");
1226 return;
1227 }
1228
1229 mutex_lock(&priv->mutex);
1230 /* new beacon skb is allocated every time; dispose previous.*/
1231 if (priv->ibss_beacon)
1232 dev_kfree_skb(priv->ibss_beacon);
1233
1234 priv->ibss_beacon = beacon;
1235 mutex_unlock(&priv->mutex);
1236
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001237 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001238}
1239
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08001240/**
1241 * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
1242 *
1243 * This callback is provided in order to send a statistics request.
1244 *
1245 * This timer function is continually reset to execute within
1246 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
1247 * was received. We need to ensure we receive the statistics in order
1248 * to update the temperature used for calibrating the TXPOWER.
1249 */
1250static void iwl4965_bg_statistics_periodic(unsigned long data)
1251{
1252 struct iwl_priv *priv = (struct iwl_priv *)data;
1253
1254 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1255 return;
1256
1257 iwl_send_statistics_request(priv, CMD_ASYNC);
1258}
1259
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001260static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001261 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001262{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001263#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08001264 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001265 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
Tomas Winklere7d326a2008-06-12 09:47:11 +08001266 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001267
1268 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
1269 "tsf %d %d rate %d\n",
Tomas Winkler25a65722008-06-12 09:47:07 +08001270 le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
Zhu Yib481de92007-09-25 17:54:57 -07001271 beacon->beacon_notify_hdr.failure_frame,
1272 le32_to_cpu(beacon->ibss_mgr_status),
1273 le32_to_cpu(beacon->high_tsf),
1274 le32_to_cpu(beacon->low_tsf), rate);
1275#endif
1276
1277 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
1278 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
1279 queue_work(priv->workqueue, &priv->beacon_update);
1280}
1281
Zhu Yib481de92007-09-25 17:54:57 -07001282/* Handle notification from uCode that card's power state is changing
1283 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001284static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001285 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001286{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001287 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001288 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
1289 unsigned long status = priv->status;
1290
1291 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
1292 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1293 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
1294
1295 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
1296 RF_CARD_DISABLED)) {
1297
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001298 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001299 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1300
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001301 if (!iwl_grab_nic_access(priv)) {
1302 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07001303 priv, HBUS_TARG_MBX_C,
1304 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1305
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001306 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001307 }
1308
1309 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001310 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07001311 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001312 if (!iwl_grab_nic_access(priv)) {
1313 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07001314 priv, HBUS_TARG_MBX_C,
1315 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1316
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001317 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001318 }
1319 }
1320
1321 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001322 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001323 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001324 iwl_read32(priv, CSR_UCODE_DRV_GP1);
1325 if (!iwl_grab_nic_access(priv))
1326 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001327 }
1328 }
1329
1330 if (flags & HW_CARD_DISABLED)
1331 set_bit(STATUS_RF_KILL_HW, &priv->status);
1332 else
1333 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1334
1335
1336 if (flags & SW_CARD_DISABLED)
1337 set_bit(STATUS_RF_KILL_SW, &priv->status);
1338 else
1339 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1340
1341 if (!(flags & RXON_CARD_DISABLED))
Tomas Winkler2a421b92008-06-12 09:47:10 +08001342 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001343
1344 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
1345 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
1346 (test_bit(STATUS_RF_KILL_SW, &status) !=
1347 test_bit(STATUS_RF_KILL_SW, &priv->status)))
1348 queue_work(priv->workqueue, &priv->rf_kill);
1349 else
1350 wake_up_interruptible(&priv->wait_command_queue);
1351}
1352
Ron Rindjunsky37a44212008-05-29 16:35:18 +08001353/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
1354 * This will be used later in iwl4965_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
1355static void iwl4965_rx_reply_rx_phy(struct iwl_priv *priv,
1356 struct iwl_rx_mem_buffer *rxb)
1357{
1358 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1359 priv->last_phy_res[0] = 1;
1360 memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
1361 sizeof(struct iwl4965_rx_phy_res));
1362}
1363
Zhu Yib481de92007-09-25 17:54:57 -07001364/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001365 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07001366 *
1367 * Setup the RX handlers for each of the reply types sent from the uCode
1368 * to the host.
1369 *
1370 * This function chains into the hardware specific files for them to setup
1371 * any hardware specific handlers as well.
1372 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001373static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001374{
Tomas Winkler885ba202008-05-29 16:34:55 +08001375 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001376 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
1377 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07001378 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001379 iwl4965_rx_spectrum_measure_notif;
1380 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001381 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001382 iwl4965_rx_pm_debug_statistics_notif;
1383 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001384
Ben Cahill9fbab512007-11-29 11:09:47 +08001385 /*
1386 * The same handler is used for both the REPLY to a discrete
1387 * statistics request from the host as well as for the periodic
1388 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07001389 */
Emmanuel Grumbach8f91aec2008-06-30 17:23:07 +08001390 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl_rx_statistics;
1391 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl_rx_statistics;
Tomas Winkler2a421b92008-06-12 09:47:10 +08001392
1393 iwl_setup_rx_scan_handlers(priv);
1394
Ron Rindjunsky37a44212008-05-29 16:35:18 +08001395 /* status change handler */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001396 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001397
Tomas Winklerc1354752008-05-29 16:35:04 +08001398 priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
1399 iwl_rx_missed_beacon_notif;
Ron Rindjunsky37a44212008-05-29 16:35:18 +08001400 /* Rx handlers */
1401 priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
1402 priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl4965_rx_reply_rx;
Ben Cahill9fbab512007-11-29 11:09:47 +08001403 /* Set up hardware specific Rx handlers */
Emmanuel Grumbachd4789ef2008-04-24 11:55:20 -07001404 priv->cfg->ops->lib->rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001405}
1406
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001407/*
1408 * this should be called while priv->lock is locked
1409*/
Tomas Winklera55360e2008-05-05 10:22:28 +08001410static void __iwl_rx_replenish(struct iwl_priv *priv)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001411{
Tomas Winklera55360e2008-05-05 10:22:28 +08001412 iwl_rx_allocate(priv);
1413 iwl_rx_queue_restock(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001414}
1415
1416
Zhu Yib481de92007-09-25 17:54:57 -07001417/**
Tomas Winklera55360e2008-05-05 10:22:28 +08001418 * iwl_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07001419 *
1420 * Uses the priv->rx_handlers callback function array to invoke
1421 * the appropriate handlers, including command responses,
1422 * frame-received notifications, and other notifications.
1423 */
Tomas Winklera55360e2008-05-05 10:22:28 +08001424void iwl_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001425{
Tomas Winklera55360e2008-05-05 10:22:28 +08001426 struct iwl_rx_mem_buffer *rxb;
Tomas Winklerdb11d632008-05-05 10:22:33 +08001427 struct iwl_rx_packet *pkt;
Tomas Winklera55360e2008-05-05 10:22:28 +08001428 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001429 u32 r, i;
1430 int reclaim;
1431 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001432 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08001433 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07001434
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001435 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1436 * buffer that the driver may process (last buffer filled by ucode). */
Ron Rindjunskyd67f5482008-05-05 10:22:49 +08001437 r = priv->cfg->ops->lib->shared_mem_rx_idx(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001438 i = rxq->read;
1439
1440 /* Rx interrupt, but nothing sent from uCode */
1441 if (i == r)
Ester Kummerf3d67992008-05-06 11:05:12 +08001442 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07001443
Tomas Winklera55360e2008-05-05 10:22:28 +08001444 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001445 fill_rx = 1;
1446
Zhu Yib481de92007-09-25 17:54:57 -07001447 while (i != r) {
1448 rxb = rxq->queue[i];
1449
Ben Cahill9fbab512007-11-29 11:09:47 +08001450 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07001451 * then a bug has been introduced in the queue refilling
1452 * routines -- catch it here */
1453 BUG_ON(rxb == NULL);
1454
1455 rxq->queue[i] = NULL;
1456
1457 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07001458 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07001459 PCI_DMA_FROMDEVICE);
Tomas Winklerdb11d632008-05-05 10:22:33 +08001460 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001461
1462 /* Reclaim a command buffer only if this packet is a response
1463 * to a (driver-originated) command.
1464 * If the packet (e.g. Rx frame) originated from uCode,
1465 * there is no command buffer to reclaim.
1466 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1467 * but apparently a few don't get set; catch them here. */
1468 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1469 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07001470 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08001471 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07001472 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1473 (pkt->hdr.cmd != REPLY_TX);
1474
1475 /* Based on type of command response or notification,
1476 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001477 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07001478 if (priv->rx_handlers[pkt->hdr.cmd]) {
Ester Kummerf3d67992008-05-06 11:05:12 +08001479 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d, %s, 0x%02x\n", r,
1480 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001481 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
1482 } else {
1483 /* No handling needed */
Ester Kummerf3d67992008-05-06 11:05:12 +08001484 IWL_DEBUG(IWL_DL_RX,
Zhu Yib481de92007-09-25 17:54:57 -07001485 "r %d i %d No handler needed for %s, 0x%02x\n",
1486 r, i, get_cmd_string(pkt->hdr.cmd),
1487 pkt->hdr.cmd);
1488 }
1489
1490 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001491 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07001492 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07001493 * as we reclaim the driver command queue */
1494 if (rxb && rxb->skb)
Tomas Winkler17b88922008-05-29 16:35:12 +08001495 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001496 else
1497 IWL_WARNING("Claim null rxb?\n");
1498 }
1499
1500 /* For now we just don't re-use anything. We can tweak this
1501 * later to try and re-use notification packets and SKBs that
1502 * fail to Rx correctly */
1503 if (rxb->skb != NULL) {
1504 priv->alloc_rxb_skb--;
1505 dev_kfree_skb_any(rxb->skb);
1506 rxb->skb = NULL;
1507 }
1508
1509 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07001510 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02001511 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07001512 spin_lock_irqsave(&rxq->lock, flags);
1513 list_add_tail(&rxb->list, &priv->rxq.rx_used);
1514 spin_unlock_irqrestore(&rxq->lock, flags);
1515 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001516 /* If there are a lot of unused frames,
1517 * restock the Rx queue so ucode wont assert. */
1518 if (fill_rx) {
1519 count++;
1520 if (count >= 8) {
1521 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08001522 __iwl_rx_replenish(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001523 count = 0;
1524 }
1525 }
Zhu Yib481de92007-09-25 17:54:57 -07001526 }
1527
1528 /* Backtrack one entry */
1529 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08001530 iwl_rx_queue_restock(priv);
1531}
Tomas Winklera55360e2008-05-05 10:22:28 +08001532
1533#define PERFECT_RSSI (-20) /* dBm */
1534#define WORST_RSSI (-95) /* dBm */
1535#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
1536
1537/* Calculate an indication of rx signal quality (a percentage, not dBm!).
1538 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
1539 * about formulas used below. */
1540int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
1541{
1542 int sig_qual;
1543 int degradation = PERFECT_RSSI - rssi_dbm;
1544
1545 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
1546 * as indicator; formula is (signal dbm - noise dbm).
1547 * SNR at or above 40 is a great signal (100%).
1548 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
1549 * Weakest usable signal is usually 10 - 15 dB SNR. */
1550 if (noise_dbm) {
1551 if (rssi_dbm - noise_dbm >= 40)
1552 return 100;
1553 else if (rssi_dbm < noise_dbm)
1554 return 0;
1555 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
1556
1557 /* Else use just the signal level.
1558 * This formula is a least squares fit of data points collected and
1559 * compared with a reference system that had a percentage (%) display
1560 * for signal quality. */
1561 } else
1562 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
1563 (15 * RSSI_RANGE + 62 * degradation)) /
1564 (RSSI_RANGE * RSSI_RANGE);
1565
1566 if (sig_qual > 100)
1567 sig_qual = 100;
1568 else if (sig_qual < 1)
1569 sig_qual = 0;
1570
1571 return sig_qual;
Zhu Yib481de92007-09-25 17:54:57 -07001572}
1573
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001574#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001575static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001576{
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08001577 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07001578 DECLARE_MAC_BUF(mac);
1579
Zhu Yib481de92007-09-25 17:54:57 -07001580 IWL_DEBUG_RADIO("RX CONFIG:\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08001581 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07001582 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
1583 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
1584 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
1585 le32_to_cpu(rxon->filter_flags));
1586 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
1587 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
1588 rxon->ofdm_basic_rates);
1589 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07001590 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
1591 print_mac(mac, rxon->node_addr));
1592 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
1593 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07001594 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
1595}
1596#endif
1597
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001598static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001599{
1600 IWL_DEBUG_ISR("Enabling interrupts\n");
1601 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001602 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07001603}
1604
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001605/* call this function to flush any scheduled tasklet */
1606static inline void iwl_synchronize_irq(struct iwl_priv *priv)
1607{
1608 /* wait to make sure we flush pedding tasklet*/
1609 synchronize_irq(priv->pci_dev->irq);
1610 tasklet_kill(&priv->irq_tasklet);
1611}
1612
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001613static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001614{
1615 clear_bit(STATUS_INT_ENABLED, &priv->status);
1616
1617 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001618 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07001619
1620 /* acknowledge/clear/reset any interrupts still pending
1621 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001622 iwl_write32(priv, CSR_INT, 0xffffffff);
1623 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07001624 IWL_DEBUG_ISR("Disabled interrupts\n");
1625}
1626
Zhu Yib481de92007-09-25 17:54:57 -07001627
Zhu Yib481de92007-09-25 17:54:57 -07001628/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001629 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07001630 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001631static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001632{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001633 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07001634 set_bit(STATUS_FW_ERROR, &priv->status);
1635
1636 /* Cancel currently queued command. */
1637 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
1638
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001639#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001640 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Ester Kummerede0cba2008-05-29 16:34:46 +08001641 iwl_dump_nic_error_log(priv);
Ester Kummer189a2b52008-05-15 13:54:18 +08001642 iwl_dump_nic_event_log(priv);
Ester Kummerbf403db2008-05-05 10:22:40 +08001643 iwl4965_print_rx_config_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001644 }
1645#endif
1646
1647 wake_up_interruptible(&priv->wait_command_queue);
1648
1649 /* Keep the restart process from trying to send host
1650 * commands by clearing the INIT status bit */
1651 clear_bit(STATUS_READY, &priv->status);
1652
1653 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08001654 IWL_DEBUG(IWL_DL_FW_ERRORS,
Zhu Yib481de92007-09-25 17:54:57 -07001655 "Restarting adapter due to uCode error.\n");
1656
Tomas Winkler3109ece2008-03-28 16:33:35 -07001657 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001658 memcpy(&priv->recovery_rxon, &priv->active_rxon,
1659 sizeof(priv->recovery_rxon));
1660 priv->error_recovering = 1;
1661 }
Ester Kummer3a1081e2008-05-06 11:05:14 +08001662 if (priv->cfg->mod_params->restart_fw)
1663 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07001664 }
1665}
1666
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001667static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001668{
1669 unsigned long flags;
1670
1671 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
1672 sizeof(priv->staging_rxon));
1673 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001674 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001675
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001676 iwl_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07001677
1678 spin_lock_irqsave(&priv->lock, flags);
1679 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
1680 priv->error_recovering = 0;
1681 spin_unlock_irqrestore(&priv->lock, flags);
1682}
1683
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001684static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001685{
1686 u32 inta, handled = 0;
1687 u32 inta_fh;
1688 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001689#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001690 u32 inta_mask;
1691#endif
1692
1693 spin_lock_irqsave(&priv->lock, flags);
1694
1695 /* Ack/clear/reset pending uCode interrupts.
1696 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1697 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001698 inta = iwl_read32(priv, CSR_INT);
1699 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001700
1701 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1702 * Any new interrupts that happen after this, either while we're
1703 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001704 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1705 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001706
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001707#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001708 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001709 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001710 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07001711 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1712 inta, inta_mask, inta_fh);
1713 }
1714#endif
1715
1716 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1717 * atomic, make sure that inta covers all the interrupts that
1718 * we've discovered, even if FH interrupt came in just after
1719 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001720 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001721 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001722 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001723 inta |= CSR_INT_BIT_FH_TX;
1724
1725 /* Now service all interrupt bits discovered above. */
1726 if (inta & CSR_INT_BIT_HW_ERR) {
1727 IWL_ERROR("Microcode HW error detected. Restarting.\n");
1728
1729 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001730 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001731
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001732 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001733
1734 handled |= CSR_INT_BIT_HW_ERR;
1735
1736 spin_unlock_irqrestore(&priv->lock, flags);
1737
1738 return;
1739 }
1740
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001741#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001742 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001743 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001744 if (inta & CSR_INT_BIT_SCD)
1745 IWL_DEBUG_ISR("Scheduler finished to transmit "
1746 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001747
1748 /* Alive notification via Rx interrupt will do the real work */
1749 if (inta & CSR_INT_BIT_ALIVE)
1750 IWL_DEBUG_ISR("Alive interrupt\n");
1751 }
1752#endif
1753 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001754 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001755
Ben Cahill9fbab512007-11-29 11:09:47 +08001756 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07001757 if (inta & CSR_INT_BIT_RF_KILL) {
1758 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001759 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07001760 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
1761 hw_rf_kill = 1;
1762
Ester Kummerf3d67992008-05-06 11:05:12 +08001763 IWL_DEBUG(IWL_DL_RF_KILL, "RF_KILL bit toggled to %s.\n",
Zhu Yib481de92007-09-25 17:54:57 -07001764 hw_rf_kill ? "disable radio":"enable radio");
1765
1766 /* Queue restart only if RF_KILL switch was set to "kill"
1767 * when we loaded driver, and is now set to "enable".
1768 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08001769 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08001770 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
1771 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07001772 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08001773 }
Zhu Yib481de92007-09-25 17:54:57 -07001774
1775 handled |= CSR_INT_BIT_RF_KILL;
1776 }
1777
Ben Cahill9fbab512007-11-29 11:09:47 +08001778 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07001779 if (inta & CSR_INT_BIT_CT_KILL) {
1780 IWL_ERROR("Microcode CT kill error detected.\n");
1781 handled |= CSR_INT_BIT_CT_KILL;
1782 }
1783
1784 /* Error detected by uCode */
1785 if (inta & CSR_INT_BIT_SW_ERR) {
1786 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
1787 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001788 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001789 handled |= CSR_INT_BIT_SW_ERR;
1790 }
1791
1792 /* uCode wakes up after power-down sleep */
1793 if (inta & CSR_INT_BIT_WAKEUP) {
1794 IWL_DEBUG_ISR("Wakeup interrupt\n");
Tomas Winklera55360e2008-05-05 10:22:28 +08001795 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Tomas Winklerbabcebf2008-05-15 13:54:00 +08001796 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1797 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1798 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1799 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1800 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1801 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07001802
1803 handled |= CSR_INT_BIT_WAKEUP;
1804 }
1805
1806 /* All uCode command responses, including Tx command responses,
1807 * Rx "responses" (frame-received notification), and other
1808 * notifications from uCode come through here*/
1809 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Tomas Winklera55360e2008-05-05 10:22:28 +08001810 iwl_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001811 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1812 }
1813
1814 if (inta & CSR_INT_BIT_FH_TX) {
1815 IWL_DEBUG_ISR("Tx interrupt\n");
1816 handled |= CSR_INT_BIT_FH_TX;
Ron Rindjunskydbb983b2008-05-15 13:54:12 +08001817 /* FH finished to write, send event */
1818 priv->ucode_write_complete = 1;
1819 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07001820 }
1821
1822 if (inta & ~handled)
1823 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
1824
1825 if (inta & ~CSR_INI_SET_MASK) {
1826 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
1827 inta & ~CSR_INI_SET_MASK);
1828 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
1829 }
1830
1831 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001832 /* only Re-enable if diabled by irq */
1833 if (test_bit(STATUS_INT_ENABLED, &priv->status))
1834 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001835
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001836#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001837 if (priv->debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001838 inta = iwl_read32(priv, CSR_INT);
1839 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1840 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07001841 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
1842 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1843 }
1844#endif
1845 spin_unlock_irqrestore(&priv->lock, flags);
1846}
1847
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001848static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07001849{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001850 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07001851 u32 inta, inta_mask;
1852 u32 inta_fh;
1853 if (!priv)
1854 return IRQ_NONE;
1855
1856 spin_lock(&priv->lock);
1857
1858 /* Disable (but don't clear!) interrupts here to avoid
1859 * back-to-back ISRs and sporadic interrupts from our NIC.
1860 * If we have something to service, the tasklet will re-enable ints.
1861 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001862 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
1863 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07001864
1865 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001866 inta = iwl_read32(priv, CSR_INT);
1867 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07001868
1869 /* Ignore interrupt if there's nothing in NIC to service.
1870 * This may be due to IRQ shared with another device,
1871 * or due to sporadic interrupts thrown from our NIC. */
1872 if (!inta && !inta_fh) {
1873 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
1874 goto none;
1875 }
1876
1877 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08001878 /* Hardware disappeared. It might have already raised
1879 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07001880 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08001881 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07001882 }
1883
1884 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1885 inta, inta_mask, inta_fh);
1886
Joonwoo Park25c03d82008-01-23 10:15:20 -08001887 inta &= ~CSR_INT_BIT_SCD;
1888
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001889 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001890 if (likely(inta || inta_fh))
1891 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07001892
Oliver Neukum66fbb542007-11-15 09:31:10 +08001893 unplugged:
1894 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07001895 return IRQ_HANDLED;
1896
1897 none:
1898 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001899 /* only Re-enable if diabled by irq */
1900 if (test_bit(STATUS_INT_ENABLED, &priv->status))
1901 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001902 spin_unlock(&priv->lock);
1903 return IRQ_NONE;
1904}
1905
Zhu Yib481de92007-09-25 17:54:57 -07001906/******************************************************************************
1907 *
1908 * uCode download functions
1909 *
1910 ******************************************************************************/
1911
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001912static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001913{
Tomas Winkler98c92212008-01-14 17:46:20 -08001914 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1915 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1916 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1917 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1918 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1919 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001920}
1921
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08001922static void iwl4965_nic_start(struct iwl_priv *priv)
1923{
1924 /* Remove all resets to allow NIC to operate */
1925 iwl_write32(priv, CSR_RESET, 0);
1926}
1927
1928
Zhu Yib481de92007-09-25 17:54:57 -07001929/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001930 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07001931 *
1932 * Copy into buffers for card to fetch via bus-mastering
1933 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001934static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001935{
Ron Rindjunsky14b3d332008-06-13 15:44:54 +08001936 struct iwl_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08001937 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001938 const struct firmware *ucode_raw;
Tomas Winkler4bf775cd2008-03-04 18:09:31 -08001939 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07001940 u8 *src;
1941 size_t len;
1942 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
1943
1944 /* Ask kernel firmware_class module to get the boot firmware off disk.
1945 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08001946 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
1947 if (ret < 0) {
1948 IWL_ERROR("%s firmware file req failed: Reason %d\n",
1949 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07001950 goto error;
1951 }
1952
1953 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
1954 name, ucode_raw->size);
1955
1956 /* Make sure that we got at least our header! */
1957 if (ucode_raw->size < sizeof(*ucode)) {
1958 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08001959 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001960 goto err_release;
1961 }
1962
1963 /* Data from ucode file: header followed by uCode images */
1964 ucode = (void *)ucode_raw->data;
1965
1966 ver = le32_to_cpu(ucode->ver);
1967 inst_size = le32_to_cpu(ucode->inst_size);
1968 data_size = le32_to_cpu(ucode->data_size);
1969 init_size = le32_to_cpu(ucode->init_size);
1970 init_data_size = le32_to_cpu(ucode->init_data_size);
1971 boot_size = le32_to_cpu(ucode->boot_size);
1972
1973 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
1974 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
1975 inst_size);
1976 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
1977 data_size);
1978 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
1979 init_size);
1980 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
1981 init_data_size);
1982 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
1983 boot_size);
1984
1985 /* Verify size of file vs. image size info in file's header */
1986 if (ucode_raw->size < sizeof(*ucode) +
1987 inst_size + data_size + init_size +
1988 init_data_size + boot_size) {
1989
1990 IWL_DEBUG_INFO("uCode file size %d too small\n",
1991 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08001992 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001993 goto err_release;
1994 }
1995
1996 /* Verify that uCode images will fit in card's SRAM */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001997 if (inst_size > priv->hw_params.max_inst_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08001998 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
1999 inst_size);
2000 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002001 goto err_release;
2002 }
2003
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002004 if (data_size > priv->hw_params.max_data_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08002005 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
2006 data_size);
2007 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002008 goto err_release;
2009 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002010 if (init_size > priv->hw_params.max_inst_size) {
Zhu Yib481de92007-09-25 17:54:57 -07002011 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08002012 ("uCode init instr len %d too large to fit in\n",
2013 init_size);
2014 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002015 goto err_release;
2016 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002017 if (init_data_size > priv->hw_params.max_data_size) {
Zhu Yib481de92007-09-25 17:54:57 -07002018 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08002019 ("uCode init data len %d too large to fit in\n",
2020 init_data_size);
2021 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002022 goto err_release;
2023 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002024 if (boot_size > priv->hw_params.max_bsm_size) {
Zhu Yib481de92007-09-25 17:54:57 -07002025 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08002026 ("uCode boot instr len %d too large to fit in\n",
2027 boot_size);
2028 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002029 goto err_release;
2030 }
2031
2032 /* Allocate ucode buffers for card's bus-master loading ... */
2033
2034 /* Runtime instructions and 2 copies of data:
2035 * 1) unmodified from disk
2036 * 2) backup cache for save/restore during power-downs */
2037 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002038 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07002039
2040 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002041 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07002042
2043 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002044 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07002045
2046 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08002047 if (init_size && init_data_size) {
2048 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002049 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07002050
Tomas Winkler90e759d2007-11-29 11:09:41 +08002051 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002052 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002053
2054 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2055 goto err_pci_alloc;
2056 }
Zhu Yib481de92007-09-25 17:54:57 -07002057
2058 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08002059 if (boot_size) {
2060 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002061 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07002062
Tomas Winkler90e759d2007-11-29 11:09:41 +08002063 if (!priv->ucode_boot.v_addr)
2064 goto err_pci_alloc;
2065 }
Zhu Yib481de92007-09-25 17:54:57 -07002066
2067 /* Copy images into buffers for card's bus-master reads ... */
2068
2069 /* Runtime instructions (first block of data in file) */
2070 src = &ucode->data[0];
2071 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002072 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002073 memcpy(priv->ucode_code.v_addr, src, len);
2074 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
2075 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2076
2077 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002078 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07002079 src = &ucode->data[inst_size];
2080 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002081 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002082 memcpy(priv->ucode_data.v_addr, src, len);
2083 memcpy(priv->ucode_data_backup.v_addr, src, len);
2084
2085 /* Initialization instructions (3rd block) */
2086 if (init_size) {
2087 src = &ucode->data[inst_size + data_size];
2088 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002089 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
2090 len);
Zhu Yib481de92007-09-25 17:54:57 -07002091 memcpy(priv->ucode_init.v_addr, src, len);
2092 }
2093
2094 /* Initialization data (4th block) */
2095 if (init_data_size) {
2096 src = &ucode->data[inst_size + data_size + init_size];
2097 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002098 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
2099 len);
Zhu Yib481de92007-09-25 17:54:57 -07002100 memcpy(priv->ucode_init_data.v_addr, src, len);
2101 }
2102
2103 /* Bootstrap instructions (5th block) */
2104 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
2105 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002106 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002107 memcpy(priv->ucode_boot.v_addr, src, len);
2108
2109 /* We have our copies now, allow OS release its copies */
2110 release_firmware(ucode_raw);
2111 return 0;
2112
2113 err_pci_alloc:
2114 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002115 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002116 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002117
2118 err_release:
2119 release_firmware(ucode_raw);
2120
2121 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08002122 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002123}
2124
Zhu Yib481de92007-09-25 17:54:57 -07002125/**
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002126 * iwl_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002127 * from protocol/runtime uCode (initialization uCode's
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002128 * Alive gets handled by iwl_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002129 */
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002130static void iwl_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002131{
Tomas Winkler57aab752008-04-14 21:16:03 -07002132 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002133
2134 IWL_DEBUG_INFO("Runtime Alive received.\n");
2135
2136 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2137 /* We had an error bringing up the hardware, so take it
2138 * all the way back down so we can try again */
2139 IWL_DEBUG_INFO("Alive failed.\n");
2140 goto restart;
2141 }
2142
2143 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2144 * This is a paranoid check, because we would not have gotten the
2145 * "runtime" alive if code weren't properly loaded. */
Emmanuel Grumbachb0692f22008-04-24 11:55:18 -07002146 if (iwl_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002147 /* Runtime instruction load was bad;
2148 * take it all the way back down so we can try again */
2149 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
2150 goto restart;
2151 }
2152
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002153 iwlcore_clear_stations_table(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07002154 ret = priv->cfg->ops->lib->alive_notify(priv);
2155 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07002156 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07002157 ret);
Zhu Yib481de92007-09-25 17:54:57 -07002158 goto restart;
2159 }
2160
Ben Cahill9fbab512007-11-29 11:09:47 +08002161 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002162 set_bit(STATUS_ALIVE, &priv->status);
2163
Tomas Winklerfee12472008-04-03 16:05:21 -07002164 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002165 return;
2166
Johannes Berg36d68252008-05-15 12:55:26 +02002167 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002168
2169 priv->active_rate = priv->rates_mask;
2170 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2171
Tomas Winkler3109ece2008-03-28 16:33:35 -07002172 if (iwl_is_associated(priv)) {
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08002173 struct iwl_rxon_cmd *active_rxon =
2174 (struct iwl_rxon_cmd *)&priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002175
2176 memcpy(&priv->staging_rxon, &priv->active_rxon,
2177 sizeof(priv->staging_rxon));
2178 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2179 } else {
2180 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002181 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002182 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2183 }
2184
Ben Cahill9fbab512007-11-29 11:09:47 +08002185 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002186 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002187
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002188 iwl_reset_run_time_calib(priv);
2189
Zhu Yib481de92007-09-25 17:54:57 -07002190 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002191 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002192
2193 /* At this point, the NIC is initialized and operational */
Emmanuel Grumbach47f4a582008-06-12 09:47:13 +08002194 iwl_rf_kill_ct_config(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08002195
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002196 iwl_leds_register(priv);
2197
Zhu Yib481de92007-09-25 17:54:57 -07002198 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002199 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08002200 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002201
2202 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002203 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002204
Emmanuel Grumbach58d0f362008-06-12 09:47:19 +08002205 iwl_power_update_mode(priv, 1);
Mohamed Abbas84363e62008-04-04 16:59:58 -07002206 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002207
2208 if (test_and_clear_bit(STATUS_MODE_PENDING, &priv->status))
2209 iwl4965_set_mode(priv, priv->iw_mode);
2210
Zhu Yib481de92007-09-25 17:54:57 -07002211 return;
2212
2213 restart:
2214 queue_work(priv->workqueue, &priv->restart);
2215}
2216
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08002217static void iwl_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002218
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002219static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002220{
2221 unsigned long flags;
2222 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002223
2224 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
2225
Zhu Yib481de92007-09-25 17:54:57 -07002226 if (!exit_pending)
2227 set_bit(STATUS_EXIT_PENDING, &priv->status);
2228
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002229 iwl_leds_unregister(priv);
2230
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002231 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002232
2233 /* Unblock any waiting calls */
2234 wake_up_interruptible_all(&priv->wait_command_queue);
2235
Zhu Yib481de92007-09-25 17:54:57 -07002236 /* Wipe out the EXIT_PENDING status bit if we are not actually
2237 * exiting the module */
2238 if (!exit_pending)
2239 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2240
2241 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002242 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002243
2244 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002245 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002246 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002247 spin_unlock_irqrestore(&priv->lock, flags);
2248 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002249
2250 if (priv->mac80211_registered)
2251 ieee80211_stop_queues(priv->hw);
2252
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002253 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07002254 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07002255 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002256 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2257 STATUS_RF_KILL_HW |
2258 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2259 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08002260 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2261 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002262 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
2263 STATUS_IN_SUSPEND;
2264 goto exit;
2265 }
2266
2267 /* ...otherwise clear out all the status bits but the RF Kill and
2268 * SUSPEND bits and continue taking the NIC down. */
2269 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2270 STATUS_RF_KILL_HW |
2271 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2272 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08002273 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2274 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002275 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
2276 STATUS_IN_SUSPEND |
2277 test_bit(STATUS_FW_ERROR, &priv->status) <<
2278 STATUS_FW_ERROR;
2279
2280 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002281 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08002282 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07002283 spin_unlock_irqrestore(&priv->lock, flags);
2284
Tomas Winklerda1bc452008-05-29 16:35:00 +08002285 iwl_txq_ctx_stop(priv);
Tomas Winklerb3bbacb2008-05-29 16:35:01 +08002286 iwl_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002287
2288 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002289 if (!iwl_grab_nic_access(priv)) {
2290 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002291 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002292 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002293 }
2294 spin_unlock_irqrestore(&priv->lock, flags);
2295
2296 udelay(5);
2297
Tomas Winkler7f066102008-05-29 16:34:57 +08002298 /* FIXME: apm_ops.suspend(priv) */
2299 priv->cfg->ops->lib->apm_ops.reset(priv);
Ron Rindjunsky399f49002008-04-23 17:14:56 -07002300 priv->cfg->ops->lib->free_shared_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002301
2302 exit:
Tomas Winkler885ba202008-05-29 16:34:55 +08002303 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002304
2305 if (priv->ibss_beacon)
2306 dev_kfree_skb(priv->ibss_beacon);
2307 priv->ibss_beacon = NULL;
2308
2309 /* clear out any free frames */
Tomas Winklerfcab4232008-05-15 13:54:01 +08002310 iwl_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002311}
2312
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002313static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002314{
2315 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002316 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002317 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002318
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08002319 iwl_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002320}
2321
2322#define MAX_HW_RESTARTS 5
2323
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002324static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002325{
Tomas Winkler57aab752008-04-14 21:16:03 -07002326 int i;
2327 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002328
2329 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
2330 IWL_WARNING("Exit pending; will not bring the NIC up\n");
2331 return -EIO;
2332 }
2333
Reinette Chatree903fbd2008-01-30 22:05:15 -08002334 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
2335 IWL_ERROR("ucode not available for device bringup\n");
2336 return -EIO;
2337 }
2338
Zhu Yie655b9f2008-01-24 02:19:38 -08002339 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002340 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08002341 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2342 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2343 else {
2344 set_bit(STATUS_RF_KILL_HW, &priv->status);
2345 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002346 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08002347 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
2348 return -ENODEV;
2349 }
Zhu Yib481de92007-09-25 17:54:57 -07002350 }
2351
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002352 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002353 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002354
Ron Rindjunsky399f49002008-04-23 17:14:56 -07002355 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
2356 if (ret) {
2357 IWL_ERROR("Unable to allocate shared memory\n");
2358 return ret;
2359 }
2360
Ron Rindjunsky1053d352008-05-05 10:22:43 +08002361 ret = iwl_hw_nic_init(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07002362 if (ret) {
2363 IWL_ERROR("Unable to init nic\n");
2364 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002365 }
2366
2367 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002368 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2369 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002370 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2371
2372 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002373 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002374 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002375
2376 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002377 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2378 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002379
2380 /* Copy original ucode data image from disk into backup cache.
2381 * This will be used to initialize the on-board processor's
2382 * data SRAM for a clean start when the runtime program first loads. */
2383 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08002384 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002385
Zhu Yie655b9f2008-01-24 02:19:38 -08002386 /* We return success when we resume from suspend and rf_kill is on. */
Mohamed Abbas64e72c32008-06-12 09:47:03 +08002387 if (test_bit(STATUS_RF_KILL_HW, &priv->status) ||
2388 test_bit(STATUS_RF_KILL_SW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07002389 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07002390
2391 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2392
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002393 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002394
2395 /* load bootstrap state machine,
2396 * load bootstrap program into processor's memory,
2397 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07002398 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002399
Tomas Winkler57aab752008-04-14 21:16:03 -07002400 if (ret) {
2401 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07002402 continue;
2403 }
2404
Emmanuel Grumbachf3d5b452008-06-12 09:47:04 +08002405 /* Clear out the uCode error bit if it is set */
2406 clear_bit(STATUS_FW_ERROR, &priv->status);
2407
Zhu Yib481de92007-09-25 17:54:57 -07002408 /* start card; "initialize" will load runtime ucode */
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08002409 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002410
Zhu Yib481de92007-09-25 17:54:57 -07002411 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
2412
2413 return 0;
2414 }
2415
2416 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002417 __iwl4965_down(priv);
Mohamed Abbas64e72c32008-06-12 09:47:03 +08002418 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002419
2420 /* tried to restart and config the device for as long as our
2421 * patience could withstand */
2422 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
2423 return -EIO;
2424}
2425
2426
2427/*****************************************************************************
2428 *
2429 * Workqueue callbacks
2430 *
2431 *****************************************************************************/
2432
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002433static void iwl_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002434{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002435 struct iwl_priv *priv =
2436 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002437
2438 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2439 return;
2440
2441 mutex_lock(&priv->mutex);
Emmanuel Grumbachf3ccc082008-05-05 10:22:45 +08002442 priv->cfg->ops->lib->init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002443 mutex_unlock(&priv->mutex);
2444}
2445
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002446static void iwl_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002447{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002448 struct iwl_priv *priv =
2449 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002450
2451 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2452 return;
2453
2454 mutex_lock(&priv->mutex);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002455 iwl_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002456 mutex_unlock(&priv->mutex);
2457}
2458
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002459static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002460{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002461 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07002462
2463 wake_up_interruptible(&priv->wait_command_queue);
2464
2465 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2466 return;
2467
2468 mutex_lock(&priv->mutex);
2469
Tomas Winklerfee12472008-04-03 16:05:21 -07002470 if (!iwl_is_rfkill(priv)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08002471 IWL_DEBUG(IWL_DL_RF_KILL,
Zhu Yib481de92007-09-25 17:54:57 -07002472 "HW and/or SW RF Kill no longer active, restarting "
2473 "device\n");
2474 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
2475 queue_work(priv->workqueue, &priv->restart);
2476 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002477 /* make sure mac80211 stop sending Tx frame */
2478 if (priv->mac80211_registered)
2479 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002480
2481 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
2482 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2483 "disabled by SW switch\n");
2484 else
2485 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
2486 "Kill switch must be turned off for "
2487 "wireless networking to work.\n");
2488 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002489 iwl_rfkill_set_hw_state(priv);
2490
Zhu Yib481de92007-09-25 17:54:57 -07002491 mutex_unlock(&priv->mutex);
2492}
2493
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08002494static void iwl4965_bg_set_monitor(struct work_struct *work)
2495{
2496 struct iwl_priv *priv = container_of(work,
2497 struct iwl_priv, set_monitor);
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002498 int ret;
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08002499
2500 IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
2501
2502 mutex_lock(&priv->mutex);
2503
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002504 ret = iwl4965_set_mode(priv, IEEE80211_IF_TYPE_MNTR);
2505
2506 if (ret) {
2507 if (ret == -EAGAIN)
2508 IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
2509 else
2510 IWL_ERROR("iwl4965_set_mode() failed ret = %d\n", ret);
2511 }
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08002512
2513 mutex_unlock(&priv->mutex);
2514}
2515
Emmanuel Grumbach16e727e2008-06-12 09:46:52 +08002516static void iwl_bg_run_time_calib_work(struct work_struct *work)
2517{
2518 struct iwl_priv *priv = container_of(work, struct iwl_priv,
2519 run_time_calib_work);
2520
2521 mutex_lock(&priv->mutex);
2522
2523 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
2524 test_bit(STATUS_SCANNING, &priv->status)) {
2525 mutex_unlock(&priv->mutex);
2526 return;
2527 }
2528
2529 if (priv->start_calib) {
2530 iwl_chain_noise_calibration(priv, &priv->statistics);
2531
2532 iwl_sensitivity_calibration(priv, &priv->statistics);
2533 }
2534
2535 mutex_unlock(&priv->mutex);
2536 return;
2537}
2538
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002539static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002540{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002541 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07002542
2543 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2544 return;
2545
2546 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002547 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002548 mutex_unlock(&priv->mutex);
2549}
2550
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002551static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002552{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002553 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07002554
2555 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2556 return;
2557
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002558 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002559 queue_work(priv->workqueue, &priv->up);
2560}
2561
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002562static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002563{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002564 struct iwl_priv *priv =
2565 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07002566
2567 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2568 return;
2569
2570 mutex_lock(&priv->mutex);
Tomas Winklera55360e2008-05-05 10:22:28 +08002571 iwl_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002572 mutex_unlock(&priv->mutex);
2573}
2574
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002575#define IWL_DELAY_NEXT_SCAN (HZ*2)
2576
Reinette Chatre508e32e2008-04-14 21:16:13 -07002577static void iwl4965_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002578{
Zhu Yib481de92007-09-25 17:54:57 -07002579 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07002580 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07002581 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002582
2583 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2584 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
2585 return;
2586 }
2587
Joe Perches0795af52007-10-03 17:59:30 -07002588 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
2589 priv->assoc_id,
2590 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07002591
2592
2593 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2594 return;
2595
Zhu Yib481de92007-09-25 17:54:57 -07002596
Reinette Chatre508e32e2008-04-14 21:16:13 -07002597 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08002598 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07002599
Tomas Winkler2a421b92008-06-12 09:47:10 +08002600 iwl_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08002601
Zhu Yib481de92007-09-25 17:54:57 -07002602 conf = ieee80211_get_hw_conf(priv->hw);
2603
2604 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002605 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002606
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002607 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
2608 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07002609 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07002610 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07002611 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07002612 IWL_WARNING("REPLY_RXON_TIMING failed - "
2613 "Attempting to continue.\n");
2614
2615 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
2616
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02002617 if (priv->current_ht_config.is_ht)
Tomas Winkler47c51962008-05-05 10:22:41 +08002618 iwl_set_rxon_ht(priv, &priv->current_ht_config);
Ron Rindjunsky4f85f5b2008-06-09 22:54:35 +03002619
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07002620 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002621 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
2622
2623 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
2624 priv->assoc_id, priv->beacon_int);
2625
2626 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
2627 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2628 else
2629 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2630
2631 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
2632 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2633 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2634 else
2635 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2636
2637 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2638 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2639
2640 }
2641
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002642 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002643
2644 switch (priv->iw_mode) {
2645 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002646 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07002647 break;
2648
2649 case IEEE80211_IF_TYPE_IBSS:
2650
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002651 /* assume default assoc id */
2652 priv->assoc_id = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002653
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08002654 iwl_rxon_add_station(priv, priv->bssid, 0);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002655 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
2656 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002657
2658 break;
2659
2660 default:
2661 IWL_ERROR("%s Should not be called in %d mode\n",
2662 __FUNCTION__, priv->iw_mode);
2663 break;
2664 }
2665
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002666 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002667
Zhu Yib481de92007-09-25 17:54:57 -07002668 /* Enable Rx differential gain and sensitivity calibrations */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07002669 iwl_chain_noise_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002670 priv->start_calib = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002671
2672 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2673 priv->assoc_station_added = 1;
2674
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002675 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08002676
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -07002677 iwl_power_update_mode(priv, 0);
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002678 /* we have just associated, don't start scan too early */
2679 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Reinette Chatre508e32e2008-04-14 21:16:13 -07002680}
2681
2682
2683static void iwl4965_bg_post_associate(struct work_struct *data)
2684{
2685 struct iwl_priv *priv = container_of(data, struct iwl_priv,
2686 post_associate.work);
2687
2688 mutex_lock(&priv->mutex);
2689 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002690 mutex_unlock(&priv->mutex);
Reinette Chatre508e32e2008-04-14 21:16:13 -07002691
Zhu Yib481de92007-09-25 17:54:57 -07002692}
2693
Zhu Yi76bb77e2007-11-22 10:53:22 +08002694static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
2695
Tomas Winkler2a421b92008-06-12 09:47:10 +08002696static void iwl_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002697{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002698 struct iwl_priv *priv =
2699 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07002700
Tomas Winkler630fe9b2008-06-12 09:47:08 +08002701 IWL_DEBUG_SCAN("SCAN complete scan\n");
Zhu Yib481de92007-09-25 17:54:57 -07002702
2703 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2704 return;
2705
Zhu Yia0646472007-12-20 14:10:01 +08002706 if (test_bit(STATUS_CONF_PENDING, &priv->status))
2707 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08002708
Zhu Yib481de92007-09-25 17:54:57 -07002709 ieee80211_scan_completed(priv->hw);
2710
2711 /* Since setting the TXPOWER may have been deferred while
2712 * performing the scan, fire one off */
2713 mutex_lock(&priv->mutex);
Tomas Winkler630fe9b2008-06-12 09:47:08 +08002714 iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
Zhu Yib481de92007-09-25 17:54:57 -07002715 mutex_unlock(&priv->mutex);
2716}
2717
2718/*****************************************************************************
2719 *
2720 * mac80211 entry point functions
2721 *
2722 *****************************************************************************/
2723
Zhu Yi5a669262008-01-14 17:46:18 -08002724#define UCODE_READY_TIMEOUT (2 * HZ)
2725
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002726static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002727{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002728 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08002729 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002730
2731 IWL_DEBUG_MAC80211("enter\n");
2732
Zhu Yi5a669262008-01-14 17:46:18 -08002733 if (pci_enable_device(priv->pci_dev)) {
2734 IWL_ERROR("Fail to pci_enable_device\n");
2735 return -ENODEV;
2736 }
2737 pci_restore_state(priv->pci_dev);
2738 pci_enable_msi(priv->pci_dev);
2739
2740 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
2741 DRV_NAME, priv);
2742 if (ret) {
2743 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
2744 goto out_disable_msi;
2745 }
2746
Zhu Yib481de92007-09-25 17:54:57 -07002747 /* we should be verifying the device is ready to be opened */
2748 mutex_lock(&priv->mutex);
2749
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08002750 memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
Zhu Yi5a669262008-01-14 17:46:18 -08002751 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
2752 * ucode filename and max sizes are card-specific. */
2753
2754 if (!priv->ucode_code.len) {
2755 ret = iwl4965_read_ucode(priv);
2756 if (ret) {
2757 IWL_ERROR("Could not read microcode: %d\n", ret);
2758 mutex_unlock(&priv->mutex);
2759 goto out_release_irq;
2760 }
2761 }
2762
Zhu Yie655b9f2008-01-24 02:19:38 -08002763 ret = __iwl4965_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08002764
Zhu Yib481de92007-09-25 17:54:57 -07002765 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08002766
Zhu Yie655b9f2008-01-24 02:19:38 -08002767 if (ret)
2768 goto out_release_irq;
2769
2770 IWL_DEBUG_INFO("Start UP work done.\n");
2771
2772 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
2773 return 0;
2774
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08002775 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
Zhu Yi5a669262008-01-14 17:46:18 -08002776 * mac80211 will not be run successfully. */
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08002777 if (priv->ucode_type == UCODE_RT) {
2778 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
2779 test_bit(STATUS_READY, &priv->status),
2780 UCODE_READY_TIMEOUT);
2781 if (!ret) {
2782 if (!test_bit(STATUS_READY, &priv->status)) {
2783 IWL_ERROR("START_ALIVE timeout after %dms.\n",
2784 jiffies_to_msecs(UCODE_READY_TIMEOUT));
2785 ret = -ETIMEDOUT;
2786 goto out_release_irq;
2787 }
Zhu Yi5a669262008-01-14 17:46:18 -08002788 }
Zhu Yi5a669262008-01-14 17:46:18 -08002789
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08002790 priv->is_open = 1;
2791 }
Zhu Yib481de92007-09-25 17:54:57 -07002792 IWL_DEBUG_MAC80211("leave\n");
2793 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08002794
2795out_release_irq:
2796 free_irq(priv->pci_dev->irq, priv);
2797out_disable_msi:
2798 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08002799 pci_disable_device(priv->pci_dev);
2800 priv->is_open = 0;
2801 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08002802 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002803}
2804
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002805static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002806{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002807 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002808
2809 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08002810
Zhu Yie655b9f2008-01-24 02:19:38 -08002811 if (!priv->is_open) {
2812 IWL_DEBUG_MAC80211("leave - skip\n");
2813 return;
2814 }
2815
Zhu Yib481de92007-09-25 17:54:57 -07002816 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08002817
Tomas Winklerfee12472008-04-03 16:05:21 -07002818 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08002819 /* stop mac, cancel any scan request and clear
2820 * RXON_FILTER_ASSOC_MSK BIT
2821 */
Zhu Yi5a669262008-01-14 17:46:18 -08002822 mutex_lock(&priv->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +08002823 iwl_scan_cancel_timeout(priv, 100);
Zhu Yi5a669262008-01-14 17:46:18 -08002824 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08002825 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08002826 }
2827
Zhu Yi5a669262008-01-14 17:46:18 -08002828 iwl4965_down(priv);
2829
2830 flush_workqueue(priv->workqueue);
2831 free_irq(priv->pci_dev->irq, priv);
2832 pci_disable_msi(priv->pci_dev);
2833 pci_save_state(priv->pci_dev);
2834 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08002835
Zhu Yib481de92007-09-25 17:54:57 -07002836 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002837}
2838
Johannes Berge039fa42008-05-15 12:55:29 +02002839static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07002840{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002841 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002842
2843 IWL_DEBUG_MAC80211("enter\n");
2844
2845 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
2846 IWL_DEBUG_MAC80211("leave - monitor\n");
2847 return -1;
2848 }
2849
2850 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02002851 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07002852
Johannes Berge039fa42008-05-15 12:55:29 +02002853 if (iwl_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07002854 dev_kfree_skb_any(skb);
2855
2856 IWL_DEBUG_MAC80211("leave\n");
2857 return 0;
2858}
2859
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002860static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07002861 struct ieee80211_if_init_conf *conf)
2862{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002863 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002864 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07002865 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002866
Johannes Berg32bfd352007-12-19 01:31:26 +01002867 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07002868
Johannes Berg32bfd352007-12-19 01:31:26 +01002869 if (priv->vif) {
2870 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08002871 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07002872 }
2873
2874 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01002875 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07002876
2877 spin_unlock_irqrestore(&priv->lock, flags);
2878
2879 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02002880
2881 if (conf->mac_addr) {
2882 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
2883 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
2884 }
Zhu Yib481de92007-09-25 17:54:57 -07002885
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002886 if (iwl4965_set_mode(priv, conf->type) == -EAGAIN)
2887 /* we are not ready, will run again when ready */
2888 set_bit(STATUS_MODE_PENDING, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08002889
Zhu Yib481de92007-09-25 17:54:57 -07002890 mutex_unlock(&priv->mutex);
2891
Zhu Yi5a669262008-01-14 17:46:18 -08002892 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002893 return 0;
2894}
2895
2896/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002897 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07002898 *
2899 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
2900 * be set inappropriately and the driver currently sets the hardware up to
2901 * use it whenever needed.
2902 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002903static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07002904{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002905 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002906 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002907 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08002908 int ret = 0;
Tomas Winkler82a66bb2008-05-29 16:35:28 +08002909 u16 channel;
Zhu Yib481de92007-09-25 17:54:57 -07002910
2911 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002912 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002913
Zhu Yi12342c42007-12-20 11:27:32 +08002914 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
2915
Emmanuel Grumbach14a08a72008-06-13 15:44:55 +08002916 if (conf->radio_enabled && iwl_radio_kill_sw_enable_radio(priv)) {
Mohamed Abbas64e72c32008-06-12 09:47:03 +08002917 IWL_DEBUG_MAC80211("leave - RF-KILL - waiting for uCode\n");
Emmanuel Grumbach14a08a72008-06-13 15:44:55 +08002918 goto out;
Mohamed Abbas64e72c32008-06-12 09:47:03 +08002919 }
2920
Emmanuel Grumbach14a08a72008-06-13 15:44:55 +08002921 if (!conf->radio_enabled)
2922 iwl_radio_kill_sw_disable_radio(priv);
2923
Tomas Winklerfee12472008-04-03 16:05:21 -07002924 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002925 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08002926 ret = -EIO;
2927 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002928 }
2929
Assaf Krauss1ea87392008-03-18 14:57:50 -07002930 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07002931 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08002932 IWL_DEBUG_MAC80211("leave - scanning\n");
2933 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002934 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08002935 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07002936 }
2937
Tomas Winkler82a66bb2008-05-29 16:35:28 +08002938 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
2939 ch_info = iwl_get_channel_info(priv, conf->channel->band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07002940 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07002941 IWL_DEBUG_MAC80211("leave - invalid channel\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08002942 ret = -EINVAL;
2943 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002944 }
2945
Assaf Krauss398f9e72008-06-12 09:47:06 +08002946 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS &&
2947 !is_channel_ibss(ch_info)) {
2948 IWL_ERROR("channel %d in band %d not IBSS channel\n",
2949 conf->channel->hw_value, conf->channel->band);
2950 ret = -EINVAL;
2951 goto out;
2952 }
2953
Tomas Winkler82a66bb2008-05-29 16:35:28 +08002954 spin_lock_irqsave(&priv->lock, flags);
2955
Tomas Winkler78330fd2008-02-06 02:37:18 +02002956 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07002957 * from any ht related info since 2.4 does not
2958 * support ht */
Tomas Winkler82a66bb2008-05-29 16:35:28 +08002959 if ((le16_to_cpu(priv->staging_rxon.channel) != channel)
Zhu Yib481de92007-09-25 17:54:57 -07002960#ifdef IEEE80211_CONF_CHANNEL_SWITCH
2961 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
2962#endif
2963 )
2964 priv->staging_rxon.flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002965
Tomas Winkler82a66bb2008-05-29 16:35:28 +08002966 iwl_set_rxon_channel(priv, conf->channel->band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07002967
Tomas Winkler82a66bb2008-05-29 16:35:28 +08002968 iwl_set_flags_for_band(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07002969
2970 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01002971 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07002972 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002973 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002974
2975 spin_unlock_irqrestore(&priv->lock, flags);
2976
2977#ifdef IEEE80211_CONF_CHANNEL_SWITCH
2978 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002979 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08002980 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002981 }
2982#endif
2983
Zhu Yib481de92007-09-25 17:54:57 -07002984 if (!conf->radio_enabled) {
2985 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08002986 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002987 }
2988
Tomas Winklerfee12472008-04-03 16:05:21 -07002989 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002990 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08002991 ret = -EIO;
2992 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002993 }
2994
Tomas Winkler630fe9b2008-06-12 09:47:08 +08002995 IWL_DEBUG_MAC80211("TX Power old=%d new=%d\n",
2996 priv->tx_power_user_lmt, conf->power_level);
2997
2998 iwl_set_tx_power(priv, conf->power_level, false);
2999
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003000 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003001
3002 if (memcmp(&priv->active_rxon,
3003 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003004 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003005 else
3006 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
3007
3008 IWL_DEBUG_MAC80211("leave\n");
3009
Zhu Yia0646472007-12-20 14:10:01 +08003010out:
3011 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08003012 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08003013 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003014}
3015
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003016static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003017{
Tomas Winkler857485c2008-03-21 13:53:44 -07003018 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003019
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003020 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003021 return;
3022
3023 /* The following should be done only at AP bring up */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +08003024 if (!(iwl_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07003025
3026 /* RXON - unassoc (to set timing command) */
3027 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003028 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003029
3030 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003031 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
3032 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07003033 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07003034 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07003035 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07003036 IWL_WARNING("REPLY_RXON_TIMING failed - "
3037 "Attempting to continue.\n");
3038
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07003039 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003040
3041 /* FIXME: what should be the assoc_id for AP? */
3042 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
3043 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3044 priv->staging_rxon.flags |=
3045 RXON_FLG_SHORT_PREAMBLE_MSK;
3046 else
3047 priv->staging_rxon.flags &=
3048 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3049
3050 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
3051 if (priv->assoc_capability &
3052 WLAN_CAPABILITY_SHORT_SLOT_TIME)
3053 priv->staging_rxon.flags |=
3054 RXON_FLG_SHORT_SLOT_MSK;
3055 else
3056 priv->staging_rxon.flags &=
3057 ~RXON_FLG_SHORT_SLOT_MSK;
3058
3059 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
3060 priv->staging_rxon.flags &=
3061 ~RXON_FLG_SHORT_SLOT_MSK;
3062 }
3063 /* restore RXON assoc */
3064 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003065 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003066 iwl4965_activate_qos(priv, 1);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08003067 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08003068 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003069 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003070
3071 /* FIXME - we need to add code here to detect a totally new
3072 * configuration, reset the AP, unassoc, rxon timing, assoc,
3073 * clear sta table, add BCAST sta... */
3074}
3075
Johannes Berg32bfd352007-12-19 01:31:26 +01003076static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
3077 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07003078 struct ieee80211_if_conf *conf)
3079{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003080 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07003081 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07003082 unsigned long flags;
3083 int rc;
3084
3085 if (conf == NULL)
3086 return -EIO;
3087
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08003088 if (priv->vif != vif) {
3089 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08003090 return 0;
3091 }
3092
Zhu Yib481de92007-09-25 17:54:57 -07003093 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3094 (!conf->beacon || !conf->ssid_len)) {
3095 IWL_DEBUG_MAC80211
3096 ("Leaving in AP mode because HostAPD is not ready.\n");
3097 return 0;
3098 }
3099
Tomas Winklerfee12472008-04-03 16:05:21 -07003100 if (!iwl_is_alive(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08003101 return -EAGAIN;
3102
Zhu Yib481de92007-09-25 17:54:57 -07003103 mutex_lock(&priv->mutex);
3104
Zhu Yib481de92007-09-25 17:54:57 -07003105 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07003106 IWL_DEBUG_MAC80211("bssid: %s\n",
3107 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07003108
Johannes Berg4150c572007-09-17 01:29:23 -04003109/*
3110 * very dubious code was here; the probe filtering flag is never set:
3111 *
Zhu Yib481de92007-09-25 17:54:57 -07003112 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
3113 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04003114 */
Zhu Yib481de92007-09-25 17:54:57 -07003115
3116 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
3117 if (!conf->bssid) {
3118 conf->bssid = priv->mac_addr;
3119 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07003120 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
3121 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07003122 }
3123 if (priv->ibss_beacon)
3124 dev_kfree_skb(priv->ibss_beacon);
3125
3126 priv->ibss_beacon = conf->beacon;
3127 }
3128
Tomas Winklerfee12472008-04-03 16:05:21 -07003129 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08003130 goto done;
3131
Zhu Yib481de92007-09-25 17:54:57 -07003132 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
3133 !is_multicast_ether_addr(conf->bssid)) {
3134 /* If there is currently a HW scan going on in the background
3135 * then we need to cancel it else the RXON below will fail. */
Tomas Winkler2a421b92008-06-12 09:47:10 +08003136 if (iwl_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07003137 IWL_WARNING("Aborted scan still in progress "
3138 "after 100ms\n");
3139 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
3140 mutex_unlock(&priv->mutex);
3141 return -EAGAIN;
3142 }
3143 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
3144
3145 /* TODO: Audit driver for usage of these members and see
3146 * if mac80211 deprecates them (priv->bssid looks like it
3147 * shouldn't be there, but I haven't scanned the IBSS code
3148 * to verify) - jpk */
3149 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
3150
3151 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003152 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003153 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003154 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003155 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08003156 iwl_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07003157 priv, priv->active_rxon.bssid_addr, 1);
3158 }
3159
3160 } else {
Tomas Winkler2a421b92008-06-12 09:47:10 +08003161 iwl_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07003162 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003163 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003164 }
3165
Mohamed Abbasfde35712007-11-29 11:10:15 +08003166 done:
Zhu Yib481de92007-09-25 17:54:57 -07003167 spin_lock_irqsave(&priv->lock, flags);
3168 if (!conf->ssid_len)
3169 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
3170 else
3171 memcpy(priv->essid, conf->ssid, conf->ssid_len);
3172
3173 priv->essid_len = conf->ssid_len;
3174 spin_unlock_irqrestore(&priv->lock, flags);
3175
3176 IWL_DEBUG_MAC80211("leave\n");
3177 mutex_unlock(&priv->mutex);
3178
3179 return 0;
3180}
3181
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003182static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04003183 unsigned int changed_flags,
3184 unsigned int *total_flags,
3185 int mc_count, struct dev_addr_list *mc_list)
3186{
3187 /*
3188 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003189 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04003190 */
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08003191 struct iwl_priv *priv = hw->priv;
3192 int new_flags = 0;
3193 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
3194 if (*total_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
3195 IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
3196 IEEE80211_IF_TYPE_MNTR,
3197 changed_flags, *total_flags);
3198 /* queue work 'cuz mac80211 is holding a lock which
3199 * prevents us from issuing (synchronous) f/w cmds */
3200 queue_work(priv->workqueue, &priv->set_monitor);
3201 new_flags &= FIF_PROMISC_IN_BSS |
3202 FIF_OTHER_BSS |
3203 FIF_ALLMULTI;
3204 }
3205 }
3206 *total_flags = new_flags;
Johannes Berg4150c572007-09-17 01:29:23 -04003207}
3208
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003209static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003210 struct ieee80211_if_init_conf *conf)
3211{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003212 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003213
3214 IWL_DEBUG_MAC80211("enter\n");
3215
3216 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08003217
Tomas Winklerfee12472008-04-03 16:05:21 -07003218 if (iwl_is_ready_rf(priv)) {
Tomas Winkler2a421b92008-06-12 09:47:10 +08003219 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003220 cancel_delayed_work(&priv->post_associate);
3221 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3222 iwl4965_commit_rxon(priv);
3223 }
Johannes Berg32bfd352007-12-19 01:31:26 +01003224 if (priv->vif == conf->vif) {
3225 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003226 memset(priv->bssid, 0, ETH_ALEN);
3227 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
3228 priv->essid_len = 0;
3229 }
3230 mutex_unlock(&priv->mutex);
3231
3232 IWL_DEBUG_MAC80211("leave\n");
3233
3234}
Johannes Berg471b3ef2007-12-28 14:32:58 +01003235
Tomas Winkler3109ece2008-03-28 16:33:35 -07003236#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01003237static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
3238 struct ieee80211_vif *vif,
3239 struct ieee80211_bss_conf *bss_conf,
3240 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02003241{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003242 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02003243
Tomas Winkler3109ece2008-03-28 16:33:35 -07003244 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
3245
Johannes Berg471b3ef2007-12-28 14:32:58 +01003246 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07003247 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
3248 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01003249 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02003250 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
3251 else
3252 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
3253 }
3254
Johannes Berg471b3ef2007-12-28 14:32:58 +01003255 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07003256 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01003257 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02003258 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
3259 else
3260 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
3261 }
3262
Tomas Winkler98952d52008-03-28 16:33:33 -07003263 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07003264 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07003265 iwl4965_ht_conf(priv, bss_conf);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07003266 iwl_set_rxon_chain(priv);
Tomas Winkler98952d52008-03-28 16:33:33 -07003267 }
3268
Johannes Berg471b3ef2007-12-28 14:32:58 +01003269 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07003270 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
Reinette Chatre508e32e2008-04-14 21:16:13 -07003271 /* This should never happen as this function should
3272 * never be called from interrupt context. */
3273 if (WARN_ON_ONCE(in_interrupt()))
3274 return;
Tomas Winkler3109ece2008-03-28 16:33:35 -07003275 if (bss_conf->assoc) {
3276 priv->assoc_id = bss_conf->aid;
3277 priv->beacon_int = bss_conf->beacon_int;
3278 priv->timestamp = bss_conf->timestamp;
3279 priv->assoc_capability = bss_conf->assoc_capability;
3280 priv->next_scan_jiffies = jiffies +
3281 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
Reinette Chatre508e32e2008-04-14 21:16:13 -07003282 mutex_lock(&priv->mutex);
3283 iwl4965_post_associate(priv);
3284 mutex_unlock(&priv->mutex);
Tomas Winkler3109ece2008-03-28 16:33:35 -07003285 } else {
3286 priv->assoc_id = 0;
3287 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
3288 }
3289 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
3290 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
Tomas Winkler7e8c5192008-04-15 16:01:43 -07003291 iwl_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01003292 }
3293
Tomas Winkler220173b2007-10-16 00:50:25 +02003294}
Zhu Yib481de92007-09-25 17:54:57 -07003295
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003296static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07003297{
3298 int rc = 0;
3299 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003300 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003301
3302 IWL_DEBUG_MAC80211("enter\n");
3303
mabbas052c4b92007-10-25 17:15:43 +08003304 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07003305 spin_lock_irqsave(&priv->lock, flags);
3306
Tomas Winklerfee12472008-04-03 16:05:21 -07003307 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003308 rc = -EIO;
3309 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
3310 goto out_unlock;
3311 }
3312
3313 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
3314 rc = -EIO;
3315 IWL_ERROR("ERROR: APs don't scan\n");
3316 goto out_unlock;
3317 }
3318
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003319 /* we don't schedule scan within next_scan_jiffies period */
3320 if (priv->next_scan_jiffies &&
3321 time_after(priv->next_scan_jiffies, jiffies)) {
3322 rc = -EAGAIN;
3323 goto out_unlock;
3324 }
Zhu Yib481de92007-09-25 17:54:57 -07003325 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003326 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
3327 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07003328 rc = -EAGAIN;
3329 goto out_unlock;
3330 }
3331 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003332 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Tomas Winkler2a421b92008-06-12 09:47:10 +08003333 iwl_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07003334
3335 priv->one_direct_scan = 1;
3336 priv->direct_ssid_len = (u8)
3337 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
3338 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08003339 } else
3340 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003341
Tomas Winkler2a421b92008-06-12 09:47:10 +08003342 rc = iwl_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003343
3344 IWL_DEBUG_MAC80211("leave\n");
3345
3346out_unlock:
3347 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08003348 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07003349
3350 return rc;
3351}
3352
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003353static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
3354 struct ieee80211_key_conf *keyconf, const u8 *addr,
3355 u32 iv32, u16 *phase1key)
3356{
3357 struct iwl_priv *priv = hw->priv;
3358 u8 sta_id = IWL_INVALID_STATION;
3359 unsigned long flags;
3360 __le16 key_flags = 0;
3361 int i;
3362 DECLARE_MAC_BUF(mac);
3363
3364 IWL_DEBUG_MAC80211("enter\n");
3365
Tomas Winkler947b13a2008-04-16 16:34:48 -07003366 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003367 if (sta_id == IWL_INVALID_STATION) {
3368 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
3369 print_mac(mac, addr));
3370 return;
3371 }
3372
Tomas Winkler2a421b92008-06-12 09:47:10 +08003373 iwl_scan_cancel_timeout(priv, 100);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003374
3375 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
3376 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
3377 key_flags &= ~STA_KEY_FLG_INVALID;
3378
Tomas Winkler5425e492008-04-15 16:01:38 -07003379 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003380 key_flags |= STA_KEY_MULTICAST_MSK;
3381
3382 spin_lock_irqsave(&priv->sta_lock, flags);
3383
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003384 priv->stations[sta_id].sta.key.key_flags = key_flags;
3385 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
3386
3387 for (i = 0; i < 5; i++)
3388 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
3389 cpu_to_le16(phase1key[i]);
3390
3391 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3392 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3393
Tomas Winkler133636d2008-05-05 10:22:34 +08003394 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003395
3396 spin_unlock_irqrestore(&priv->sta_lock, flags);
3397
3398 IWL_DEBUG_MAC80211("leave\n");
3399}
3400
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003401static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07003402 const u8 *local_addr, const u8 *addr,
3403 struct ieee80211_key_conf *key)
3404{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003405 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07003406 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003407 int ret = 0;
3408 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003409 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003410
3411 IWL_DEBUG_MAC80211("enter\n");
3412
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07003413 if (priv->hw_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07003414 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
3415 return -EOPNOTSUPP;
3416 }
3417
3418 if (is_zero_ether_addr(addr))
3419 /* only support pairwise keys */
3420 return -EOPNOTSUPP;
3421
Tomas Winkler947b13a2008-04-16 16:34:48 -07003422 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003423 if (sta_id == IWL_INVALID_STATION) {
3424 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
3425 print_mac(mac, addr));
3426 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003427
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003428 }
Zhu Yib481de92007-09-25 17:54:57 -07003429
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003430 mutex_lock(&priv->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +08003431 iwl_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003432 mutex_unlock(&priv->mutex);
3433
3434 /* If we are getting WEP group key and we didn't receive any key mapping
3435 * so far, we are in legacy wep mode (group key only), otherwise we are
3436 * in 1X mode.
3437 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07003438 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003439 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
3440 if (cmd == SET_KEY)
3441 is_default_wep_key = !priv->key_mapping_key;
3442 else
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08003443 is_default_wep_key =
3444 (key->hw_key_idx == HW_KEY_DEFAULT);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003445 }
mabbas052c4b92007-10-25 17:15:43 +08003446
Zhu Yib481de92007-09-25 17:54:57 -07003447 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003448 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003449 if (is_default_wep_key)
3450 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003451 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07003452 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003453
3454 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003455 break;
3456 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003457 if (is_default_wep_key)
3458 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003459 else
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07003460 ret = iwl_remove_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003461
3462 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003463 break;
3464 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003465 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003466 }
3467
3468 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003469
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003470 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003471}
3472
Johannes Berge100bb62008-04-30 18:51:21 +02003473static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07003474 const struct ieee80211_tx_queue_params *params)
3475{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003476 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003477 unsigned long flags;
3478 int q;
Zhu Yib481de92007-09-25 17:54:57 -07003479
3480 IWL_DEBUG_MAC80211("enter\n");
3481
Tomas Winklerfee12472008-04-03 16:05:21 -07003482 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003483 IWL_DEBUG_MAC80211("leave - RF not ready\n");
3484 return -EIO;
3485 }
3486
3487 if (queue >= AC_NUM) {
3488 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
3489 return 0;
3490 }
3491
Zhu Yib481de92007-09-25 17:54:57 -07003492 if (!priv->qos_data.qos_enable) {
3493 priv->qos_data.qos_active = 0;
3494 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
3495 return 0;
3496 }
3497 q = AC_NUM - 1 - queue;
3498
3499 spin_lock_irqsave(&priv->lock, flags);
3500
3501 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
3502 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
3503 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
3504 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01003505 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07003506
3507 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
3508 priv->qos_data.qos_active = 1;
3509
3510 spin_unlock_irqrestore(&priv->lock, flags);
3511
3512 mutex_lock(&priv->mutex);
3513 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003514 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07003515 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003516 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003517
3518 mutex_unlock(&priv->mutex);
3519
Zhu Yib481de92007-09-25 17:54:57 -07003520 IWL_DEBUG_MAC80211("leave\n");
3521 return 0;
3522}
3523
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003524static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003525 struct ieee80211_tx_queue_stats *stats)
3526{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003527 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003528 int i, avail;
Ron Rindjunsky16466902008-05-05 10:22:50 +08003529 struct iwl_tx_queue *txq;
Tomas Winkler443cfd42008-05-15 13:53:57 +08003530 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07003531 unsigned long flags;
3532
3533 IWL_DEBUG_MAC80211("enter\n");
3534
Tomas Winklerfee12472008-04-03 16:05:21 -07003535 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003536 IWL_DEBUG_MAC80211("leave - RF not ready\n");
3537 return -EIO;
3538 }
3539
3540 spin_lock_irqsave(&priv->lock, flags);
3541
3542 for (i = 0; i < AC_NUM; i++) {
3543 txq = &priv->txq[i];
3544 q = &txq->q;
Tomas Winkler443cfd42008-05-15 13:53:57 +08003545 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07003546
Johannes Berg57ffc582008-04-29 17:18:59 +02003547 stats[i].len = q->n_window - avail;
3548 stats[i].limit = q->n_window - q->high_mark;
3549 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07003550
3551 }
3552 spin_unlock_irqrestore(&priv->lock, flags);
3553
3554 IWL_DEBUG_MAC80211("leave\n");
3555
3556 return 0;
3557}
3558
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003559static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003560 struct ieee80211_low_level_stats *stats)
3561{
Ester Kummerbf403db2008-05-05 10:22:40 +08003562 struct iwl_priv *priv = hw->priv;
3563
3564 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003565 IWL_DEBUG_MAC80211("enter\n");
3566 IWL_DEBUG_MAC80211("leave\n");
3567
3568 return 0;
3569}
3570
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003571static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003572{
Ester Kummerbf403db2008-05-05 10:22:40 +08003573 struct iwl_priv *priv;
3574
3575 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003576 IWL_DEBUG_MAC80211("enter\n");
3577 IWL_DEBUG_MAC80211("leave\n");
3578
3579 return 0;
3580}
3581
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003582static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003583{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003584 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003585 unsigned long flags;
3586
3587 mutex_lock(&priv->mutex);
3588 IWL_DEBUG_MAC80211("enter\n");
3589
3590 priv->lq_mngr.lq_ready = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003591 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02003592 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07003593 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -07003594
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07003595 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003596
3597 cancel_delayed_work(&priv->post_associate);
3598
3599 spin_lock_irqsave(&priv->lock, flags);
3600 priv->assoc_id = 0;
3601 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003602 priv->assoc_station_added = 0;
3603
3604 /* new association get rid of ibss beacon skb */
3605 if (priv->ibss_beacon)
3606 dev_kfree_skb(priv->ibss_beacon);
3607
3608 priv->ibss_beacon = NULL;
3609
3610 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07003611 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003612 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
3613 priv->beacon_int = 0;
3614
3615 spin_unlock_irqrestore(&priv->lock, flags);
3616
Tomas Winklerfee12472008-04-03 16:05:21 -07003617 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08003618 IWL_DEBUG_MAC80211("leave - not ready\n");
3619 mutex_unlock(&priv->mutex);
3620 return;
3621 }
3622
mabbas052c4b92007-10-25 17:15:43 +08003623 /* we are restarting association process
3624 * clear RXON_FILTER_ASSOC_MSK bit
3625 */
3626 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Tomas Winkler2a421b92008-06-12 09:47:10 +08003627 iwl_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08003628 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003629 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08003630 }
3631
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -07003632 iwl_power_update_mode(priv, 0);
3633
Zhu Yib481de92007-09-25 17:54:57 -07003634 /* Per mac80211.h: This is only used in IBSS mode... */
3635 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08003636
Zhu Yib481de92007-09-25 17:54:57 -07003637 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
3638 mutex_unlock(&priv->mutex);
3639 return;
3640 }
3641
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003642 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003643
3644 mutex_unlock(&priv->mutex);
3645
3646 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003647}
3648
Johannes Berge039fa42008-05-15 12:55:29 +02003649static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003650{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003651 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003652 unsigned long flags;
3653
3654 mutex_lock(&priv->mutex);
3655 IWL_DEBUG_MAC80211("enter\n");
3656
Tomas Winklerfee12472008-04-03 16:05:21 -07003657 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003658 IWL_DEBUG_MAC80211("leave - RF not ready\n");
3659 mutex_unlock(&priv->mutex);
3660 return -EIO;
3661 }
3662
3663 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
3664 IWL_DEBUG_MAC80211("leave - not IBSS\n");
3665 mutex_unlock(&priv->mutex);
3666 return -EIO;
3667 }
3668
3669 spin_lock_irqsave(&priv->lock, flags);
3670
3671 if (priv->ibss_beacon)
3672 dev_kfree_skb(priv->ibss_beacon);
3673
3674 priv->ibss_beacon = skb;
3675
3676 priv->assoc_id = 0;
3677
3678 IWL_DEBUG_MAC80211("leave\n");
3679 spin_unlock_irqrestore(&priv->lock, flags);
3680
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07003681 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003682
Assaf Kraussc46fbef2008-06-12 09:47:05 +08003683 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003684
3685 mutex_unlock(&priv->mutex);
3686
3687 return 0;
3688}
3689
Zhu Yib481de92007-09-25 17:54:57 -07003690/*****************************************************************************
3691 *
3692 * sysfs attributes
3693 *
3694 *****************************************************************************/
3695
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003696#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003697
3698/*
3699 * The following adds a new attribute to the sysfs representation
3700 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3701 * used for controlling the debug level.
3702 *
3703 * See the level definitions in iwl for details.
3704 */
3705
Ester Kummer8cf769c2008-05-06 11:05:11 +08003706static ssize_t show_debug_level(struct device *d,
3707 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003708{
Ester Kummer8cf769c2008-05-06 11:05:11 +08003709 struct iwl_priv *priv = d->driver_data;
3710
3711 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003712}
Ester Kummer8cf769c2008-05-06 11:05:11 +08003713static ssize_t store_debug_level(struct device *d,
3714 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003715 const char *buf, size_t count)
3716{
Ester Kummer8cf769c2008-05-06 11:05:11 +08003717 struct iwl_priv *priv = d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003718 char *p = (char *)buf;
3719 u32 val;
3720
3721 val = simple_strtoul(p, &p, 0);
3722 if (p == buf)
3723 printk(KERN_INFO DRV_NAME
3724 ": %s is not in hex or decimal form.\n", buf);
3725 else
Ester Kummer8cf769c2008-05-06 11:05:11 +08003726 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07003727
3728 return strnlen(buf, count);
3729}
3730
Ester Kummer8cf769c2008-05-06 11:05:11 +08003731static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3732 show_debug_level, store_debug_level);
3733
Zhu Yib481de92007-09-25 17:54:57 -07003734
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003735#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003736
Zhu Yib481de92007-09-25 17:54:57 -07003737
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08003738static ssize_t show_version(struct device *d,
3739 struct device_attribute *attr, char *buf)
3740{
3741 struct iwl_priv *priv = d->driver_data;
Tomas Winkler885ba202008-05-29 16:34:55 +08003742 struct iwl_alive_resp *palive = &priv->card_alive;
Tomas Winklerf236a262008-06-30 17:23:02 +08003743 ssize_t pos = 0;
3744 u16 eeprom_ver;
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08003745
3746 if (palive->is_valid)
Tomas Winklerf236a262008-06-30 17:23:02 +08003747 pos += sprintf(buf + pos,
3748 "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
3749 "fw type: 0x%01X 0x%01X\n",
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08003750 palive->ucode_major, palive->ucode_minor,
3751 palive->sw_rev[0], palive->sw_rev[1],
3752 palive->ver_type, palive->ver_subtype);
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08003753 else
Tomas Winklerf236a262008-06-30 17:23:02 +08003754 pos += sprintf(buf + pos, "fw not loaded\n");
3755
3756 if (priv->eeprom) {
3757 eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
3758 pos += sprintf(buf + pos, "EEPROM version: 0x%x\n",
3759 eeprom_ver);
3760 } else {
3761 pos += sprintf(buf + pos, "EEPROM not initialzed\n");
3762 }
3763
3764 return pos;
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08003765}
3766
3767static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
3768
Zhu Yib481de92007-09-25 17:54:57 -07003769static ssize_t show_temperature(struct device *d,
3770 struct device_attribute *attr, char *buf)
3771{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003772 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003773
Tomas Winklerfee12472008-04-03 16:05:21 -07003774 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003775 return -EAGAIN;
3776
Emmanuel Grumbach91dbc5b2008-06-12 09:47:14 +08003777 return sprintf(buf, "%d\n", priv->temperature);
Zhu Yib481de92007-09-25 17:54:57 -07003778}
3779
3780static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3781
3782static ssize_t show_rs_window(struct device *d,
3783 struct device_attribute *attr,
3784 char *buf)
3785{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003786 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003787 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003788}
3789static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
3790
3791static ssize_t show_tx_power(struct device *d,
3792 struct device_attribute *attr, char *buf)
3793{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003794 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winkler630fe9b2008-06-12 09:47:08 +08003795 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07003796}
3797
3798static ssize_t store_tx_power(struct device *d,
3799 struct device_attribute *attr,
3800 const char *buf, size_t count)
3801{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003802 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003803 char *p = (char *)buf;
3804 u32 val;
3805
3806 val = simple_strtoul(p, &p, 10);
3807 if (p == buf)
3808 printk(KERN_INFO DRV_NAME
3809 ": %s is not in decimal form.\n", buf);
3810 else
Tomas Winkler630fe9b2008-06-12 09:47:08 +08003811 iwl_set_tx_power(priv, val, false);
Zhu Yib481de92007-09-25 17:54:57 -07003812
3813 return count;
3814}
3815
3816static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3817
3818static ssize_t show_flags(struct device *d,
3819 struct device_attribute *attr, char *buf)
3820{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003821 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003822
3823 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
3824}
3825
3826static ssize_t store_flags(struct device *d,
3827 struct device_attribute *attr,
3828 const char *buf, size_t count)
3829{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003830 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003831 u32 flags = simple_strtoul(buf, NULL, 0);
3832
3833 mutex_lock(&priv->mutex);
3834 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
3835 /* Cancel any currently running scans... */
Tomas Winkler2a421b92008-06-12 09:47:10 +08003836 if (iwl_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07003837 IWL_WARNING("Could not cancel scan.\n");
3838 else {
3839 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
3840 flags);
3841 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003842 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003843 }
3844 }
3845 mutex_unlock(&priv->mutex);
3846
3847 return count;
3848}
3849
3850static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3851
3852static ssize_t show_filter_flags(struct device *d,
3853 struct device_attribute *attr, char *buf)
3854{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003855 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003856
3857 return sprintf(buf, "0x%04X\n",
3858 le32_to_cpu(priv->active_rxon.filter_flags));
3859}
3860
3861static ssize_t store_filter_flags(struct device *d,
3862 struct device_attribute *attr,
3863 const char *buf, size_t count)
3864{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003865 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003866 u32 filter_flags = simple_strtoul(buf, NULL, 0);
3867
3868 mutex_lock(&priv->mutex);
3869 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
3870 /* Cancel any currently running scans... */
Tomas Winkler2a421b92008-06-12 09:47:10 +08003871 if (iwl_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07003872 IWL_WARNING("Could not cancel scan.\n");
3873 else {
3874 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
3875 "0x%04X\n", filter_flags);
3876 priv->staging_rxon.filter_flags =
3877 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003878 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003879 }
3880 }
3881 mutex_unlock(&priv->mutex);
3882
3883 return count;
3884}
3885
3886static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3887 store_filter_flags);
3888
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003889#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07003890
3891static ssize_t show_measurement(struct device *d,
3892 struct device_attribute *attr, char *buf)
3893{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003894 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003895 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003896 u32 size = sizeof(measure_report), len = 0, ofs = 0;
3897 u8 *data = (u8 *) & measure_report;
3898 unsigned long flags;
3899
3900 spin_lock_irqsave(&priv->lock, flags);
3901 if (!(priv->measurement_status & MEASUREMENT_READY)) {
3902 spin_unlock_irqrestore(&priv->lock, flags);
3903 return 0;
3904 }
3905 memcpy(&measure_report, &priv->measure_report, size);
3906 priv->measurement_status = 0;
3907 spin_unlock_irqrestore(&priv->lock, flags);
3908
3909 while (size && (PAGE_SIZE - len)) {
3910 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3911 PAGE_SIZE - len, 1);
3912 len = strlen(buf);
3913 if (PAGE_SIZE - len)
3914 buf[len++] = '\n';
3915
3916 ofs += 16;
3917 size -= min(size, 16U);
3918 }
3919
3920 return len;
3921}
3922
3923static ssize_t store_measurement(struct device *d,
3924 struct device_attribute *attr,
3925 const char *buf, size_t count)
3926{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003927 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003928 struct ieee80211_measurement_params params = {
3929 .channel = le16_to_cpu(priv->active_rxon.channel),
3930 .start_time = cpu_to_le64(priv->last_tsf),
3931 .duration = cpu_to_le16(1),
3932 };
3933 u8 type = IWL_MEASURE_BASIC;
3934 u8 buffer[32];
3935 u8 channel;
3936
3937 if (count) {
3938 char *p = buffer;
3939 strncpy(buffer, buf, min(sizeof(buffer), count));
3940 channel = simple_strtoul(p, NULL, 0);
3941 if (channel)
3942 params.channel = channel;
3943
3944 p = buffer;
3945 while (*p && *p != ' ')
3946 p++;
3947 if (*p)
3948 type = simple_strtoul(p + 1, NULL, 0);
3949 }
3950
3951 IWL_DEBUG_INFO("Invoking measurement of type %d on "
3952 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003953 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07003954
3955 return count;
3956}
3957
3958static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
3959 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003960#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07003961
3962static ssize_t store_retry_rate(struct device *d,
3963 struct device_attribute *attr,
3964 const char *buf, size_t count)
3965{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003966 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003967
3968 priv->retry_rate = simple_strtoul(buf, NULL, 0);
3969 if (priv->retry_rate <= 0)
3970 priv->retry_rate = 1;
3971
3972 return count;
3973}
3974
3975static ssize_t show_retry_rate(struct device *d,
3976 struct device_attribute *attr, char *buf)
3977{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003978 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003979 return sprintf(buf, "%d", priv->retry_rate);
3980}
3981
3982static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
3983 store_retry_rate);
3984
3985static ssize_t store_power_level(struct device *d,
3986 struct device_attribute *attr,
3987 const char *buf, size_t count)
3988{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003989 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003990 int rc;
3991 int mode;
3992
3993 mode = simple_strtoul(buf, NULL, 0);
3994 mutex_lock(&priv->mutex);
3995
Tomas Winklerfee12472008-04-03 16:05:21 -07003996 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003997 rc = -EAGAIN;
3998 goto out;
3999 }
4000
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -07004001 rc = iwl_power_set_user_mode(priv, mode);
4002 if (rc) {
4003 IWL_DEBUG_MAC80211("failed setting power mode.\n");
4004 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004005 }
Zhu Yib481de92007-09-25 17:54:57 -07004006 rc = count;
4007
4008 out:
4009 mutex_unlock(&priv->mutex);
4010 return rc;
4011}
4012
4013#define MAX_WX_STRING 80
4014
4015/* Values are in microsecond */
4016static const s32 timeout_duration[] = {
4017 350000,
4018 250000,
4019 75000,
4020 37000,
4021 25000,
4022};
4023static const s32 period_duration[] = {
4024 400000,
4025 700000,
4026 1000000,
4027 1000000,
4028 1000000
4029};
4030
4031static ssize_t show_power_level(struct device *d,
4032 struct device_attribute *attr, char *buf)
4033{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004034 struct iwl_priv *priv = dev_get_drvdata(d);
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -07004035 int level = priv->power_data.power_mode;
Zhu Yib481de92007-09-25 17:54:57 -07004036 char *p = buf;
4037
4038 p += sprintf(p, "%d ", level);
4039 switch (level) {
4040 case IWL_POWER_MODE_CAM:
4041 case IWL_POWER_AC:
4042 p += sprintf(p, "(AC)");
4043 break;
4044 case IWL_POWER_BATTERY:
4045 p += sprintf(p, "(BATTERY)");
4046 break;
4047 default:
4048 p += sprintf(p,
4049 "(Timeout %dms, Period %dms)",
4050 timeout_duration[level - 1] / 1000,
4051 period_duration[level - 1] / 1000);
4052 }
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -07004053/*
Zhu Yib481de92007-09-25 17:54:57 -07004054 if (!(priv->power_mode & IWL_POWER_ENABLED))
4055 p += sprintf(p, " OFF\n");
4056 else
4057 p += sprintf(p, " \n");
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -07004058*/
4059 p += sprintf(p, " \n");
Zhu Yib481de92007-09-25 17:54:57 -07004060 return (p - buf + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004061}
4062
4063static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
4064 store_power_level);
4065
4066static ssize_t show_channels(struct device *d,
4067 struct device_attribute *attr, char *buf)
4068{
Ester Kummer5d72a1f2008-06-13 15:44:53 +08004069
4070 struct iwl_priv *priv = dev_get_drvdata(d);
4071 struct ieee80211_channel *channels = NULL;
4072 const struct ieee80211_supported_band *supp_band = NULL;
4073 int len = 0, i;
4074 int count = 0;
4075
4076 if (!test_bit(STATUS_GEO_CONFIGURED, &priv->status))
4077 return -EAGAIN;
4078
4079 supp_band = iwl_get_hw_mode(priv, IEEE80211_BAND_2GHZ);
4080 channels = supp_band->channels;
4081 count = supp_band->n_channels;
4082
4083 len += sprintf(&buf[len],
4084 "Displaying %d channels in 2.4GHz band "
4085 "(802.11bg):\n", count);
4086
4087 for (i = 0; i < count; i++)
4088 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
4089 ieee80211_frequency_to_channel(
4090 channels[i].center_freq),
4091 channels[i].max_power,
4092 channels[i].flags & IEEE80211_CHAN_RADAR ?
4093 " (IEEE 802.11h required)" : "",
4094 (!(channels[i].flags & IEEE80211_CHAN_NO_IBSS)
4095 || (channels[i].flags &
4096 IEEE80211_CHAN_RADAR)) ? "" :
4097 ", IBSS",
4098 channels[i].flags &
4099 IEEE80211_CHAN_PASSIVE_SCAN ?
4100 "passive only" : "active/passive");
4101
4102 supp_band = iwl_get_hw_mode(priv, IEEE80211_BAND_5GHZ);
4103 channels = supp_band->channels;
4104 count = supp_band->n_channels;
4105
4106 len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
4107 "(802.11a):\n", count);
4108
4109 for (i = 0; i < count; i++)
4110 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
4111 ieee80211_frequency_to_channel(
4112 channels[i].center_freq),
4113 channels[i].max_power,
4114 channels[i].flags & IEEE80211_CHAN_RADAR ?
4115 " (IEEE 802.11h required)" : "",
4116 ((channels[i].flags & IEEE80211_CHAN_NO_IBSS)
4117 || (channels[i].flags &
4118 IEEE80211_CHAN_RADAR)) ? "" :
4119 ", IBSS",
4120 channels[i].flags &
4121 IEEE80211_CHAN_PASSIVE_SCAN ?
4122 "passive only" : "active/passive");
4123
4124 return len;
Zhu Yib481de92007-09-25 17:54:57 -07004125}
4126
4127static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
4128
4129static ssize_t show_statistics(struct device *d,
4130 struct device_attribute *attr, char *buf)
4131{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004132 struct iwl_priv *priv = dev_get_drvdata(d);
Emmanuel Grumbach8f91aec2008-06-30 17:23:07 +08004133 u32 size = sizeof(struct iwl_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07004134 u32 len = 0, ofs = 0;
4135 u8 *data = (u8 *) & priv->statistics;
4136 int rc = 0;
4137
Tomas Winklerfee12472008-04-03 16:05:21 -07004138 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004139 return -EAGAIN;
4140
4141 mutex_lock(&priv->mutex);
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07004142 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004143 mutex_unlock(&priv->mutex);
4144
4145 if (rc) {
4146 len = sprintf(buf,
4147 "Error sending statistics request: 0x%08X\n", rc);
4148 return len;
4149 }
4150
4151 while (size && (PAGE_SIZE - len)) {
4152 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4153 PAGE_SIZE - len, 1);
4154 len = strlen(buf);
4155 if (PAGE_SIZE - len)
4156 buf[len++] = '\n';
4157
4158 ofs += 16;
4159 size -= min(size, 16U);
4160 }
4161
4162 return len;
4163}
4164
4165static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
4166
Zhu Yib481de92007-09-25 17:54:57 -07004167static ssize_t show_status(struct device *d,
4168 struct device_attribute *attr, char *buf)
4169{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004170 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07004171 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004172 return -EAGAIN;
4173 return sprintf(buf, "0x%08x\n", (int)priv->status);
4174}
4175
4176static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
4177
Zhu Yib481de92007-09-25 17:54:57 -07004178/*****************************************************************************
4179 *
4180 * driver setup and teardown
4181 *
4182 *****************************************************************************/
4183
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004184static void iwl_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004185{
4186 priv->workqueue = create_workqueue(DRV_NAME);
4187
4188 init_waitqueue_head(&priv->wait_command_queue);
4189
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004190 INIT_WORK(&priv->up, iwl4965_bg_up);
4191 INIT_WORK(&priv->restart, iwl4965_bg_restart);
4192 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004193 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
4194 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08004195 INIT_WORK(&priv->set_monitor, iwl4965_bg_set_monitor);
Emmanuel Grumbach16e727e2008-06-12 09:46:52 +08004196 INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004197 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08004198 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
4199 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
Tomas Winkler2a421b92008-06-12 09:47:10 +08004200
4201 /* FIXME : remove when resolved PENDING */
4202 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
4203 iwl_setup_scan_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004204
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004205 if (priv->cfg->ops->lib->setup_deferred_work)
4206 priv->cfg->ops->lib->setup_deferred_work(priv);
4207
4208 init_timer(&priv->statistics_periodic);
4209 priv->statistics_periodic.data = (unsigned long)priv;
4210 priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
Zhu Yib481de92007-09-25 17:54:57 -07004211
4212 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004213 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07004214}
4215
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004216static void iwl_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004217{
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004218 if (priv->cfg->ops->lib->cancel_deferred_work)
4219 priv->cfg->ops->lib->cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004220
Joonwoo Park3ae6a052007-11-29 10:43:16 +09004221 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07004222 cancel_delayed_work(&priv->scan_check);
4223 cancel_delayed_work(&priv->alive_start);
4224 cancel_delayed_work(&priv->post_associate);
4225 cancel_work_sync(&priv->beacon_update);
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004226 del_timer_sync(&priv->statistics_periodic);
Zhu Yib481de92007-09-25 17:54:57 -07004227}
4228
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004229static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07004230 &dev_attr_channels.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004231 &dev_attr_flags.attr,
4232 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004233#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07004234 &dev_attr_measurement.attr,
4235#endif
4236 &dev_attr_power_level.attr,
4237 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004238 &dev_attr_rs_window.attr,
4239 &dev_attr_statistics.attr,
4240 &dev_attr_status.attr,
4241 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004242 &dev_attr_tx_power.attr,
Ester Kummer8cf769c2008-05-06 11:05:11 +08004243#ifdef CONFIG_IWLWIFI_DEBUG
4244 &dev_attr_debug_level.attr,
4245#endif
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08004246 &dev_attr_version.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004247
4248 NULL
4249};
4250
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004251static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07004252 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004253 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07004254};
4255
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004256static struct ieee80211_ops iwl4965_hw_ops = {
4257 .tx = iwl4965_mac_tx,
4258 .start = iwl4965_mac_start,
4259 .stop = iwl4965_mac_stop,
4260 .add_interface = iwl4965_mac_add_interface,
4261 .remove_interface = iwl4965_mac_remove_interface,
4262 .config = iwl4965_mac_config,
4263 .config_interface = iwl4965_mac_config_interface,
4264 .configure_filter = iwl4965_configure_filter,
4265 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004266 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004267 .get_stats = iwl4965_mac_get_stats,
4268 .get_tx_stats = iwl4965_mac_get_tx_stats,
4269 .conf_tx = iwl4965_mac_conf_tx,
4270 .get_tsf = iwl4965_mac_get_tsf,
4271 .reset_tsf = iwl4965_mac_reset_tsf,
4272 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01004273 .bss_info_changed = iwl4965_bss_info_changed,
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02004274 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004275 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07004276};
4277
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004278static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07004279{
4280 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004281 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07004282 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08004283 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004284 unsigned long flags;
Zhu Yi5a669262008-01-14 17:46:18 -08004285 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07004286
Assaf Krauss316c30d2008-03-14 10:38:46 -07004287 /************************
4288 * 1. Allocating HW data
4289 ************************/
4290
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004291 /* Disabling hardware scan means that mac80211 will perform scans
4292 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07004293 if (cfg->mod_params->disable_hw_scan) {
Ester Kummerbf403db2008-05-05 10:22:40 +08004294 if (cfg->mod_params->debug & IWL_DL_INFO)
4295 dev_printk(KERN_DEBUG, &(pdev->dev),
4296 "Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004297 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07004298 }
4299
Assaf Krauss1d0a0822008-03-14 10:38:48 -07004300 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
4301 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07004302 err = -ENOMEM;
4303 goto out;
4304 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07004305 priv = hw->priv;
4306 /* At this point both hw and priv are allocated. */
4307
Zhu Yib481de92007-09-25 17:54:57 -07004308 SET_IEEE80211_DEV(hw, &pdev->dev);
4309
4310 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08004311 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07004312 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07004313
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004314#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08004315 priv->debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07004316 atomic_set(&priv->restrict_refcnt, 0);
4317#endif
Zhu Yib481de92007-09-25 17:54:57 -07004318
Assaf Krauss316c30d2008-03-14 10:38:46 -07004319 /**************************
4320 * 2. Initializing PCI bus
4321 **************************/
4322 if (pci_enable_device(pdev)) {
4323 err = -ENODEV;
4324 goto out_ieee80211_free_hw;
4325 }
4326
4327 pci_set_master(pdev);
4328
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07004329 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004330 if (!err)
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07004331 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
4332 if (err) {
4333 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
4334 if (!err)
4335 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
4336 /* both attempts failed: */
Assaf Krauss316c30d2008-03-14 10:38:46 -07004337 if (err) {
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07004338 printk(KERN_WARNING "%s: No suitable DMA available.\n",
4339 DRV_NAME);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004340 goto out_pci_disable_device;
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07004341 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07004342 }
4343
4344 err = pci_request_regions(pdev, DRV_NAME);
4345 if (err)
4346 goto out_pci_disable_device;
4347
4348 pci_set_drvdata(pdev, priv);
4349
4350 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4351 * PCI Tx retries from interfering with C3 CPU state */
4352 pci_write_config_byte(pdev, 0x41, 0x00);
4353
4354 /***********************
4355 * 3. Read REV register
4356 ***********************/
4357 priv->hw_base = pci_iomap(pdev, 0, 0);
4358 if (!priv->hw_base) {
4359 err = -ENODEV;
4360 goto out_pci_release_regions;
4361 }
4362
4363 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
4364 (unsigned long long) pci_resource_len(pdev, 0));
4365 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
4366
Tomas Winklerb661c812008-04-23 17:14:54 -07004367 iwl_hw_detect(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004368 printk(KERN_INFO DRV_NAME
Tomas Winklerb661c812008-04-23 17:14:54 -07004369 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
4370 priv->cfg->name, priv->hw_rev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004371
Tomas Winkler91238712008-04-23 17:14:53 -07004372 /* amp init */
4373 err = priv->cfg->ops->lib->apm_ops.init(priv);
4374 if (err < 0) {
4375 IWL_DEBUG_INFO("Failed to init APMG\n");
4376 goto out_iounmap;
4377 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07004378 /*****************
4379 * 4. Read EEPROM
4380 *****************/
Assaf Krauss316c30d2008-03-14 10:38:46 -07004381 /* Read the EEPROM */
4382 err = iwl_eeprom_init(priv);
4383 if (err) {
4384 IWL_ERROR("Unable to init EEPROM\n");
4385 goto out_iounmap;
4386 }
Tomas Winkler8614f362008-04-23 17:14:55 -07004387 err = iwl_eeprom_check_version(priv);
4388 if (err)
4389 goto out_iounmap;
4390
Ron Rindjunsky02883012008-05-15 13:53:53 +08004391 /* extract MAC Address */
Assaf Krauss316c30d2008-03-14 10:38:46 -07004392 iwl_eeprom_get_mac(priv, priv->mac_addr);
4393 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
4394 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
4395
4396 /************************
4397 * 5. Setup HW constants
4398 ************************/
4399 /* Device-specific setup */
Tomas Winkler5425e492008-04-15 16:01:38 -07004400 if (priv->cfg->ops->lib->set_hw_params(priv)) {
4401 IWL_ERROR("failed to set hw parameters\n");
Tomas Winkler073d3f52008-04-21 15:41:52 -07004402 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07004403 }
4404
4405 /*******************
Tomas Winkler6ba87952008-05-15 13:54:17 +08004406 * 6. Setup priv
Assaf Krauss316c30d2008-03-14 10:38:46 -07004407 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07004408
Tomas Winkler6ba87952008-05-15 13:54:17 +08004409 err = iwl_init_drv(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004410 if (err)
Ron Rindjunsky399f49002008-04-23 17:14:56 -07004411 goto out_free_eeprom;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004412 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07004413
4414 /**********************************
4415 * 7. Initialize module parameters
4416 **********************************/
4417
4418 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07004419 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07004420 set_bit(STATUS_RF_KILL_SW, &priv->status);
4421 IWL_DEBUG_INFO("Radio disabled.\n");
4422 }
4423
Assaf Krauss316c30d2008-03-14 10:38:46 -07004424 /********************
4425 * 8. Setup services
4426 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004427 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004428 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004429 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004430
4431 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
4432 if (err) {
4433 IWL_ERROR("failed to create sysfs device attributes\n");
Tomas Winkler6ba87952008-05-15 13:54:17 +08004434 goto out_uninit_drv;
Assaf Krauss316c30d2008-03-14 10:38:46 -07004435 }
4436
Assaf Krauss316c30d2008-03-14 10:38:46 -07004437
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004438 iwl_setup_deferred_work(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004439 iwl4965_setup_rx_handlers(priv);
4440
4441 /********************
4442 * 9. Conclude
4443 ********************/
Zhu Yi5a669262008-01-14 17:46:18 -08004444 pci_save_state(pdev);
4445 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004446
Tomas Winkler6ba87952008-05-15 13:54:17 +08004447 /**********************************
4448 * 10. Setup and register mac80211
4449 **********************************/
4450
4451 err = iwl_setup_mac(priv);
4452 if (err)
4453 goto out_remove_sysfs;
4454
4455 err = iwl_dbgfs_register(priv, DRV_NAME);
4456 if (err)
4457 IWL_ERROR("failed to create debugfs files\n");
4458
Emmanuel Grumbach58d0f362008-06-12 09:47:19 +08004459 err = iwl_rfkill_init(priv);
4460 if (err)
4461 IWL_ERROR("Unable to initialize RFKILL system. "
4462 "Ignoring error: %d\n", err);
4463 iwl_power_initialize(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004464 return 0;
4465
Assaf Krauss316c30d2008-03-14 10:38:46 -07004466 out_remove_sysfs:
4467 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Tomas Winkler6ba87952008-05-15 13:54:17 +08004468 out_uninit_drv:
4469 iwl_uninit_drv(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07004470 out_free_eeprom:
4471 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004472 out_iounmap:
4473 pci_iounmap(pdev, priv->hw_base);
4474 out_pci_release_regions:
4475 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004476 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07004477 out_pci_disable_device:
4478 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004479 out_ieee80211_free_hw:
4480 ieee80211_free_hw(priv->hw);
4481 out:
4482 return err;
4483}
4484
Reinette Chatrec83dbf62008-03-21 13:53:41 -07004485static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004486{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004487 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004488 struct list_head *p, *q;
4489 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004490 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004491
4492 if (!priv)
4493 return;
4494
4495 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
4496
Emmanuel Grumbach67249622008-05-29 16:35:26 +08004497 iwl_dbgfs_unregister(priv);
4498 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
4499
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07004500 if (priv->mac80211_registered) {
4501 ieee80211_unregister_hw(priv->hw);
4502 priv->mac80211_registered = 0;
4503 }
4504
Zhu Yib481de92007-09-25 17:54:57 -07004505 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08004506
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004507 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004508
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004509 /* make sure we flush any pending irq or
4510 * tasklet for the driver
4511 */
4512 spin_lock_irqsave(&priv->lock, flags);
4513 iwl4965_disable_interrupts(priv);
4514 spin_unlock_irqrestore(&priv->lock, flags);
4515
4516 iwl_synchronize_irq(priv);
4517
Zhu Yib481de92007-09-25 17:54:57 -07004518 /* Free MAC hash list for ADHOC */
4519 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
4520 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
4521 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004522 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07004523 }
4524 }
4525
Emmanuel Grumbach58d0f362008-06-12 09:47:19 +08004526 iwl_rfkill_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004527 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004528
4529 if (priv->rxq.bd)
Tomas Winklera55360e2008-05-05 10:22:28 +08004530 iwl_rx_queue_free(priv, &priv->rxq);
Ron Rindjunsky1053d352008-05-05 10:22:43 +08004531 iwl_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004532
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004533 iwlcore_clear_stations_table(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07004534 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004535
Zhu Yib481de92007-09-25 17:54:57 -07004536
Mohamed Abbas948c1712007-10-25 17:15:45 +08004537 /*netif_stop_queue(dev); */
4538 flush_workqueue(priv->workqueue);
4539
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004540 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07004541 * priv->workqueue... so we can't take down the workqueue
4542 * until now... */
4543 destroy_workqueue(priv->workqueue);
4544 priv->workqueue = NULL;
4545
Zhu Yib481de92007-09-25 17:54:57 -07004546 pci_iounmap(pdev, priv->hw_base);
4547 pci_release_regions(pdev);
4548 pci_disable_device(pdev);
4549 pci_set_drvdata(pdev, NULL);
4550
Tomas Winkler6ba87952008-05-15 13:54:17 +08004551 iwl_uninit_drv(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004552
4553 if (priv->ibss_beacon)
4554 dev_kfree_skb(priv->ibss_beacon);
4555
4556 ieee80211_free_hw(priv->hw);
4557}
4558
4559#ifdef CONFIG_PM
4560
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004561static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07004562{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004563 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004564
Zhu Yie655b9f2008-01-24 02:19:38 -08004565 if (priv->is_open) {
4566 set_bit(STATUS_IN_SUSPEND, &priv->status);
4567 iwl4965_mac_stop(priv->hw);
4568 priv->is_open = 1;
4569 }
Zhu Yib481de92007-09-25 17:54:57 -07004570
Zhu Yib481de92007-09-25 17:54:57 -07004571 pci_set_power_state(pdev, PCI_D3hot);
4572
Zhu Yib481de92007-09-25 17:54:57 -07004573 return 0;
4574}
4575
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004576static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004577{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004578 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004579
Zhu Yib481de92007-09-25 17:54:57 -07004580 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07004581
Zhu Yie655b9f2008-01-24 02:19:38 -08004582 if (priv->is_open)
4583 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004584
Zhu Yie655b9f2008-01-24 02:19:38 -08004585 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004586 return 0;
4587}
4588
4589#endif /* CONFIG_PM */
4590
4591/*****************************************************************************
4592 *
4593 * driver and module entry point
4594 *
4595 *****************************************************************************/
4596
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004597/* Hardware specific file defines the PCI IDs table for that hardware module */
4598static struct pci_device_id iwl_hw_card_ids[] = {
4599 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
4600 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
Tomas Winkler5a6a2562008-04-24 11:55:23 -07004601#ifdef CONFIG_IWL5000
4602 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
4603 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
4604 {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
4605#endif /* CONFIG_IWL5000 */
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004606 {0}
4607};
4608MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
4609
4610static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07004611 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004612 .id_table = iwl_hw_card_ids,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004613 .probe = iwl4965_pci_probe,
4614 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07004615#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004616 .suspend = iwl4965_pci_suspend,
4617 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07004618#endif
4619};
4620
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004621static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07004622{
4623
4624 int ret;
4625 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
4626 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004627
4628 ret = iwl4965_rate_control_register();
4629 if (ret) {
4630 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
4631 return ret;
4632 }
4633
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004634 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07004635 if (ret) {
4636 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004637 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07004638 }
Zhu Yib481de92007-09-25 17:54:57 -07004639
4640 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004641
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004642error_register:
4643 iwl4965_rate_control_unregister();
4644 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004645}
4646
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004647static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07004648{
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004649 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004650 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07004651}
4652
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004653module_exit(iwl4965_exit);
4654module_init(iwl4965_init);