blob: de40e893f09e6b2b1aaef8ebd23c5aeba13826f8 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatreeb7ae892008-03-11 16:17:17 -07003 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
Zhu Yib481de92007-09-25 17:54:57 -070030#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/version.h>
33#include <linux/init.h>
34#include <linux/pci.h>
35#include <linux/dma-mapping.h>
36#include <linux/delay.h>
37#include <linux/skbuff.h>
38#include <linux/netdevice.h>
39#include <linux/wireless.h>
40#include <linux/firmware.h>
Zhu Yib481de92007-09-25 17:54:57 -070041#include <linux/etherdevice.h>
42#include <linux/if_arp.h>
43
Zhu Yib481de92007-09-25 17:54:57 -070044#include <net/mac80211.h>
45
46#include <asm/div64.h>
47
Assaf Krauss6bc913b2008-03-11 16:17:18 -070048#include "iwl-eeprom.h"
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070049#include "iwl-dev.h"
Tomas Winklerfee12472008-04-03 16:05:21 -070050#include "iwl-core.h"
Tomas Winkler3395f6e2008-03-25 16:33:37 -070051#include "iwl-io.h"
Zhu Yib481de92007-09-25 17:54:57 -070052#include "iwl-helpers.h"
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070053#include "iwl-sta.h"
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070054#include "iwl-calib.h"
Zhu Yib481de92007-09-25 17:54:57 -070055
Christoph Hellwig416e1432007-10-25 17:15:49 +080056
Zhu Yib481de92007-09-25 17:54:57 -070057/******************************************************************************
58 *
59 * module boiler plate
60 *
61 ******************************************************************************/
62
Zhu Yib481de92007-09-25 17:54:57 -070063/*
64 * module name, copyright, version, etc.
65 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
66 */
67
68#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
69
Tomas Winkler0a6857e2008-03-12 16:58:49 -070070#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070071#define VD "d"
72#else
73#define VD
74#endif
75
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080076#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070077#define VS "s"
78#else
79#define VS
80#endif
81
Tomas Winklerdf48c322008-03-06 10:40:19 -080082#define DRV_VERSION IWLWIFI_VERSION VD VS
Zhu Yib481de92007-09-25 17:54:57 -070083
Zhu Yib481de92007-09-25 17:54:57 -070084
85MODULE_DESCRIPTION(DRV_DESCRIPTION);
86MODULE_VERSION(DRV_VERSION);
87MODULE_AUTHOR(DRV_COPYRIGHT);
88MODULE_LICENSE("GPL");
89
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080090static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -070091{
92 /* Single white space is for Linksys APs */
93 if (essid_len == 1 && essid[0] == ' ')
94 return 1;
95
96 /* Otherwise, if the entire essid is 0, we assume it is hidden */
97 while (essid_len) {
98 essid_len--;
99 if (essid[essid_len] != '\0')
100 return 0;
101 }
102
103 return 1;
104}
105
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800106static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700107{
108 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
109 const char *s = essid;
110 char *d = escaped;
111
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800112 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700113 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
114 return escaped;
115 }
116
117 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
118 while (essid_len--) {
119 if (*s == '\0') {
120 *d++ = '\\';
121 *d++ = '0';
122 s++;
123 } else
124 *d++ = *s++;
125 }
126 *d = '\0';
127 return escaped;
128}
129
Zhu Yib481de92007-09-25 17:54:57 -0700130/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800131 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700132 * the functionality provided here
133 */
134
135/**************************************************************/
136
Zhu Yib481de92007-09-25 17:54:57 -0700137
Zhu Yib481de92007-09-25 17:54:57 -0700138
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700139static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
140{
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800141 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700142
143 if (hw_decrypt)
144 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
145 else
146 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
147
148}
149
Zhu Yib481de92007-09-25 17:54:57 -0700150/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800151 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700152 *
153 * NOTE: This is really only useful during development and can eventually
154 * be #ifdef'd out once the driver is stable and folks aren't actively
155 * making changes
156 */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800157static int iwl4965_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700158{
159 int error = 0;
160 int counter = 1;
161
162 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
163 error |= le32_to_cpu(rxon->flags &
164 (RXON_FLG_TGJ_NARROW_BAND_MSK |
165 RXON_FLG_RADAR_DETECT_MSK));
166 if (error)
167 IWL_WARNING("check 24G fields %d | %d\n",
168 counter++, error);
169 } else {
170 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
171 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
172 if (error)
173 IWL_WARNING("check 52 fields %d | %d\n",
174 counter++, error);
175 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
176 if (error)
177 IWL_WARNING("check 52 CCK %d | %d\n",
178 counter++, error);
179 }
180 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
181 if (error)
182 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
183
184 /* make sure basic rates 6Mbps and 1Mbps are supported */
185 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
186 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
187 if (error)
188 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
189
190 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
191 if (error)
192 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
193
194 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
195 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
196 if (error)
197 IWL_WARNING("check CCK and short slot %d | %d\n",
198 counter++, error);
199
200 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
201 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
202 if (error)
203 IWL_WARNING("check CCK & auto detect %d | %d\n",
204 counter++, error);
205
206 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
207 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
208 if (error)
209 IWL_WARNING("check TGG and auto detect %d | %d\n",
210 counter++, error);
211
212 if (error)
213 IWL_WARNING("Tuning to channel %d\n",
214 le16_to_cpu(rxon->channel));
215
216 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800217 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700218 return -1;
219 }
220 return 0;
221}
222
223/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800224 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800225 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700226 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800227 * If the RXON structure is changing enough to require a new tune,
228 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
229 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700230 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700231static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700232{
233
234 /* These items are only settable from the full RXON command */
235 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
236 compare_ether_addr(priv->staging_rxon.bssid_addr,
237 priv->active_rxon.bssid_addr) ||
238 compare_ether_addr(priv->staging_rxon.node_addr,
239 priv->active_rxon.node_addr) ||
240 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
241 priv->active_rxon.wlap_bssid_addr) ||
242 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
243 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
244 (priv->staging_rxon.air_propagation !=
245 priv->active_rxon.air_propagation) ||
246 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
247 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
248 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
249 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
250 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
251 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
252 return 1;
253
254 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
255 * be updated with the RXON_ASSOC command -- however only some
256 * flag transitions are allowed using RXON_ASSOC */
257
258 /* Check if we are not switching bands */
259 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
260 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
261 return 1;
262
263 /* Check if we are switching association toggle */
264 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
265 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
266 return 1;
267
268 return 0;
269}
270
Zhu Yib481de92007-09-25 17:54:57 -0700271/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800272 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700273 *
Ian Schram01ebd062007-10-25 17:15:22 +0800274 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700275 * the active_rxon structure is updated with the new data. This
276 * function correctly transitions out of the RXON_ASSOC_MSK state if
277 * a HW tune is required based on the RXON structure changes.
278 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700279static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700280{
281 /* cast away the const for active_rxon in this function */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800282 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -0700283 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700284 int rc = 0;
285
Tomas Winklerfee12472008-04-03 16:05:21 -0700286 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700287 return -1;
288
289 /* always get timestamp with Rx frame */
290 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
291
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800292 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700293 if (rc) {
294 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
295 return -EINVAL;
296 }
297
298 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800299 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700300 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800301 if (!iwl4965_full_rxon_required(priv)) {
Tomas Winkler7e8c5192008-04-15 16:01:43 -0700302 rc = iwl_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700303 if (rc) {
304 IWL_ERROR("Error setting RXON_ASSOC "
305 "configuration (%d).\n", rc);
306 return rc;
307 }
308
309 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
310
311 return 0;
312 }
313
314 /* station table will be cleared */
315 priv->assoc_station_added = 0;
316
Zhu Yib481de92007-09-25 17:54:57 -0700317 /* If we are currently associated and the new config requires
318 * an RXON_ASSOC and the new config wants the associated mask enabled,
319 * we must clear the associated from the active configuration
320 * before we apply the new config */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700321 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700322 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
323 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
324 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
325
Tomas Winkler857485c2008-03-21 13:53:44 -0700326 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800327 sizeof(struct iwl_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700328 &priv->active_rxon);
329
330 /* If the mask clearing failed then we set
331 * active_rxon back to what it was previously */
332 if (rc) {
333 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
334 IWL_ERROR("Error clearing ASSOC_MSK on current "
335 "configuration (%d).\n", rc);
336 return rc;
337 }
Zhu Yib481de92007-09-25 17:54:57 -0700338 }
339
340 IWL_DEBUG_INFO("Sending RXON\n"
341 "* with%s RXON_FILTER_ASSOC_MSK\n"
342 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -0700343 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700344 ((priv->staging_rxon.filter_flags &
345 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
346 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -0700347 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -0700348
Ron Rindjunsky099b40b2008-04-21 15:41:53 -0700349 iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
Zhu Yib481de92007-09-25 17:54:57 -0700350 /* Apply the new configuration */
Tomas Winkler857485c2008-03-21 13:53:44 -0700351 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800352 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700353 if (rc) {
354 IWL_ERROR("Error setting new configuration (%d).\n", rc);
355 return rc;
356 }
357
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800358 iwl_remove_station(priv, iwl_bcast_addr, 0);
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700359 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800360
Zhu Yib481de92007-09-25 17:54:57 -0700361 if (!priv->error_recovering)
362 priv->start_calib = 0;
363
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700364 iwl_init_sensitivity(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700365
366 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
367
368 /* If we issue a new RXON command which required a tune then we must
369 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800370 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700371 if (rc) {
372 IWL_ERROR("Error setting Tx power (%d).\n", rc);
373 return rc;
374 }
375
376 /* Add the broadcast address so we can send broadcast frames */
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800377 if (iwl_rxon_add_station(priv, iwl_bcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700378 IWL_INVALID_STATION) {
379 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
380 return -EIO;
381 }
382
383 /* If we have set the ASSOC_MSK and we are in BSS mode then
384 * add the IWL_AP_ID to the station rate table */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700385 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700386 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800387 if (iwl_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700388 == IWL_INVALID_STATION) {
389 IWL_ERROR("Error adding AP address for transmit.\n");
390 return -EIO;
391 }
392 priv->assoc_station_added = 1;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700393 if (priv->default_wep_key &&
394 iwl_send_static_wepkey_cmd(priv, 0))
395 IWL_ERROR("Could not send WEP static key.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700396 }
397
398 return 0;
399}
400
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700401void iwl4965_update_chain_flags(struct iwl_priv *priv)
402{
403
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -0700404 iwl_set_rxon_chain(priv);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700405 iwl4965_commit_rxon(priv);
406}
407
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700408static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700409{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800410 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700411 .flags = 3,
412 .lead_time = 0xAA,
413 .max_kill = 1,
414 .kill_ack_mask = 0,
415 .kill_cts_mask = 0,
416 };
417
Tomas Winkler857485c2008-03-21 13:53:44 -0700418 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800419 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700420}
421
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700422static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700423{
Tomas Winklerdb11d632008-05-05 10:22:33 +0800424 int ret = 0;
425 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -0700426 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700427 .id = REPLY_SCAN_ABORT_CMD,
428 .meta.flags = CMD_WANT_SKB,
429 };
430
431 /* If there isn't a scan actively going on in the hardware
432 * then we are in between scan bands and not actually
433 * actively scanning, so don't send the abort command */
434 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
435 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
436 return 0;
437 }
438
Tomas Winklerdb11d632008-05-05 10:22:33 +0800439 ret = iwl_send_cmd_sync(priv, &cmd);
440 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700441 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
Tomas Winklerdb11d632008-05-05 10:22:33 +0800442 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700443 }
444
Tomas Winklerdb11d632008-05-05 10:22:33 +0800445 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700446 if (res->u.status != CAN_ABORT_STATUS) {
447 /* The scan abort will return 1 for success or
448 * 2 for "failure". A failure condition can be
449 * due to simply not being in an active scan which
450 * can occur if we send the scan abort before we
451 * the microcode has notified us that a scan is
452 * completed. */
453 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
454 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
455 clear_bit(STATUS_SCAN_HW, &priv->status);
456 }
457
458 dev_kfree_skb_any(cmd.meta.u.skb);
459
Tomas Winklerdb11d632008-05-05 10:22:33 +0800460 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700461}
462
Zhu Yib481de92007-09-25 17:54:57 -0700463/*
464 * CARD_STATE_CMD
465 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800466 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -0700467 *
468 * When in the 'enable' state the card operates as normal.
469 * When in the 'disable' state, the card enters into a low power mode.
470 * When in the 'halt' state, the card is shut down and must be fully
471 * restarted to come back on.
472 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700473static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -0700474{
Tomas Winkler857485c2008-03-21 13:53:44 -0700475 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700476 .id = REPLY_CARD_STATE_CMD,
477 .len = sizeof(u32),
478 .data = &flags,
479 .meta.flags = meta_flag,
480 };
481
Tomas Winkler857485c2008-03-21 13:53:44 -0700482 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700483}
484
Tomas Winklerfcab4232008-05-15 13:54:01 +0800485static void iwl_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700486{
487 struct list_head *element;
488
489 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
490 priv->frames_count);
491
492 while (!list_empty(&priv->free_frames)) {
493 element = priv->free_frames.next;
494 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800495 kfree(list_entry(element, struct iwl_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700496 priv->frames_count--;
497 }
498
499 if (priv->frames_count) {
500 IWL_WARNING("%d frames still in use. Did we lose one?\n",
501 priv->frames_count);
502 priv->frames_count = 0;
503 }
504}
505
Tomas Winklerfcab4232008-05-15 13:54:01 +0800506static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700507{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800508 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700509 struct list_head *element;
510 if (list_empty(&priv->free_frames)) {
511 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
512 if (!frame) {
513 IWL_ERROR("Could not allocate frame!\n");
514 return NULL;
515 }
516
517 priv->frames_count++;
518 return frame;
519 }
520
521 element = priv->free_frames.next;
522 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800523 return list_entry(element, struct iwl_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700524}
525
Tomas Winklerfcab4232008-05-15 13:54:01 +0800526static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700527{
528 memset(frame, 0, sizeof(*frame));
529 list_add(&frame->list, &priv->free_frames);
530}
531
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700532unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700533 struct ieee80211_hdr *hdr,
534 const u8 *dest, int left)
535{
536
Tomas Winkler3109ece2008-03-28 16:33:35 -0700537 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -0700538 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
539 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
540 return 0;
541
542 if (priv->ibss_beacon->len > left)
543 return 0;
544
545 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
546
547 return priv->ibss_beacon->len;
548}
549
Guy Cohen39e88502008-04-23 17:14:57 -0700550static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700551{
Guy Cohen39e88502008-04-23 17:14:57 -0700552 int i;
553 int rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -0700554
Guy Cohen39e88502008-04-23 17:14:57 -0700555 /* Set rate mask*/
556 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
557 rate_mask = priv->active_rate_basic & 0xF;
558 else
559 rate_mask = priv->active_rate_basic & 0xFF0;
560
561 /* Find lowest valid rate */
Zhu Yib481de92007-09-25 17:54:57 -0700562 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800563 i = iwl_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -0700564 if (rate_mask & (1 << i))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800565 return iwl_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700566 }
567
Guy Cohen39e88502008-04-23 17:14:57 -0700568 /* No valid rate was found. Assign the lowest one */
569 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
570 return IWL_RATE_1M_PLCP;
571 else
572 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -0700573}
574
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700575static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700576{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800577 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700578 unsigned int frame_size;
579 int rc;
580 u8 rate;
581
Tomas Winklerfcab4232008-05-15 13:54:01 +0800582 frame = iwl_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700583
584 if (!frame) {
585 IWL_ERROR("Could not obtain free frame buffer for beacon "
586 "command.\n");
587 return -ENOMEM;
588 }
589
Guy Cohen39e88502008-04-23 17:14:57 -0700590 rate = iwl4965_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700591
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800592 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700593
Tomas Winkler857485c2008-03-21 13:53:44 -0700594 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700595 &frame->u.cmd[0]);
596
Tomas Winklerfcab4232008-05-15 13:54:01 +0800597 iwl_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700598
599 return rc;
600}
601
602/******************************************************************************
603 *
Zhu Yib481de92007-09-25 17:54:57 -0700604 * Misc. internal state and helper functions
605 *
606 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -0700607
Zhu Yib481de92007-09-25 17:54:57 -0700608/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800609 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -0700610 *
611 * return : set the bit for each supported rate insert in ie
612 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800613static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +0200614 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -0700615{
616 u16 ret_rates = 0, bit;
617 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200618 u8 *cnt = ie;
619 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -0700620
621 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
622 if (bit & supported_rate) {
623 ret_rates |= bit;
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800624 rates[*cnt] = iwl_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +0200625 ((bit & basic_rate) ? 0x80 : 0x00);
626 (*cnt)++;
627 (*left)--;
628 if ((*left <= 0) ||
629 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -0700630 break;
631 }
632 }
633
634 return ret_rates;
635}
636
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700637static void iwl4965_ht_conf(struct iwl_priv *priv,
638 struct ieee80211_bss_conf *bss_conf)
639{
640 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
641 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
642 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
643
644 IWL_DEBUG_MAC80211("enter: \n");
645
646 iwl_conf->is_ht = bss_conf->assoc_ht;
647
648 if (!iwl_conf->is_ht)
649 return;
650
651 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
652
653 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
Emmanuel Grumbacha9841012008-05-15 13:54:08 +0800654 iwl_conf->sgf |= HT_SHORT_GI_20MHZ;
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700655 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
Emmanuel Grumbacha9841012008-05-15 13:54:08 +0800656 iwl_conf->sgf |= HT_SHORT_GI_40MHZ;
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700657
658 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
659 iwl_conf->max_amsdu_size =
660 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
661
662 iwl_conf->supported_chan_width =
663 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
664 iwl_conf->extension_chan_offset =
665 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
666 /* If no above or below channel supplied disable FAT channel */
Emmanuel Grumbach963f5512008-06-12 09:47:00 +0800667 if (iwl_conf->extension_chan_offset != IEEE80211_HT_IE_CHA_SEC_ABOVE &&
668 iwl_conf->extension_chan_offset != IEEE80211_HT_IE_CHA_SEC_BELOW) {
669 iwl_conf->extension_chan_offset = IEEE80211_HT_IE_CHA_SEC_NONE;
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700670 iwl_conf->supported_chan_width = 0;
Emmanuel Grumbach963f5512008-06-12 09:47:00 +0800671 }
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700672
673 iwl_conf->tx_mimo_ps_mode =
674 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
675 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
676
677 iwl_conf->control_channel = ht_bss_conf->primary_channel;
678 iwl_conf->tx_chan_width =
679 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
680 iwl_conf->ht_protection =
681 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
682 iwl_conf->non_GF_STA_present =
683 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
684
685 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
686 IWL_DEBUG_MAC80211("leave\n");
687}
688
689static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
690 u8 *pos, int *left)
691{
692 struct ieee80211_ht_cap *ht_cap;
693
694 if (!sband || !sband->ht_info.ht_supported)
695 return;
696
697 if (*left < sizeof(struct ieee80211_ht_cap))
698 return;
699
700 *pos++ = sizeof(struct ieee80211_ht_cap);
701 ht_cap = (struct ieee80211_ht_cap *) pos;
702
703 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
704 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
705 ht_cap->ampdu_params_info =
706 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
707 ((sband->ht_info.ampdu_density << 2) &
708 IEEE80211_HT_CAP_AMPDU_DENSITY);
709 *left -= sizeof(struct ieee80211_ht_cap);
710}
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700711
Zhu Yib481de92007-09-25 17:54:57 -0700712/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800713 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -0700714 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700715static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +0200716 enum ieee80211_band band,
717 struct ieee80211_mgmt *frame,
718 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -0700719{
720 int len = 0;
721 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +0800722 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Tomas Winkler78330fd2008-02-06 02:37:18 +0200723 const struct ieee80211_supported_band *sband =
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700724 iwl_get_hw_mode(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -0700725
726 /* Make sure there is enough space for the probe request,
727 * two mandatory IEs and the data */
728 left -= 24;
729 if (left < 0)
730 return 0;
731 len += 24;
732
733 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Tomas Winkler57bd1be2008-05-15 13:54:03 +0800734 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -0700735 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Tomas Winkler57bd1be2008-05-15 13:54:03 +0800736 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -0700737 frame->seq_ctrl = 0;
738
739 /* fill in our indirect SSID IE */
740 /* ...next IE... */
741
742 left -= 2;
743 if (left < 0)
744 return 0;
745 len += 2;
746 pos = &(frame->u.probe_req.variable[0]);
747 *pos++ = WLAN_EID_SSID;
748 *pos++ = 0;
749
750 /* fill in our direct SSID IE... */
751 if (is_direct) {
752 /* ...next IE... */
753 left -= 2 + priv->essid_len;
754 if (left < 0)
755 return 0;
756 /* ... fill it in... */
757 *pos++ = WLAN_EID_SSID;
758 *pos++ = priv->essid_len;
759 memcpy(pos, priv->essid, priv->essid_len);
760 pos += priv->essid_len;
761 len += 2 + priv->essid_len;
762 }
763
764 /* fill in supported rate */
765 /* ...next IE... */
766 left -= 2;
767 if (left < 0)
768 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200769
Zhu Yib481de92007-09-25 17:54:57 -0700770 /* ... fill it in... */
771 *pos++ = WLAN_EID_SUPP_RATES;
772 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200773
mabbasbee488d2007-10-25 17:15:42 +0800774 /* exclude 60M rate */
775 active_rates = priv->rates_mask;
776 active_rates &= ~IWL_RATE_60M_MASK;
777
778 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -0700779
Tomas Winklerc7c46672007-10-18 02:04:15 +0200780 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800781 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +0800782 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +0200783 active_rates &= ~ret_rates;
784
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800785 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +0800786 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +0200787 active_rates &= ~ret_rates;
788
Zhu Yib481de92007-09-25 17:54:57 -0700789 len += 2 + *pos;
790 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200791 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -0700792 goto fill_end;
793
794 /* fill in supported extended rate */
795 /* ...next IE... */
796 left -= 2;
797 if (left < 0)
798 return 0;
799 /* ... fill it in... */
800 *pos++ = WLAN_EID_EXT_SUPP_RATES;
801 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800802 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +0800803 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -0700804 if (*pos > 0)
805 len += 2 + *pos;
806
Zhu Yib481de92007-09-25 17:54:57 -0700807 fill_end:
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700808 /* fill in HT IE */
809 left -= 2;
810 if (left < 0)
811 return 0;
812
813 *pos++ = WLAN_EID_HT_CAPABILITY;
814 *pos = 0;
815
816 iwl_ht_cap_to_ie(sband, pos, &left);
817
818 if (*pos > 0)
819 len += 2 + *pos;
Zhu Yib481de92007-09-25 17:54:57 -0700820 return (u16)len;
821}
822
823/*
824 * QoS support
825*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700826static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800827 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -0700828{
829
Tomas Winkler857485c2008-03-21 13:53:44 -0700830 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800831 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -0700832}
833
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700834static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -0700835{
836 unsigned long flags;
837
Zhu Yib481de92007-09-25 17:54:57 -0700838 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
839 return;
840
841 if (!priv->qos_data.qos_enable)
842 return;
843
844 spin_lock_irqsave(&priv->lock, flags);
845 priv->qos_data.def_qos_parm.qos_flags = 0;
846
847 if (priv->qos_data.qos_cap.q_AP.queue_request &&
848 !priv->qos_data.qos_cap.q_AP.txop_request)
849 priv->qos_data.def_qos_parm.qos_flags |=
850 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700851 if (priv->qos_data.qos_active)
852 priv->qos_data.def_qos_parm.qos_flags |=
853 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
854
Ron Rindjunskyfd105e72007-11-26 16:14:39 +0200855 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800856 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800857
Zhu Yib481de92007-09-25 17:54:57 -0700858 spin_unlock_irqrestore(&priv->lock, flags);
859
Tomas Winkler3109ece2008-03-28 16:33:35 -0700860 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800861 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
862 priv->qos_data.qos_active,
863 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700864
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800865 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -0700866 &(priv->qos_data.def_qos_parm));
867 }
868}
869
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700870int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -0700871{
872 /* Filter incoming packets to determine if they are targeted toward
873 * this network, discarding packets coming from ourselves */
874 switch (priv->iw_mode) {
875 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
876 /* packets from our adapter are dropped (echo) */
877 if (!compare_ether_addr(header->addr2, priv->mac_addr))
878 return 0;
879 /* {broad,multi}cast packets to our IBSS go through */
880 if (is_multicast_ether_addr(header->addr1))
881 return !compare_ether_addr(header->addr3, priv->bssid);
882 /* packets to our adapter go through */
883 return !compare_ether_addr(header->addr1, priv->mac_addr);
884 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
885 /* packets from our adapter are dropped (echo) */
886 if (!compare_ether_addr(header->addr3, priv->mac_addr))
887 return 0;
888 /* {broad,multi}cast packets to our BSS go through */
889 if (is_multicast_ether_addr(header->addr1))
890 return !compare_ether_addr(header->addr2, priv->bssid);
891 /* packets to our adapter go through */
892 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -0700893 default:
894 break;
Zhu Yib481de92007-09-25 17:54:57 -0700895 }
896
897 return 1;
898}
899
Zhu Yib481de92007-09-25 17:54:57 -0700900
Zhu Yib481de92007-09-25 17:54:57 -0700901
902/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800903 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -0700904 *
905 * NOTE: priv->mutex is not required before calling this function
906 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700907static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700908{
909 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
910 clear_bit(STATUS_SCANNING, &priv->status);
911 return 0;
912 }
913
914 if (test_bit(STATUS_SCANNING, &priv->status)) {
915 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
916 IWL_DEBUG_SCAN("Queuing scan abort.\n");
917 set_bit(STATUS_SCAN_ABORTING, &priv->status);
918 queue_work(priv->workqueue, &priv->abort_scan);
919
920 } else
921 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
922
923 return test_bit(STATUS_SCANNING, &priv->status);
924 }
925
926 return 0;
927}
928
929/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800930 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -0700931 * @ms: amount of time to wait (in milliseconds) for scan to abort
932 *
933 * NOTE: priv->mutex must be held before calling this function
934 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700935static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -0700936{
937 unsigned long now = jiffies;
938 int ret;
939
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800940 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700941 if (ret && ms) {
942 mutex_unlock(&priv->mutex);
943 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
944 test_bit(STATUS_SCANNING, &priv->status))
945 msleep(1);
946 mutex_lock(&priv->mutex);
947
948 return test_bit(STATUS_SCANNING, &priv->status);
949 }
950
951 return ret;
952}
953
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700954static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700955{
956 /* Reset ieee stats */
957
958 /* We don't reset the net_device_stats (ieee->stats) on
959 * re-association */
960
961 priv->last_seq_num = -1;
962 priv->last_frag_num = -1;
963 priv->last_packet_time = 0;
964
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800965 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700966}
967
968#define MAX_UCODE_BEACON_INTERVAL 4096
969#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
970
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800971static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -0700972{
973 u16 new_val = 0;
974 u16 beacon_factor = 0;
975
976 beacon_factor =
977 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
978 / MAX_UCODE_BEACON_INTERVAL;
979 new_val = beacon_val / beacon_factor;
980
981 return cpu_to_le16(new_val);
982}
983
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700984static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700985{
986 u64 interval_tm_unit;
987 u64 tsf, result;
988 unsigned long flags;
989 struct ieee80211_conf *conf = NULL;
990 u16 beacon_int = 0;
991
992 conf = ieee80211_get_hw_conf(priv->hw);
993
994 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -0700995 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
996 priv->rxon_timing.timestamp.dw[0] =
997 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -0700998
999 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1000
Tomas Winkler3109ece2008-03-28 16:33:35 -07001001 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001002
1003 beacon_int = priv->beacon_int;
1004 spin_unlock_irqrestore(&priv->lock, flags);
1005
1006 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1007 if (beacon_int == 0) {
1008 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1009 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1010 } else {
1011 priv->rxon_timing.beacon_interval =
1012 cpu_to_le16(beacon_int);
1013 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001014 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001015 le16_to_cpu(priv->rxon_timing.beacon_interval));
1016 }
1017
1018 priv->rxon_timing.atim_window = 0;
1019 } else {
1020 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001021 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001022 /* TODO: we need to get atim_window from upper stack
1023 * for now we set to 0 */
1024 priv->rxon_timing.atim_window = 0;
1025 }
1026
1027 interval_tm_unit =
1028 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1029 result = do_div(tsf, interval_tm_unit);
1030 priv->rxon_timing.beacon_init_val =
1031 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1032
1033 IWL_DEBUG_ASSOC
1034 ("beacon interval %d beacon timer %d beacon tim %d\n",
1035 le16_to_cpu(priv->rxon_timing.beacon_interval),
1036 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1037 le16_to_cpu(priv->rxon_timing.atim_window));
1038}
1039
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001040static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001041{
1042 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1043 IWL_ERROR("APs don't scan.\n");
1044 return 0;
1045 }
1046
Tomas Winklerfee12472008-04-03 16:05:21 -07001047 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001048 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1049 return -EIO;
1050 }
1051
1052 if (test_bit(STATUS_SCANNING, &priv->status)) {
1053 IWL_DEBUG_SCAN("Scan already in progress.\n");
1054 return -EAGAIN;
1055 }
1056
1057 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1058 IWL_DEBUG_SCAN("Scan request while abort pending. "
1059 "Queuing.\n");
1060 return -EAGAIN;
1061 }
1062
1063 IWL_DEBUG_INFO("Starting scan...\n");
1064 priv->scan_bands = 2;
1065 set_bit(STATUS_SCANNING, &priv->status);
1066 priv->scan_start = jiffies;
1067 priv->scan_pass_start = priv->scan_start;
1068
1069 queue_work(priv->workqueue, &priv->request_scan);
1070
1071 return 0;
1072}
1073
Zhu Yib481de92007-09-25 17:54:57 -07001074
Tomas Winkler82a66bb2008-05-29 16:35:28 +08001075static void iwl_set_flags_for_band(struct iwl_priv *priv,
1076 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001077{
Johannes Berg8318d782008-01-24 19:38:38 +01001078 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07001079 priv->staging_rxon.flags &=
1080 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1081 | RXON_FLG_CCK_MSK);
1082 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1083 } else {
Reinette Chatre508e32e2008-04-14 21:16:13 -07001084 /* Copied from iwl4965_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001085 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1086 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1087 else
1088 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1089
1090 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1091 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1092
1093 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1094 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1095 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1096 }
1097}
1098
1099/*
Ian Schram01ebd062007-10-25 17:15:22 +08001100 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001101 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001102static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001103{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001104 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001105
1106 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1107
1108 switch (priv->iw_mode) {
1109 case IEEE80211_IF_TYPE_AP:
1110 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1111 break;
1112
1113 case IEEE80211_IF_TYPE_STA:
1114 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1115 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1116 break;
1117
1118 case IEEE80211_IF_TYPE_IBSS:
1119 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1120 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1121 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1122 RXON_FILTER_ACCEPT_GRP_MSK;
1123 break;
1124
1125 case IEEE80211_IF_TYPE_MNTR:
1126 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1127 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1128 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1129 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001130 default:
1131 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1132 break;
Zhu Yib481de92007-09-25 17:54:57 -07001133 }
1134
1135#if 0
1136 /* TODO: Figure out when short_preamble would be set and cache from
1137 * that */
1138 if (!hw_to_local(priv->hw)->short_preamble)
1139 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1140 else
1141 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1142#endif
1143
Assaf Krauss8622e702008-03-21 13:53:43 -07001144 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001145 le16_to_cpu(priv->staging_rxon.channel));
1146
1147 if (!ch_info)
1148 ch_info = &priv->channel_info[0];
1149
1150 /*
1151 * in some case A channels are all non IBSS
1152 * in this case force B/G channel
1153 */
1154 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1155 !(is_channel_ibss(ch_info)))
1156 ch_info = &priv->channel_info[0];
1157
1158 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01001159 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07001160
Tomas Winkler82a66bb2008-05-29 16:35:28 +08001161 iwl_set_flags_for_band(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001162
1163 priv->staging_rxon.ofdm_basic_rates =
1164 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1165 priv->staging_rxon.cck_basic_rates =
1166 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1167
1168 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1169 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1170 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1171 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1172 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1173 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07001174 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001175}
1176
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001177static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001178{
Zhu Yib481de92007-09-25 17:54:57 -07001179 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001180 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001181
Assaf Krauss8622e702008-03-21 13:53:43 -07001182 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001183 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001184 le16_to_cpu(priv->staging_rxon.channel));
1185
1186 if (!ch_info || !is_channel_ibss(ch_info)) {
1187 IWL_ERROR("channel %d not IBSS channel\n",
1188 le16_to_cpu(priv->staging_rxon.channel));
1189 return -EINVAL;
1190 }
1191 }
1192
Zhu Yib481de92007-09-25 17:54:57 -07001193 priv->iw_mode = mode;
1194
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001195 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001196 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1197
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001198 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001199
Mohamed Abbasfde35712007-11-29 11:10:15 +08001200 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -07001201 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08001202 return -EAGAIN;
1203
1204 cancel_delayed_work(&priv->scan_check);
1205 if (iwl4965_scan_cancel_timeout(priv, 100)) {
1206 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1207 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1208 return -EAGAIN;
1209 }
1210
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001211 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001212
1213 return 0;
1214}
1215
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001216static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001217{
Johannes Berg8318d782008-01-24 19:38:38 +01001218 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001219 struct ieee80211_rate *rate;
1220 int i;
1221
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001222 hw = iwl_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08001223 if (!hw) {
1224 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
1225 return;
1226 }
Zhu Yib481de92007-09-25 17:54:57 -07001227
1228 priv->active_rate = 0;
1229 priv->active_rate_basic = 0;
1230
Johannes Berg8318d782008-01-24 19:38:38 +01001231 for (i = 0; i < hw->n_bitrates; i++) {
1232 rate = &(hw->bitrates[i]);
1233 if (rate->hw_value < IWL_RATE_COUNT)
1234 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07001235 }
1236
1237 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
1238 priv->active_rate, priv->active_rate_basic);
1239
1240 /*
1241 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
1242 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
1243 * OFDM
1244 */
1245 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
1246 priv->staging_rxon.cck_basic_rates =
1247 ((priv->active_rate_basic &
1248 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
1249 else
1250 priv->staging_rxon.cck_basic_rates =
1251 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1252
1253 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
1254 priv->staging_rxon.ofdm_basic_rates =
1255 ((priv->active_rate_basic &
1256 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
1257 IWL_FIRST_OFDM_RATE) & 0xFF;
1258 else
1259 priv->staging_rxon.ofdm_basic_rates =
1260 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1261}
1262
Mohamed Abbasad97edd2008-03-28 16:21:06 -07001263void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07001264{
1265 unsigned long flags;
1266
1267 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
1268 return;
1269
1270 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
1271 disable_radio ? "OFF" : "ON");
1272
1273 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001274 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001275 /* FIXME: This is a workaround for AP */
1276 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
1277 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001278 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001279 CSR_UCODE_SW_BIT_RFKILL);
1280 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07001281 /* call the host command only if no hw rf-kill set */
Mohamed Abbas59003832008-04-15 16:01:46 -07001282 if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
1283 iwl_is_ready(priv))
Mohamed Abbasad97edd2008-03-28 16:21:06 -07001284 iwl4965_send_card_state(priv,
1285 CARD_STATE_CMD_DISABLE,
1286 0);
Zhu Yib481de92007-09-25 17:54:57 -07001287 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07001288
1289 /* make sure mac80211 stop sending Tx frame */
1290 if (priv->mac80211_registered)
1291 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07001292 }
1293 return;
1294 }
1295
1296 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001297 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07001298
1299 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1300 spin_unlock_irqrestore(&priv->lock, flags);
1301
1302 /* wake up ucode */
1303 msleep(10);
1304
1305 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001306 iwl_read32(priv, CSR_UCODE_DRV_GP1);
1307 if (!iwl_grab_nic_access(priv))
1308 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001309 spin_unlock_irqrestore(&priv->lock, flags);
1310
1311 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
1312 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
1313 "disabled by HW switch\n");
1314 return;
1315 }
1316
1317 queue_work(priv->workqueue, &priv->restart);
1318 return;
1319}
1320
Zhu Yib481de92007-09-25 17:54:57 -07001321#define IWL_PACKET_RETRY_TIME HZ
1322
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001323int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001324{
1325 u16 sc = le16_to_cpu(header->seq_ctrl);
1326 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1327 u16 frag = sc & IEEE80211_SCTL_FRAG;
1328 u16 *last_seq, *last_frag;
1329 unsigned long *last_time;
1330
1331 switch (priv->iw_mode) {
1332 case IEEE80211_IF_TYPE_IBSS:{
1333 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001334 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001335 u8 *mac = header->addr2;
1336 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
1337
1338 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001339 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07001340 if (!compare_ether_addr(entry->mac, mac))
1341 break;
1342 }
1343 if (p == &priv->ibss_mac_hash[index]) {
1344 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1345 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08001346 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07001347 return 0;
1348 }
1349 memcpy(entry->mac, mac, ETH_ALEN);
1350 entry->seq_num = seq;
1351 entry->frag_num = frag;
1352 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08001353 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07001354 return 0;
1355 }
1356 last_seq = &entry->seq_num;
1357 last_frag = &entry->frag_num;
1358 last_time = &entry->packet_time;
1359 break;
1360 }
1361 case IEEE80211_IF_TYPE_STA:
1362 last_seq = &priv->last_seq_num;
1363 last_frag = &priv->last_frag_num;
1364 last_time = &priv->last_packet_time;
1365 break;
1366 default:
1367 return 0;
1368 }
1369 if ((*last_seq == seq) &&
1370 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
1371 if (*last_frag == frag)
1372 goto drop;
1373 if (*last_frag + 1 != frag)
1374 /* out-of-order fragment */
1375 goto drop;
1376 } else
1377 *last_seq = seq;
1378
1379 *last_frag = frag;
1380 *last_time = jiffies;
1381 return 0;
1382
1383 drop:
1384 return 1;
1385}
1386
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001387#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07001388
1389#include "iwl-spectrum.h"
1390
1391#define BEACON_TIME_MASK_LOW 0x00FFFFFF
1392#define BEACON_TIME_MASK_HIGH 0xFF000000
1393#define TIME_UNIT 1024
1394
1395/*
1396 * extended beacon time format
1397 * time in usec will be changed into a 32-bit value in 8:24 format
1398 * the high 1 byte is the beacon counts
1399 * the lower 3 bytes is the time in usec within one beacon interval
1400 */
1401
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001402static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07001403{
1404 u32 quot;
1405 u32 rem;
1406 u32 interval = beacon_interval * 1024;
1407
1408 if (!interval || !usec)
1409 return 0;
1410
1411 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
1412 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
1413
1414 return (quot << 24) + rem;
1415}
1416
1417/* base is usually what we get from ucode with each received frame,
1418 * the same as HW timer counter counting down
1419 */
1420
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001421static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07001422{
1423 u32 base_low = base & BEACON_TIME_MASK_LOW;
1424 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
1425 u32 interval = beacon_interval * TIME_UNIT;
1426 u32 res = (base & BEACON_TIME_MASK_HIGH) +
1427 (addon & BEACON_TIME_MASK_HIGH);
1428
1429 if (base_low > addon_low)
1430 res += base_low - addon_low;
1431 else if (base_low < addon_low) {
1432 res += interval + base_low - addon_low;
1433 res += (1 << 24);
1434 } else
1435 res += (1 << 24);
1436
1437 return cpu_to_le32(res);
1438}
1439
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001440static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001441 struct ieee80211_measurement_params *params,
1442 u8 type)
1443{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001444 struct iwl4965_spectrum_cmd spectrum;
Tomas Winklerdb11d632008-05-05 10:22:33 +08001445 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07001446 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001447 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
1448 .data = (void *)&spectrum,
1449 .meta.flags = CMD_WANT_SKB,
1450 };
1451 u32 add_time = le64_to_cpu(params->start_time);
1452 int rc;
1453 int spectrum_resp_status;
1454 int duration = le16_to_cpu(params->duration);
1455
Tomas Winkler3109ece2008-03-28 16:33:35 -07001456 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001457 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001458 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07001459 le64_to_cpu(params->start_time) - priv->last_tsf,
1460 le16_to_cpu(priv->rxon_timing.beacon_interval));
1461
1462 memset(&spectrum, 0, sizeof(spectrum));
1463
1464 spectrum.channel_count = cpu_to_le16(1);
1465 spectrum.flags =
1466 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
1467 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
1468 cmd.len = sizeof(spectrum);
1469 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
1470
Tomas Winkler3109ece2008-03-28 16:33:35 -07001471 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001472 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001473 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07001474 add_time,
1475 le16_to_cpu(priv->rxon_timing.beacon_interval));
1476 else
1477 spectrum.start_time = 0;
1478
1479 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
1480 spectrum.channels[0].channel = params->channel;
1481 spectrum.channels[0].type = type;
1482 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
1483 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
1484 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
1485
Tomas Winkler857485c2008-03-21 13:53:44 -07001486 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001487 if (rc)
1488 return rc;
1489
Tomas Winklerdb11d632008-05-05 10:22:33 +08001490 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001491 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1492 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
1493 rc = -EIO;
1494 }
1495
1496 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
1497 switch (spectrum_resp_status) {
1498 case 0: /* Command will be handled */
1499 if (res->u.spectrum.id != 0xff) {
1500 IWL_DEBUG_INFO
1501 ("Replaced existing measurement: %d\n",
1502 res->u.spectrum.id);
1503 priv->measurement_status &= ~MEASUREMENT_READY;
1504 }
1505 priv->measurement_status |= MEASUREMENT_ACTIVE;
1506 rc = 0;
1507 break;
1508
1509 case 1: /* Command will not be handled */
1510 rc = -EAGAIN;
1511 break;
1512 }
1513
1514 dev_kfree_skb_any(cmd.meta.u.skb);
1515
1516 return rc;
1517}
1518#endif
1519
Zhu Yib481de92007-09-25 17:54:57 -07001520/******************************************************************************
1521 *
1522 * Generic RX handler implementations
1523 *
1524 ******************************************************************************/
Tomas Winkler885ba202008-05-29 16:34:55 +08001525static void iwl_rx_reply_alive(struct iwl_priv *priv,
1526 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001527{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001528 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Tomas Winkler885ba202008-05-29 16:34:55 +08001529 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07001530 struct delayed_work *pwork;
1531
1532 palive = &pkt->u.alive_frame;
1533
1534 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
1535 "0x%01X 0x%01X\n",
1536 palive->is_valid, palive->ver_type,
1537 palive->ver_subtype);
1538
1539 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
1540 IWL_DEBUG_INFO("Initialization Alive received.\n");
1541 memcpy(&priv->card_alive_init,
1542 &pkt->u.alive_frame,
Tomas Winkler885ba202008-05-29 16:34:55 +08001543 sizeof(struct iwl_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001544 pwork = &priv->init_alive_start;
1545 } else {
1546 IWL_DEBUG_INFO("Runtime Alive received.\n");
1547 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler885ba202008-05-29 16:34:55 +08001548 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001549 pwork = &priv->alive_start;
1550 }
1551
1552 /* We delay the ALIVE response by 5ms to
1553 * give the HW RF Kill time to activate... */
1554 if (palive->is_valid == UCODE_VALID_OK)
1555 queue_delayed_work(priv->workqueue, pwork,
1556 msecs_to_jiffies(5));
1557 else
1558 IWL_WARNING("uCode did not respond OK.\n");
1559}
1560
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001561static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001562 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001563{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001564 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001565
1566 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
1567 "seq 0x%04X ser 0x%08X\n",
1568 le32_to_cpu(pkt->u.err_resp.error_type),
1569 get_cmd_string(pkt->u.err_resp.cmd_id),
1570 pkt->u.err_resp.cmd_id,
1571 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1572 le32_to_cpu(pkt->u.err_resp.error_info));
1573}
1574
1575#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1576
Tomas Winklera55360e2008-05-05 10:22:28 +08001577static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001578{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001579 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08001580 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001581 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07001582 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
1583 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
1584 rxon->channel = csa->channel;
1585 priv->staging_rxon.channel = csa->channel;
1586}
1587
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001588static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001589 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001590{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001591#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Tomas Winklerdb11d632008-05-05 10:22:33 +08001592 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001593 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07001594
1595 if (!report->state) {
Ester Kummerf3d67992008-05-06 11:05:12 +08001596 IWL_DEBUG(IWL_DL_11H,
1597 "Spectrum Measure Notification: Start\n");
Zhu Yib481de92007-09-25 17:54:57 -07001598 return;
1599 }
1600
1601 memcpy(&priv->measure_report, report, sizeof(*report));
1602 priv->measurement_status |= MEASUREMENT_READY;
1603#endif
1604}
1605
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001606static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001607 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001608{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001609#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08001610 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001611 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07001612 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
1613 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1614#endif
1615}
1616
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001617static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001618 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001619{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001620 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001621 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
1622 "notification for %s:\n",
1623 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Ester Kummerbf403db2008-05-05 10:22:40 +08001624 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07001625}
1626
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001627static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07001628{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001629 struct iwl_priv *priv =
1630 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07001631 struct sk_buff *beacon;
1632
1633 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02001634 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07001635
1636 if (!beacon) {
1637 IWL_ERROR("update beacon failed\n");
1638 return;
1639 }
1640
1641 mutex_lock(&priv->mutex);
1642 /* new beacon skb is allocated every time; dispose previous.*/
1643 if (priv->ibss_beacon)
1644 dev_kfree_skb(priv->ibss_beacon);
1645
1646 priv->ibss_beacon = beacon;
1647 mutex_unlock(&priv->mutex);
1648
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001649 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001650}
1651
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08001652/**
1653 * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
1654 *
1655 * This callback is provided in order to send a statistics request.
1656 *
1657 * This timer function is continually reset to execute within
1658 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
1659 * was received. We need to ensure we receive the statistics in order
1660 * to update the temperature used for calibrating the TXPOWER.
1661 */
1662static void iwl4965_bg_statistics_periodic(unsigned long data)
1663{
1664 struct iwl_priv *priv = (struct iwl_priv *)data;
1665
1666 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1667 return;
1668
1669 iwl_send_statistics_request(priv, CMD_ASYNC);
1670}
1671
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001672static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001673 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001674{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001675#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08001676 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001677 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
1678 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001679
1680 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
1681 "tsf %d %d rate %d\n",
1682 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
1683 beacon->beacon_notify_hdr.failure_frame,
1684 le32_to_cpu(beacon->ibss_mgr_status),
1685 le32_to_cpu(beacon->high_tsf),
1686 le32_to_cpu(beacon->low_tsf), rate);
1687#endif
1688
1689 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
1690 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
1691 queue_work(priv->workqueue, &priv->beacon_update);
1692}
1693
1694/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001695static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001696 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001697{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001698#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08001699 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001700 struct iwl4965_scanreq_notification *notif =
1701 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07001702
1703 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
1704#endif
1705}
1706
1707/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001708static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001709 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001710{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001711 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001712 struct iwl4965_scanstart_notification *notif =
1713 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07001714 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
1715 IWL_DEBUG_SCAN("Scan start: "
1716 "%d [802.11%s] "
1717 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
1718 notif->channel,
1719 notif->band ? "bg" : "a",
1720 notif->tsf_high,
1721 notif->tsf_low, notif->status, notif->beacon_timer);
1722}
1723
1724/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001725static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001726 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001727{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001728 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001729 struct iwl4965_scanresults_notification *notif =
1730 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07001731
1732 IWL_DEBUG_SCAN("Scan ch.res: "
1733 "%d [802.11%s] "
1734 "(TSF: 0x%08X:%08X) - %d "
1735 "elapsed=%lu usec (%dms since last)\n",
1736 notif->channel,
1737 notif->band ? "bg" : "a",
1738 le32_to_cpu(notif->tsf_high),
1739 le32_to_cpu(notif->tsf_low),
1740 le32_to_cpu(notif->statistics[0]),
1741 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
1742 jiffies_to_msecs(elapsed_jiffies
1743 (priv->last_scan_jiffies, jiffies)));
1744
1745 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08001746 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001747}
1748
1749/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001750static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001751 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001752{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001753 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001754 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07001755
1756 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
1757 scan_notif->scanned_channels,
1758 scan_notif->tsf_low,
1759 scan_notif->tsf_high, scan_notif->status);
1760
1761 /* The HW is no longer scanning */
1762 clear_bit(STATUS_SCAN_HW, &priv->status);
1763
1764 /* The scan completion notification came in, so kill that timer... */
1765 cancel_delayed_work(&priv->scan_check);
1766
1767 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
1768 (priv->scan_bands == 2) ? "2.4" : "5.2",
1769 jiffies_to_msecs(elapsed_jiffies
1770 (priv->scan_pass_start, jiffies)));
1771
1772 /* Remove this scanned band from the list
1773 * of pending bands to scan */
1774 priv->scan_bands--;
1775
1776 /* If a request to abort was given, or the scan did not succeed
1777 * then we reset the scan state machine and terminate,
1778 * re-queuing another scan if one has been requested */
1779 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1780 IWL_DEBUG_INFO("Aborted scan completed.\n");
1781 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1782 } else {
1783 /* If there are more bands on this scan pass reschedule */
1784 if (priv->scan_bands > 0)
1785 goto reschedule;
1786 }
1787
1788 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08001789 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001790 IWL_DEBUG_INFO("Setting scan to off\n");
1791
1792 clear_bit(STATUS_SCANNING, &priv->status);
1793
1794 IWL_DEBUG_INFO("Scan took %dms\n",
1795 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
1796
1797 queue_work(priv->workqueue, &priv->scan_completed);
1798
1799 return;
1800
1801reschedule:
1802 priv->scan_pass_start = jiffies;
1803 queue_work(priv->workqueue, &priv->request_scan);
1804}
1805
1806/* Handle notification from uCode that card's power state is changing
1807 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001808static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001809 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001810{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001811 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001812 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
1813 unsigned long status = priv->status;
1814
1815 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
1816 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1817 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
1818
1819 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
1820 RF_CARD_DISABLED)) {
1821
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001822 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001823 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1824
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001825 if (!iwl_grab_nic_access(priv)) {
1826 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07001827 priv, HBUS_TARG_MBX_C,
1828 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1829
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001830 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001831 }
1832
1833 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001834 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07001835 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001836 if (!iwl_grab_nic_access(priv)) {
1837 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07001838 priv, HBUS_TARG_MBX_C,
1839 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1840
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001841 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001842 }
1843 }
1844
1845 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001846 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001847 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001848 iwl_read32(priv, CSR_UCODE_DRV_GP1);
1849 if (!iwl_grab_nic_access(priv))
1850 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001851 }
1852 }
1853
1854 if (flags & HW_CARD_DISABLED)
1855 set_bit(STATUS_RF_KILL_HW, &priv->status);
1856 else
1857 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1858
1859
1860 if (flags & SW_CARD_DISABLED)
1861 set_bit(STATUS_RF_KILL_SW, &priv->status);
1862 else
1863 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1864
1865 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001866 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001867
1868 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
1869 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
1870 (test_bit(STATUS_RF_KILL_SW, &status) !=
1871 test_bit(STATUS_RF_KILL_SW, &priv->status)))
1872 queue_work(priv->workqueue, &priv->rf_kill);
1873 else
1874 wake_up_interruptible(&priv->wait_command_queue);
1875}
1876
Ron Rindjunsky37a44212008-05-29 16:35:18 +08001877/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
1878 * This will be used later in iwl4965_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
1879static void iwl4965_rx_reply_rx_phy(struct iwl_priv *priv,
1880 struct iwl_rx_mem_buffer *rxb)
1881{
1882 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1883 priv->last_phy_res[0] = 1;
1884 memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
1885 sizeof(struct iwl4965_rx_phy_res));
1886}
1887
Zhu Yib481de92007-09-25 17:54:57 -07001888/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001889 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07001890 *
1891 * Setup the RX handlers for each of the reply types sent from the uCode
1892 * to the host.
1893 *
1894 * This function chains into the hardware specific files for them to setup
1895 * any hardware specific handlers as well.
1896 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001897static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001898{
Tomas Winkler885ba202008-05-29 16:34:55 +08001899 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001900 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
1901 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07001902 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001903 iwl4965_rx_spectrum_measure_notif;
1904 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001905 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001906 iwl4965_rx_pm_debug_statistics_notif;
1907 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001908
Ben Cahill9fbab512007-11-29 11:09:47 +08001909 /*
1910 * The same handler is used for both the REPLY to a discrete
1911 * statistics request from the host as well as for the periodic
1912 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07001913 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001914 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
1915 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Ron Rindjunsky37a44212008-05-29 16:35:18 +08001916 /* scan handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001917 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
1918 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001919 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001920 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001921 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001922 iwl4965_rx_scan_complete_notif;
Ron Rindjunsky37a44212008-05-29 16:35:18 +08001923 /* status change handler */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001924 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001925
Tomas Winklerc1354752008-05-29 16:35:04 +08001926 priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
1927 iwl_rx_missed_beacon_notif;
Ron Rindjunsky37a44212008-05-29 16:35:18 +08001928 /* Rx handlers */
1929 priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
1930 priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl4965_rx_reply_rx;
Ben Cahill9fbab512007-11-29 11:09:47 +08001931 /* Set up hardware specific Rx handlers */
Emmanuel Grumbachd4789ef2008-04-24 11:55:20 -07001932 priv->cfg->ops->lib->rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001933}
1934
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001935/*
1936 * this should be called while priv->lock is locked
1937*/
Tomas Winklera55360e2008-05-05 10:22:28 +08001938static void __iwl_rx_replenish(struct iwl_priv *priv)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001939{
Tomas Winklera55360e2008-05-05 10:22:28 +08001940 iwl_rx_allocate(priv);
1941 iwl_rx_queue_restock(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001942}
1943
1944
Zhu Yib481de92007-09-25 17:54:57 -07001945/**
Tomas Winklera55360e2008-05-05 10:22:28 +08001946 * iwl_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07001947 *
1948 * Uses the priv->rx_handlers callback function array to invoke
1949 * the appropriate handlers, including command responses,
1950 * frame-received notifications, and other notifications.
1951 */
Tomas Winklera55360e2008-05-05 10:22:28 +08001952void iwl_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001953{
Tomas Winklera55360e2008-05-05 10:22:28 +08001954 struct iwl_rx_mem_buffer *rxb;
Tomas Winklerdb11d632008-05-05 10:22:33 +08001955 struct iwl_rx_packet *pkt;
Tomas Winklera55360e2008-05-05 10:22:28 +08001956 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001957 u32 r, i;
1958 int reclaim;
1959 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001960 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08001961 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07001962
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001963 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1964 * buffer that the driver may process (last buffer filled by ucode). */
Ron Rindjunskyd67f5482008-05-05 10:22:49 +08001965 r = priv->cfg->ops->lib->shared_mem_rx_idx(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001966 i = rxq->read;
1967
1968 /* Rx interrupt, but nothing sent from uCode */
1969 if (i == r)
Ester Kummerf3d67992008-05-06 11:05:12 +08001970 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07001971
Tomas Winklera55360e2008-05-05 10:22:28 +08001972 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001973 fill_rx = 1;
1974
Zhu Yib481de92007-09-25 17:54:57 -07001975 while (i != r) {
1976 rxb = rxq->queue[i];
1977
Ben Cahill9fbab512007-11-29 11:09:47 +08001978 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07001979 * then a bug has been introduced in the queue refilling
1980 * routines -- catch it here */
1981 BUG_ON(rxb == NULL);
1982
1983 rxq->queue[i] = NULL;
1984
1985 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07001986 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07001987 PCI_DMA_FROMDEVICE);
Tomas Winklerdb11d632008-05-05 10:22:33 +08001988 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001989
1990 /* Reclaim a command buffer only if this packet is a response
1991 * to a (driver-originated) command.
1992 * If the packet (e.g. Rx frame) originated from uCode,
1993 * there is no command buffer to reclaim.
1994 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1995 * but apparently a few don't get set; catch them here. */
1996 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1997 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07001998 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08001999 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07002000 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
2001 (pkt->hdr.cmd != REPLY_TX);
2002
2003 /* Based on type of command response or notification,
2004 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002005 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07002006 if (priv->rx_handlers[pkt->hdr.cmd]) {
Ester Kummerf3d67992008-05-06 11:05:12 +08002007 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d, %s, 0x%02x\n", r,
2008 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002009 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
2010 } else {
2011 /* No handling needed */
Ester Kummerf3d67992008-05-06 11:05:12 +08002012 IWL_DEBUG(IWL_DL_RX,
Zhu Yib481de92007-09-25 17:54:57 -07002013 "r %d i %d No handler needed for %s, 0x%02x\n",
2014 r, i, get_cmd_string(pkt->hdr.cmd),
2015 pkt->hdr.cmd);
2016 }
2017
2018 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08002019 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07002020 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07002021 * as we reclaim the driver command queue */
2022 if (rxb && rxb->skb)
Tomas Winkler17b88922008-05-29 16:35:12 +08002023 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07002024 else
2025 IWL_WARNING("Claim null rxb?\n");
2026 }
2027
2028 /* For now we just don't re-use anything. We can tweak this
2029 * later to try and re-use notification packets and SKBs that
2030 * fail to Rx correctly */
2031 if (rxb->skb != NULL) {
2032 priv->alloc_rxb_skb--;
2033 dev_kfree_skb_any(rxb->skb);
2034 rxb->skb = NULL;
2035 }
2036
2037 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07002038 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02002039 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07002040 spin_lock_irqsave(&rxq->lock, flags);
2041 list_add_tail(&rxb->list, &priv->rxq.rx_used);
2042 spin_unlock_irqrestore(&rxq->lock, flags);
2043 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002044 /* If there are a lot of unused frames,
2045 * restock the Rx queue so ucode wont assert. */
2046 if (fill_rx) {
2047 count++;
2048 if (count >= 8) {
2049 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08002050 __iwl_rx_replenish(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002051 count = 0;
2052 }
2053 }
Zhu Yib481de92007-09-25 17:54:57 -07002054 }
2055
2056 /* Backtrack one entry */
2057 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08002058 iwl_rx_queue_restock(priv);
2059}
2060/* Convert linear signal-to-noise ratio into dB */
2061static u8 ratio2dB[100] = {
2062/* 0 1 2 3 4 5 6 7 8 9 */
2063 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
2064 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
2065 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
2066 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
2067 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
2068 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
2069 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
2070 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
2071 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
2072 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
2073};
2074
2075/* Calculates a relative dB value from a ratio of linear
2076 * (i.e. not dB) signal levels.
2077 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
2078int iwl4965_calc_db_from_ratio(int sig_ratio)
2079{
2080 /* 1000:1 or higher just report as 60 dB */
2081 if (sig_ratio >= 1000)
2082 return 60;
2083
2084 /* 100:1 or higher, divide by 10 and use table,
2085 * add 20 dB to make up for divide by 10 */
2086 if (sig_ratio >= 100)
2087 return (20 + (int)ratio2dB[sig_ratio/10]);
2088
2089 /* We shouldn't see this */
2090 if (sig_ratio < 1)
2091 return 0;
2092
2093 /* Use table for ratios 1:1 - 99:1 */
2094 return (int)ratio2dB[sig_ratio];
2095}
2096
2097#define PERFECT_RSSI (-20) /* dBm */
2098#define WORST_RSSI (-95) /* dBm */
2099#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
2100
2101/* Calculate an indication of rx signal quality (a percentage, not dBm!).
2102 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
2103 * about formulas used below. */
2104int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
2105{
2106 int sig_qual;
2107 int degradation = PERFECT_RSSI - rssi_dbm;
2108
2109 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
2110 * as indicator; formula is (signal dbm - noise dbm).
2111 * SNR at or above 40 is a great signal (100%).
2112 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
2113 * Weakest usable signal is usually 10 - 15 dB SNR. */
2114 if (noise_dbm) {
2115 if (rssi_dbm - noise_dbm >= 40)
2116 return 100;
2117 else if (rssi_dbm < noise_dbm)
2118 return 0;
2119 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
2120
2121 /* Else use just the signal level.
2122 * This formula is a least squares fit of data points collected and
2123 * compared with a reference system that had a percentage (%) display
2124 * for signal quality. */
2125 } else
2126 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
2127 (15 * RSSI_RANGE + 62 * degradation)) /
2128 (RSSI_RANGE * RSSI_RANGE);
2129
2130 if (sig_qual > 100)
2131 sig_qual = 100;
2132 else if (sig_qual < 1)
2133 sig_qual = 0;
2134
2135 return sig_qual;
Zhu Yib481de92007-09-25 17:54:57 -07002136}
2137
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002138#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08002139static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002140{
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08002141 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07002142 DECLARE_MAC_BUF(mac);
2143
Zhu Yib481de92007-09-25 17:54:57 -07002144 IWL_DEBUG_RADIO("RX CONFIG:\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002145 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07002146 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
2147 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
2148 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
2149 le32_to_cpu(rxon->filter_flags));
2150 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
2151 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
2152 rxon->ofdm_basic_rates);
2153 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07002154 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
2155 print_mac(mac, rxon->node_addr));
2156 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
2157 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07002158 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
2159}
2160#endif
2161
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002162static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002163{
2164 IWL_DEBUG_ISR("Enabling interrupts\n");
2165 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002166 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07002167}
2168
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002169/* call this function to flush any scheduled tasklet */
2170static inline void iwl_synchronize_irq(struct iwl_priv *priv)
2171{
2172 /* wait to make sure we flush pedding tasklet*/
2173 synchronize_irq(priv->pci_dev->irq);
2174 tasklet_kill(&priv->irq_tasklet);
2175}
2176
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002177static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002178{
2179 clear_bit(STATUS_INT_ENABLED, &priv->status);
2180
2181 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002182 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07002183
2184 /* acknowledge/clear/reset any interrupts still pending
2185 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002186 iwl_write32(priv, CSR_INT, 0xffffffff);
2187 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07002188 IWL_DEBUG_ISR("Disabled interrupts\n");
2189}
2190
Zhu Yib481de92007-09-25 17:54:57 -07002191
Zhu Yib481de92007-09-25 17:54:57 -07002192/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002193 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07002194 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002195static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002196{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002197 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07002198 set_bit(STATUS_FW_ERROR, &priv->status);
2199
2200 /* Cancel currently queued command. */
2201 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
2202
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002203#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08002204 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Ester Kummerede0cba2008-05-29 16:34:46 +08002205 iwl_dump_nic_error_log(priv);
Ester Kummer189a2b52008-05-15 13:54:18 +08002206 iwl_dump_nic_event_log(priv);
Ester Kummerbf403db2008-05-05 10:22:40 +08002207 iwl4965_print_rx_config_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002208 }
2209#endif
2210
2211 wake_up_interruptible(&priv->wait_command_queue);
2212
2213 /* Keep the restart process from trying to send host
2214 * commands by clearing the INIT status bit */
2215 clear_bit(STATUS_READY, &priv->status);
2216
2217 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08002218 IWL_DEBUG(IWL_DL_FW_ERRORS,
Zhu Yib481de92007-09-25 17:54:57 -07002219 "Restarting adapter due to uCode error.\n");
2220
Tomas Winkler3109ece2008-03-28 16:33:35 -07002221 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002222 memcpy(&priv->recovery_rxon, &priv->active_rxon,
2223 sizeof(priv->recovery_rxon));
2224 priv->error_recovering = 1;
2225 }
Ester Kummer3a1081e2008-05-06 11:05:14 +08002226 if (priv->cfg->mod_params->restart_fw)
2227 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07002228 }
2229}
2230
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002231static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002232{
2233 unsigned long flags;
2234
2235 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
2236 sizeof(priv->staging_rxon));
2237 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002238 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002239
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08002240 iwl_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07002241
2242 spin_lock_irqsave(&priv->lock, flags);
2243 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
2244 priv->error_recovering = 0;
2245 spin_unlock_irqrestore(&priv->lock, flags);
2246}
2247
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002248static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002249{
2250 u32 inta, handled = 0;
2251 u32 inta_fh;
2252 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002253#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002254 u32 inta_mask;
2255#endif
2256
2257 spin_lock_irqsave(&priv->lock, flags);
2258
2259 /* Ack/clear/reset pending uCode interrupts.
2260 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
2261 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002262 inta = iwl_read32(priv, CSR_INT);
2263 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07002264
2265 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
2266 * Any new interrupts that happen after this, either while we're
2267 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002268 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
2269 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07002270
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002271#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08002272 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08002273 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002274 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07002275 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
2276 inta, inta_mask, inta_fh);
2277 }
2278#endif
2279
2280 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
2281 * atomic, make sure that inta covers all the interrupts that
2282 * we've discovered, even if FH interrupt came in just after
2283 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08002284 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07002285 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08002286 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07002287 inta |= CSR_INT_BIT_FH_TX;
2288
2289 /* Now service all interrupt bits discovered above. */
2290 if (inta & CSR_INT_BIT_HW_ERR) {
2291 IWL_ERROR("Microcode HW error detected. Restarting.\n");
2292
2293 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002294 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002295
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002296 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002297
2298 handled |= CSR_INT_BIT_HW_ERR;
2299
2300 spin_unlock_irqrestore(&priv->lock, flags);
2301
2302 return;
2303 }
2304
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002305#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08002306 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07002307 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08002308 if (inta & CSR_INT_BIT_SCD)
2309 IWL_DEBUG_ISR("Scheduler finished to transmit "
2310 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002311
2312 /* Alive notification via Rx interrupt will do the real work */
2313 if (inta & CSR_INT_BIT_ALIVE)
2314 IWL_DEBUG_ISR("Alive interrupt\n");
2315 }
2316#endif
2317 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08002318 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07002319
Ben Cahill9fbab512007-11-29 11:09:47 +08002320 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07002321 if (inta & CSR_INT_BIT_RF_KILL) {
2322 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002323 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07002324 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
2325 hw_rf_kill = 1;
2326
Ester Kummerf3d67992008-05-06 11:05:12 +08002327 IWL_DEBUG(IWL_DL_RF_KILL, "RF_KILL bit toggled to %s.\n",
Zhu Yib481de92007-09-25 17:54:57 -07002328 hw_rf_kill ? "disable radio":"enable radio");
2329
2330 /* Queue restart only if RF_KILL switch was set to "kill"
2331 * when we loaded driver, and is now set to "enable".
2332 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08002333 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08002334 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
2335 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002336 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08002337 }
Zhu Yib481de92007-09-25 17:54:57 -07002338
2339 handled |= CSR_INT_BIT_RF_KILL;
2340 }
2341
Ben Cahill9fbab512007-11-29 11:09:47 +08002342 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07002343 if (inta & CSR_INT_BIT_CT_KILL) {
2344 IWL_ERROR("Microcode CT kill error detected.\n");
2345 handled |= CSR_INT_BIT_CT_KILL;
2346 }
2347
2348 /* Error detected by uCode */
2349 if (inta & CSR_INT_BIT_SW_ERR) {
2350 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
2351 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002352 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002353 handled |= CSR_INT_BIT_SW_ERR;
2354 }
2355
2356 /* uCode wakes up after power-down sleep */
2357 if (inta & CSR_INT_BIT_WAKEUP) {
2358 IWL_DEBUG_ISR("Wakeup interrupt\n");
Tomas Winklera55360e2008-05-05 10:22:28 +08002359 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Tomas Winklerbabcebf2008-05-15 13:54:00 +08002360 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
2361 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
2362 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
2363 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
2364 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
2365 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07002366
2367 handled |= CSR_INT_BIT_WAKEUP;
2368 }
2369
2370 /* All uCode command responses, including Tx command responses,
2371 * Rx "responses" (frame-received notification), and other
2372 * notifications from uCode come through here*/
2373 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Tomas Winklera55360e2008-05-05 10:22:28 +08002374 iwl_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002375 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
2376 }
2377
2378 if (inta & CSR_INT_BIT_FH_TX) {
2379 IWL_DEBUG_ISR("Tx interrupt\n");
2380 handled |= CSR_INT_BIT_FH_TX;
Ron Rindjunskydbb983b2008-05-15 13:54:12 +08002381 /* FH finished to write, send event */
2382 priv->ucode_write_complete = 1;
2383 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002384 }
2385
2386 if (inta & ~handled)
2387 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
2388
2389 if (inta & ~CSR_INI_SET_MASK) {
2390 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
2391 inta & ~CSR_INI_SET_MASK);
2392 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
2393 }
2394
2395 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002396 /* only Re-enable if diabled by irq */
2397 if (test_bit(STATUS_INT_ENABLED, &priv->status))
2398 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002399
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002400#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08002401 if (priv->debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002402 inta = iwl_read32(priv, CSR_INT);
2403 inta_mask = iwl_read32(priv, CSR_INT_MASK);
2404 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07002405 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
2406 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
2407 }
2408#endif
2409 spin_unlock_irqrestore(&priv->lock, flags);
2410}
2411
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002412static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07002413{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002414 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07002415 u32 inta, inta_mask;
2416 u32 inta_fh;
2417 if (!priv)
2418 return IRQ_NONE;
2419
2420 spin_lock(&priv->lock);
2421
2422 /* Disable (but don't clear!) interrupts here to avoid
2423 * back-to-back ISRs and sporadic interrupts from our NIC.
2424 * If we have something to service, the tasklet will re-enable ints.
2425 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002426 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
2427 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07002428
2429 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002430 inta = iwl_read32(priv, CSR_INT);
2431 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07002432
2433 /* Ignore interrupt if there's nothing in NIC to service.
2434 * This may be due to IRQ shared with another device,
2435 * or due to sporadic interrupts thrown from our NIC. */
2436 if (!inta && !inta_fh) {
2437 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
2438 goto none;
2439 }
2440
2441 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08002442 /* Hardware disappeared. It might have already raised
2443 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07002444 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08002445 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07002446 }
2447
2448 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
2449 inta, inta_mask, inta_fh);
2450
Joonwoo Park25c03d82008-01-23 10:15:20 -08002451 inta &= ~CSR_INT_BIT_SCD;
2452
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002453 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08002454 if (likely(inta || inta_fh))
2455 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07002456
Oliver Neukum66fbb542007-11-15 09:31:10 +08002457 unplugged:
2458 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07002459 return IRQ_HANDLED;
2460
2461 none:
2462 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002463 /* only Re-enable if diabled by irq */
2464 if (test_bit(STATUS_INT_ENABLED, &priv->status))
2465 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002466 spin_unlock(&priv->lock);
2467 return IRQ_NONE;
2468}
2469
Zhu Yib481de92007-09-25 17:54:57 -07002470/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
2471 * sending probe req. This should be set long enough to hear probe responses
2472 * from more than one AP. */
2473#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
2474#define IWL_ACTIVE_DWELL_TIME_52 (10)
2475
2476/* For faster active scanning, scan will move to the next channel if fewer than
2477 * PLCP_QUIET_THRESH packets are heard on this channel within
2478 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
2479 * time if it's a quiet channel (nothing responded to our probe, and there's
2480 * no other traffic).
2481 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
2482#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
2483#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
2484
2485/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
2486 * Must be set longer than active dwell time.
2487 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
2488#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
2489#define IWL_PASSIVE_DWELL_TIME_52 (10)
2490#define IWL_PASSIVE_DWELL_BASE (100)
2491#define IWL_CHANNEL_TUNE_TIME 5
2492
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002493static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002494 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002495{
Johannes Berg8318d782008-01-24 19:38:38 +01002496 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07002497 return IWL_ACTIVE_DWELL_TIME_52;
2498 else
2499 return IWL_ACTIVE_DWELL_TIME_24;
2500}
2501
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002502static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002503 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002504{
Johannes Berg8318d782008-01-24 19:38:38 +01002505 u16 active = iwl4965_get_active_dwell_time(priv, band);
2506 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07002507 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
2508 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
2509
Tomas Winkler3109ece2008-03-28 16:33:35 -07002510 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002511 /* If we're associated, we clamp the maximum passive
2512 * dwell time to be 98% of the beacon interval (minus
2513 * 2 * channel tune time) */
2514 passive = priv->beacon_int;
2515 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
2516 passive = IWL_PASSIVE_DWELL_BASE;
2517 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
2518 }
2519
2520 if (passive <= active)
2521 passive = active + 1;
2522
2523 return passive;
2524}
2525
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002526static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002527 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07002528 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002529 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07002530{
2531 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01002532 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002533 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002534 u16 passive_dwell = 0;
2535 u16 active_dwell = 0;
2536 int added, i;
2537
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07002538 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01002539 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07002540 return 0;
2541
Johannes Berg8318d782008-01-24 19:38:38 +01002542 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07002543
Johannes Berg8318d782008-01-24 19:38:38 +01002544 active_dwell = iwl4965_get_active_dwell_time(priv, band);
2545 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07002546
Johannes Berg8318d782008-01-24 19:38:38 +01002547 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02002548 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
2549 continue;
2550
Johannes Berg8318d782008-01-24 19:38:38 +01002551 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07002552
Assaf Krauss8622e702008-03-21 13:53:43 -07002553 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08002554 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07002555 if (!is_channel_valid(ch_info)) {
2556 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
2557 scan_ch->channel);
2558 continue;
2559 }
2560
2561 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01002562 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07002563 scan_ch->type = 0; /* passive */
2564 else
2565 scan_ch->type = 1; /* active */
2566
2567 if (scan_ch->type & 1)
2568 scan_ch->type |= (direct_mask << 1);
2569
Zhu Yib481de92007-09-25 17:54:57 -07002570 scan_ch->active_dwell = cpu_to_le16(active_dwell);
2571 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
2572
Ben Cahill9fbab512007-11-29 11:09:47 +08002573 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07002574 scan_ch->tpc.dsp_atten = 110;
2575 /* scan_pwr_info->tpc.dsp_atten; */
2576
2577 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01002578 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07002579 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
2580 else {
2581 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
2582 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08002583 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08002584 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07002585 */
2586 }
2587
2588 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
2589 scan_ch->channel,
2590 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
2591 (scan_ch->type & 1) ?
2592 active_dwell : passive_dwell);
2593
2594 scan_ch++;
2595 added++;
2596 }
2597
2598 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
2599 return added;
2600}
2601
Zhu Yib481de92007-09-25 17:54:57 -07002602/******************************************************************************
2603 *
2604 * uCode download functions
2605 *
2606 ******************************************************************************/
2607
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002608static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002609{
Tomas Winkler98c92212008-01-14 17:46:20 -08002610 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
2611 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
2612 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
2613 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
2614 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
2615 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07002616}
2617
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08002618static void iwl4965_nic_start(struct iwl_priv *priv)
2619{
2620 /* Remove all resets to allow NIC to operate */
2621 iwl_write32(priv, CSR_RESET, 0);
2622}
2623
2624
Zhu Yib481de92007-09-25 17:54:57 -07002625/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002626 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07002627 *
2628 * Copy into buffers for card to fetch via bus-mastering
2629 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002630static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002631{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002632 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002633 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002634 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08002635 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07002636 u8 *src;
2637 size_t len;
2638 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
2639
2640 /* Ask kernel firmware_class module to get the boot firmware off disk.
2641 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08002642 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
2643 if (ret < 0) {
2644 IWL_ERROR("%s firmware file req failed: Reason %d\n",
2645 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07002646 goto error;
2647 }
2648
2649 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
2650 name, ucode_raw->size);
2651
2652 /* Make sure that we got at least our header! */
2653 if (ucode_raw->size < sizeof(*ucode)) {
2654 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002655 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002656 goto err_release;
2657 }
2658
2659 /* Data from ucode file: header followed by uCode images */
2660 ucode = (void *)ucode_raw->data;
2661
2662 ver = le32_to_cpu(ucode->ver);
2663 inst_size = le32_to_cpu(ucode->inst_size);
2664 data_size = le32_to_cpu(ucode->data_size);
2665 init_size = le32_to_cpu(ucode->init_size);
2666 init_data_size = le32_to_cpu(ucode->init_data_size);
2667 boot_size = le32_to_cpu(ucode->boot_size);
2668
2669 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
2670 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
2671 inst_size);
2672 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
2673 data_size);
2674 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
2675 init_size);
2676 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
2677 init_data_size);
2678 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
2679 boot_size);
2680
2681 /* Verify size of file vs. image size info in file's header */
2682 if (ucode_raw->size < sizeof(*ucode) +
2683 inst_size + data_size + init_size +
2684 init_data_size + boot_size) {
2685
2686 IWL_DEBUG_INFO("uCode file size %d too small\n",
2687 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002688 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002689 goto err_release;
2690 }
2691
2692 /* Verify that uCode images will fit in card's SRAM */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002693 if (inst_size > priv->hw_params.max_inst_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08002694 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
2695 inst_size);
2696 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002697 goto err_release;
2698 }
2699
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002700 if (data_size > priv->hw_params.max_data_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08002701 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
2702 data_size);
2703 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002704 goto err_release;
2705 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002706 if (init_size > priv->hw_params.max_inst_size) {
Zhu Yib481de92007-09-25 17:54:57 -07002707 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08002708 ("uCode init instr len %d too large to fit in\n",
2709 init_size);
2710 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002711 goto err_release;
2712 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002713 if (init_data_size > priv->hw_params.max_data_size) {
Zhu Yib481de92007-09-25 17:54:57 -07002714 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08002715 ("uCode init data len %d too large to fit in\n",
2716 init_data_size);
2717 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002718 goto err_release;
2719 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002720 if (boot_size > priv->hw_params.max_bsm_size) {
Zhu Yib481de92007-09-25 17:54:57 -07002721 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08002722 ("uCode boot instr len %d too large to fit in\n",
2723 boot_size);
2724 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002725 goto err_release;
2726 }
2727
2728 /* Allocate ucode buffers for card's bus-master loading ... */
2729
2730 /* Runtime instructions and 2 copies of data:
2731 * 1) unmodified from disk
2732 * 2) backup cache for save/restore during power-downs */
2733 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002734 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07002735
2736 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002737 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07002738
2739 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002740 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07002741
2742 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08002743 if (init_size && init_data_size) {
2744 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002745 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07002746
Tomas Winkler90e759d2007-11-29 11:09:41 +08002747 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002748 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002749
2750 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2751 goto err_pci_alloc;
2752 }
Zhu Yib481de92007-09-25 17:54:57 -07002753
2754 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08002755 if (boot_size) {
2756 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002757 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07002758
Tomas Winkler90e759d2007-11-29 11:09:41 +08002759 if (!priv->ucode_boot.v_addr)
2760 goto err_pci_alloc;
2761 }
Zhu Yib481de92007-09-25 17:54:57 -07002762
2763 /* Copy images into buffers for card's bus-master reads ... */
2764
2765 /* Runtime instructions (first block of data in file) */
2766 src = &ucode->data[0];
2767 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002768 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002769 memcpy(priv->ucode_code.v_addr, src, len);
2770 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
2771 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2772
2773 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002774 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07002775 src = &ucode->data[inst_size];
2776 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002777 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002778 memcpy(priv->ucode_data.v_addr, src, len);
2779 memcpy(priv->ucode_data_backup.v_addr, src, len);
2780
2781 /* Initialization instructions (3rd block) */
2782 if (init_size) {
2783 src = &ucode->data[inst_size + data_size];
2784 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002785 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
2786 len);
Zhu Yib481de92007-09-25 17:54:57 -07002787 memcpy(priv->ucode_init.v_addr, src, len);
2788 }
2789
2790 /* Initialization data (4th block) */
2791 if (init_data_size) {
2792 src = &ucode->data[inst_size + data_size + init_size];
2793 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002794 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
2795 len);
Zhu Yib481de92007-09-25 17:54:57 -07002796 memcpy(priv->ucode_init_data.v_addr, src, len);
2797 }
2798
2799 /* Bootstrap instructions (5th block) */
2800 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
2801 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08002802 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002803 memcpy(priv->ucode_boot.v_addr, src, len);
2804
2805 /* We have our copies now, allow OS release its copies */
2806 release_firmware(ucode_raw);
2807 return 0;
2808
2809 err_pci_alloc:
2810 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002811 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002812 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002813
2814 err_release:
2815 release_firmware(ucode_raw);
2816
2817 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08002818 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002819}
2820
Zhu Yib481de92007-09-25 17:54:57 -07002821/**
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002822 * iwl_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002823 * from protocol/runtime uCode (initialization uCode's
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002824 * Alive gets handled by iwl_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002825 */
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002826static void iwl_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002827{
Tomas Winkler57aab752008-04-14 21:16:03 -07002828 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002829
2830 IWL_DEBUG_INFO("Runtime Alive received.\n");
2831
2832 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2833 /* We had an error bringing up the hardware, so take it
2834 * all the way back down so we can try again */
2835 IWL_DEBUG_INFO("Alive failed.\n");
2836 goto restart;
2837 }
2838
2839 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2840 * This is a paranoid check, because we would not have gotten the
2841 * "runtime" alive if code weren't properly loaded. */
Emmanuel Grumbachb0692f22008-04-24 11:55:18 -07002842 if (iwl_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002843 /* Runtime instruction load was bad;
2844 * take it all the way back down so we can try again */
2845 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
2846 goto restart;
2847 }
2848
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002849 iwlcore_clear_stations_table(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07002850 ret = priv->cfg->ops->lib->alive_notify(priv);
2851 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07002852 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07002853 ret);
Zhu Yib481de92007-09-25 17:54:57 -07002854 goto restart;
2855 }
2856
Ben Cahill9fbab512007-11-29 11:09:47 +08002857 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002858 set_bit(STATUS_ALIVE, &priv->status);
2859
2860 /* Clear out the uCode error bit if it is set */
2861 clear_bit(STATUS_FW_ERROR, &priv->status);
2862
Tomas Winklerfee12472008-04-03 16:05:21 -07002863 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002864 return;
2865
Johannes Berg36d68252008-05-15 12:55:26 +02002866 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002867
2868 priv->active_rate = priv->rates_mask;
2869 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2870
Tomas Winkler3109ece2008-03-28 16:33:35 -07002871 if (iwl_is_associated(priv)) {
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08002872 struct iwl_rxon_cmd *active_rxon =
2873 (struct iwl_rxon_cmd *)&priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002874
2875 memcpy(&priv->staging_rxon, &priv->active_rxon,
2876 sizeof(priv->staging_rxon));
2877 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2878 } else {
2879 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002880 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002881 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2882 }
2883
Ben Cahill9fbab512007-11-29 11:09:47 +08002884 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002885 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002886
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002887 iwl_reset_run_time_calib(priv);
2888
Zhu Yib481de92007-09-25 17:54:57 -07002889 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002890 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002891
2892 /* At this point, the NIC is initialized and operational */
Zhu Yib481de92007-09-25 17:54:57 -07002893 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002894
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002895 iwl_leds_register(priv);
2896
Zhu Yib481de92007-09-25 17:54:57 -07002897 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002898 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002899 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002900
2901 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002902 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002903
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07002904 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Mohamed Abbas84363e62008-04-04 16:59:58 -07002905 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Zhu Yib481de92007-09-25 17:54:57 -07002906 return;
2907
2908 restart:
2909 queue_work(priv->workqueue, &priv->restart);
2910}
2911
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08002912static void iwl_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002913
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002914static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002915{
2916 unsigned long flags;
2917 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002918
2919 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
2920
Zhu Yib481de92007-09-25 17:54:57 -07002921 if (!exit_pending)
2922 set_bit(STATUS_EXIT_PENDING, &priv->status);
2923
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002924 iwl_leds_unregister(priv);
2925
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07002926 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
2927
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002928 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002929
2930 /* Unblock any waiting calls */
2931 wake_up_interruptible_all(&priv->wait_command_queue);
2932
Zhu Yib481de92007-09-25 17:54:57 -07002933 /* Wipe out the EXIT_PENDING status bit if we are not actually
2934 * exiting the module */
2935 if (!exit_pending)
2936 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2937
2938 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002939 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002940
2941 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002942 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002943 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002944 spin_unlock_irqrestore(&priv->lock, flags);
2945 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002946
2947 if (priv->mac80211_registered)
2948 ieee80211_stop_queues(priv->hw);
2949
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002950 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07002951 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07002952 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002953 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2954 STATUS_RF_KILL_HW |
2955 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2956 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08002957 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2958 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002959 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
2960 STATUS_IN_SUSPEND;
2961 goto exit;
2962 }
2963
2964 /* ...otherwise clear out all the status bits but the RF Kill and
2965 * SUSPEND bits and continue taking the NIC down. */
2966 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2967 STATUS_RF_KILL_HW |
2968 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2969 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08002970 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2971 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002972 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
2973 STATUS_IN_SUSPEND |
2974 test_bit(STATUS_FW_ERROR, &priv->status) <<
2975 STATUS_FW_ERROR;
2976
2977 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002978 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08002979 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07002980 spin_unlock_irqrestore(&priv->lock, flags);
2981
Tomas Winklerda1bc452008-05-29 16:35:00 +08002982 iwl_txq_ctx_stop(priv);
Tomas Winklerb3bbacb2008-05-29 16:35:01 +08002983 iwl_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002984
2985 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002986 if (!iwl_grab_nic_access(priv)) {
2987 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002988 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002989 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002990 }
2991 spin_unlock_irqrestore(&priv->lock, flags);
2992
2993 udelay(5);
2994
Tomas Winkler7f066102008-05-29 16:34:57 +08002995 /* FIXME: apm_ops.suspend(priv) */
2996 priv->cfg->ops->lib->apm_ops.reset(priv);
Ron Rindjunsky399f4902008-04-23 17:14:56 -07002997 priv->cfg->ops->lib->free_shared_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002998
2999 exit:
Tomas Winkler885ba202008-05-29 16:34:55 +08003000 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003001
3002 if (priv->ibss_beacon)
3003 dev_kfree_skb(priv->ibss_beacon);
3004 priv->ibss_beacon = NULL;
3005
3006 /* clear out any free frames */
Tomas Winklerfcab4232008-05-15 13:54:01 +08003007 iwl_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003008}
3009
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003010static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003011{
3012 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003013 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003014 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08003015
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003016 iwl_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003017}
3018
3019#define MAX_HW_RESTARTS 5
3020
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003021static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003022{
Tomas Winkler57aab752008-04-14 21:16:03 -07003023 int i;
3024 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003025
3026 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3027 IWL_WARNING("Exit pending; will not bring the NIC up\n");
3028 return -EIO;
3029 }
3030
3031 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
3032 IWL_WARNING("Radio disabled by SW RF kill (module "
3033 "parameter)\n");
Mohamed Abbasad97edd2008-03-28 16:21:06 -07003034 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08003035 return -ENODEV;
3036 }
3037
Reinette Chatree903fbd2008-01-30 22:05:15 -08003038 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
3039 IWL_ERROR("ucode not available for device bringup\n");
3040 return -EIO;
3041 }
3042
Zhu Yie655b9f2008-01-24 02:19:38 -08003043 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003044 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08003045 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3046 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3047 else {
3048 set_bit(STATUS_RF_KILL_HW, &priv->status);
3049 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07003050 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08003051 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
3052 return -ENODEV;
3053 }
Zhu Yib481de92007-09-25 17:54:57 -07003054 }
3055
Mohamed Abbasad97edd2008-03-28 16:21:06 -07003056 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003057 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07003058
Ron Rindjunsky399f4902008-04-23 17:14:56 -07003059 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
3060 if (ret) {
3061 IWL_ERROR("Unable to allocate shared memory\n");
3062 return ret;
3063 }
3064
Ron Rindjunsky1053d352008-05-05 10:22:43 +08003065 ret = iwl_hw_nic_init(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07003066 if (ret) {
3067 IWL_ERROR("Unable to init nic\n");
3068 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003069 }
3070
3071 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003072 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3073 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003074 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3075
3076 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003077 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003078 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003079
3080 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003081 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3082 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07003083
3084 /* Copy original ucode data image from disk into backup cache.
3085 * This will be used to initialize the on-board processor's
3086 * data SRAM for a clean start when the runtime program first loads. */
3087 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08003088 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07003089
Zhu Yie655b9f2008-01-24 02:19:38 -08003090 /* We return success when we resume from suspend and rf_kill is on. */
3091 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003092 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003093
3094 for (i = 0; i < MAX_HW_RESTARTS; i++) {
3095
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003096 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003097
3098 /* load bootstrap state machine,
3099 * load bootstrap program into processor's memory,
3100 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07003101 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003102
Tomas Winkler57aab752008-04-14 21:16:03 -07003103 if (ret) {
3104 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07003105 continue;
3106 }
3107
3108 /* start card; "initialize" will load runtime ucode */
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08003109 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003110
Zhu Yib481de92007-09-25 17:54:57 -07003111 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
3112
3113 return 0;
3114 }
3115
3116 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003117 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003118
3119 /* tried to restart and config the device for as long as our
3120 * patience could withstand */
3121 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
3122 return -EIO;
3123}
3124
3125
3126/*****************************************************************************
3127 *
3128 * Workqueue callbacks
3129 *
3130 *****************************************************************************/
3131
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08003132static void iwl_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003133{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003134 struct iwl_priv *priv =
3135 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07003136
3137 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3138 return;
3139
3140 mutex_lock(&priv->mutex);
Emmanuel Grumbachf3ccc082008-05-05 10:22:45 +08003141 priv->cfg->ops->lib->init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003142 mutex_unlock(&priv->mutex);
3143}
3144
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08003145static void iwl_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003146{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003147 struct iwl_priv *priv =
3148 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07003149
3150 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3151 return;
3152
3153 mutex_lock(&priv->mutex);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08003154 iwl_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003155 mutex_unlock(&priv->mutex);
3156}
3157
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003158static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003159{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003160 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07003161
3162 wake_up_interruptible(&priv->wait_command_queue);
3163
3164 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3165 return;
3166
3167 mutex_lock(&priv->mutex);
3168
Tomas Winklerfee12472008-04-03 16:05:21 -07003169 if (!iwl_is_rfkill(priv)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08003170 IWL_DEBUG(IWL_DL_RF_KILL,
Zhu Yib481de92007-09-25 17:54:57 -07003171 "HW and/or SW RF Kill no longer active, restarting "
3172 "device\n");
3173 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
3174 queue_work(priv->workqueue, &priv->restart);
3175 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07003176 /* make sure mac80211 stop sending Tx frame */
3177 if (priv->mac80211_registered)
3178 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07003179
3180 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
3181 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3182 "disabled by SW switch\n");
3183 else
3184 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
3185 "Kill switch must be turned off for "
3186 "wireless networking to work.\n");
3187 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07003188 iwl_rfkill_set_hw_state(priv);
3189
Zhu Yib481de92007-09-25 17:54:57 -07003190 mutex_unlock(&priv->mutex);
3191}
3192
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08003193static void iwl4965_bg_set_monitor(struct work_struct *work)
3194{
3195 struct iwl_priv *priv = container_of(work,
3196 struct iwl_priv, set_monitor);
3197
3198 IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
3199
3200 mutex_lock(&priv->mutex);
3201
3202 if (!iwl_is_ready(priv))
3203 IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
3204 else
3205 if (iwl4965_set_mode(priv, IEEE80211_IF_TYPE_MNTR) != 0)
3206 IWL_ERROR("iwl4965_set_mode() failed\n");
3207
3208 mutex_unlock(&priv->mutex);
3209}
3210
Zhu Yib481de92007-09-25 17:54:57 -07003211#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
3212
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003213static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003214{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003215 struct iwl_priv *priv =
3216 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07003217
3218 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3219 return;
3220
3221 mutex_lock(&priv->mutex);
3222 if (test_bit(STATUS_SCANNING, &priv->status) ||
3223 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08003224 IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
3225 "adapter (%dms)\n",
3226 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08003227
Zhu Yib481de92007-09-25 17:54:57 -07003228 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003229 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003230 }
3231 mutex_unlock(&priv->mutex);
3232}
3233
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003234static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003235{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003236 struct iwl_priv *priv =
3237 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07003238 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003239 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003240 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07003241 .meta.flags = CMD_SIZE_HUGE,
3242 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003243 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07003244 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02003245 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01003246 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02003247 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07003248 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003249
3250 conf = ieee80211_get_hw_conf(priv->hw);
3251
3252 mutex_lock(&priv->mutex);
3253
Tomas Winklerfee12472008-04-03 16:05:21 -07003254 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003255 IWL_WARNING("request scan called when driver not ready.\n");
3256 goto done;
3257 }
3258
3259 /* Make sure the scan wasn't cancelled before this queued work
3260 * was given the chance to run... */
3261 if (!test_bit(STATUS_SCANNING, &priv->status))
3262 goto done;
3263
3264 /* This should never be called or scheduled if there is currently
3265 * a scan active in the hardware. */
3266 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
3267 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
3268 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07003269 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07003270 goto done;
3271 }
3272
3273 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3274 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
3275 goto done;
3276 }
3277
3278 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3279 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
3280 goto done;
3281 }
3282
Tomas Winklerfee12472008-04-03 16:05:21 -07003283 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003284 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
3285 goto done;
3286 }
3287
3288 if (!test_bit(STATUS_READY, &priv->status)) {
3289 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
3290 goto done;
3291 }
3292
3293 if (!priv->scan_bands) {
3294 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
3295 goto done;
3296 }
3297
3298 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003299 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07003300 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
3301 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07003302 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07003303 goto done;
3304 }
3305 }
3306 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003307 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07003308
3309 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
3310 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
3311
Tomas Winkler3109ece2008-03-28 16:33:35 -07003312 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003313 u16 interval = 0;
3314 u32 extra;
3315 u32 suspend_time = 100;
3316 u32 scan_suspend_time = 100;
3317 unsigned long flags;
3318
3319 IWL_DEBUG_INFO("Scanning while associated...\n");
3320
3321 spin_lock_irqsave(&priv->lock, flags);
3322 interval = priv->beacon_int;
3323 spin_unlock_irqrestore(&priv->lock, flags);
3324
3325 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08003326 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07003327 if (!interval)
3328 interval = suspend_time;
3329
3330 extra = (suspend_time / interval) << 22;
3331 scan_suspend_time = (extra |
3332 ((suspend_time % interval) * 1024));
3333 scan->suspend_time = cpu_to_le32(scan_suspend_time);
3334 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
3335 scan_suspend_time, interval);
3336 }
3337
3338 /* We should add the ability for user to lock to PASSIVE ONLY */
3339 if (priv->one_direct_scan) {
3340 IWL_DEBUG_SCAN
3341 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003342 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07003343 priv->direct_ssid_len));
3344 scan->direct_scan[0].id = WLAN_EID_SSID;
3345 scan->direct_scan[0].len = priv->direct_ssid_len;
3346 memcpy(scan->direct_scan[0].ssid,
3347 priv->direct_ssid, priv->direct_ssid_len);
3348 direct_mask = 1;
Tomas Winkler3109ece2008-03-28 16:33:35 -07003349 } else if (!iwl_is_associated(priv) && priv->essid_len) {
Bill Moss786b4552008-04-17 16:03:40 -07003350 IWL_DEBUG_SCAN
3351 ("Kicking off one direct scan for '%s' when not associated\n",
3352 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07003353 scan->direct_scan[0].id = WLAN_EID_SSID;
3354 scan->direct_scan[0].len = priv->essid_len;
3355 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
3356 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07003357 } else {
Bill Moss786b4552008-04-17 16:03:40 -07003358 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003359 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07003360 }
Zhu Yib481de92007-09-25 17:54:57 -07003361
Zhu Yib481de92007-09-25 17:54:57 -07003362 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler5425e492008-04-15 16:01:38 -07003363 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07003364 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
3365
Zhu Yib481de92007-09-25 17:54:57 -07003366
3367 switch (priv->scan_bands) {
3368 case 2:
3369 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
3370 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003371 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07003372 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
3373
3374 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003375 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07003376 break;
3377
3378 case 1:
3379 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003380 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07003381 RATE_MCS_ANT_B_MSK);
3382 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01003383 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07003384 break;
3385
3386 default:
3387 IWL_WARNING("Invalid scan band count\n");
3388 goto done;
3389 }
3390
Tomas Winkler78330fd2008-02-06 02:37:18 +02003391 /* We don't build a direct scan probe request; the uCode will do
3392 * that based on the direct_mask added to each channel entry */
3393 cmd_len = iwl4965_fill_probe_req(priv, band,
3394 (struct ieee80211_mgmt *)scan->data,
3395 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
3396
3397 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07003398 /* select Rx chains */
3399
3400 /* Force use of chains B and C (0x6) for scan Rx.
3401 * Avoid A (0x1) because of its off-channel reception on A-band.
3402 * MIMO is not used here, but value is required to make uCode happy. */
3403 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
3404 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
3405 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
3406 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
3407
3408 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
3409 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
3410
Bill Moss786b4552008-04-17 16:03:40 -07003411 if (direct_mask)
Reinette Chatre26c0f032008-03-11 16:17:15 -07003412 scan->channel_count =
3413 iwl4965_get_channels_for_scan(
3414 priv, band, 1, /* active */
3415 direct_mask,
3416 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Bill Moss786b4552008-04-17 16:03:40 -07003417 else
Reinette Chatre26c0f032008-03-11 16:17:15 -07003418 scan->channel_count =
3419 iwl4965_get_channels_for_scan(
3420 priv, band, 0, /* passive */
3421 direct_mask,
3422 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07003423
Mohamed Abbas5da4b552008-04-21 15:41:51 -07003424 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
3425 RXON_FILTER_BCON_AWARE_MSK);
Zhu Yib481de92007-09-25 17:54:57 -07003426 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003427 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07003428 cmd.data = scan;
3429 scan->len = cpu_to_le16(cmd.len);
3430
3431 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07003432 ret = iwl_send_cmd_sync(priv, &cmd);
3433 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07003434 goto done;
3435
3436 queue_delayed_work(priv->workqueue, &priv->scan_check,
3437 IWL_SCAN_CHECK_WATCHDOG);
3438
3439 mutex_unlock(&priv->mutex);
3440 return;
3441
3442 done:
Ian Schram01ebd062007-10-25 17:15:22 +08003443 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07003444 queue_work(priv->workqueue, &priv->scan_completed);
3445 mutex_unlock(&priv->mutex);
3446}
3447
Emmanuel Grumbach16e727e2008-06-12 09:46:52 +08003448static void iwl_bg_run_time_calib_work(struct work_struct *work)
3449{
3450 struct iwl_priv *priv = container_of(work, struct iwl_priv,
3451 run_time_calib_work);
3452
3453 mutex_lock(&priv->mutex);
3454
3455 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
3456 test_bit(STATUS_SCANNING, &priv->status)) {
3457 mutex_unlock(&priv->mutex);
3458 return;
3459 }
3460
3461 if (priv->start_calib) {
3462 iwl_chain_noise_calibration(priv, &priv->statistics);
3463
3464 iwl_sensitivity_calibration(priv, &priv->statistics);
3465 }
3466
3467 mutex_unlock(&priv->mutex);
3468 return;
3469}
3470
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003471static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003472{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003473 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07003474
3475 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3476 return;
3477
3478 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003479 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003480 mutex_unlock(&priv->mutex);
3481}
3482
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003483static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003484{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003485 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07003486
3487 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3488 return;
3489
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003490 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003491 queue_work(priv->workqueue, &priv->up);
3492}
3493
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003494static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003495{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003496 struct iwl_priv *priv =
3497 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07003498
3499 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3500 return;
3501
3502 mutex_lock(&priv->mutex);
Tomas Winklera55360e2008-05-05 10:22:28 +08003503 iwl_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003504 mutex_unlock(&priv->mutex);
3505}
3506
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003507#define IWL_DELAY_NEXT_SCAN (HZ*2)
3508
Reinette Chatre508e32e2008-04-14 21:16:13 -07003509static void iwl4965_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003510{
Zhu Yib481de92007-09-25 17:54:57 -07003511 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07003512 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07003513 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07003514
3515 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
3516 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
3517 return;
3518 }
3519
Joe Perches0795af52007-10-03 17:59:30 -07003520 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
3521 priv->assoc_id,
3522 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07003523
3524
3525 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3526 return;
3527
Zhu Yib481de92007-09-25 17:54:57 -07003528
Reinette Chatre508e32e2008-04-14 21:16:13 -07003529 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08003530 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07003531
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003532 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08003533
Zhu Yib481de92007-09-25 17:54:57 -07003534 conf = ieee80211_get_hw_conf(priv->hw);
3535
3536 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003537 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003538
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003539 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
3540 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07003541 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07003542 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07003543 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07003544 IWL_WARNING("REPLY_RXON_TIMING failed - "
3545 "Attempting to continue.\n");
3546
3547 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
3548
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02003549 if (priv->current_ht_config.is_ht)
Tomas Winkler47c51962008-05-05 10:22:41 +08003550 iwl_set_rxon_ht(priv, &priv->current_ht_config);
Ron Rindjunsky4f85f5b2008-06-09 22:54:35 +03003551
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07003552 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003553 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
3554
3555 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
3556 priv->assoc_id, priv->beacon_int);
3557
3558 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3559 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
3560 else
3561 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
3562
3563 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
3564 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
3565 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
3566 else
3567 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3568
3569 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
3570 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3571
3572 }
3573
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003574 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003575
3576 switch (priv->iw_mode) {
3577 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003578 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003579 break;
3580
3581 case IEEE80211_IF_TYPE_IBSS:
3582
3583 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003584 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003585
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08003586 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
3587 iwl_rxon_add_station(priv, priv->bssid, 0);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003588 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
3589 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003590
3591 break;
3592
3593 default:
3594 IWL_ERROR("%s Should not be called in %d mode\n",
3595 __FUNCTION__, priv->iw_mode);
3596 break;
3597 }
3598
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003599 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003600
Zhu Yib481de92007-09-25 17:54:57 -07003601 /* Enable Rx differential gain and sensitivity calibrations */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07003602 iwl_chain_noise_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003603 priv->start_calib = 1;
Zhu Yib481de92007-09-25 17:54:57 -07003604
3605 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
3606 priv->assoc_station_added = 1;
3607
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003608 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08003609
Mohamed Abbas5da4b552008-04-21 15:41:51 -07003610 iwl_power_update_mode(priv, 0);
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003611 /* we have just associated, don't start scan too early */
3612 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Reinette Chatre508e32e2008-04-14 21:16:13 -07003613}
3614
3615
3616static void iwl4965_bg_post_associate(struct work_struct *data)
3617{
3618 struct iwl_priv *priv = container_of(data, struct iwl_priv,
3619 post_associate.work);
3620
3621 mutex_lock(&priv->mutex);
3622 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003623 mutex_unlock(&priv->mutex);
Reinette Chatre508e32e2008-04-14 21:16:13 -07003624
Zhu Yib481de92007-09-25 17:54:57 -07003625}
3626
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003627static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003628{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003629 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07003630
Tomas Winklerfee12472008-04-03 16:05:21 -07003631 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003632 return;
3633
3634 mutex_lock(&priv->mutex);
3635
3636 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003637 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003638
3639 mutex_unlock(&priv->mutex);
3640}
3641
Zhu Yi76bb77e2007-11-22 10:53:22 +08003642static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
3643
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003644static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003645{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003646 struct iwl_priv *priv =
3647 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07003648
Ester Kummerf3d67992008-05-06 11:05:12 +08003649 IWL_DEBUG(IWL_DL_SCAN, "SCAN complete scan\n");
Zhu Yib481de92007-09-25 17:54:57 -07003650
3651 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3652 return;
3653
Zhu Yia0646472007-12-20 14:10:01 +08003654 if (test_bit(STATUS_CONF_PENDING, &priv->status))
3655 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08003656
Zhu Yib481de92007-09-25 17:54:57 -07003657 ieee80211_scan_completed(priv->hw);
3658
3659 /* Since setting the TXPOWER may have been deferred while
3660 * performing the scan, fire one off */
3661 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003662 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003663 mutex_unlock(&priv->mutex);
3664}
3665
3666/*****************************************************************************
3667 *
3668 * mac80211 entry point functions
3669 *
3670 *****************************************************************************/
3671
Zhu Yi5a66926a2008-01-14 17:46:18 -08003672#define UCODE_READY_TIMEOUT (2 * HZ)
3673
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003674static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003675{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003676 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003677 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003678
3679 IWL_DEBUG_MAC80211("enter\n");
3680
Zhu Yi5a66926a2008-01-14 17:46:18 -08003681 if (pci_enable_device(priv->pci_dev)) {
3682 IWL_ERROR("Fail to pci_enable_device\n");
3683 return -ENODEV;
3684 }
3685 pci_restore_state(priv->pci_dev);
3686 pci_enable_msi(priv->pci_dev);
3687
3688 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
3689 DRV_NAME, priv);
3690 if (ret) {
3691 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
3692 goto out_disable_msi;
3693 }
3694
Zhu Yib481de92007-09-25 17:54:57 -07003695 /* we should be verifying the device is ready to be opened */
3696 mutex_lock(&priv->mutex);
3697
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08003698 memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
Zhu Yi5a66926a2008-01-14 17:46:18 -08003699 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3700 * ucode filename and max sizes are card-specific. */
3701
3702 if (!priv->ucode_code.len) {
3703 ret = iwl4965_read_ucode(priv);
3704 if (ret) {
3705 IWL_ERROR("Could not read microcode: %d\n", ret);
3706 mutex_unlock(&priv->mutex);
3707 goto out_release_irq;
3708 }
3709 }
3710
Zhu Yie655b9f2008-01-24 02:19:38 -08003711 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003712
Zhu Yib481de92007-09-25 17:54:57 -07003713 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003714
Zhu Yie655b9f2008-01-24 02:19:38 -08003715 if (ret)
3716 goto out_release_irq;
3717
3718 IWL_DEBUG_INFO("Start UP work done.\n");
3719
3720 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
3721 return 0;
3722
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08003723 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
Zhu Yi5a66926a2008-01-14 17:46:18 -08003724 * mac80211 will not be run successfully. */
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08003725 if (priv->ucode_type == UCODE_RT) {
3726 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3727 test_bit(STATUS_READY, &priv->status),
3728 UCODE_READY_TIMEOUT);
3729 if (!ret) {
3730 if (!test_bit(STATUS_READY, &priv->status)) {
3731 IWL_ERROR("START_ALIVE timeout after %dms.\n",
3732 jiffies_to_msecs(UCODE_READY_TIMEOUT));
3733 ret = -ETIMEDOUT;
3734 goto out_release_irq;
3735 }
Zhu Yi5a66926a2008-01-14 17:46:18 -08003736 }
Zhu Yi5a66926a2008-01-14 17:46:18 -08003737
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08003738 priv->is_open = 1;
3739 }
Zhu Yib481de92007-09-25 17:54:57 -07003740 IWL_DEBUG_MAC80211("leave\n");
3741 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003742
3743out_release_irq:
3744 free_irq(priv->pci_dev->irq, priv);
3745out_disable_msi:
3746 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08003747 pci_disable_device(priv->pci_dev);
3748 priv->is_open = 0;
3749 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08003750 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003751}
3752
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003753static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003754{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003755 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003756
3757 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08003758
Zhu Yie655b9f2008-01-24 02:19:38 -08003759 if (!priv->is_open) {
3760 IWL_DEBUG_MAC80211("leave - skip\n");
3761 return;
3762 }
3763
Zhu Yib481de92007-09-25 17:54:57 -07003764 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003765
Tomas Winklerfee12472008-04-03 16:05:21 -07003766 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08003767 /* stop mac, cancel any scan request and clear
3768 * RXON_FILTER_ASSOC_MSK BIT
3769 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08003770 mutex_lock(&priv->mutex);
3771 iwl4965_scan_cancel_timeout(priv, 100);
3772 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003773 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003774 }
3775
Zhu Yi5a66926a2008-01-14 17:46:18 -08003776 iwl4965_down(priv);
3777
3778 flush_workqueue(priv->workqueue);
3779 free_irq(priv->pci_dev->irq, priv);
3780 pci_disable_msi(priv->pci_dev);
3781 pci_save_state(priv->pci_dev);
3782 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08003783
Zhu Yib481de92007-09-25 17:54:57 -07003784 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003785}
3786
Johannes Berge039fa42008-05-15 12:55:29 +02003787static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003788{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003789 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003790
3791 IWL_DEBUG_MAC80211("enter\n");
3792
3793 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
3794 IWL_DEBUG_MAC80211("leave - monitor\n");
3795 return -1;
3796 }
3797
3798 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02003799 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07003800
Johannes Berge039fa42008-05-15 12:55:29 +02003801 if (iwl_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07003802 dev_kfree_skb_any(skb);
3803
3804 IWL_DEBUG_MAC80211("leave\n");
3805 return 0;
3806}
3807
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003808static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003809 struct ieee80211_if_init_conf *conf)
3810{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003811 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003812 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07003813 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07003814
Johannes Berg32bfd352007-12-19 01:31:26 +01003815 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07003816
Johannes Berg32bfd352007-12-19 01:31:26 +01003817 if (priv->vif) {
3818 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08003819 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07003820 }
3821
3822 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01003823 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07003824
3825 spin_unlock_irqrestore(&priv->lock, flags);
3826
3827 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02003828
3829 if (conf->mac_addr) {
3830 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
3831 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
3832 }
Zhu Yib481de92007-09-25 17:54:57 -07003833
Tomas Winklerfee12472008-04-03 16:05:21 -07003834 if (iwl_is_ready(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08003835 iwl4965_set_mode(priv, conf->type);
3836
Zhu Yib481de92007-09-25 17:54:57 -07003837 mutex_unlock(&priv->mutex);
3838
Zhu Yi5a66926a2008-01-14 17:46:18 -08003839 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003840 return 0;
3841}
3842
3843/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003844 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07003845 *
3846 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
3847 * be set inappropriately and the driver currently sets the hardware up to
3848 * use it whenever needed.
3849 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003850static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07003851{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003852 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003853 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07003854 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08003855 int ret = 0;
Tomas Winkler82a66bb2008-05-29 16:35:28 +08003856 u16 channel;
Zhu Yib481de92007-09-25 17:54:57 -07003857
3858 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01003859 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07003860
Zhu Yi12342c42007-12-20 11:27:32 +08003861 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
3862
Tomas Winklerfee12472008-04-03 16:05:21 -07003863 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003864 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08003865 ret = -EIO;
3866 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003867 }
3868
Assaf Krauss1ea87392008-03-18 14:57:50 -07003869 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07003870 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08003871 IWL_DEBUG_MAC80211("leave - scanning\n");
3872 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003873 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08003874 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003875 }
3876
Tomas Winkler82a66bb2008-05-29 16:35:28 +08003877 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
3878 ch_info = iwl_get_channel_info(priv, conf->channel->band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07003879 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07003880 IWL_DEBUG_MAC80211("leave - invalid channel\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08003881 ret = -EINVAL;
3882 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003883 }
3884
Tomas Winkler82a66bb2008-05-29 16:35:28 +08003885 spin_lock_irqsave(&priv->lock, flags);
3886
Tomas Winkler78330fd2008-02-06 02:37:18 +02003887 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07003888 * from any ht related info since 2.4 does not
3889 * support ht */
Tomas Winkler82a66bb2008-05-29 16:35:28 +08003890 if ((le16_to_cpu(priv->staging_rxon.channel) != channel)
Zhu Yib481de92007-09-25 17:54:57 -07003891#ifdef IEEE80211_CONF_CHANNEL_SWITCH
3892 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
3893#endif
3894 )
3895 priv->staging_rxon.flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003896
Tomas Winkler82a66bb2008-05-29 16:35:28 +08003897 iwl_set_rxon_channel(priv, conf->channel->band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07003898
Tomas Winkler82a66bb2008-05-29 16:35:28 +08003899 iwl_set_flags_for_band(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07003900
3901 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01003902 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07003903 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003904 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003905
3906 spin_unlock_irqrestore(&priv->lock, flags);
3907
3908#ifdef IEEE80211_CONF_CHANNEL_SWITCH
3909 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003910 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08003911 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003912 }
3913#endif
3914
Mohamed Abbasad97edd2008-03-28 16:21:06 -07003915 if (priv->cfg->ops->lib->radio_kill_sw)
3916 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07003917
3918 if (!conf->radio_enabled) {
3919 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08003920 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003921 }
3922
Tomas Winklerfee12472008-04-03 16:05:21 -07003923 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003924 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08003925 ret = -EIO;
3926 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003927 }
3928
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003929 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003930
3931 if (memcmp(&priv->active_rxon,
3932 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003933 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003934 else
3935 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
3936
3937 IWL_DEBUG_MAC80211("leave\n");
3938
Zhu Yia0646472007-12-20 14:10:01 +08003939out:
3940 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003941 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08003942 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003943}
3944
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003945static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003946{
Tomas Winkler857485c2008-03-21 13:53:44 -07003947 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003948
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003949 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003950 return;
3951
3952 /* The following should be done only at AP bring up */
3953 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
3954
3955 /* RXON - unassoc (to set timing command) */
3956 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003957 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003958
3959 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003960 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
3961 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07003962 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07003963 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07003964 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07003965 IWL_WARNING("REPLY_RXON_TIMING failed - "
3966 "Attempting to continue.\n");
3967
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07003968 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003969
3970 /* FIXME: what should be the assoc_id for AP? */
3971 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
3972 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3973 priv->staging_rxon.flags |=
3974 RXON_FLG_SHORT_PREAMBLE_MSK;
3975 else
3976 priv->staging_rxon.flags &=
3977 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3978
3979 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
3980 if (priv->assoc_capability &
3981 WLAN_CAPABILITY_SHORT_SLOT_TIME)
3982 priv->staging_rxon.flags |=
3983 RXON_FLG_SHORT_SLOT_MSK;
3984 else
3985 priv->staging_rxon.flags &=
3986 ~RXON_FLG_SHORT_SLOT_MSK;
3987
3988 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
3989 priv->staging_rxon.flags &=
3990 ~RXON_FLG_SHORT_SLOT_MSK;
3991 }
3992 /* restore RXON assoc */
3993 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003994 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003995 iwl4965_activate_qos(priv, 1);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08003996 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08003997 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003998 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003999
4000 /* FIXME - we need to add code here to detect a totally new
4001 * configuration, reset the AP, unassoc, rxon timing, assoc,
4002 * clear sta table, add BCAST sta... */
4003}
4004
Johannes Berg32bfd352007-12-19 01:31:26 +01004005static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
4006 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07004007 struct ieee80211_if_conf *conf)
4008{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004009 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07004010 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07004011 unsigned long flags;
4012 int rc;
4013
4014 if (conf == NULL)
4015 return -EIO;
4016
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08004017 if (priv->vif != vif) {
4018 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08004019 return 0;
4020 }
4021
Zhu Yib481de92007-09-25 17:54:57 -07004022 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
4023 (!conf->beacon || !conf->ssid_len)) {
4024 IWL_DEBUG_MAC80211
4025 ("Leaving in AP mode because HostAPD is not ready.\n");
4026 return 0;
4027 }
4028
Tomas Winklerfee12472008-04-03 16:05:21 -07004029 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08004030 return -EAGAIN;
4031
Zhu Yib481de92007-09-25 17:54:57 -07004032 mutex_lock(&priv->mutex);
4033
Zhu Yib481de92007-09-25 17:54:57 -07004034 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07004035 IWL_DEBUG_MAC80211("bssid: %s\n",
4036 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07004037
Johannes Berg4150c572007-09-17 01:29:23 -04004038/*
4039 * very dubious code was here; the probe filtering flag is never set:
4040 *
Zhu Yib481de92007-09-25 17:54:57 -07004041 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
4042 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04004043 */
Zhu Yib481de92007-09-25 17:54:57 -07004044
4045 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
4046 if (!conf->bssid) {
4047 conf->bssid = priv->mac_addr;
4048 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07004049 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
4050 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07004051 }
4052 if (priv->ibss_beacon)
4053 dev_kfree_skb(priv->ibss_beacon);
4054
4055 priv->ibss_beacon = conf->beacon;
4056 }
4057
Tomas Winklerfee12472008-04-03 16:05:21 -07004058 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08004059 goto done;
4060
Zhu Yib481de92007-09-25 17:54:57 -07004061 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
4062 !is_multicast_ether_addr(conf->bssid)) {
4063 /* If there is currently a HW scan going on in the background
4064 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004065 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07004066 IWL_WARNING("Aborted scan still in progress "
4067 "after 100ms\n");
4068 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
4069 mutex_unlock(&priv->mutex);
4070 return -EAGAIN;
4071 }
4072 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
4073
4074 /* TODO: Audit driver for usage of these members and see
4075 * if mac80211 deprecates them (priv->bssid looks like it
4076 * shouldn't be there, but I haven't scanned the IBSS code
4077 * to verify) - jpk */
4078 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
4079
4080 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004081 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004082 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004083 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004084 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08004085 iwl_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07004086 priv, priv->active_rxon.bssid_addr, 1);
4087 }
4088
4089 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004090 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07004091 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004092 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004093 }
4094
Mohamed Abbasfde35712007-11-29 11:10:15 +08004095 done:
Zhu Yib481de92007-09-25 17:54:57 -07004096 spin_lock_irqsave(&priv->lock, flags);
4097 if (!conf->ssid_len)
4098 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
4099 else
4100 memcpy(priv->essid, conf->ssid, conf->ssid_len);
4101
4102 priv->essid_len = conf->ssid_len;
4103 spin_unlock_irqrestore(&priv->lock, flags);
4104
4105 IWL_DEBUG_MAC80211("leave\n");
4106 mutex_unlock(&priv->mutex);
4107
4108 return 0;
4109}
4110
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004111static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04004112 unsigned int changed_flags,
4113 unsigned int *total_flags,
4114 int mc_count, struct dev_addr_list *mc_list)
4115{
4116 /*
4117 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004118 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04004119 */
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08004120 struct iwl_priv *priv = hw->priv;
4121 int new_flags = 0;
4122 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
4123 if (*total_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
4124 IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
4125 IEEE80211_IF_TYPE_MNTR,
4126 changed_flags, *total_flags);
4127 /* queue work 'cuz mac80211 is holding a lock which
4128 * prevents us from issuing (synchronous) f/w cmds */
4129 queue_work(priv->workqueue, &priv->set_monitor);
4130 new_flags &= FIF_PROMISC_IN_BSS |
4131 FIF_OTHER_BSS |
4132 FIF_ALLMULTI;
4133 }
4134 }
4135 *total_flags = new_flags;
Johannes Berg4150c572007-09-17 01:29:23 -04004136}
4137
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004138static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07004139 struct ieee80211_if_init_conf *conf)
4140{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004141 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004142
4143 IWL_DEBUG_MAC80211("enter\n");
4144
4145 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08004146
Tomas Winklerfee12472008-04-03 16:05:21 -07004147 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08004148 iwl4965_scan_cancel_timeout(priv, 100);
4149 cancel_delayed_work(&priv->post_associate);
4150 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4151 iwl4965_commit_rxon(priv);
4152 }
Johannes Berg32bfd352007-12-19 01:31:26 +01004153 if (priv->vif == conf->vif) {
4154 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07004155 memset(priv->bssid, 0, ETH_ALEN);
4156 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
4157 priv->essid_len = 0;
4158 }
4159 mutex_unlock(&priv->mutex);
4160
4161 IWL_DEBUG_MAC80211("leave\n");
4162
4163}
Johannes Berg471b3ef2007-12-28 14:32:58 +01004164
Tomas Winkler3109ece2008-03-28 16:33:35 -07004165#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01004166static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
4167 struct ieee80211_vif *vif,
4168 struct ieee80211_bss_conf *bss_conf,
4169 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02004170{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004171 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02004172
Tomas Winkler3109ece2008-03-28 16:33:35 -07004173 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
4174
Johannes Berg471b3ef2007-12-28 14:32:58 +01004175 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07004176 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
4177 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01004178 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02004179 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
4180 else
4181 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
4182 }
4183
Johannes Berg471b3ef2007-12-28 14:32:58 +01004184 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07004185 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01004186 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02004187 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
4188 else
4189 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
4190 }
4191
Tomas Winkler98952d52008-03-28 16:33:33 -07004192 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07004193 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07004194 iwl4965_ht_conf(priv, bss_conf);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07004195 iwl_set_rxon_chain(priv);
Tomas Winkler98952d52008-03-28 16:33:33 -07004196 }
4197
Johannes Berg471b3ef2007-12-28 14:32:58 +01004198 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07004199 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
Reinette Chatre508e32e2008-04-14 21:16:13 -07004200 /* This should never happen as this function should
4201 * never be called from interrupt context. */
4202 if (WARN_ON_ONCE(in_interrupt()))
4203 return;
Tomas Winkler3109ece2008-03-28 16:33:35 -07004204 if (bss_conf->assoc) {
4205 priv->assoc_id = bss_conf->aid;
4206 priv->beacon_int = bss_conf->beacon_int;
4207 priv->timestamp = bss_conf->timestamp;
4208 priv->assoc_capability = bss_conf->assoc_capability;
4209 priv->next_scan_jiffies = jiffies +
4210 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
Reinette Chatre508e32e2008-04-14 21:16:13 -07004211 mutex_lock(&priv->mutex);
4212 iwl4965_post_associate(priv);
4213 mutex_unlock(&priv->mutex);
Tomas Winkler3109ece2008-03-28 16:33:35 -07004214 } else {
4215 priv->assoc_id = 0;
4216 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
4217 }
4218 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
4219 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
Tomas Winkler7e8c5192008-04-15 16:01:43 -07004220 iwl_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01004221 }
4222
Tomas Winkler220173b2007-10-16 00:50:25 +02004223}
Zhu Yib481de92007-09-25 17:54:57 -07004224
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004225static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07004226{
4227 int rc = 0;
4228 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004229 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004230
4231 IWL_DEBUG_MAC80211("enter\n");
4232
mabbas052c4b92007-10-25 17:15:43 +08004233 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07004234 spin_lock_irqsave(&priv->lock, flags);
4235
Tomas Winklerfee12472008-04-03 16:05:21 -07004236 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004237 rc = -EIO;
4238 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
4239 goto out_unlock;
4240 }
4241
4242 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
4243 rc = -EIO;
4244 IWL_ERROR("ERROR: APs don't scan\n");
4245 goto out_unlock;
4246 }
4247
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004248 /* we don't schedule scan within next_scan_jiffies period */
4249 if (priv->next_scan_jiffies &&
4250 time_after(priv->next_scan_jiffies, jiffies)) {
4251 rc = -EAGAIN;
4252 goto out_unlock;
4253 }
Zhu Yib481de92007-09-25 17:54:57 -07004254 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004255 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
4256 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07004257 rc = -EAGAIN;
4258 goto out_unlock;
4259 }
4260 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004261 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004262 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07004263
4264 priv->one_direct_scan = 1;
4265 priv->direct_ssid_len = (u8)
4266 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
4267 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08004268 } else
4269 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004270
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004271 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004272
4273 IWL_DEBUG_MAC80211("leave\n");
4274
4275out_unlock:
4276 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08004277 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07004278
4279 return rc;
4280}
4281
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004282static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
4283 struct ieee80211_key_conf *keyconf, const u8 *addr,
4284 u32 iv32, u16 *phase1key)
4285{
4286 struct iwl_priv *priv = hw->priv;
4287 u8 sta_id = IWL_INVALID_STATION;
4288 unsigned long flags;
4289 __le16 key_flags = 0;
4290 int i;
4291 DECLARE_MAC_BUF(mac);
4292
4293 IWL_DEBUG_MAC80211("enter\n");
4294
Tomas Winkler947b13a2008-04-16 16:34:48 -07004295 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004296 if (sta_id == IWL_INVALID_STATION) {
4297 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
4298 print_mac(mac, addr));
4299 return;
4300 }
4301
4302 iwl4965_scan_cancel_timeout(priv, 100);
4303
4304 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
4305 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
4306 key_flags &= ~STA_KEY_FLG_INVALID;
4307
Tomas Winkler5425e492008-04-15 16:01:38 -07004308 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004309 key_flags |= STA_KEY_MULTICAST_MSK;
4310
4311 spin_lock_irqsave(&priv->sta_lock, flags);
4312
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004313 priv->stations[sta_id].sta.key.key_flags = key_flags;
4314 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
4315
4316 for (i = 0; i < 5; i++)
4317 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
4318 cpu_to_le16(phase1key[i]);
4319
4320 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
4321 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
4322
Tomas Winkler133636d2008-05-05 10:22:34 +08004323 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004324
4325 spin_unlock_irqrestore(&priv->sta_lock, flags);
4326
4327 IWL_DEBUG_MAC80211("leave\n");
4328}
4329
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004330static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07004331 const u8 *local_addr, const u8 *addr,
4332 struct ieee80211_key_conf *key)
4333{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004334 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07004335 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004336 int ret = 0;
4337 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004338 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004339
4340 IWL_DEBUG_MAC80211("enter\n");
4341
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004342 if (priv->hw_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07004343 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
4344 return -EOPNOTSUPP;
4345 }
4346
4347 if (is_zero_ether_addr(addr))
4348 /* only support pairwise keys */
4349 return -EOPNOTSUPP;
4350
Tomas Winkler947b13a2008-04-16 16:34:48 -07004351 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004352 if (sta_id == IWL_INVALID_STATION) {
4353 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
4354 print_mac(mac, addr));
4355 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004356
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004357 }
Zhu Yib481de92007-09-25 17:54:57 -07004358
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004359 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004360 iwl4965_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004361 mutex_unlock(&priv->mutex);
4362
4363 /* If we are getting WEP group key and we didn't receive any key mapping
4364 * so far, we are in legacy wep mode (group key only), otherwise we are
4365 * in 1X mode.
4366 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07004367 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004368 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
4369 if (cmd == SET_KEY)
4370 is_default_wep_key = !priv->key_mapping_key;
4371 else
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08004372 is_default_wep_key =
4373 (key->hw_key_idx == HW_KEY_DEFAULT);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004374 }
mabbas052c4b92007-10-25 17:15:43 +08004375
Zhu Yib481de92007-09-25 17:54:57 -07004376 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004377 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004378 if (is_default_wep_key)
4379 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004380 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07004381 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004382
4383 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07004384 break;
4385 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004386 if (is_default_wep_key)
4387 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004388 else
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07004389 ret = iwl_remove_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004390
4391 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07004392 break;
4393 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004394 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004395 }
4396
4397 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07004398
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004399 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004400}
4401
Johannes Berge100bb62008-04-30 18:51:21 +02004402static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07004403 const struct ieee80211_tx_queue_params *params)
4404{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004405 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004406 unsigned long flags;
4407 int q;
Zhu Yib481de92007-09-25 17:54:57 -07004408
4409 IWL_DEBUG_MAC80211("enter\n");
4410
Tomas Winklerfee12472008-04-03 16:05:21 -07004411 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004412 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4413 return -EIO;
4414 }
4415
4416 if (queue >= AC_NUM) {
4417 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
4418 return 0;
4419 }
4420
Zhu Yib481de92007-09-25 17:54:57 -07004421 if (!priv->qos_data.qos_enable) {
4422 priv->qos_data.qos_active = 0;
4423 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
4424 return 0;
4425 }
4426 q = AC_NUM - 1 - queue;
4427
4428 spin_lock_irqsave(&priv->lock, flags);
4429
4430 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
4431 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
4432 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
4433 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01004434 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07004435
4436 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
4437 priv->qos_data.qos_active = 1;
4438
4439 spin_unlock_irqrestore(&priv->lock, flags);
4440
4441 mutex_lock(&priv->mutex);
4442 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004443 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07004444 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004445 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004446
4447 mutex_unlock(&priv->mutex);
4448
Zhu Yib481de92007-09-25 17:54:57 -07004449 IWL_DEBUG_MAC80211("leave\n");
4450 return 0;
4451}
4452
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004453static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07004454 struct ieee80211_tx_queue_stats *stats)
4455{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004456 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004457 int i, avail;
Ron Rindjunsky16466902008-05-05 10:22:50 +08004458 struct iwl_tx_queue *txq;
Tomas Winkler443cfd42008-05-15 13:53:57 +08004459 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07004460 unsigned long flags;
4461
4462 IWL_DEBUG_MAC80211("enter\n");
4463
Tomas Winklerfee12472008-04-03 16:05:21 -07004464 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004465 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4466 return -EIO;
4467 }
4468
4469 spin_lock_irqsave(&priv->lock, flags);
4470
4471 for (i = 0; i < AC_NUM; i++) {
4472 txq = &priv->txq[i];
4473 q = &txq->q;
Tomas Winkler443cfd42008-05-15 13:53:57 +08004474 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07004475
Johannes Berg57ffc582008-04-29 17:18:59 +02004476 stats[i].len = q->n_window - avail;
4477 stats[i].limit = q->n_window - q->high_mark;
4478 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07004479
4480 }
4481 spin_unlock_irqrestore(&priv->lock, flags);
4482
4483 IWL_DEBUG_MAC80211("leave\n");
4484
4485 return 0;
4486}
4487
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004488static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07004489 struct ieee80211_low_level_stats *stats)
4490{
Ester Kummerbf403db2008-05-05 10:22:40 +08004491 struct iwl_priv *priv = hw->priv;
4492
4493 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004494 IWL_DEBUG_MAC80211("enter\n");
4495 IWL_DEBUG_MAC80211("leave\n");
4496
4497 return 0;
4498}
4499
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004500static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07004501{
Ester Kummerbf403db2008-05-05 10:22:40 +08004502 struct iwl_priv *priv;
4503
4504 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004505 IWL_DEBUG_MAC80211("enter\n");
4506 IWL_DEBUG_MAC80211("leave\n");
4507
4508 return 0;
4509}
4510
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004511static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07004512{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004513 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004514 unsigned long flags;
4515
4516 mutex_lock(&priv->mutex);
4517 IWL_DEBUG_MAC80211("enter\n");
4518
4519 priv->lq_mngr.lq_ready = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004520 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02004521 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07004522 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -07004523
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07004524 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004525
4526 cancel_delayed_work(&priv->post_associate);
4527
4528 spin_lock_irqsave(&priv->lock, flags);
4529 priv->assoc_id = 0;
4530 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004531 priv->assoc_station_added = 0;
4532
4533 /* new association get rid of ibss beacon skb */
4534 if (priv->ibss_beacon)
4535 dev_kfree_skb(priv->ibss_beacon);
4536
4537 priv->ibss_beacon = NULL;
4538
4539 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07004540 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004541 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
4542 priv->beacon_int = 0;
4543
4544 spin_unlock_irqrestore(&priv->lock, flags);
4545
Tomas Winklerfee12472008-04-03 16:05:21 -07004546 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08004547 IWL_DEBUG_MAC80211("leave - not ready\n");
4548 mutex_unlock(&priv->mutex);
4549 return;
4550 }
4551
mabbas052c4b92007-10-25 17:15:43 +08004552 /* we are restarting association process
4553 * clear RXON_FILTER_ASSOC_MSK bit
4554 */
4555 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004556 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08004557 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004558 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08004559 }
4560
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004561 iwl_power_update_mode(priv, 0);
4562
Zhu Yib481de92007-09-25 17:54:57 -07004563 /* Per mac80211.h: This is only used in IBSS mode... */
4564 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08004565
Zhu Yib481de92007-09-25 17:54:57 -07004566 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
4567 mutex_unlock(&priv->mutex);
4568 return;
4569 }
4570
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004571 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004572
4573 mutex_unlock(&priv->mutex);
4574
4575 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07004576}
4577
Johannes Berge039fa42008-05-15 12:55:29 +02004578static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07004579{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004580 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004581 unsigned long flags;
4582
4583 mutex_lock(&priv->mutex);
4584 IWL_DEBUG_MAC80211("enter\n");
4585
Tomas Winklerfee12472008-04-03 16:05:21 -07004586 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004587 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4588 mutex_unlock(&priv->mutex);
4589 return -EIO;
4590 }
4591
4592 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
4593 IWL_DEBUG_MAC80211("leave - not IBSS\n");
4594 mutex_unlock(&priv->mutex);
4595 return -EIO;
4596 }
4597
4598 spin_lock_irqsave(&priv->lock, flags);
4599
4600 if (priv->ibss_beacon)
4601 dev_kfree_skb(priv->ibss_beacon);
4602
4603 priv->ibss_beacon = skb;
4604
4605 priv->assoc_id = 0;
4606
4607 IWL_DEBUG_MAC80211("leave\n");
4608 spin_unlock_irqrestore(&priv->lock, flags);
4609
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07004610 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004611
4612 queue_work(priv->workqueue, &priv->post_associate.work);
4613
4614 mutex_unlock(&priv->mutex);
4615
4616 return 0;
4617}
4618
Zhu Yib481de92007-09-25 17:54:57 -07004619/*****************************************************************************
4620 *
4621 * sysfs attributes
4622 *
4623 *****************************************************************************/
4624
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004625#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004626
4627/*
4628 * The following adds a new attribute to the sysfs representation
4629 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
4630 * used for controlling the debug level.
4631 *
4632 * See the level definitions in iwl for details.
4633 */
4634
Ester Kummer8cf769c2008-05-06 11:05:11 +08004635static ssize_t show_debug_level(struct device *d,
4636 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07004637{
Ester Kummer8cf769c2008-05-06 11:05:11 +08004638 struct iwl_priv *priv = d->driver_data;
4639
4640 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07004641}
Ester Kummer8cf769c2008-05-06 11:05:11 +08004642static ssize_t store_debug_level(struct device *d,
4643 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07004644 const char *buf, size_t count)
4645{
Ester Kummer8cf769c2008-05-06 11:05:11 +08004646 struct iwl_priv *priv = d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004647 char *p = (char *)buf;
4648 u32 val;
4649
4650 val = simple_strtoul(p, &p, 0);
4651 if (p == buf)
4652 printk(KERN_INFO DRV_NAME
4653 ": %s is not in hex or decimal form.\n", buf);
4654 else
Ester Kummer8cf769c2008-05-06 11:05:11 +08004655 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07004656
4657 return strnlen(buf, count);
4658}
4659
Ester Kummer8cf769c2008-05-06 11:05:11 +08004660static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
4661 show_debug_level, store_debug_level);
4662
Zhu Yib481de92007-09-25 17:54:57 -07004663
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004664#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07004665
Zhu Yib481de92007-09-25 17:54:57 -07004666
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08004667static ssize_t show_version(struct device *d,
4668 struct device_attribute *attr, char *buf)
4669{
4670 struct iwl_priv *priv = d->driver_data;
Tomas Winkler885ba202008-05-29 16:34:55 +08004671 struct iwl_alive_resp *palive = &priv->card_alive;
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08004672
4673 if (palive->is_valid)
4674 return sprintf(buf, "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
4675 "fw type: 0x%01X 0x%01X\n",
4676 palive->ucode_major, palive->ucode_minor,
4677 palive->sw_rev[0], palive->sw_rev[1],
4678 palive->ver_type, palive->ver_subtype);
4679
4680 else
4681 return sprintf(buf, "fw not loaded\n");
4682}
4683
4684static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
4685
Zhu Yib481de92007-09-25 17:54:57 -07004686static ssize_t show_temperature(struct device *d,
4687 struct device_attribute *attr, char *buf)
4688{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004689 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004690
Tomas Winklerfee12472008-04-03 16:05:21 -07004691 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004692 return -EAGAIN;
4693
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004694 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07004695}
4696
4697static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
4698
4699static ssize_t show_rs_window(struct device *d,
4700 struct device_attribute *attr,
4701 char *buf)
4702{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004703 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004704 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07004705}
4706static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
4707
4708static ssize_t show_tx_power(struct device *d,
4709 struct device_attribute *attr, char *buf)
4710{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004711 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004712 return sprintf(buf, "%d\n", priv->user_txpower_limit);
4713}
4714
4715static ssize_t store_tx_power(struct device *d,
4716 struct device_attribute *attr,
4717 const char *buf, size_t count)
4718{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004719 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004720 char *p = (char *)buf;
4721 u32 val;
4722
4723 val = simple_strtoul(p, &p, 10);
4724 if (p == buf)
4725 printk(KERN_INFO DRV_NAME
4726 ": %s is not in decimal form.\n", buf);
4727 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004728 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07004729
4730 return count;
4731}
4732
4733static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
4734
4735static ssize_t show_flags(struct device *d,
4736 struct device_attribute *attr, char *buf)
4737{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004738 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004739
4740 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
4741}
4742
4743static ssize_t store_flags(struct device *d,
4744 struct device_attribute *attr,
4745 const char *buf, size_t count)
4746{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004747 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004748 u32 flags = simple_strtoul(buf, NULL, 0);
4749
4750 mutex_lock(&priv->mutex);
4751 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
4752 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004753 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07004754 IWL_WARNING("Could not cancel scan.\n");
4755 else {
4756 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
4757 flags);
4758 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004759 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004760 }
4761 }
4762 mutex_unlock(&priv->mutex);
4763
4764 return count;
4765}
4766
4767static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
4768
4769static ssize_t show_filter_flags(struct device *d,
4770 struct device_attribute *attr, char *buf)
4771{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004772 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004773
4774 return sprintf(buf, "0x%04X\n",
4775 le32_to_cpu(priv->active_rxon.filter_flags));
4776}
4777
4778static ssize_t store_filter_flags(struct device *d,
4779 struct device_attribute *attr,
4780 const char *buf, size_t count)
4781{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004782 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004783 u32 filter_flags = simple_strtoul(buf, NULL, 0);
4784
4785 mutex_lock(&priv->mutex);
4786 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
4787 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004788 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07004789 IWL_WARNING("Could not cancel scan.\n");
4790 else {
4791 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
4792 "0x%04X\n", filter_flags);
4793 priv->staging_rxon.filter_flags =
4794 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004795 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004796 }
4797 }
4798 mutex_unlock(&priv->mutex);
4799
4800 return count;
4801}
4802
4803static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
4804 store_filter_flags);
4805
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004806#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07004807
4808static ssize_t show_measurement(struct device *d,
4809 struct device_attribute *attr, char *buf)
4810{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004811 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004812 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07004813 u32 size = sizeof(measure_report), len = 0, ofs = 0;
4814 u8 *data = (u8 *) & measure_report;
4815 unsigned long flags;
4816
4817 spin_lock_irqsave(&priv->lock, flags);
4818 if (!(priv->measurement_status & MEASUREMENT_READY)) {
4819 spin_unlock_irqrestore(&priv->lock, flags);
4820 return 0;
4821 }
4822 memcpy(&measure_report, &priv->measure_report, size);
4823 priv->measurement_status = 0;
4824 spin_unlock_irqrestore(&priv->lock, flags);
4825
4826 while (size && (PAGE_SIZE - len)) {
4827 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4828 PAGE_SIZE - len, 1);
4829 len = strlen(buf);
4830 if (PAGE_SIZE - len)
4831 buf[len++] = '\n';
4832
4833 ofs += 16;
4834 size -= min(size, 16U);
4835 }
4836
4837 return len;
4838}
4839
4840static ssize_t store_measurement(struct device *d,
4841 struct device_attribute *attr,
4842 const char *buf, size_t count)
4843{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004844 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004845 struct ieee80211_measurement_params params = {
4846 .channel = le16_to_cpu(priv->active_rxon.channel),
4847 .start_time = cpu_to_le64(priv->last_tsf),
4848 .duration = cpu_to_le16(1),
4849 };
4850 u8 type = IWL_MEASURE_BASIC;
4851 u8 buffer[32];
4852 u8 channel;
4853
4854 if (count) {
4855 char *p = buffer;
4856 strncpy(buffer, buf, min(sizeof(buffer), count));
4857 channel = simple_strtoul(p, NULL, 0);
4858 if (channel)
4859 params.channel = channel;
4860
4861 p = buffer;
4862 while (*p && *p != ' ')
4863 p++;
4864 if (*p)
4865 type = simple_strtoul(p + 1, NULL, 0);
4866 }
4867
4868 IWL_DEBUG_INFO("Invoking measurement of type %d on "
4869 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004870 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07004871
4872 return count;
4873}
4874
4875static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
4876 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004877#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07004878
4879static ssize_t store_retry_rate(struct device *d,
4880 struct device_attribute *attr,
4881 const char *buf, size_t count)
4882{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004883 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004884
4885 priv->retry_rate = simple_strtoul(buf, NULL, 0);
4886 if (priv->retry_rate <= 0)
4887 priv->retry_rate = 1;
4888
4889 return count;
4890}
4891
4892static ssize_t show_retry_rate(struct device *d,
4893 struct device_attribute *attr, char *buf)
4894{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004895 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004896 return sprintf(buf, "%d", priv->retry_rate);
4897}
4898
4899static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
4900 store_retry_rate);
4901
4902static ssize_t store_power_level(struct device *d,
4903 struct device_attribute *attr,
4904 const char *buf, size_t count)
4905{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004906 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004907 int rc;
4908 int mode;
4909
4910 mode = simple_strtoul(buf, NULL, 0);
4911 mutex_lock(&priv->mutex);
4912
Tomas Winklerfee12472008-04-03 16:05:21 -07004913 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004914 rc = -EAGAIN;
4915 goto out;
4916 }
4917
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004918 rc = iwl_power_set_user_mode(priv, mode);
4919 if (rc) {
4920 IWL_DEBUG_MAC80211("failed setting power mode.\n");
4921 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004922 }
Zhu Yib481de92007-09-25 17:54:57 -07004923 rc = count;
4924
4925 out:
4926 mutex_unlock(&priv->mutex);
4927 return rc;
4928}
4929
4930#define MAX_WX_STRING 80
4931
4932/* Values are in microsecond */
4933static const s32 timeout_duration[] = {
4934 350000,
4935 250000,
4936 75000,
4937 37000,
4938 25000,
4939};
4940static const s32 period_duration[] = {
4941 400000,
4942 700000,
4943 1000000,
4944 1000000,
4945 1000000
4946};
4947
4948static ssize_t show_power_level(struct device *d,
4949 struct device_attribute *attr, char *buf)
4950{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004951 struct iwl_priv *priv = dev_get_drvdata(d);
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004952 int level = priv->power_data.power_mode;
Zhu Yib481de92007-09-25 17:54:57 -07004953 char *p = buf;
4954
4955 p += sprintf(p, "%d ", level);
4956 switch (level) {
4957 case IWL_POWER_MODE_CAM:
4958 case IWL_POWER_AC:
4959 p += sprintf(p, "(AC)");
4960 break;
4961 case IWL_POWER_BATTERY:
4962 p += sprintf(p, "(BATTERY)");
4963 break;
4964 default:
4965 p += sprintf(p,
4966 "(Timeout %dms, Period %dms)",
4967 timeout_duration[level - 1] / 1000,
4968 period_duration[level - 1] / 1000);
4969 }
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004970/*
Zhu Yib481de92007-09-25 17:54:57 -07004971 if (!(priv->power_mode & IWL_POWER_ENABLED))
4972 p += sprintf(p, " OFF\n");
4973 else
4974 p += sprintf(p, " \n");
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004975*/
4976 p += sprintf(p, " \n");
Zhu Yib481de92007-09-25 17:54:57 -07004977 return (p - buf + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004978}
4979
4980static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
4981 store_power_level);
4982
4983static ssize_t show_channels(struct device *d,
4984 struct device_attribute *attr, char *buf)
4985{
Johannes Berg8318d782008-01-24 19:38:38 +01004986 /* all this shit doesn't belong into sysfs anyway */
4987 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07004988}
4989
4990static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
4991
4992static ssize_t show_statistics(struct device *d,
4993 struct device_attribute *attr, char *buf)
4994{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004995 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004996 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07004997 u32 len = 0, ofs = 0;
4998 u8 *data = (u8 *) & priv->statistics;
4999 int rc = 0;
5000
Tomas Winklerfee12472008-04-03 16:05:21 -07005001 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005002 return -EAGAIN;
5003
5004 mutex_lock(&priv->mutex);
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07005005 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005006 mutex_unlock(&priv->mutex);
5007
5008 if (rc) {
5009 len = sprintf(buf,
5010 "Error sending statistics request: 0x%08X\n", rc);
5011 return len;
5012 }
5013
5014 while (size && (PAGE_SIZE - len)) {
5015 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
5016 PAGE_SIZE - len, 1);
5017 len = strlen(buf);
5018 if (PAGE_SIZE - len)
5019 buf[len++] = '\n';
5020
5021 ofs += 16;
5022 size -= min(size, 16U);
5023 }
5024
5025 return len;
5026}
5027
5028static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
5029
Zhu Yib481de92007-09-25 17:54:57 -07005030static ssize_t show_status(struct device *d,
5031 struct device_attribute *attr, char *buf)
5032{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005033 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07005034 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005035 return -EAGAIN;
5036 return sprintf(buf, "0x%08x\n", (int)priv->status);
5037}
5038
5039static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
5040
Zhu Yib481de92007-09-25 17:54:57 -07005041/*****************************************************************************
5042 *
5043 * driver setup and teardown
5044 *
5045 *****************************************************************************/
5046
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08005047static void iwl_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005048{
5049 priv->workqueue = create_workqueue(DRV_NAME);
5050
5051 init_waitqueue_head(&priv->wait_command_queue);
5052
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005053 INIT_WORK(&priv->up, iwl4965_bg_up);
5054 INIT_WORK(&priv->restart, iwl4965_bg_restart);
5055 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
5056 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
5057 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
5058 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
5059 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
5060 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08005061 INIT_WORK(&priv->set_monitor, iwl4965_bg_set_monitor);
Emmanuel Grumbach16e727e2008-06-12 09:46:52 +08005062 INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005063 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08005064 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
5065 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005066 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07005067
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08005068 if (priv->cfg->ops->lib->setup_deferred_work)
5069 priv->cfg->ops->lib->setup_deferred_work(priv);
5070
5071 init_timer(&priv->statistics_periodic);
5072 priv->statistics_periodic.data = (unsigned long)priv;
5073 priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
Zhu Yib481de92007-09-25 17:54:57 -07005074
5075 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005076 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07005077}
5078
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08005079static void iwl_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005080{
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08005081 if (priv->cfg->ops->lib->cancel_deferred_work)
5082 priv->cfg->ops->lib->cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005083
Joonwoo Park3ae6a052007-11-29 10:43:16 +09005084 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07005085 cancel_delayed_work(&priv->scan_check);
5086 cancel_delayed_work(&priv->alive_start);
5087 cancel_delayed_work(&priv->post_associate);
5088 cancel_work_sync(&priv->beacon_update);
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08005089 del_timer_sync(&priv->statistics_periodic);
Zhu Yib481de92007-09-25 17:54:57 -07005090}
5091
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005092static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07005093 &dev_attr_channels.attr,
Zhu Yib481de92007-09-25 17:54:57 -07005094 &dev_attr_flags.attr,
5095 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005096#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07005097 &dev_attr_measurement.attr,
5098#endif
5099 &dev_attr_power_level.attr,
5100 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07005101 &dev_attr_rs_window.attr,
5102 &dev_attr_statistics.attr,
5103 &dev_attr_status.attr,
5104 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07005105 &dev_attr_tx_power.attr,
Ester Kummer8cf769c2008-05-06 11:05:11 +08005106#ifdef CONFIG_IWLWIFI_DEBUG
5107 &dev_attr_debug_level.attr,
5108#endif
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08005109 &dev_attr_version.attr,
Zhu Yib481de92007-09-25 17:54:57 -07005110
5111 NULL
5112};
5113
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005114static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07005115 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005116 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07005117};
5118
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005119static struct ieee80211_ops iwl4965_hw_ops = {
5120 .tx = iwl4965_mac_tx,
5121 .start = iwl4965_mac_start,
5122 .stop = iwl4965_mac_stop,
5123 .add_interface = iwl4965_mac_add_interface,
5124 .remove_interface = iwl4965_mac_remove_interface,
5125 .config = iwl4965_mac_config,
5126 .config_interface = iwl4965_mac_config_interface,
5127 .configure_filter = iwl4965_configure_filter,
5128 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005129 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005130 .get_stats = iwl4965_mac_get_stats,
5131 .get_tx_stats = iwl4965_mac_get_tx_stats,
5132 .conf_tx = iwl4965_mac_conf_tx,
5133 .get_tsf = iwl4965_mac_get_tsf,
5134 .reset_tsf = iwl4965_mac_reset_tsf,
5135 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01005136 .bss_info_changed = iwl4965_bss_info_changed,
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02005137 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005138 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07005139};
5140
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005141static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07005142{
5143 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005144 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07005145 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08005146 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005147 unsigned long flags;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005148 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005149
Assaf Krauss316c30d2008-03-14 10:38:46 -07005150 /************************
5151 * 1. Allocating HW data
5152 ************************/
5153
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005154 /* Disabling hardware scan means that mac80211 will perform scans
5155 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07005156 if (cfg->mod_params->disable_hw_scan) {
Ester Kummerbf403db2008-05-05 10:22:40 +08005157 if (cfg->mod_params->debug & IWL_DL_INFO)
5158 dev_printk(KERN_DEBUG, &(pdev->dev),
5159 "Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005160 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005161 }
5162
Assaf Krauss1d0a0822008-03-14 10:38:48 -07005163 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
5164 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07005165 err = -ENOMEM;
5166 goto out;
5167 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07005168 priv = hw->priv;
5169 /* At this point both hw and priv are allocated. */
5170
Zhu Yib481de92007-09-25 17:54:57 -07005171 SET_IEEE80211_DEV(hw, &pdev->dev);
5172
5173 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08005174 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07005175 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07005176
Tomas Winkler0a6857e2008-03-12 16:58:49 -07005177#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08005178 priv->debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07005179 atomic_set(&priv->restrict_refcnt, 0);
5180#endif
Zhu Yib481de92007-09-25 17:54:57 -07005181
Assaf Krauss316c30d2008-03-14 10:38:46 -07005182 /**************************
5183 * 2. Initializing PCI bus
5184 **************************/
5185 if (pci_enable_device(pdev)) {
5186 err = -ENODEV;
5187 goto out_ieee80211_free_hw;
5188 }
5189
5190 pci_set_master(pdev);
5191
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07005192 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005193 if (!err)
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07005194 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
5195 if (err) {
5196 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
5197 if (!err)
5198 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
5199 /* both attempts failed: */
Assaf Krauss316c30d2008-03-14 10:38:46 -07005200 if (err) {
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07005201 printk(KERN_WARNING "%s: No suitable DMA available.\n",
5202 DRV_NAME);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005203 goto out_pci_disable_device;
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07005204 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07005205 }
5206
5207 err = pci_request_regions(pdev, DRV_NAME);
5208 if (err)
5209 goto out_pci_disable_device;
5210
5211 pci_set_drvdata(pdev, priv);
5212
5213 /* We disable the RETRY_TIMEOUT register (0x41) to keep
5214 * PCI Tx retries from interfering with C3 CPU state */
5215 pci_write_config_byte(pdev, 0x41, 0x00);
5216
5217 /***********************
5218 * 3. Read REV register
5219 ***********************/
5220 priv->hw_base = pci_iomap(pdev, 0, 0);
5221 if (!priv->hw_base) {
5222 err = -ENODEV;
5223 goto out_pci_release_regions;
5224 }
5225
5226 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
5227 (unsigned long long) pci_resource_len(pdev, 0));
5228 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
5229
Tomas Winklerb661c812008-04-23 17:14:54 -07005230 iwl_hw_detect(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005231 printk(KERN_INFO DRV_NAME
Tomas Winklerb661c812008-04-23 17:14:54 -07005232 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
5233 priv->cfg->name, priv->hw_rev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005234
Tomas Winkler91238712008-04-23 17:14:53 -07005235 /* amp init */
5236 err = priv->cfg->ops->lib->apm_ops.init(priv);
5237 if (err < 0) {
5238 IWL_DEBUG_INFO("Failed to init APMG\n");
5239 goto out_iounmap;
5240 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07005241 /*****************
5242 * 4. Read EEPROM
5243 *****************/
Assaf Krauss316c30d2008-03-14 10:38:46 -07005244 /* Read the EEPROM */
5245 err = iwl_eeprom_init(priv);
5246 if (err) {
5247 IWL_ERROR("Unable to init EEPROM\n");
5248 goto out_iounmap;
5249 }
Tomas Winkler8614f362008-04-23 17:14:55 -07005250 err = iwl_eeprom_check_version(priv);
5251 if (err)
5252 goto out_iounmap;
5253
Ron Rindjunsky02883012008-05-15 13:53:53 +08005254 /* extract MAC Address */
Assaf Krauss316c30d2008-03-14 10:38:46 -07005255 iwl_eeprom_get_mac(priv, priv->mac_addr);
5256 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
5257 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
5258
5259 /************************
5260 * 5. Setup HW constants
5261 ************************/
5262 /* Device-specific setup */
Tomas Winkler5425e492008-04-15 16:01:38 -07005263 if (priv->cfg->ops->lib->set_hw_params(priv)) {
5264 IWL_ERROR("failed to set hw parameters\n");
Tomas Winkler073d3f52008-04-21 15:41:52 -07005265 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07005266 }
5267
5268 /*******************
Tomas Winkler6ba87952008-05-15 13:54:17 +08005269 * 6. Setup priv
Assaf Krauss316c30d2008-03-14 10:38:46 -07005270 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07005271
Tomas Winkler6ba87952008-05-15 13:54:17 +08005272 err = iwl_init_drv(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005273 if (err)
Ron Rindjunsky399f4902008-04-23 17:14:56 -07005274 goto out_free_eeprom;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005275 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07005276
5277 /**********************************
5278 * 7. Initialize module parameters
5279 **********************************/
5280
5281 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07005282 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07005283 set_bit(STATUS_RF_KILL_SW, &priv->status);
5284 IWL_DEBUG_INFO("Radio disabled.\n");
5285 }
5286
Assaf Krauss316c30d2008-03-14 10:38:46 -07005287 /********************
5288 * 8. Setup services
5289 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005290 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005291 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005292 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005293
5294 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
5295 if (err) {
5296 IWL_ERROR("failed to create sysfs device attributes\n");
Tomas Winkler6ba87952008-05-15 13:54:17 +08005297 goto out_uninit_drv;
Assaf Krauss316c30d2008-03-14 10:38:46 -07005298 }
5299
Assaf Krauss316c30d2008-03-14 10:38:46 -07005300
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08005301 iwl_setup_deferred_work(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005302 iwl4965_setup_rx_handlers(priv);
5303
5304 /********************
5305 * 9. Conclude
5306 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08005307 pci_save_state(pdev);
5308 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07005309
Tomas Winkler6ba87952008-05-15 13:54:17 +08005310 /**********************************
5311 * 10. Setup and register mac80211
5312 **********************************/
5313
5314 err = iwl_setup_mac(priv);
5315 if (err)
5316 goto out_remove_sysfs;
5317
5318 err = iwl_dbgfs_register(priv, DRV_NAME);
5319 if (err)
5320 IWL_ERROR("failed to create debugfs files\n");
5321
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005322 /* notify iwlcore to init */
5323 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07005324 return 0;
5325
Assaf Krauss316c30d2008-03-14 10:38:46 -07005326 out_remove_sysfs:
5327 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Tomas Winkler6ba87952008-05-15 13:54:17 +08005328 out_uninit_drv:
5329 iwl_uninit_drv(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07005330 out_free_eeprom:
5331 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005332 out_iounmap:
5333 pci_iounmap(pdev, priv->hw_base);
5334 out_pci_release_regions:
5335 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005336 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07005337 out_pci_disable_device:
5338 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07005339 out_ieee80211_free_hw:
5340 ieee80211_free_hw(priv->hw);
5341 out:
5342 return err;
5343}
5344
Reinette Chatrec83dbf62008-03-21 13:53:41 -07005345static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07005346{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005347 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07005348 struct list_head *p, *q;
5349 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005350 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07005351
5352 if (!priv)
5353 return;
5354
5355 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
5356
Emmanuel Grumbach67249622008-05-29 16:35:26 +08005357 iwl_dbgfs_unregister(priv);
5358 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
5359
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07005360 if (priv->mac80211_registered) {
5361 ieee80211_unregister_hw(priv->hw);
5362 priv->mac80211_registered = 0;
5363 }
5364
Zhu Yib481de92007-09-25 17:54:57 -07005365 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08005366
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005367 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005368
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005369 /* make sure we flush any pending irq or
5370 * tasklet for the driver
5371 */
5372 spin_lock_irqsave(&priv->lock, flags);
5373 iwl4965_disable_interrupts(priv);
5374 spin_unlock_irqrestore(&priv->lock, flags);
5375
5376 iwl_synchronize_irq(priv);
5377
Zhu Yib481de92007-09-25 17:54:57 -07005378 /* Free MAC hash list for ADHOC */
5379 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
5380 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
5381 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005382 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07005383 }
5384 }
5385
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005386 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07005387
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005388 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005389
5390 if (priv->rxq.bd)
Tomas Winklera55360e2008-05-05 10:22:28 +08005391 iwl_rx_queue_free(priv, &priv->rxq);
Ron Rindjunsky1053d352008-05-05 10:22:43 +08005392 iwl_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005393
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005394 iwlcore_clear_stations_table(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07005395 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005396
Zhu Yib481de92007-09-25 17:54:57 -07005397
Mohamed Abbas948c1712007-10-25 17:15:45 +08005398 /*netif_stop_queue(dev); */
5399 flush_workqueue(priv->workqueue);
5400
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005401 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07005402 * priv->workqueue... so we can't take down the workqueue
5403 * until now... */
5404 destroy_workqueue(priv->workqueue);
5405 priv->workqueue = NULL;
5406
Zhu Yib481de92007-09-25 17:54:57 -07005407 pci_iounmap(pdev, priv->hw_base);
5408 pci_release_regions(pdev);
5409 pci_disable_device(pdev);
5410 pci_set_drvdata(pdev, NULL);
5411
Tomas Winkler6ba87952008-05-15 13:54:17 +08005412 iwl_uninit_drv(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005413
5414 if (priv->ibss_beacon)
5415 dev_kfree_skb(priv->ibss_beacon);
5416
5417 ieee80211_free_hw(priv->hw);
5418}
5419
5420#ifdef CONFIG_PM
5421
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005422static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07005423{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005424 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07005425
Zhu Yie655b9f2008-01-24 02:19:38 -08005426 if (priv->is_open) {
5427 set_bit(STATUS_IN_SUSPEND, &priv->status);
5428 iwl4965_mac_stop(priv->hw);
5429 priv->is_open = 1;
5430 }
Zhu Yib481de92007-09-25 17:54:57 -07005431
Zhu Yib481de92007-09-25 17:54:57 -07005432 pci_set_power_state(pdev, PCI_D3hot);
5433
Zhu Yib481de92007-09-25 17:54:57 -07005434 return 0;
5435}
5436
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005437static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07005438{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005439 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07005440
Zhu Yib481de92007-09-25 17:54:57 -07005441 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07005442
Zhu Yie655b9f2008-01-24 02:19:38 -08005443 if (priv->is_open)
5444 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005445
Zhu Yie655b9f2008-01-24 02:19:38 -08005446 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005447 return 0;
5448}
5449
5450#endif /* CONFIG_PM */
5451
5452/*****************************************************************************
5453 *
5454 * driver and module entry point
5455 *
5456 *****************************************************************************/
5457
Ron Rindjunskyfed90172008-04-15 16:01:41 -07005458/* Hardware specific file defines the PCI IDs table for that hardware module */
5459static struct pci_device_id iwl_hw_card_ids[] = {
5460 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
5461 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
Tomas Winkler5a6a2562008-04-24 11:55:23 -07005462#ifdef CONFIG_IWL5000
5463 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
5464 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
5465 {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
5466#endif /* CONFIG_IWL5000 */
Ron Rindjunskyfed90172008-04-15 16:01:41 -07005467 {0}
5468};
5469MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
5470
5471static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07005472 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07005473 .id_table = iwl_hw_card_ids,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005474 .probe = iwl4965_pci_probe,
5475 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07005476#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005477 .suspend = iwl4965_pci_suspend,
5478 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07005479#endif
5480};
5481
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005482static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07005483{
5484
5485 int ret;
5486 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
5487 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07005488
5489 ret = iwl4965_rate_control_register();
5490 if (ret) {
5491 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
5492 return ret;
5493 }
5494
Ron Rindjunskyfed90172008-04-15 16:01:41 -07005495 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07005496 if (ret) {
5497 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07005498 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07005499 }
Zhu Yib481de92007-09-25 17:54:57 -07005500
5501 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07005502
Reinette Chatre897e1cf2008-03-28 16:21:09 -07005503error_register:
5504 iwl4965_rate_control_unregister();
5505 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005506}
5507
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005508static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07005509{
Ron Rindjunskyfed90172008-04-15 16:01:41 -07005510 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07005511 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07005512}
5513
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005514module_exit(iwl4965_exit);
5515module_init(iwl4965_init);