blob: a2dffe401c78007e3b459cda4f07773e077d5853 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatreeb7ae892008-03-11 16:17:17 -07003 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
Zhu Yib481de92007-09-25 17:54:57 -070030#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/version.h>
33#include <linux/init.h>
34#include <linux/pci.h>
35#include <linux/dma-mapping.h>
36#include <linux/delay.h>
37#include <linux/skbuff.h>
38#include <linux/netdevice.h>
39#include <linux/wireless.h>
40#include <linux/firmware.h>
Zhu Yib481de92007-09-25 17:54:57 -070041#include <linux/etherdevice.h>
42#include <linux/if_arp.h>
43
Zhu Yib481de92007-09-25 17:54:57 -070044#include <net/mac80211.h>
45
46#include <asm/div64.h>
47
Assaf Krauss6bc913b2008-03-11 16:17:18 -070048#include "iwl-eeprom.h"
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070049#include "iwl-dev.h"
Tomas Winklerfee12472008-04-03 16:05:21 -070050#include "iwl-core.h"
Tomas Winkler3395f6e2008-03-25 16:33:37 -070051#include "iwl-io.h"
Zhu Yib481de92007-09-25 17:54:57 -070052#include "iwl-helpers.h"
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070053#include "iwl-sta.h"
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070054#include "iwl-calib.h"
Zhu Yib481de92007-09-25 17:54:57 -070055
Christoph Hellwig416e1432007-10-25 17:15:49 +080056
Zhu Yib481de92007-09-25 17:54:57 -070057/******************************************************************************
58 *
59 * module boiler plate
60 *
61 ******************************************************************************/
62
Zhu Yib481de92007-09-25 17:54:57 -070063/*
64 * module name, copyright, version, etc.
65 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
66 */
67
68#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
69
Tomas Winkler0a6857e2008-03-12 16:58:49 -070070#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070071#define VD "d"
72#else
73#define VD
74#endif
75
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080076#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070077#define VS "s"
78#else
79#define VS
80#endif
81
Tomas Winklerdf48c322008-03-06 10:40:19 -080082#define DRV_VERSION IWLWIFI_VERSION VD VS
Zhu Yib481de92007-09-25 17:54:57 -070083
Zhu Yib481de92007-09-25 17:54:57 -070084
85MODULE_DESCRIPTION(DRV_DESCRIPTION);
86MODULE_VERSION(DRV_VERSION);
87MODULE_AUTHOR(DRV_COPYRIGHT);
88MODULE_LICENSE("GPL");
89
Zhu Yib481de92007-09-25 17:54:57 -070090/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +080091 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -070092 * the functionality provided here
93 */
94
95/**************************************************************/
96
Zhu Yib481de92007-09-25 17:54:57 -070097
Zhu Yib481de92007-09-25 17:54:57 -070098
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -070099static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
100{
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800101 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700102
103 if (hw_decrypt)
104 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
105 else
106 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
107
108}
109
Zhu Yib481de92007-09-25 17:54:57 -0700110/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800111 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700112 *
113 * NOTE: This is really only useful during development and can eventually
114 * be #ifdef'd out once the driver is stable and folks aren't actively
115 * making changes
116 */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800117static int iwl4965_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700118{
119 int error = 0;
120 int counter = 1;
121
122 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
123 error |= le32_to_cpu(rxon->flags &
124 (RXON_FLG_TGJ_NARROW_BAND_MSK |
125 RXON_FLG_RADAR_DETECT_MSK));
126 if (error)
127 IWL_WARNING("check 24G fields %d | %d\n",
128 counter++, error);
129 } else {
130 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
131 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
132 if (error)
133 IWL_WARNING("check 52 fields %d | %d\n",
134 counter++, error);
135 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
136 if (error)
137 IWL_WARNING("check 52 CCK %d | %d\n",
138 counter++, error);
139 }
140 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
141 if (error)
142 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
143
144 /* make sure basic rates 6Mbps and 1Mbps are supported */
145 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
146 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
147 if (error)
148 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
149
150 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
151 if (error)
152 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
153
154 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
155 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
156 if (error)
157 IWL_WARNING("check CCK and short slot %d | %d\n",
158 counter++, error);
159
160 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
161 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
162 if (error)
163 IWL_WARNING("check CCK & auto detect %d | %d\n",
164 counter++, error);
165
166 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
167 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
168 if (error)
169 IWL_WARNING("check TGG and auto detect %d | %d\n",
170 counter++, error);
171
172 if (error)
173 IWL_WARNING("Tuning to channel %d\n",
174 le16_to_cpu(rxon->channel));
175
176 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800177 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700178 return -1;
179 }
180 return 0;
181}
182
183/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800184 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800185 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700186 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800187 * If the RXON structure is changing enough to require a new tune,
188 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
189 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700190 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700191static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700192{
193
194 /* These items are only settable from the full RXON command */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +0800195 if (!(iwl_is_associated(priv)) ||
Zhu Yib481de92007-09-25 17:54:57 -0700196 compare_ether_addr(priv->staging_rxon.bssid_addr,
197 priv->active_rxon.bssid_addr) ||
198 compare_ether_addr(priv->staging_rxon.node_addr,
199 priv->active_rxon.node_addr) ||
200 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
201 priv->active_rxon.wlap_bssid_addr) ||
202 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
203 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
204 (priv->staging_rxon.air_propagation !=
205 priv->active_rxon.air_propagation) ||
206 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
207 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
208 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
209 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
210 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
211 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
212 return 1;
213
214 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
215 * be updated with the RXON_ASSOC command -- however only some
216 * flag transitions are allowed using RXON_ASSOC */
217
218 /* Check if we are not switching bands */
219 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
220 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
221 return 1;
222
223 /* Check if we are switching association toggle */
224 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
225 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
226 return 1;
227
228 return 0;
229}
230
Zhu Yib481de92007-09-25 17:54:57 -0700231/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800232 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700233 *
Ian Schram01ebd062007-10-25 17:15:22 +0800234 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700235 * the active_rxon structure is updated with the new data. This
236 * function correctly transitions out of the RXON_ASSOC_MSK state if
237 * a HW tune is required based on the RXON structure changes.
238 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700239static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700240{
241 /* cast away the const for active_rxon in this function */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800242 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -0700243 DECLARE_MAC_BUF(mac);
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
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700318 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800319
Zhu Yib481de92007-09-25 17:54:57 -0700320 if (!priv->error_recovering)
321 priv->start_calib = 0;
322
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700323 iwl_init_sensitivity(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700324
325 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
326
327 /* If we issue a new RXON command which required a tune then we must
328 * send a new TXPOWER command or we won't be able to Tx any frames */
Tomas Winkler630fe9b2008-06-12 09:47:08 +0800329 rc = iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
Zhu Yib481de92007-09-25 17:54:57 -0700330 if (rc) {
Tomas Winkler630fe9b2008-06-12 09:47:08 +0800331 IWL_ERROR("Error sending TX power (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700332 return rc;
333 }
334
335 /* Add the broadcast address so we can send broadcast frames */
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800336 if (iwl_rxon_add_station(priv, iwl_bcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700337 IWL_INVALID_STATION) {
338 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
339 return -EIO;
340 }
341
342 /* If we have set the ASSOC_MSK and we are in BSS mode then
343 * add the IWL_AP_ID to the station rate table */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700344 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700345 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800346 if (iwl_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700347 == IWL_INVALID_STATION) {
348 IWL_ERROR("Error adding AP address for transmit.\n");
349 return -EIO;
350 }
351 priv->assoc_station_added = 1;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700352 if (priv->default_wep_key &&
353 iwl_send_static_wepkey_cmd(priv, 0))
354 IWL_ERROR("Could not send WEP static key.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700355 }
356
357 return 0;
358}
359
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700360void iwl4965_update_chain_flags(struct iwl_priv *priv)
361{
362
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -0700363 iwl_set_rxon_chain(priv);
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700364 iwl4965_commit_rxon(priv);
365}
366
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700367static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700368{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800369 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700370 .flags = 3,
371 .lead_time = 0xAA,
372 .max_kill = 1,
373 .kill_ack_mask = 0,
374 .kill_cts_mask = 0,
375 };
376
Tomas Winkler857485c2008-03-21 13:53:44 -0700377 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800378 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700379}
380
Tomas Winklerfcab4232008-05-15 13:54:01 +0800381static void iwl_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700382{
383 struct list_head *element;
384
385 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
386 priv->frames_count);
387
388 while (!list_empty(&priv->free_frames)) {
389 element = priv->free_frames.next;
390 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800391 kfree(list_entry(element, struct iwl_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700392 priv->frames_count--;
393 }
394
395 if (priv->frames_count) {
396 IWL_WARNING("%d frames still in use. Did we lose one?\n",
397 priv->frames_count);
398 priv->frames_count = 0;
399 }
400}
401
Tomas Winklerfcab4232008-05-15 13:54:01 +0800402static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700403{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800404 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700405 struct list_head *element;
406 if (list_empty(&priv->free_frames)) {
407 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
408 if (!frame) {
409 IWL_ERROR("Could not allocate frame!\n");
410 return NULL;
411 }
412
413 priv->frames_count++;
414 return frame;
415 }
416
417 element = priv->free_frames.next;
418 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800419 return list_entry(element, struct iwl_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700420}
421
Tomas Winklerfcab4232008-05-15 13:54:01 +0800422static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700423{
424 memset(frame, 0, sizeof(*frame));
425 list_add(&frame->list, &priv->free_frames);
426}
427
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700428unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700429 struct ieee80211_hdr *hdr,
430 const u8 *dest, int left)
431{
432
Tomas Winkler3109ece2008-03-28 16:33:35 -0700433 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -0700434 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
435 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
436 return 0;
437
438 if (priv->ibss_beacon->len > left)
439 return 0;
440
441 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
442
443 return priv->ibss_beacon->len;
444}
445
Guy Cohen39e88502008-04-23 17:14:57 -0700446static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700447{
Guy Cohen39e88502008-04-23 17:14:57 -0700448 int i;
449 int rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -0700450
Guy Cohen39e88502008-04-23 17:14:57 -0700451 /* Set rate mask*/
452 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
453 rate_mask = priv->active_rate_basic & 0xF;
454 else
455 rate_mask = priv->active_rate_basic & 0xFF0;
456
457 /* Find lowest valid rate */
Zhu Yib481de92007-09-25 17:54:57 -0700458 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800459 i = iwl_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -0700460 if (rate_mask & (1 << i))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800461 return iwl_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700462 }
463
Guy Cohen39e88502008-04-23 17:14:57 -0700464 /* No valid rate was found. Assign the lowest one */
465 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
466 return IWL_RATE_1M_PLCP;
467 else
468 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -0700469}
470
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700471static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700472{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800473 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700474 unsigned int frame_size;
475 int rc;
476 u8 rate;
477
Tomas Winklerfcab4232008-05-15 13:54:01 +0800478 frame = iwl_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700479
480 if (!frame) {
481 IWL_ERROR("Could not obtain free frame buffer for beacon "
482 "command.\n");
483 return -ENOMEM;
484 }
485
Guy Cohen39e88502008-04-23 17:14:57 -0700486 rate = iwl4965_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700487
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800488 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700489
Tomas Winkler857485c2008-03-21 13:53:44 -0700490 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700491 &frame->u.cmd[0]);
492
Tomas Winklerfcab4232008-05-15 13:54:01 +0800493 iwl_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700494
495 return rc;
496}
497
498/******************************************************************************
499 *
Zhu Yib481de92007-09-25 17:54:57 -0700500 * Misc. internal state and helper functions
501 *
502 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -0700503
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700504static void iwl4965_ht_conf(struct iwl_priv *priv,
505 struct ieee80211_bss_conf *bss_conf)
506{
507 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
508 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
509 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
510
511 IWL_DEBUG_MAC80211("enter: \n");
512
513 iwl_conf->is_ht = bss_conf->assoc_ht;
514
515 if (!iwl_conf->is_ht)
516 return;
517
518 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
519
520 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
Emmanuel Grumbacha9841012008-05-15 13:54:08 +0800521 iwl_conf->sgf |= HT_SHORT_GI_20MHZ;
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700522 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
Emmanuel Grumbacha9841012008-05-15 13:54:08 +0800523 iwl_conf->sgf |= HT_SHORT_GI_40MHZ;
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700524
525 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
526 iwl_conf->max_amsdu_size =
527 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
528
529 iwl_conf->supported_chan_width =
530 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
531 iwl_conf->extension_chan_offset =
532 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
533 /* If no above or below channel supplied disable FAT channel */
Emmanuel Grumbach963f5512008-06-12 09:47:00 +0800534 if (iwl_conf->extension_chan_offset != IEEE80211_HT_IE_CHA_SEC_ABOVE &&
535 iwl_conf->extension_chan_offset != IEEE80211_HT_IE_CHA_SEC_BELOW) {
536 iwl_conf->extension_chan_offset = IEEE80211_HT_IE_CHA_SEC_NONE;
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700537 iwl_conf->supported_chan_width = 0;
Emmanuel Grumbach963f5512008-06-12 09:47:00 +0800538 }
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700539
540 iwl_conf->tx_mimo_ps_mode =
541 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
542 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
543
544 iwl_conf->control_channel = ht_bss_conf->primary_channel;
545 iwl_conf->tx_chan_width =
546 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
547 iwl_conf->ht_protection =
548 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
549 iwl_conf->non_GF_STA_present =
550 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
551
552 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
553 IWL_DEBUG_MAC80211("leave\n");
554}
555
Zhu Yib481de92007-09-25 17:54:57 -0700556/*
557 * QoS support
558*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700559static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800560 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -0700561{
562
Tomas Winkler857485c2008-03-21 13:53:44 -0700563 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800564 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -0700565}
566
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700567static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -0700568{
569 unsigned long flags;
570
Zhu Yib481de92007-09-25 17:54:57 -0700571 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
572 return;
573
574 if (!priv->qos_data.qos_enable)
575 return;
576
577 spin_lock_irqsave(&priv->lock, flags);
578 priv->qos_data.def_qos_parm.qos_flags = 0;
579
580 if (priv->qos_data.qos_cap.q_AP.queue_request &&
581 !priv->qos_data.qos_cap.q_AP.txop_request)
582 priv->qos_data.def_qos_parm.qos_flags |=
583 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700584 if (priv->qos_data.qos_active)
585 priv->qos_data.def_qos_parm.qos_flags |=
586 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
587
Ron Rindjunskyfd105e72007-11-26 16:14:39 +0200588 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800589 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800590
Zhu Yib481de92007-09-25 17:54:57 -0700591 spin_unlock_irqrestore(&priv->lock, flags);
592
Tomas Winkler3109ece2008-03-28 16:33:35 -0700593 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800594 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
595 priv->qos_data.qos_active,
596 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700597
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800598 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -0700599 &(priv->qos_data.def_qos_parm));
600 }
601}
602
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700603int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -0700604{
605 /* Filter incoming packets to determine if they are targeted toward
606 * this network, discarding packets coming from ourselves */
607 switch (priv->iw_mode) {
608 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
609 /* packets from our adapter are dropped (echo) */
610 if (!compare_ether_addr(header->addr2, priv->mac_addr))
611 return 0;
612 /* {broad,multi}cast packets to our IBSS go through */
613 if (is_multicast_ether_addr(header->addr1))
614 return !compare_ether_addr(header->addr3, priv->bssid);
615 /* packets to our adapter go through */
616 return !compare_ether_addr(header->addr1, priv->mac_addr);
617 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
618 /* packets from our adapter are dropped (echo) */
619 if (!compare_ether_addr(header->addr3, priv->mac_addr))
620 return 0;
621 /* {broad,multi}cast packets to our BSS go through */
622 if (is_multicast_ether_addr(header->addr1))
623 return !compare_ether_addr(header->addr2, priv->bssid);
624 /* packets to our adapter go through */
625 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -0700626 default:
627 break;
Zhu Yib481de92007-09-25 17:54:57 -0700628 }
629
630 return 1;
631}
632
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700633static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700634{
635 /* Reset ieee stats */
636
637 /* We don't reset the net_device_stats (ieee->stats) on
638 * re-association */
639
640 priv->last_seq_num = -1;
641 priv->last_frag_num = -1;
642 priv->last_packet_time = 0;
643
Tomas Winkler2a421b92008-06-12 09:47:10 +0800644 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700645}
646
647#define MAX_UCODE_BEACON_INTERVAL 4096
648#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
649
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800650static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -0700651{
652 u16 new_val = 0;
653 u16 beacon_factor = 0;
654
655 beacon_factor =
656 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
657 / MAX_UCODE_BEACON_INTERVAL;
658 new_val = beacon_val / beacon_factor;
659
660 return cpu_to_le16(new_val);
661}
662
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700663static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700664{
665 u64 interval_tm_unit;
666 u64 tsf, result;
667 unsigned long flags;
668 struct ieee80211_conf *conf = NULL;
669 u16 beacon_int = 0;
670
671 conf = ieee80211_get_hw_conf(priv->hw);
672
673 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -0700674 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
675 priv->rxon_timing.timestamp.dw[0] =
676 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -0700677
678 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
679
Tomas Winkler3109ece2008-03-28 16:33:35 -0700680 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -0700681
682 beacon_int = priv->beacon_int;
683 spin_unlock_irqrestore(&priv->lock, flags);
684
685 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
686 if (beacon_int == 0) {
687 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
688 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
689 } else {
690 priv->rxon_timing.beacon_interval =
691 cpu_to_le16(beacon_int);
692 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800693 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -0700694 le16_to_cpu(priv->rxon_timing.beacon_interval));
695 }
696
697 priv->rxon_timing.atim_window = 0;
698 } else {
699 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800700 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -0700701 /* TODO: we need to get atim_window from upper stack
702 * for now we set to 0 */
703 priv->rxon_timing.atim_window = 0;
704 }
705
706 interval_tm_unit =
707 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
708 result = do_div(tsf, interval_tm_unit);
709 priv->rxon_timing.beacon_init_val =
710 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
711
712 IWL_DEBUG_ASSOC
713 ("beacon interval %d beacon timer %d beacon tim %d\n",
714 le16_to_cpu(priv->rxon_timing.beacon_interval),
715 le32_to_cpu(priv->rxon_timing.beacon_init_val),
716 le16_to_cpu(priv->rxon_timing.atim_window));
717}
718
Tomas Winkler82a66bb2008-05-29 16:35:28 +0800719static void iwl_set_flags_for_band(struct iwl_priv *priv,
720 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700721{
Johannes Berg8318d782008-01-24 19:38:38 +0100722 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -0700723 priv->staging_rxon.flags &=
724 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
725 | RXON_FLG_CCK_MSK);
726 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
727 } else {
Reinette Chatre508e32e2008-04-14 21:16:13 -0700728 /* Copied from iwl4965_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -0700729 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
730 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
731 else
732 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
733
734 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
735 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
736
737 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
738 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
739 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
740 }
741}
742
743/*
Ian Schram01ebd062007-10-25 17:15:22 +0800744 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -0700745 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700746static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700747{
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700748 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -0700749
750 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
751
752 switch (priv->iw_mode) {
753 case IEEE80211_IF_TYPE_AP:
754 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
755 break;
756
757 case IEEE80211_IF_TYPE_STA:
758 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
759 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
760 break;
761
762 case IEEE80211_IF_TYPE_IBSS:
763 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
764 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
765 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
766 RXON_FILTER_ACCEPT_GRP_MSK;
767 break;
768
769 case IEEE80211_IF_TYPE_MNTR:
770 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
771 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
772 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
773 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -0700774 default:
775 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
776 break;
Zhu Yib481de92007-09-25 17:54:57 -0700777 }
778
779#if 0
780 /* TODO: Figure out when short_preamble would be set and cache from
781 * that */
782 if (!hw_to_local(priv->hw)->short_preamble)
783 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
784 else
785 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
786#endif
787
Assaf Krauss8622e702008-03-21 13:53:43 -0700788 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700789 le16_to_cpu(priv->staging_rxon.channel));
790
791 if (!ch_info)
792 ch_info = &priv->channel_info[0];
793
794 /*
795 * in some case A channels are all non IBSS
796 * in this case force B/G channel
797 */
798 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
799 !(is_channel_ibss(ch_info)))
800 ch_info = &priv->channel_info[0];
801
802 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100803 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -0700804
Tomas Winkler82a66bb2008-05-29 16:35:28 +0800805 iwl_set_flags_for_band(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -0700806
807 priv->staging_rxon.ofdm_basic_rates =
808 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
809 priv->staging_rxon.cck_basic_rates =
810 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
811
812 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
813 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
814 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
815 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
816 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
817 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -0700818 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700819}
820
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700821static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -0700822{
Zhu Yib481de92007-09-25 17:54:57 -0700823 priv->iw_mode = mode;
824
Assaf Krauss398f9e72008-06-12 09:47:06 +0800825 /* init channel/phymode to values given at driver init */
826 iwl_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
827
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800828 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700829 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
830
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700831 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700832
Mohamed Abbasfde35712007-11-29 11:10:15 +0800833 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -0700834 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +0800835 return -EAGAIN;
836
837 cancel_delayed_work(&priv->scan_check);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800838 if (iwl_scan_cancel_timeout(priv, 100)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +0800839 IWL_WARNING("Aborted scan still in progress after 100ms\n");
840 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
841 return -EAGAIN;
842 }
843
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800844 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700845
846 return 0;
847}
848
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700849static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700850{
Johannes Berg8318d782008-01-24 19:38:38 +0100851 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700852 struct ieee80211_rate *rate;
853 int i;
854
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700855 hw = iwl_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -0800856 if (!hw) {
857 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
858 return;
859 }
Zhu Yib481de92007-09-25 17:54:57 -0700860
861 priv->active_rate = 0;
862 priv->active_rate_basic = 0;
863
Johannes Berg8318d782008-01-24 19:38:38 +0100864 for (i = 0; i < hw->n_bitrates; i++) {
865 rate = &(hw->bitrates[i]);
866 if (rate->hw_value < IWL_RATE_COUNT)
867 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -0700868 }
869
870 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
871 priv->active_rate, priv->active_rate_basic);
872
873 /*
874 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
875 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
876 * OFDM
877 */
878 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
879 priv->staging_rxon.cck_basic_rates =
880 ((priv->active_rate_basic &
881 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
882 else
883 priv->staging_rxon.cck_basic_rates =
884 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
885
886 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
887 priv->staging_rxon.ofdm_basic_rates =
888 ((priv->active_rate_basic &
889 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
890 IWL_FIRST_OFDM_RATE) & 0xFF;
891 else
892 priv->staging_rxon.ofdm_basic_rates =
893 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
894}
895
Zhu Yib481de92007-09-25 17:54:57 -0700896#define IWL_PACKET_RETRY_TIME HZ
897
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700898int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -0700899{
900 u16 sc = le16_to_cpu(header->seq_ctrl);
901 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
902 u16 frag = sc & IEEE80211_SCTL_FRAG;
903 u16 *last_seq, *last_frag;
904 unsigned long *last_time;
905
906 switch (priv->iw_mode) {
907 case IEEE80211_IF_TYPE_IBSS:{
908 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800909 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700910 u8 *mac = header->addr2;
911 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
912
913 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800914 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -0700915 if (!compare_ether_addr(entry->mac, mac))
916 break;
917 }
918 if (p == &priv->ibss_mac_hash[index]) {
919 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
920 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +0800921 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -0700922 return 0;
923 }
924 memcpy(entry->mac, mac, ETH_ALEN);
925 entry->seq_num = seq;
926 entry->frag_num = frag;
927 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +0800928 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -0700929 return 0;
930 }
931 last_seq = &entry->seq_num;
932 last_frag = &entry->frag_num;
933 last_time = &entry->packet_time;
934 break;
935 }
936 case IEEE80211_IF_TYPE_STA:
937 last_seq = &priv->last_seq_num;
938 last_frag = &priv->last_frag_num;
939 last_time = &priv->last_packet_time;
940 break;
941 default:
942 return 0;
943 }
944 if ((*last_seq == seq) &&
945 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
946 if (*last_frag == frag)
947 goto drop;
948 if (*last_frag + 1 != frag)
949 /* out-of-order fragment */
950 goto drop;
951 } else
952 *last_seq = seq;
953
954 *last_frag = frag;
955 *last_time = jiffies;
956 return 0;
957
958 drop:
959 return 1;
960}
961
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800962#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -0700963
964#include "iwl-spectrum.h"
965
966#define BEACON_TIME_MASK_LOW 0x00FFFFFF
967#define BEACON_TIME_MASK_HIGH 0xFF000000
968#define TIME_UNIT 1024
969
970/*
971 * extended beacon time format
972 * time in usec will be changed into a 32-bit value in 8:24 format
973 * the high 1 byte is the beacon counts
974 * the lower 3 bytes is the time in usec within one beacon interval
975 */
976
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800977static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -0700978{
979 u32 quot;
980 u32 rem;
981 u32 interval = beacon_interval * 1024;
982
983 if (!interval || !usec)
984 return 0;
985
986 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
987 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
988
989 return (quot << 24) + rem;
990}
991
992/* base is usually what we get from ucode with each received frame,
993 * the same as HW timer counter counting down
994 */
995
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800996static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -0700997{
998 u32 base_low = base & BEACON_TIME_MASK_LOW;
999 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
1000 u32 interval = beacon_interval * TIME_UNIT;
1001 u32 res = (base & BEACON_TIME_MASK_HIGH) +
1002 (addon & BEACON_TIME_MASK_HIGH);
1003
1004 if (base_low > addon_low)
1005 res += base_low - addon_low;
1006 else if (base_low < addon_low) {
1007 res += interval + base_low - addon_low;
1008 res += (1 << 24);
1009 } else
1010 res += (1 << 24);
1011
1012 return cpu_to_le32(res);
1013}
1014
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001015static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001016 struct ieee80211_measurement_params *params,
1017 u8 type)
1018{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001019 struct iwl4965_spectrum_cmd spectrum;
Tomas Winklerdb11d632008-05-05 10:22:33 +08001020 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07001021 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001022 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
1023 .data = (void *)&spectrum,
1024 .meta.flags = CMD_WANT_SKB,
1025 };
1026 u32 add_time = le64_to_cpu(params->start_time);
1027 int rc;
1028 int spectrum_resp_status;
1029 int duration = le16_to_cpu(params->duration);
1030
Tomas Winkler3109ece2008-03-28 16:33:35 -07001031 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001032 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001033 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07001034 le64_to_cpu(params->start_time) - priv->last_tsf,
1035 le16_to_cpu(priv->rxon_timing.beacon_interval));
1036
1037 memset(&spectrum, 0, sizeof(spectrum));
1038
1039 spectrum.channel_count = cpu_to_le16(1);
1040 spectrum.flags =
1041 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
1042 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
1043 cmd.len = sizeof(spectrum);
1044 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
1045
Tomas Winkler3109ece2008-03-28 16:33:35 -07001046 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001047 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001048 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07001049 add_time,
1050 le16_to_cpu(priv->rxon_timing.beacon_interval));
1051 else
1052 spectrum.start_time = 0;
1053
1054 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
1055 spectrum.channels[0].channel = params->channel;
1056 spectrum.channels[0].type = type;
1057 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
1058 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
1059 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
1060
Tomas Winkler857485c2008-03-21 13:53:44 -07001061 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001062 if (rc)
1063 return rc;
1064
Tomas Winklerdb11d632008-05-05 10:22:33 +08001065 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001066 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1067 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
1068 rc = -EIO;
1069 }
1070
1071 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
1072 switch (spectrum_resp_status) {
1073 case 0: /* Command will be handled */
1074 if (res->u.spectrum.id != 0xff) {
1075 IWL_DEBUG_INFO
1076 ("Replaced existing measurement: %d\n",
1077 res->u.spectrum.id);
1078 priv->measurement_status &= ~MEASUREMENT_READY;
1079 }
1080 priv->measurement_status |= MEASUREMENT_ACTIVE;
1081 rc = 0;
1082 break;
1083
1084 case 1: /* Command will not be handled */
1085 rc = -EAGAIN;
1086 break;
1087 }
1088
1089 dev_kfree_skb_any(cmd.meta.u.skb);
1090
1091 return rc;
1092}
1093#endif
1094
Zhu Yib481de92007-09-25 17:54:57 -07001095/******************************************************************************
1096 *
1097 * Generic RX handler implementations
1098 *
1099 ******************************************************************************/
Tomas Winkler885ba202008-05-29 16:34:55 +08001100static void iwl_rx_reply_alive(struct iwl_priv *priv,
1101 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001102{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001103 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Tomas Winkler885ba202008-05-29 16:34:55 +08001104 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07001105 struct delayed_work *pwork;
1106
1107 palive = &pkt->u.alive_frame;
1108
1109 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
1110 "0x%01X 0x%01X\n",
1111 palive->is_valid, palive->ver_type,
1112 palive->ver_subtype);
1113
1114 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
1115 IWL_DEBUG_INFO("Initialization Alive received.\n");
1116 memcpy(&priv->card_alive_init,
1117 &pkt->u.alive_frame,
Tomas Winkler885ba202008-05-29 16:34:55 +08001118 sizeof(struct iwl_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001119 pwork = &priv->init_alive_start;
1120 } else {
1121 IWL_DEBUG_INFO("Runtime Alive received.\n");
1122 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler885ba202008-05-29 16:34:55 +08001123 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001124 pwork = &priv->alive_start;
1125 }
1126
1127 /* We delay the ALIVE response by 5ms to
1128 * give the HW RF Kill time to activate... */
1129 if (palive->is_valid == UCODE_VALID_OK)
1130 queue_delayed_work(priv->workqueue, pwork,
1131 msecs_to_jiffies(5));
1132 else
1133 IWL_WARNING("uCode did not respond OK.\n");
1134}
1135
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001136static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001137 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001138{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001139 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001140
1141 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
1142 "seq 0x%04X ser 0x%08X\n",
1143 le32_to_cpu(pkt->u.err_resp.error_type),
1144 get_cmd_string(pkt->u.err_resp.cmd_id),
1145 pkt->u.err_resp.cmd_id,
1146 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1147 le32_to_cpu(pkt->u.err_resp.error_info));
1148}
1149
1150#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1151
Tomas Winklera55360e2008-05-05 10:22:28 +08001152static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001153{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001154 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08001155 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001156 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07001157 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
1158 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
1159 rxon->channel = csa->channel;
1160 priv->staging_rxon.channel = csa->channel;
1161}
1162
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001163static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001164 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001165{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001166#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Tomas Winklerdb11d632008-05-05 10:22:33 +08001167 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001168 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07001169
1170 if (!report->state) {
Ester Kummerf3d67992008-05-06 11:05:12 +08001171 IWL_DEBUG(IWL_DL_11H,
1172 "Spectrum Measure Notification: Start\n");
Zhu Yib481de92007-09-25 17:54:57 -07001173 return;
1174 }
1175
1176 memcpy(&priv->measure_report, report, sizeof(*report));
1177 priv->measurement_status |= MEASUREMENT_READY;
1178#endif
1179}
1180
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001181static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001182 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001183{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001184#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08001185 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001186 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07001187 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
1188 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1189#endif
1190}
1191
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001192static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001193 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001194{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001195 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001196 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
1197 "notification for %s:\n",
1198 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Ester Kummerbf403db2008-05-05 10:22:40 +08001199 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07001200}
1201
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001202static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07001203{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001204 struct iwl_priv *priv =
1205 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07001206 struct sk_buff *beacon;
1207
1208 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02001209 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07001210
1211 if (!beacon) {
1212 IWL_ERROR("update beacon failed\n");
1213 return;
1214 }
1215
1216 mutex_lock(&priv->mutex);
1217 /* new beacon skb is allocated every time; dispose previous.*/
1218 if (priv->ibss_beacon)
1219 dev_kfree_skb(priv->ibss_beacon);
1220
1221 priv->ibss_beacon = beacon;
1222 mutex_unlock(&priv->mutex);
1223
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001224 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001225}
1226
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08001227/**
1228 * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
1229 *
1230 * This callback is provided in order to send a statistics request.
1231 *
1232 * This timer function is continually reset to execute within
1233 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
1234 * was received. We need to ensure we receive the statistics in order
1235 * to update the temperature used for calibrating the TXPOWER.
1236 */
1237static void iwl4965_bg_statistics_periodic(unsigned long data)
1238{
1239 struct iwl_priv *priv = (struct iwl_priv *)data;
1240
1241 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1242 return;
1243
1244 iwl_send_statistics_request(priv, CMD_ASYNC);
1245}
1246
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001247static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001248 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001249{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001250#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08001251 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001252 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
Tomas Winklere7d326a2008-06-12 09:47:11 +08001253 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001254
1255 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
1256 "tsf %d %d rate %d\n",
Tomas Winkler25a65722008-06-12 09:47:07 +08001257 le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
Zhu Yib481de92007-09-25 17:54:57 -07001258 beacon->beacon_notify_hdr.failure_frame,
1259 le32_to_cpu(beacon->ibss_mgr_status),
1260 le32_to_cpu(beacon->high_tsf),
1261 le32_to_cpu(beacon->low_tsf), rate);
1262#endif
1263
1264 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
1265 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
1266 queue_work(priv->workqueue, &priv->beacon_update);
1267}
1268
Zhu Yib481de92007-09-25 17:54:57 -07001269/* Handle notification from uCode that card's power state is changing
1270 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001271static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001272 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001273{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001274 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001275 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
1276 unsigned long status = priv->status;
1277
1278 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
1279 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1280 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
1281
1282 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
1283 RF_CARD_DISABLED)) {
1284
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001285 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001286 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1287
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001288 if (!iwl_grab_nic_access(priv)) {
1289 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07001290 priv, HBUS_TARG_MBX_C,
1291 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1292
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001293 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001294 }
1295
1296 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001297 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07001298 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001299 if (!iwl_grab_nic_access(priv)) {
1300 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07001301 priv, HBUS_TARG_MBX_C,
1302 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1303
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001304 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001305 }
1306 }
1307
1308 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001309 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001310 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001311 iwl_read32(priv, CSR_UCODE_DRV_GP1);
1312 if (!iwl_grab_nic_access(priv))
1313 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001314 }
1315 }
1316
1317 if (flags & HW_CARD_DISABLED)
1318 set_bit(STATUS_RF_KILL_HW, &priv->status);
1319 else
1320 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1321
1322
1323 if (flags & SW_CARD_DISABLED)
1324 set_bit(STATUS_RF_KILL_SW, &priv->status);
1325 else
1326 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1327
1328 if (!(flags & RXON_CARD_DISABLED))
Tomas Winkler2a421b92008-06-12 09:47:10 +08001329 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001330
1331 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
1332 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
1333 (test_bit(STATUS_RF_KILL_SW, &status) !=
1334 test_bit(STATUS_RF_KILL_SW, &priv->status)))
1335 queue_work(priv->workqueue, &priv->rf_kill);
1336 else
1337 wake_up_interruptible(&priv->wait_command_queue);
1338}
1339
Ron Rindjunsky37a44212008-05-29 16:35:18 +08001340/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
1341 * This will be used later in iwl4965_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
1342static void iwl4965_rx_reply_rx_phy(struct iwl_priv *priv,
1343 struct iwl_rx_mem_buffer *rxb)
1344{
1345 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1346 priv->last_phy_res[0] = 1;
1347 memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
1348 sizeof(struct iwl4965_rx_phy_res));
1349}
1350
Zhu Yib481de92007-09-25 17:54:57 -07001351/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001352 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07001353 *
1354 * Setup the RX handlers for each of the reply types sent from the uCode
1355 * to the host.
1356 *
1357 * This function chains into the hardware specific files for them to setup
1358 * any hardware specific handlers as well.
1359 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001360static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001361{
Tomas Winkler885ba202008-05-29 16:34:55 +08001362 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001363 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
1364 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07001365 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001366 iwl4965_rx_spectrum_measure_notif;
1367 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001368 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001369 iwl4965_rx_pm_debug_statistics_notif;
1370 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001371
Ben Cahill9fbab512007-11-29 11:09:47 +08001372 /*
1373 * The same handler is used for both the REPLY to a discrete
1374 * statistics request from the host as well as for the periodic
1375 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07001376 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001377 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
1378 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Tomas Winkler2a421b92008-06-12 09:47:10 +08001379
1380 iwl_setup_rx_scan_handlers(priv);
1381
Ron Rindjunsky37a44212008-05-29 16:35:18 +08001382 /* status change handler */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001383 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001384
Tomas Winklerc1354752008-05-29 16:35:04 +08001385 priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
1386 iwl_rx_missed_beacon_notif;
Ron Rindjunsky37a44212008-05-29 16:35:18 +08001387 /* Rx handlers */
1388 priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
1389 priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl4965_rx_reply_rx;
Ben Cahill9fbab512007-11-29 11:09:47 +08001390 /* Set up hardware specific Rx handlers */
Emmanuel Grumbachd4789ef2008-04-24 11:55:20 -07001391 priv->cfg->ops->lib->rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001392}
1393
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001394/*
1395 * this should be called while priv->lock is locked
1396*/
Tomas Winklera55360e2008-05-05 10:22:28 +08001397static void __iwl_rx_replenish(struct iwl_priv *priv)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001398{
Tomas Winklera55360e2008-05-05 10:22:28 +08001399 iwl_rx_allocate(priv);
1400 iwl_rx_queue_restock(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001401}
1402
1403
Zhu Yib481de92007-09-25 17:54:57 -07001404/**
Tomas Winklera55360e2008-05-05 10:22:28 +08001405 * iwl_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07001406 *
1407 * Uses the priv->rx_handlers callback function array to invoke
1408 * the appropriate handlers, including command responses,
1409 * frame-received notifications, and other notifications.
1410 */
Tomas Winklera55360e2008-05-05 10:22:28 +08001411void iwl_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001412{
Tomas Winklera55360e2008-05-05 10:22:28 +08001413 struct iwl_rx_mem_buffer *rxb;
Tomas Winklerdb11d632008-05-05 10:22:33 +08001414 struct iwl_rx_packet *pkt;
Tomas Winklera55360e2008-05-05 10:22:28 +08001415 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001416 u32 r, i;
1417 int reclaim;
1418 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001419 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08001420 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07001421
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001422 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1423 * buffer that the driver may process (last buffer filled by ucode). */
Ron Rindjunskyd67f5482008-05-05 10:22:49 +08001424 r = priv->cfg->ops->lib->shared_mem_rx_idx(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001425 i = rxq->read;
1426
1427 /* Rx interrupt, but nothing sent from uCode */
1428 if (i == r)
Ester Kummerf3d67992008-05-06 11:05:12 +08001429 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07001430
Tomas Winklera55360e2008-05-05 10:22:28 +08001431 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001432 fill_rx = 1;
1433
Zhu Yib481de92007-09-25 17:54:57 -07001434 while (i != r) {
1435 rxb = rxq->queue[i];
1436
Ben Cahill9fbab512007-11-29 11:09:47 +08001437 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07001438 * then a bug has been introduced in the queue refilling
1439 * routines -- catch it here */
1440 BUG_ON(rxb == NULL);
1441
1442 rxq->queue[i] = NULL;
1443
1444 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07001445 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07001446 PCI_DMA_FROMDEVICE);
Tomas Winklerdb11d632008-05-05 10:22:33 +08001447 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001448
1449 /* Reclaim a command buffer only if this packet is a response
1450 * to a (driver-originated) command.
1451 * If the packet (e.g. Rx frame) originated from uCode,
1452 * there is no command buffer to reclaim.
1453 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1454 * but apparently a few don't get set; catch them here. */
1455 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1456 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07001457 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08001458 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07001459 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1460 (pkt->hdr.cmd != REPLY_TX);
1461
1462 /* Based on type of command response or notification,
1463 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001464 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07001465 if (priv->rx_handlers[pkt->hdr.cmd]) {
Ester Kummerf3d67992008-05-06 11:05:12 +08001466 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d, %s, 0x%02x\n", r,
1467 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001468 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
1469 } else {
1470 /* No handling needed */
Ester Kummerf3d67992008-05-06 11:05:12 +08001471 IWL_DEBUG(IWL_DL_RX,
Zhu Yib481de92007-09-25 17:54:57 -07001472 "r %d i %d No handler needed for %s, 0x%02x\n",
1473 r, i, get_cmd_string(pkt->hdr.cmd),
1474 pkt->hdr.cmd);
1475 }
1476
1477 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001478 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07001479 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07001480 * as we reclaim the driver command queue */
1481 if (rxb && rxb->skb)
Tomas Winkler17b88922008-05-29 16:35:12 +08001482 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001483 else
1484 IWL_WARNING("Claim null rxb?\n");
1485 }
1486
1487 /* For now we just don't re-use anything. We can tweak this
1488 * later to try and re-use notification packets and SKBs that
1489 * fail to Rx correctly */
1490 if (rxb->skb != NULL) {
1491 priv->alloc_rxb_skb--;
1492 dev_kfree_skb_any(rxb->skb);
1493 rxb->skb = NULL;
1494 }
1495
1496 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07001497 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02001498 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07001499 spin_lock_irqsave(&rxq->lock, flags);
1500 list_add_tail(&rxb->list, &priv->rxq.rx_used);
1501 spin_unlock_irqrestore(&rxq->lock, flags);
1502 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001503 /* If there are a lot of unused frames,
1504 * restock the Rx queue so ucode wont assert. */
1505 if (fill_rx) {
1506 count++;
1507 if (count >= 8) {
1508 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08001509 __iwl_rx_replenish(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001510 count = 0;
1511 }
1512 }
Zhu Yib481de92007-09-25 17:54:57 -07001513 }
1514
1515 /* Backtrack one entry */
1516 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08001517 iwl_rx_queue_restock(priv);
1518}
Tomas Winklera55360e2008-05-05 10:22:28 +08001519
1520#define PERFECT_RSSI (-20) /* dBm */
1521#define WORST_RSSI (-95) /* dBm */
1522#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
1523
1524/* Calculate an indication of rx signal quality (a percentage, not dBm!).
1525 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
1526 * about formulas used below. */
1527int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
1528{
1529 int sig_qual;
1530 int degradation = PERFECT_RSSI - rssi_dbm;
1531
1532 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
1533 * as indicator; formula is (signal dbm - noise dbm).
1534 * SNR at or above 40 is a great signal (100%).
1535 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
1536 * Weakest usable signal is usually 10 - 15 dB SNR. */
1537 if (noise_dbm) {
1538 if (rssi_dbm - noise_dbm >= 40)
1539 return 100;
1540 else if (rssi_dbm < noise_dbm)
1541 return 0;
1542 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
1543
1544 /* Else use just the signal level.
1545 * This formula is a least squares fit of data points collected and
1546 * compared with a reference system that had a percentage (%) display
1547 * for signal quality. */
1548 } else
1549 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
1550 (15 * RSSI_RANGE + 62 * degradation)) /
1551 (RSSI_RANGE * RSSI_RANGE);
1552
1553 if (sig_qual > 100)
1554 sig_qual = 100;
1555 else if (sig_qual < 1)
1556 sig_qual = 0;
1557
1558 return sig_qual;
Zhu Yib481de92007-09-25 17:54:57 -07001559}
1560
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001561#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001562static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001563{
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08001564 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07001565 DECLARE_MAC_BUF(mac);
1566
Zhu Yib481de92007-09-25 17:54:57 -07001567 IWL_DEBUG_RADIO("RX CONFIG:\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08001568 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07001569 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
1570 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
1571 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
1572 le32_to_cpu(rxon->filter_flags));
1573 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
1574 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
1575 rxon->ofdm_basic_rates);
1576 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07001577 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
1578 print_mac(mac, rxon->node_addr));
1579 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
1580 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07001581 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
1582}
1583#endif
1584
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001585static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001586{
1587 IWL_DEBUG_ISR("Enabling interrupts\n");
1588 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001589 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07001590}
1591
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001592/* call this function to flush any scheduled tasklet */
1593static inline void iwl_synchronize_irq(struct iwl_priv *priv)
1594{
1595 /* wait to make sure we flush pedding tasklet*/
1596 synchronize_irq(priv->pci_dev->irq);
1597 tasklet_kill(&priv->irq_tasklet);
1598}
1599
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001600static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001601{
1602 clear_bit(STATUS_INT_ENABLED, &priv->status);
1603
1604 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001605 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07001606
1607 /* acknowledge/clear/reset any interrupts still pending
1608 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001609 iwl_write32(priv, CSR_INT, 0xffffffff);
1610 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07001611 IWL_DEBUG_ISR("Disabled interrupts\n");
1612}
1613
Zhu Yib481de92007-09-25 17:54:57 -07001614
Zhu Yib481de92007-09-25 17:54:57 -07001615/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001616 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07001617 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001618static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001619{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001620 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07001621 set_bit(STATUS_FW_ERROR, &priv->status);
1622
1623 /* Cancel currently queued command. */
1624 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
1625
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001626#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001627 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Ester Kummerede0cba2008-05-29 16:34:46 +08001628 iwl_dump_nic_error_log(priv);
Ester Kummer189a2b52008-05-15 13:54:18 +08001629 iwl_dump_nic_event_log(priv);
Ester Kummerbf403db2008-05-05 10:22:40 +08001630 iwl4965_print_rx_config_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001631 }
1632#endif
1633
1634 wake_up_interruptible(&priv->wait_command_queue);
1635
1636 /* Keep the restart process from trying to send host
1637 * commands by clearing the INIT status bit */
1638 clear_bit(STATUS_READY, &priv->status);
1639
1640 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08001641 IWL_DEBUG(IWL_DL_FW_ERRORS,
Zhu Yib481de92007-09-25 17:54:57 -07001642 "Restarting adapter due to uCode error.\n");
1643
Tomas Winkler3109ece2008-03-28 16:33:35 -07001644 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001645 memcpy(&priv->recovery_rxon, &priv->active_rxon,
1646 sizeof(priv->recovery_rxon));
1647 priv->error_recovering = 1;
1648 }
Ester Kummer3a1081e2008-05-06 11:05:14 +08001649 if (priv->cfg->mod_params->restart_fw)
1650 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07001651 }
1652}
1653
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001654static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001655{
1656 unsigned long flags;
1657
1658 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
1659 sizeof(priv->staging_rxon));
1660 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001661 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001662
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001663 iwl_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07001664
1665 spin_lock_irqsave(&priv->lock, flags);
1666 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
1667 priv->error_recovering = 0;
1668 spin_unlock_irqrestore(&priv->lock, flags);
1669}
1670
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001671static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001672{
1673 u32 inta, handled = 0;
1674 u32 inta_fh;
1675 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001676#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001677 u32 inta_mask;
1678#endif
1679
1680 spin_lock_irqsave(&priv->lock, flags);
1681
1682 /* Ack/clear/reset pending uCode interrupts.
1683 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1684 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001685 inta = iwl_read32(priv, CSR_INT);
1686 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001687
1688 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1689 * Any new interrupts that happen after this, either while we're
1690 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001691 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1692 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001693
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001694#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001695 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001696 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001697 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07001698 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1699 inta, inta_mask, inta_fh);
1700 }
1701#endif
1702
1703 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1704 * atomic, make sure that inta covers all the interrupts that
1705 * we've discovered, even if FH interrupt came in just after
1706 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001707 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001708 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001709 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001710 inta |= CSR_INT_BIT_FH_TX;
1711
1712 /* Now service all interrupt bits discovered above. */
1713 if (inta & CSR_INT_BIT_HW_ERR) {
1714 IWL_ERROR("Microcode HW error detected. Restarting.\n");
1715
1716 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001717 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001718
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001719 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001720
1721 handled |= CSR_INT_BIT_HW_ERR;
1722
1723 spin_unlock_irqrestore(&priv->lock, flags);
1724
1725 return;
1726 }
1727
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001728#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001729 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001730 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001731 if (inta & CSR_INT_BIT_SCD)
1732 IWL_DEBUG_ISR("Scheduler finished to transmit "
1733 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001734
1735 /* Alive notification via Rx interrupt will do the real work */
1736 if (inta & CSR_INT_BIT_ALIVE)
1737 IWL_DEBUG_ISR("Alive interrupt\n");
1738 }
1739#endif
1740 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001741 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001742
Ben Cahill9fbab512007-11-29 11:09:47 +08001743 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07001744 if (inta & CSR_INT_BIT_RF_KILL) {
1745 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001746 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07001747 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
1748 hw_rf_kill = 1;
1749
Ester Kummerf3d67992008-05-06 11:05:12 +08001750 IWL_DEBUG(IWL_DL_RF_KILL, "RF_KILL bit toggled to %s.\n",
Zhu Yib481de92007-09-25 17:54:57 -07001751 hw_rf_kill ? "disable radio":"enable radio");
1752
1753 /* Queue restart only if RF_KILL switch was set to "kill"
1754 * when we loaded driver, and is now set to "enable".
1755 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08001756 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08001757 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
1758 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07001759 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08001760 }
Zhu Yib481de92007-09-25 17:54:57 -07001761
1762 handled |= CSR_INT_BIT_RF_KILL;
1763 }
1764
Ben Cahill9fbab512007-11-29 11:09:47 +08001765 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07001766 if (inta & CSR_INT_BIT_CT_KILL) {
1767 IWL_ERROR("Microcode CT kill error detected.\n");
1768 handled |= CSR_INT_BIT_CT_KILL;
1769 }
1770
1771 /* Error detected by uCode */
1772 if (inta & CSR_INT_BIT_SW_ERR) {
1773 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
1774 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001775 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001776 handled |= CSR_INT_BIT_SW_ERR;
1777 }
1778
1779 /* uCode wakes up after power-down sleep */
1780 if (inta & CSR_INT_BIT_WAKEUP) {
1781 IWL_DEBUG_ISR("Wakeup interrupt\n");
Tomas Winklera55360e2008-05-05 10:22:28 +08001782 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Tomas Winklerbabcebf2008-05-15 13:54:00 +08001783 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1784 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1785 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1786 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1787 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1788 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07001789
1790 handled |= CSR_INT_BIT_WAKEUP;
1791 }
1792
1793 /* All uCode command responses, including Tx command responses,
1794 * Rx "responses" (frame-received notification), and other
1795 * notifications from uCode come through here*/
1796 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Tomas Winklera55360e2008-05-05 10:22:28 +08001797 iwl_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001798 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1799 }
1800
1801 if (inta & CSR_INT_BIT_FH_TX) {
1802 IWL_DEBUG_ISR("Tx interrupt\n");
1803 handled |= CSR_INT_BIT_FH_TX;
Ron Rindjunskydbb983b2008-05-15 13:54:12 +08001804 /* FH finished to write, send event */
1805 priv->ucode_write_complete = 1;
1806 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07001807 }
1808
1809 if (inta & ~handled)
1810 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
1811
1812 if (inta & ~CSR_INI_SET_MASK) {
1813 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
1814 inta & ~CSR_INI_SET_MASK);
1815 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
1816 }
1817
1818 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001819 /* only Re-enable if diabled by irq */
1820 if (test_bit(STATUS_INT_ENABLED, &priv->status))
1821 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001822
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001823#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001824 if (priv->debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001825 inta = iwl_read32(priv, CSR_INT);
1826 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1827 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07001828 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
1829 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1830 }
1831#endif
1832 spin_unlock_irqrestore(&priv->lock, flags);
1833}
1834
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001835static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07001836{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001837 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07001838 u32 inta, inta_mask;
1839 u32 inta_fh;
1840 if (!priv)
1841 return IRQ_NONE;
1842
1843 spin_lock(&priv->lock);
1844
1845 /* Disable (but don't clear!) interrupts here to avoid
1846 * back-to-back ISRs and sporadic interrupts from our NIC.
1847 * If we have something to service, the tasklet will re-enable ints.
1848 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001849 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
1850 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07001851
1852 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001853 inta = iwl_read32(priv, CSR_INT);
1854 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07001855
1856 /* Ignore interrupt if there's nothing in NIC to service.
1857 * This may be due to IRQ shared with another device,
1858 * or due to sporadic interrupts thrown from our NIC. */
1859 if (!inta && !inta_fh) {
1860 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
1861 goto none;
1862 }
1863
1864 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08001865 /* Hardware disappeared. It might have already raised
1866 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07001867 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08001868 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07001869 }
1870
1871 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1872 inta, inta_mask, inta_fh);
1873
Joonwoo Park25c03d82008-01-23 10:15:20 -08001874 inta &= ~CSR_INT_BIT_SCD;
1875
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001876 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001877 if (likely(inta || inta_fh))
1878 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07001879
Oliver Neukum66fbb542007-11-15 09:31:10 +08001880 unplugged:
1881 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07001882 return IRQ_HANDLED;
1883
1884 none:
1885 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001886 /* only Re-enable if diabled by irq */
1887 if (test_bit(STATUS_INT_ENABLED, &priv->status))
1888 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001889 spin_unlock(&priv->lock);
1890 return IRQ_NONE;
1891}
1892
Zhu Yib481de92007-09-25 17:54:57 -07001893/******************************************************************************
1894 *
1895 * uCode download functions
1896 *
1897 ******************************************************************************/
1898
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001899static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001900{
Tomas Winkler98c92212008-01-14 17:46:20 -08001901 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1902 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1903 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1904 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1905 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1906 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001907}
1908
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08001909static void iwl4965_nic_start(struct iwl_priv *priv)
1910{
1911 /* Remove all resets to allow NIC to operate */
1912 iwl_write32(priv, CSR_RESET, 0);
1913}
1914
1915
Zhu Yib481de92007-09-25 17:54:57 -07001916/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001917 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07001918 *
1919 * Copy into buffers for card to fetch via bus-mastering
1920 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001921static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001922{
Ron Rindjunsky14b3d332008-06-13 15:44:54 +08001923 struct iwl_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08001924 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001925 const struct firmware *ucode_raw;
Tomas Winkler4bf775cd2008-03-04 18:09:31 -08001926 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07001927 u8 *src;
1928 size_t len;
1929 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
1930
1931 /* Ask kernel firmware_class module to get the boot firmware off disk.
1932 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08001933 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
1934 if (ret < 0) {
1935 IWL_ERROR("%s firmware file req failed: Reason %d\n",
1936 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07001937 goto error;
1938 }
1939
1940 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
1941 name, ucode_raw->size);
1942
1943 /* Make sure that we got at least our header! */
1944 if (ucode_raw->size < sizeof(*ucode)) {
1945 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08001946 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001947 goto err_release;
1948 }
1949
1950 /* Data from ucode file: header followed by uCode images */
1951 ucode = (void *)ucode_raw->data;
1952
1953 ver = le32_to_cpu(ucode->ver);
1954 inst_size = le32_to_cpu(ucode->inst_size);
1955 data_size = le32_to_cpu(ucode->data_size);
1956 init_size = le32_to_cpu(ucode->init_size);
1957 init_data_size = le32_to_cpu(ucode->init_data_size);
1958 boot_size = le32_to_cpu(ucode->boot_size);
1959
1960 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
1961 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
1962 inst_size);
1963 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
1964 data_size);
1965 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
1966 init_size);
1967 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
1968 init_data_size);
1969 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
1970 boot_size);
1971
1972 /* Verify size of file vs. image size info in file's header */
1973 if (ucode_raw->size < sizeof(*ucode) +
1974 inst_size + data_size + init_size +
1975 init_data_size + boot_size) {
1976
1977 IWL_DEBUG_INFO("uCode file size %d too small\n",
1978 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08001979 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001980 goto err_release;
1981 }
1982
1983 /* Verify that uCode images will fit in card's SRAM */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001984 if (inst_size > priv->hw_params.max_inst_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08001985 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
1986 inst_size);
1987 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001988 goto err_release;
1989 }
1990
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001991 if (data_size > priv->hw_params.max_data_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08001992 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
1993 data_size);
1994 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001995 goto err_release;
1996 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001997 if (init_size > priv->hw_params.max_inst_size) {
Zhu Yib481de92007-09-25 17:54:57 -07001998 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08001999 ("uCode init instr len %d too large to fit in\n",
2000 init_size);
2001 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002002 goto err_release;
2003 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002004 if (init_data_size > priv->hw_params.max_data_size) {
Zhu Yib481de92007-09-25 17:54:57 -07002005 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08002006 ("uCode init data len %d too large to fit in\n",
2007 init_data_size);
2008 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002009 goto err_release;
2010 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002011 if (boot_size > priv->hw_params.max_bsm_size) {
Zhu Yib481de92007-09-25 17:54:57 -07002012 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08002013 ("uCode boot instr len %d too large to fit in\n",
2014 boot_size);
2015 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002016 goto err_release;
2017 }
2018
2019 /* Allocate ucode buffers for card's bus-master loading ... */
2020
2021 /* Runtime instructions and 2 copies of data:
2022 * 1) unmodified from disk
2023 * 2) backup cache for save/restore during power-downs */
2024 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002025 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07002026
2027 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002028 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07002029
2030 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002031 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07002032
2033 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08002034 if (init_size && init_data_size) {
2035 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002036 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07002037
Tomas Winkler90e759d2007-11-29 11:09:41 +08002038 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002039 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002040
2041 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2042 goto err_pci_alloc;
2043 }
Zhu Yib481de92007-09-25 17:54:57 -07002044
2045 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08002046 if (boot_size) {
2047 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002048 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07002049
Tomas Winkler90e759d2007-11-29 11:09:41 +08002050 if (!priv->ucode_boot.v_addr)
2051 goto err_pci_alloc;
2052 }
Zhu Yib481de92007-09-25 17:54:57 -07002053
2054 /* Copy images into buffers for card's bus-master reads ... */
2055
2056 /* Runtime instructions (first block of data in file) */
2057 src = &ucode->data[0];
2058 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002059 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002060 memcpy(priv->ucode_code.v_addr, src, len);
2061 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
2062 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2063
2064 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002065 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07002066 src = &ucode->data[inst_size];
2067 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002068 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002069 memcpy(priv->ucode_data.v_addr, src, len);
2070 memcpy(priv->ucode_data_backup.v_addr, src, len);
2071
2072 /* Initialization instructions (3rd block) */
2073 if (init_size) {
2074 src = &ucode->data[inst_size + data_size];
2075 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002076 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
2077 len);
Zhu Yib481de92007-09-25 17:54:57 -07002078 memcpy(priv->ucode_init.v_addr, src, len);
2079 }
2080
2081 /* Initialization data (4th block) */
2082 if (init_data_size) {
2083 src = &ucode->data[inst_size + data_size + init_size];
2084 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002085 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
2086 len);
Zhu Yib481de92007-09-25 17:54:57 -07002087 memcpy(priv->ucode_init_data.v_addr, src, len);
2088 }
2089
2090 /* Bootstrap instructions (5th block) */
2091 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
2092 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002093 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002094 memcpy(priv->ucode_boot.v_addr, src, len);
2095
2096 /* We have our copies now, allow OS release its copies */
2097 release_firmware(ucode_raw);
2098 return 0;
2099
2100 err_pci_alloc:
2101 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002102 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002103 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002104
2105 err_release:
2106 release_firmware(ucode_raw);
2107
2108 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08002109 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002110}
2111
Zhu Yib481de92007-09-25 17:54:57 -07002112/**
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002113 * iwl_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002114 * from protocol/runtime uCode (initialization uCode's
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002115 * Alive gets handled by iwl_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002116 */
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002117static void iwl_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002118{
Tomas Winkler57aab752008-04-14 21:16:03 -07002119 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002120
2121 IWL_DEBUG_INFO("Runtime Alive received.\n");
2122
2123 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2124 /* We had an error bringing up the hardware, so take it
2125 * all the way back down so we can try again */
2126 IWL_DEBUG_INFO("Alive failed.\n");
2127 goto restart;
2128 }
2129
2130 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2131 * This is a paranoid check, because we would not have gotten the
2132 * "runtime" alive if code weren't properly loaded. */
Emmanuel Grumbachb0692f22008-04-24 11:55:18 -07002133 if (iwl_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002134 /* Runtime instruction load was bad;
2135 * take it all the way back down so we can try again */
2136 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
2137 goto restart;
2138 }
2139
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002140 iwlcore_clear_stations_table(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07002141 ret = priv->cfg->ops->lib->alive_notify(priv);
2142 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07002143 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07002144 ret);
Zhu Yib481de92007-09-25 17:54:57 -07002145 goto restart;
2146 }
2147
Ben Cahill9fbab512007-11-29 11:09:47 +08002148 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002149 set_bit(STATUS_ALIVE, &priv->status);
2150
Tomas Winklerfee12472008-04-03 16:05:21 -07002151 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002152 return;
2153
Johannes Berg36d68252008-05-15 12:55:26 +02002154 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002155
2156 priv->active_rate = priv->rates_mask;
2157 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2158
Tomas Winkler3109ece2008-03-28 16:33:35 -07002159 if (iwl_is_associated(priv)) {
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08002160 struct iwl_rxon_cmd *active_rxon =
2161 (struct iwl_rxon_cmd *)&priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002162
2163 memcpy(&priv->staging_rxon, &priv->active_rxon,
2164 sizeof(priv->staging_rxon));
2165 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2166 } else {
2167 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002168 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002169 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2170 }
2171
Ben Cahill9fbab512007-11-29 11:09:47 +08002172 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002173 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002174
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002175 iwl_reset_run_time_calib(priv);
2176
Zhu Yib481de92007-09-25 17:54:57 -07002177 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002178 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002179
2180 /* At this point, the NIC is initialized and operational */
Emmanuel Grumbach47f4a582008-06-12 09:47:13 +08002181 iwl_rf_kill_ct_config(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08002182
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002183 iwl_leds_register(priv);
2184
Zhu Yib481de92007-09-25 17:54:57 -07002185 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002186 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08002187 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002188
2189 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002190 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002191
Emmanuel Grumbach58d0f362008-06-12 09:47:19 +08002192 iwl_power_update_mode(priv, 1);
Mohamed Abbas84363e62008-04-04 16:59:58 -07002193 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002194
2195 if (test_and_clear_bit(STATUS_MODE_PENDING, &priv->status))
2196 iwl4965_set_mode(priv, priv->iw_mode);
2197
Zhu Yib481de92007-09-25 17:54:57 -07002198 return;
2199
2200 restart:
2201 queue_work(priv->workqueue, &priv->restart);
2202}
2203
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08002204static void iwl_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002205
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002206static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002207{
2208 unsigned long flags;
2209 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002210
2211 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
2212
Zhu Yib481de92007-09-25 17:54:57 -07002213 if (!exit_pending)
2214 set_bit(STATUS_EXIT_PENDING, &priv->status);
2215
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002216 iwl_leds_unregister(priv);
2217
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002218 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002219
2220 /* Unblock any waiting calls */
2221 wake_up_interruptible_all(&priv->wait_command_queue);
2222
Zhu Yib481de92007-09-25 17:54:57 -07002223 /* Wipe out the EXIT_PENDING status bit if we are not actually
2224 * exiting the module */
2225 if (!exit_pending)
2226 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2227
2228 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002229 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002230
2231 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002232 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002233 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002234 spin_unlock_irqrestore(&priv->lock, flags);
2235 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002236
2237 if (priv->mac80211_registered)
2238 ieee80211_stop_queues(priv->hw);
2239
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002240 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07002241 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07002242 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002243 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2244 STATUS_RF_KILL_HW |
2245 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2246 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08002247 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2248 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002249 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
2250 STATUS_IN_SUSPEND;
2251 goto exit;
2252 }
2253
2254 /* ...otherwise clear out all the status bits but the RF Kill and
2255 * SUSPEND bits and continue taking the NIC down. */
2256 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2257 STATUS_RF_KILL_HW |
2258 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2259 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08002260 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2261 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002262 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
2263 STATUS_IN_SUSPEND |
2264 test_bit(STATUS_FW_ERROR, &priv->status) <<
2265 STATUS_FW_ERROR;
2266
2267 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002268 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08002269 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07002270 spin_unlock_irqrestore(&priv->lock, flags);
2271
Tomas Winklerda1bc452008-05-29 16:35:00 +08002272 iwl_txq_ctx_stop(priv);
Tomas Winklerb3bbacb2008-05-29 16:35:01 +08002273 iwl_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002274
2275 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002276 if (!iwl_grab_nic_access(priv)) {
2277 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002278 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002279 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002280 }
2281 spin_unlock_irqrestore(&priv->lock, flags);
2282
2283 udelay(5);
2284
Tomas Winkler7f066102008-05-29 16:34:57 +08002285 /* FIXME: apm_ops.suspend(priv) */
2286 priv->cfg->ops->lib->apm_ops.reset(priv);
Ron Rindjunsky399f49002008-04-23 17:14:56 -07002287 priv->cfg->ops->lib->free_shared_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002288
2289 exit:
Tomas Winkler885ba202008-05-29 16:34:55 +08002290 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002291
2292 if (priv->ibss_beacon)
2293 dev_kfree_skb(priv->ibss_beacon);
2294 priv->ibss_beacon = NULL;
2295
2296 /* clear out any free frames */
Tomas Winklerfcab4232008-05-15 13:54:01 +08002297 iwl_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002298}
2299
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002300static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002301{
2302 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002303 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002304 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002305
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08002306 iwl_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002307}
2308
2309#define MAX_HW_RESTARTS 5
2310
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002311static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002312{
Tomas Winkler57aab752008-04-14 21:16:03 -07002313 int i;
2314 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002315
2316 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
2317 IWL_WARNING("Exit pending; will not bring the NIC up\n");
2318 return -EIO;
2319 }
2320
Reinette Chatree903fbd2008-01-30 22:05:15 -08002321 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
2322 IWL_ERROR("ucode not available for device bringup\n");
2323 return -EIO;
2324 }
2325
Zhu Yie655b9f2008-01-24 02:19:38 -08002326 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002327 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08002328 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2329 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2330 else {
2331 set_bit(STATUS_RF_KILL_HW, &priv->status);
2332 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002333 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08002334 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
2335 return -ENODEV;
2336 }
Zhu Yib481de92007-09-25 17:54:57 -07002337 }
2338
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002339 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002340 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002341
Ron Rindjunsky399f49002008-04-23 17:14:56 -07002342 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
2343 if (ret) {
2344 IWL_ERROR("Unable to allocate shared memory\n");
2345 return ret;
2346 }
2347
Ron Rindjunsky1053d352008-05-05 10:22:43 +08002348 ret = iwl_hw_nic_init(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07002349 if (ret) {
2350 IWL_ERROR("Unable to init nic\n");
2351 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002352 }
2353
2354 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002355 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2356 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002357 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2358
2359 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002360 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002361 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002362
2363 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002364 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2365 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002366
2367 /* Copy original ucode data image from disk into backup cache.
2368 * This will be used to initialize the on-board processor's
2369 * data SRAM for a clean start when the runtime program first loads. */
2370 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08002371 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002372
Zhu Yie655b9f2008-01-24 02:19:38 -08002373 /* We return success when we resume from suspend and rf_kill is on. */
Mohamed Abbas64e72c32008-06-12 09:47:03 +08002374 if (test_bit(STATUS_RF_KILL_HW, &priv->status) ||
2375 test_bit(STATUS_RF_KILL_SW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07002376 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07002377
2378 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2379
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002380 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002381
2382 /* load bootstrap state machine,
2383 * load bootstrap program into processor's memory,
2384 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07002385 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002386
Tomas Winkler57aab752008-04-14 21:16:03 -07002387 if (ret) {
2388 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07002389 continue;
2390 }
2391
Emmanuel Grumbachf3d5b452008-06-12 09:47:04 +08002392 /* Clear out the uCode error bit if it is set */
2393 clear_bit(STATUS_FW_ERROR, &priv->status);
2394
Zhu Yib481de92007-09-25 17:54:57 -07002395 /* start card; "initialize" will load runtime ucode */
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08002396 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002397
Zhu Yib481de92007-09-25 17:54:57 -07002398 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
2399
2400 return 0;
2401 }
2402
2403 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002404 __iwl4965_down(priv);
Mohamed Abbas64e72c32008-06-12 09:47:03 +08002405 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002406
2407 /* tried to restart and config the device for as long as our
2408 * patience could withstand */
2409 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
2410 return -EIO;
2411}
2412
2413
2414/*****************************************************************************
2415 *
2416 * Workqueue callbacks
2417 *
2418 *****************************************************************************/
2419
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002420static void iwl_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002421{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002422 struct iwl_priv *priv =
2423 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002424
2425 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2426 return;
2427
2428 mutex_lock(&priv->mutex);
Emmanuel Grumbachf3ccc082008-05-05 10:22:45 +08002429 priv->cfg->ops->lib->init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002430 mutex_unlock(&priv->mutex);
2431}
2432
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002433static void iwl_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002434{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002435 struct iwl_priv *priv =
2436 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002437
2438 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2439 return;
2440
2441 mutex_lock(&priv->mutex);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002442 iwl_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002443 mutex_unlock(&priv->mutex);
2444}
2445
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002446static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002447{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002448 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07002449
2450 wake_up_interruptible(&priv->wait_command_queue);
2451
2452 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2453 return;
2454
2455 mutex_lock(&priv->mutex);
2456
Tomas Winklerfee12472008-04-03 16:05:21 -07002457 if (!iwl_is_rfkill(priv)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08002458 IWL_DEBUG(IWL_DL_RF_KILL,
Zhu Yib481de92007-09-25 17:54:57 -07002459 "HW and/or SW RF Kill no longer active, restarting "
2460 "device\n");
2461 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
2462 queue_work(priv->workqueue, &priv->restart);
2463 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002464 /* make sure mac80211 stop sending Tx frame */
2465 if (priv->mac80211_registered)
2466 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002467
2468 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
2469 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2470 "disabled by SW switch\n");
2471 else
2472 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
2473 "Kill switch must be turned off for "
2474 "wireless networking to work.\n");
2475 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002476 iwl_rfkill_set_hw_state(priv);
2477
Zhu Yib481de92007-09-25 17:54:57 -07002478 mutex_unlock(&priv->mutex);
2479}
2480
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08002481static void iwl4965_bg_set_monitor(struct work_struct *work)
2482{
2483 struct iwl_priv *priv = container_of(work,
2484 struct iwl_priv, set_monitor);
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002485 int ret;
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08002486
2487 IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
2488
2489 mutex_lock(&priv->mutex);
2490
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002491 ret = iwl4965_set_mode(priv, IEEE80211_IF_TYPE_MNTR);
2492
2493 if (ret) {
2494 if (ret == -EAGAIN)
2495 IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
2496 else
2497 IWL_ERROR("iwl4965_set_mode() failed ret = %d\n", ret);
2498 }
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08002499
2500 mutex_unlock(&priv->mutex);
2501}
2502
Emmanuel Grumbach16e727e2008-06-12 09:46:52 +08002503static void iwl_bg_run_time_calib_work(struct work_struct *work)
2504{
2505 struct iwl_priv *priv = container_of(work, struct iwl_priv,
2506 run_time_calib_work);
2507
2508 mutex_lock(&priv->mutex);
2509
2510 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
2511 test_bit(STATUS_SCANNING, &priv->status)) {
2512 mutex_unlock(&priv->mutex);
2513 return;
2514 }
2515
2516 if (priv->start_calib) {
2517 iwl_chain_noise_calibration(priv, &priv->statistics);
2518
2519 iwl_sensitivity_calibration(priv, &priv->statistics);
2520 }
2521
2522 mutex_unlock(&priv->mutex);
2523 return;
2524}
2525
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002526static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002527{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002528 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07002529
2530 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2531 return;
2532
2533 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002534 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002535 mutex_unlock(&priv->mutex);
2536}
2537
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002538static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002539{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002540 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07002541
2542 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2543 return;
2544
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002545 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002546 queue_work(priv->workqueue, &priv->up);
2547}
2548
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002549static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002550{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002551 struct iwl_priv *priv =
2552 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07002553
2554 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2555 return;
2556
2557 mutex_lock(&priv->mutex);
Tomas Winklera55360e2008-05-05 10:22:28 +08002558 iwl_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002559 mutex_unlock(&priv->mutex);
2560}
2561
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002562#define IWL_DELAY_NEXT_SCAN (HZ*2)
2563
Reinette Chatre508e32e2008-04-14 21:16:13 -07002564static void iwl4965_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002565{
Zhu Yib481de92007-09-25 17:54:57 -07002566 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07002567 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07002568 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002569
2570 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2571 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
2572 return;
2573 }
2574
Joe Perches0795af52007-10-03 17:59:30 -07002575 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
2576 priv->assoc_id,
2577 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07002578
2579
2580 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2581 return;
2582
Zhu Yib481de92007-09-25 17:54:57 -07002583
Reinette Chatre508e32e2008-04-14 21:16:13 -07002584 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08002585 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07002586
Tomas Winkler2a421b92008-06-12 09:47:10 +08002587 iwl_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08002588
Zhu Yib481de92007-09-25 17:54:57 -07002589 conf = ieee80211_get_hw_conf(priv->hw);
2590
2591 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002592 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002593
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002594 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
2595 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07002596 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07002597 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07002598 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07002599 IWL_WARNING("REPLY_RXON_TIMING failed - "
2600 "Attempting to continue.\n");
2601
2602 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
2603
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02002604 if (priv->current_ht_config.is_ht)
Tomas Winkler47c51962008-05-05 10:22:41 +08002605 iwl_set_rxon_ht(priv, &priv->current_ht_config);
Ron Rindjunsky4f85f5b2008-06-09 22:54:35 +03002606
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07002607 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002608 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
2609
2610 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
2611 priv->assoc_id, priv->beacon_int);
2612
2613 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
2614 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2615 else
2616 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2617
2618 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
2619 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2620 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2621 else
2622 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2623
2624 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2625 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2626
2627 }
2628
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002629 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002630
2631 switch (priv->iw_mode) {
2632 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002633 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07002634 break;
2635
2636 case IEEE80211_IF_TYPE_IBSS:
2637
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002638 /* assume default assoc id */
2639 priv->assoc_id = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002640
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08002641 iwl_rxon_add_station(priv, priv->bssid, 0);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002642 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
2643 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002644
2645 break;
2646
2647 default:
2648 IWL_ERROR("%s Should not be called in %d mode\n",
2649 __FUNCTION__, priv->iw_mode);
2650 break;
2651 }
2652
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002653 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002654
Zhu Yib481de92007-09-25 17:54:57 -07002655 /* Enable Rx differential gain and sensitivity calibrations */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07002656 iwl_chain_noise_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002657 priv->start_calib = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002658
2659 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2660 priv->assoc_station_added = 1;
2661
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002662 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08002663
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -07002664 iwl_power_update_mode(priv, 0);
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002665 /* we have just associated, don't start scan too early */
2666 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Reinette Chatre508e32e2008-04-14 21:16:13 -07002667}
2668
2669
2670static void iwl4965_bg_post_associate(struct work_struct *data)
2671{
2672 struct iwl_priv *priv = container_of(data, struct iwl_priv,
2673 post_associate.work);
2674
2675 mutex_lock(&priv->mutex);
2676 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002677 mutex_unlock(&priv->mutex);
Reinette Chatre508e32e2008-04-14 21:16:13 -07002678
Zhu Yib481de92007-09-25 17:54:57 -07002679}
2680
Zhu Yi76bb77e2007-11-22 10:53:22 +08002681static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
2682
Tomas Winkler2a421b92008-06-12 09:47:10 +08002683static void iwl_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002684{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002685 struct iwl_priv *priv =
2686 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07002687
Tomas Winkler630fe9b2008-06-12 09:47:08 +08002688 IWL_DEBUG_SCAN("SCAN complete scan\n");
Zhu Yib481de92007-09-25 17:54:57 -07002689
2690 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2691 return;
2692
Zhu Yia0646472007-12-20 14:10:01 +08002693 if (test_bit(STATUS_CONF_PENDING, &priv->status))
2694 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08002695
Zhu Yib481de92007-09-25 17:54:57 -07002696 ieee80211_scan_completed(priv->hw);
2697
2698 /* Since setting the TXPOWER may have been deferred while
2699 * performing the scan, fire one off */
2700 mutex_lock(&priv->mutex);
Tomas Winkler630fe9b2008-06-12 09:47:08 +08002701 iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
Zhu Yib481de92007-09-25 17:54:57 -07002702 mutex_unlock(&priv->mutex);
2703}
2704
2705/*****************************************************************************
2706 *
2707 * mac80211 entry point functions
2708 *
2709 *****************************************************************************/
2710
Zhu Yi5a669262008-01-14 17:46:18 -08002711#define UCODE_READY_TIMEOUT (2 * HZ)
2712
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002713static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002714{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002715 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08002716 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002717
2718 IWL_DEBUG_MAC80211("enter\n");
2719
Zhu Yi5a669262008-01-14 17:46:18 -08002720 if (pci_enable_device(priv->pci_dev)) {
2721 IWL_ERROR("Fail to pci_enable_device\n");
2722 return -ENODEV;
2723 }
2724 pci_restore_state(priv->pci_dev);
2725 pci_enable_msi(priv->pci_dev);
2726
2727 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
2728 DRV_NAME, priv);
2729 if (ret) {
2730 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
2731 goto out_disable_msi;
2732 }
2733
Zhu Yib481de92007-09-25 17:54:57 -07002734 /* we should be verifying the device is ready to be opened */
2735 mutex_lock(&priv->mutex);
2736
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08002737 memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
Zhu Yi5a669262008-01-14 17:46:18 -08002738 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
2739 * ucode filename and max sizes are card-specific. */
2740
2741 if (!priv->ucode_code.len) {
2742 ret = iwl4965_read_ucode(priv);
2743 if (ret) {
2744 IWL_ERROR("Could not read microcode: %d\n", ret);
2745 mutex_unlock(&priv->mutex);
2746 goto out_release_irq;
2747 }
2748 }
2749
Zhu Yie655b9f2008-01-24 02:19:38 -08002750 ret = __iwl4965_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08002751
Zhu Yib481de92007-09-25 17:54:57 -07002752 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08002753
Zhu Yie655b9f2008-01-24 02:19:38 -08002754 if (ret)
2755 goto out_release_irq;
2756
2757 IWL_DEBUG_INFO("Start UP work done.\n");
2758
2759 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
2760 return 0;
2761
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08002762 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
Zhu Yi5a669262008-01-14 17:46:18 -08002763 * mac80211 will not be run successfully. */
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08002764 if (priv->ucode_type == UCODE_RT) {
2765 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
2766 test_bit(STATUS_READY, &priv->status),
2767 UCODE_READY_TIMEOUT);
2768 if (!ret) {
2769 if (!test_bit(STATUS_READY, &priv->status)) {
2770 IWL_ERROR("START_ALIVE timeout after %dms.\n",
2771 jiffies_to_msecs(UCODE_READY_TIMEOUT));
2772 ret = -ETIMEDOUT;
2773 goto out_release_irq;
2774 }
Zhu Yi5a669262008-01-14 17:46:18 -08002775 }
Zhu Yi5a669262008-01-14 17:46:18 -08002776
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08002777 priv->is_open = 1;
2778 }
Zhu Yib481de92007-09-25 17:54:57 -07002779 IWL_DEBUG_MAC80211("leave\n");
2780 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08002781
2782out_release_irq:
2783 free_irq(priv->pci_dev->irq, priv);
2784out_disable_msi:
2785 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08002786 pci_disable_device(priv->pci_dev);
2787 priv->is_open = 0;
2788 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08002789 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002790}
2791
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002792static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002793{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002794 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002795
2796 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08002797
Zhu Yie655b9f2008-01-24 02:19:38 -08002798 if (!priv->is_open) {
2799 IWL_DEBUG_MAC80211("leave - skip\n");
2800 return;
2801 }
2802
Zhu Yib481de92007-09-25 17:54:57 -07002803 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08002804
Tomas Winklerfee12472008-04-03 16:05:21 -07002805 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08002806 /* stop mac, cancel any scan request and clear
2807 * RXON_FILTER_ASSOC_MSK BIT
2808 */
Zhu Yi5a669262008-01-14 17:46:18 -08002809 mutex_lock(&priv->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +08002810 iwl_scan_cancel_timeout(priv, 100);
Zhu Yi5a669262008-01-14 17:46:18 -08002811 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08002812 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08002813 }
2814
Zhu Yi5a669262008-01-14 17:46:18 -08002815 iwl4965_down(priv);
2816
2817 flush_workqueue(priv->workqueue);
2818 free_irq(priv->pci_dev->irq, priv);
2819 pci_disable_msi(priv->pci_dev);
2820 pci_save_state(priv->pci_dev);
2821 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08002822
Zhu Yib481de92007-09-25 17:54:57 -07002823 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002824}
2825
Johannes Berge039fa42008-05-15 12:55:29 +02002826static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07002827{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002828 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002829
2830 IWL_DEBUG_MAC80211("enter\n");
2831
2832 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
2833 IWL_DEBUG_MAC80211("leave - monitor\n");
2834 return -1;
2835 }
2836
2837 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02002838 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07002839
Johannes Berge039fa42008-05-15 12:55:29 +02002840 if (iwl_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07002841 dev_kfree_skb_any(skb);
2842
2843 IWL_DEBUG_MAC80211("leave\n");
2844 return 0;
2845}
2846
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002847static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07002848 struct ieee80211_if_init_conf *conf)
2849{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002850 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002851 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07002852 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002853
Johannes Berg32bfd352007-12-19 01:31:26 +01002854 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07002855
Johannes Berg32bfd352007-12-19 01:31:26 +01002856 if (priv->vif) {
2857 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08002858 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07002859 }
2860
2861 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01002862 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07002863
2864 spin_unlock_irqrestore(&priv->lock, flags);
2865
2866 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02002867
2868 if (conf->mac_addr) {
2869 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
2870 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
2871 }
Zhu Yib481de92007-09-25 17:54:57 -07002872
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002873 if (iwl4965_set_mode(priv, conf->type) == -EAGAIN)
2874 /* we are not ready, will run again when ready */
2875 set_bit(STATUS_MODE_PENDING, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08002876
Zhu Yib481de92007-09-25 17:54:57 -07002877 mutex_unlock(&priv->mutex);
2878
Zhu Yi5a669262008-01-14 17:46:18 -08002879 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002880 return 0;
2881}
2882
2883/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002884 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07002885 *
2886 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
2887 * be set inappropriately and the driver currently sets the hardware up to
2888 * use it whenever needed.
2889 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002890static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07002891{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002892 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002893 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002894 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08002895 int ret = 0;
Tomas Winkler82a66bb2008-05-29 16:35:28 +08002896 u16 channel;
Zhu Yib481de92007-09-25 17:54:57 -07002897
2898 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002899 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002900
Zhu Yi12342c42007-12-20 11:27:32 +08002901 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
2902
Emmanuel Grumbach14a08a72008-06-13 15:44:55 +08002903 if (conf->radio_enabled && iwl_radio_kill_sw_enable_radio(priv)) {
Mohamed Abbas64e72c32008-06-12 09:47:03 +08002904 IWL_DEBUG_MAC80211("leave - RF-KILL - waiting for uCode\n");
Emmanuel Grumbach14a08a72008-06-13 15:44:55 +08002905 goto out;
Mohamed Abbas64e72c32008-06-12 09:47:03 +08002906 }
2907
Emmanuel Grumbach14a08a72008-06-13 15:44:55 +08002908 if (!conf->radio_enabled)
2909 iwl_radio_kill_sw_disable_radio(priv);
2910
Tomas Winklerfee12472008-04-03 16:05:21 -07002911 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002912 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08002913 ret = -EIO;
2914 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002915 }
2916
Assaf Krauss1ea87392008-03-18 14:57:50 -07002917 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07002918 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08002919 IWL_DEBUG_MAC80211("leave - scanning\n");
2920 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002921 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08002922 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07002923 }
2924
Tomas Winkler82a66bb2008-05-29 16:35:28 +08002925 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
2926 ch_info = iwl_get_channel_info(priv, conf->channel->band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07002927 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07002928 IWL_DEBUG_MAC80211("leave - invalid channel\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08002929 ret = -EINVAL;
2930 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002931 }
2932
Assaf Krauss398f9e72008-06-12 09:47:06 +08002933 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS &&
2934 !is_channel_ibss(ch_info)) {
2935 IWL_ERROR("channel %d in band %d not IBSS channel\n",
2936 conf->channel->hw_value, conf->channel->band);
2937 ret = -EINVAL;
2938 goto out;
2939 }
2940
Tomas Winkler82a66bb2008-05-29 16:35:28 +08002941 spin_lock_irqsave(&priv->lock, flags);
2942
Tomas Winkler78330fd2008-02-06 02:37:18 +02002943 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07002944 * from any ht related info since 2.4 does not
2945 * support ht */
Tomas Winkler82a66bb2008-05-29 16:35:28 +08002946 if ((le16_to_cpu(priv->staging_rxon.channel) != channel)
Zhu Yib481de92007-09-25 17:54:57 -07002947#ifdef IEEE80211_CONF_CHANNEL_SWITCH
2948 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
2949#endif
2950 )
2951 priv->staging_rxon.flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002952
Tomas Winkler82a66bb2008-05-29 16:35:28 +08002953 iwl_set_rxon_channel(priv, conf->channel->band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07002954
Tomas Winkler82a66bb2008-05-29 16:35:28 +08002955 iwl_set_flags_for_band(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07002956
2957 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01002958 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07002959 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002960 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002961
2962 spin_unlock_irqrestore(&priv->lock, flags);
2963
2964#ifdef IEEE80211_CONF_CHANNEL_SWITCH
2965 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002966 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08002967 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002968 }
2969#endif
2970
Zhu Yib481de92007-09-25 17:54:57 -07002971 if (!conf->radio_enabled) {
2972 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08002973 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002974 }
2975
Tomas Winklerfee12472008-04-03 16:05:21 -07002976 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002977 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08002978 ret = -EIO;
2979 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002980 }
2981
Tomas Winkler630fe9b2008-06-12 09:47:08 +08002982 IWL_DEBUG_MAC80211("TX Power old=%d new=%d\n",
2983 priv->tx_power_user_lmt, conf->power_level);
2984
2985 iwl_set_tx_power(priv, conf->power_level, false);
2986
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002987 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002988
2989 if (memcmp(&priv->active_rxon,
2990 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002991 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002992 else
2993 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
2994
2995 IWL_DEBUG_MAC80211("leave\n");
2996
Zhu Yia0646472007-12-20 14:10:01 +08002997out:
2998 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08002999 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08003000 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003001}
3002
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003003static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003004{
Tomas Winkler857485c2008-03-21 13:53:44 -07003005 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003006
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003007 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003008 return;
3009
3010 /* The following should be done only at AP bring up */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +08003011 if (!(iwl_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07003012
3013 /* RXON - unassoc (to set timing command) */
3014 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003015 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003016
3017 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003018 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
3019 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07003020 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07003021 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07003022 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07003023 IWL_WARNING("REPLY_RXON_TIMING failed - "
3024 "Attempting to continue.\n");
3025
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07003026 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003027
3028 /* FIXME: what should be the assoc_id for AP? */
3029 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
3030 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3031 priv->staging_rxon.flags |=
3032 RXON_FLG_SHORT_PREAMBLE_MSK;
3033 else
3034 priv->staging_rxon.flags &=
3035 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3036
3037 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
3038 if (priv->assoc_capability &
3039 WLAN_CAPABILITY_SHORT_SLOT_TIME)
3040 priv->staging_rxon.flags |=
3041 RXON_FLG_SHORT_SLOT_MSK;
3042 else
3043 priv->staging_rxon.flags &=
3044 ~RXON_FLG_SHORT_SLOT_MSK;
3045
3046 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
3047 priv->staging_rxon.flags &=
3048 ~RXON_FLG_SHORT_SLOT_MSK;
3049 }
3050 /* restore RXON assoc */
3051 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003052 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003053 iwl4965_activate_qos(priv, 1);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08003054 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08003055 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003056 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003057
3058 /* FIXME - we need to add code here to detect a totally new
3059 * configuration, reset the AP, unassoc, rxon timing, assoc,
3060 * clear sta table, add BCAST sta... */
3061}
3062
Johannes Berg32bfd352007-12-19 01:31:26 +01003063static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
3064 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07003065 struct ieee80211_if_conf *conf)
3066{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003067 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07003068 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07003069 unsigned long flags;
3070 int rc;
3071
3072 if (conf == NULL)
3073 return -EIO;
3074
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08003075 if (priv->vif != vif) {
3076 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08003077 return 0;
3078 }
3079
Zhu Yib481de92007-09-25 17:54:57 -07003080 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3081 (!conf->beacon || !conf->ssid_len)) {
3082 IWL_DEBUG_MAC80211
3083 ("Leaving in AP mode because HostAPD is not ready.\n");
3084 return 0;
3085 }
3086
Tomas Winklerfee12472008-04-03 16:05:21 -07003087 if (!iwl_is_alive(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08003088 return -EAGAIN;
3089
Zhu Yib481de92007-09-25 17:54:57 -07003090 mutex_lock(&priv->mutex);
3091
Zhu Yib481de92007-09-25 17:54:57 -07003092 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07003093 IWL_DEBUG_MAC80211("bssid: %s\n",
3094 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07003095
Johannes Berg4150c572007-09-17 01:29:23 -04003096/*
3097 * very dubious code was here; the probe filtering flag is never set:
3098 *
Zhu Yib481de92007-09-25 17:54:57 -07003099 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
3100 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04003101 */
Zhu Yib481de92007-09-25 17:54:57 -07003102
3103 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
3104 if (!conf->bssid) {
3105 conf->bssid = priv->mac_addr;
3106 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07003107 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
3108 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07003109 }
3110 if (priv->ibss_beacon)
3111 dev_kfree_skb(priv->ibss_beacon);
3112
3113 priv->ibss_beacon = conf->beacon;
3114 }
3115
Tomas Winklerfee12472008-04-03 16:05:21 -07003116 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08003117 goto done;
3118
Zhu Yib481de92007-09-25 17:54:57 -07003119 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
3120 !is_multicast_ether_addr(conf->bssid)) {
3121 /* If there is currently a HW scan going on in the background
3122 * then we need to cancel it else the RXON below will fail. */
Tomas Winkler2a421b92008-06-12 09:47:10 +08003123 if (iwl_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07003124 IWL_WARNING("Aborted scan still in progress "
3125 "after 100ms\n");
3126 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
3127 mutex_unlock(&priv->mutex);
3128 return -EAGAIN;
3129 }
3130 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
3131
3132 /* TODO: Audit driver for usage of these members and see
3133 * if mac80211 deprecates them (priv->bssid looks like it
3134 * shouldn't be there, but I haven't scanned the IBSS code
3135 * to verify) - jpk */
3136 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
3137
3138 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003139 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003140 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003141 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003142 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08003143 iwl_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07003144 priv, priv->active_rxon.bssid_addr, 1);
3145 }
3146
3147 } else {
Tomas Winkler2a421b92008-06-12 09:47:10 +08003148 iwl_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07003149 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003150 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003151 }
3152
Mohamed Abbasfde35712007-11-29 11:10:15 +08003153 done:
Zhu Yib481de92007-09-25 17:54:57 -07003154 spin_lock_irqsave(&priv->lock, flags);
3155 if (!conf->ssid_len)
3156 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
3157 else
3158 memcpy(priv->essid, conf->ssid, conf->ssid_len);
3159
3160 priv->essid_len = conf->ssid_len;
3161 spin_unlock_irqrestore(&priv->lock, flags);
3162
3163 IWL_DEBUG_MAC80211("leave\n");
3164 mutex_unlock(&priv->mutex);
3165
3166 return 0;
3167}
3168
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003169static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04003170 unsigned int changed_flags,
3171 unsigned int *total_flags,
3172 int mc_count, struct dev_addr_list *mc_list)
3173{
3174 /*
3175 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003176 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04003177 */
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08003178 struct iwl_priv *priv = hw->priv;
3179 int new_flags = 0;
3180 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
3181 if (*total_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
3182 IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
3183 IEEE80211_IF_TYPE_MNTR,
3184 changed_flags, *total_flags);
3185 /* queue work 'cuz mac80211 is holding a lock which
3186 * prevents us from issuing (synchronous) f/w cmds */
3187 queue_work(priv->workqueue, &priv->set_monitor);
3188 new_flags &= FIF_PROMISC_IN_BSS |
3189 FIF_OTHER_BSS |
3190 FIF_ALLMULTI;
3191 }
3192 }
3193 *total_flags = new_flags;
Johannes Berg4150c572007-09-17 01:29:23 -04003194}
3195
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003196static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003197 struct ieee80211_if_init_conf *conf)
3198{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003199 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003200
3201 IWL_DEBUG_MAC80211("enter\n");
3202
3203 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08003204
Tomas Winklerfee12472008-04-03 16:05:21 -07003205 if (iwl_is_ready_rf(priv)) {
Tomas Winkler2a421b92008-06-12 09:47:10 +08003206 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003207 cancel_delayed_work(&priv->post_associate);
3208 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3209 iwl4965_commit_rxon(priv);
3210 }
Johannes Berg32bfd352007-12-19 01:31:26 +01003211 if (priv->vif == conf->vif) {
3212 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003213 memset(priv->bssid, 0, ETH_ALEN);
3214 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
3215 priv->essid_len = 0;
3216 }
3217 mutex_unlock(&priv->mutex);
3218
3219 IWL_DEBUG_MAC80211("leave\n");
3220
3221}
Johannes Berg471b3ef2007-12-28 14:32:58 +01003222
Tomas Winkler3109ece2008-03-28 16:33:35 -07003223#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01003224static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
3225 struct ieee80211_vif *vif,
3226 struct ieee80211_bss_conf *bss_conf,
3227 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02003228{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003229 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02003230
Tomas Winkler3109ece2008-03-28 16:33:35 -07003231 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
3232
Johannes Berg471b3ef2007-12-28 14:32:58 +01003233 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07003234 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
3235 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01003236 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02003237 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
3238 else
3239 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
3240 }
3241
Johannes Berg471b3ef2007-12-28 14:32:58 +01003242 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07003243 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01003244 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02003245 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
3246 else
3247 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
3248 }
3249
Tomas Winkler98952d52008-03-28 16:33:33 -07003250 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07003251 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07003252 iwl4965_ht_conf(priv, bss_conf);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07003253 iwl_set_rxon_chain(priv);
Tomas Winkler98952d52008-03-28 16:33:33 -07003254 }
3255
Johannes Berg471b3ef2007-12-28 14:32:58 +01003256 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07003257 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
Reinette Chatre508e32e2008-04-14 21:16:13 -07003258 /* This should never happen as this function should
3259 * never be called from interrupt context. */
3260 if (WARN_ON_ONCE(in_interrupt()))
3261 return;
Tomas Winkler3109ece2008-03-28 16:33:35 -07003262 if (bss_conf->assoc) {
3263 priv->assoc_id = bss_conf->aid;
3264 priv->beacon_int = bss_conf->beacon_int;
3265 priv->timestamp = bss_conf->timestamp;
3266 priv->assoc_capability = bss_conf->assoc_capability;
3267 priv->next_scan_jiffies = jiffies +
3268 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
Reinette Chatre508e32e2008-04-14 21:16:13 -07003269 mutex_lock(&priv->mutex);
3270 iwl4965_post_associate(priv);
3271 mutex_unlock(&priv->mutex);
Tomas Winkler3109ece2008-03-28 16:33:35 -07003272 } else {
3273 priv->assoc_id = 0;
3274 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
3275 }
3276 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
3277 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
Tomas Winkler7e8c5192008-04-15 16:01:43 -07003278 iwl_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01003279 }
3280
Tomas Winkler220173b2007-10-16 00:50:25 +02003281}
Zhu Yib481de92007-09-25 17:54:57 -07003282
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003283static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07003284{
3285 int rc = 0;
3286 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003287 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003288
3289 IWL_DEBUG_MAC80211("enter\n");
3290
mabbas052c4b92007-10-25 17:15:43 +08003291 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07003292 spin_lock_irqsave(&priv->lock, flags);
3293
Tomas Winklerfee12472008-04-03 16:05:21 -07003294 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003295 rc = -EIO;
3296 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
3297 goto out_unlock;
3298 }
3299
3300 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
3301 rc = -EIO;
3302 IWL_ERROR("ERROR: APs don't scan\n");
3303 goto out_unlock;
3304 }
3305
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003306 /* we don't schedule scan within next_scan_jiffies period */
3307 if (priv->next_scan_jiffies &&
3308 time_after(priv->next_scan_jiffies, jiffies)) {
3309 rc = -EAGAIN;
3310 goto out_unlock;
3311 }
Zhu Yib481de92007-09-25 17:54:57 -07003312 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003313 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
3314 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07003315 rc = -EAGAIN;
3316 goto out_unlock;
3317 }
3318 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003319 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Tomas Winkler2a421b92008-06-12 09:47:10 +08003320 iwl_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07003321
3322 priv->one_direct_scan = 1;
3323 priv->direct_ssid_len = (u8)
3324 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
3325 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08003326 } else
3327 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003328
Tomas Winkler2a421b92008-06-12 09:47:10 +08003329 rc = iwl_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003330
3331 IWL_DEBUG_MAC80211("leave\n");
3332
3333out_unlock:
3334 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08003335 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07003336
3337 return rc;
3338}
3339
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003340static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
3341 struct ieee80211_key_conf *keyconf, const u8 *addr,
3342 u32 iv32, u16 *phase1key)
3343{
3344 struct iwl_priv *priv = hw->priv;
3345 u8 sta_id = IWL_INVALID_STATION;
3346 unsigned long flags;
3347 __le16 key_flags = 0;
3348 int i;
3349 DECLARE_MAC_BUF(mac);
3350
3351 IWL_DEBUG_MAC80211("enter\n");
3352
Tomas Winkler947b13a2008-04-16 16:34:48 -07003353 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003354 if (sta_id == IWL_INVALID_STATION) {
3355 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
3356 print_mac(mac, addr));
3357 return;
3358 }
3359
Tomas Winkler2a421b92008-06-12 09:47:10 +08003360 iwl_scan_cancel_timeout(priv, 100);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003361
3362 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
3363 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
3364 key_flags &= ~STA_KEY_FLG_INVALID;
3365
Tomas Winkler5425e492008-04-15 16:01:38 -07003366 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003367 key_flags |= STA_KEY_MULTICAST_MSK;
3368
3369 spin_lock_irqsave(&priv->sta_lock, flags);
3370
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003371 priv->stations[sta_id].sta.key.key_flags = key_flags;
3372 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
3373
3374 for (i = 0; i < 5; i++)
3375 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
3376 cpu_to_le16(phase1key[i]);
3377
3378 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3379 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3380
Tomas Winkler133636d2008-05-05 10:22:34 +08003381 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02003382
3383 spin_unlock_irqrestore(&priv->sta_lock, flags);
3384
3385 IWL_DEBUG_MAC80211("leave\n");
3386}
3387
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003388static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07003389 const u8 *local_addr, const u8 *addr,
3390 struct ieee80211_key_conf *key)
3391{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003392 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07003393 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003394 int ret = 0;
3395 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003396 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003397
3398 IWL_DEBUG_MAC80211("enter\n");
3399
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07003400 if (priv->hw_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07003401 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
3402 return -EOPNOTSUPP;
3403 }
3404
3405 if (is_zero_ether_addr(addr))
3406 /* only support pairwise keys */
3407 return -EOPNOTSUPP;
3408
Tomas Winkler947b13a2008-04-16 16:34:48 -07003409 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003410 if (sta_id == IWL_INVALID_STATION) {
3411 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
3412 print_mac(mac, addr));
3413 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003414
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003415 }
Zhu Yib481de92007-09-25 17:54:57 -07003416
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003417 mutex_lock(&priv->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +08003418 iwl_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003419 mutex_unlock(&priv->mutex);
3420
3421 /* If we are getting WEP group key and we didn't receive any key mapping
3422 * so far, we are in legacy wep mode (group key only), otherwise we are
3423 * in 1X mode.
3424 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07003425 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003426 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
3427 if (cmd == SET_KEY)
3428 is_default_wep_key = !priv->key_mapping_key;
3429 else
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08003430 is_default_wep_key =
3431 (key->hw_key_idx == HW_KEY_DEFAULT);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003432 }
mabbas052c4b92007-10-25 17:15:43 +08003433
Zhu Yib481de92007-09-25 17:54:57 -07003434 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003435 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003436 if (is_default_wep_key)
3437 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003438 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07003439 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003440
3441 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003442 break;
3443 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07003444 if (is_default_wep_key)
3445 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003446 else
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07003447 ret = iwl_remove_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003448
3449 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003450 break;
3451 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003452 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003453 }
3454
3455 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003456
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07003457 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003458}
3459
Johannes Berge100bb62008-04-30 18:51:21 +02003460static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07003461 const struct ieee80211_tx_queue_params *params)
3462{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003463 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003464 unsigned long flags;
3465 int q;
Zhu Yib481de92007-09-25 17:54:57 -07003466
3467 IWL_DEBUG_MAC80211("enter\n");
3468
Tomas Winklerfee12472008-04-03 16:05:21 -07003469 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003470 IWL_DEBUG_MAC80211("leave - RF not ready\n");
3471 return -EIO;
3472 }
3473
3474 if (queue >= AC_NUM) {
3475 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
3476 return 0;
3477 }
3478
Zhu Yib481de92007-09-25 17:54:57 -07003479 if (!priv->qos_data.qos_enable) {
3480 priv->qos_data.qos_active = 0;
3481 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
3482 return 0;
3483 }
3484 q = AC_NUM - 1 - queue;
3485
3486 spin_lock_irqsave(&priv->lock, flags);
3487
3488 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
3489 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
3490 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
3491 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01003492 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07003493
3494 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
3495 priv->qos_data.qos_active = 1;
3496
3497 spin_unlock_irqrestore(&priv->lock, flags);
3498
3499 mutex_lock(&priv->mutex);
3500 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003501 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07003502 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003503 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003504
3505 mutex_unlock(&priv->mutex);
3506
Zhu Yib481de92007-09-25 17:54:57 -07003507 IWL_DEBUG_MAC80211("leave\n");
3508 return 0;
3509}
3510
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003511static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003512 struct ieee80211_tx_queue_stats *stats)
3513{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003514 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003515 int i, avail;
Ron Rindjunsky16466902008-05-05 10:22:50 +08003516 struct iwl_tx_queue *txq;
Tomas Winkler443cfd42008-05-15 13:53:57 +08003517 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07003518 unsigned long flags;
3519
3520 IWL_DEBUG_MAC80211("enter\n");
3521
Tomas Winklerfee12472008-04-03 16:05:21 -07003522 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003523 IWL_DEBUG_MAC80211("leave - RF not ready\n");
3524 return -EIO;
3525 }
3526
3527 spin_lock_irqsave(&priv->lock, flags);
3528
3529 for (i = 0; i < AC_NUM; i++) {
3530 txq = &priv->txq[i];
3531 q = &txq->q;
Tomas Winkler443cfd42008-05-15 13:53:57 +08003532 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07003533
Johannes Berg57ffc582008-04-29 17:18:59 +02003534 stats[i].len = q->n_window - avail;
3535 stats[i].limit = q->n_window - q->high_mark;
3536 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07003537
3538 }
3539 spin_unlock_irqrestore(&priv->lock, flags);
3540
3541 IWL_DEBUG_MAC80211("leave\n");
3542
3543 return 0;
3544}
3545
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003546static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003547 struct ieee80211_low_level_stats *stats)
3548{
Ester Kummerbf403db2008-05-05 10:22:40 +08003549 struct iwl_priv *priv = hw->priv;
3550
3551 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003552 IWL_DEBUG_MAC80211("enter\n");
3553 IWL_DEBUG_MAC80211("leave\n");
3554
3555 return 0;
3556}
3557
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003558static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003559{
Ester Kummerbf403db2008-05-05 10:22:40 +08003560 struct iwl_priv *priv;
3561
3562 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003563 IWL_DEBUG_MAC80211("enter\n");
3564 IWL_DEBUG_MAC80211("leave\n");
3565
3566 return 0;
3567}
3568
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003569static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003570{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003571 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003572 unsigned long flags;
3573
3574 mutex_lock(&priv->mutex);
3575 IWL_DEBUG_MAC80211("enter\n");
3576
3577 priv->lq_mngr.lq_ready = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003578 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02003579 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07003580 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -07003581
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07003582 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003583
3584 cancel_delayed_work(&priv->post_associate);
3585
3586 spin_lock_irqsave(&priv->lock, flags);
3587 priv->assoc_id = 0;
3588 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003589 priv->assoc_station_added = 0;
3590
3591 /* new association get rid of ibss beacon skb */
3592 if (priv->ibss_beacon)
3593 dev_kfree_skb(priv->ibss_beacon);
3594
3595 priv->ibss_beacon = NULL;
3596
3597 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07003598 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003599 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
3600 priv->beacon_int = 0;
3601
3602 spin_unlock_irqrestore(&priv->lock, flags);
3603
Tomas Winklerfee12472008-04-03 16:05:21 -07003604 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08003605 IWL_DEBUG_MAC80211("leave - not ready\n");
3606 mutex_unlock(&priv->mutex);
3607 return;
3608 }
3609
mabbas052c4b92007-10-25 17:15:43 +08003610 /* we are restarting association process
3611 * clear RXON_FILTER_ASSOC_MSK bit
3612 */
3613 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Tomas Winkler2a421b92008-06-12 09:47:10 +08003614 iwl_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08003615 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003616 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08003617 }
3618
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -07003619 iwl_power_update_mode(priv, 0);
3620
Zhu Yib481de92007-09-25 17:54:57 -07003621 /* Per mac80211.h: This is only used in IBSS mode... */
3622 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08003623
Zhu Yib481de92007-09-25 17:54:57 -07003624 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
3625 mutex_unlock(&priv->mutex);
3626 return;
3627 }
3628
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003629 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003630
3631 mutex_unlock(&priv->mutex);
3632
3633 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003634}
3635
Johannes Berge039fa42008-05-15 12:55:29 +02003636static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003637{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003638 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003639 unsigned long flags;
3640
3641 mutex_lock(&priv->mutex);
3642 IWL_DEBUG_MAC80211("enter\n");
3643
Tomas Winklerfee12472008-04-03 16:05:21 -07003644 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003645 IWL_DEBUG_MAC80211("leave - RF not ready\n");
3646 mutex_unlock(&priv->mutex);
3647 return -EIO;
3648 }
3649
3650 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
3651 IWL_DEBUG_MAC80211("leave - not IBSS\n");
3652 mutex_unlock(&priv->mutex);
3653 return -EIO;
3654 }
3655
3656 spin_lock_irqsave(&priv->lock, flags);
3657
3658 if (priv->ibss_beacon)
3659 dev_kfree_skb(priv->ibss_beacon);
3660
3661 priv->ibss_beacon = skb;
3662
3663 priv->assoc_id = 0;
3664
3665 IWL_DEBUG_MAC80211("leave\n");
3666 spin_unlock_irqrestore(&priv->lock, flags);
3667
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07003668 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003669
Assaf Kraussc46fbef2008-06-12 09:47:05 +08003670 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003671
3672 mutex_unlock(&priv->mutex);
3673
3674 return 0;
3675}
3676
Zhu Yib481de92007-09-25 17:54:57 -07003677/*****************************************************************************
3678 *
3679 * sysfs attributes
3680 *
3681 *****************************************************************************/
3682
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003683#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003684
3685/*
3686 * The following adds a new attribute to the sysfs representation
3687 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3688 * used for controlling the debug level.
3689 *
3690 * See the level definitions in iwl for details.
3691 */
3692
Ester Kummer8cf769c2008-05-06 11:05:11 +08003693static ssize_t show_debug_level(struct device *d,
3694 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003695{
Ester Kummer8cf769c2008-05-06 11:05:11 +08003696 struct iwl_priv *priv = d->driver_data;
3697
3698 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003699}
Ester Kummer8cf769c2008-05-06 11:05:11 +08003700static ssize_t store_debug_level(struct device *d,
3701 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003702 const char *buf, size_t count)
3703{
Ester Kummer8cf769c2008-05-06 11:05:11 +08003704 struct iwl_priv *priv = d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003705 char *p = (char *)buf;
3706 u32 val;
3707
3708 val = simple_strtoul(p, &p, 0);
3709 if (p == buf)
3710 printk(KERN_INFO DRV_NAME
3711 ": %s is not in hex or decimal form.\n", buf);
3712 else
Ester Kummer8cf769c2008-05-06 11:05:11 +08003713 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07003714
3715 return strnlen(buf, count);
3716}
3717
Ester Kummer8cf769c2008-05-06 11:05:11 +08003718static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3719 show_debug_level, store_debug_level);
3720
Zhu Yib481de92007-09-25 17:54:57 -07003721
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003722#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003723
Zhu Yib481de92007-09-25 17:54:57 -07003724
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08003725static ssize_t show_version(struct device *d,
3726 struct device_attribute *attr, char *buf)
3727{
3728 struct iwl_priv *priv = d->driver_data;
Tomas Winkler885ba202008-05-29 16:34:55 +08003729 struct iwl_alive_resp *palive = &priv->card_alive;
Tomas Winklerf236a262008-06-30 17:23:02 +08003730 ssize_t pos = 0;
3731 u16 eeprom_ver;
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08003732
3733 if (palive->is_valid)
Tomas Winklerf236a262008-06-30 17:23:02 +08003734 pos += sprintf(buf + pos,
3735 "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
3736 "fw type: 0x%01X 0x%01X\n",
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08003737 palive->ucode_major, palive->ucode_minor,
3738 palive->sw_rev[0], palive->sw_rev[1],
3739 palive->ver_type, palive->ver_subtype);
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08003740 else
Tomas Winklerf236a262008-06-30 17:23:02 +08003741 pos += sprintf(buf + pos, "fw not loaded\n");
3742
3743 if (priv->eeprom) {
3744 eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
3745 pos += sprintf(buf + pos, "EEPROM version: 0x%x\n",
3746 eeprom_ver);
3747 } else {
3748 pos += sprintf(buf + pos, "EEPROM not initialzed\n");
3749 }
3750
3751 return pos;
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08003752}
3753
3754static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
3755
Zhu Yib481de92007-09-25 17:54:57 -07003756static ssize_t show_temperature(struct device *d,
3757 struct device_attribute *attr, char *buf)
3758{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003759 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003760
Tomas Winklerfee12472008-04-03 16:05:21 -07003761 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003762 return -EAGAIN;
3763
Emmanuel Grumbach91dbc5b2008-06-12 09:47:14 +08003764 return sprintf(buf, "%d\n", priv->temperature);
Zhu Yib481de92007-09-25 17:54:57 -07003765}
3766
3767static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3768
3769static ssize_t show_rs_window(struct device *d,
3770 struct device_attribute *attr,
3771 char *buf)
3772{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003773 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003774 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003775}
3776static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
3777
3778static ssize_t show_tx_power(struct device *d,
3779 struct device_attribute *attr, char *buf)
3780{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003781 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winkler630fe9b2008-06-12 09:47:08 +08003782 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07003783}
3784
3785static ssize_t store_tx_power(struct device *d,
3786 struct device_attribute *attr,
3787 const char *buf, size_t count)
3788{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003789 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003790 char *p = (char *)buf;
3791 u32 val;
3792
3793 val = simple_strtoul(p, &p, 10);
3794 if (p == buf)
3795 printk(KERN_INFO DRV_NAME
3796 ": %s is not in decimal form.\n", buf);
3797 else
Tomas Winkler630fe9b2008-06-12 09:47:08 +08003798 iwl_set_tx_power(priv, val, false);
Zhu Yib481de92007-09-25 17:54:57 -07003799
3800 return count;
3801}
3802
3803static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3804
3805static ssize_t show_flags(struct device *d,
3806 struct device_attribute *attr, char *buf)
3807{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003808 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003809
3810 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
3811}
3812
3813static ssize_t store_flags(struct device *d,
3814 struct device_attribute *attr,
3815 const char *buf, size_t count)
3816{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003817 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003818 u32 flags = simple_strtoul(buf, NULL, 0);
3819
3820 mutex_lock(&priv->mutex);
3821 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
3822 /* Cancel any currently running scans... */
Tomas Winkler2a421b92008-06-12 09:47:10 +08003823 if (iwl_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07003824 IWL_WARNING("Could not cancel scan.\n");
3825 else {
3826 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
3827 flags);
3828 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003829 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003830 }
3831 }
3832 mutex_unlock(&priv->mutex);
3833
3834 return count;
3835}
3836
3837static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3838
3839static ssize_t show_filter_flags(struct device *d,
3840 struct device_attribute *attr, char *buf)
3841{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003842 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003843
3844 return sprintf(buf, "0x%04X\n",
3845 le32_to_cpu(priv->active_rxon.filter_flags));
3846}
3847
3848static ssize_t store_filter_flags(struct device *d,
3849 struct device_attribute *attr,
3850 const char *buf, size_t count)
3851{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003852 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003853 u32 filter_flags = simple_strtoul(buf, NULL, 0);
3854
3855 mutex_lock(&priv->mutex);
3856 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
3857 /* Cancel any currently running scans... */
Tomas Winkler2a421b92008-06-12 09:47:10 +08003858 if (iwl_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07003859 IWL_WARNING("Could not cancel scan.\n");
3860 else {
3861 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
3862 "0x%04X\n", filter_flags);
3863 priv->staging_rxon.filter_flags =
3864 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003865 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003866 }
3867 }
3868 mutex_unlock(&priv->mutex);
3869
3870 return count;
3871}
3872
3873static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3874 store_filter_flags);
3875
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003876#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07003877
3878static ssize_t show_measurement(struct device *d,
3879 struct device_attribute *attr, char *buf)
3880{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003881 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003882 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003883 u32 size = sizeof(measure_report), len = 0, ofs = 0;
3884 u8 *data = (u8 *) & measure_report;
3885 unsigned long flags;
3886
3887 spin_lock_irqsave(&priv->lock, flags);
3888 if (!(priv->measurement_status & MEASUREMENT_READY)) {
3889 spin_unlock_irqrestore(&priv->lock, flags);
3890 return 0;
3891 }
3892 memcpy(&measure_report, &priv->measure_report, size);
3893 priv->measurement_status = 0;
3894 spin_unlock_irqrestore(&priv->lock, flags);
3895
3896 while (size && (PAGE_SIZE - len)) {
3897 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3898 PAGE_SIZE - len, 1);
3899 len = strlen(buf);
3900 if (PAGE_SIZE - len)
3901 buf[len++] = '\n';
3902
3903 ofs += 16;
3904 size -= min(size, 16U);
3905 }
3906
3907 return len;
3908}
3909
3910static ssize_t store_measurement(struct device *d,
3911 struct device_attribute *attr,
3912 const char *buf, size_t count)
3913{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003914 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003915 struct ieee80211_measurement_params params = {
3916 .channel = le16_to_cpu(priv->active_rxon.channel),
3917 .start_time = cpu_to_le64(priv->last_tsf),
3918 .duration = cpu_to_le16(1),
3919 };
3920 u8 type = IWL_MEASURE_BASIC;
3921 u8 buffer[32];
3922 u8 channel;
3923
3924 if (count) {
3925 char *p = buffer;
3926 strncpy(buffer, buf, min(sizeof(buffer), count));
3927 channel = simple_strtoul(p, NULL, 0);
3928 if (channel)
3929 params.channel = channel;
3930
3931 p = buffer;
3932 while (*p && *p != ' ')
3933 p++;
3934 if (*p)
3935 type = simple_strtoul(p + 1, NULL, 0);
3936 }
3937
3938 IWL_DEBUG_INFO("Invoking measurement of type %d on "
3939 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003940 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07003941
3942 return count;
3943}
3944
3945static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
3946 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003947#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07003948
3949static ssize_t store_retry_rate(struct device *d,
3950 struct device_attribute *attr,
3951 const char *buf, size_t count)
3952{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003953 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003954
3955 priv->retry_rate = simple_strtoul(buf, NULL, 0);
3956 if (priv->retry_rate <= 0)
3957 priv->retry_rate = 1;
3958
3959 return count;
3960}
3961
3962static ssize_t show_retry_rate(struct device *d,
3963 struct device_attribute *attr, char *buf)
3964{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003965 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003966 return sprintf(buf, "%d", priv->retry_rate);
3967}
3968
3969static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
3970 store_retry_rate);
3971
3972static ssize_t store_power_level(struct device *d,
3973 struct device_attribute *attr,
3974 const char *buf, size_t count)
3975{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003976 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003977 int rc;
3978 int mode;
3979
3980 mode = simple_strtoul(buf, NULL, 0);
3981 mutex_lock(&priv->mutex);
3982
Tomas Winklerfee12472008-04-03 16:05:21 -07003983 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003984 rc = -EAGAIN;
3985 goto out;
3986 }
3987
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -07003988 rc = iwl_power_set_user_mode(priv, mode);
3989 if (rc) {
3990 IWL_DEBUG_MAC80211("failed setting power mode.\n");
3991 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003992 }
Zhu Yib481de92007-09-25 17:54:57 -07003993 rc = count;
3994
3995 out:
3996 mutex_unlock(&priv->mutex);
3997 return rc;
3998}
3999
4000#define MAX_WX_STRING 80
4001
4002/* Values are in microsecond */
4003static const s32 timeout_duration[] = {
4004 350000,
4005 250000,
4006 75000,
4007 37000,
4008 25000,
4009};
4010static const s32 period_duration[] = {
4011 400000,
4012 700000,
4013 1000000,
4014 1000000,
4015 1000000
4016};
4017
4018static ssize_t show_power_level(struct device *d,
4019 struct device_attribute *attr, char *buf)
4020{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004021 struct iwl_priv *priv = dev_get_drvdata(d);
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -07004022 int level = priv->power_data.power_mode;
Zhu Yib481de92007-09-25 17:54:57 -07004023 char *p = buf;
4024
4025 p += sprintf(p, "%d ", level);
4026 switch (level) {
4027 case IWL_POWER_MODE_CAM:
4028 case IWL_POWER_AC:
4029 p += sprintf(p, "(AC)");
4030 break;
4031 case IWL_POWER_BATTERY:
4032 p += sprintf(p, "(BATTERY)");
4033 break;
4034 default:
4035 p += sprintf(p,
4036 "(Timeout %dms, Period %dms)",
4037 timeout_duration[level - 1] / 1000,
4038 period_duration[level - 1] / 1000);
4039 }
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -07004040/*
Zhu Yib481de92007-09-25 17:54:57 -07004041 if (!(priv->power_mode & IWL_POWER_ENABLED))
4042 p += sprintf(p, " OFF\n");
4043 else
4044 p += sprintf(p, " \n");
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -07004045*/
4046 p += sprintf(p, " \n");
Zhu Yib481de92007-09-25 17:54:57 -07004047 return (p - buf + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004048}
4049
4050static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
4051 store_power_level);
4052
4053static ssize_t show_channels(struct device *d,
4054 struct device_attribute *attr, char *buf)
4055{
Ester Kummer5d72a1f2008-06-13 15:44:53 +08004056
4057 struct iwl_priv *priv = dev_get_drvdata(d);
4058 struct ieee80211_channel *channels = NULL;
4059 const struct ieee80211_supported_band *supp_band = NULL;
4060 int len = 0, i;
4061 int count = 0;
4062
4063 if (!test_bit(STATUS_GEO_CONFIGURED, &priv->status))
4064 return -EAGAIN;
4065
4066 supp_band = iwl_get_hw_mode(priv, IEEE80211_BAND_2GHZ);
4067 channels = supp_band->channels;
4068 count = supp_band->n_channels;
4069
4070 len += sprintf(&buf[len],
4071 "Displaying %d channels in 2.4GHz band "
4072 "(802.11bg):\n", count);
4073
4074 for (i = 0; i < count; i++)
4075 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
4076 ieee80211_frequency_to_channel(
4077 channels[i].center_freq),
4078 channels[i].max_power,
4079 channels[i].flags & IEEE80211_CHAN_RADAR ?
4080 " (IEEE 802.11h required)" : "",
4081 (!(channels[i].flags & IEEE80211_CHAN_NO_IBSS)
4082 || (channels[i].flags &
4083 IEEE80211_CHAN_RADAR)) ? "" :
4084 ", IBSS",
4085 channels[i].flags &
4086 IEEE80211_CHAN_PASSIVE_SCAN ?
4087 "passive only" : "active/passive");
4088
4089 supp_band = iwl_get_hw_mode(priv, IEEE80211_BAND_5GHZ);
4090 channels = supp_band->channels;
4091 count = supp_band->n_channels;
4092
4093 len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
4094 "(802.11a):\n", count);
4095
4096 for (i = 0; i < count; i++)
4097 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
4098 ieee80211_frequency_to_channel(
4099 channels[i].center_freq),
4100 channels[i].max_power,
4101 channels[i].flags & IEEE80211_CHAN_RADAR ?
4102 " (IEEE 802.11h required)" : "",
4103 ((channels[i].flags & IEEE80211_CHAN_NO_IBSS)
4104 || (channels[i].flags &
4105 IEEE80211_CHAN_RADAR)) ? "" :
4106 ", IBSS",
4107 channels[i].flags &
4108 IEEE80211_CHAN_PASSIVE_SCAN ?
4109 "passive only" : "active/passive");
4110
4111 return len;
Zhu Yib481de92007-09-25 17:54:57 -07004112}
4113
4114static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
4115
4116static ssize_t show_statistics(struct device *d,
4117 struct device_attribute *attr, char *buf)
4118{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004119 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004120 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07004121 u32 len = 0, ofs = 0;
4122 u8 *data = (u8 *) & priv->statistics;
4123 int rc = 0;
4124
Tomas Winklerfee12472008-04-03 16:05:21 -07004125 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004126 return -EAGAIN;
4127
4128 mutex_lock(&priv->mutex);
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07004129 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004130 mutex_unlock(&priv->mutex);
4131
4132 if (rc) {
4133 len = sprintf(buf,
4134 "Error sending statistics request: 0x%08X\n", rc);
4135 return len;
4136 }
4137
4138 while (size && (PAGE_SIZE - len)) {
4139 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4140 PAGE_SIZE - len, 1);
4141 len = strlen(buf);
4142 if (PAGE_SIZE - len)
4143 buf[len++] = '\n';
4144
4145 ofs += 16;
4146 size -= min(size, 16U);
4147 }
4148
4149 return len;
4150}
4151
4152static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
4153
Zhu Yib481de92007-09-25 17:54:57 -07004154static ssize_t show_status(struct device *d,
4155 struct device_attribute *attr, char *buf)
4156{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004157 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07004158 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004159 return -EAGAIN;
4160 return sprintf(buf, "0x%08x\n", (int)priv->status);
4161}
4162
4163static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
4164
Zhu Yib481de92007-09-25 17:54:57 -07004165/*****************************************************************************
4166 *
4167 * driver setup and teardown
4168 *
4169 *****************************************************************************/
4170
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004171static void iwl_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004172{
4173 priv->workqueue = create_workqueue(DRV_NAME);
4174
4175 init_waitqueue_head(&priv->wait_command_queue);
4176
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004177 INIT_WORK(&priv->up, iwl4965_bg_up);
4178 INIT_WORK(&priv->restart, iwl4965_bg_restart);
4179 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004180 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
4181 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08004182 INIT_WORK(&priv->set_monitor, iwl4965_bg_set_monitor);
Emmanuel Grumbach16e727e2008-06-12 09:46:52 +08004183 INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004184 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08004185 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
4186 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
Tomas Winkler2a421b92008-06-12 09:47:10 +08004187
4188 /* FIXME : remove when resolved PENDING */
4189 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
4190 iwl_setup_scan_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004191
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004192 if (priv->cfg->ops->lib->setup_deferred_work)
4193 priv->cfg->ops->lib->setup_deferred_work(priv);
4194
4195 init_timer(&priv->statistics_periodic);
4196 priv->statistics_periodic.data = (unsigned long)priv;
4197 priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
Zhu Yib481de92007-09-25 17:54:57 -07004198
4199 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004200 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07004201}
4202
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004203static void iwl_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004204{
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004205 if (priv->cfg->ops->lib->cancel_deferred_work)
4206 priv->cfg->ops->lib->cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004207
Joonwoo Park3ae6a052007-11-29 10:43:16 +09004208 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07004209 cancel_delayed_work(&priv->scan_check);
4210 cancel_delayed_work(&priv->alive_start);
4211 cancel_delayed_work(&priv->post_associate);
4212 cancel_work_sync(&priv->beacon_update);
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004213 del_timer_sync(&priv->statistics_periodic);
Zhu Yib481de92007-09-25 17:54:57 -07004214}
4215
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004216static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07004217 &dev_attr_channels.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004218 &dev_attr_flags.attr,
4219 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004220#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07004221 &dev_attr_measurement.attr,
4222#endif
4223 &dev_attr_power_level.attr,
4224 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004225 &dev_attr_rs_window.attr,
4226 &dev_attr_statistics.attr,
4227 &dev_attr_status.attr,
4228 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004229 &dev_attr_tx_power.attr,
Ester Kummer8cf769c2008-05-06 11:05:11 +08004230#ifdef CONFIG_IWLWIFI_DEBUG
4231 &dev_attr_debug_level.attr,
4232#endif
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08004233 &dev_attr_version.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004234
4235 NULL
4236};
4237
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004238static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07004239 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004240 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07004241};
4242
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004243static struct ieee80211_ops iwl4965_hw_ops = {
4244 .tx = iwl4965_mac_tx,
4245 .start = iwl4965_mac_start,
4246 .stop = iwl4965_mac_stop,
4247 .add_interface = iwl4965_mac_add_interface,
4248 .remove_interface = iwl4965_mac_remove_interface,
4249 .config = iwl4965_mac_config,
4250 .config_interface = iwl4965_mac_config_interface,
4251 .configure_filter = iwl4965_configure_filter,
4252 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004253 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004254 .get_stats = iwl4965_mac_get_stats,
4255 .get_tx_stats = iwl4965_mac_get_tx_stats,
4256 .conf_tx = iwl4965_mac_conf_tx,
4257 .get_tsf = iwl4965_mac_get_tsf,
4258 .reset_tsf = iwl4965_mac_reset_tsf,
4259 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01004260 .bss_info_changed = iwl4965_bss_info_changed,
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02004261 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004262 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07004263};
4264
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004265static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07004266{
4267 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004268 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07004269 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08004270 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004271 unsigned long flags;
Zhu Yi5a669262008-01-14 17:46:18 -08004272 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07004273
Assaf Krauss316c30d2008-03-14 10:38:46 -07004274 /************************
4275 * 1. Allocating HW data
4276 ************************/
4277
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004278 /* Disabling hardware scan means that mac80211 will perform scans
4279 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07004280 if (cfg->mod_params->disable_hw_scan) {
Ester Kummerbf403db2008-05-05 10:22:40 +08004281 if (cfg->mod_params->debug & IWL_DL_INFO)
4282 dev_printk(KERN_DEBUG, &(pdev->dev),
4283 "Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004284 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07004285 }
4286
Assaf Krauss1d0a0822008-03-14 10:38:48 -07004287 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
4288 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07004289 err = -ENOMEM;
4290 goto out;
4291 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07004292 priv = hw->priv;
4293 /* At this point both hw and priv are allocated. */
4294
Zhu Yib481de92007-09-25 17:54:57 -07004295 SET_IEEE80211_DEV(hw, &pdev->dev);
4296
4297 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08004298 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07004299 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07004300
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004301#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08004302 priv->debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07004303 atomic_set(&priv->restrict_refcnt, 0);
4304#endif
Zhu Yib481de92007-09-25 17:54:57 -07004305
Assaf Krauss316c30d2008-03-14 10:38:46 -07004306 /**************************
4307 * 2. Initializing PCI bus
4308 **************************/
4309 if (pci_enable_device(pdev)) {
4310 err = -ENODEV;
4311 goto out_ieee80211_free_hw;
4312 }
4313
4314 pci_set_master(pdev);
4315
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07004316 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004317 if (!err)
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07004318 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
4319 if (err) {
4320 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
4321 if (!err)
4322 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
4323 /* both attempts failed: */
Assaf Krauss316c30d2008-03-14 10:38:46 -07004324 if (err) {
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07004325 printk(KERN_WARNING "%s: No suitable DMA available.\n",
4326 DRV_NAME);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004327 goto out_pci_disable_device;
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07004328 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07004329 }
4330
4331 err = pci_request_regions(pdev, DRV_NAME);
4332 if (err)
4333 goto out_pci_disable_device;
4334
4335 pci_set_drvdata(pdev, priv);
4336
4337 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4338 * PCI Tx retries from interfering with C3 CPU state */
4339 pci_write_config_byte(pdev, 0x41, 0x00);
4340
4341 /***********************
4342 * 3. Read REV register
4343 ***********************/
4344 priv->hw_base = pci_iomap(pdev, 0, 0);
4345 if (!priv->hw_base) {
4346 err = -ENODEV;
4347 goto out_pci_release_regions;
4348 }
4349
4350 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
4351 (unsigned long long) pci_resource_len(pdev, 0));
4352 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
4353
Tomas Winklerb661c812008-04-23 17:14:54 -07004354 iwl_hw_detect(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004355 printk(KERN_INFO DRV_NAME
Tomas Winklerb661c812008-04-23 17:14:54 -07004356 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
4357 priv->cfg->name, priv->hw_rev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004358
Tomas Winkler91238712008-04-23 17:14:53 -07004359 /* amp init */
4360 err = priv->cfg->ops->lib->apm_ops.init(priv);
4361 if (err < 0) {
4362 IWL_DEBUG_INFO("Failed to init APMG\n");
4363 goto out_iounmap;
4364 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07004365 /*****************
4366 * 4. Read EEPROM
4367 *****************/
Assaf Krauss316c30d2008-03-14 10:38:46 -07004368 /* Read the EEPROM */
4369 err = iwl_eeprom_init(priv);
4370 if (err) {
4371 IWL_ERROR("Unable to init EEPROM\n");
4372 goto out_iounmap;
4373 }
Tomas Winkler8614f362008-04-23 17:14:55 -07004374 err = iwl_eeprom_check_version(priv);
4375 if (err)
4376 goto out_iounmap;
4377
Ron Rindjunsky02883012008-05-15 13:53:53 +08004378 /* extract MAC Address */
Assaf Krauss316c30d2008-03-14 10:38:46 -07004379 iwl_eeprom_get_mac(priv, priv->mac_addr);
4380 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
4381 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
4382
4383 /************************
4384 * 5. Setup HW constants
4385 ************************/
4386 /* Device-specific setup */
Tomas Winkler5425e492008-04-15 16:01:38 -07004387 if (priv->cfg->ops->lib->set_hw_params(priv)) {
4388 IWL_ERROR("failed to set hw parameters\n");
Tomas Winkler073d3f52008-04-21 15:41:52 -07004389 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07004390 }
4391
4392 /*******************
Tomas Winkler6ba87952008-05-15 13:54:17 +08004393 * 6. Setup priv
Assaf Krauss316c30d2008-03-14 10:38:46 -07004394 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07004395
Tomas Winkler6ba87952008-05-15 13:54:17 +08004396 err = iwl_init_drv(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004397 if (err)
Ron Rindjunsky399f49002008-04-23 17:14:56 -07004398 goto out_free_eeprom;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004399 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07004400
4401 /**********************************
4402 * 7. Initialize module parameters
4403 **********************************/
4404
4405 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07004406 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07004407 set_bit(STATUS_RF_KILL_SW, &priv->status);
4408 IWL_DEBUG_INFO("Radio disabled.\n");
4409 }
4410
Assaf Krauss316c30d2008-03-14 10:38:46 -07004411 /********************
4412 * 8. Setup services
4413 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004414 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004415 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004416 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004417
4418 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
4419 if (err) {
4420 IWL_ERROR("failed to create sysfs device attributes\n");
Tomas Winkler6ba87952008-05-15 13:54:17 +08004421 goto out_uninit_drv;
Assaf Krauss316c30d2008-03-14 10:38:46 -07004422 }
4423
Assaf Krauss316c30d2008-03-14 10:38:46 -07004424
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08004425 iwl_setup_deferred_work(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004426 iwl4965_setup_rx_handlers(priv);
4427
4428 /********************
4429 * 9. Conclude
4430 ********************/
Zhu Yi5a669262008-01-14 17:46:18 -08004431 pci_save_state(pdev);
4432 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004433
Tomas Winkler6ba87952008-05-15 13:54:17 +08004434 /**********************************
4435 * 10. Setup and register mac80211
4436 **********************************/
4437
4438 err = iwl_setup_mac(priv);
4439 if (err)
4440 goto out_remove_sysfs;
4441
4442 err = iwl_dbgfs_register(priv, DRV_NAME);
4443 if (err)
4444 IWL_ERROR("failed to create debugfs files\n");
4445
Emmanuel Grumbach58d0f362008-06-12 09:47:19 +08004446 err = iwl_rfkill_init(priv);
4447 if (err)
4448 IWL_ERROR("Unable to initialize RFKILL system. "
4449 "Ignoring error: %d\n", err);
4450 iwl_power_initialize(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004451 return 0;
4452
Assaf Krauss316c30d2008-03-14 10:38:46 -07004453 out_remove_sysfs:
4454 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Tomas Winkler6ba87952008-05-15 13:54:17 +08004455 out_uninit_drv:
4456 iwl_uninit_drv(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07004457 out_free_eeprom:
4458 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004459 out_iounmap:
4460 pci_iounmap(pdev, priv->hw_base);
4461 out_pci_release_regions:
4462 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07004463 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07004464 out_pci_disable_device:
4465 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004466 out_ieee80211_free_hw:
4467 ieee80211_free_hw(priv->hw);
4468 out:
4469 return err;
4470}
4471
Reinette Chatrec83dbf62008-03-21 13:53:41 -07004472static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004473{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004474 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004475 struct list_head *p, *q;
4476 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004477 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004478
4479 if (!priv)
4480 return;
4481
4482 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
4483
Emmanuel Grumbach67249622008-05-29 16:35:26 +08004484 iwl_dbgfs_unregister(priv);
4485 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
4486
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07004487 if (priv->mac80211_registered) {
4488 ieee80211_unregister_hw(priv->hw);
4489 priv->mac80211_registered = 0;
4490 }
4491
Zhu Yib481de92007-09-25 17:54:57 -07004492 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08004493
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004494 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004495
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004496 /* make sure we flush any pending irq or
4497 * tasklet for the driver
4498 */
4499 spin_lock_irqsave(&priv->lock, flags);
4500 iwl4965_disable_interrupts(priv);
4501 spin_unlock_irqrestore(&priv->lock, flags);
4502
4503 iwl_synchronize_irq(priv);
4504
Zhu Yib481de92007-09-25 17:54:57 -07004505 /* Free MAC hash list for ADHOC */
4506 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
4507 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
4508 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004509 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07004510 }
4511 }
4512
Emmanuel Grumbach58d0f362008-06-12 09:47:19 +08004513 iwl_rfkill_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004514 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004515
4516 if (priv->rxq.bd)
Tomas Winklera55360e2008-05-05 10:22:28 +08004517 iwl_rx_queue_free(priv, &priv->rxq);
Ron Rindjunsky1053d352008-05-05 10:22:43 +08004518 iwl_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004519
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004520 iwlcore_clear_stations_table(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07004521 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004522
Zhu Yib481de92007-09-25 17:54:57 -07004523
Mohamed Abbas948c1712007-10-25 17:15:45 +08004524 /*netif_stop_queue(dev); */
4525 flush_workqueue(priv->workqueue);
4526
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004527 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07004528 * priv->workqueue... so we can't take down the workqueue
4529 * until now... */
4530 destroy_workqueue(priv->workqueue);
4531 priv->workqueue = NULL;
4532
Zhu Yib481de92007-09-25 17:54:57 -07004533 pci_iounmap(pdev, priv->hw_base);
4534 pci_release_regions(pdev);
4535 pci_disable_device(pdev);
4536 pci_set_drvdata(pdev, NULL);
4537
Tomas Winkler6ba87952008-05-15 13:54:17 +08004538 iwl_uninit_drv(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004539
4540 if (priv->ibss_beacon)
4541 dev_kfree_skb(priv->ibss_beacon);
4542
4543 ieee80211_free_hw(priv->hw);
4544}
4545
4546#ifdef CONFIG_PM
4547
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004548static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07004549{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004550 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004551
Zhu Yie655b9f2008-01-24 02:19:38 -08004552 if (priv->is_open) {
4553 set_bit(STATUS_IN_SUSPEND, &priv->status);
4554 iwl4965_mac_stop(priv->hw);
4555 priv->is_open = 1;
4556 }
Zhu Yib481de92007-09-25 17:54:57 -07004557
Zhu Yib481de92007-09-25 17:54:57 -07004558 pci_set_power_state(pdev, PCI_D3hot);
4559
Zhu Yib481de92007-09-25 17:54:57 -07004560 return 0;
4561}
4562
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004563static int iwl4965_pci_resume(struct pci_dev *pdev)
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 Yib481de92007-09-25 17:54:57 -07004567 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07004568
Zhu Yie655b9f2008-01-24 02:19:38 -08004569 if (priv->is_open)
4570 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004571
Zhu Yie655b9f2008-01-24 02:19:38 -08004572 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004573 return 0;
4574}
4575
4576#endif /* CONFIG_PM */
4577
4578/*****************************************************************************
4579 *
4580 * driver and module entry point
4581 *
4582 *****************************************************************************/
4583
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004584/* Hardware specific file defines the PCI IDs table for that hardware module */
4585static struct pci_device_id iwl_hw_card_ids[] = {
4586 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
4587 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
Tomas Winkler5a6a2562008-04-24 11:55:23 -07004588#ifdef CONFIG_IWL5000
4589 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
4590 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
4591 {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
4592#endif /* CONFIG_IWL5000 */
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004593 {0}
4594};
4595MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
4596
4597static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07004598 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004599 .id_table = iwl_hw_card_ids,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004600 .probe = iwl4965_pci_probe,
4601 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07004602#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004603 .suspend = iwl4965_pci_suspend,
4604 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07004605#endif
4606};
4607
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004608static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07004609{
4610
4611 int ret;
4612 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
4613 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004614
4615 ret = iwl4965_rate_control_register();
4616 if (ret) {
4617 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
4618 return ret;
4619 }
4620
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004621 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07004622 if (ret) {
4623 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004624 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07004625 }
Zhu Yib481de92007-09-25 17:54:57 -07004626
4627 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004628
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004629error_register:
4630 iwl4965_rate_control_unregister();
4631 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004632}
4633
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004634static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07004635{
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004636 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004637 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07004638}
4639
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004640module_exit(iwl4965_exit);
4641module_init(iwl4965_init);