blob: 5aec7d5fb9cc67638120aaeca6581ee09e8a069e [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 */
195 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
196 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);
Zhu Yib481de92007-09-25 17:54:57 -0700244 int rc = 0;
245
Tomas Winklerfee12472008-04-03 16:05:21 -0700246 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700247 return -1;
248
249 /* always get timestamp with Rx frame */
250 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
251
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800252 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700253 if (rc) {
254 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
255 return -EINVAL;
256 }
257
258 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800259 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700260 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800261 if (!iwl4965_full_rxon_required(priv)) {
Tomas Winkler7e8c5192008-04-15 16:01:43 -0700262 rc = iwl_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700263 if (rc) {
264 IWL_ERROR("Error setting RXON_ASSOC "
265 "configuration (%d).\n", rc);
266 return rc;
267 }
268
269 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
270
271 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 */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700281 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700282 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
283 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
284 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
285
Tomas Winkler857485c2008-03-21 13:53:44 -0700286 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800287 sizeof(struct iwl_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700288 &priv->active_rxon);
289
290 /* If the mask clearing failed then we set
291 * active_rxon back to what it was previously */
292 if (rc) {
293 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
294 IWL_ERROR("Error clearing ASSOC_MSK on current "
295 "configuration (%d).\n", rc);
296 return rc;
297 }
Zhu Yib481de92007-09-25 17:54:57 -0700298 }
299
300 IWL_DEBUG_INFO("Sending RXON\n"
301 "* with%s RXON_FILTER_ASSOC_MSK\n"
302 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -0700303 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700304 ((priv->staging_rxon.filter_flags &
305 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
306 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -0700307 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -0700308
Ron Rindjunsky099b40b2008-04-21 15:41:53 -0700309 iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
Zhu Yib481de92007-09-25 17:54:57 -0700310 /* Apply the new configuration */
Tomas Winkler857485c2008-03-21 13:53:44 -0700311 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800312 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700313 if (rc) {
314 IWL_ERROR("Error setting new configuration (%d).\n", rc);
315 return rc;
316 }
317
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800318 iwl_remove_station(priv, iwl_bcast_addr, 0);
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700319 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800320
Zhu Yib481de92007-09-25 17:54:57 -0700321 if (!priv->error_recovering)
322 priv->start_calib = 0;
323
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700324 iwl_init_sensitivity(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700325
326 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
327
328 /* If we issue a new RXON command which required a tune then we must
329 * send a new TXPOWER command or we won't be able to Tx any frames */
Tomas Winkler630fe9b2008-06-12 09:47:08 +0800330 rc = iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
Zhu Yib481de92007-09-25 17:54:57 -0700331 if (rc) {
Tomas Winkler630fe9b2008-06-12 09:47:08 +0800332 IWL_ERROR("Error sending TX power (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700333 return rc;
334 }
335
336 /* Add the broadcast address so we can send broadcast frames */
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800337 if (iwl_rxon_add_station(priv, iwl_bcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700338 IWL_INVALID_STATION) {
339 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
340 return -EIO;
341 }
342
343 /* If we have set the ASSOC_MSK and we are in BSS mode then
344 * add the IWL_AP_ID to the station rate table */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700345 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700346 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800347 if (iwl_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700348 == IWL_INVALID_STATION) {
349 IWL_ERROR("Error adding AP address for transmit.\n");
350 return -EIO;
351 }
352 priv->assoc_station_added = 1;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700353 if (priv->default_wep_key &&
354 iwl_send_static_wepkey_cmd(priv, 0))
355 IWL_ERROR("Could not send WEP static key.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700356 }
357
358 return 0;
359}
360
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700361void iwl4965_update_chain_flags(struct iwl_priv *priv)
362{
363
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -0700364 iwl_set_rxon_chain(priv);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700365 iwl4965_commit_rxon(priv);
366}
367
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700368static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700369{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800370 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700371 .flags = 3,
372 .lead_time = 0xAA,
373 .max_kill = 1,
374 .kill_ack_mask = 0,
375 .kill_cts_mask = 0,
376 };
377
Tomas Winkler857485c2008-03-21 13:53:44 -0700378 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800379 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700380}
381
Zhu Yib481de92007-09-25 17:54:57 -0700382/*
383 * CARD_STATE_CMD
384 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800385 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -0700386 *
387 * When in the 'enable' state the card operates as normal.
388 * When in the 'disable' state, the card enters into a low power mode.
389 * When in the 'halt' state, the card is shut down and must be fully
390 * restarted to come back on.
391 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700392static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -0700393{
Tomas Winkler857485c2008-03-21 13:53:44 -0700394 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700395 .id = REPLY_CARD_STATE_CMD,
396 .len = sizeof(u32),
397 .data = &flags,
398 .meta.flags = meta_flag,
399 };
400
Tomas Winkler857485c2008-03-21 13:53:44 -0700401 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700402}
403
Tomas Winklerfcab4232008-05-15 13:54:01 +0800404static void iwl_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700405{
406 struct list_head *element;
407
408 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
409 priv->frames_count);
410
411 while (!list_empty(&priv->free_frames)) {
412 element = priv->free_frames.next;
413 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800414 kfree(list_entry(element, struct iwl_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700415 priv->frames_count--;
416 }
417
418 if (priv->frames_count) {
419 IWL_WARNING("%d frames still in use. Did we lose one?\n",
420 priv->frames_count);
421 priv->frames_count = 0;
422 }
423}
424
Tomas Winklerfcab4232008-05-15 13:54:01 +0800425static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700426{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800427 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700428 struct list_head *element;
429 if (list_empty(&priv->free_frames)) {
430 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
431 if (!frame) {
432 IWL_ERROR("Could not allocate frame!\n");
433 return NULL;
434 }
435
436 priv->frames_count++;
437 return frame;
438 }
439
440 element = priv->free_frames.next;
441 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800442 return list_entry(element, struct iwl_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700443}
444
Tomas Winklerfcab4232008-05-15 13:54:01 +0800445static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700446{
447 memset(frame, 0, sizeof(*frame));
448 list_add(&frame->list, &priv->free_frames);
449}
450
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700451unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700452 struct ieee80211_hdr *hdr,
453 const u8 *dest, int left)
454{
455
Tomas Winkler3109ece2008-03-28 16:33:35 -0700456 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -0700457 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
458 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
459 return 0;
460
461 if (priv->ibss_beacon->len > left)
462 return 0;
463
464 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
465
466 return priv->ibss_beacon->len;
467}
468
Guy Cohen39e88502008-04-23 17:14:57 -0700469static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700470{
Guy Cohen39e88502008-04-23 17:14:57 -0700471 int i;
472 int rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -0700473
Guy Cohen39e88502008-04-23 17:14:57 -0700474 /* Set rate mask*/
475 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
476 rate_mask = priv->active_rate_basic & 0xF;
477 else
478 rate_mask = priv->active_rate_basic & 0xFF0;
479
480 /* Find lowest valid rate */
Zhu Yib481de92007-09-25 17:54:57 -0700481 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800482 i = iwl_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -0700483 if (rate_mask & (1 << i))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800484 return iwl_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700485 }
486
Guy Cohen39e88502008-04-23 17:14:57 -0700487 /* No valid rate was found. Assign the lowest one */
488 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
489 return IWL_RATE_1M_PLCP;
490 else
491 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -0700492}
493
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700494static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700495{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800496 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700497 unsigned int frame_size;
498 int rc;
499 u8 rate;
500
Tomas Winklerfcab4232008-05-15 13:54:01 +0800501 frame = iwl_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700502
503 if (!frame) {
504 IWL_ERROR("Could not obtain free frame buffer for beacon "
505 "command.\n");
506 return -ENOMEM;
507 }
508
Guy Cohen39e88502008-04-23 17:14:57 -0700509 rate = iwl4965_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700510
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800511 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700512
Tomas Winkler857485c2008-03-21 13:53:44 -0700513 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700514 &frame->u.cmd[0]);
515
Tomas Winklerfcab4232008-05-15 13:54:01 +0800516 iwl_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700517
518 return rc;
519}
520
521/******************************************************************************
522 *
Zhu Yib481de92007-09-25 17:54:57 -0700523 * Misc. internal state and helper functions
524 *
525 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -0700526
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700527static void iwl4965_ht_conf(struct iwl_priv *priv,
528 struct ieee80211_bss_conf *bss_conf)
529{
530 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
531 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
532 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
533
534 IWL_DEBUG_MAC80211("enter: \n");
535
536 iwl_conf->is_ht = bss_conf->assoc_ht;
537
538 if (!iwl_conf->is_ht)
539 return;
540
541 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
542
543 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
Emmanuel Grumbacha9841012008-05-15 13:54:08 +0800544 iwl_conf->sgf |= HT_SHORT_GI_20MHZ;
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700545 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
Emmanuel Grumbacha9841012008-05-15 13:54:08 +0800546 iwl_conf->sgf |= HT_SHORT_GI_40MHZ;
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700547
548 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
549 iwl_conf->max_amsdu_size =
550 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
551
552 iwl_conf->supported_chan_width =
553 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
554 iwl_conf->extension_chan_offset =
555 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
556 /* If no above or below channel supplied disable FAT channel */
Emmanuel Grumbach963f5512008-06-12 09:47:00 +0800557 if (iwl_conf->extension_chan_offset != IEEE80211_HT_IE_CHA_SEC_ABOVE &&
558 iwl_conf->extension_chan_offset != IEEE80211_HT_IE_CHA_SEC_BELOW) {
559 iwl_conf->extension_chan_offset = IEEE80211_HT_IE_CHA_SEC_NONE;
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700560 iwl_conf->supported_chan_width = 0;
Emmanuel Grumbach963f5512008-06-12 09:47:00 +0800561 }
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700562
563 iwl_conf->tx_mimo_ps_mode =
564 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
565 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
566
567 iwl_conf->control_channel = ht_bss_conf->primary_channel;
568 iwl_conf->tx_chan_width =
569 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
570 iwl_conf->ht_protection =
571 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
572 iwl_conf->non_GF_STA_present =
573 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
574
575 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
576 IWL_DEBUG_MAC80211("leave\n");
577}
578
Zhu Yib481de92007-09-25 17:54:57 -0700579/*
580 * QoS support
581*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700582static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800583 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -0700584{
585
Tomas Winkler857485c2008-03-21 13:53:44 -0700586 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800587 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -0700588}
589
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700590static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -0700591{
592 unsigned long flags;
593
Zhu Yib481de92007-09-25 17:54:57 -0700594 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
595 return;
596
597 if (!priv->qos_data.qos_enable)
598 return;
599
600 spin_lock_irqsave(&priv->lock, flags);
601 priv->qos_data.def_qos_parm.qos_flags = 0;
602
603 if (priv->qos_data.qos_cap.q_AP.queue_request &&
604 !priv->qos_data.qos_cap.q_AP.txop_request)
605 priv->qos_data.def_qos_parm.qos_flags |=
606 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700607 if (priv->qos_data.qos_active)
608 priv->qos_data.def_qos_parm.qos_flags |=
609 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
610
Ron Rindjunskyfd105e72007-11-26 16:14:39 +0200611 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800612 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800613
Zhu Yib481de92007-09-25 17:54:57 -0700614 spin_unlock_irqrestore(&priv->lock, flags);
615
Tomas Winkler3109ece2008-03-28 16:33:35 -0700616 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800617 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
618 priv->qos_data.qos_active,
619 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700620
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800621 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -0700622 &(priv->qos_data.def_qos_parm));
623 }
624}
625
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700626int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -0700627{
628 /* Filter incoming packets to determine if they are targeted toward
629 * this network, discarding packets coming from ourselves */
630 switch (priv->iw_mode) {
631 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
632 /* packets from our adapter are dropped (echo) */
633 if (!compare_ether_addr(header->addr2, priv->mac_addr))
634 return 0;
635 /* {broad,multi}cast packets to our IBSS go through */
636 if (is_multicast_ether_addr(header->addr1))
637 return !compare_ether_addr(header->addr3, priv->bssid);
638 /* packets to our adapter go through */
639 return !compare_ether_addr(header->addr1, priv->mac_addr);
640 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
641 /* packets from our adapter are dropped (echo) */
642 if (!compare_ether_addr(header->addr3, priv->mac_addr))
643 return 0;
644 /* {broad,multi}cast packets to our BSS go through */
645 if (is_multicast_ether_addr(header->addr1))
646 return !compare_ether_addr(header->addr2, priv->bssid);
647 /* packets to our adapter go through */
648 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -0700649 default:
650 break;
Zhu Yib481de92007-09-25 17:54:57 -0700651 }
652
653 return 1;
654}
655
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700656static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700657{
658 /* Reset ieee stats */
659
660 /* We don't reset the net_device_stats (ieee->stats) on
661 * re-association */
662
663 priv->last_seq_num = -1;
664 priv->last_frag_num = -1;
665 priv->last_packet_time = 0;
666
Tomas Winkler2a421b92008-06-12 09:47:10 +0800667 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700668}
669
670#define MAX_UCODE_BEACON_INTERVAL 4096
671#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
672
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800673static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -0700674{
675 u16 new_val = 0;
676 u16 beacon_factor = 0;
677
678 beacon_factor =
679 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
680 / MAX_UCODE_BEACON_INTERVAL;
681 new_val = beacon_val / beacon_factor;
682
683 return cpu_to_le16(new_val);
684}
685
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700686static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700687{
688 u64 interval_tm_unit;
689 u64 tsf, result;
690 unsigned long flags;
691 struct ieee80211_conf *conf = NULL;
692 u16 beacon_int = 0;
693
694 conf = ieee80211_get_hw_conf(priv->hw);
695
696 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -0700697 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
698 priv->rxon_timing.timestamp.dw[0] =
699 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -0700700
701 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
702
Tomas Winkler3109ece2008-03-28 16:33:35 -0700703 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -0700704
705 beacon_int = priv->beacon_int;
706 spin_unlock_irqrestore(&priv->lock, flags);
707
708 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
709 if (beacon_int == 0) {
710 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
711 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
712 } else {
713 priv->rxon_timing.beacon_interval =
714 cpu_to_le16(beacon_int);
715 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800716 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -0700717 le16_to_cpu(priv->rxon_timing.beacon_interval));
718 }
719
720 priv->rxon_timing.atim_window = 0;
721 } else {
722 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800723 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -0700724 /* TODO: we need to get atim_window from upper stack
725 * for now we set to 0 */
726 priv->rxon_timing.atim_window = 0;
727 }
728
729 interval_tm_unit =
730 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
731 result = do_div(tsf, interval_tm_unit);
732 priv->rxon_timing.beacon_init_val =
733 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
734
735 IWL_DEBUG_ASSOC
736 ("beacon interval %d beacon timer %d beacon tim %d\n",
737 le16_to_cpu(priv->rxon_timing.beacon_interval),
738 le32_to_cpu(priv->rxon_timing.beacon_init_val),
739 le16_to_cpu(priv->rxon_timing.atim_window));
740}
741
Tomas Winkler82a66bb2008-05-29 16:35:28 +0800742static void iwl_set_flags_for_band(struct iwl_priv *priv,
743 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700744{
Johannes Berg8318d782008-01-24 19:38:38 +0100745 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -0700746 priv->staging_rxon.flags &=
747 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
748 | RXON_FLG_CCK_MSK);
749 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
750 } else {
Reinette Chatre508e32e2008-04-14 21:16:13 -0700751 /* Copied from iwl4965_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -0700752 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
753 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
754 else
755 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
756
757 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
758 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
759
760 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
761 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
762 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
763 }
764}
765
766/*
Ian Schram01ebd062007-10-25 17:15:22 +0800767 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -0700768 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700769static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700770{
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700771 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -0700772
773 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
774
775 switch (priv->iw_mode) {
776 case IEEE80211_IF_TYPE_AP:
777 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
778 break;
779
780 case IEEE80211_IF_TYPE_STA:
781 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
782 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
783 break;
784
785 case IEEE80211_IF_TYPE_IBSS:
786 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
787 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
788 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
789 RXON_FILTER_ACCEPT_GRP_MSK;
790 break;
791
792 case IEEE80211_IF_TYPE_MNTR:
793 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
794 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
795 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
796 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -0700797 default:
798 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
799 break;
Zhu Yib481de92007-09-25 17:54:57 -0700800 }
801
802#if 0
803 /* TODO: Figure out when short_preamble would be set and cache from
804 * that */
805 if (!hw_to_local(priv->hw)->short_preamble)
806 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
807 else
808 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
809#endif
810
Assaf Krauss8622e702008-03-21 13:53:43 -0700811 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700812 le16_to_cpu(priv->staging_rxon.channel));
813
814 if (!ch_info)
815 ch_info = &priv->channel_info[0];
816
817 /*
818 * in some case A channels are all non IBSS
819 * in this case force B/G channel
820 */
821 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
822 !(is_channel_ibss(ch_info)))
823 ch_info = &priv->channel_info[0];
824
825 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100826 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -0700827
Tomas Winkler82a66bb2008-05-29 16:35:28 +0800828 iwl_set_flags_for_band(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -0700829
830 priv->staging_rxon.ofdm_basic_rates =
831 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
832 priv->staging_rxon.cck_basic_rates =
833 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
834
835 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
836 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
837 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
838 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
839 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
840 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -0700841 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700842}
843
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700844static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -0700845{
Zhu Yib481de92007-09-25 17:54:57 -0700846 priv->iw_mode = mode;
847
Assaf Krauss398f9e72008-06-12 09:47:06 +0800848 /* init channel/phymode to values given at driver init */
849 iwl_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
850
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800851 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700852 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
853
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700854 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700855
Mohamed Abbasfde35712007-11-29 11:10:15 +0800856 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -0700857 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +0800858 return -EAGAIN;
859
860 cancel_delayed_work(&priv->scan_check);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800861 if (iwl_scan_cancel_timeout(priv, 100)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +0800862 IWL_WARNING("Aborted scan still in progress after 100ms\n");
863 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
864 return -EAGAIN;
865 }
866
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800867 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700868
869 return 0;
870}
871
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700872static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700873{
Johannes Berg8318d782008-01-24 19:38:38 +0100874 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700875 struct ieee80211_rate *rate;
876 int i;
877
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700878 hw = iwl_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -0800879 if (!hw) {
880 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
881 return;
882 }
Zhu Yib481de92007-09-25 17:54:57 -0700883
884 priv->active_rate = 0;
885 priv->active_rate_basic = 0;
886
Johannes Berg8318d782008-01-24 19:38:38 +0100887 for (i = 0; i < hw->n_bitrates; i++) {
888 rate = &(hw->bitrates[i]);
889 if (rate->hw_value < IWL_RATE_COUNT)
890 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -0700891 }
892
893 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
894 priv->active_rate, priv->active_rate_basic);
895
896 /*
897 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
898 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
899 * OFDM
900 */
901 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
902 priv->staging_rxon.cck_basic_rates =
903 ((priv->active_rate_basic &
904 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
905 else
906 priv->staging_rxon.cck_basic_rates =
907 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
908
909 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
910 priv->staging_rxon.ofdm_basic_rates =
911 ((priv->active_rate_basic &
912 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
913 IWL_FIRST_OFDM_RATE) & 0xFF;
914 else
915 priv->staging_rxon.ofdm_basic_rates =
916 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
917}
918
Mohamed Abbas64e72c3e2008-06-12 09:47:03 +0800919int iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -0700920{
921 unsigned long flags;
922
923 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
Mohamed Abbas64e72c3e2008-06-12 09:47:03 +0800924 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700925
926 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
927 disable_radio ? "OFF" : "ON");
928
929 if (disable_radio) {
Tomas Winkler2a421b92008-06-12 09:47:10 +0800930 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700931 /* FIXME: This is a workaround for AP */
932 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
933 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700934 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -0700935 CSR_UCODE_SW_BIT_RFKILL);
936 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd2008-03-28 16:21:06 -0700937 /* call the host command only if no hw rf-kill set */
Mohamed Abbas59003832008-04-15 16:01:46 -0700938 if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
939 iwl_is_ready(priv))
Mohamed Abbasad97edd2008-03-28 16:21:06 -0700940 iwl4965_send_card_state(priv,
941 CARD_STATE_CMD_DISABLE,
942 0);
Zhu Yib481de92007-09-25 17:54:57 -0700943 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd2008-03-28 16:21:06 -0700944
945 /* make sure mac80211 stop sending Tx frame */
946 if (priv->mac80211_registered)
947 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -0700948 }
Mohamed Abbas64e72c3e2008-06-12 09:47:03 +0800949 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700950 }
951
952 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700953 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -0700954
955 clear_bit(STATUS_RF_KILL_SW, &priv->status);
956 spin_unlock_irqrestore(&priv->lock, flags);
957
958 /* wake up ucode */
959 msleep(10);
960
961 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700962 iwl_read32(priv, CSR_UCODE_DRV_GP1);
963 if (!iwl_grab_nic_access(priv))
964 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700965 spin_unlock_irqrestore(&priv->lock, flags);
966
967 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
968 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
969 "disabled by HW switch\n");
Mohamed Abbas64e72c3e2008-06-12 09:47:03 +0800970 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700971 }
972
Zhu Yi808e72a2008-06-12 09:47:17 +0800973 if (priv->is_open)
974 queue_work(priv->workqueue, &priv->restart);
Mohamed Abbas64e72c3e2008-06-12 09:47:03 +0800975 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700976}
977
Zhu Yib481de92007-09-25 17:54:57 -0700978#define IWL_PACKET_RETRY_TIME HZ
979
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700980int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -0700981{
982 u16 sc = le16_to_cpu(header->seq_ctrl);
983 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
984 u16 frag = sc & IEEE80211_SCTL_FRAG;
985 u16 *last_seq, *last_frag;
986 unsigned long *last_time;
987
988 switch (priv->iw_mode) {
989 case IEEE80211_IF_TYPE_IBSS:{
990 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800991 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700992 u8 *mac = header->addr2;
993 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
994
995 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800996 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -0700997 if (!compare_ether_addr(entry->mac, mac))
998 break;
999 }
1000 if (p == &priv->ibss_mac_hash[index]) {
1001 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1002 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08001003 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07001004 return 0;
1005 }
1006 memcpy(entry->mac, mac, ETH_ALEN);
1007 entry->seq_num = seq;
1008 entry->frag_num = frag;
1009 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08001010 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07001011 return 0;
1012 }
1013 last_seq = &entry->seq_num;
1014 last_frag = &entry->frag_num;
1015 last_time = &entry->packet_time;
1016 break;
1017 }
1018 case IEEE80211_IF_TYPE_STA:
1019 last_seq = &priv->last_seq_num;
1020 last_frag = &priv->last_frag_num;
1021 last_time = &priv->last_packet_time;
1022 break;
1023 default:
1024 return 0;
1025 }
1026 if ((*last_seq == seq) &&
1027 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
1028 if (*last_frag == frag)
1029 goto drop;
1030 if (*last_frag + 1 != frag)
1031 /* out-of-order fragment */
1032 goto drop;
1033 } else
1034 *last_seq = seq;
1035
1036 *last_frag = frag;
1037 *last_time = jiffies;
1038 return 0;
1039
1040 drop:
1041 return 1;
1042}
1043
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001044#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07001045
1046#include "iwl-spectrum.h"
1047
1048#define BEACON_TIME_MASK_LOW 0x00FFFFFF
1049#define BEACON_TIME_MASK_HIGH 0xFF000000
1050#define TIME_UNIT 1024
1051
1052/*
1053 * extended beacon time format
1054 * time in usec will be changed into a 32-bit value in 8:24 format
1055 * the high 1 byte is the beacon counts
1056 * the lower 3 bytes is the time in usec within one beacon interval
1057 */
1058
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001059static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07001060{
1061 u32 quot;
1062 u32 rem;
1063 u32 interval = beacon_interval * 1024;
1064
1065 if (!interval || !usec)
1066 return 0;
1067
1068 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
1069 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
1070
1071 return (quot << 24) + rem;
1072}
1073
1074/* base is usually what we get from ucode with each received frame,
1075 * the same as HW timer counter counting down
1076 */
1077
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001078static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07001079{
1080 u32 base_low = base & BEACON_TIME_MASK_LOW;
1081 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
1082 u32 interval = beacon_interval * TIME_UNIT;
1083 u32 res = (base & BEACON_TIME_MASK_HIGH) +
1084 (addon & BEACON_TIME_MASK_HIGH);
1085
1086 if (base_low > addon_low)
1087 res += base_low - addon_low;
1088 else if (base_low < addon_low) {
1089 res += interval + base_low - addon_low;
1090 res += (1 << 24);
1091 } else
1092 res += (1 << 24);
1093
1094 return cpu_to_le32(res);
1095}
1096
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001097static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001098 struct ieee80211_measurement_params *params,
1099 u8 type)
1100{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001101 struct iwl4965_spectrum_cmd spectrum;
Tomas Winklerdb11d632008-05-05 10:22:33 +08001102 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07001103 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001104 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
1105 .data = (void *)&spectrum,
1106 .meta.flags = CMD_WANT_SKB,
1107 };
1108 u32 add_time = le64_to_cpu(params->start_time);
1109 int rc;
1110 int spectrum_resp_status;
1111 int duration = le16_to_cpu(params->duration);
1112
Tomas Winkler3109ece2008-03-28 16:33:35 -07001113 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001114 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001115 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07001116 le64_to_cpu(params->start_time) - priv->last_tsf,
1117 le16_to_cpu(priv->rxon_timing.beacon_interval));
1118
1119 memset(&spectrum, 0, sizeof(spectrum));
1120
1121 spectrum.channel_count = cpu_to_le16(1);
1122 spectrum.flags =
1123 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
1124 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
1125 cmd.len = sizeof(spectrum);
1126 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
1127
Tomas Winkler3109ece2008-03-28 16:33:35 -07001128 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001129 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001130 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07001131 add_time,
1132 le16_to_cpu(priv->rxon_timing.beacon_interval));
1133 else
1134 spectrum.start_time = 0;
1135
1136 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
1137 spectrum.channels[0].channel = params->channel;
1138 spectrum.channels[0].type = type;
1139 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
1140 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
1141 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
1142
Tomas Winkler857485c2008-03-21 13:53:44 -07001143 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001144 if (rc)
1145 return rc;
1146
Tomas Winklerdb11d632008-05-05 10:22:33 +08001147 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001148 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1149 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
1150 rc = -EIO;
1151 }
1152
1153 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
1154 switch (spectrum_resp_status) {
1155 case 0: /* Command will be handled */
1156 if (res->u.spectrum.id != 0xff) {
1157 IWL_DEBUG_INFO
1158 ("Replaced existing measurement: %d\n",
1159 res->u.spectrum.id);
1160 priv->measurement_status &= ~MEASUREMENT_READY;
1161 }
1162 priv->measurement_status |= MEASUREMENT_ACTIVE;
1163 rc = 0;
1164 break;
1165
1166 case 1: /* Command will not be handled */
1167 rc = -EAGAIN;
1168 break;
1169 }
1170
1171 dev_kfree_skb_any(cmd.meta.u.skb);
1172
1173 return rc;
1174}
1175#endif
1176
Zhu Yib481de92007-09-25 17:54:57 -07001177/******************************************************************************
1178 *
1179 * Generic RX handler implementations
1180 *
1181 ******************************************************************************/
Tomas Winkler885ba202008-05-29 16:34:55 +08001182static void iwl_rx_reply_alive(struct iwl_priv *priv,
1183 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001184{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001185 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Tomas Winkler885ba202008-05-29 16:34:55 +08001186 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07001187 struct delayed_work *pwork;
1188
1189 palive = &pkt->u.alive_frame;
1190
1191 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
1192 "0x%01X 0x%01X\n",
1193 palive->is_valid, palive->ver_type,
1194 palive->ver_subtype);
1195
1196 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
1197 IWL_DEBUG_INFO("Initialization Alive received.\n");
1198 memcpy(&priv->card_alive_init,
1199 &pkt->u.alive_frame,
Tomas Winkler885ba202008-05-29 16:34:55 +08001200 sizeof(struct iwl_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001201 pwork = &priv->init_alive_start;
1202 } else {
1203 IWL_DEBUG_INFO("Runtime Alive received.\n");
1204 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler885ba202008-05-29 16:34:55 +08001205 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001206 pwork = &priv->alive_start;
1207 }
1208
1209 /* We delay the ALIVE response by 5ms to
1210 * give the HW RF Kill time to activate... */
1211 if (palive->is_valid == UCODE_VALID_OK)
1212 queue_delayed_work(priv->workqueue, pwork,
1213 msecs_to_jiffies(5));
1214 else
1215 IWL_WARNING("uCode did not respond OK.\n");
1216}
1217
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001218static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001219 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001220{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001221 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001222
1223 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
1224 "seq 0x%04X ser 0x%08X\n",
1225 le32_to_cpu(pkt->u.err_resp.error_type),
1226 get_cmd_string(pkt->u.err_resp.cmd_id),
1227 pkt->u.err_resp.cmd_id,
1228 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1229 le32_to_cpu(pkt->u.err_resp.error_info));
1230}
1231
1232#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1233
Tomas Winklera55360e2008-05-05 10:22:28 +08001234static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001235{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001236 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08001237 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001238 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07001239 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
1240 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
1241 rxon->channel = csa->channel;
1242 priv->staging_rxon.channel = csa->channel;
1243}
1244
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001245static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001246 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001247{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001248#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Tomas Winklerdb11d632008-05-05 10:22:33 +08001249 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001250 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07001251
1252 if (!report->state) {
Ester Kummerf3d67992008-05-06 11:05:12 +08001253 IWL_DEBUG(IWL_DL_11H,
1254 "Spectrum Measure Notification: Start\n");
Zhu Yib481de92007-09-25 17:54:57 -07001255 return;
1256 }
1257
1258 memcpy(&priv->measure_report, report, sizeof(*report));
1259 priv->measurement_status |= MEASUREMENT_READY;
1260#endif
1261}
1262
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001263static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001264 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001265{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001266#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08001267 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001268 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07001269 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
1270 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1271#endif
1272}
1273
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001274static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001275 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001276{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001277 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001278 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
1279 "notification for %s:\n",
1280 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Ester Kummerbf403db2008-05-05 10:22:40 +08001281 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07001282}
1283
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001284static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07001285{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001286 struct iwl_priv *priv =
1287 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07001288 struct sk_buff *beacon;
1289
1290 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02001291 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07001292
1293 if (!beacon) {
1294 IWL_ERROR("update beacon failed\n");
1295 return;
1296 }
1297
1298 mutex_lock(&priv->mutex);
1299 /* new beacon skb is allocated every time; dispose previous.*/
1300 if (priv->ibss_beacon)
1301 dev_kfree_skb(priv->ibss_beacon);
1302
1303 priv->ibss_beacon = beacon;
1304 mutex_unlock(&priv->mutex);
1305
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001306 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001307}
1308
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08001309/**
1310 * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
1311 *
1312 * This callback is provided in order to send a statistics request.
1313 *
1314 * This timer function is continually reset to execute within
1315 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
1316 * was received. We need to ensure we receive the statistics in order
1317 * to update the temperature used for calibrating the TXPOWER.
1318 */
1319static void iwl4965_bg_statistics_periodic(unsigned long data)
1320{
1321 struct iwl_priv *priv = (struct iwl_priv *)data;
1322
1323 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1324 return;
1325
1326 iwl_send_statistics_request(priv, CMD_ASYNC);
1327}
1328
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001329static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001330 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001331{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001332#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08001333 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001334 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
Tomas Winklere7d326a2008-06-12 09:47:11 +08001335 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001336
1337 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
1338 "tsf %d %d rate %d\n",
Tomas Winkler25a65722008-06-12 09:47:07 +08001339 le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
Zhu Yib481de92007-09-25 17:54:57 -07001340 beacon->beacon_notify_hdr.failure_frame,
1341 le32_to_cpu(beacon->ibss_mgr_status),
1342 le32_to_cpu(beacon->high_tsf),
1343 le32_to_cpu(beacon->low_tsf), rate);
1344#endif
1345
1346 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
1347 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
1348 queue_work(priv->workqueue, &priv->beacon_update);
1349}
1350
Zhu Yib481de92007-09-25 17:54:57 -07001351/* Handle notification from uCode that card's power state is changing
1352 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001353static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001354 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001355{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001356 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001357 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
1358 unsigned long status = priv->status;
1359
1360 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
1361 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1362 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
1363
1364 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
1365 RF_CARD_DISABLED)) {
1366
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001367 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001368 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1369
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001370 if (!iwl_grab_nic_access(priv)) {
1371 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07001372 priv, HBUS_TARG_MBX_C,
1373 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1374
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001375 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001376 }
1377
1378 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001379 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07001380 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001381 if (!iwl_grab_nic_access(priv)) {
1382 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07001383 priv, HBUS_TARG_MBX_C,
1384 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1385
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001386 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001387 }
1388 }
1389
1390 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001391 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001392 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001393 iwl_read32(priv, CSR_UCODE_DRV_GP1);
1394 if (!iwl_grab_nic_access(priv))
1395 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001396 }
1397 }
1398
1399 if (flags & HW_CARD_DISABLED)
1400 set_bit(STATUS_RF_KILL_HW, &priv->status);
1401 else
1402 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1403
1404
1405 if (flags & SW_CARD_DISABLED)
1406 set_bit(STATUS_RF_KILL_SW, &priv->status);
1407 else
1408 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1409
1410 if (!(flags & RXON_CARD_DISABLED))
Tomas Winkler2a421b92008-06-12 09:47:10 +08001411 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001412
1413 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
1414 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
1415 (test_bit(STATUS_RF_KILL_SW, &status) !=
1416 test_bit(STATUS_RF_KILL_SW, &priv->status)))
1417 queue_work(priv->workqueue, &priv->rf_kill);
1418 else
1419 wake_up_interruptible(&priv->wait_command_queue);
1420}
1421
Ron Rindjunsky37a44212008-05-29 16:35:18 +08001422/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
1423 * This will be used later in iwl4965_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
1424static void iwl4965_rx_reply_rx_phy(struct iwl_priv *priv,
1425 struct iwl_rx_mem_buffer *rxb)
1426{
1427 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1428 priv->last_phy_res[0] = 1;
1429 memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
1430 sizeof(struct iwl4965_rx_phy_res));
1431}
1432
Zhu Yib481de92007-09-25 17:54:57 -07001433/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001434 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07001435 *
1436 * Setup the RX handlers for each of the reply types sent from the uCode
1437 * to the host.
1438 *
1439 * This function chains into the hardware specific files for them to setup
1440 * any hardware specific handlers as well.
1441 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001442static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001443{
Tomas Winkler885ba202008-05-29 16:34:55 +08001444 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001445 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
1446 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07001447 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001448 iwl4965_rx_spectrum_measure_notif;
1449 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001450 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001451 iwl4965_rx_pm_debug_statistics_notif;
1452 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001453
Ben Cahill9fbab512007-11-29 11:09:47 +08001454 /*
1455 * The same handler is used for both the REPLY to a discrete
1456 * statistics request from the host as well as for the periodic
1457 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07001458 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001459 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
1460 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Tomas Winkler2a421b92008-06-12 09:47:10 +08001461
1462 iwl_setup_rx_scan_handlers(priv);
1463
Ron Rindjunsky37a44212008-05-29 16:35:18 +08001464 /* status change handler */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001465 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001466
Tomas Winklerc1354752008-05-29 16:35:04 +08001467 priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
1468 iwl_rx_missed_beacon_notif;
Ron Rindjunsky37a44212008-05-29 16:35:18 +08001469 /* Rx handlers */
1470 priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
1471 priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl4965_rx_reply_rx;
Ben Cahill9fbab512007-11-29 11:09:47 +08001472 /* Set up hardware specific Rx handlers */
Emmanuel Grumbachd4789ef2008-04-24 11:55:20 -07001473 priv->cfg->ops->lib->rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001474}
1475
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001476/*
1477 * this should be called while priv->lock is locked
1478*/
Tomas Winklera55360e2008-05-05 10:22:28 +08001479static void __iwl_rx_replenish(struct iwl_priv *priv)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001480{
Tomas Winklera55360e2008-05-05 10:22:28 +08001481 iwl_rx_allocate(priv);
1482 iwl_rx_queue_restock(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001483}
1484
1485
Zhu Yib481de92007-09-25 17:54:57 -07001486/**
Tomas Winklera55360e2008-05-05 10:22:28 +08001487 * iwl_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07001488 *
1489 * Uses the priv->rx_handlers callback function array to invoke
1490 * the appropriate handlers, including command responses,
1491 * frame-received notifications, and other notifications.
1492 */
Tomas Winklera55360e2008-05-05 10:22:28 +08001493void iwl_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001494{
Tomas Winklera55360e2008-05-05 10:22:28 +08001495 struct iwl_rx_mem_buffer *rxb;
Tomas Winklerdb11d632008-05-05 10:22:33 +08001496 struct iwl_rx_packet *pkt;
Tomas Winklera55360e2008-05-05 10:22:28 +08001497 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001498 u32 r, i;
1499 int reclaim;
1500 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001501 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08001502 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07001503
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001504 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1505 * buffer that the driver may process (last buffer filled by ucode). */
Ron Rindjunskyd67f5482008-05-05 10:22:49 +08001506 r = priv->cfg->ops->lib->shared_mem_rx_idx(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001507 i = rxq->read;
1508
1509 /* Rx interrupt, but nothing sent from uCode */
1510 if (i == r)
Ester Kummerf3d67992008-05-06 11:05:12 +08001511 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07001512
Tomas Winklera55360e2008-05-05 10:22:28 +08001513 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001514 fill_rx = 1;
1515
Zhu Yib481de92007-09-25 17:54:57 -07001516 while (i != r) {
1517 rxb = rxq->queue[i];
1518
Ben Cahill9fbab512007-11-29 11:09:47 +08001519 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07001520 * then a bug has been introduced in the queue refilling
1521 * routines -- catch it here */
1522 BUG_ON(rxb == NULL);
1523
1524 rxq->queue[i] = NULL;
1525
1526 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07001527 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07001528 PCI_DMA_FROMDEVICE);
Tomas Winklerdb11d632008-05-05 10:22:33 +08001529 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001530
1531 /* Reclaim a command buffer only if this packet is a response
1532 * to a (driver-originated) command.
1533 * If the packet (e.g. Rx frame) originated from uCode,
1534 * there is no command buffer to reclaim.
1535 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1536 * but apparently a few don't get set; catch them here. */
1537 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1538 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07001539 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08001540 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07001541 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1542 (pkt->hdr.cmd != REPLY_TX);
1543
1544 /* Based on type of command response or notification,
1545 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001546 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07001547 if (priv->rx_handlers[pkt->hdr.cmd]) {
Ester Kummerf3d67992008-05-06 11:05:12 +08001548 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d, %s, 0x%02x\n", r,
1549 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001550 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
1551 } else {
1552 /* No handling needed */
Ester Kummerf3d67992008-05-06 11:05:12 +08001553 IWL_DEBUG(IWL_DL_RX,
Zhu Yib481de92007-09-25 17:54:57 -07001554 "r %d i %d No handler needed for %s, 0x%02x\n",
1555 r, i, get_cmd_string(pkt->hdr.cmd),
1556 pkt->hdr.cmd);
1557 }
1558
1559 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001560 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07001561 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07001562 * as we reclaim the driver command queue */
1563 if (rxb && rxb->skb)
Tomas Winkler17b88922008-05-29 16:35:12 +08001564 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001565 else
1566 IWL_WARNING("Claim null rxb?\n");
1567 }
1568
1569 /* For now we just don't re-use anything. We can tweak this
1570 * later to try and re-use notification packets and SKBs that
1571 * fail to Rx correctly */
1572 if (rxb->skb != NULL) {
1573 priv->alloc_rxb_skb--;
1574 dev_kfree_skb_any(rxb->skb);
1575 rxb->skb = NULL;
1576 }
1577
1578 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07001579 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02001580 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07001581 spin_lock_irqsave(&rxq->lock, flags);
1582 list_add_tail(&rxb->list, &priv->rxq.rx_used);
1583 spin_unlock_irqrestore(&rxq->lock, flags);
1584 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001585 /* If there are a lot of unused frames,
1586 * restock the Rx queue so ucode wont assert. */
1587 if (fill_rx) {
1588 count++;
1589 if (count >= 8) {
1590 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08001591 __iwl_rx_replenish(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001592 count = 0;
1593 }
1594 }
Zhu Yib481de92007-09-25 17:54:57 -07001595 }
1596
1597 /* Backtrack one entry */
1598 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08001599 iwl_rx_queue_restock(priv);
1600}
Tomas Winklera55360e2008-05-05 10:22:28 +08001601
1602#define PERFECT_RSSI (-20) /* dBm */
1603#define WORST_RSSI (-95) /* dBm */
1604#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
1605
1606/* Calculate an indication of rx signal quality (a percentage, not dBm!).
1607 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
1608 * about formulas used below. */
1609int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
1610{
1611 int sig_qual;
1612 int degradation = PERFECT_RSSI - rssi_dbm;
1613
1614 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
1615 * as indicator; formula is (signal dbm - noise dbm).
1616 * SNR at or above 40 is a great signal (100%).
1617 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
1618 * Weakest usable signal is usually 10 - 15 dB SNR. */
1619 if (noise_dbm) {
1620 if (rssi_dbm - noise_dbm >= 40)
1621 return 100;
1622 else if (rssi_dbm < noise_dbm)
1623 return 0;
1624 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
1625
1626 /* Else use just the signal level.
1627 * This formula is a least squares fit of data points collected and
1628 * compared with a reference system that had a percentage (%) display
1629 * for signal quality. */
1630 } else
1631 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
1632 (15 * RSSI_RANGE + 62 * degradation)) /
1633 (RSSI_RANGE * RSSI_RANGE);
1634
1635 if (sig_qual > 100)
1636 sig_qual = 100;
1637 else if (sig_qual < 1)
1638 sig_qual = 0;
1639
1640 return sig_qual;
Zhu Yib481de92007-09-25 17:54:57 -07001641}
1642
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001643#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001644static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001645{
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08001646 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07001647 DECLARE_MAC_BUF(mac);
1648
Zhu Yib481de92007-09-25 17:54:57 -07001649 IWL_DEBUG_RADIO("RX CONFIG:\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08001650 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07001651 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
1652 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
1653 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
1654 le32_to_cpu(rxon->filter_flags));
1655 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
1656 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
1657 rxon->ofdm_basic_rates);
1658 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07001659 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
1660 print_mac(mac, rxon->node_addr));
1661 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
1662 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07001663 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
1664}
1665#endif
1666
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001667static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001668{
1669 IWL_DEBUG_ISR("Enabling interrupts\n");
1670 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001671 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07001672}
1673
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001674/* call this function to flush any scheduled tasklet */
1675static inline void iwl_synchronize_irq(struct iwl_priv *priv)
1676{
1677 /* wait to make sure we flush pedding tasklet*/
1678 synchronize_irq(priv->pci_dev->irq);
1679 tasklet_kill(&priv->irq_tasklet);
1680}
1681
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001682static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001683{
1684 clear_bit(STATUS_INT_ENABLED, &priv->status);
1685
1686 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001687 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07001688
1689 /* acknowledge/clear/reset any interrupts still pending
1690 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001691 iwl_write32(priv, CSR_INT, 0xffffffff);
1692 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07001693 IWL_DEBUG_ISR("Disabled interrupts\n");
1694}
1695
Zhu Yib481de92007-09-25 17:54:57 -07001696
Zhu Yib481de92007-09-25 17:54:57 -07001697/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001698 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07001699 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001700static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001701{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001702 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07001703 set_bit(STATUS_FW_ERROR, &priv->status);
1704
1705 /* Cancel currently queued command. */
1706 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
1707
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001708#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001709 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Ester Kummerede0cba2008-05-29 16:34:46 +08001710 iwl_dump_nic_error_log(priv);
Ester Kummer189a2b52008-05-15 13:54:18 +08001711 iwl_dump_nic_event_log(priv);
Ester Kummerbf403db2008-05-05 10:22:40 +08001712 iwl4965_print_rx_config_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001713 }
1714#endif
1715
1716 wake_up_interruptible(&priv->wait_command_queue);
1717
1718 /* Keep the restart process from trying to send host
1719 * commands by clearing the INIT status bit */
1720 clear_bit(STATUS_READY, &priv->status);
1721
1722 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08001723 IWL_DEBUG(IWL_DL_FW_ERRORS,
Zhu Yib481de92007-09-25 17:54:57 -07001724 "Restarting adapter due to uCode error.\n");
1725
Tomas Winkler3109ece2008-03-28 16:33:35 -07001726 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001727 memcpy(&priv->recovery_rxon, &priv->active_rxon,
1728 sizeof(priv->recovery_rxon));
1729 priv->error_recovering = 1;
1730 }
Ester Kummer3a1081e2008-05-06 11:05:14 +08001731 if (priv->cfg->mod_params->restart_fw)
1732 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07001733 }
1734}
1735
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001736static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001737{
1738 unsigned long flags;
1739
1740 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
1741 sizeof(priv->staging_rxon));
1742 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001743 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001744
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001745 iwl_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07001746
1747 spin_lock_irqsave(&priv->lock, flags);
1748 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
1749 priv->error_recovering = 0;
1750 spin_unlock_irqrestore(&priv->lock, flags);
1751}
1752
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001753static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001754{
1755 u32 inta, handled = 0;
1756 u32 inta_fh;
1757 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001758#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001759 u32 inta_mask;
1760#endif
1761
1762 spin_lock_irqsave(&priv->lock, flags);
1763
1764 /* Ack/clear/reset pending uCode interrupts.
1765 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1766 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001767 inta = iwl_read32(priv, CSR_INT);
1768 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001769
1770 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1771 * Any new interrupts that happen after this, either while we're
1772 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001773 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1774 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001775
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001776#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001777 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001778 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001779 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07001780 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1781 inta, inta_mask, inta_fh);
1782 }
1783#endif
1784
1785 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1786 * atomic, make sure that inta covers all the interrupts that
1787 * we've discovered, even if FH interrupt came in just after
1788 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001789 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001790 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001791 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001792 inta |= CSR_INT_BIT_FH_TX;
1793
1794 /* Now service all interrupt bits discovered above. */
1795 if (inta & CSR_INT_BIT_HW_ERR) {
1796 IWL_ERROR("Microcode HW error detected. Restarting.\n");
1797
1798 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001799 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001800
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001801 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001802
1803 handled |= CSR_INT_BIT_HW_ERR;
1804
1805 spin_unlock_irqrestore(&priv->lock, flags);
1806
1807 return;
1808 }
1809
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001810#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001811 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001812 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001813 if (inta & CSR_INT_BIT_SCD)
1814 IWL_DEBUG_ISR("Scheduler finished to transmit "
1815 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001816
1817 /* Alive notification via Rx interrupt will do the real work */
1818 if (inta & CSR_INT_BIT_ALIVE)
1819 IWL_DEBUG_ISR("Alive interrupt\n");
1820 }
1821#endif
1822 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001823 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001824
Ben Cahill9fbab512007-11-29 11:09:47 +08001825 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07001826 if (inta & CSR_INT_BIT_RF_KILL) {
1827 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001828 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07001829 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
1830 hw_rf_kill = 1;
1831
Ester Kummerf3d67992008-05-06 11:05:12 +08001832 IWL_DEBUG(IWL_DL_RF_KILL, "RF_KILL bit toggled to %s.\n",
Zhu Yib481de92007-09-25 17:54:57 -07001833 hw_rf_kill ? "disable radio":"enable radio");
1834
1835 /* Queue restart only if RF_KILL switch was set to "kill"
1836 * when we loaded driver, and is now set to "enable".
1837 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08001838 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08001839 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
1840 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07001841 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08001842 }
Zhu Yib481de92007-09-25 17:54:57 -07001843
1844 handled |= CSR_INT_BIT_RF_KILL;
1845 }
1846
Ben Cahill9fbab512007-11-29 11:09:47 +08001847 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07001848 if (inta & CSR_INT_BIT_CT_KILL) {
1849 IWL_ERROR("Microcode CT kill error detected.\n");
1850 handled |= CSR_INT_BIT_CT_KILL;
1851 }
1852
1853 /* Error detected by uCode */
1854 if (inta & CSR_INT_BIT_SW_ERR) {
1855 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
1856 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001857 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001858 handled |= CSR_INT_BIT_SW_ERR;
1859 }
1860
1861 /* uCode wakes up after power-down sleep */
1862 if (inta & CSR_INT_BIT_WAKEUP) {
1863 IWL_DEBUG_ISR("Wakeup interrupt\n");
Tomas Winklera55360e2008-05-05 10:22:28 +08001864 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Tomas Winklerbabcebf2008-05-15 13:54:00 +08001865 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1866 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1867 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1868 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1869 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1870 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07001871
1872 handled |= CSR_INT_BIT_WAKEUP;
1873 }
1874
1875 /* All uCode command responses, including Tx command responses,
1876 * Rx "responses" (frame-received notification), and other
1877 * notifications from uCode come through here*/
1878 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Tomas Winklera55360e2008-05-05 10:22:28 +08001879 iwl_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001880 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1881 }
1882
1883 if (inta & CSR_INT_BIT_FH_TX) {
1884 IWL_DEBUG_ISR("Tx interrupt\n");
1885 handled |= CSR_INT_BIT_FH_TX;
Ron Rindjunskydbb983b2008-05-15 13:54:12 +08001886 /* FH finished to write, send event */
1887 priv->ucode_write_complete = 1;
1888 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07001889 }
1890
1891 if (inta & ~handled)
1892 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
1893
1894 if (inta & ~CSR_INI_SET_MASK) {
1895 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
1896 inta & ~CSR_INI_SET_MASK);
1897 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
1898 }
1899
1900 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001901 /* only Re-enable if diabled by irq */
1902 if (test_bit(STATUS_INT_ENABLED, &priv->status))
1903 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001904
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001905#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001906 if (priv->debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001907 inta = iwl_read32(priv, CSR_INT);
1908 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1909 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07001910 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
1911 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1912 }
1913#endif
1914 spin_unlock_irqrestore(&priv->lock, flags);
1915}
1916
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001917static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07001918{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001919 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07001920 u32 inta, inta_mask;
1921 u32 inta_fh;
1922 if (!priv)
1923 return IRQ_NONE;
1924
1925 spin_lock(&priv->lock);
1926
1927 /* Disable (but don't clear!) interrupts here to avoid
1928 * back-to-back ISRs and sporadic interrupts from our NIC.
1929 * If we have something to service, the tasklet will re-enable ints.
1930 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001931 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
1932 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07001933
1934 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001935 inta = iwl_read32(priv, CSR_INT);
1936 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07001937
1938 /* Ignore interrupt if there's nothing in NIC to service.
1939 * This may be due to IRQ shared with another device,
1940 * or due to sporadic interrupts thrown from our NIC. */
1941 if (!inta && !inta_fh) {
1942 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
1943 goto none;
1944 }
1945
1946 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08001947 /* Hardware disappeared. It might have already raised
1948 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07001949 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08001950 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07001951 }
1952
1953 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1954 inta, inta_mask, inta_fh);
1955
Joonwoo Park25c03d82008-01-23 10:15:20 -08001956 inta &= ~CSR_INT_BIT_SCD;
1957
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001958 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001959 if (likely(inta || inta_fh))
1960 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07001961
Oliver Neukum66fbb542007-11-15 09:31:10 +08001962 unplugged:
1963 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07001964 return IRQ_HANDLED;
1965
1966 none:
1967 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001968 /* only Re-enable if diabled by irq */
1969 if (test_bit(STATUS_INT_ENABLED, &priv->status))
1970 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001971 spin_unlock(&priv->lock);
1972 return IRQ_NONE;
1973}
1974
Zhu Yib481de92007-09-25 17:54:57 -07001975/******************************************************************************
1976 *
1977 * uCode download functions
1978 *
1979 ******************************************************************************/
1980
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001981static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001982{
Tomas Winkler98c92212008-01-14 17:46:20 -08001983 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1984 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1985 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1986 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1987 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1988 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001989}
1990
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08001991static void iwl4965_nic_start(struct iwl_priv *priv)
1992{
1993 /* Remove all resets to allow NIC to operate */
1994 iwl_write32(priv, CSR_RESET, 0);
1995}
1996
1997
Zhu Yib481de92007-09-25 17:54:57 -07001998/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001999 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07002000 *
2001 * Copy into buffers for card to fetch via bus-mastering
2002 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002003static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002004{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002005 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002006 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002007 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08002008 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07002009 u8 *src;
2010 size_t len;
2011 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
2012
2013 /* Ask kernel firmware_class module to get the boot firmware off disk.
2014 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08002015 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
2016 if (ret < 0) {
2017 IWL_ERROR("%s firmware file req failed: Reason %d\n",
2018 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07002019 goto error;
2020 }
2021
2022 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
2023 name, ucode_raw->size);
2024
2025 /* Make sure that we got at least our header! */
2026 if (ucode_raw->size < sizeof(*ucode)) {
2027 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002028 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002029 goto err_release;
2030 }
2031
2032 /* Data from ucode file: header followed by uCode images */
2033 ucode = (void *)ucode_raw->data;
2034
2035 ver = le32_to_cpu(ucode->ver);
2036 inst_size = le32_to_cpu(ucode->inst_size);
2037 data_size = le32_to_cpu(ucode->data_size);
2038 init_size = le32_to_cpu(ucode->init_size);
2039 init_data_size = le32_to_cpu(ucode->init_data_size);
2040 boot_size = le32_to_cpu(ucode->boot_size);
2041
2042 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
2043 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
2044 inst_size);
2045 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
2046 data_size);
2047 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
2048 init_size);
2049 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
2050 init_data_size);
2051 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
2052 boot_size);
2053
2054 /* Verify size of file vs. image size info in file's header */
2055 if (ucode_raw->size < sizeof(*ucode) +
2056 inst_size + data_size + init_size +
2057 init_data_size + boot_size) {
2058
2059 IWL_DEBUG_INFO("uCode file size %d too small\n",
2060 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002061 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002062 goto err_release;
2063 }
2064
2065 /* Verify that uCode images will fit in card's SRAM */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002066 if (inst_size > priv->hw_params.max_inst_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08002067 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
2068 inst_size);
2069 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002070 goto err_release;
2071 }
2072
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002073 if (data_size > priv->hw_params.max_data_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08002074 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
2075 data_size);
2076 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002077 goto err_release;
2078 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002079 if (init_size > priv->hw_params.max_inst_size) {
Zhu Yib481de92007-09-25 17:54:57 -07002080 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08002081 ("uCode init instr len %d too large to fit in\n",
2082 init_size);
2083 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002084 goto err_release;
2085 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002086 if (init_data_size > priv->hw_params.max_data_size) {
Zhu Yib481de92007-09-25 17:54:57 -07002087 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08002088 ("uCode init data len %d too large to fit in\n",
2089 init_data_size);
2090 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002091 goto err_release;
2092 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002093 if (boot_size > priv->hw_params.max_bsm_size) {
Zhu Yib481de92007-09-25 17:54:57 -07002094 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08002095 ("uCode boot instr len %d too large to fit in\n",
2096 boot_size);
2097 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002098 goto err_release;
2099 }
2100
2101 /* Allocate ucode buffers for card's bus-master loading ... */
2102
2103 /* Runtime instructions and 2 copies of data:
2104 * 1) unmodified from disk
2105 * 2) backup cache for save/restore during power-downs */
2106 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002107 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07002108
2109 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002110 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07002111
2112 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002113 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07002114
2115 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08002116 if (init_size && init_data_size) {
2117 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002118 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07002119
Tomas Winkler90e759d2007-11-29 11:09:41 +08002120 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002121 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002122
2123 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2124 goto err_pci_alloc;
2125 }
Zhu Yib481de92007-09-25 17:54:57 -07002126
2127 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08002128 if (boot_size) {
2129 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002130 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07002131
Tomas Winkler90e759d2007-11-29 11:09:41 +08002132 if (!priv->ucode_boot.v_addr)
2133 goto err_pci_alloc;
2134 }
Zhu Yib481de92007-09-25 17:54:57 -07002135
2136 /* Copy images into buffers for card's bus-master reads ... */
2137
2138 /* Runtime instructions (first block of data in file) */
2139 src = &ucode->data[0];
2140 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002141 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002142 memcpy(priv->ucode_code.v_addr, src, len);
2143 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
2144 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2145
2146 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002147 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07002148 src = &ucode->data[inst_size];
2149 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002150 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002151 memcpy(priv->ucode_data.v_addr, src, len);
2152 memcpy(priv->ucode_data_backup.v_addr, src, len);
2153
2154 /* Initialization instructions (3rd block) */
2155 if (init_size) {
2156 src = &ucode->data[inst_size + data_size];
2157 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002158 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
2159 len);
Zhu Yib481de92007-09-25 17:54:57 -07002160 memcpy(priv->ucode_init.v_addr, src, len);
2161 }
2162
2163 /* Initialization data (4th block) */
2164 if (init_data_size) {
2165 src = &ucode->data[inst_size + data_size + init_size];
2166 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002167 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
2168 len);
Zhu Yib481de92007-09-25 17:54:57 -07002169 memcpy(priv->ucode_init_data.v_addr, src, len);
2170 }
2171
2172 /* Bootstrap instructions (5th block) */
2173 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
2174 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002175 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002176 memcpy(priv->ucode_boot.v_addr, src, len);
2177
2178 /* We have our copies now, allow OS release its copies */
2179 release_firmware(ucode_raw);
2180 return 0;
2181
2182 err_pci_alloc:
2183 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002184 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002185 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002186
2187 err_release:
2188 release_firmware(ucode_raw);
2189
2190 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08002191 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002192}
2193
Zhu Yib481de92007-09-25 17:54:57 -07002194/**
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002195 * iwl_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002196 * from protocol/runtime uCode (initialization uCode's
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002197 * Alive gets handled by iwl_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002198 */
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002199static void iwl_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002200{
Tomas Winkler57aab752008-04-14 21:16:03 -07002201 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002202
2203 IWL_DEBUG_INFO("Runtime Alive received.\n");
2204
2205 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2206 /* We had an error bringing up the hardware, so take it
2207 * all the way back down so we can try again */
2208 IWL_DEBUG_INFO("Alive failed.\n");
2209 goto restart;
2210 }
2211
2212 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2213 * This is a paranoid check, because we would not have gotten the
2214 * "runtime" alive if code weren't properly loaded. */
Emmanuel Grumbachb0692f22008-04-24 11:55:18 -07002215 if (iwl_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002216 /* Runtime instruction load was bad;
2217 * take it all the way back down so we can try again */
2218 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
2219 goto restart;
2220 }
2221
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002222 iwlcore_clear_stations_table(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07002223 ret = priv->cfg->ops->lib->alive_notify(priv);
2224 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07002225 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07002226 ret);
Zhu Yib481de92007-09-25 17:54:57 -07002227 goto restart;
2228 }
2229
Ben Cahill9fbab512007-11-29 11:09:47 +08002230 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002231 set_bit(STATUS_ALIVE, &priv->status);
2232
Tomas Winklerfee12472008-04-03 16:05:21 -07002233 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002234 return;
2235
Johannes Berg36d68252008-05-15 12:55:26 +02002236 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002237
2238 priv->active_rate = priv->rates_mask;
2239 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2240
Tomas Winkler3109ece2008-03-28 16:33:35 -07002241 if (iwl_is_associated(priv)) {
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08002242 struct iwl_rxon_cmd *active_rxon =
2243 (struct iwl_rxon_cmd *)&priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002244
2245 memcpy(&priv->staging_rxon, &priv->active_rxon,
2246 sizeof(priv->staging_rxon));
2247 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2248 } else {
2249 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002250 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002251 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2252 }
2253
Ben Cahill9fbab512007-11-29 11:09:47 +08002254 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002255 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002256
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002257 iwl_reset_run_time_calib(priv);
2258
Zhu Yib481de92007-09-25 17:54:57 -07002259 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002260 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002261
2262 /* At this point, the NIC is initialized and operational */
Emmanuel Grumbach47f4a582008-06-12 09:47:13 +08002263 iwl_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002264
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002265 iwl_leds_register(priv);
2266
Zhu Yib481de92007-09-25 17:54:57 -07002267 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002268 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002269 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002270
2271 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002272 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002273
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07002274 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Mohamed Abbas84363e62008-04-04 16:59:58 -07002275 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002276
2277 if (test_and_clear_bit(STATUS_MODE_PENDING, &priv->status))
2278 iwl4965_set_mode(priv, priv->iw_mode);
2279
Zhu Yib481de92007-09-25 17:54:57 -07002280 return;
2281
2282 restart:
2283 queue_work(priv->workqueue, &priv->restart);
2284}
2285
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08002286static void iwl_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002287
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002288static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002289{
2290 unsigned long flags;
2291 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002292
2293 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
2294
Zhu Yib481de92007-09-25 17:54:57 -07002295 if (!exit_pending)
2296 set_bit(STATUS_EXIT_PENDING, &priv->status);
2297
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002298 iwl_leds_unregister(priv);
2299
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07002300 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
2301
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002302 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002303
2304 /* Unblock any waiting calls */
2305 wake_up_interruptible_all(&priv->wait_command_queue);
2306
Zhu Yib481de92007-09-25 17:54:57 -07002307 /* Wipe out the EXIT_PENDING status bit if we are not actually
2308 * exiting the module */
2309 if (!exit_pending)
2310 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2311
2312 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002313 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002314
2315 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002316 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002317 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002318 spin_unlock_irqrestore(&priv->lock, flags);
2319 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002320
2321 if (priv->mac80211_registered)
2322 ieee80211_stop_queues(priv->hw);
2323
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002324 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07002325 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07002326 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002327 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2328 STATUS_RF_KILL_HW |
2329 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2330 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08002331 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2332 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002333 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
2334 STATUS_IN_SUSPEND;
2335 goto exit;
2336 }
2337
2338 /* ...otherwise clear out all the status bits but the RF Kill and
2339 * SUSPEND bits and continue taking the NIC down. */
2340 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2341 STATUS_RF_KILL_HW |
2342 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2343 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08002344 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2345 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002346 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
2347 STATUS_IN_SUSPEND |
2348 test_bit(STATUS_FW_ERROR, &priv->status) <<
2349 STATUS_FW_ERROR;
2350
2351 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002352 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08002353 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07002354 spin_unlock_irqrestore(&priv->lock, flags);
2355
Tomas Winklerda1bc452008-05-29 16:35:00 +08002356 iwl_txq_ctx_stop(priv);
Tomas Winklerb3bbacb2008-05-29 16:35:01 +08002357 iwl_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002358
2359 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002360 if (!iwl_grab_nic_access(priv)) {
2361 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002362 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002363 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002364 }
2365 spin_unlock_irqrestore(&priv->lock, flags);
2366
2367 udelay(5);
2368
Tomas Winkler7f066102008-05-29 16:34:57 +08002369 /* FIXME: apm_ops.suspend(priv) */
2370 priv->cfg->ops->lib->apm_ops.reset(priv);
Ron Rindjunsky399f4902008-04-23 17:14:56 -07002371 priv->cfg->ops->lib->free_shared_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002372
2373 exit:
Tomas Winkler885ba202008-05-29 16:34:55 +08002374 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002375
2376 if (priv->ibss_beacon)
2377 dev_kfree_skb(priv->ibss_beacon);
2378 priv->ibss_beacon = NULL;
2379
2380 /* clear out any free frames */
Tomas Winklerfcab4232008-05-15 13:54:01 +08002381 iwl_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002382}
2383
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002384static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002385{
2386 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002387 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002388 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002389
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08002390 iwl_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002391}
2392
2393#define MAX_HW_RESTARTS 5
2394
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002395static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002396{
Tomas Winkler57aab752008-04-14 21:16:03 -07002397 int i;
2398 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002399
2400 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
2401 IWL_WARNING("Exit pending; will not bring the NIC up\n");
2402 return -EIO;
2403 }
2404
Reinette Chatree903fbd2008-01-30 22:05:15 -08002405 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
2406 IWL_ERROR("ucode not available for device bringup\n");
2407 return -EIO;
2408 }
2409
Zhu Yie655b9f2008-01-24 02:19:38 -08002410 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002411 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08002412 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2413 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2414 else {
2415 set_bit(STATUS_RF_KILL_HW, &priv->status);
2416 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002417 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08002418 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
2419 return -ENODEV;
2420 }
Zhu Yib481de92007-09-25 17:54:57 -07002421 }
2422
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002423 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002424 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002425
Ron Rindjunsky399f4902008-04-23 17:14:56 -07002426 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
2427 if (ret) {
2428 IWL_ERROR("Unable to allocate shared memory\n");
2429 return ret;
2430 }
2431
Ron Rindjunsky1053d352008-05-05 10:22:43 +08002432 ret = iwl_hw_nic_init(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07002433 if (ret) {
2434 IWL_ERROR("Unable to init nic\n");
2435 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002436 }
2437
2438 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002439 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2440 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002441 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2442
2443 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002444 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002445 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002446
2447 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002448 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2449 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002450
2451 /* Copy original ucode data image from disk into backup cache.
2452 * This will be used to initialize the on-board processor's
2453 * data SRAM for a clean start when the runtime program first loads. */
2454 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08002455 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002456
Zhu Yie655b9f2008-01-24 02:19:38 -08002457 /* We return success when we resume from suspend and rf_kill is on. */
Mohamed Abbas64e72c3e2008-06-12 09:47:03 +08002458 if (test_bit(STATUS_RF_KILL_HW, &priv->status) ||
2459 test_bit(STATUS_RF_KILL_SW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07002460 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07002461
2462 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2463
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002464 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002465
2466 /* load bootstrap state machine,
2467 * load bootstrap program into processor's memory,
2468 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07002469 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002470
Tomas Winkler57aab752008-04-14 21:16:03 -07002471 if (ret) {
2472 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07002473 continue;
2474 }
2475
Emmanuel Grumbachf3d5b452008-06-12 09:47:04 +08002476 /* Clear out the uCode error bit if it is set */
2477 clear_bit(STATUS_FW_ERROR, &priv->status);
2478
Zhu Yib481de92007-09-25 17:54:57 -07002479 /* start card; "initialize" will load runtime ucode */
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08002480 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002481
Zhu Yib481de92007-09-25 17:54:57 -07002482 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
2483
2484 return 0;
2485 }
2486
2487 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002488 __iwl4965_down(priv);
Mohamed Abbas64e72c3e2008-06-12 09:47:03 +08002489 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002490
2491 /* tried to restart and config the device for as long as our
2492 * patience could withstand */
2493 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
2494 return -EIO;
2495}
2496
2497
2498/*****************************************************************************
2499 *
2500 * Workqueue callbacks
2501 *
2502 *****************************************************************************/
2503
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002504static void iwl_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002505{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002506 struct iwl_priv *priv =
2507 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002508
2509 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2510 return;
2511
2512 mutex_lock(&priv->mutex);
Emmanuel Grumbachf3ccc082008-05-05 10:22:45 +08002513 priv->cfg->ops->lib->init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002514 mutex_unlock(&priv->mutex);
2515}
2516
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002517static void iwl_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002518{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002519 struct iwl_priv *priv =
2520 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002521
2522 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2523 return;
2524
2525 mutex_lock(&priv->mutex);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002526 iwl_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002527 mutex_unlock(&priv->mutex);
2528}
2529
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002530static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002531{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002532 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07002533
2534 wake_up_interruptible(&priv->wait_command_queue);
2535
2536 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2537 return;
2538
2539 mutex_lock(&priv->mutex);
2540
Tomas Winklerfee12472008-04-03 16:05:21 -07002541 if (!iwl_is_rfkill(priv)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08002542 IWL_DEBUG(IWL_DL_RF_KILL,
Zhu Yib481de92007-09-25 17:54:57 -07002543 "HW and/or SW RF Kill no longer active, restarting "
2544 "device\n");
2545 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
2546 queue_work(priv->workqueue, &priv->restart);
2547 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002548 /* make sure mac80211 stop sending Tx frame */
2549 if (priv->mac80211_registered)
2550 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002551
2552 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
2553 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2554 "disabled by SW switch\n");
2555 else
2556 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
2557 "Kill switch must be turned off for "
2558 "wireless networking to work.\n");
2559 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002560 iwl_rfkill_set_hw_state(priv);
2561
Zhu Yib481de92007-09-25 17:54:57 -07002562 mutex_unlock(&priv->mutex);
2563}
2564
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08002565static void iwl4965_bg_set_monitor(struct work_struct *work)
2566{
2567 struct iwl_priv *priv = container_of(work,
2568 struct iwl_priv, set_monitor);
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002569 int ret;
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08002570
2571 IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
2572
2573 mutex_lock(&priv->mutex);
2574
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002575 ret = iwl4965_set_mode(priv, IEEE80211_IF_TYPE_MNTR);
2576
2577 if (ret) {
2578 if (ret == -EAGAIN)
2579 IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
2580 else
2581 IWL_ERROR("iwl4965_set_mode() failed ret = %d\n", ret);
2582 }
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08002583
2584 mutex_unlock(&priv->mutex);
2585}
2586
Emmanuel Grumbach16e727e2008-06-12 09:46:52 +08002587static void iwl_bg_run_time_calib_work(struct work_struct *work)
2588{
2589 struct iwl_priv *priv = container_of(work, struct iwl_priv,
2590 run_time_calib_work);
2591
2592 mutex_lock(&priv->mutex);
2593
2594 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
2595 test_bit(STATUS_SCANNING, &priv->status)) {
2596 mutex_unlock(&priv->mutex);
2597 return;
2598 }
2599
2600 if (priv->start_calib) {
2601 iwl_chain_noise_calibration(priv, &priv->statistics);
2602
2603 iwl_sensitivity_calibration(priv, &priv->statistics);
2604 }
2605
2606 mutex_unlock(&priv->mutex);
2607 return;
2608}
2609
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002610static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002611{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002612 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07002613
2614 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2615 return;
2616
2617 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002618 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002619 mutex_unlock(&priv->mutex);
2620}
2621
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002622static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002623{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002624 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07002625
2626 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2627 return;
2628
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002629 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002630 queue_work(priv->workqueue, &priv->up);
2631}
2632
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002633static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002634{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002635 struct iwl_priv *priv =
2636 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07002637
2638 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2639 return;
2640
2641 mutex_lock(&priv->mutex);
Tomas Winklera55360e2008-05-05 10:22:28 +08002642 iwl_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002643 mutex_unlock(&priv->mutex);
2644}
2645
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002646#define IWL_DELAY_NEXT_SCAN (HZ*2)
2647
Reinette Chatre508e32e2008-04-14 21:16:13 -07002648static void iwl4965_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002649{
Zhu Yib481de92007-09-25 17:54:57 -07002650 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07002651 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07002652 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002653
2654 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2655 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
2656 return;
2657 }
2658
Joe Perches0795af52007-10-03 17:59:30 -07002659 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
2660 priv->assoc_id,
2661 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07002662
2663
2664 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2665 return;
2666
Zhu Yib481de92007-09-25 17:54:57 -07002667
Reinette Chatre508e32e2008-04-14 21:16:13 -07002668 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08002669 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07002670
Tomas Winkler2a421b92008-06-12 09:47:10 +08002671 iwl_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08002672
Zhu Yib481de92007-09-25 17:54:57 -07002673 conf = ieee80211_get_hw_conf(priv->hw);
2674
2675 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002676 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002677
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002678 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
2679 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07002680 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07002681 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07002682 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07002683 IWL_WARNING("REPLY_RXON_TIMING failed - "
2684 "Attempting to continue.\n");
2685
2686 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
2687
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02002688 if (priv->current_ht_config.is_ht)
Tomas Winkler47c51962008-05-05 10:22:41 +08002689 iwl_set_rxon_ht(priv, &priv->current_ht_config);
Ron Rindjunsky4f85f5b2008-06-09 22:54:35 +03002690
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07002691 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002692 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
2693
2694 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
2695 priv->assoc_id, priv->beacon_int);
2696
2697 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
2698 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2699 else
2700 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2701
2702 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
2703 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2704 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2705 else
2706 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2707
2708 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2709 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2710
2711 }
2712
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002713 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002714
2715 switch (priv->iw_mode) {
2716 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002717 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07002718 break;
2719
2720 case IEEE80211_IF_TYPE_IBSS:
2721
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002722 /* assume default assoc id */
2723 priv->assoc_id = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002724
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08002725 iwl_rxon_add_station(priv, priv->bssid, 0);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002726 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
2727 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002728
2729 break;
2730
2731 default:
2732 IWL_ERROR("%s Should not be called in %d mode\n",
2733 __FUNCTION__, priv->iw_mode);
2734 break;
2735 }
2736
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002737 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002738
Zhu Yib481de92007-09-25 17:54:57 -07002739 /* Enable Rx differential gain and sensitivity calibrations */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07002740 iwl_chain_noise_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002741 priv->start_calib = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002742
2743 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2744 priv->assoc_station_added = 1;
2745
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002746 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08002747
Mohamed Abbas5da4b552008-04-21 15:41:51 -07002748 iwl_power_update_mode(priv, 0);
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002749 /* we have just associated, don't start scan too early */
2750 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Reinette Chatre508e32e2008-04-14 21:16:13 -07002751}
2752
2753
2754static void iwl4965_bg_post_associate(struct work_struct *data)
2755{
2756 struct iwl_priv *priv = container_of(data, struct iwl_priv,
2757 post_associate.work);
2758
2759 mutex_lock(&priv->mutex);
2760 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002761 mutex_unlock(&priv->mutex);
Reinette Chatre508e32e2008-04-14 21:16:13 -07002762
Zhu Yib481de92007-09-25 17:54:57 -07002763}
2764
Zhu Yi76bb77e2007-11-22 10:53:22 +08002765static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
2766
Tomas Winkler2a421b92008-06-12 09:47:10 +08002767static void iwl_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002768{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002769 struct iwl_priv *priv =
2770 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07002771
Tomas Winkler630fe9b2008-06-12 09:47:08 +08002772 IWL_DEBUG_SCAN("SCAN complete scan\n");
Zhu Yib481de92007-09-25 17:54:57 -07002773
2774 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2775 return;
2776
Zhu Yia0646472007-12-20 14:10:01 +08002777 if (test_bit(STATUS_CONF_PENDING, &priv->status))
2778 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08002779
Zhu Yib481de92007-09-25 17:54:57 -07002780 ieee80211_scan_completed(priv->hw);
2781
2782 /* Since setting the TXPOWER may have been deferred while
2783 * performing the scan, fire one off */
2784 mutex_lock(&priv->mutex);
Tomas Winkler630fe9b2008-06-12 09:47:08 +08002785 iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
Zhu Yib481de92007-09-25 17:54:57 -07002786 mutex_unlock(&priv->mutex);
2787}
2788
2789/*****************************************************************************
2790 *
2791 * mac80211 entry point functions
2792 *
2793 *****************************************************************************/
2794
Zhu Yi5a66926a2008-01-14 17:46:18 -08002795#define UCODE_READY_TIMEOUT (2 * HZ)
2796
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002797static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002798{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002799 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08002800 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002801
2802 IWL_DEBUG_MAC80211("enter\n");
2803
Zhu Yi5a66926a2008-01-14 17:46:18 -08002804 if (pci_enable_device(priv->pci_dev)) {
2805 IWL_ERROR("Fail to pci_enable_device\n");
2806 return -ENODEV;
2807 }
2808 pci_restore_state(priv->pci_dev);
2809 pci_enable_msi(priv->pci_dev);
2810
2811 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
2812 DRV_NAME, priv);
2813 if (ret) {
2814 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
2815 goto out_disable_msi;
2816 }
2817
Zhu Yib481de92007-09-25 17:54:57 -07002818 /* we should be verifying the device is ready to be opened */
2819 mutex_lock(&priv->mutex);
2820
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08002821 memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
Zhu Yi5a66926a2008-01-14 17:46:18 -08002822 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
2823 * ucode filename and max sizes are card-specific. */
2824
2825 if (!priv->ucode_code.len) {
2826 ret = iwl4965_read_ucode(priv);
2827 if (ret) {
2828 IWL_ERROR("Could not read microcode: %d\n", ret);
2829 mutex_unlock(&priv->mutex);
2830 goto out_release_irq;
2831 }
2832 }
2833
Zhu Yie655b9f2008-01-24 02:19:38 -08002834 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002835
Zhu Yib481de92007-09-25 17:54:57 -07002836 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002837
Zhu Yie655b9f2008-01-24 02:19:38 -08002838 if (ret)
2839 goto out_release_irq;
2840
2841 IWL_DEBUG_INFO("Start UP work done.\n");
2842
2843 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
2844 return 0;
2845
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08002846 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
Zhu Yi5a66926a2008-01-14 17:46:18 -08002847 * mac80211 will not be run successfully. */
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08002848 if (priv->ucode_type == UCODE_RT) {
2849 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
2850 test_bit(STATUS_READY, &priv->status),
2851 UCODE_READY_TIMEOUT);
2852 if (!ret) {
2853 if (!test_bit(STATUS_READY, &priv->status)) {
2854 IWL_ERROR("START_ALIVE timeout after %dms.\n",
2855 jiffies_to_msecs(UCODE_READY_TIMEOUT));
2856 ret = -ETIMEDOUT;
2857 goto out_release_irq;
2858 }
Zhu Yi5a66926a2008-01-14 17:46:18 -08002859 }
Zhu Yi5a66926a2008-01-14 17:46:18 -08002860
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08002861 priv->is_open = 1;
2862 }
Zhu Yib481de92007-09-25 17:54:57 -07002863 IWL_DEBUG_MAC80211("leave\n");
2864 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08002865
2866out_release_irq:
2867 free_irq(priv->pci_dev->irq, priv);
2868out_disable_msi:
2869 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08002870 pci_disable_device(priv->pci_dev);
2871 priv->is_open = 0;
2872 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08002873 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002874}
2875
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002876static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002877{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002878 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002879
2880 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08002881
Zhu Yie655b9f2008-01-24 02:19:38 -08002882 if (!priv->is_open) {
2883 IWL_DEBUG_MAC80211("leave - skip\n");
2884 return;
2885 }
2886
Zhu Yib481de92007-09-25 17:54:57 -07002887 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08002888
Tomas Winklerfee12472008-04-03 16:05:21 -07002889 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08002890 /* stop mac, cancel any scan request and clear
2891 * RXON_FILTER_ASSOC_MSK BIT
2892 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08002893 mutex_lock(&priv->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +08002894 iwl_scan_cancel_timeout(priv, 100);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002895 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08002896 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08002897 }
2898
Zhu Yi5a66926a2008-01-14 17:46:18 -08002899 iwl4965_down(priv);
2900
2901 flush_workqueue(priv->workqueue);
2902 free_irq(priv->pci_dev->irq, priv);
2903 pci_disable_msi(priv->pci_dev);
2904 pci_save_state(priv->pci_dev);
2905 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08002906
Zhu Yib481de92007-09-25 17:54:57 -07002907 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002908}
2909
Johannes Berge039fa42008-05-15 12:55:29 +02002910static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07002911{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002912 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002913
2914 IWL_DEBUG_MAC80211("enter\n");
2915
2916 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
2917 IWL_DEBUG_MAC80211("leave - monitor\n");
2918 return -1;
2919 }
2920
2921 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02002922 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07002923
Johannes Berge039fa42008-05-15 12:55:29 +02002924 if (iwl_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07002925 dev_kfree_skb_any(skb);
2926
2927 IWL_DEBUG_MAC80211("leave\n");
2928 return 0;
2929}
2930
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002931static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07002932 struct ieee80211_if_init_conf *conf)
2933{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002934 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002935 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07002936 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002937
Johannes Berg32bfd352007-12-19 01:31:26 +01002938 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07002939
Johannes Berg32bfd352007-12-19 01:31:26 +01002940 if (priv->vif) {
2941 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08002942 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07002943 }
2944
2945 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01002946 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07002947
2948 spin_unlock_irqrestore(&priv->lock, flags);
2949
2950 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02002951
2952 if (conf->mac_addr) {
2953 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
2954 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
2955 }
Zhu Yib481de92007-09-25 17:54:57 -07002956
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002957 if (iwl4965_set_mode(priv, conf->type) == -EAGAIN)
2958 /* we are not ready, will run again when ready */
2959 set_bit(STATUS_MODE_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002960
Zhu Yib481de92007-09-25 17:54:57 -07002961 mutex_unlock(&priv->mutex);
2962
Zhu Yi5a66926a2008-01-14 17:46:18 -08002963 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002964 return 0;
2965}
2966
2967/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002968 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07002969 *
2970 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
2971 * be set inappropriately and the driver currently sets the hardware up to
2972 * use it whenever needed.
2973 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002974static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07002975{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002976 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002977 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002978 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08002979 int ret = 0;
Tomas Winkler82a66bb2008-05-29 16:35:28 +08002980 u16 channel;
Zhu Yib481de92007-09-25 17:54:57 -07002981
2982 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002983 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002984
Zhu Yi12342c42007-12-20 11:27:32 +08002985 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
2986
Mohamed Abbas64e72c3e2008-06-12 09:47:03 +08002987
2988 if (priv->cfg->ops->lib->radio_kill_sw &&
2989 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled)) {
2990 IWL_DEBUG_MAC80211("leave - RF-KILL - waiting for uCode\n");
2991 mutex_unlock(&priv->mutex);
2992 }
2993
Tomas Winklerfee12472008-04-03 16:05:21 -07002994 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002995 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08002996 ret = -EIO;
2997 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002998 }
2999
Assaf Krauss1ea87392008-03-18 14:57:50 -07003000 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07003001 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08003002 IWL_DEBUG_MAC80211("leave - scanning\n");
3003 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003004 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08003005 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003006 }
3007
Tomas Winkler82a66bb2008-05-29 16:35:28 +08003008 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
3009 ch_info = iwl_get_channel_info(priv, conf->channel->band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07003010 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07003011 IWL_DEBUG_MAC80211("leave - invalid channel\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08003012 ret = -EINVAL;
3013 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003014 }
3015
Assaf Krauss398f9e72008-06-12 09:47:06 +08003016 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS &&
3017 !is_channel_ibss(ch_info)) {
3018 IWL_ERROR("channel %d in band %d not IBSS channel\n",
3019 conf->channel->hw_value, conf->channel->band);
3020 ret = -EINVAL;
3021 goto out;
3022 }
3023
Tomas Winkler82a66bb2008-05-29 16:35:28 +08003024 spin_lock_irqsave(&priv->lock, flags);
3025
Tomas Winkler78330fd2008-02-06 02:37:18 +02003026 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07003027 * from any ht related info since 2.4 does not
3028 * support ht */
Tomas Winkler82a66bb2008-05-29 16:35:28 +08003029 if ((le16_to_cpu(priv->staging_rxon.channel) != channel)
Zhu Yib481de92007-09-25 17:54:57 -07003030#ifdef IEEE80211_CONF_CHANNEL_SWITCH
3031 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
3032#endif
3033 )
3034 priv->staging_rxon.flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003035
Tomas Winkler82a66bb2008-05-29 16:35:28 +08003036 iwl_set_rxon_channel(priv, conf->channel->band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07003037
Tomas Winkler82a66bb2008-05-29 16:35:28 +08003038 iwl_set_flags_for_band(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07003039
3040 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01003041 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07003042 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003043 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003044
3045 spin_unlock_irqrestore(&priv->lock, flags);
3046
3047#ifdef IEEE80211_CONF_CHANNEL_SWITCH
3048 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003049 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08003050 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003051 }
3052#endif
3053
Zhu Yib481de92007-09-25 17:54:57 -07003054 if (!conf->radio_enabled) {
3055 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08003056 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003057 }
3058
Tomas Winklerfee12472008-04-03 16:05:21 -07003059 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003060 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08003061 ret = -EIO;
3062 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003063 }
3064
Tomas Winkler630fe9b2008-06-12 09:47:08 +08003065 IWL_DEBUG_MAC80211("TX Power old=%d new=%d\n",
3066 priv->tx_power_user_lmt, conf->power_level);
3067
3068 iwl_set_tx_power(priv, conf->power_level, false);
3069
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003070 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003071
3072 if (memcmp(&priv->active_rxon,
3073 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003074 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003075 else
3076 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
3077
3078 IWL_DEBUG_MAC80211("leave\n");
3079
Zhu Yia0646472007-12-20 14:10:01 +08003080out:
3081 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003082 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08003083 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003084}
3085
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003086static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003087{
Tomas Winkler857485c2008-03-21 13:53:44 -07003088 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003089
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003090 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003091 return;
3092
3093 /* The following should be done only at AP bring up */
3094 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
3095
3096 /* RXON - unassoc (to set timing command) */
3097 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003098 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003099
3100 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003101 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
3102 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07003103 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07003104 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07003105 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07003106 IWL_WARNING("REPLY_RXON_TIMING failed - "
3107 "Attempting to continue.\n");
3108
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07003109 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003110
3111 /* FIXME: what should be the assoc_id for AP? */
3112 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
3113 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3114 priv->staging_rxon.flags |=
3115 RXON_FLG_SHORT_PREAMBLE_MSK;
3116 else
3117 priv->staging_rxon.flags &=
3118 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3119
3120 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
3121 if (priv->assoc_capability &
3122 WLAN_CAPABILITY_SHORT_SLOT_TIME)
3123 priv->staging_rxon.flags |=
3124 RXON_FLG_SHORT_SLOT_MSK;
3125 else
3126 priv->staging_rxon.flags &=
3127 ~RXON_FLG_SHORT_SLOT_MSK;
3128
3129 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
3130 priv->staging_rxon.flags &=
3131 ~RXON_FLG_SHORT_SLOT_MSK;
3132 }
3133 /* restore RXON assoc */
3134 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003135 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003136 iwl4965_activate_qos(priv, 1);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08003137 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08003138 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003139 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003140
3141 /* FIXME - we need to add code here to detect a totally new
3142 * configuration, reset the AP, unassoc, rxon timing, assoc,
3143 * clear sta table, add BCAST sta... */
3144}
3145
Johannes Berg32bfd352007-12-19 01:31:26 +01003146static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
3147 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07003148 struct ieee80211_if_conf *conf)
3149{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003150 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07003151 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07003152 unsigned long flags;
3153 int rc;
3154
3155 if (conf == NULL)
3156 return -EIO;
3157
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08003158 if (priv->vif != vif) {
3159 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08003160 return 0;
3161 }
3162
Zhu Yib481de92007-09-25 17:54:57 -07003163 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3164 (!conf->beacon || !conf->ssid_len)) {
3165 IWL_DEBUG_MAC80211
3166 ("Leaving in AP mode because HostAPD is not ready.\n");
3167 return 0;
3168 }
3169
Tomas Winklerfee12472008-04-03 16:05:21 -07003170 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08003171 return -EAGAIN;
3172
Zhu Yib481de92007-09-25 17:54:57 -07003173 mutex_lock(&priv->mutex);
3174
Zhu Yib481de92007-09-25 17:54:57 -07003175 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07003176 IWL_DEBUG_MAC80211("bssid: %s\n",
3177 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07003178
Johannes Berg4150c572007-09-17 01:29:23 -04003179/*
3180 * very dubious code was here; the probe filtering flag is never set:
3181 *
Zhu Yib481de92007-09-25 17:54:57 -07003182 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
3183 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04003184 */
Zhu Yib481de92007-09-25 17:54:57 -07003185
3186 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
3187 if (!conf->bssid) {
3188 conf->bssid = priv->mac_addr;
3189 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07003190 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
3191 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07003192 }
3193 if (priv->ibss_beacon)
3194 dev_kfree_skb(priv->ibss_beacon);
3195
3196 priv->ibss_beacon = conf->beacon;
3197 }
3198
Tomas Winklerfee12472008-04-03 16:05:21 -07003199 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08003200 goto done;
3201
Zhu Yib481de92007-09-25 17:54:57 -07003202 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
3203 !is_multicast_ether_addr(conf->bssid)) {
3204 /* If there is currently a HW scan going on in the background
3205 * then we need to cancel it else the RXON below will fail. */
Tomas Winkler2a421b92008-06-12 09:47:10 +08003206 if (iwl_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07003207 IWL_WARNING("Aborted scan still in progress "
3208 "after 100ms\n");
3209 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
3210 mutex_unlock(&priv->mutex);
3211 return -EAGAIN;
3212 }
3213 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
3214
3215 /* TODO: Audit driver for usage of these members and see
3216 * if mac80211 deprecates them (priv->bssid looks like it
3217 * shouldn't be there, but I haven't scanned the IBSS code
3218 * to verify) - jpk */
3219 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
3220
3221 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003222 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003223 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003224 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003225 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08003226 iwl_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07003227 priv, priv->active_rxon.bssid_addr, 1);
3228 }
3229
3230 } else {
Tomas Winkler2a421b92008-06-12 09:47:10 +08003231 iwl_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07003232 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003233 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003234 }
3235
Mohamed Abbasfde35712007-11-29 11:10:15 +08003236 done:
Zhu Yib481de92007-09-25 17:54:57 -07003237 spin_lock_irqsave(&priv->lock, flags);
3238 if (!conf->ssid_len)
3239 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
3240 else
3241 memcpy(priv->essid, conf->ssid, conf->ssid_len);
3242
3243 priv->essid_len = conf->ssid_len;
3244 spin_unlock_irqrestore(&priv->lock, flags);
3245
3246 IWL_DEBUG_MAC80211("leave\n");
3247 mutex_unlock(&priv->mutex);
3248
3249 return 0;
3250}
3251
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003252static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04003253 unsigned int changed_flags,
3254 unsigned int *total_flags,
3255 int mc_count, struct dev_addr_list *mc_list)
3256{
3257 /*
3258 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003259 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04003260 */
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08003261 struct iwl_priv *priv = hw->priv;
3262 int new_flags = 0;
3263 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
3264 if (*total_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
3265 IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
3266 IEEE80211_IF_TYPE_MNTR,
3267 changed_flags, *total_flags);
3268 /* queue work 'cuz mac80211 is holding a lock which
3269 * prevents us from issuing (synchronous) f/w cmds */
3270 queue_work(priv->workqueue, &priv->set_monitor);
3271 new_flags &= FIF_PROMISC_IN_BSS |
3272 FIF_OTHER_BSS |
3273 FIF_ALLMULTI;
3274 }
3275 }
3276 *total_flags = new_flags;
Johannes Berg4150c572007-09-17 01:29:23 -04003277}
3278
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003279static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003280 struct ieee80211_if_init_conf *conf)
3281{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003282 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003283
3284 IWL_DEBUG_MAC80211("enter\n");
3285
3286 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08003287
Tomas Winklerfee12472008-04-03 16:05:21 -07003288 if (iwl_is_ready_rf(priv)) {
Tomas Winkler2a421b92008-06-12 09:47:10 +08003289 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003290 cancel_delayed_work(&priv->post_associate);
3291 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3292 iwl4965_commit_rxon(priv);
3293 }
Johannes Berg32bfd352007-12-19 01:31:26 +01003294 if (priv->vif == conf->vif) {
3295 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003296 memset(priv->bssid, 0, ETH_ALEN);
3297 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
3298 priv->essid_len = 0;
3299 }
3300 mutex_unlock(&priv->mutex);
3301
3302 IWL_DEBUG_MAC80211("leave\n");
3303
3304}
Johannes Berg471b3ef2007-12-28 14:32:58 +01003305
Tomas Winkler3109ece2008-03-28 16:33:35 -07003306#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01003307static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
3308 struct ieee80211_vif *vif,
3309 struct ieee80211_bss_conf *bss_conf,
3310 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02003311{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003312 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02003313
Tomas Winkler3109ece2008-03-28 16:33:35 -07003314 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
3315
Johannes Berg471b3ef2007-12-28 14:32:58 +01003316 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07003317 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
3318 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01003319 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02003320 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
3321 else
3322 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
3323 }
3324
Johannes Berg471b3ef2007-12-28 14:32:58 +01003325 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07003326 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01003327 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02003328 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
3329 else
3330 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
3331 }
3332
Tomas Winkler98952d52008-03-28 16:33:33 -07003333 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07003334 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07003335 iwl4965_ht_conf(priv, bss_conf);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07003336 iwl_set_rxon_chain(priv);
Tomas Winkler98952d52008-03-28 16:33:33 -07003337 }
3338
Johannes Berg471b3ef2007-12-28 14:32:58 +01003339 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07003340 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
Reinette Chatre508e32e2008-04-14 21:16:13 -07003341 /* This should never happen as this function should
3342 * never be called from interrupt context. */
3343 if (WARN_ON_ONCE(in_interrupt()))
3344 return;
Tomas Winkler3109ece2008-03-28 16:33:35 -07003345 if (bss_conf->assoc) {
3346 priv->assoc_id = bss_conf->aid;
3347 priv->beacon_int = bss_conf->beacon_int;
3348 priv->timestamp = bss_conf->timestamp;
3349 priv->assoc_capability = bss_conf->assoc_capability;
3350 priv->next_scan_jiffies = jiffies +
3351 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
Reinette Chatre508e32e2008-04-14 21:16:13 -07003352 mutex_lock(&priv->mutex);
3353 iwl4965_post_associate(priv);
3354 mutex_unlock(&priv->mutex);
Tomas Winkler3109ece2008-03-28 16:33:35 -07003355 } else {
3356 priv->assoc_id = 0;
3357 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
3358 }
3359 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
3360 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
Tomas Winkler7e8c5192008-04-15 16:01:43 -07003361 iwl_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01003362 }
3363
Tomas Winkler220173b2007-10-16 00:50:25 +02003364}
Zhu Yib481de92007-09-25 17:54:57 -07003365
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003366static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07003367{
3368 int rc = 0;
3369 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003370 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003371
3372 IWL_DEBUG_MAC80211("enter\n");
3373
mabbas052c4b92007-10-25 17:15:43 +08003374 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07003375 spin_lock_irqsave(&priv->lock, flags);
3376
Tomas Winklerfee12472008-04-03 16:05:21 -07003377 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003378 rc = -EIO;
3379 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
3380 goto out_unlock;
3381 }
3382
3383 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
3384 rc = -EIO;
3385 IWL_ERROR("ERROR: APs don't scan\n");
3386 goto out_unlock;
3387 }
3388
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003389 /* we don't schedule scan within next_scan_jiffies period */
3390 if (priv->next_scan_jiffies &&
3391 time_after(priv->next_scan_jiffies, jiffies)) {
3392 rc = -EAGAIN;
3393 goto out_unlock;
3394 }
Zhu Yib481de92007-09-25 17:54:57 -07003395 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003396 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
3397 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07003398 rc = -EAGAIN;
3399 goto out_unlock;
3400 }
3401 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003402 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Tomas Winkler2a421b92008-06-12 09:47:10 +08003403 iwl_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07003404
3405 priv->one_direct_scan = 1;
3406 priv->direct_ssid_len = (u8)
3407 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
3408 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08003409 } else
3410 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003411
Tomas Winkler2a421b92008-06-12 09:47:10 +08003412 rc = iwl_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003413
3414 IWL_DEBUG_MAC80211("leave\n");
3415
3416out_unlock:
3417 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08003418 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07003419
3420 return rc;
3421}
3422
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003423static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
3424 struct ieee80211_key_conf *keyconf, const u8 *addr,
3425 u32 iv32, u16 *phase1key)
3426{
3427 struct iwl_priv *priv = hw->priv;
3428 u8 sta_id = IWL_INVALID_STATION;
3429 unsigned long flags;
3430 __le16 key_flags = 0;
3431 int i;
3432 DECLARE_MAC_BUF(mac);
3433
3434 IWL_DEBUG_MAC80211("enter\n");
3435
Tomas Winkler947b13a2008-04-16 16:34:48 -07003436 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003437 if (sta_id == IWL_INVALID_STATION) {
3438 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
3439 print_mac(mac, addr));
3440 return;
3441 }
3442
Tomas Winkler2a421b92008-06-12 09:47:10 +08003443 iwl_scan_cancel_timeout(priv, 100);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003444
3445 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
3446 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
3447 key_flags &= ~STA_KEY_FLG_INVALID;
3448
Tomas Winkler5425e492008-04-15 16:01:38 -07003449 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003450 key_flags |= STA_KEY_MULTICAST_MSK;
3451
3452 spin_lock_irqsave(&priv->sta_lock, flags);
3453
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003454 priv->stations[sta_id].sta.key.key_flags = key_flags;
3455 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
3456
3457 for (i = 0; i < 5; i++)
3458 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
3459 cpu_to_le16(phase1key[i]);
3460
3461 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3462 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3463
Tomas Winkler133636d2008-05-05 10:22:34 +08003464 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003465
3466 spin_unlock_irqrestore(&priv->sta_lock, flags);
3467
3468 IWL_DEBUG_MAC80211("leave\n");
3469}
3470
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003471static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07003472 const u8 *local_addr, const u8 *addr,
3473 struct ieee80211_key_conf *key)
3474{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003475 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07003476 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003477 int ret = 0;
3478 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003479 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003480
3481 IWL_DEBUG_MAC80211("enter\n");
3482
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07003483 if (priv->hw_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07003484 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
3485 return -EOPNOTSUPP;
3486 }
3487
3488 if (is_zero_ether_addr(addr))
3489 /* only support pairwise keys */
3490 return -EOPNOTSUPP;
3491
Tomas Winkler947b13a2008-04-16 16:34:48 -07003492 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003493 if (sta_id == IWL_INVALID_STATION) {
3494 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
3495 print_mac(mac, addr));
3496 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003497
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003498 }
Zhu Yib481de92007-09-25 17:54:57 -07003499
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003500 mutex_lock(&priv->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +08003501 iwl_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003502 mutex_unlock(&priv->mutex);
3503
3504 /* If we are getting WEP group key and we didn't receive any key mapping
3505 * so far, we are in legacy wep mode (group key only), otherwise we are
3506 * in 1X mode.
3507 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07003508 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003509 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
3510 if (cmd == SET_KEY)
3511 is_default_wep_key = !priv->key_mapping_key;
3512 else
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08003513 is_default_wep_key =
3514 (key->hw_key_idx == HW_KEY_DEFAULT);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003515 }
mabbas052c4b92007-10-25 17:15:43 +08003516
Zhu Yib481de92007-09-25 17:54:57 -07003517 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003518 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003519 if (is_default_wep_key)
3520 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003521 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07003522 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003523
3524 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003525 break;
3526 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003527 if (is_default_wep_key)
3528 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003529 else
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07003530 ret = iwl_remove_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003531
3532 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003533 break;
3534 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003535 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003536 }
3537
3538 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003539
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003540 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003541}
3542
Johannes Berge100bb62008-04-30 18:51:21 +02003543static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07003544 const struct ieee80211_tx_queue_params *params)
3545{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003546 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003547 unsigned long flags;
3548 int q;
Zhu Yib481de92007-09-25 17:54:57 -07003549
3550 IWL_DEBUG_MAC80211("enter\n");
3551
Tomas Winklerfee12472008-04-03 16:05:21 -07003552 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003553 IWL_DEBUG_MAC80211("leave - RF not ready\n");
3554 return -EIO;
3555 }
3556
3557 if (queue >= AC_NUM) {
3558 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
3559 return 0;
3560 }
3561
Zhu Yib481de92007-09-25 17:54:57 -07003562 if (!priv->qos_data.qos_enable) {
3563 priv->qos_data.qos_active = 0;
3564 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
3565 return 0;
3566 }
3567 q = AC_NUM - 1 - queue;
3568
3569 spin_lock_irqsave(&priv->lock, flags);
3570
3571 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
3572 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
3573 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
3574 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01003575 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07003576
3577 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
3578 priv->qos_data.qos_active = 1;
3579
3580 spin_unlock_irqrestore(&priv->lock, flags);
3581
3582 mutex_lock(&priv->mutex);
3583 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003584 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07003585 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003586 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003587
3588 mutex_unlock(&priv->mutex);
3589
Zhu Yib481de92007-09-25 17:54:57 -07003590 IWL_DEBUG_MAC80211("leave\n");
3591 return 0;
3592}
3593
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003594static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003595 struct ieee80211_tx_queue_stats *stats)
3596{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003597 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003598 int i, avail;
Ron Rindjunsky16466902008-05-05 10:22:50 +08003599 struct iwl_tx_queue *txq;
Tomas Winkler443cfd42008-05-15 13:53:57 +08003600 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07003601 unsigned long flags;
3602
3603 IWL_DEBUG_MAC80211("enter\n");
3604
Tomas Winklerfee12472008-04-03 16:05:21 -07003605 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003606 IWL_DEBUG_MAC80211("leave - RF not ready\n");
3607 return -EIO;
3608 }
3609
3610 spin_lock_irqsave(&priv->lock, flags);
3611
3612 for (i = 0; i < AC_NUM; i++) {
3613 txq = &priv->txq[i];
3614 q = &txq->q;
Tomas Winkler443cfd42008-05-15 13:53:57 +08003615 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07003616
Johannes Berg57ffc582008-04-29 17:18:59 +02003617 stats[i].len = q->n_window - avail;
3618 stats[i].limit = q->n_window - q->high_mark;
3619 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07003620
3621 }
3622 spin_unlock_irqrestore(&priv->lock, flags);
3623
3624 IWL_DEBUG_MAC80211("leave\n");
3625
3626 return 0;
3627}
3628
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003629static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003630 struct ieee80211_low_level_stats *stats)
3631{
Ester Kummerbf403db2008-05-05 10:22:40 +08003632 struct iwl_priv *priv = hw->priv;
3633
3634 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003635 IWL_DEBUG_MAC80211("enter\n");
3636 IWL_DEBUG_MAC80211("leave\n");
3637
3638 return 0;
3639}
3640
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003641static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003642{
Ester Kummerbf403db2008-05-05 10:22:40 +08003643 struct iwl_priv *priv;
3644
3645 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003646 IWL_DEBUG_MAC80211("enter\n");
3647 IWL_DEBUG_MAC80211("leave\n");
3648
3649 return 0;
3650}
3651
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003652static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003653{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003654 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003655 unsigned long flags;
3656
3657 mutex_lock(&priv->mutex);
3658 IWL_DEBUG_MAC80211("enter\n");
3659
3660 priv->lq_mngr.lq_ready = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003661 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02003662 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07003663 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -07003664
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07003665 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003666
3667 cancel_delayed_work(&priv->post_associate);
3668
3669 spin_lock_irqsave(&priv->lock, flags);
3670 priv->assoc_id = 0;
3671 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003672 priv->assoc_station_added = 0;
3673
3674 /* new association get rid of ibss beacon skb */
3675 if (priv->ibss_beacon)
3676 dev_kfree_skb(priv->ibss_beacon);
3677
3678 priv->ibss_beacon = NULL;
3679
3680 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07003681 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003682 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
3683 priv->beacon_int = 0;
3684
3685 spin_unlock_irqrestore(&priv->lock, flags);
3686
Tomas Winklerfee12472008-04-03 16:05:21 -07003687 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08003688 IWL_DEBUG_MAC80211("leave - not ready\n");
3689 mutex_unlock(&priv->mutex);
3690 return;
3691 }
3692
mabbas052c4b92007-10-25 17:15:43 +08003693 /* we are restarting association process
3694 * clear RXON_FILTER_ASSOC_MSK bit
3695 */
3696 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Tomas Winkler2a421b92008-06-12 09:47:10 +08003697 iwl_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08003698 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003699 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08003700 }
3701
Mohamed Abbas5da4b552008-04-21 15:41:51 -07003702 iwl_power_update_mode(priv, 0);
3703
Zhu Yib481de92007-09-25 17:54:57 -07003704 /* Per mac80211.h: This is only used in IBSS mode... */
3705 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08003706
Zhu Yib481de92007-09-25 17:54:57 -07003707 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
3708 mutex_unlock(&priv->mutex);
3709 return;
3710 }
3711
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003712 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003713
3714 mutex_unlock(&priv->mutex);
3715
3716 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003717}
3718
Johannes Berge039fa42008-05-15 12:55:29 +02003719static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003720{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003721 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003722 unsigned long flags;
3723
3724 mutex_lock(&priv->mutex);
3725 IWL_DEBUG_MAC80211("enter\n");
3726
Tomas Winklerfee12472008-04-03 16:05:21 -07003727 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003728 IWL_DEBUG_MAC80211("leave - RF not ready\n");
3729 mutex_unlock(&priv->mutex);
3730 return -EIO;
3731 }
3732
3733 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
3734 IWL_DEBUG_MAC80211("leave - not IBSS\n");
3735 mutex_unlock(&priv->mutex);
3736 return -EIO;
3737 }
3738
3739 spin_lock_irqsave(&priv->lock, flags);
3740
3741 if (priv->ibss_beacon)
3742 dev_kfree_skb(priv->ibss_beacon);
3743
3744 priv->ibss_beacon = skb;
3745
3746 priv->assoc_id = 0;
3747
3748 IWL_DEBUG_MAC80211("leave\n");
3749 spin_unlock_irqrestore(&priv->lock, flags);
3750
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07003751 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003752
Assaf Kraussc46fbef2008-06-12 09:47:05 +08003753 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003754
3755 mutex_unlock(&priv->mutex);
3756
3757 return 0;
3758}
3759
Zhu Yib481de92007-09-25 17:54:57 -07003760/*****************************************************************************
3761 *
3762 * sysfs attributes
3763 *
3764 *****************************************************************************/
3765
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003766#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003767
3768/*
3769 * The following adds a new attribute to the sysfs representation
3770 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3771 * used for controlling the debug level.
3772 *
3773 * See the level definitions in iwl for details.
3774 */
3775
Ester Kummer8cf769c2008-05-06 11:05:11 +08003776static ssize_t show_debug_level(struct device *d,
3777 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003778{
Ester Kummer8cf769c2008-05-06 11:05:11 +08003779 struct iwl_priv *priv = d->driver_data;
3780
3781 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003782}
Ester Kummer8cf769c2008-05-06 11:05:11 +08003783static ssize_t store_debug_level(struct device *d,
3784 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003785 const char *buf, size_t count)
3786{
Ester Kummer8cf769c2008-05-06 11:05:11 +08003787 struct iwl_priv *priv = d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003788 char *p = (char *)buf;
3789 u32 val;
3790
3791 val = simple_strtoul(p, &p, 0);
3792 if (p == buf)
3793 printk(KERN_INFO DRV_NAME
3794 ": %s is not in hex or decimal form.\n", buf);
3795 else
Ester Kummer8cf769c2008-05-06 11:05:11 +08003796 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07003797
3798 return strnlen(buf, count);
3799}
3800
Ester Kummer8cf769c2008-05-06 11:05:11 +08003801static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3802 show_debug_level, store_debug_level);
3803
Zhu Yib481de92007-09-25 17:54:57 -07003804
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003805#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003806
Zhu Yib481de92007-09-25 17:54:57 -07003807
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08003808static ssize_t show_version(struct device *d,
3809 struct device_attribute *attr, char *buf)
3810{
3811 struct iwl_priv *priv = d->driver_data;
Tomas Winkler885ba202008-05-29 16:34:55 +08003812 struct iwl_alive_resp *palive = &priv->card_alive;
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08003813
3814 if (palive->is_valid)
3815 return sprintf(buf, "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
3816 "fw type: 0x%01X 0x%01X\n",
3817 palive->ucode_major, palive->ucode_minor,
3818 palive->sw_rev[0], palive->sw_rev[1],
3819 palive->ver_type, palive->ver_subtype);
3820
3821 else
3822 return sprintf(buf, "fw not loaded\n");
3823}
3824
3825static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
3826
Zhu Yib481de92007-09-25 17:54:57 -07003827static ssize_t show_temperature(struct device *d,
3828 struct device_attribute *attr, char *buf)
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
Tomas Winklerfee12472008-04-03 16:05:21 -07003832 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003833 return -EAGAIN;
3834
Emmanuel Grumbach91dbc5bd2008-06-12 09:47:14 +08003835 return sprintf(buf, "%d\n", priv->temperature);
Zhu Yib481de92007-09-25 17:54:57 -07003836}
3837
3838static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3839
3840static ssize_t show_rs_window(struct device *d,
3841 struct device_attribute *attr,
3842 char *buf)
3843{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003844 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003845 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003846}
3847static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
3848
3849static ssize_t show_tx_power(struct device *d,
3850 struct device_attribute *attr, char *buf)
3851{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003852 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winkler630fe9b2008-06-12 09:47:08 +08003853 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07003854}
3855
3856static ssize_t store_tx_power(struct device *d,
3857 struct device_attribute *attr,
3858 const char *buf, size_t count)
3859{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003860 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003861 char *p = (char *)buf;
3862 u32 val;
3863
3864 val = simple_strtoul(p, &p, 10);
3865 if (p == buf)
3866 printk(KERN_INFO DRV_NAME
3867 ": %s is not in decimal form.\n", buf);
3868 else
Tomas Winkler630fe9b2008-06-12 09:47:08 +08003869 iwl_set_tx_power(priv, val, false);
Zhu Yib481de92007-09-25 17:54:57 -07003870
3871 return count;
3872}
3873
3874static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3875
3876static ssize_t show_flags(struct device *d,
3877 struct device_attribute *attr, char *buf)
3878{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003879 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003880
3881 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
3882}
3883
3884static ssize_t store_flags(struct device *d,
3885 struct device_attribute *attr,
3886 const char *buf, size_t count)
3887{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003888 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003889 u32 flags = simple_strtoul(buf, NULL, 0);
3890
3891 mutex_lock(&priv->mutex);
3892 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
3893 /* Cancel any currently running scans... */
Tomas Winkler2a421b92008-06-12 09:47:10 +08003894 if (iwl_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07003895 IWL_WARNING("Could not cancel scan.\n");
3896 else {
3897 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
3898 flags);
3899 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003900 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003901 }
3902 }
3903 mutex_unlock(&priv->mutex);
3904
3905 return count;
3906}
3907
3908static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3909
3910static ssize_t show_filter_flags(struct device *d,
3911 struct device_attribute *attr, char *buf)
3912{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003913 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003914
3915 return sprintf(buf, "0x%04X\n",
3916 le32_to_cpu(priv->active_rxon.filter_flags));
3917}
3918
3919static ssize_t store_filter_flags(struct device *d,
3920 struct device_attribute *attr,
3921 const char *buf, size_t count)
3922{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003923 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003924 u32 filter_flags = simple_strtoul(buf, NULL, 0);
3925
3926 mutex_lock(&priv->mutex);
3927 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
3928 /* Cancel any currently running scans... */
Tomas Winkler2a421b92008-06-12 09:47:10 +08003929 if (iwl_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07003930 IWL_WARNING("Could not cancel scan.\n");
3931 else {
3932 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
3933 "0x%04X\n", filter_flags);
3934 priv->staging_rxon.filter_flags =
3935 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003936 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003937 }
3938 }
3939 mutex_unlock(&priv->mutex);
3940
3941 return count;
3942}
3943
3944static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3945 store_filter_flags);
3946
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003947#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07003948
3949static ssize_t show_measurement(struct device *d,
3950 struct device_attribute *attr, char *buf)
3951{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003952 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003953 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003954 u32 size = sizeof(measure_report), len = 0, ofs = 0;
3955 u8 *data = (u8 *) & measure_report;
3956 unsigned long flags;
3957
3958 spin_lock_irqsave(&priv->lock, flags);
3959 if (!(priv->measurement_status & MEASUREMENT_READY)) {
3960 spin_unlock_irqrestore(&priv->lock, flags);
3961 return 0;
3962 }
3963 memcpy(&measure_report, &priv->measure_report, size);
3964 priv->measurement_status = 0;
3965 spin_unlock_irqrestore(&priv->lock, flags);
3966
3967 while (size && (PAGE_SIZE - len)) {
3968 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3969 PAGE_SIZE - len, 1);
3970 len = strlen(buf);
3971 if (PAGE_SIZE - len)
3972 buf[len++] = '\n';
3973
3974 ofs += 16;
3975 size -= min(size, 16U);
3976 }
3977
3978 return len;
3979}
3980
3981static ssize_t store_measurement(struct device *d,
3982 struct device_attribute *attr,
3983 const char *buf, size_t count)
3984{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003985 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003986 struct ieee80211_measurement_params params = {
3987 .channel = le16_to_cpu(priv->active_rxon.channel),
3988 .start_time = cpu_to_le64(priv->last_tsf),
3989 .duration = cpu_to_le16(1),
3990 };
3991 u8 type = IWL_MEASURE_BASIC;
3992 u8 buffer[32];
3993 u8 channel;
3994
3995 if (count) {
3996 char *p = buffer;
3997 strncpy(buffer, buf, min(sizeof(buffer), count));
3998 channel = simple_strtoul(p, NULL, 0);
3999 if (channel)
4000 params.channel = channel;
4001
4002 p = buffer;
4003 while (*p && *p != ' ')
4004 p++;
4005 if (*p)
4006 type = simple_strtoul(p + 1, NULL, 0);
4007 }
4008
4009 IWL_DEBUG_INFO("Invoking measurement of type %d on "
4010 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004011 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07004012
4013 return count;
4014}
4015
4016static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
4017 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004018#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07004019
4020static ssize_t store_retry_rate(struct device *d,
4021 struct device_attribute *attr,
4022 const char *buf, size_t count)
4023{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004024 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004025
4026 priv->retry_rate = simple_strtoul(buf, NULL, 0);
4027 if (priv->retry_rate <= 0)
4028 priv->retry_rate = 1;
4029
4030 return count;
4031}
4032
4033static ssize_t show_retry_rate(struct device *d,
4034 struct device_attribute *attr, char *buf)
4035{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004036 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004037 return sprintf(buf, "%d", priv->retry_rate);
4038}
4039
4040static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
4041 store_retry_rate);
4042
4043static ssize_t store_power_level(struct device *d,
4044 struct device_attribute *attr,
4045 const char *buf, size_t count)
4046{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004047 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004048 int rc;
4049 int mode;
4050
4051 mode = simple_strtoul(buf, NULL, 0);
4052 mutex_lock(&priv->mutex);
4053
Tomas Winklerfee12472008-04-03 16:05:21 -07004054 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004055 rc = -EAGAIN;
4056 goto out;
4057 }
4058
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004059 rc = iwl_power_set_user_mode(priv, mode);
4060 if (rc) {
4061 IWL_DEBUG_MAC80211("failed setting power mode.\n");
4062 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004063 }
Zhu Yib481de92007-09-25 17:54:57 -07004064 rc = count;
4065
4066 out:
4067 mutex_unlock(&priv->mutex);
4068 return rc;
4069}
4070
4071#define MAX_WX_STRING 80
4072
4073/* Values are in microsecond */
4074static const s32 timeout_duration[] = {
4075 350000,
4076 250000,
4077 75000,
4078 37000,
4079 25000,
4080};
4081static const s32 period_duration[] = {
4082 400000,
4083 700000,
4084 1000000,
4085 1000000,
4086 1000000
4087};
4088
4089static ssize_t show_power_level(struct device *d,
4090 struct device_attribute *attr, char *buf)
4091{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004092 struct iwl_priv *priv = dev_get_drvdata(d);
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004093 int level = priv->power_data.power_mode;
Zhu Yib481de92007-09-25 17:54:57 -07004094 char *p = buf;
4095
4096 p += sprintf(p, "%d ", level);
4097 switch (level) {
4098 case IWL_POWER_MODE_CAM:
4099 case IWL_POWER_AC:
4100 p += sprintf(p, "(AC)");
4101 break;
4102 case IWL_POWER_BATTERY:
4103 p += sprintf(p, "(BATTERY)");
4104 break;
4105 default:
4106 p += sprintf(p,
4107 "(Timeout %dms, Period %dms)",
4108 timeout_duration[level - 1] / 1000,
4109 period_duration[level - 1] / 1000);
4110 }
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004111/*
Zhu Yib481de92007-09-25 17:54:57 -07004112 if (!(priv->power_mode & IWL_POWER_ENABLED))
4113 p += sprintf(p, " OFF\n");
4114 else
4115 p += sprintf(p, " \n");
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004116*/
4117 p += sprintf(p, " \n");
Zhu Yib481de92007-09-25 17:54:57 -07004118 return (p - buf + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004119}
4120
4121static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
4122 store_power_level);
4123
4124static ssize_t show_channels(struct device *d,
4125 struct device_attribute *attr, char *buf)
4126{
Johannes Berg8318d782008-01-24 19:38:38 +01004127 /* all this shit doesn't belong into sysfs anyway */
4128 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07004129}
4130
4131static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
4132
4133static ssize_t show_statistics(struct device *d,
4134 struct device_attribute *attr, char *buf)
4135{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004136 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004137 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07004138 u32 len = 0, ofs = 0;
4139 u8 *data = (u8 *) & priv->statistics;
4140 int rc = 0;
4141
Tomas Winklerfee12472008-04-03 16:05:21 -07004142 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004143 return -EAGAIN;
4144
4145 mutex_lock(&priv->mutex);
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07004146 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004147 mutex_unlock(&priv->mutex);
4148
4149 if (rc) {
4150 len = sprintf(buf,
4151 "Error sending statistics request: 0x%08X\n", rc);
4152 return len;
4153 }
4154
4155 while (size && (PAGE_SIZE - len)) {
4156 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4157 PAGE_SIZE - len, 1);
4158 len = strlen(buf);
4159 if (PAGE_SIZE - len)
4160 buf[len++] = '\n';
4161
4162 ofs += 16;
4163 size -= min(size, 16U);
4164 }
4165
4166 return len;
4167}
4168
4169static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
4170
Zhu Yib481de92007-09-25 17:54:57 -07004171static ssize_t show_status(struct device *d,
4172 struct device_attribute *attr, char *buf)
4173{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004174 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07004175 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004176 return -EAGAIN;
4177 return sprintf(buf, "0x%08x\n", (int)priv->status);
4178}
4179
4180static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
4181
Zhu Yib481de92007-09-25 17:54:57 -07004182/*****************************************************************************
4183 *
4184 * driver setup and teardown
4185 *
4186 *****************************************************************************/
4187
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004188static void iwl_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004189{
4190 priv->workqueue = create_workqueue(DRV_NAME);
4191
4192 init_waitqueue_head(&priv->wait_command_queue);
4193
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004194 INIT_WORK(&priv->up, iwl4965_bg_up);
4195 INIT_WORK(&priv->restart, iwl4965_bg_restart);
4196 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004197 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
4198 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08004199 INIT_WORK(&priv->set_monitor, iwl4965_bg_set_monitor);
Emmanuel Grumbach16e727e2008-06-12 09:46:52 +08004200 INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004201 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08004202 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
4203 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
Tomas Winkler2a421b92008-06-12 09:47:10 +08004204
4205 /* FIXME : remove when resolved PENDING */
4206 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
4207 iwl_setup_scan_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004208
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004209 if (priv->cfg->ops->lib->setup_deferred_work)
4210 priv->cfg->ops->lib->setup_deferred_work(priv);
4211
4212 init_timer(&priv->statistics_periodic);
4213 priv->statistics_periodic.data = (unsigned long)priv;
4214 priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
Zhu Yib481de92007-09-25 17:54:57 -07004215
4216 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004217 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07004218}
4219
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004220static void iwl_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004221{
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004222 if (priv->cfg->ops->lib->cancel_deferred_work)
4223 priv->cfg->ops->lib->cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004224
Joonwoo Park3ae6a052007-11-29 10:43:16 +09004225 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07004226 cancel_delayed_work(&priv->scan_check);
4227 cancel_delayed_work(&priv->alive_start);
4228 cancel_delayed_work(&priv->post_associate);
4229 cancel_work_sync(&priv->beacon_update);
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004230 del_timer_sync(&priv->statistics_periodic);
Zhu Yib481de92007-09-25 17:54:57 -07004231}
4232
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004233static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07004234 &dev_attr_channels.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004235 &dev_attr_flags.attr,
4236 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004237#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07004238 &dev_attr_measurement.attr,
4239#endif
4240 &dev_attr_power_level.attr,
4241 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004242 &dev_attr_rs_window.attr,
4243 &dev_attr_statistics.attr,
4244 &dev_attr_status.attr,
4245 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004246 &dev_attr_tx_power.attr,
Ester Kummer8cf769c2008-05-06 11:05:11 +08004247#ifdef CONFIG_IWLWIFI_DEBUG
4248 &dev_attr_debug_level.attr,
4249#endif
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08004250 &dev_attr_version.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004251
4252 NULL
4253};
4254
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004255static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07004256 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004257 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07004258};
4259
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004260static struct ieee80211_ops iwl4965_hw_ops = {
4261 .tx = iwl4965_mac_tx,
4262 .start = iwl4965_mac_start,
4263 .stop = iwl4965_mac_stop,
4264 .add_interface = iwl4965_mac_add_interface,
4265 .remove_interface = iwl4965_mac_remove_interface,
4266 .config = iwl4965_mac_config,
4267 .config_interface = iwl4965_mac_config_interface,
4268 .configure_filter = iwl4965_configure_filter,
4269 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004270 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004271 .get_stats = iwl4965_mac_get_stats,
4272 .get_tx_stats = iwl4965_mac_get_tx_stats,
4273 .conf_tx = iwl4965_mac_conf_tx,
4274 .get_tsf = iwl4965_mac_get_tsf,
4275 .reset_tsf = iwl4965_mac_reset_tsf,
4276 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01004277 .bss_info_changed = iwl4965_bss_info_changed,
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02004278 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004279 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07004280};
4281
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004282static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07004283{
4284 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004285 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07004286 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08004287 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004288 unsigned long flags;
Zhu Yi5a66926a2008-01-14 17:46:18 -08004289 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07004290
Assaf Krauss316c30d2008-03-14 10:38:46 -07004291 /************************
4292 * 1. Allocating HW data
4293 ************************/
4294
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004295 /* Disabling hardware scan means that mac80211 will perform scans
4296 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07004297 if (cfg->mod_params->disable_hw_scan) {
Ester Kummerbf403db2008-05-05 10:22:40 +08004298 if (cfg->mod_params->debug & IWL_DL_INFO)
4299 dev_printk(KERN_DEBUG, &(pdev->dev),
4300 "Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004301 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07004302 }
4303
Assaf Krauss1d0a0822008-03-14 10:38:48 -07004304 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
4305 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07004306 err = -ENOMEM;
4307 goto out;
4308 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07004309 priv = hw->priv;
4310 /* At this point both hw and priv are allocated. */
4311
Zhu Yib481de92007-09-25 17:54:57 -07004312 SET_IEEE80211_DEV(hw, &pdev->dev);
4313
4314 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08004315 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07004316 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07004317
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004318#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08004319 priv->debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07004320 atomic_set(&priv->restrict_refcnt, 0);
4321#endif
Zhu Yib481de92007-09-25 17:54:57 -07004322
Assaf Krauss316c30d2008-03-14 10:38:46 -07004323 /**************************
4324 * 2. Initializing PCI bus
4325 **************************/
4326 if (pci_enable_device(pdev)) {
4327 err = -ENODEV;
4328 goto out_ieee80211_free_hw;
4329 }
4330
4331 pci_set_master(pdev);
4332
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07004333 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004334 if (!err)
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07004335 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
4336 if (err) {
4337 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
4338 if (!err)
4339 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
4340 /* both attempts failed: */
Assaf Krauss316c30d2008-03-14 10:38:46 -07004341 if (err) {
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07004342 printk(KERN_WARNING "%s: No suitable DMA available.\n",
4343 DRV_NAME);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004344 goto out_pci_disable_device;
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07004345 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07004346 }
4347
4348 err = pci_request_regions(pdev, DRV_NAME);
4349 if (err)
4350 goto out_pci_disable_device;
4351
4352 pci_set_drvdata(pdev, priv);
4353
4354 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4355 * PCI Tx retries from interfering with C3 CPU state */
4356 pci_write_config_byte(pdev, 0x41, 0x00);
4357
4358 /***********************
4359 * 3. Read REV register
4360 ***********************/
4361 priv->hw_base = pci_iomap(pdev, 0, 0);
4362 if (!priv->hw_base) {
4363 err = -ENODEV;
4364 goto out_pci_release_regions;
4365 }
4366
4367 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
4368 (unsigned long long) pci_resource_len(pdev, 0));
4369 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
4370
Tomas Winklerb661c812008-04-23 17:14:54 -07004371 iwl_hw_detect(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004372 printk(KERN_INFO DRV_NAME
Tomas Winklerb661c812008-04-23 17:14:54 -07004373 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
4374 priv->cfg->name, priv->hw_rev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004375
Tomas Winkler91238712008-04-23 17:14:53 -07004376 /* amp init */
4377 err = priv->cfg->ops->lib->apm_ops.init(priv);
4378 if (err < 0) {
4379 IWL_DEBUG_INFO("Failed to init APMG\n");
4380 goto out_iounmap;
4381 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07004382 /*****************
4383 * 4. Read EEPROM
4384 *****************/
Assaf Krauss316c30d2008-03-14 10:38:46 -07004385 /* Read the EEPROM */
4386 err = iwl_eeprom_init(priv);
4387 if (err) {
4388 IWL_ERROR("Unable to init EEPROM\n");
4389 goto out_iounmap;
4390 }
Tomas Winkler8614f362008-04-23 17:14:55 -07004391 err = iwl_eeprom_check_version(priv);
4392 if (err)
4393 goto out_iounmap;
4394
Ron Rindjunsky02883012008-05-15 13:53:53 +08004395 /* extract MAC Address */
Assaf Krauss316c30d2008-03-14 10:38:46 -07004396 iwl_eeprom_get_mac(priv, priv->mac_addr);
4397 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
4398 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
4399
4400 /************************
4401 * 5. Setup HW constants
4402 ************************/
4403 /* Device-specific setup */
Tomas Winkler5425e492008-04-15 16:01:38 -07004404 if (priv->cfg->ops->lib->set_hw_params(priv)) {
4405 IWL_ERROR("failed to set hw parameters\n");
Tomas Winkler073d3f52008-04-21 15:41:52 -07004406 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07004407 }
4408
4409 /*******************
Tomas Winkler6ba87952008-05-15 13:54:17 +08004410 * 6. Setup priv
Assaf Krauss316c30d2008-03-14 10:38:46 -07004411 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07004412
Tomas Winkler6ba87952008-05-15 13:54:17 +08004413 err = iwl_init_drv(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004414 if (err)
Ron Rindjunsky399f4902008-04-23 17:14:56 -07004415 goto out_free_eeprom;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004416 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07004417
4418 /**********************************
4419 * 7. Initialize module parameters
4420 **********************************/
4421
4422 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07004423 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07004424 set_bit(STATUS_RF_KILL_SW, &priv->status);
4425 IWL_DEBUG_INFO("Radio disabled.\n");
4426 }
4427
Assaf Krauss316c30d2008-03-14 10:38:46 -07004428 /********************
4429 * 8. Setup services
4430 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004431 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004432 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004433 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004434
4435 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
4436 if (err) {
4437 IWL_ERROR("failed to create sysfs device attributes\n");
Tomas Winkler6ba87952008-05-15 13:54:17 +08004438 goto out_uninit_drv;
Assaf Krauss316c30d2008-03-14 10:38:46 -07004439 }
4440
Assaf Krauss316c30d2008-03-14 10:38:46 -07004441
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004442 iwl_setup_deferred_work(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004443 iwl4965_setup_rx_handlers(priv);
4444
4445 /********************
4446 * 9. Conclude
4447 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08004448 pci_save_state(pdev);
4449 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004450
Tomas Winkler6ba87952008-05-15 13:54:17 +08004451 /**********************************
4452 * 10. Setup and register mac80211
4453 **********************************/
4454
4455 err = iwl_setup_mac(priv);
4456 if (err)
4457 goto out_remove_sysfs;
4458
4459 err = iwl_dbgfs_register(priv, DRV_NAME);
4460 if (err)
4461 IWL_ERROR("failed to create debugfs files\n");
4462
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07004463 /* notify iwlcore to init */
4464 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07004465 return 0;
4466
Assaf Krauss316c30d2008-03-14 10:38:46 -07004467 out_remove_sysfs:
4468 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Tomas Winkler6ba87952008-05-15 13:54:17 +08004469 out_uninit_drv:
4470 iwl_uninit_drv(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07004471 out_free_eeprom:
4472 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004473 out_iounmap:
4474 pci_iounmap(pdev, priv->hw_base);
4475 out_pci_release_regions:
4476 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004477 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07004478 out_pci_disable_device:
4479 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004480 out_ieee80211_free_hw:
4481 ieee80211_free_hw(priv->hw);
4482 out:
4483 return err;
4484}
4485
Reinette Chatrec83dbf62008-03-21 13:53:41 -07004486static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004487{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004488 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004489 struct list_head *p, *q;
4490 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004491 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004492
4493 if (!priv)
4494 return;
4495
4496 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
4497
Emmanuel Grumbach67249622008-05-29 16:35:26 +08004498 iwl_dbgfs_unregister(priv);
4499 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
4500
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07004501 if (priv->mac80211_registered) {
4502 ieee80211_unregister_hw(priv->hw);
4503 priv->mac80211_registered = 0;
4504 }
4505
Zhu Yib481de92007-09-25 17:54:57 -07004506 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08004507
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004508 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004509
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004510 /* make sure we flush any pending irq or
4511 * tasklet for the driver
4512 */
4513 spin_lock_irqsave(&priv->lock, flags);
4514 iwl4965_disable_interrupts(priv);
4515 spin_unlock_irqrestore(&priv->lock, flags);
4516
4517 iwl_synchronize_irq(priv);
4518
Zhu Yib481de92007-09-25 17:54:57 -07004519 /* Free MAC hash list for ADHOC */
4520 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
4521 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
4522 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004523 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07004524 }
4525 }
4526
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07004527 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07004528
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004529 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004530
4531 if (priv->rxq.bd)
Tomas Winklera55360e2008-05-05 10:22:28 +08004532 iwl_rx_queue_free(priv, &priv->rxq);
Ron Rindjunsky1053d352008-05-05 10:22:43 +08004533 iwl_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004534
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004535 iwlcore_clear_stations_table(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07004536 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004537
Zhu Yib481de92007-09-25 17:54:57 -07004538
Mohamed Abbas948c1712007-10-25 17:15:45 +08004539 /*netif_stop_queue(dev); */
4540 flush_workqueue(priv->workqueue);
4541
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004542 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07004543 * priv->workqueue... so we can't take down the workqueue
4544 * until now... */
4545 destroy_workqueue(priv->workqueue);
4546 priv->workqueue = NULL;
4547
Zhu Yib481de92007-09-25 17:54:57 -07004548 pci_iounmap(pdev, priv->hw_base);
4549 pci_release_regions(pdev);
4550 pci_disable_device(pdev);
4551 pci_set_drvdata(pdev, NULL);
4552
Tomas Winkler6ba87952008-05-15 13:54:17 +08004553 iwl_uninit_drv(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004554
4555 if (priv->ibss_beacon)
4556 dev_kfree_skb(priv->ibss_beacon);
4557
4558 ieee80211_free_hw(priv->hw);
4559}
4560
4561#ifdef CONFIG_PM
4562
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004563static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07004564{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004565 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004566
Zhu Yie655b9f2008-01-24 02:19:38 -08004567 if (priv->is_open) {
4568 set_bit(STATUS_IN_SUSPEND, &priv->status);
4569 iwl4965_mac_stop(priv->hw);
4570 priv->is_open = 1;
4571 }
Zhu Yib481de92007-09-25 17:54:57 -07004572
Zhu Yib481de92007-09-25 17:54:57 -07004573 pci_set_power_state(pdev, PCI_D3hot);
4574
Zhu Yib481de92007-09-25 17:54:57 -07004575 return 0;
4576}
4577
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004578static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004579{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004580 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004581
Zhu Yib481de92007-09-25 17:54:57 -07004582 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07004583
Zhu Yie655b9f2008-01-24 02:19:38 -08004584 if (priv->is_open)
4585 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004586
Zhu Yie655b9f2008-01-24 02:19:38 -08004587 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004588 return 0;
4589}
4590
4591#endif /* CONFIG_PM */
4592
4593/*****************************************************************************
4594 *
4595 * driver and module entry point
4596 *
4597 *****************************************************************************/
4598
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004599/* Hardware specific file defines the PCI IDs table for that hardware module */
4600static struct pci_device_id iwl_hw_card_ids[] = {
4601 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
4602 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
Tomas Winkler5a6a2562008-04-24 11:55:23 -07004603#ifdef CONFIG_IWL5000
4604 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
4605 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
4606 {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
4607#endif /* CONFIG_IWL5000 */
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004608 {0}
4609};
4610MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
4611
4612static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07004613 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004614 .id_table = iwl_hw_card_ids,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004615 .probe = iwl4965_pci_probe,
4616 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07004617#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004618 .suspend = iwl4965_pci_suspend,
4619 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07004620#endif
4621};
4622
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004623static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07004624{
4625
4626 int ret;
4627 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
4628 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004629
4630 ret = iwl4965_rate_control_register();
4631 if (ret) {
4632 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
4633 return ret;
4634 }
4635
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004636 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07004637 if (ret) {
4638 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004639 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07004640 }
Zhu Yib481de92007-09-25 17:54:57 -07004641
4642 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004643
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004644error_register:
4645 iwl4965_rate_control_unregister();
4646 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004647}
4648
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004649static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07004650{
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004651 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004652 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07004653}
4654
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004655module_exit(iwl4965_exit);
4656module_init(iwl4965_init);