blob: 05edde178302a6ca478e6e926994aae7475fc31a [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
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -070090static const struct ieee80211_supported_band *iwl_get_hw_mode(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -070091 struct iwl_priv *priv, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -070092{
Johannes Berg8318d782008-01-24 19:38:38 +010093 return priv->hw->wiphy->bands[band];
Zhu Yib481de92007-09-25 17:54:57 -070094}
95
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080096static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -070097{
98 /* Single white space is for Linksys APs */
99 if (essid_len == 1 && essid[0] == ' ')
100 return 1;
101
102 /* Otherwise, if the entire essid is 0, we assume it is hidden */
103 while (essid_len) {
104 essid_len--;
105 if (essid[essid_len] != '\0')
106 return 0;
107 }
108
109 return 1;
110}
111
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800112static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700113{
114 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
115 const char *s = essid;
116 char *d = escaped;
117
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800118 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700119 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
120 return escaped;
121 }
122
123 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
124 while (essid_len--) {
125 if (*s == '\0') {
126 *d++ = '\\';
127 *d++ = '0';
128 s++;
129 } else
130 *d++ = *s++;
131 }
132 *d = '\0';
133 return escaped;
134}
135
Zhu Yib481de92007-09-25 17:54:57 -0700136/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800137 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700138 * the functionality provided here
139 */
140
141/**************************************************************/
142
Ian Schram01ebd062007-10-25 17:15:22 +0800143#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800144/**
145 * iwl4965_remove_station - Remove driver's knowledge of station.
146 *
147 * NOTE: This does not remove station from device's station table.
148 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700149static u8 iwl4965_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700150{
151 int index = IWL_INVALID_STATION;
152 int i;
153 unsigned long flags;
154
155 spin_lock_irqsave(&priv->sta_lock, flags);
156
157 if (is_ap)
158 index = IWL_AP_ID;
159 else if (is_broadcast_ether_addr(addr))
Tomas Winkler5425e492008-04-15 16:01:38 -0700160 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700161 else
Tomas Winkler5425e492008-04-15 16:01:38 -0700162 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
Zhu Yib481de92007-09-25 17:54:57 -0700163 if (priv->stations[i].used &&
164 !compare_ether_addr(priv->stations[i].sta.sta.addr,
165 addr)) {
166 index = i;
167 break;
168 }
169
170 if (unlikely(index == IWL_INVALID_STATION))
171 goto out;
172
173 if (priv->stations[index].used) {
174 priv->stations[index].used = 0;
175 priv->num_stations--;
176 }
177
178 BUG_ON(priv->num_stations < 0);
179
180out:
181 spin_unlock_irqrestore(&priv->sta_lock, flags);
182 return 0;
183}
Zhu Yi556f8db2007-09-27 11:27:33 +0800184#endif
Zhu Yib481de92007-09-25 17:54:57 -0700185
Zhu Yib481de92007-09-25 17:54:57 -0700186
Zhu Yib481de92007-09-25 17:54:57 -0700187
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700188static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
189{
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800190 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700191
192 if (hw_decrypt)
193 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
194 else
195 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
196
197}
198
Zhu Yib481de92007-09-25 17:54:57 -0700199/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800200 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700201 *
202 * NOTE: This is really only useful during development and can eventually
203 * be #ifdef'd out once the driver is stable and folks aren't actively
204 * making changes
205 */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800206static int iwl4965_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700207{
208 int error = 0;
209 int counter = 1;
210
211 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
212 error |= le32_to_cpu(rxon->flags &
213 (RXON_FLG_TGJ_NARROW_BAND_MSK |
214 RXON_FLG_RADAR_DETECT_MSK));
215 if (error)
216 IWL_WARNING("check 24G fields %d | %d\n",
217 counter++, error);
218 } else {
219 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
220 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
221 if (error)
222 IWL_WARNING("check 52 fields %d | %d\n",
223 counter++, error);
224 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
225 if (error)
226 IWL_WARNING("check 52 CCK %d | %d\n",
227 counter++, error);
228 }
229 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
230 if (error)
231 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
232
233 /* make sure basic rates 6Mbps and 1Mbps are supported */
234 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
235 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
236 if (error)
237 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
238
239 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
240 if (error)
241 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
242
243 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
244 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
245 if (error)
246 IWL_WARNING("check CCK and short slot %d | %d\n",
247 counter++, error);
248
249 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
250 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
251 if (error)
252 IWL_WARNING("check CCK & auto detect %d | %d\n",
253 counter++, error);
254
255 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
256 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
257 if (error)
258 IWL_WARNING("check TGG and auto detect %d | %d\n",
259 counter++, error);
260
261 if (error)
262 IWL_WARNING("Tuning to channel %d\n",
263 le16_to_cpu(rxon->channel));
264
265 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800266 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700267 return -1;
268 }
269 return 0;
270}
271
272/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800273 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800274 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700275 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800276 * If the RXON structure is changing enough to require a new tune,
277 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
278 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700279 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700280static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700281{
282
283 /* These items are only settable from the full RXON command */
284 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
285 compare_ether_addr(priv->staging_rxon.bssid_addr,
286 priv->active_rxon.bssid_addr) ||
287 compare_ether_addr(priv->staging_rxon.node_addr,
288 priv->active_rxon.node_addr) ||
289 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
290 priv->active_rxon.wlap_bssid_addr) ||
291 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
292 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
293 (priv->staging_rxon.air_propagation !=
294 priv->active_rxon.air_propagation) ||
295 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
296 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
297 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
298 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
299 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
300 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
301 return 1;
302
303 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
304 * be updated with the RXON_ASSOC command -- however only some
305 * flag transitions are allowed using RXON_ASSOC */
306
307 /* Check if we are not switching bands */
308 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
309 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
310 return 1;
311
312 /* Check if we are switching association toggle */
313 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
314 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
315 return 1;
316
317 return 0;
318}
319
Zhu Yib481de92007-09-25 17:54:57 -0700320/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800321 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700322 *
Ian Schram01ebd062007-10-25 17:15:22 +0800323 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700324 * the active_rxon structure is updated with the new data. This
325 * function correctly transitions out of the RXON_ASSOC_MSK state if
326 * a HW tune is required based on the RXON structure changes.
327 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700328static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700329{
330 /* cast away the const for active_rxon in this function */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800331 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -0700332 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700333 int rc = 0;
334
Tomas Winklerfee12472008-04-03 16:05:21 -0700335 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700336 return -1;
337
338 /* always get timestamp with Rx frame */
339 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
340
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800341 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700342 if (rc) {
343 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
344 return -EINVAL;
345 }
346
347 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800348 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700349 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800350 if (!iwl4965_full_rxon_required(priv)) {
Tomas Winkler7e8c5192008-04-15 16:01:43 -0700351 rc = iwl_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700352 if (rc) {
353 IWL_ERROR("Error setting RXON_ASSOC "
354 "configuration (%d).\n", rc);
355 return rc;
356 }
357
358 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
359
360 return 0;
361 }
362
363 /* station table will be cleared */
364 priv->assoc_station_added = 0;
365
Zhu Yib481de92007-09-25 17:54:57 -0700366 /* If we are currently associated and the new config requires
367 * an RXON_ASSOC and the new config wants the associated mask enabled,
368 * we must clear the associated from the active configuration
369 * before we apply the new config */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700370 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700371 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
372 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
373 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
374
Tomas Winkler857485c2008-03-21 13:53:44 -0700375 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800376 sizeof(struct iwl_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700377 &priv->active_rxon);
378
379 /* If the mask clearing failed then we set
380 * active_rxon back to what it was previously */
381 if (rc) {
382 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
383 IWL_ERROR("Error clearing ASSOC_MSK on current "
384 "configuration (%d).\n", rc);
385 return rc;
386 }
Zhu Yib481de92007-09-25 17:54:57 -0700387 }
388
389 IWL_DEBUG_INFO("Sending RXON\n"
390 "* with%s RXON_FILTER_ASSOC_MSK\n"
391 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -0700392 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700393 ((priv->staging_rxon.filter_flags &
394 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
395 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -0700396 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -0700397
Ron Rindjunsky099b40b2008-04-21 15:41:53 -0700398 iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
Zhu Yib481de92007-09-25 17:54:57 -0700399 /* Apply the new configuration */
Tomas Winkler857485c2008-03-21 13:53:44 -0700400 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800401 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700402 if (rc) {
403 IWL_ERROR("Error setting new configuration (%d).\n", rc);
404 return rc;
405 }
406
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700407 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800408
Zhu Yib481de92007-09-25 17:54:57 -0700409 if (!priv->error_recovering)
410 priv->start_calib = 0;
411
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700412 iwl_init_sensitivity(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700413
414 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
415
416 /* If we issue a new RXON command which required a tune then we must
417 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800418 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700419 if (rc) {
420 IWL_ERROR("Error setting Tx power (%d).\n", rc);
421 return rc;
422 }
423
424 /* Add the broadcast address so we can send broadcast frames */
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800425 if (iwl_rxon_add_station(priv, iwl_bcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700426 IWL_INVALID_STATION) {
427 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
428 return -EIO;
429 }
430
431 /* If we have set the ASSOC_MSK and we are in BSS mode then
432 * add the IWL_AP_ID to the station rate table */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700433 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700434 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800435 if (iwl_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700436 == IWL_INVALID_STATION) {
437 IWL_ERROR("Error adding AP address for transmit.\n");
438 return -EIO;
439 }
440 priv->assoc_station_added = 1;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700441 if (priv->default_wep_key &&
442 iwl_send_static_wepkey_cmd(priv, 0))
443 IWL_ERROR("Could not send WEP static key.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700444 }
445
446 return 0;
447}
448
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700449void iwl4965_update_chain_flags(struct iwl_priv *priv)
450{
451
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -0700452 iwl_set_rxon_chain(priv);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700453 iwl4965_commit_rxon(priv);
454}
455
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700456static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700457{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800458 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700459 .flags = 3,
460 .lead_time = 0xAA,
461 .max_kill = 1,
462 .kill_ack_mask = 0,
463 .kill_cts_mask = 0,
464 };
465
Tomas Winkler857485c2008-03-21 13:53:44 -0700466 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800467 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700468}
469
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700470static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700471{
Tomas Winklerdb11d632008-05-05 10:22:33 +0800472 int ret = 0;
473 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -0700474 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700475 .id = REPLY_SCAN_ABORT_CMD,
476 .meta.flags = CMD_WANT_SKB,
477 };
478
479 /* If there isn't a scan actively going on in the hardware
480 * then we are in between scan bands and not actually
481 * actively scanning, so don't send the abort command */
482 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
483 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
484 return 0;
485 }
486
Tomas Winklerdb11d632008-05-05 10:22:33 +0800487 ret = iwl_send_cmd_sync(priv, &cmd);
488 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700489 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
Tomas Winklerdb11d632008-05-05 10:22:33 +0800490 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700491 }
492
Tomas Winklerdb11d632008-05-05 10:22:33 +0800493 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700494 if (res->u.status != CAN_ABORT_STATUS) {
495 /* The scan abort will return 1 for success or
496 * 2 for "failure". A failure condition can be
497 * due to simply not being in an active scan which
498 * can occur if we send the scan abort before we
499 * the microcode has notified us that a scan is
500 * completed. */
501 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
502 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
503 clear_bit(STATUS_SCAN_HW, &priv->status);
504 }
505
506 dev_kfree_skb_any(cmd.meta.u.skb);
507
Tomas Winklerdb11d632008-05-05 10:22:33 +0800508 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700509}
510
Zhu Yib481de92007-09-25 17:54:57 -0700511/*
512 * CARD_STATE_CMD
513 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800514 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -0700515 *
516 * When in the 'enable' state the card operates as normal.
517 * When in the 'disable' state, the card enters into a low power mode.
518 * When in the 'halt' state, the card is shut down and must be fully
519 * restarted to come back on.
520 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700521static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -0700522{
Tomas Winkler857485c2008-03-21 13:53:44 -0700523 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700524 .id = REPLY_CARD_STATE_CMD,
525 .len = sizeof(u32),
526 .data = &flags,
527 .meta.flags = meta_flag,
528 };
529
Tomas Winkler857485c2008-03-21 13:53:44 -0700530 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700531}
532
Tomas Winklerfcab4232008-05-15 13:54:01 +0800533static void iwl_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700534{
535 struct list_head *element;
536
537 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
538 priv->frames_count);
539
540 while (!list_empty(&priv->free_frames)) {
541 element = priv->free_frames.next;
542 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800543 kfree(list_entry(element, struct iwl_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700544 priv->frames_count--;
545 }
546
547 if (priv->frames_count) {
548 IWL_WARNING("%d frames still in use. Did we lose one?\n",
549 priv->frames_count);
550 priv->frames_count = 0;
551 }
552}
553
Tomas Winklerfcab4232008-05-15 13:54:01 +0800554static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700555{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800556 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700557 struct list_head *element;
558 if (list_empty(&priv->free_frames)) {
559 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
560 if (!frame) {
561 IWL_ERROR("Could not allocate frame!\n");
562 return NULL;
563 }
564
565 priv->frames_count++;
566 return frame;
567 }
568
569 element = priv->free_frames.next;
570 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800571 return list_entry(element, struct iwl_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700572}
573
Tomas Winklerfcab4232008-05-15 13:54:01 +0800574static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700575{
576 memset(frame, 0, sizeof(*frame));
577 list_add(&frame->list, &priv->free_frames);
578}
579
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700580unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700581 struct ieee80211_hdr *hdr,
582 const u8 *dest, int left)
583{
584
Tomas Winkler3109ece2008-03-28 16:33:35 -0700585 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -0700586 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
587 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
588 return 0;
589
590 if (priv->ibss_beacon->len > left)
591 return 0;
592
593 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
594
595 return priv->ibss_beacon->len;
596}
597
Guy Cohen39e88502008-04-23 17:14:57 -0700598static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700599{
Guy Cohen39e88502008-04-23 17:14:57 -0700600 int i;
601 int rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -0700602
Guy Cohen39e88502008-04-23 17:14:57 -0700603 /* Set rate mask*/
604 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
605 rate_mask = priv->active_rate_basic & 0xF;
606 else
607 rate_mask = priv->active_rate_basic & 0xFF0;
608
609 /* Find lowest valid rate */
Zhu Yib481de92007-09-25 17:54:57 -0700610 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800611 i = iwl_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -0700612 if (rate_mask & (1 << i))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800613 return iwl_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700614 }
615
Guy Cohen39e88502008-04-23 17:14:57 -0700616 /* No valid rate was found. Assign the lowest one */
617 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
618 return IWL_RATE_1M_PLCP;
619 else
620 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -0700621}
622
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700623static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700624{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800625 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700626 unsigned int frame_size;
627 int rc;
628 u8 rate;
629
Tomas Winklerfcab4232008-05-15 13:54:01 +0800630 frame = iwl_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700631
632 if (!frame) {
633 IWL_ERROR("Could not obtain free frame buffer for beacon "
634 "command.\n");
635 return -ENOMEM;
636 }
637
Guy Cohen39e88502008-04-23 17:14:57 -0700638 rate = iwl4965_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700639
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800640 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700641
Tomas Winkler857485c2008-03-21 13:53:44 -0700642 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700643 &frame->u.cmd[0]);
644
Tomas Winklerfcab4232008-05-15 13:54:01 +0800645 iwl_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700646
647 return rc;
648}
649
650/******************************************************************************
651 *
Zhu Yib481de92007-09-25 17:54:57 -0700652 * Misc. internal state and helper functions
653 *
654 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -0700655
Zhu Yib481de92007-09-25 17:54:57 -0700656/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800657 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -0700658 *
659 * return : set the bit for each supported rate insert in ie
660 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800661static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +0200662 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -0700663{
664 u16 ret_rates = 0, bit;
665 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200666 u8 *cnt = ie;
667 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -0700668
669 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
670 if (bit & supported_rate) {
671 ret_rates |= bit;
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800672 rates[*cnt] = iwl_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +0200673 ((bit & basic_rate) ? 0x80 : 0x00);
674 (*cnt)++;
675 (*left)--;
676 if ((*left <= 0) ||
677 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -0700678 break;
679 }
680 }
681
682 return ret_rates;
683}
684
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700685#ifdef CONFIG_IWL4965_HT
686static void iwl4965_ht_conf(struct iwl_priv *priv,
687 struct ieee80211_bss_conf *bss_conf)
688{
689 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
690 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
691 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
692
693 IWL_DEBUG_MAC80211("enter: \n");
694
695 iwl_conf->is_ht = bss_conf->assoc_ht;
696
697 if (!iwl_conf->is_ht)
698 return;
699
700 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
701
702 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
Emmanuel Grumbacha9841012008-05-15 13:54:08 +0800703 iwl_conf->sgf |= HT_SHORT_GI_20MHZ;
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700704 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
Emmanuel Grumbacha9841012008-05-15 13:54:08 +0800705 iwl_conf->sgf |= HT_SHORT_GI_40MHZ;
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700706
707 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
708 iwl_conf->max_amsdu_size =
709 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
710
711 iwl_conf->supported_chan_width =
712 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
713 iwl_conf->extension_chan_offset =
714 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
715 /* If no above or below channel supplied disable FAT channel */
716 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
717 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
718 iwl_conf->supported_chan_width = 0;
719
720 iwl_conf->tx_mimo_ps_mode =
721 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
722 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
723
724 iwl_conf->control_channel = ht_bss_conf->primary_channel;
725 iwl_conf->tx_chan_width =
726 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
727 iwl_conf->ht_protection =
728 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
729 iwl_conf->non_GF_STA_present =
730 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
731
732 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
733 IWL_DEBUG_MAC80211("leave\n");
734}
735
736static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
737 u8 *pos, int *left)
738{
739 struct ieee80211_ht_cap *ht_cap;
740
741 if (!sband || !sband->ht_info.ht_supported)
742 return;
743
744 if (*left < sizeof(struct ieee80211_ht_cap))
745 return;
746
747 *pos++ = sizeof(struct ieee80211_ht_cap);
748 ht_cap = (struct ieee80211_ht_cap *) pos;
749
750 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
751 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
752 ht_cap->ampdu_params_info =
753 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
754 ((sband->ht_info.ampdu_density << 2) &
755 IEEE80211_HT_CAP_AMPDU_DENSITY);
756 *left -= sizeof(struct ieee80211_ht_cap);
757}
758#else
759static inline void iwl4965_ht_conf(struct iwl_priv *priv,
760 struct ieee80211_bss_conf *bss_conf)
761{
762}
763static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
764 u8 *pos, int *left)
765{
766}
767#endif
768
769
Zhu Yib481de92007-09-25 17:54:57 -0700770/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800771 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -0700772 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700773static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +0200774 enum ieee80211_band band,
775 struct ieee80211_mgmt *frame,
776 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -0700777{
778 int len = 0;
779 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +0800780 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Tomas Winkler78330fd2008-02-06 02:37:18 +0200781 const struct ieee80211_supported_band *sband =
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700782 iwl_get_hw_mode(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -0700783
784 /* Make sure there is enough space for the probe request,
785 * two mandatory IEs and the data */
786 left -= 24;
787 if (left < 0)
788 return 0;
789 len += 24;
790
791 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Tomas Winkler57bd1be2008-05-15 13:54:03 +0800792 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -0700793 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Tomas Winkler57bd1be2008-05-15 13:54:03 +0800794 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -0700795 frame->seq_ctrl = 0;
796
797 /* fill in our indirect SSID IE */
798 /* ...next IE... */
799
800 left -= 2;
801 if (left < 0)
802 return 0;
803 len += 2;
804 pos = &(frame->u.probe_req.variable[0]);
805 *pos++ = WLAN_EID_SSID;
806 *pos++ = 0;
807
808 /* fill in our direct SSID IE... */
809 if (is_direct) {
810 /* ...next IE... */
811 left -= 2 + priv->essid_len;
812 if (left < 0)
813 return 0;
814 /* ... fill it in... */
815 *pos++ = WLAN_EID_SSID;
816 *pos++ = priv->essid_len;
817 memcpy(pos, priv->essid, priv->essid_len);
818 pos += priv->essid_len;
819 len += 2 + priv->essid_len;
820 }
821
822 /* fill in supported rate */
823 /* ...next IE... */
824 left -= 2;
825 if (left < 0)
826 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200827
Zhu Yib481de92007-09-25 17:54:57 -0700828 /* ... fill it in... */
829 *pos++ = WLAN_EID_SUPP_RATES;
830 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200831
mabbasbee488d2007-10-25 17:15:42 +0800832 /* exclude 60M rate */
833 active_rates = priv->rates_mask;
834 active_rates &= ~IWL_RATE_60M_MASK;
835
836 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -0700837
Tomas Winklerc7c46672007-10-18 02:04:15 +0200838 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800839 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +0800840 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +0200841 active_rates &= ~ret_rates;
842
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800843 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +0800844 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +0200845 active_rates &= ~ret_rates;
846
Zhu Yib481de92007-09-25 17:54:57 -0700847 len += 2 + *pos;
848 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200849 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -0700850 goto fill_end;
851
852 /* fill in supported extended rate */
853 /* ...next IE... */
854 left -= 2;
855 if (left < 0)
856 return 0;
857 /* ... fill it in... */
858 *pos++ = WLAN_EID_EXT_SUPP_RATES;
859 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800860 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +0800861 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -0700862 if (*pos > 0)
863 len += 2 + *pos;
864
Zhu Yib481de92007-09-25 17:54:57 -0700865 fill_end:
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700866 /* fill in HT IE */
867 left -= 2;
868 if (left < 0)
869 return 0;
870
871 *pos++ = WLAN_EID_HT_CAPABILITY;
872 *pos = 0;
873
874 iwl_ht_cap_to_ie(sband, pos, &left);
875
876 if (*pos > 0)
877 len += 2 + *pos;
Zhu Yib481de92007-09-25 17:54:57 -0700878 return (u16)len;
879}
880
881/*
882 * QoS support
883*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700884static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800885 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -0700886{
887
Tomas Winkler857485c2008-03-21 13:53:44 -0700888 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800889 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -0700890}
891
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700892static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -0700893{
894 unsigned long flags;
895
Zhu Yib481de92007-09-25 17:54:57 -0700896 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
897 return;
898
899 if (!priv->qos_data.qos_enable)
900 return;
901
902 spin_lock_irqsave(&priv->lock, flags);
903 priv->qos_data.def_qos_parm.qos_flags = 0;
904
905 if (priv->qos_data.qos_cap.q_AP.queue_request &&
906 !priv->qos_data.qos_cap.q_AP.txop_request)
907 priv->qos_data.def_qos_parm.qos_flags |=
908 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700909 if (priv->qos_data.qos_active)
910 priv->qos_data.def_qos_parm.qos_flags |=
911 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
912
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800913#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +0200914 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800915 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800916#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800917
Zhu Yib481de92007-09-25 17:54:57 -0700918 spin_unlock_irqrestore(&priv->lock, flags);
919
Tomas Winkler3109ece2008-03-28 16:33:35 -0700920 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +0800921 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
922 priv->qos_data.qos_active,
923 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700924
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800925 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -0700926 &(priv->qos_data.def_qos_parm));
927 }
928}
929
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700930int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -0700931{
932 /* Filter incoming packets to determine if they are targeted toward
933 * this network, discarding packets coming from ourselves */
934 switch (priv->iw_mode) {
935 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
936 /* packets from our adapter are dropped (echo) */
937 if (!compare_ether_addr(header->addr2, priv->mac_addr))
938 return 0;
939 /* {broad,multi}cast packets to our IBSS go through */
940 if (is_multicast_ether_addr(header->addr1))
941 return !compare_ether_addr(header->addr3, priv->bssid);
942 /* packets to our adapter go through */
943 return !compare_ether_addr(header->addr1, priv->mac_addr);
944 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
945 /* packets from our adapter are dropped (echo) */
946 if (!compare_ether_addr(header->addr3, priv->mac_addr))
947 return 0;
948 /* {broad,multi}cast packets to our BSS go through */
949 if (is_multicast_ether_addr(header->addr1))
950 return !compare_ether_addr(header->addr2, priv->bssid);
951 /* packets to our adapter go through */
952 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -0700953 default:
954 break;
Zhu Yib481de92007-09-25 17:54:57 -0700955 }
956
957 return 1;
958}
959
960#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
961
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800962static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -0700963{
964 switch (status & TX_STATUS_MSK) {
965 case TX_STATUS_SUCCESS:
966 return "SUCCESS";
967 TX_STATUS_ENTRY(SHORT_LIMIT);
968 TX_STATUS_ENTRY(LONG_LIMIT);
969 TX_STATUS_ENTRY(FIFO_UNDERRUN);
970 TX_STATUS_ENTRY(MGMNT_ABORT);
971 TX_STATUS_ENTRY(NEXT_FRAG);
972 TX_STATUS_ENTRY(LIFE_EXPIRE);
973 TX_STATUS_ENTRY(DEST_PS);
974 TX_STATUS_ENTRY(ABORTED);
975 TX_STATUS_ENTRY(BT_RETRY);
976 TX_STATUS_ENTRY(STA_INVALID);
977 TX_STATUS_ENTRY(FRAG_DROPPED);
978 TX_STATUS_ENTRY(TID_DISABLE);
979 TX_STATUS_ENTRY(FRAME_FLUSHED);
980 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
981 TX_STATUS_ENTRY(TX_LOCKED);
982 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
983 }
984
985 return "UNKNOWN";
986}
987
988/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800989 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -0700990 *
991 * NOTE: priv->mutex is not required before calling this function
992 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700993static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700994{
995 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
996 clear_bit(STATUS_SCANNING, &priv->status);
997 return 0;
998 }
999
1000 if (test_bit(STATUS_SCANNING, &priv->status)) {
1001 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1002 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1003 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1004 queue_work(priv->workqueue, &priv->abort_scan);
1005
1006 } else
1007 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1008
1009 return test_bit(STATUS_SCANNING, &priv->status);
1010 }
1011
1012 return 0;
1013}
1014
1015/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001016 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001017 * @ms: amount of time to wait (in milliseconds) for scan to abort
1018 *
1019 * NOTE: priv->mutex must be held before calling this function
1020 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001021static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001022{
1023 unsigned long now = jiffies;
1024 int ret;
1025
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001026 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001027 if (ret && ms) {
1028 mutex_unlock(&priv->mutex);
1029 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1030 test_bit(STATUS_SCANNING, &priv->status))
1031 msleep(1);
1032 mutex_lock(&priv->mutex);
1033
1034 return test_bit(STATUS_SCANNING, &priv->status);
1035 }
1036
1037 return ret;
1038}
1039
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001040static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001041{
1042 /* Reset ieee stats */
1043
1044 /* We don't reset the net_device_stats (ieee->stats) on
1045 * re-association */
1046
1047 priv->last_seq_num = -1;
1048 priv->last_frag_num = -1;
1049 priv->last_packet_time = 0;
1050
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001051 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001052}
1053
1054#define MAX_UCODE_BEACON_INTERVAL 4096
1055#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1056
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001057static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001058{
1059 u16 new_val = 0;
1060 u16 beacon_factor = 0;
1061
1062 beacon_factor =
1063 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1064 / MAX_UCODE_BEACON_INTERVAL;
1065 new_val = beacon_val / beacon_factor;
1066
1067 return cpu_to_le16(new_val);
1068}
1069
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001070static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001071{
1072 u64 interval_tm_unit;
1073 u64 tsf, result;
1074 unsigned long flags;
1075 struct ieee80211_conf *conf = NULL;
1076 u16 beacon_int = 0;
1077
1078 conf = ieee80211_get_hw_conf(priv->hw);
1079
1080 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -07001081 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1082 priv->rxon_timing.timestamp.dw[0] =
1083 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07001084
1085 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1086
Tomas Winkler3109ece2008-03-28 16:33:35 -07001087 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001088
1089 beacon_int = priv->beacon_int;
1090 spin_unlock_irqrestore(&priv->lock, flags);
1091
1092 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1093 if (beacon_int == 0) {
1094 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1095 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1096 } else {
1097 priv->rxon_timing.beacon_interval =
1098 cpu_to_le16(beacon_int);
1099 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001100 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001101 le16_to_cpu(priv->rxon_timing.beacon_interval));
1102 }
1103
1104 priv->rxon_timing.atim_window = 0;
1105 } else {
1106 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001107 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001108 /* TODO: we need to get atim_window from upper stack
1109 * for now we set to 0 */
1110 priv->rxon_timing.atim_window = 0;
1111 }
1112
1113 interval_tm_unit =
1114 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1115 result = do_div(tsf, interval_tm_unit);
1116 priv->rxon_timing.beacon_init_val =
1117 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1118
1119 IWL_DEBUG_ASSOC
1120 ("beacon interval %d beacon timer %d beacon tim %d\n",
1121 le16_to_cpu(priv->rxon_timing.beacon_interval),
1122 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1123 le16_to_cpu(priv->rxon_timing.atim_window));
1124}
1125
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001126static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001127{
1128 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1129 IWL_ERROR("APs don't scan.\n");
1130 return 0;
1131 }
1132
Tomas Winklerfee12472008-04-03 16:05:21 -07001133 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001134 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1135 return -EIO;
1136 }
1137
1138 if (test_bit(STATUS_SCANNING, &priv->status)) {
1139 IWL_DEBUG_SCAN("Scan already in progress.\n");
1140 return -EAGAIN;
1141 }
1142
1143 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1144 IWL_DEBUG_SCAN("Scan request while abort pending. "
1145 "Queuing.\n");
1146 return -EAGAIN;
1147 }
1148
1149 IWL_DEBUG_INFO("Starting scan...\n");
1150 priv->scan_bands = 2;
1151 set_bit(STATUS_SCANNING, &priv->status);
1152 priv->scan_start = jiffies;
1153 priv->scan_pass_start = priv->scan_start;
1154
1155 queue_work(priv->workqueue, &priv->request_scan);
1156
1157 return 0;
1158}
1159
Zhu Yib481de92007-09-25 17:54:57 -07001160
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001161static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001162 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001163{
Johannes Berg8318d782008-01-24 19:38:38 +01001164 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07001165 priv->staging_rxon.flags &=
1166 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1167 | RXON_FLG_CCK_MSK);
1168 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1169 } else {
Reinette Chatre508e32e2008-04-14 21:16:13 -07001170 /* Copied from iwl4965_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001171 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1172 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1173 else
1174 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1175
1176 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1177 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1178
1179 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1180 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1181 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1182 }
1183}
1184
1185/*
Ian Schram01ebd062007-10-25 17:15:22 +08001186 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001187 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001188static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001189{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001190 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001191
1192 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1193
1194 switch (priv->iw_mode) {
1195 case IEEE80211_IF_TYPE_AP:
1196 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1197 break;
1198
1199 case IEEE80211_IF_TYPE_STA:
1200 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1201 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1202 break;
1203
1204 case IEEE80211_IF_TYPE_IBSS:
1205 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1206 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1207 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1208 RXON_FILTER_ACCEPT_GRP_MSK;
1209 break;
1210
1211 case IEEE80211_IF_TYPE_MNTR:
1212 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1213 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1214 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1215 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001216 default:
1217 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1218 break;
Zhu Yib481de92007-09-25 17:54:57 -07001219 }
1220
1221#if 0
1222 /* TODO: Figure out when short_preamble would be set and cache from
1223 * that */
1224 if (!hw_to_local(priv->hw)->short_preamble)
1225 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1226 else
1227 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1228#endif
1229
Assaf Krauss8622e702008-03-21 13:53:43 -07001230 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001231 le16_to_cpu(priv->staging_rxon.channel));
1232
1233 if (!ch_info)
1234 ch_info = &priv->channel_info[0];
1235
1236 /*
1237 * in some case A channels are all non IBSS
1238 * in this case force B/G channel
1239 */
1240 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1241 !(is_channel_ibss(ch_info)))
1242 ch_info = &priv->channel_info[0];
1243
1244 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01001245 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07001246
Johannes Berg8318d782008-01-24 19:38:38 +01001247 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001248
1249 priv->staging_rxon.ofdm_basic_rates =
1250 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1251 priv->staging_rxon.cck_basic_rates =
1252 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1253
1254 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1255 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1256 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1257 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1258 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1259 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07001260 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001261}
1262
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001263static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001264{
Zhu Yib481de92007-09-25 17:54:57 -07001265 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001266 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001267
Assaf Krauss8622e702008-03-21 13:53:43 -07001268 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001269 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001270 le16_to_cpu(priv->staging_rxon.channel));
1271
1272 if (!ch_info || !is_channel_ibss(ch_info)) {
1273 IWL_ERROR("channel %d not IBSS channel\n",
1274 le16_to_cpu(priv->staging_rxon.channel));
1275 return -EINVAL;
1276 }
1277 }
1278
Zhu Yib481de92007-09-25 17:54:57 -07001279 priv->iw_mode = mode;
1280
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001281 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001282 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1283
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001284 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001285
Mohamed Abbasfde35712007-11-29 11:10:15 +08001286 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -07001287 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08001288 return -EAGAIN;
1289
1290 cancel_delayed_work(&priv->scan_check);
1291 if (iwl4965_scan_cancel_timeout(priv, 100)) {
1292 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1293 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1294 return -EAGAIN;
1295 }
1296
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001297 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001298
1299 return 0;
1300}
1301
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001302static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001303{
Johannes Berg8318d782008-01-24 19:38:38 +01001304 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001305 struct ieee80211_rate *rate;
1306 int i;
1307
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001308 hw = iwl_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08001309 if (!hw) {
1310 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
1311 return;
1312 }
Zhu Yib481de92007-09-25 17:54:57 -07001313
1314 priv->active_rate = 0;
1315 priv->active_rate_basic = 0;
1316
Johannes Berg8318d782008-01-24 19:38:38 +01001317 for (i = 0; i < hw->n_bitrates; i++) {
1318 rate = &(hw->bitrates[i]);
1319 if (rate->hw_value < IWL_RATE_COUNT)
1320 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07001321 }
1322
1323 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
1324 priv->active_rate, priv->active_rate_basic);
1325
1326 /*
1327 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
1328 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
1329 * OFDM
1330 */
1331 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
1332 priv->staging_rxon.cck_basic_rates =
1333 ((priv->active_rate_basic &
1334 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
1335 else
1336 priv->staging_rxon.cck_basic_rates =
1337 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1338
1339 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
1340 priv->staging_rxon.ofdm_basic_rates =
1341 ((priv->active_rate_basic &
1342 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
1343 IWL_FIRST_OFDM_RATE) & 0xFF;
1344 else
1345 priv->staging_rxon.ofdm_basic_rates =
1346 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1347}
1348
Mohamed Abbasad97edd2008-03-28 16:21:06 -07001349void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07001350{
1351 unsigned long flags;
1352
1353 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
1354 return;
1355
1356 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
1357 disable_radio ? "OFF" : "ON");
1358
1359 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001360 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001361 /* FIXME: This is a workaround for AP */
1362 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
1363 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001364 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001365 CSR_UCODE_SW_BIT_RFKILL);
1366 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07001367 /* call the host command only if no hw rf-kill set */
Mohamed Abbas59003832008-04-15 16:01:46 -07001368 if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
1369 iwl_is_ready(priv))
Mohamed Abbasad97edd2008-03-28 16:21:06 -07001370 iwl4965_send_card_state(priv,
1371 CARD_STATE_CMD_DISABLE,
1372 0);
Zhu Yib481de92007-09-25 17:54:57 -07001373 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07001374
1375 /* make sure mac80211 stop sending Tx frame */
1376 if (priv->mac80211_registered)
1377 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07001378 }
1379 return;
1380 }
1381
1382 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001383 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07001384
1385 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1386 spin_unlock_irqrestore(&priv->lock, flags);
1387
1388 /* wake up ucode */
1389 msleep(10);
1390
1391 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001392 iwl_read32(priv, CSR_UCODE_DRV_GP1);
1393 if (!iwl_grab_nic_access(priv))
1394 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001395 spin_unlock_irqrestore(&priv->lock, flags);
1396
1397 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
1398 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
1399 "disabled by HW switch\n");
1400 return;
1401 }
1402
1403 queue_work(priv->workqueue, &priv->restart);
1404 return;
1405}
1406
Zhu Yib481de92007-09-25 17:54:57 -07001407#define IWL_PACKET_RETRY_TIME HZ
1408
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001409int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001410{
1411 u16 sc = le16_to_cpu(header->seq_ctrl);
1412 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1413 u16 frag = sc & IEEE80211_SCTL_FRAG;
1414 u16 *last_seq, *last_frag;
1415 unsigned long *last_time;
1416
1417 switch (priv->iw_mode) {
1418 case IEEE80211_IF_TYPE_IBSS:{
1419 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001420 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001421 u8 *mac = header->addr2;
1422 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
1423
1424 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001425 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07001426 if (!compare_ether_addr(entry->mac, mac))
1427 break;
1428 }
1429 if (p == &priv->ibss_mac_hash[index]) {
1430 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1431 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08001432 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07001433 return 0;
1434 }
1435 memcpy(entry->mac, mac, ETH_ALEN);
1436 entry->seq_num = seq;
1437 entry->frag_num = frag;
1438 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08001439 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07001440 return 0;
1441 }
1442 last_seq = &entry->seq_num;
1443 last_frag = &entry->frag_num;
1444 last_time = &entry->packet_time;
1445 break;
1446 }
1447 case IEEE80211_IF_TYPE_STA:
1448 last_seq = &priv->last_seq_num;
1449 last_frag = &priv->last_frag_num;
1450 last_time = &priv->last_packet_time;
1451 break;
1452 default:
1453 return 0;
1454 }
1455 if ((*last_seq == seq) &&
1456 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
1457 if (*last_frag == frag)
1458 goto drop;
1459 if (*last_frag + 1 != frag)
1460 /* out-of-order fragment */
1461 goto drop;
1462 } else
1463 *last_seq = seq;
1464
1465 *last_frag = frag;
1466 *last_time = jiffies;
1467 return 0;
1468
1469 drop:
1470 return 1;
1471}
1472
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001473#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07001474
1475#include "iwl-spectrum.h"
1476
1477#define BEACON_TIME_MASK_LOW 0x00FFFFFF
1478#define BEACON_TIME_MASK_HIGH 0xFF000000
1479#define TIME_UNIT 1024
1480
1481/*
1482 * extended beacon time format
1483 * time in usec will be changed into a 32-bit value in 8:24 format
1484 * the high 1 byte is the beacon counts
1485 * the lower 3 bytes is the time in usec within one beacon interval
1486 */
1487
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001488static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07001489{
1490 u32 quot;
1491 u32 rem;
1492 u32 interval = beacon_interval * 1024;
1493
1494 if (!interval || !usec)
1495 return 0;
1496
1497 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
1498 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
1499
1500 return (quot << 24) + rem;
1501}
1502
1503/* base is usually what we get from ucode with each received frame,
1504 * the same as HW timer counter counting down
1505 */
1506
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001507static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07001508{
1509 u32 base_low = base & BEACON_TIME_MASK_LOW;
1510 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
1511 u32 interval = beacon_interval * TIME_UNIT;
1512 u32 res = (base & BEACON_TIME_MASK_HIGH) +
1513 (addon & BEACON_TIME_MASK_HIGH);
1514
1515 if (base_low > addon_low)
1516 res += base_low - addon_low;
1517 else if (base_low < addon_low) {
1518 res += interval + base_low - addon_low;
1519 res += (1 << 24);
1520 } else
1521 res += (1 << 24);
1522
1523 return cpu_to_le32(res);
1524}
1525
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001526static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001527 struct ieee80211_measurement_params *params,
1528 u8 type)
1529{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001530 struct iwl4965_spectrum_cmd spectrum;
Tomas Winklerdb11d632008-05-05 10:22:33 +08001531 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07001532 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001533 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
1534 .data = (void *)&spectrum,
1535 .meta.flags = CMD_WANT_SKB,
1536 };
1537 u32 add_time = le64_to_cpu(params->start_time);
1538 int rc;
1539 int spectrum_resp_status;
1540 int duration = le16_to_cpu(params->duration);
1541
Tomas Winkler3109ece2008-03-28 16:33:35 -07001542 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001543 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001544 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07001545 le64_to_cpu(params->start_time) - priv->last_tsf,
1546 le16_to_cpu(priv->rxon_timing.beacon_interval));
1547
1548 memset(&spectrum, 0, sizeof(spectrum));
1549
1550 spectrum.channel_count = cpu_to_le16(1);
1551 spectrum.flags =
1552 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
1553 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
1554 cmd.len = sizeof(spectrum);
1555 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
1556
Tomas Winkler3109ece2008-03-28 16:33:35 -07001557 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001558 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001559 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07001560 add_time,
1561 le16_to_cpu(priv->rxon_timing.beacon_interval));
1562 else
1563 spectrum.start_time = 0;
1564
1565 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
1566 spectrum.channels[0].channel = params->channel;
1567 spectrum.channels[0].type = type;
1568 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
1569 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
1570 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
1571
Tomas Winkler857485c2008-03-21 13:53:44 -07001572 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001573 if (rc)
1574 return rc;
1575
Tomas Winklerdb11d632008-05-05 10:22:33 +08001576 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001577 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1578 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
1579 rc = -EIO;
1580 }
1581
1582 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
1583 switch (spectrum_resp_status) {
1584 case 0: /* Command will be handled */
1585 if (res->u.spectrum.id != 0xff) {
1586 IWL_DEBUG_INFO
1587 ("Replaced existing measurement: %d\n",
1588 res->u.spectrum.id);
1589 priv->measurement_status &= ~MEASUREMENT_READY;
1590 }
1591 priv->measurement_status |= MEASUREMENT_ACTIVE;
1592 rc = 0;
1593 break;
1594
1595 case 1: /* Command will not be handled */
1596 rc = -EAGAIN;
1597 break;
1598 }
1599
1600 dev_kfree_skb_any(cmd.meta.u.skb);
1601
1602 return rc;
1603}
1604#endif
1605
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001606static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Tomas Winkler8567c632008-05-15 13:53:58 +08001607 struct iwl_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001608{
1609
1610 tx_sta->status.ack_signal = 0;
1611 tx_sta->status.excessive_retries = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001612
1613 if (in_interrupt())
1614 ieee80211_tx_status_irqsafe(priv->hw,
1615 tx_sta->skb[0], &(tx_sta->status));
1616 else
1617 ieee80211_tx_status(priv->hw,
1618 tx_sta->skb[0], &(tx_sta->status));
1619
1620 tx_sta->skb[0] = NULL;
1621}
1622
1623/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001624 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07001625 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001626 * When FW advances 'R' index, all entries between old and new 'R' index
1627 * need to be reclaimed. As result, some free space forms. If there is
1628 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07001629 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001630int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001631{
Ron Rindjunsky16466902008-05-05 10:22:50 +08001632 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Tomas Winkler443cfd42008-05-15 13:53:57 +08001633 struct iwl_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07001634 int nfreed = 0;
1635
Tomas Winkler443cfd42008-05-15 13:53:57 +08001636 if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
Zhu Yib481de92007-09-25 17:54:57 -07001637 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
1638 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001639 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07001640 return 0;
1641 }
1642
Tomas Winklerc54b6792008-03-06 17:36:53 -08001643 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001644 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08001645 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07001646 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001647 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001648 &(txq->txb[txq->q.read_ptr]));
Ron Rindjunsky1053d352008-05-05 10:22:43 +08001649 iwl_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07001650 } else if (nfreed > 1) {
1651 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001652 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07001653 queue_work(priv->workqueue, &priv->restart);
1654 }
1655 nfreed++;
1656 }
1657
Zhu Yib481de92007-09-25 17:54:57 -07001658 return nfreed;
1659}
1660
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001661static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001662{
1663 status &= TX_STATUS_MSK;
1664 return (status == TX_STATUS_SUCCESS)
1665 || (status == TX_STATUS_DIRECT_DONE);
1666}
1667
1668/******************************************************************************
1669 *
1670 * Generic RX handler implementations
1671 *
1672 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001673#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001674
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001675static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001676 struct ieee80211_hdr *hdr)
1677{
1678 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
1679 return IWL_AP_ID;
1680 else {
1681 u8 *da = ieee80211_get_DA(hdr);
Tomas Winkler947b13a2008-04-16 16:34:48 -07001682 return iwl_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07001683 }
1684}
1685
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001686static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001687 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07001688{
1689 if (priv->txq[txq_id].txb[idx].skb[0])
1690 return (struct ieee80211_hdr *)priv->txq[txq_id].
1691 txb[idx].skb[0]->data;
1692 return NULL;
1693}
1694
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001695static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07001696{
1697 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
1698 tx_resp->frame_count);
1699 return le32_to_cpu(*scd_ssn) & MAX_SN;
1700
1701}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001702
1703/**
1704 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
1705 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001706static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Tomas Winkler6def9762008-05-05 10:22:31 +08001707 struct iwl_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001708 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07001709 u16 start_idx)
1710{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001711 u16 status;
1712 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07001713 struct ieee80211_tx_status *tx_status = NULL;
1714 struct ieee80211_hdr *hdr = NULL;
1715 int i, sh;
1716 int txq_id, idx;
1717 u16 seq;
1718
1719 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001720 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07001721
1722 agg->frame_count = tx_resp->frame_count;
1723 agg->start_idx = start_idx;
1724 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001725 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001726
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001727 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07001728 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001729 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001730 status = le16_to_cpu(frame_status[0].status);
1731 seq = le16_to_cpu(frame_status[0].sequence);
1732 idx = SEQ_TO_INDEX(seq);
1733 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07001734
Zhu Yib481de92007-09-25 17:54:57 -07001735 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001736 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
1737 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07001738
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001739 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07001740 tx_status->retry_count = tx_resp->failure_frame;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001741 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001742 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07001743 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08001744 iwl4965_hwrate_to_tx_control(priv,
1745 le32_to_cpu(tx_resp->rate_n_flags),
1746 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07001747 /* FIXME: code repetition end */
1748
1749 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
1750 status & 0xff, tx_resp->failure_frame);
1751 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001752 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07001753
1754 agg->wait_for_ba = 0;
1755 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001756 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07001757 u64 bitmap = 0;
1758 int start = agg->start_idx;
1759
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001760 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07001761 for (i = 0; i < agg->frame_count; i++) {
1762 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001763 status = le16_to_cpu(frame_status[i].status);
1764 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07001765 idx = SEQ_TO_INDEX(seq);
1766 txq_id = SEQ_TO_QUEUE(seq);
1767
1768 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
1769 AGG_TX_STATE_ABORT_MSK))
1770 continue;
1771
1772 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
1773 agg->frame_count, txq_id, idx);
1774
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001775 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07001776
1777 sc = le16_to_cpu(hdr->seq_ctrl);
1778 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
1779 IWL_ERROR("BUG_ON idx doesn't match seq control"
1780 " idx=%d, seq_idx=%d, seq=%d\n",
1781 idx, SEQ_TO_SN(sc),
1782 hdr->seq_ctrl);
1783 return -1;
1784 }
1785
1786 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
1787 i, idx, SEQ_TO_SN(sc));
1788
1789 sh = idx - start;
1790 if (sh > 64) {
1791 sh = (start - idx) + 0xff;
1792 bitmap = bitmap << sh;
1793 sh = 0;
1794 start = idx;
1795 } else if (sh < -64)
1796 sh = 0xff - (start - idx);
1797 else if (sh < 0) {
1798 sh = start - idx;
1799 start = idx;
1800 bitmap = bitmap << sh;
1801 sh = 0;
1802 }
1803 bitmap |= (1 << sh);
1804 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
1805 start, (u32)(bitmap & 0xFFFFFFFF));
1806 }
1807
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001808 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07001809 agg->start_idx = start;
1810 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001811 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07001812 agg->frame_count, agg->start_idx,
John W. Linville06501d22008-04-01 17:38:47 -04001813 (unsigned long long)agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07001814
1815 if (bitmap)
1816 agg->wait_for_ba = 1;
1817 }
1818 return 0;
1819}
1820#endif
Zhu Yib481de92007-09-25 17:54:57 -07001821
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001822/**
1823 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
1824 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001825static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001826 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001827{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001828 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001829 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1830 int txq_id = SEQ_TO_QUEUE(sequence);
1831 int index = SEQ_TO_INDEX(sequence);
Ron Rindjunsky16466902008-05-05 10:22:50 +08001832 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07001833 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001834 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07001835 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001836#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001837 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
Tomas Winkler54dbb522008-05-15 13:54:06 +08001838 u16 fc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001839 struct ieee80211_hdr *hdr;
Tomas Winkler54dbb522008-05-15 13:54:06 +08001840 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001841#endif
1842
Tomas Winkler443cfd42008-05-15 13:53:57 +08001843 if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
Zhu Yib481de92007-09-25 17:54:57 -07001844 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
1845 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001846 index, txq->q.n_bd, txq->q.write_ptr,
1847 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07001848 return;
1849 }
1850
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001851#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001852 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
Tomas Winkler54dbb522008-05-15 13:54:06 +08001853 fc = le16_to_cpu(hdr->frame_control);
1854 if (ieee80211_is_qos_data(fc)) {
1855 qc = ieee80211_get_qos_ctrl(hdr, ieee80211_get_hdrlen(fc));
1856 tid = qc[0] & 0xf;
1857 }
Zhu Yib481de92007-09-25 17:54:57 -07001858
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001859 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
1860 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
1861 IWL_ERROR("Station not known\n");
1862 return;
1863 }
1864
1865 if (txq->sched_retry) {
1866 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
Tomas Winkler6def9762008-05-05 10:22:31 +08001867 struct iwl_ht_agg *agg = NULL;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001868
1869 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07001870 return;
Zhu Yib481de92007-09-25 17:54:57 -07001871
1872 agg = &priv->stations[sta_id].tid[tid].agg;
1873
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001874 iwl4965_tx_status_reply_tx(priv, agg,
1875 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07001876
1877 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001878 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07001879 /* TODO: send BAR */
1880 }
1881
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001882 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03001883 int freed, ampdu_q;
Tomas Winklerc54b6792008-03-06 17:36:53 -08001884 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07001885 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
1886 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001887 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
1888 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
1889
Tomas Winkler443cfd42008-05-15 13:53:57 +08001890 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001891 txq_id >= 0 && priv->mac80211_registered &&
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03001892 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA) {
1893 /* calculate mac80211 ampdu sw queue to wake */
1894 ampdu_q = txq_id - IWL_BACK_QUEUE_FIRST_ID +
1895 priv->hw->queues;
1896 if (agg->state == IWL_AGG_OFF)
1897 ieee80211_wake_queue(priv->hw, txq_id);
1898 else
1899 ieee80211_wake_queue(priv->hw, ampdu_q);
1900 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001901 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07001902 }
1903 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001904#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001905 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07001906
1907 tx_status->retry_count = tx_resp->failure_frame;
Zhu Yib481de92007-09-25 17:54:57 -07001908 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001909 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08001910 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
1911 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07001912
Zhu Yib481de92007-09-25 17:54:57 -07001913 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001914 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07001915 status, le32_to_cpu(tx_resp->rate_n_flags),
1916 tx_resp->failure_frame);
1917
1918 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Tomas Winkler47c51962008-05-05 10:22:41 +08001919#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001920 if (index != -1) {
1921 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001922 if (tid != MAX_TID_COUNT)
1923 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
Tomas Winkler443cfd42008-05-15 13:53:57 +08001924 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03001925 (txq_id >= 0) && priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001926 ieee80211_wake_queue(priv->hw, txq_id);
1927 if (tid != MAX_TID_COUNT)
1928 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07001929 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001930 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001931#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001932
1933 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
1934 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
1935}
1936
1937
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001938static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001939 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001940{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001941 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001942 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07001943 struct delayed_work *pwork;
1944
1945 palive = &pkt->u.alive_frame;
1946
1947 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
1948 "0x%01X 0x%01X\n",
1949 palive->is_valid, palive->ver_type,
1950 palive->ver_subtype);
1951
1952 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
1953 IWL_DEBUG_INFO("Initialization Alive received.\n");
1954 memcpy(&priv->card_alive_init,
1955 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001956 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001957 pwork = &priv->init_alive_start;
1958 } else {
1959 IWL_DEBUG_INFO("Runtime Alive received.\n");
1960 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001961 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001962 pwork = &priv->alive_start;
1963 }
1964
1965 /* We delay the ALIVE response by 5ms to
1966 * give the HW RF Kill time to activate... */
1967 if (palive->is_valid == UCODE_VALID_OK)
1968 queue_delayed_work(priv->workqueue, pwork,
1969 msecs_to_jiffies(5));
1970 else
1971 IWL_WARNING("uCode did not respond OK.\n");
1972}
1973
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001974static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001975 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001976{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001977 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001978
1979 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
1980 return;
1981}
1982
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001983static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08001984 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001985{
Tomas Winklerdb11d632008-05-05 10:22:33 +08001986 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001987
1988 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
1989 "seq 0x%04X ser 0x%08X\n",
1990 le32_to_cpu(pkt->u.err_resp.error_type),
1991 get_cmd_string(pkt->u.err_resp.cmd_id),
1992 pkt->u.err_resp.cmd_id,
1993 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1994 le32_to_cpu(pkt->u.err_resp.error_info));
1995}
1996
1997#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1998
Tomas Winklera55360e2008-05-05 10:22:28 +08001999static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002000{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002001 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08002002 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002003 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002004 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2005 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2006 rxon->channel = csa->channel;
2007 priv->staging_rxon.channel = csa->channel;
2008}
2009
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002010static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002011 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002012{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002013#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Tomas Winklerdb11d632008-05-05 10:22:33 +08002014 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002015 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002016
2017 if (!report->state) {
Ester Kummerf3d67992008-05-06 11:05:12 +08002018 IWL_DEBUG(IWL_DL_11H,
2019 "Spectrum Measure Notification: Start\n");
Zhu Yib481de92007-09-25 17:54:57 -07002020 return;
2021 }
2022
2023 memcpy(&priv->measure_report, report, sizeof(*report));
2024 priv->measurement_status |= MEASUREMENT_READY;
2025#endif
2026}
2027
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002028static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002029 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002030{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002031#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002032 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002033 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002034 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2035 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2036#endif
2037}
2038
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002039static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002040 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002041{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002042 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002043 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2044 "notification for %s:\n",
2045 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Ester Kummerbf403db2008-05-05 10:22:40 +08002046 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002047}
2048
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002049static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002050{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002051 struct iwl_priv *priv =
2052 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002053 struct sk_buff *beacon;
2054
2055 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01002056 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002057
2058 if (!beacon) {
2059 IWL_ERROR("update beacon failed\n");
2060 return;
2061 }
2062
2063 mutex_lock(&priv->mutex);
2064 /* new beacon skb is allocated every time; dispose previous.*/
2065 if (priv->ibss_beacon)
2066 dev_kfree_skb(priv->ibss_beacon);
2067
2068 priv->ibss_beacon = beacon;
2069 mutex_unlock(&priv->mutex);
2070
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002071 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002072}
2073
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002074static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002075 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002076{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002077#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002078 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002079 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
2080 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002081
2082 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2083 "tsf %d %d rate %d\n",
2084 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2085 beacon->beacon_notify_hdr.failure_frame,
2086 le32_to_cpu(beacon->ibss_mgr_status),
2087 le32_to_cpu(beacon->high_tsf),
2088 le32_to_cpu(beacon->low_tsf), rate);
2089#endif
2090
2091 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
2092 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2093 queue_work(priv->workqueue, &priv->beacon_update);
2094}
2095
2096/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002097static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002098 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002099{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002100#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002101 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002102 struct iwl4965_scanreq_notification *notif =
2103 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002104
2105 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2106#endif
2107}
2108
2109/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002110static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002111 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002112{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002113 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002114 struct iwl4965_scanstart_notification *notif =
2115 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002116 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2117 IWL_DEBUG_SCAN("Scan start: "
2118 "%d [802.11%s] "
2119 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2120 notif->channel,
2121 notif->band ? "bg" : "a",
2122 notif->tsf_high,
2123 notif->tsf_low, notif->status, notif->beacon_timer);
2124}
2125
2126/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002127static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002128 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002129{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002130 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002131 struct iwl4965_scanresults_notification *notif =
2132 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002133
2134 IWL_DEBUG_SCAN("Scan ch.res: "
2135 "%d [802.11%s] "
2136 "(TSF: 0x%08X:%08X) - %d "
2137 "elapsed=%lu usec (%dms since last)\n",
2138 notif->channel,
2139 notif->band ? "bg" : "a",
2140 le32_to_cpu(notif->tsf_high),
2141 le32_to_cpu(notif->tsf_low),
2142 le32_to_cpu(notif->statistics[0]),
2143 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
2144 jiffies_to_msecs(elapsed_jiffies
2145 (priv->last_scan_jiffies, jiffies)));
2146
2147 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002148 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002149}
2150
2151/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002152static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002153 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002154{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002155 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002156 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002157
2158 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
2159 scan_notif->scanned_channels,
2160 scan_notif->tsf_low,
2161 scan_notif->tsf_high, scan_notif->status);
2162
2163 /* The HW is no longer scanning */
2164 clear_bit(STATUS_SCAN_HW, &priv->status);
2165
2166 /* The scan completion notification came in, so kill that timer... */
2167 cancel_delayed_work(&priv->scan_check);
2168
2169 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
2170 (priv->scan_bands == 2) ? "2.4" : "5.2",
2171 jiffies_to_msecs(elapsed_jiffies
2172 (priv->scan_pass_start, jiffies)));
2173
2174 /* Remove this scanned band from the list
2175 * of pending bands to scan */
2176 priv->scan_bands--;
2177
2178 /* If a request to abort was given, or the scan did not succeed
2179 * then we reset the scan state machine and terminate,
2180 * re-queuing another scan if one has been requested */
2181 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2182 IWL_DEBUG_INFO("Aborted scan completed.\n");
2183 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
2184 } else {
2185 /* If there are more bands on this scan pass reschedule */
2186 if (priv->scan_bands > 0)
2187 goto reschedule;
2188 }
2189
2190 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002191 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002192 IWL_DEBUG_INFO("Setting scan to off\n");
2193
2194 clear_bit(STATUS_SCANNING, &priv->status);
2195
2196 IWL_DEBUG_INFO("Scan took %dms\n",
2197 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
2198
2199 queue_work(priv->workqueue, &priv->scan_completed);
2200
2201 return;
2202
2203reschedule:
2204 priv->scan_pass_start = jiffies;
2205 queue_work(priv->workqueue, &priv->request_scan);
2206}
2207
2208/* Handle notification from uCode that card's power state is changing
2209 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002210static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002211 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002212{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002213 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002214 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
2215 unsigned long status = priv->status;
2216
2217 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
2218 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
2219 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
2220
2221 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
2222 RF_CARD_DISABLED)) {
2223
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002224 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002225 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2226
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002227 if (!iwl_grab_nic_access(priv)) {
2228 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07002229 priv, HBUS_TARG_MBX_C,
2230 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
2231
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002232 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002233 }
2234
2235 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002236 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002237 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002238 if (!iwl_grab_nic_access(priv)) {
2239 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07002240 priv, HBUS_TARG_MBX_C,
2241 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
2242
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002243 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002244 }
2245 }
2246
2247 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002248 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002249 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002250 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2251 if (!iwl_grab_nic_access(priv))
2252 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002253 }
2254 }
2255
2256 if (flags & HW_CARD_DISABLED)
2257 set_bit(STATUS_RF_KILL_HW, &priv->status);
2258 else
2259 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2260
2261
2262 if (flags & SW_CARD_DISABLED)
2263 set_bit(STATUS_RF_KILL_SW, &priv->status);
2264 else
2265 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2266
2267 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002268 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002269
2270 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
2271 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
2272 (test_bit(STATUS_RF_KILL_SW, &status) !=
2273 test_bit(STATUS_RF_KILL_SW, &priv->status)))
2274 queue_work(priv->workqueue, &priv->rf_kill);
2275 else
2276 wake_up_interruptible(&priv->wait_command_queue);
2277}
2278
2279/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002280 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07002281 *
2282 * Setup the RX handlers for each of the reply types sent from the uCode
2283 * to the host.
2284 *
2285 * This function chains into the hardware specific files for them to setup
2286 * any hardware specific handlers as well.
2287 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002288static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002289{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002290 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
2291 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
2292 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
2293 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07002294 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002295 iwl4965_rx_spectrum_measure_notif;
2296 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002297 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002298 iwl4965_rx_pm_debug_statistics_notif;
2299 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002300
Ben Cahill9fbab512007-11-29 11:09:47 +08002301 /*
2302 * The same handler is used for both the REPLY to a discrete
2303 * statistics request from the host as well as for the periodic
2304 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07002305 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002306 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
2307 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07002308
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002309 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
2310 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002311 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002312 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002313 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002314 iwl4965_rx_scan_complete_notif;
2315 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
2316 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07002317
Ben Cahill9fbab512007-11-29 11:09:47 +08002318 /* Set up hardware specific Rx handlers */
Emmanuel Grumbachd4789ef2008-04-24 11:55:20 -07002319 priv->cfg->ops->lib->rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002320}
2321
2322/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002323 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07002324 * @rxb: Rx buffer to reclaim
2325 *
2326 * If an Rx buffer has an async callback associated with it the callback
2327 * will be executed. The attached skb (if present) will only be freed
2328 * if the callback returns 1
2329 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002330static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002331 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002332{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002333 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002334 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2335 int txq_id = SEQ_TO_QUEUE(sequence);
2336 int index = SEQ_TO_INDEX(sequence);
2337 int huge = sequence & SEQ_HUGE_FRAME;
2338 int cmd_index;
Tomas Winkler857485c2008-03-21 13:53:44 -07002339 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002340
2341 /* If a Tx command is being handled and it isn't in the actual
2342 * command queue then there a command routing bug has been introduced
2343 * in the queue management code. */
2344 if (txq_id != IWL_CMD_QUEUE_NUM)
2345 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
2346 txq_id, pkt->hdr.cmd);
2347 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
2348
2349 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
2350 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
2351
2352 /* Input error checking is done when commands are added to queue. */
2353 if (cmd->meta.flags & CMD_WANT_SKB) {
2354 cmd->meta.source->u.skb = rxb->skb;
2355 rxb->skb = NULL;
2356 } else if (cmd->meta.u.callback &&
2357 !cmd->meta.u.callback(priv, cmd, rxb->skb))
2358 rxb->skb = NULL;
2359
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002360 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07002361
2362 if (!(cmd->meta.flags & CMD_ASYNC)) {
2363 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
2364 wake_up_interruptible(&priv->wait_command_queue);
2365 }
2366}
2367
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002368/*
2369 * this should be called while priv->lock is locked
2370*/
Tomas Winklera55360e2008-05-05 10:22:28 +08002371static void __iwl_rx_replenish(struct iwl_priv *priv)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002372{
Tomas Winklera55360e2008-05-05 10:22:28 +08002373 iwl_rx_allocate(priv);
2374 iwl_rx_queue_restock(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002375}
2376
2377
Zhu Yib481de92007-09-25 17:54:57 -07002378/**
Tomas Winklera55360e2008-05-05 10:22:28 +08002379 * iwl_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07002380 *
2381 * Uses the priv->rx_handlers callback function array to invoke
2382 * the appropriate handlers, including command responses,
2383 * frame-received notifications, and other notifications.
2384 */
Tomas Winklera55360e2008-05-05 10:22:28 +08002385void iwl_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002386{
Tomas Winklera55360e2008-05-05 10:22:28 +08002387 struct iwl_rx_mem_buffer *rxb;
Tomas Winklerdb11d632008-05-05 10:22:33 +08002388 struct iwl_rx_packet *pkt;
Tomas Winklera55360e2008-05-05 10:22:28 +08002389 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07002390 u32 r, i;
2391 int reclaim;
2392 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002393 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08002394 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07002395
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002396 /* uCode's read index (stored in shared DRAM) indicates the last Rx
2397 * buffer that the driver may process (last buffer filled by ucode). */
Ron Rindjunskyd67f5482008-05-05 10:22:49 +08002398 r = priv->cfg->ops->lib->shared_mem_rx_idx(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002399 i = rxq->read;
2400
2401 /* Rx interrupt, but nothing sent from uCode */
2402 if (i == r)
Ester Kummerf3d67992008-05-06 11:05:12 +08002403 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07002404
Tomas Winklera55360e2008-05-05 10:22:28 +08002405 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002406 fill_rx = 1;
2407
Zhu Yib481de92007-09-25 17:54:57 -07002408 while (i != r) {
2409 rxb = rxq->queue[i];
2410
Ben Cahill9fbab512007-11-29 11:09:47 +08002411 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07002412 * then a bug has been introduced in the queue refilling
2413 * routines -- catch it here */
2414 BUG_ON(rxb == NULL);
2415
2416 rxq->queue[i] = NULL;
2417
2418 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07002419 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07002420 PCI_DMA_FROMDEVICE);
Tomas Winklerdb11d632008-05-05 10:22:33 +08002421 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002422
2423 /* Reclaim a command buffer only if this packet is a response
2424 * to a (driver-originated) command.
2425 * If the packet (e.g. Rx frame) originated from uCode,
2426 * there is no command buffer to reclaim.
2427 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
2428 * but apparently a few don't get set; catch them here. */
2429 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
2430 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07002431 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08002432 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07002433 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
2434 (pkt->hdr.cmd != REPLY_TX);
2435
2436 /* Based on type of command response or notification,
2437 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002438 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07002439 if (priv->rx_handlers[pkt->hdr.cmd]) {
Ester Kummerf3d67992008-05-06 11:05:12 +08002440 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d, %s, 0x%02x\n", r,
2441 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002442 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
2443 } else {
2444 /* No handling needed */
Ester Kummerf3d67992008-05-06 11:05:12 +08002445 IWL_DEBUG(IWL_DL_RX,
Zhu Yib481de92007-09-25 17:54:57 -07002446 "r %d i %d No handler needed for %s, 0x%02x\n",
2447 r, i, get_cmd_string(pkt->hdr.cmd),
2448 pkt->hdr.cmd);
2449 }
2450
2451 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08002452 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07002453 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07002454 * as we reclaim the driver command queue */
2455 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002456 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07002457 else
2458 IWL_WARNING("Claim null rxb?\n");
2459 }
2460
2461 /* For now we just don't re-use anything. We can tweak this
2462 * later to try and re-use notification packets and SKBs that
2463 * fail to Rx correctly */
2464 if (rxb->skb != NULL) {
2465 priv->alloc_rxb_skb--;
2466 dev_kfree_skb_any(rxb->skb);
2467 rxb->skb = NULL;
2468 }
2469
2470 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07002471 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02002472 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07002473 spin_lock_irqsave(&rxq->lock, flags);
2474 list_add_tail(&rxb->list, &priv->rxq.rx_used);
2475 spin_unlock_irqrestore(&rxq->lock, flags);
2476 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002477 /* If there are a lot of unused frames,
2478 * restock the Rx queue so ucode wont assert. */
2479 if (fill_rx) {
2480 count++;
2481 if (count >= 8) {
2482 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08002483 __iwl_rx_replenish(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002484 count = 0;
2485 }
2486 }
Zhu Yib481de92007-09-25 17:54:57 -07002487 }
2488
2489 /* Backtrack one entry */
2490 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08002491 iwl_rx_queue_restock(priv);
2492}
2493/* Convert linear signal-to-noise ratio into dB */
2494static u8 ratio2dB[100] = {
2495/* 0 1 2 3 4 5 6 7 8 9 */
2496 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
2497 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
2498 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
2499 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
2500 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
2501 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
2502 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
2503 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
2504 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
2505 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
2506};
2507
2508/* Calculates a relative dB value from a ratio of linear
2509 * (i.e. not dB) signal levels.
2510 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
2511int iwl4965_calc_db_from_ratio(int sig_ratio)
2512{
2513 /* 1000:1 or higher just report as 60 dB */
2514 if (sig_ratio >= 1000)
2515 return 60;
2516
2517 /* 100:1 or higher, divide by 10 and use table,
2518 * add 20 dB to make up for divide by 10 */
2519 if (sig_ratio >= 100)
2520 return (20 + (int)ratio2dB[sig_ratio/10]);
2521
2522 /* We shouldn't see this */
2523 if (sig_ratio < 1)
2524 return 0;
2525
2526 /* Use table for ratios 1:1 - 99:1 */
2527 return (int)ratio2dB[sig_ratio];
2528}
2529
2530#define PERFECT_RSSI (-20) /* dBm */
2531#define WORST_RSSI (-95) /* dBm */
2532#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
2533
2534/* Calculate an indication of rx signal quality (a percentage, not dBm!).
2535 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
2536 * about formulas used below. */
2537int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
2538{
2539 int sig_qual;
2540 int degradation = PERFECT_RSSI - rssi_dbm;
2541
2542 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
2543 * as indicator; formula is (signal dbm - noise dbm).
2544 * SNR at or above 40 is a great signal (100%).
2545 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
2546 * Weakest usable signal is usually 10 - 15 dB SNR. */
2547 if (noise_dbm) {
2548 if (rssi_dbm - noise_dbm >= 40)
2549 return 100;
2550 else if (rssi_dbm < noise_dbm)
2551 return 0;
2552 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
2553
2554 /* Else use just the signal level.
2555 * This formula is a least squares fit of data points collected and
2556 * compared with a reference system that had a percentage (%) display
2557 * for signal quality. */
2558 } else
2559 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
2560 (15 * RSSI_RANGE + 62 * degradation)) /
2561 (RSSI_RANGE * RSSI_RANGE);
2562
2563 if (sig_qual > 100)
2564 sig_qual = 100;
2565 else if (sig_qual < 1)
2566 sig_qual = 0;
2567
2568 return sig_qual;
Zhu Yib481de92007-09-25 17:54:57 -07002569}
2570
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002571#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08002572static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002573{
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08002574 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07002575 DECLARE_MAC_BUF(mac);
2576
Zhu Yib481de92007-09-25 17:54:57 -07002577 IWL_DEBUG_RADIO("RX CONFIG:\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002578 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07002579 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
2580 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
2581 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
2582 le32_to_cpu(rxon->filter_flags));
2583 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
2584 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
2585 rxon->ofdm_basic_rates);
2586 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07002587 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
2588 print_mac(mac, rxon->node_addr));
2589 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
2590 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07002591 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
2592}
2593#endif
2594
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002595static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002596{
2597 IWL_DEBUG_ISR("Enabling interrupts\n");
2598 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002599 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07002600}
2601
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002602/* call this function to flush any scheduled tasklet */
2603static inline void iwl_synchronize_irq(struct iwl_priv *priv)
2604{
2605 /* wait to make sure we flush pedding tasklet*/
2606 synchronize_irq(priv->pci_dev->irq);
2607 tasklet_kill(&priv->irq_tasklet);
2608}
2609
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002610static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002611{
2612 clear_bit(STATUS_INT_ENABLED, &priv->status);
2613
2614 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002615 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07002616
2617 /* acknowledge/clear/reset any interrupts still pending
2618 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002619 iwl_write32(priv, CSR_INT, 0xffffffff);
2620 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07002621 IWL_DEBUG_ISR("Disabled interrupts\n");
2622}
2623
2624static const char *desc_lookup(int i)
2625{
2626 switch (i) {
2627 case 1:
2628 return "FAIL";
2629 case 2:
2630 return "BAD_PARAM";
2631 case 3:
2632 return "BAD_CHECKSUM";
2633 case 4:
2634 return "NMI_INTERRUPT";
2635 case 5:
2636 return "SYSASSERT";
2637 case 6:
2638 return "FATAL_ERROR";
2639 }
2640
2641 return "UNKNOWN";
2642}
2643
2644#define ERROR_START_OFFSET (1 * sizeof(u32))
2645#define ERROR_ELEM_SIZE (7 * sizeof(u32))
2646
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002647static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002648{
2649 u32 data2, line;
2650 u32 desc, time, count, base, data1;
2651 u32 blink1, blink2, ilink1, ilink2;
2652 int rc;
2653
2654 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
2655
Tomas Winkler57aab752008-04-14 21:16:03 -07002656 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07002657 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
2658 return;
2659 }
2660
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002661 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002662 if (rc) {
2663 IWL_WARNING("Can not read from adapter at this time.\n");
2664 return;
2665 }
2666
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002667 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07002668
2669 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
2670 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02002671 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07002672 }
2673
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002674 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
2675 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
2676 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
2677 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
2678 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
2679 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
2680 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
2681 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
2682 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07002683
2684 IWL_ERROR("Desc Time "
2685 "data1 data2 line\n");
2686 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
2687 desc_lookup(desc), desc, time, data1, data2, line);
2688 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
2689 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
2690 ilink1, ilink2);
2691
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002692 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002693}
2694
2695#define EVENT_START_OFFSET (4 * sizeof(u32))
2696
2697/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002698 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07002699 *
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002700 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07002701 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002702static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07002703 u32 num_events, u32 mode)
2704{
2705 u32 i;
2706 u32 base; /* SRAM byte address of event log header */
2707 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
2708 u32 ptr; /* SRAM byte address of log data */
2709 u32 ev, time, data; /* event log data */
2710
2711 if (num_events == 0)
2712 return;
2713
2714 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
2715
2716 if (mode == 0)
2717 event_size = 2 * sizeof(u32);
2718 else
2719 event_size = 3 * sizeof(u32);
2720
2721 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
2722
2723 /* "time" is actually "data" for mode 0 (no timestamp).
2724 * place event id # at far right for easier visual parsing. */
2725 for (i = 0; i < num_events; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002726 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002727 ptr += sizeof(u32);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002728 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002729 ptr += sizeof(u32);
2730 if (mode == 0)
2731 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
2732 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002733 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002734 ptr += sizeof(u32);
2735 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
2736 }
2737 }
2738}
2739
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002740static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002741{
2742 int rc;
2743 u32 base; /* SRAM byte address of event log header */
2744 u32 capacity; /* event log capacity in # entries */
2745 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
2746 u32 num_wraps; /* # times uCode wrapped to top of log */
2747 u32 next_entry; /* index of next entry to be written by uCode */
2748 u32 size; /* # entries that we'll print */
2749
2750 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Tomas Winkler57aab752008-04-14 21:16:03 -07002751 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07002752 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
2753 return;
2754 }
2755
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002756 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002757 if (rc) {
2758 IWL_WARNING("Can not read from adapter at this time.\n");
2759 return;
2760 }
2761
2762 /* event log header */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002763 capacity = iwl_read_targ_mem(priv, base);
2764 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
2765 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
2766 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07002767
2768 size = num_wraps ? capacity : next_entry;
2769
2770 /* bail out if nothing in log */
2771 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08002772 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002773 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002774 return;
2775 }
2776
Zhu Yi583fab32007-09-27 11:27:30 +08002777 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07002778 size, num_wraps);
2779
2780 /* if uCode has wrapped back to top of log, start at the oldest entry,
2781 * i.e the next one that uCode would fill. */
2782 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002783 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07002784 capacity - next_entry, mode);
2785
2786 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002787 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07002788
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002789 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002790}
2791
2792/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002793 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07002794 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002795static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002796{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002797 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07002798 set_bit(STATUS_FW_ERROR, &priv->status);
2799
2800 /* Cancel currently queued command. */
2801 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
2802
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002803#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08002804 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002805 iwl4965_dump_nic_error_log(priv);
2806 iwl4965_dump_nic_event_log(priv);
Ester Kummerbf403db2008-05-05 10:22:40 +08002807 iwl4965_print_rx_config_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002808 }
2809#endif
2810
2811 wake_up_interruptible(&priv->wait_command_queue);
2812
2813 /* Keep the restart process from trying to send host
2814 * commands by clearing the INIT status bit */
2815 clear_bit(STATUS_READY, &priv->status);
2816
2817 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08002818 IWL_DEBUG(IWL_DL_FW_ERRORS,
Zhu Yib481de92007-09-25 17:54:57 -07002819 "Restarting adapter due to uCode error.\n");
2820
Tomas Winkler3109ece2008-03-28 16:33:35 -07002821 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002822 memcpy(&priv->recovery_rxon, &priv->active_rxon,
2823 sizeof(priv->recovery_rxon));
2824 priv->error_recovering = 1;
2825 }
Ester Kummer3a1081e2008-05-06 11:05:14 +08002826 if (priv->cfg->mod_params->restart_fw)
2827 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07002828 }
2829}
2830
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002831static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002832{
2833 unsigned long flags;
2834
2835 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
2836 sizeof(priv->staging_rxon));
2837 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002838 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002839
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08002840 iwl_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07002841
2842 spin_lock_irqsave(&priv->lock, flags);
2843 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
2844 priv->error_recovering = 0;
2845 spin_unlock_irqrestore(&priv->lock, flags);
2846}
2847
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002848static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002849{
2850 u32 inta, handled = 0;
2851 u32 inta_fh;
2852 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002853#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002854 u32 inta_mask;
2855#endif
2856
2857 spin_lock_irqsave(&priv->lock, flags);
2858
2859 /* Ack/clear/reset pending uCode interrupts.
2860 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
2861 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002862 inta = iwl_read32(priv, CSR_INT);
2863 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07002864
2865 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
2866 * Any new interrupts that happen after this, either while we're
2867 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002868 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
2869 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07002870
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002871#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08002872 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08002873 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002874 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07002875 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
2876 inta, inta_mask, inta_fh);
2877 }
2878#endif
2879
2880 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
2881 * atomic, make sure that inta covers all the interrupts that
2882 * we've discovered, even if FH interrupt came in just after
2883 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08002884 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07002885 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08002886 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07002887 inta |= CSR_INT_BIT_FH_TX;
2888
2889 /* Now service all interrupt bits discovered above. */
2890 if (inta & CSR_INT_BIT_HW_ERR) {
2891 IWL_ERROR("Microcode HW error detected. Restarting.\n");
2892
2893 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002894 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002895
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002896 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002897
2898 handled |= CSR_INT_BIT_HW_ERR;
2899
2900 spin_unlock_irqrestore(&priv->lock, flags);
2901
2902 return;
2903 }
2904
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002905#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08002906 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07002907 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08002908 if (inta & CSR_INT_BIT_SCD)
2909 IWL_DEBUG_ISR("Scheduler finished to transmit "
2910 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002911
2912 /* Alive notification via Rx interrupt will do the real work */
2913 if (inta & CSR_INT_BIT_ALIVE)
2914 IWL_DEBUG_ISR("Alive interrupt\n");
2915 }
2916#endif
2917 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08002918 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07002919
Ben Cahill9fbab512007-11-29 11:09:47 +08002920 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07002921 if (inta & CSR_INT_BIT_RF_KILL) {
2922 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002923 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07002924 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
2925 hw_rf_kill = 1;
2926
Ester Kummerf3d67992008-05-06 11:05:12 +08002927 IWL_DEBUG(IWL_DL_RF_KILL, "RF_KILL bit toggled to %s.\n",
Zhu Yib481de92007-09-25 17:54:57 -07002928 hw_rf_kill ? "disable radio":"enable radio");
2929
2930 /* Queue restart only if RF_KILL switch was set to "kill"
2931 * when we loaded driver, and is now set to "enable".
2932 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08002933 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08002934 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
2935 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002936 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08002937 }
Zhu Yib481de92007-09-25 17:54:57 -07002938
2939 handled |= CSR_INT_BIT_RF_KILL;
2940 }
2941
Ben Cahill9fbab512007-11-29 11:09:47 +08002942 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07002943 if (inta & CSR_INT_BIT_CT_KILL) {
2944 IWL_ERROR("Microcode CT kill error detected.\n");
2945 handled |= CSR_INT_BIT_CT_KILL;
2946 }
2947
2948 /* Error detected by uCode */
2949 if (inta & CSR_INT_BIT_SW_ERR) {
2950 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
2951 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002952 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002953 handled |= CSR_INT_BIT_SW_ERR;
2954 }
2955
2956 /* uCode wakes up after power-down sleep */
2957 if (inta & CSR_INT_BIT_WAKEUP) {
2958 IWL_DEBUG_ISR("Wakeup interrupt\n");
Tomas Winklera55360e2008-05-05 10:22:28 +08002959 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Tomas Winklerbabcebf2008-05-15 13:54:00 +08002960 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
2961 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
2962 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
2963 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
2964 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
2965 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07002966
2967 handled |= CSR_INT_BIT_WAKEUP;
2968 }
2969
2970 /* All uCode command responses, including Tx command responses,
2971 * Rx "responses" (frame-received notification), and other
2972 * notifications from uCode come through here*/
2973 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Tomas Winklera55360e2008-05-05 10:22:28 +08002974 iwl_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002975 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
2976 }
2977
2978 if (inta & CSR_INT_BIT_FH_TX) {
2979 IWL_DEBUG_ISR("Tx interrupt\n");
2980 handled |= CSR_INT_BIT_FH_TX;
Ron Rindjunskydbb983b2008-05-15 13:54:12 +08002981 /* FH finished to write, send event */
2982 priv->ucode_write_complete = 1;
2983 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002984 }
2985
2986 if (inta & ~handled)
2987 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
2988
2989 if (inta & ~CSR_INI_SET_MASK) {
2990 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
2991 inta & ~CSR_INI_SET_MASK);
2992 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
2993 }
2994
2995 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002996 /* only Re-enable if diabled by irq */
2997 if (test_bit(STATUS_INT_ENABLED, &priv->status))
2998 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002999
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003000#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003001 if (priv->debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003002 inta = iwl_read32(priv, CSR_INT);
3003 inta_mask = iwl_read32(priv, CSR_INT_MASK);
3004 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003005 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
3006 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
3007 }
3008#endif
3009 spin_unlock_irqrestore(&priv->lock, flags);
3010}
3011
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003012static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07003013{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003014 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07003015 u32 inta, inta_mask;
3016 u32 inta_fh;
3017 if (!priv)
3018 return IRQ_NONE;
3019
3020 spin_lock(&priv->lock);
3021
3022 /* Disable (but don't clear!) interrupts here to avoid
3023 * back-to-back ISRs and sporadic interrupts from our NIC.
3024 * If we have something to service, the tasklet will re-enable ints.
3025 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003026 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
3027 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003028
3029 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003030 inta = iwl_read32(priv, CSR_INT);
3031 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003032
3033 /* Ignore interrupt if there's nothing in NIC to service.
3034 * This may be due to IRQ shared with another device,
3035 * or due to sporadic interrupts thrown from our NIC. */
3036 if (!inta && !inta_fh) {
3037 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
3038 goto none;
3039 }
3040
3041 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08003042 /* Hardware disappeared. It might have already raised
3043 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07003044 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08003045 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07003046 }
3047
3048 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3049 inta, inta_mask, inta_fh);
3050
Joonwoo Park25c03d82008-01-23 10:15:20 -08003051 inta &= ~CSR_INT_BIT_SCD;
3052
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003053 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003054 if (likely(inta || inta_fh))
3055 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07003056
Oliver Neukum66fbb542007-11-15 09:31:10 +08003057 unplugged:
3058 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07003059 return IRQ_HANDLED;
3060
3061 none:
3062 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003063 /* only Re-enable if diabled by irq */
3064 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3065 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003066 spin_unlock(&priv->lock);
3067 return IRQ_NONE;
3068}
3069
Zhu Yib481de92007-09-25 17:54:57 -07003070/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
3071 * sending probe req. This should be set long enough to hear probe responses
3072 * from more than one AP. */
3073#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
3074#define IWL_ACTIVE_DWELL_TIME_52 (10)
3075
3076/* For faster active scanning, scan will move to the next channel if fewer than
3077 * PLCP_QUIET_THRESH packets are heard on this channel within
3078 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
3079 * time if it's a quiet channel (nothing responded to our probe, and there's
3080 * no other traffic).
3081 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
3082#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
3083#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
3084
3085/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
3086 * Must be set longer than active dwell time.
3087 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
3088#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
3089#define IWL_PASSIVE_DWELL_TIME_52 (10)
3090#define IWL_PASSIVE_DWELL_BASE (100)
3091#define IWL_CHANNEL_TUNE_TIME 5
3092
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003093static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003094 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07003095{
Johannes Berg8318d782008-01-24 19:38:38 +01003096 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07003097 return IWL_ACTIVE_DWELL_TIME_52;
3098 else
3099 return IWL_ACTIVE_DWELL_TIME_24;
3100}
3101
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003102static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003103 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07003104{
Johannes Berg8318d782008-01-24 19:38:38 +01003105 u16 active = iwl4965_get_active_dwell_time(priv, band);
3106 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07003107 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
3108 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
3109
Tomas Winkler3109ece2008-03-28 16:33:35 -07003110 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003111 /* If we're associated, we clamp the maximum passive
3112 * dwell time to be 98% of the beacon interval (minus
3113 * 2 * channel tune time) */
3114 passive = priv->beacon_int;
3115 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
3116 passive = IWL_PASSIVE_DWELL_BASE;
3117 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
3118 }
3119
3120 if (passive <= active)
3121 passive = active + 1;
3122
3123 return passive;
3124}
3125
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003126static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003127 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07003128 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003129 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07003130{
3131 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01003132 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003133 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07003134 u16 passive_dwell = 0;
3135 u16 active_dwell = 0;
3136 int added, i;
3137
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07003138 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01003139 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07003140 return 0;
3141
Johannes Berg8318d782008-01-24 19:38:38 +01003142 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07003143
Johannes Berg8318d782008-01-24 19:38:38 +01003144 active_dwell = iwl4965_get_active_dwell_time(priv, band);
3145 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07003146
Johannes Berg8318d782008-01-24 19:38:38 +01003147 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02003148 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3149 continue;
3150
Johannes Berg8318d782008-01-24 19:38:38 +01003151 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07003152
Assaf Krauss8622e702008-03-21 13:53:43 -07003153 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08003154 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07003155 if (!is_channel_valid(ch_info)) {
3156 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
3157 scan_ch->channel);
3158 continue;
3159 }
3160
3161 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01003162 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07003163 scan_ch->type = 0; /* passive */
3164 else
3165 scan_ch->type = 1; /* active */
3166
3167 if (scan_ch->type & 1)
3168 scan_ch->type |= (direct_mask << 1);
3169
Zhu Yib481de92007-09-25 17:54:57 -07003170 scan_ch->active_dwell = cpu_to_le16(active_dwell);
3171 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
3172
Ben Cahill9fbab512007-11-29 11:09:47 +08003173 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07003174 scan_ch->tpc.dsp_atten = 110;
3175 /* scan_pwr_info->tpc.dsp_atten; */
3176
3177 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01003178 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07003179 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
3180 else {
3181 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
3182 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08003183 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08003184 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07003185 */
3186 }
3187
3188 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
3189 scan_ch->channel,
3190 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
3191 (scan_ch->type & 1) ?
3192 active_dwell : passive_dwell);
3193
3194 scan_ch++;
3195 added++;
3196 }
3197
3198 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
3199 return added;
3200}
3201
Zhu Yib481de92007-09-25 17:54:57 -07003202/******************************************************************************
3203 *
3204 * uCode download functions
3205 *
3206 ******************************************************************************/
3207
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003208static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003209{
Tomas Winkler98c92212008-01-14 17:46:20 -08003210 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
3211 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
3212 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
3213 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
3214 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
3215 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07003216}
3217
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08003218static void iwl4965_nic_start(struct iwl_priv *priv)
3219{
3220 /* Remove all resets to allow NIC to operate */
3221 iwl_write32(priv, CSR_RESET, 0);
3222}
3223
3224
Zhu Yib481de92007-09-25 17:54:57 -07003225/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003226 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07003227 *
3228 * Copy into buffers for card to fetch via bus-mastering
3229 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003230static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003231{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003232 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003233 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003234 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08003235 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07003236 u8 *src;
3237 size_t len;
3238 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
3239
3240 /* Ask kernel firmware_class module to get the boot firmware off disk.
3241 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08003242 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
3243 if (ret < 0) {
3244 IWL_ERROR("%s firmware file req failed: Reason %d\n",
3245 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07003246 goto error;
3247 }
3248
3249 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
3250 name, ucode_raw->size);
3251
3252 /* Make sure that we got at least our header! */
3253 if (ucode_raw->size < sizeof(*ucode)) {
3254 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08003255 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003256 goto err_release;
3257 }
3258
3259 /* Data from ucode file: header followed by uCode images */
3260 ucode = (void *)ucode_raw->data;
3261
3262 ver = le32_to_cpu(ucode->ver);
3263 inst_size = le32_to_cpu(ucode->inst_size);
3264 data_size = le32_to_cpu(ucode->data_size);
3265 init_size = le32_to_cpu(ucode->init_size);
3266 init_data_size = le32_to_cpu(ucode->init_data_size);
3267 boot_size = le32_to_cpu(ucode->boot_size);
3268
3269 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
3270 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
3271 inst_size);
3272 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
3273 data_size);
3274 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
3275 init_size);
3276 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
3277 init_data_size);
3278 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
3279 boot_size);
3280
3281 /* Verify size of file vs. image size info in file's header */
3282 if (ucode_raw->size < sizeof(*ucode) +
3283 inst_size + data_size + init_size +
3284 init_data_size + boot_size) {
3285
3286 IWL_DEBUG_INFO("uCode file size %d too small\n",
3287 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08003288 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003289 goto err_release;
3290 }
3291
3292 /* Verify that uCode images will fit in card's SRAM */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07003293 if (inst_size > priv->hw_params.max_inst_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08003294 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
3295 inst_size);
3296 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003297 goto err_release;
3298 }
3299
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07003300 if (data_size > priv->hw_params.max_data_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08003301 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
3302 data_size);
3303 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003304 goto err_release;
3305 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07003306 if (init_size > priv->hw_params.max_inst_size) {
Zhu Yib481de92007-09-25 17:54:57 -07003307 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08003308 ("uCode init instr len %d too large to fit in\n",
3309 init_size);
3310 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003311 goto err_release;
3312 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07003313 if (init_data_size > priv->hw_params.max_data_size) {
Zhu Yib481de92007-09-25 17:54:57 -07003314 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08003315 ("uCode init data len %d too large to fit in\n",
3316 init_data_size);
3317 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003318 goto err_release;
3319 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07003320 if (boot_size > priv->hw_params.max_bsm_size) {
Zhu Yib481de92007-09-25 17:54:57 -07003321 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08003322 ("uCode boot instr len %d too large to fit in\n",
3323 boot_size);
3324 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003325 goto err_release;
3326 }
3327
3328 /* Allocate ucode buffers for card's bus-master loading ... */
3329
3330 /* Runtime instructions and 2 copies of data:
3331 * 1) unmodified from disk
3332 * 2) backup cache for save/restore during power-downs */
3333 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003334 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07003335
3336 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003337 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07003338
3339 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003340 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07003341
3342 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08003343 if (init_size && init_data_size) {
3344 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003345 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07003346
Tomas Winkler90e759d2007-11-29 11:09:41 +08003347 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003348 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08003349
3350 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
3351 goto err_pci_alloc;
3352 }
Zhu Yib481de92007-09-25 17:54:57 -07003353
3354 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08003355 if (boot_size) {
3356 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003357 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07003358
Tomas Winkler90e759d2007-11-29 11:09:41 +08003359 if (!priv->ucode_boot.v_addr)
3360 goto err_pci_alloc;
3361 }
Zhu Yib481de92007-09-25 17:54:57 -07003362
3363 /* Copy images into buffers for card's bus-master reads ... */
3364
3365 /* Runtime instructions (first block of data in file) */
3366 src = &ucode->data[0];
3367 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003368 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07003369 memcpy(priv->ucode_code.v_addr, src, len);
3370 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
3371 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
3372
3373 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003374 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07003375 src = &ucode->data[inst_size];
3376 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003377 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07003378 memcpy(priv->ucode_data.v_addr, src, len);
3379 memcpy(priv->ucode_data_backup.v_addr, src, len);
3380
3381 /* Initialization instructions (3rd block) */
3382 if (init_size) {
3383 src = &ucode->data[inst_size + data_size];
3384 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003385 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
3386 len);
Zhu Yib481de92007-09-25 17:54:57 -07003387 memcpy(priv->ucode_init.v_addr, src, len);
3388 }
3389
3390 /* Initialization data (4th block) */
3391 if (init_data_size) {
3392 src = &ucode->data[inst_size + data_size + init_size];
3393 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003394 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
3395 len);
Zhu Yib481de92007-09-25 17:54:57 -07003396 memcpy(priv->ucode_init_data.v_addr, src, len);
3397 }
3398
3399 /* Bootstrap instructions (5th block) */
3400 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
3401 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003402 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07003403 memcpy(priv->ucode_boot.v_addr, src, len);
3404
3405 /* We have our copies now, allow OS release its copies */
3406 release_firmware(ucode_raw);
3407 return 0;
3408
3409 err_pci_alloc:
3410 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08003411 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003412 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003413
3414 err_release:
3415 release_firmware(ucode_raw);
3416
3417 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08003418 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003419}
3420
Zhu Yib481de92007-09-25 17:54:57 -07003421/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003422 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07003423 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003424 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07003425 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003426static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003427{
Tomas Winkler57aab752008-04-14 21:16:03 -07003428 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003429
3430 IWL_DEBUG_INFO("Runtime Alive received.\n");
3431
3432 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
3433 /* We had an error bringing up the hardware, so take it
3434 * all the way back down so we can try again */
3435 IWL_DEBUG_INFO("Alive failed.\n");
3436 goto restart;
3437 }
3438
3439 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
3440 * This is a paranoid check, because we would not have gotten the
3441 * "runtime" alive if code weren't properly loaded. */
Emmanuel Grumbachb0692f22008-04-24 11:55:18 -07003442 if (iwl_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003443 /* Runtime instruction load was bad;
3444 * take it all the way back down so we can try again */
3445 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
3446 goto restart;
3447 }
3448
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003449 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003450
Tomas Winkler57aab752008-04-14 21:16:03 -07003451 ret = priv->cfg->ops->lib->alive_notify(priv);
3452 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07003453 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07003454 ret);
Zhu Yib481de92007-09-25 17:54:57 -07003455 goto restart;
3456 }
3457
Ben Cahill9fbab512007-11-29 11:09:47 +08003458 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07003459 set_bit(STATUS_ALIVE, &priv->status);
3460
3461 /* Clear out the uCode error bit if it is set */
3462 clear_bit(STATUS_FW_ERROR, &priv->status);
3463
Tomas Winklerfee12472008-04-03 16:05:21 -07003464 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003465 return;
3466
Zhu Yi5a66926a2008-01-14 17:46:18 -08003467 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07003468
3469 priv->active_rate = priv->rates_mask;
3470 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
3471
Tomas Winkler3109ece2008-03-28 16:33:35 -07003472 if (iwl_is_associated(priv)) {
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08003473 struct iwl_rxon_cmd *active_rxon =
3474 (struct iwl_rxon_cmd *)&priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07003475
3476 memcpy(&priv->staging_rxon, &priv->active_rxon,
3477 sizeof(priv->staging_rxon));
3478 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3479 } else {
3480 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003481 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003482 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
3483 }
3484
Ben Cahill9fbab512007-11-29 11:09:47 +08003485 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003486 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003487
3488 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003489 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003490
3491 /* At this point, the NIC is initialized and operational */
3492 priv->notif_missed_beacons = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003493
3494 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003495
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07003496 iwl_leds_register(priv);
3497
Zhu Yib481de92007-09-25 17:54:57 -07003498 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07003499 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003500 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07003501
3502 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003503 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003504
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07003505 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Mohamed Abbas84363e62008-04-04 16:59:58 -07003506 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Zhu Yib481de92007-09-25 17:54:57 -07003507 return;
3508
3509 restart:
3510 queue_work(priv->workqueue, &priv->restart);
3511}
3512
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003513static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07003514
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003515static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003516{
3517 unsigned long flags;
3518 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
3519 struct ieee80211_conf *conf = NULL;
3520
3521 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
3522
3523 conf = ieee80211_get_hw_conf(priv->hw);
3524
3525 if (!exit_pending)
3526 set_bit(STATUS_EXIT_PENDING, &priv->status);
3527
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07003528 iwl_leds_unregister(priv);
3529
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07003530 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
3531
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003532 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003533
3534 /* Unblock any waiting calls */
3535 wake_up_interruptible_all(&priv->wait_command_queue);
3536
Zhu Yib481de92007-09-25 17:54:57 -07003537 /* Wipe out the EXIT_PENDING status bit if we are not actually
3538 * exiting the module */
3539 if (!exit_pending)
3540 clear_bit(STATUS_EXIT_PENDING, &priv->status);
3541
3542 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003543 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07003544
3545 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003546 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003547 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003548 spin_unlock_irqrestore(&priv->lock, flags);
3549 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003550
3551 if (priv->mac80211_registered)
3552 ieee80211_stop_queues(priv->hw);
3553
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003554 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07003555 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07003556 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003557 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
3558 STATUS_RF_KILL_HW |
3559 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
3560 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08003561 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
3562 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07003563 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
3564 STATUS_IN_SUSPEND;
3565 goto exit;
3566 }
3567
3568 /* ...otherwise clear out all the status bits but the RF Kill and
3569 * SUSPEND bits and continue taking the NIC down. */
3570 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
3571 STATUS_RF_KILL_HW |
3572 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
3573 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08003574 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
3575 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07003576 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
3577 STATUS_IN_SUSPEND |
3578 test_bit(STATUS_FW_ERROR, &priv->status) <<
3579 STATUS_FW_ERROR;
3580
3581 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003582 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08003583 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07003584 spin_unlock_irqrestore(&priv->lock, flags);
3585
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003586 iwl4965_hw_txq_ctx_stop(priv);
3587 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003588
3589 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003590 if (!iwl_grab_nic_access(priv)) {
3591 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07003592 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003593 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003594 }
3595 spin_unlock_irqrestore(&priv->lock, flags);
3596
3597 udelay(5);
3598
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003599 iwl4965_hw_nic_stop_master(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003600 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003601 iwl4965_hw_nic_reset(priv);
Ron Rindjunsky399f4902008-04-23 17:14:56 -07003602 priv->cfg->ops->lib->free_shared_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003603
3604 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003605 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003606
3607 if (priv->ibss_beacon)
3608 dev_kfree_skb(priv->ibss_beacon);
3609 priv->ibss_beacon = NULL;
3610
3611 /* clear out any free frames */
Tomas Winklerfcab4232008-05-15 13:54:01 +08003612 iwl_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003613}
3614
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003615static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003616{
3617 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003618 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003619 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08003620
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003621 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003622}
3623
3624#define MAX_HW_RESTARTS 5
3625
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003626static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003627{
Tomas Winkler57aab752008-04-14 21:16:03 -07003628 int i;
3629 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003630
3631 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3632 IWL_WARNING("Exit pending; will not bring the NIC up\n");
3633 return -EIO;
3634 }
3635
3636 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
3637 IWL_WARNING("Radio disabled by SW RF kill (module "
3638 "parameter)\n");
Mohamed Abbasad97edd2008-03-28 16:21:06 -07003639 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08003640 return -ENODEV;
3641 }
3642
Reinette Chatree903fbd2008-01-30 22:05:15 -08003643 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
3644 IWL_ERROR("ucode not available for device bringup\n");
3645 return -EIO;
3646 }
3647
Zhu Yie655b9f2008-01-24 02:19:38 -08003648 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003649 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08003650 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3651 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3652 else {
3653 set_bit(STATUS_RF_KILL_HW, &priv->status);
3654 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07003655 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08003656 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
3657 return -ENODEV;
3658 }
Zhu Yib481de92007-09-25 17:54:57 -07003659 }
3660
Mohamed Abbasad97edd2008-03-28 16:21:06 -07003661 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003662 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07003663
Ron Rindjunsky399f4902008-04-23 17:14:56 -07003664 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
3665 if (ret) {
3666 IWL_ERROR("Unable to allocate shared memory\n");
3667 return ret;
3668 }
3669
Ron Rindjunsky1053d352008-05-05 10:22:43 +08003670 ret = iwl_hw_nic_init(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07003671 if (ret) {
3672 IWL_ERROR("Unable to init nic\n");
3673 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003674 }
3675
3676 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003677 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3678 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003679 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3680
3681 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003682 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003683 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003684
3685 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003686 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3687 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07003688
3689 /* Copy original ucode data image from disk into backup cache.
3690 * This will be used to initialize the on-board processor's
3691 * data SRAM for a clean start when the runtime program first loads. */
3692 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08003693 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07003694
Zhu Yie655b9f2008-01-24 02:19:38 -08003695 /* We return success when we resume from suspend and rf_kill is on. */
3696 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003697 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003698
3699 for (i = 0; i < MAX_HW_RESTARTS; i++) {
3700
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003701 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003702
3703 /* load bootstrap state machine,
3704 * load bootstrap program into processor's memory,
3705 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07003706 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003707
Tomas Winkler57aab752008-04-14 21:16:03 -07003708 if (ret) {
3709 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07003710 continue;
3711 }
3712
3713 /* start card; "initialize" will load runtime ucode */
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08003714 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003715
Zhu Yib481de92007-09-25 17:54:57 -07003716 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
3717
3718 return 0;
3719 }
3720
3721 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003722 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003723
3724 /* tried to restart and config the device for as long as our
3725 * patience could withstand */
3726 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
3727 return -EIO;
3728}
3729
3730
3731/*****************************************************************************
3732 *
3733 * Workqueue callbacks
3734 *
3735 *****************************************************************************/
3736
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003737static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003738{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003739 struct iwl_priv *priv =
3740 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07003741
3742 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3743 return;
3744
3745 mutex_lock(&priv->mutex);
Emmanuel Grumbachf3ccc082008-05-05 10:22:45 +08003746 priv->cfg->ops->lib->init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003747 mutex_unlock(&priv->mutex);
3748}
3749
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003750static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003751{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003752 struct iwl_priv *priv =
3753 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07003754
3755 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3756 return;
3757
3758 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003759 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003760 mutex_unlock(&priv->mutex);
3761}
3762
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003763static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003764{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003765 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07003766
3767 wake_up_interruptible(&priv->wait_command_queue);
3768
3769 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3770 return;
3771
3772 mutex_lock(&priv->mutex);
3773
Tomas Winklerfee12472008-04-03 16:05:21 -07003774 if (!iwl_is_rfkill(priv)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08003775 IWL_DEBUG(IWL_DL_RF_KILL,
Zhu Yib481de92007-09-25 17:54:57 -07003776 "HW and/or SW RF Kill no longer active, restarting "
3777 "device\n");
3778 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
3779 queue_work(priv->workqueue, &priv->restart);
3780 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07003781 /* make sure mac80211 stop sending Tx frame */
3782 if (priv->mac80211_registered)
3783 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07003784
3785 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
3786 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3787 "disabled by SW switch\n");
3788 else
3789 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
3790 "Kill switch must be turned off for "
3791 "wireless networking to work.\n");
3792 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07003793 iwl_rfkill_set_hw_state(priv);
3794
Zhu Yib481de92007-09-25 17:54:57 -07003795 mutex_unlock(&priv->mutex);
3796}
3797
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08003798static void iwl4965_bg_set_monitor(struct work_struct *work)
3799{
3800 struct iwl_priv *priv = container_of(work,
3801 struct iwl_priv, set_monitor);
3802
3803 IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
3804
3805 mutex_lock(&priv->mutex);
3806
3807 if (!iwl_is_ready(priv))
3808 IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
3809 else
3810 if (iwl4965_set_mode(priv, IEEE80211_IF_TYPE_MNTR) != 0)
3811 IWL_ERROR("iwl4965_set_mode() failed\n");
3812
3813 mutex_unlock(&priv->mutex);
3814}
3815
Zhu Yib481de92007-09-25 17:54:57 -07003816#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
3817
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003818static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003819{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003820 struct iwl_priv *priv =
3821 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07003822
3823 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3824 return;
3825
3826 mutex_lock(&priv->mutex);
3827 if (test_bit(STATUS_SCANNING, &priv->status) ||
3828 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08003829 IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
3830 "adapter (%dms)\n",
3831 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08003832
Zhu Yib481de92007-09-25 17:54:57 -07003833 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003834 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003835 }
3836 mutex_unlock(&priv->mutex);
3837}
3838
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003839static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003840{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003841 struct iwl_priv *priv =
3842 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07003843 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003844 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003845 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07003846 .meta.flags = CMD_SIZE_HUGE,
3847 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003848 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07003849 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02003850 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01003851 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02003852 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07003853 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003854
3855 conf = ieee80211_get_hw_conf(priv->hw);
3856
3857 mutex_lock(&priv->mutex);
3858
Tomas Winklerfee12472008-04-03 16:05:21 -07003859 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003860 IWL_WARNING("request scan called when driver not ready.\n");
3861 goto done;
3862 }
3863
3864 /* Make sure the scan wasn't cancelled before this queued work
3865 * was given the chance to run... */
3866 if (!test_bit(STATUS_SCANNING, &priv->status))
3867 goto done;
3868
3869 /* This should never be called or scheduled if there is currently
3870 * a scan active in the hardware. */
3871 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
3872 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
3873 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07003874 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07003875 goto done;
3876 }
3877
3878 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3879 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
3880 goto done;
3881 }
3882
3883 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3884 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
3885 goto done;
3886 }
3887
Tomas Winklerfee12472008-04-03 16:05:21 -07003888 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003889 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
3890 goto done;
3891 }
3892
3893 if (!test_bit(STATUS_READY, &priv->status)) {
3894 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
3895 goto done;
3896 }
3897
3898 if (!priv->scan_bands) {
3899 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
3900 goto done;
3901 }
3902
3903 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003904 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07003905 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
3906 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07003907 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07003908 goto done;
3909 }
3910 }
3911 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003912 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07003913
3914 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
3915 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
3916
Tomas Winkler3109ece2008-03-28 16:33:35 -07003917 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003918 u16 interval = 0;
3919 u32 extra;
3920 u32 suspend_time = 100;
3921 u32 scan_suspend_time = 100;
3922 unsigned long flags;
3923
3924 IWL_DEBUG_INFO("Scanning while associated...\n");
3925
3926 spin_lock_irqsave(&priv->lock, flags);
3927 interval = priv->beacon_int;
3928 spin_unlock_irqrestore(&priv->lock, flags);
3929
3930 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08003931 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07003932 if (!interval)
3933 interval = suspend_time;
3934
3935 extra = (suspend_time / interval) << 22;
3936 scan_suspend_time = (extra |
3937 ((suspend_time % interval) * 1024));
3938 scan->suspend_time = cpu_to_le32(scan_suspend_time);
3939 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
3940 scan_suspend_time, interval);
3941 }
3942
3943 /* We should add the ability for user to lock to PASSIVE ONLY */
3944 if (priv->one_direct_scan) {
3945 IWL_DEBUG_SCAN
3946 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003947 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07003948 priv->direct_ssid_len));
3949 scan->direct_scan[0].id = WLAN_EID_SSID;
3950 scan->direct_scan[0].len = priv->direct_ssid_len;
3951 memcpy(scan->direct_scan[0].ssid,
3952 priv->direct_ssid, priv->direct_ssid_len);
3953 direct_mask = 1;
Tomas Winkler3109ece2008-03-28 16:33:35 -07003954 } else if (!iwl_is_associated(priv) && priv->essid_len) {
Bill Moss786b4552008-04-17 16:03:40 -07003955 IWL_DEBUG_SCAN
3956 ("Kicking off one direct scan for '%s' when not associated\n",
3957 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07003958 scan->direct_scan[0].id = WLAN_EID_SSID;
3959 scan->direct_scan[0].len = priv->essid_len;
3960 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
3961 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07003962 } else {
Bill Moss786b4552008-04-17 16:03:40 -07003963 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003964 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07003965 }
Zhu Yib481de92007-09-25 17:54:57 -07003966
Zhu Yib481de92007-09-25 17:54:57 -07003967 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler5425e492008-04-15 16:01:38 -07003968 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07003969 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
3970
Zhu Yib481de92007-09-25 17:54:57 -07003971
3972 switch (priv->scan_bands) {
3973 case 2:
3974 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
3975 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003976 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07003977 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
3978
3979 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003980 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07003981 break;
3982
3983 case 1:
3984 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003985 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07003986 RATE_MCS_ANT_B_MSK);
3987 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01003988 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07003989 break;
3990
3991 default:
3992 IWL_WARNING("Invalid scan band count\n");
3993 goto done;
3994 }
3995
Tomas Winkler78330fd2008-02-06 02:37:18 +02003996 /* We don't build a direct scan probe request; the uCode will do
3997 * that based on the direct_mask added to each channel entry */
3998 cmd_len = iwl4965_fill_probe_req(priv, band,
3999 (struct ieee80211_mgmt *)scan->data,
4000 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
4001
4002 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07004003 /* select Rx chains */
4004
4005 /* Force use of chains B and C (0x6) for scan Rx.
4006 * Avoid A (0x1) because of its off-channel reception on A-band.
4007 * MIMO is not used here, but value is required to make uCode happy. */
4008 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
4009 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
4010 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
4011 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
4012
4013 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
4014 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
4015
Bill Moss786b4552008-04-17 16:03:40 -07004016 if (direct_mask)
Reinette Chatre26c0f032008-03-11 16:17:15 -07004017 scan->channel_count =
4018 iwl4965_get_channels_for_scan(
4019 priv, band, 1, /* active */
4020 direct_mask,
4021 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Bill Moss786b4552008-04-17 16:03:40 -07004022 else
Reinette Chatre26c0f032008-03-11 16:17:15 -07004023 scan->channel_count =
4024 iwl4965_get_channels_for_scan(
4025 priv, band, 0, /* passive */
4026 direct_mask,
4027 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07004028
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004029 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
4030 RXON_FILTER_BCON_AWARE_MSK);
Zhu Yib481de92007-09-25 17:54:57 -07004031 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004032 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07004033 cmd.data = scan;
4034 scan->len = cpu_to_le16(cmd.len);
4035
4036 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07004037 ret = iwl_send_cmd_sync(priv, &cmd);
4038 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07004039 goto done;
4040
4041 queue_delayed_work(priv->workqueue, &priv->scan_check,
4042 IWL_SCAN_CHECK_WATCHDOG);
4043
4044 mutex_unlock(&priv->mutex);
4045 return;
4046
4047 done:
Ian Schram01ebd062007-10-25 17:15:22 +08004048 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07004049 queue_work(priv->workqueue, &priv->scan_completed);
4050 mutex_unlock(&priv->mutex);
4051}
4052
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004053static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004054{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004055 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07004056
4057 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4058 return;
4059
4060 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004061 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004062 mutex_unlock(&priv->mutex);
4063}
4064
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004065static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004066{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004067 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07004068
4069 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4070 return;
4071
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004072 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004073 queue_work(priv->workqueue, &priv->up);
4074}
4075
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004076static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004077{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004078 struct iwl_priv *priv =
4079 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07004080
4081 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4082 return;
4083
4084 mutex_lock(&priv->mutex);
Tomas Winklera55360e2008-05-05 10:22:28 +08004085 iwl_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004086 mutex_unlock(&priv->mutex);
4087}
4088
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004089#define IWL_DELAY_NEXT_SCAN (HZ*2)
4090
Reinette Chatre508e32e2008-04-14 21:16:13 -07004091static void iwl4965_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004092{
Zhu Yib481de92007-09-25 17:54:57 -07004093 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07004094 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07004095 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07004096
4097 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
4098 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
4099 return;
4100 }
4101
Joe Perches0795af52007-10-03 17:59:30 -07004102 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
4103 priv->assoc_id,
4104 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004105
4106
4107 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4108 return;
4109
Zhu Yib481de92007-09-25 17:54:57 -07004110
Reinette Chatre508e32e2008-04-14 21:16:13 -07004111 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08004112 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07004113
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004114 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08004115
Zhu Yib481de92007-09-25 17:54:57 -07004116 conf = ieee80211_get_hw_conf(priv->hw);
4117
4118 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004119 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004120
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004121 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
4122 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07004123 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07004124 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07004125 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07004126 IWL_WARNING("REPLY_RXON_TIMING failed - "
4127 "Attempting to continue.\n");
4128
4129 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
4130
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004131#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02004132 if (priv->current_ht_config.is_ht)
Tomas Winkler47c51962008-05-05 10:22:41 +08004133 iwl_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004134#endif /* CONFIG_IWL4965_HT*/
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07004135 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004136 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
4137
4138 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
4139 priv->assoc_id, priv->beacon_int);
4140
4141 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
4142 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
4143 else
4144 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
4145
4146 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
4147 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
4148 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
4149 else
4150 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
4151
4152 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4153 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
4154
4155 }
4156
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004157 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004158
4159 switch (priv->iw_mode) {
4160 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004161 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07004162 break;
4163
4164 case IEEE80211_IF_TYPE_IBSS:
4165
4166 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004167 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004168
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08004169 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
4170 iwl_rxon_add_station(priv, priv->bssid, 0);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004171 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
4172 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004173
4174 break;
4175
4176 default:
4177 IWL_ERROR("%s Should not be called in %d mode\n",
4178 __FUNCTION__, priv->iw_mode);
4179 break;
4180 }
4181
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004182 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004183
Zhu Yib481de92007-09-25 17:54:57 -07004184 /* Enable Rx differential gain and sensitivity calibrations */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07004185 iwl_chain_noise_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004186 priv->start_calib = 1;
Zhu Yib481de92007-09-25 17:54:57 -07004187
4188 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4189 priv->assoc_station_added = 1;
4190
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004191 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08004192
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004193 iwl_power_update_mode(priv, 0);
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004194 /* we have just associated, don't start scan too early */
4195 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Reinette Chatre508e32e2008-04-14 21:16:13 -07004196}
4197
4198
4199static void iwl4965_bg_post_associate(struct work_struct *data)
4200{
4201 struct iwl_priv *priv = container_of(data, struct iwl_priv,
4202 post_associate.work);
4203
4204 mutex_lock(&priv->mutex);
4205 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004206 mutex_unlock(&priv->mutex);
Reinette Chatre508e32e2008-04-14 21:16:13 -07004207
Zhu Yib481de92007-09-25 17:54:57 -07004208}
4209
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004210static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07004211{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004212 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07004213
Tomas Winklerfee12472008-04-03 16:05:21 -07004214 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004215 return;
4216
4217 mutex_lock(&priv->mutex);
4218
4219 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004220 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004221
4222 mutex_unlock(&priv->mutex);
4223}
4224
Zhu Yi76bb77e2007-11-22 10:53:22 +08004225static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
4226
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004227static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07004228{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004229 struct iwl_priv *priv =
4230 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07004231
Ester Kummerf3d67992008-05-06 11:05:12 +08004232 IWL_DEBUG(IWL_DL_SCAN, "SCAN complete scan\n");
Zhu Yib481de92007-09-25 17:54:57 -07004233
4234 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4235 return;
4236
Zhu Yia0646472007-12-20 14:10:01 +08004237 if (test_bit(STATUS_CONF_PENDING, &priv->status))
4238 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08004239
Zhu Yib481de92007-09-25 17:54:57 -07004240 ieee80211_scan_completed(priv->hw);
4241
4242 /* Since setting the TXPOWER may have been deferred while
4243 * performing the scan, fire one off */
4244 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004245 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004246 mutex_unlock(&priv->mutex);
4247}
4248
4249/*****************************************************************************
4250 *
4251 * mac80211 entry point functions
4252 *
4253 *****************************************************************************/
4254
Zhu Yi5a66926a2008-01-14 17:46:18 -08004255#define UCODE_READY_TIMEOUT (2 * HZ)
4256
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004257static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07004258{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004259 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08004260 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07004261
4262 IWL_DEBUG_MAC80211("enter\n");
4263
Zhu Yi5a66926a2008-01-14 17:46:18 -08004264 if (pci_enable_device(priv->pci_dev)) {
4265 IWL_ERROR("Fail to pci_enable_device\n");
4266 return -ENODEV;
4267 }
4268 pci_restore_state(priv->pci_dev);
4269 pci_enable_msi(priv->pci_dev);
4270
4271 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
4272 DRV_NAME, priv);
4273 if (ret) {
4274 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
4275 goto out_disable_msi;
4276 }
4277
Zhu Yib481de92007-09-25 17:54:57 -07004278 /* we should be verifying the device is ready to be opened */
4279 mutex_lock(&priv->mutex);
4280
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08004281 memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
Zhu Yi5a66926a2008-01-14 17:46:18 -08004282 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
4283 * ucode filename and max sizes are card-specific. */
4284
4285 if (!priv->ucode_code.len) {
4286 ret = iwl4965_read_ucode(priv);
4287 if (ret) {
4288 IWL_ERROR("Could not read microcode: %d\n", ret);
4289 mutex_unlock(&priv->mutex);
4290 goto out_release_irq;
4291 }
4292 }
4293
Zhu Yie655b9f2008-01-24 02:19:38 -08004294 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004295
Zhu Yib481de92007-09-25 17:54:57 -07004296 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004297
Zhu Yie655b9f2008-01-24 02:19:38 -08004298 if (ret)
4299 goto out_release_irq;
4300
4301 IWL_DEBUG_INFO("Start UP work done.\n");
4302
4303 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
4304 return 0;
4305
Zhu Yi5a66926a2008-01-14 17:46:18 -08004306 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
4307 * mac80211 will not be run successfully. */
4308 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
4309 test_bit(STATUS_READY, &priv->status),
4310 UCODE_READY_TIMEOUT);
4311 if (!ret) {
4312 if (!test_bit(STATUS_READY, &priv->status)) {
4313 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
4314 jiffies_to_msecs(UCODE_READY_TIMEOUT));
4315 ret = -ETIMEDOUT;
4316 goto out_release_irq;
4317 }
4318 }
4319
Zhu Yie655b9f2008-01-24 02:19:38 -08004320 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07004321 IWL_DEBUG_MAC80211("leave\n");
4322 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08004323
4324out_release_irq:
4325 free_irq(priv->pci_dev->irq, priv);
4326out_disable_msi:
4327 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08004328 pci_disable_device(priv->pci_dev);
4329 priv->is_open = 0;
4330 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08004331 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004332}
4333
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004334static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07004335{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004336 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004337
4338 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08004339
Zhu Yie655b9f2008-01-24 02:19:38 -08004340 if (!priv->is_open) {
4341 IWL_DEBUG_MAC80211("leave - skip\n");
4342 return;
4343 }
4344
Zhu Yib481de92007-09-25 17:54:57 -07004345 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08004346
Tomas Winklerfee12472008-04-03 16:05:21 -07004347 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08004348 /* stop mac, cancel any scan request and clear
4349 * RXON_FILTER_ASSOC_MSK BIT
4350 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08004351 mutex_lock(&priv->mutex);
4352 iwl4965_scan_cancel_timeout(priv, 100);
4353 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08004354 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08004355 }
4356
Zhu Yi5a66926a2008-01-14 17:46:18 -08004357 iwl4965_down(priv);
4358
4359 flush_workqueue(priv->workqueue);
4360 free_irq(priv->pci_dev->irq, priv);
4361 pci_disable_msi(priv->pci_dev);
4362 pci_save_state(priv->pci_dev);
4363 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08004364
Zhu Yib481de92007-09-25 17:54:57 -07004365 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07004366}
4367
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004368static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07004369 struct ieee80211_tx_control *ctl)
4370{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004371 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004372
4373 IWL_DEBUG_MAC80211("enter\n");
4374
4375 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
4376 IWL_DEBUG_MAC80211("leave - monitor\n");
4377 return -1;
4378 }
4379
4380 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01004381 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07004382
Tomas Winklerfd4abac2008-05-15 13:54:07 +08004383 if (iwl_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07004384 dev_kfree_skb_any(skb);
4385
4386 IWL_DEBUG_MAC80211("leave\n");
4387 return 0;
4388}
4389
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004390static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07004391 struct ieee80211_if_init_conf *conf)
4392{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004393 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004394 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07004395 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07004396
Johannes Berg32bfd352007-12-19 01:31:26 +01004397 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07004398
Johannes Berg32bfd352007-12-19 01:31:26 +01004399 if (priv->vif) {
4400 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08004401 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07004402 }
4403
4404 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01004405 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07004406
4407 spin_unlock_irqrestore(&priv->lock, flags);
4408
4409 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02004410
4411 if (conf->mac_addr) {
4412 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
4413 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
4414 }
Zhu Yib481de92007-09-25 17:54:57 -07004415
Tomas Winklerfee12472008-04-03 16:05:21 -07004416 if (iwl_is_ready(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08004417 iwl4965_set_mode(priv, conf->type);
4418
Zhu Yib481de92007-09-25 17:54:57 -07004419 mutex_unlock(&priv->mutex);
4420
Zhu Yi5a66926a2008-01-14 17:46:18 -08004421 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07004422 return 0;
4423}
4424
4425/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004426 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07004427 *
4428 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
4429 * be set inappropriately and the driver currently sets the hardware up to
4430 * use it whenever needed.
4431 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004432static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07004433{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004434 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004435 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004436 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08004437 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004438
4439 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01004440 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07004441
Zhu Yi12342c42007-12-20 11:27:32 +08004442 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
4443
Tomas Winklerfee12472008-04-03 16:05:21 -07004444 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004445 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08004446 ret = -EIO;
4447 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004448 }
4449
Assaf Krauss1ea87392008-03-18 14:57:50 -07004450 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07004451 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08004452 IWL_DEBUG_MAC80211("leave - scanning\n");
4453 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004454 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08004455 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07004456 }
4457
4458 spin_lock_irqsave(&priv->lock, flags);
4459
Assaf Krauss8622e702008-03-21 13:53:43 -07004460 ch_info = iwl_get_channel_info(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01004461 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07004462 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07004463 IWL_DEBUG_MAC80211("leave - invalid channel\n");
4464 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08004465 ret = -EINVAL;
4466 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004467 }
4468
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004469#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02004470 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07004471 * from any ht related info since 2.4 does not
4472 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02004473 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07004474#ifdef IEEE80211_CONF_CHANNEL_SWITCH
4475 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
4476#endif
4477 )
4478 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004479#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07004480
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07004481 iwl_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01004482 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07004483
Johannes Berg8318d782008-01-24 19:38:38 +01004484 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07004485
4486 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01004487 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07004488 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004489 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004490
4491 spin_unlock_irqrestore(&priv->lock, flags);
4492
4493#ifdef IEEE80211_CONF_CHANNEL_SWITCH
4494 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004495 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08004496 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004497 }
4498#endif
4499
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004500 if (priv->cfg->ops->lib->radio_kill_sw)
4501 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07004502
4503 if (!conf->radio_enabled) {
4504 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08004505 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004506 }
4507
Tomas Winklerfee12472008-04-03 16:05:21 -07004508 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004509 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08004510 ret = -EIO;
4511 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004512 }
4513
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004514 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004515
4516 if (memcmp(&priv->active_rxon,
4517 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004518 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004519 else
4520 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
4521
4522 IWL_DEBUG_MAC80211("leave\n");
4523
Zhu Yia0646472007-12-20 14:10:01 +08004524out:
4525 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004526 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08004527 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004528}
4529
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004530static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004531{
Tomas Winkler857485c2008-03-21 13:53:44 -07004532 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004533
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08004534 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07004535 return;
4536
4537 /* The following should be done only at AP bring up */
4538 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
4539
4540 /* RXON - unassoc (to set timing command) */
4541 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004542 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004543
4544 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004545 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
4546 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07004547 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07004548 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07004549 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07004550 IWL_WARNING("REPLY_RXON_TIMING failed - "
4551 "Attempting to continue.\n");
4552
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07004553 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004554
4555 /* FIXME: what should be the assoc_id for AP? */
4556 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
4557 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
4558 priv->staging_rxon.flags |=
4559 RXON_FLG_SHORT_PREAMBLE_MSK;
4560 else
4561 priv->staging_rxon.flags &=
4562 ~RXON_FLG_SHORT_PREAMBLE_MSK;
4563
4564 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
4565 if (priv->assoc_capability &
4566 WLAN_CAPABILITY_SHORT_SLOT_TIME)
4567 priv->staging_rxon.flags |=
4568 RXON_FLG_SHORT_SLOT_MSK;
4569 else
4570 priv->staging_rxon.flags &=
4571 ~RXON_FLG_SHORT_SLOT_MSK;
4572
4573 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4574 priv->staging_rxon.flags &=
4575 ~RXON_FLG_SHORT_SLOT_MSK;
4576 }
4577 /* restore RXON assoc */
4578 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004579 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004580 iwl4965_activate_qos(priv, 1);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08004581 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08004582 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004583 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004584
4585 /* FIXME - we need to add code here to detect a totally new
4586 * configuration, reset the AP, unassoc, rxon timing, assoc,
4587 * clear sta table, add BCAST sta... */
4588}
4589
Johannes Berg32bfd352007-12-19 01:31:26 +01004590static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
4591 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07004592 struct ieee80211_if_conf *conf)
4593{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004594 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07004595 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07004596 unsigned long flags;
4597 int rc;
4598
4599 if (conf == NULL)
4600 return -EIO;
4601
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08004602 if (priv->vif != vif) {
4603 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08004604 return 0;
4605 }
4606
Zhu Yib481de92007-09-25 17:54:57 -07004607 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
4608 (!conf->beacon || !conf->ssid_len)) {
4609 IWL_DEBUG_MAC80211
4610 ("Leaving in AP mode because HostAPD is not ready.\n");
4611 return 0;
4612 }
4613
Tomas Winklerfee12472008-04-03 16:05:21 -07004614 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08004615 return -EAGAIN;
4616
Zhu Yib481de92007-09-25 17:54:57 -07004617 mutex_lock(&priv->mutex);
4618
Zhu Yib481de92007-09-25 17:54:57 -07004619 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07004620 IWL_DEBUG_MAC80211("bssid: %s\n",
4621 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07004622
Johannes Berg4150c572007-09-17 01:29:23 -04004623/*
4624 * very dubious code was here; the probe filtering flag is never set:
4625 *
Zhu Yib481de92007-09-25 17:54:57 -07004626 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
4627 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04004628 */
Zhu Yib481de92007-09-25 17:54:57 -07004629
4630 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
4631 if (!conf->bssid) {
4632 conf->bssid = priv->mac_addr;
4633 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07004634 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
4635 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07004636 }
4637 if (priv->ibss_beacon)
4638 dev_kfree_skb(priv->ibss_beacon);
4639
4640 priv->ibss_beacon = conf->beacon;
4641 }
4642
Tomas Winklerfee12472008-04-03 16:05:21 -07004643 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08004644 goto done;
4645
Zhu Yib481de92007-09-25 17:54:57 -07004646 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
4647 !is_multicast_ether_addr(conf->bssid)) {
4648 /* If there is currently a HW scan going on in the background
4649 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004650 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07004651 IWL_WARNING("Aborted scan still in progress "
4652 "after 100ms\n");
4653 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
4654 mutex_unlock(&priv->mutex);
4655 return -EAGAIN;
4656 }
4657 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
4658
4659 /* TODO: Audit driver for usage of these members and see
4660 * if mac80211 deprecates them (priv->bssid looks like it
4661 * shouldn't be there, but I haven't scanned the IBSS code
4662 * to verify) - jpk */
4663 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
4664
4665 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004666 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004667 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004668 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004669 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08004670 iwl_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07004671 priv, priv->active_rxon.bssid_addr, 1);
4672 }
4673
4674 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004675 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07004676 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004677 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004678 }
4679
Mohamed Abbasfde35712007-11-29 11:10:15 +08004680 done:
Zhu Yib481de92007-09-25 17:54:57 -07004681 spin_lock_irqsave(&priv->lock, flags);
4682 if (!conf->ssid_len)
4683 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
4684 else
4685 memcpy(priv->essid, conf->ssid, conf->ssid_len);
4686
4687 priv->essid_len = conf->ssid_len;
4688 spin_unlock_irqrestore(&priv->lock, flags);
4689
4690 IWL_DEBUG_MAC80211("leave\n");
4691 mutex_unlock(&priv->mutex);
4692
4693 return 0;
4694}
4695
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004696static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04004697 unsigned int changed_flags,
4698 unsigned int *total_flags,
4699 int mc_count, struct dev_addr_list *mc_list)
4700{
4701 /*
4702 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004703 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04004704 */
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08004705 struct iwl_priv *priv = hw->priv;
4706 int new_flags = 0;
4707 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
4708 if (*total_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
4709 IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
4710 IEEE80211_IF_TYPE_MNTR,
4711 changed_flags, *total_flags);
4712 /* queue work 'cuz mac80211 is holding a lock which
4713 * prevents us from issuing (synchronous) f/w cmds */
4714 queue_work(priv->workqueue, &priv->set_monitor);
4715 new_flags &= FIF_PROMISC_IN_BSS |
4716 FIF_OTHER_BSS |
4717 FIF_ALLMULTI;
4718 }
4719 }
4720 *total_flags = new_flags;
Johannes Berg4150c572007-09-17 01:29:23 -04004721}
4722
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004723static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07004724 struct ieee80211_if_init_conf *conf)
4725{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004726 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004727
4728 IWL_DEBUG_MAC80211("enter\n");
4729
4730 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08004731
Tomas Winklerfee12472008-04-03 16:05:21 -07004732 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08004733 iwl4965_scan_cancel_timeout(priv, 100);
4734 cancel_delayed_work(&priv->post_associate);
4735 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4736 iwl4965_commit_rxon(priv);
4737 }
Johannes Berg32bfd352007-12-19 01:31:26 +01004738 if (priv->vif == conf->vif) {
4739 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07004740 memset(priv->bssid, 0, ETH_ALEN);
4741 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
4742 priv->essid_len = 0;
4743 }
4744 mutex_unlock(&priv->mutex);
4745
4746 IWL_DEBUG_MAC80211("leave\n");
4747
4748}
Johannes Berg471b3ef2007-12-28 14:32:58 +01004749
Tomas Winkler3109ece2008-03-28 16:33:35 -07004750#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01004751static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
4752 struct ieee80211_vif *vif,
4753 struct ieee80211_bss_conf *bss_conf,
4754 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02004755{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004756 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02004757
Tomas Winkler3109ece2008-03-28 16:33:35 -07004758 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
4759
Johannes Berg471b3ef2007-12-28 14:32:58 +01004760 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07004761 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
4762 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01004763 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02004764 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
4765 else
4766 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
4767 }
4768
Johannes Berg471b3ef2007-12-28 14:32:58 +01004769 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07004770 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01004771 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02004772 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
4773 else
4774 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
4775 }
4776
Tomas Winkler98952d52008-03-28 16:33:33 -07004777 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07004778 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07004779 iwl4965_ht_conf(priv, bss_conf);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07004780 iwl_set_rxon_chain(priv);
Tomas Winkler98952d52008-03-28 16:33:33 -07004781 }
4782
Johannes Berg471b3ef2007-12-28 14:32:58 +01004783 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07004784 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
Reinette Chatre508e32e2008-04-14 21:16:13 -07004785 /* This should never happen as this function should
4786 * never be called from interrupt context. */
4787 if (WARN_ON_ONCE(in_interrupt()))
4788 return;
Tomas Winkler3109ece2008-03-28 16:33:35 -07004789 if (bss_conf->assoc) {
4790 priv->assoc_id = bss_conf->aid;
4791 priv->beacon_int = bss_conf->beacon_int;
4792 priv->timestamp = bss_conf->timestamp;
4793 priv->assoc_capability = bss_conf->assoc_capability;
4794 priv->next_scan_jiffies = jiffies +
4795 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
Reinette Chatre508e32e2008-04-14 21:16:13 -07004796 mutex_lock(&priv->mutex);
4797 iwl4965_post_associate(priv);
4798 mutex_unlock(&priv->mutex);
Tomas Winkler3109ece2008-03-28 16:33:35 -07004799 } else {
4800 priv->assoc_id = 0;
4801 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
4802 }
4803 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
4804 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
Tomas Winkler7e8c5192008-04-15 16:01:43 -07004805 iwl_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01004806 }
4807
Tomas Winkler220173b2007-10-16 00:50:25 +02004808}
Zhu Yib481de92007-09-25 17:54:57 -07004809
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004810static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07004811{
4812 int rc = 0;
4813 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004814 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004815
4816 IWL_DEBUG_MAC80211("enter\n");
4817
mabbas052c4b92007-10-25 17:15:43 +08004818 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07004819 spin_lock_irqsave(&priv->lock, flags);
4820
Tomas Winklerfee12472008-04-03 16:05:21 -07004821 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004822 rc = -EIO;
4823 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
4824 goto out_unlock;
4825 }
4826
4827 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
4828 rc = -EIO;
4829 IWL_ERROR("ERROR: APs don't scan\n");
4830 goto out_unlock;
4831 }
4832
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004833 /* we don't schedule scan within next_scan_jiffies period */
4834 if (priv->next_scan_jiffies &&
4835 time_after(priv->next_scan_jiffies, jiffies)) {
4836 rc = -EAGAIN;
4837 goto out_unlock;
4838 }
Zhu Yib481de92007-09-25 17:54:57 -07004839 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004840 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
4841 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07004842 rc = -EAGAIN;
4843 goto out_unlock;
4844 }
4845 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004846 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004847 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07004848
4849 priv->one_direct_scan = 1;
4850 priv->direct_ssid_len = (u8)
4851 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
4852 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08004853 } else
4854 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004855
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004856 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004857
4858 IWL_DEBUG_MAC80211("leave\n");
4859
4860out_unlock:
4861 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08004862 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07004863
4864 return rc;
4865}
4866
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004867static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
4868 struct ieee80211_key_conf *keyconf, const u8 *addr,
4869 u32 iv32, u16 *phase1key)
4870{
4871 struct iwl_priv *priv = hw->priv;
4872 u8 sta_id = IWL_INVALID_STATION;
4873 unsigned long flags;
4874 __le16 key_flags = 0;
4875 int i;
4876 DECLARE_MAC_BUF(mac);
4877
4878 IWL_DEBUG_MAC80211("enter\n");
4879
Tomas Winkler947b13a2008-04-16 16:34:48 -07004880 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004881 if (sta_id == IWL_INVALID_STATION) {
4882 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
4883 print_mac(mac, addr));
4884 return;
4885 }
4886
4887 iwl4965_scan_cancel_timeout(priv, 100);
4888
4889 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
4890 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
4891 key_flags &= ~STA_KEY_FLG_INVALID;
4892
Tomas Winkler5425e492008-04-15 16:01:38 -07004893 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004894 key_flags |= STA_KEY_MULTICAST_MSK;
4895
4896 spin_lock_irqsave(&priv->sta_lock, flags);
4897
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004898 priv->stations[sta_id].sta.key.key_flags = key_flags;
4899 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
4900
4901 for (i = 0; i < 5; i++)
4902 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
4903 cpu_to_le16(phase1key[i]);
4904
4905 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
4906 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
4907
Tomas Winkler133636d2008-05-05 10:22:34 +08004908 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02004909
4910 spin_unlock_irqrestore(&priv->sta_lock, flags);
4911
4912 IWL_DEBUG_MAC80211("leave\n");
4913}
4914
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004915static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07004916 const u8 *local_addr, const u8 *addr,
4917 struct ieee80211_key_conf *key)
4918{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004919 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07004920 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004921 int ret = 0;
4922 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004923 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004924
4925 IWL_DEBUG_MAC80211("enter\n");
4926
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004927 if (priv->hw_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07004928 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
4929 return -EOPNOTSUPP;
4930 }
4931
4932 if (is_zero_ether_addr(addr))
4933 /* only support pairwise keys */
4934 return -EOPNOTSUPP;
4935
Tomas Winkler947b13a2008-04-16 16:34:48 -07004936 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004937 if (sta_id == IWL_INVALID_STATION) {
4938 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
4939 print_mac(mac, addr));
4940 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004941
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004942 }
Zhu Yib481de92007-09-25 17:54:57 -07004943
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004944 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004945 iwl4965_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004946 mutex_unlock(&priv->mutex);
4947
4948 /* If we are getting WEP group key and we didn't receive any key mapping
4949 * so far, we are in legacy wep mode (group key only), otherwise we are
4950 * in 1X mode.
4951 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07004952 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004953 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
4954 if (cmd == SET_KEY)
4955 is_default_wep_key = !priv->key_mapping_key;
4956 else
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08004957 is_default_wep_key =
4958 (key->hw_key_idx == HW_KEY_DEFAULT);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004959 }
mabbas052c4b92007-10-25 17:15:43 +08004960
Zhu Yib481de92007-09-25 17:54:57 -07004961 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004962 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004963 if (is_default_wep_key)
4964 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004965 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07004966 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004967
4968 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07004969 break;
4970 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004971 if (is_default_wep_key)
4972 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004973 else
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07004974 ret = iwl_remove_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004975
4976 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07004977 break;
4978 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004979 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004980 }
4981
4982 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07004983
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07004984 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004985}
4986
Johannes Berge100bb62008-04-30 18:51:21 +02004987static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07004988 const struct ieee80211_tx_queue_params *params)
4989{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004990 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004991 unsigned long flags;
4992 int q;
Zhu Yib481de92007-09-25 17:54:57 -07004993
4994 IWL_DEBUG_MAC80211("enter\n");
4995
Tomas Winklerfee12472008-04-03 16:05:21 -07004996 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004997 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4998 return -EIO;
4999 }
5000
5001 if (queue >= AC_NUM) {
5002 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
5003 return 0;
5004 }
5005
Zhu Yib481de92007-09-25 17:54:57 -07005006 if (!priv->qos_data.qos_enable) {
5007 priv->qos_data.qos_active = 0;
5008 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
5009 return 0;
5010 }
5011 q = AC_NUM - 1 - queue;
5012
5013 spin_lock_irqsave(&priv->lock, flags);
5014
5015 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
5016 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
5017 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
5018 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01005019 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07005020
5021 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
5022 priv->qos_data.qos_active = 1;
5023
5024 spin_unlock_irqrestore(&priv->lock, flags);
5025
5026 mutex_lock(&priv->mutex);
5027 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005028 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07005029 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005030 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005031
5032 mutex_unlock(&priv->mutex);
5033
Zhu Yib481de92007-09-25 17:54:57 -07005034 IWL_DEBUG_MAC80211("leave\n");
5035 return 0;
5036}
5037
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005038static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005039 struct ieee80211_tx_queue_stats *stats)
5040{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005041 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005042 int i, avail;
Ron Rindjunsky16466902008-05-05 10:22:50 +08005043 struct iwl_tx_queue *txq;
Tomas Winkler443cfd42008-05-15 13:53:57 +08005044 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07005045 unsigned long flags;
5046
5047 IWL_DEBUG_MAC80211("enter\n");
5048
Tomas Winklerfee12472008-04-03 16:05:21 -07005049 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005050 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5051 return -EIO;
5052 }
5053
5054 spin_lock_irqsave(&priv->lock, flags);
5055
5056 for (i = 0; i < AC_NUM; i++) {
5057 txq = &priv->txq[i];
5058 q = &txq->q;
Tomas Winkler443cfd42008-05-15 13:53:57 +08005059 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07005060
Johannes Berg57ffc582008-04-29 17:18:59 +02005061 stats[i].len = q->n_window - avail;
5062 stats[i].limit = q->n_window - q->high_mark;
5063 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07005064
5065 }
5066 spin_unlock_irqrestore(&priv->lock, flags);
5067
5068 IWL_DEBUG_MAC80211("leave\n");
5069
5070 return 0;
5071}
5072
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005073static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005074 struct ieee80211_low_level_stats *stats)
5075{
Ester Kummerbf403db2008-05-05 10:22:40 +08005076 struct iwl_priv *priv = hw->priv;
5077
5078 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005079 IWL_DEBUG_MAC80211("enter\n");
5080 IWL_DEBUG_MAC80211("leave\n");
5081
5082 return 0;
5083}
5084
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005085static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005086{
Ester Kummerbf403db2008-05-05 10:22:40 +08005087 struct iwl_priv *priv;
5088
5089 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005090 IWL_DEBUG_MAC80211("enter\n");
5091 IWL_DEBUG_MAC80211("leave\n");
5092
5093 return 0;
5094}
5095
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005096static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005097{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005098 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005099 unsigned long flags;
5100
5101 mutex_lock(&priv->mutex);
5102 IWL_DEBUG_MAC80211("enter\n");
5103
5104 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005105#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07005106 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02005107 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07005108 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005109#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07005110
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005111 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005112
5113 cancel_delayed_work(&priv->post_associate);
5114
5115 spin_lock_irqsave(&priv->lock, flags);
5116 priv->assoc_id = 0;
5117 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005118 priv->assoc_station_added = 0;
5119
5120 /* new association get rid of ibss beacon skb */
5121 if (priv->ibss_beacon)
5122 dev_kfree_skb(priv->ibss_beacon);
5123
5124 priv->ibss_beacon = NULL;
5125
5126 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07005127 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005128 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
5129 priv->beacon_int = 0;
5130
5131 spin_unlock_irqrestore(&priv->lock, flags);
5132
Tomas Winklerfee12472008-04-03 16:05:21 -07005133 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08005134 IWL_DEBUG_MAC80211("leave - not ready\n");
5135 mutex_unlock(&priv->mutex);
5136 return;
5137 }
5138
mabbas052c4b92007-10-25 17:15:43 +08005139 /* we are restarting association process
5140 * clear RXON_FILTER_ASSOC_MSK bit
5141 */
5142 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005143 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08005144 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005145 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08005146 }
5147
Mohamed Abbas5da4b552008-04-21 15:41:51 -07005148 iwl_power_update_mode(priv, 0);
5149
Zhu Yib481de92007-09-25 17:54:57 -07005150 /* Per mac80211.h: This is only used in IBSS mode... */
5151 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08005152
Zhu Yib481de92007-09-25 17:54:57 -07005153 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
5154 mutex_unlock(&priv->mutex);
5155 return;
5156 }
5157
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005158 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005159
5160 mutex_unlock(&priv->mutex);
5161
5162 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005163}
5164
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005165static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07005166 struct ieee80211_tx_control *control)
5167{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005168 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005169 unsigned long flags;
5170
5171 mutex_lock(&priv->mutex);
5172 IWL_DEBUG_MAC80211("enter\n");
5173
Tomas Winklerfee12472008-04-03 16:05:21 -07005174 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005175 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5176 mutex_unlock(&priv->mutex);
5177 return -EIO;
5178 }
5179
5180 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
5181 IWL_DEBUG_MAC80211("leave - not IBSS\n");
5182 mutex_unlock(&priv->mutex);
5183 return -EIO;
5184 }
5185
5186 spin_lock_irqsave(&priv->lock, flags);
5187
5188 if (priv->ibss_beacon)
5189 dev_kfree_skb(priv->ibss_beacon);
5190
5191 priv->ibss_beacon = skb;
5192
5193 priv->assoc_id = 0;
5194
5195 IWL_DEBUG_MAC80211("leave\n");
5196 spin_unlock_irqrestore(&priv->lock, flags);
5197
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005198 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005199
5200 queue_work(priv->workqueue, &priv->post_associate.work);
5201
5202 mutex_unlock(&priv->mutex);
5203
5204 return 0;
5205}
5206
Zhu Yib481de92007-09-25 17:54:57 -07005207/*****************************************************************************
5208 *
5209 * sysfs attributes
5210 *
5211 *****************************************************************************/
5212
Tomas Winkler0a6857e2008-03-12 16:58:49 -07005213#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07005214
5215/*
5216 * The following adds a new attribute to the sysfs representation
5217 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
5218 * used for controlling the debug level.
5219 *
5220 * See the level definitions in iwl for details.
5221 */
5222
Ester Kummer8cf769c2008-05-06 11:05:11 +08005223static ssize_t show_debug_level(struct device *d,
5224 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07005225{
Ester Kummer8cf769c2008-05-06 11:05:11 +08005226 struct iwl_priv *priv = d->driver_data;
5227
5228 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07005229}
Ester Kummer8cf769c2008-05-06 11:05:11 +08005230static ssize_t store_debug_level(struct device *d,
5231 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07005232 const char *buf, size_t count)
5233{
Ester Kummer8cf769c2008-05-06 11:05:11 +08005234 struct iwl_priv *priv = d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005235 char *p = (char *)buf;
5236 u32 val;
5237
5238 val = simple_strtoul(p, &p, 0);
5239 if (p == buf)
5240 printk(KERN_INFO DRV_NAME
5241 ": %s is not in hex or decimal form.\n", buf);
5242 else
Ester Kummer8cf769c2008-05-06 11:05:11 +08005243 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07005244
5245 return strnlen(buf, count);
5246}
5247
Ester Kummer8cf769c2008-05-06 11:05:11 +08005248static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
5249 show_debug_level, store_debug_level);
5250
Zhu Yib481de92007-09-25 17:54:57 -07005251
Tomas Winkler0a6857e2008-03-12 16:58:49 -07005252#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07005253
Zhu Yib481de92007-09-25 17:54:57 -07005254
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08005255static ssize_t show_version(struct device *d,
5256 struct device_attribute *attr, char *buf)
5257{
5258 struct iwl_priv *priv = d->driver_data;
5259 struct iwl4965_alive_resp *palive = &priv->card_alive;
5260
5261 if (palive->is_valid)
5262 return sprintf(buf, "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
5263 "fw type: 0x%01X 0x%01X\n",
5264 palive->ucode_major, palive->ucode_minor,
5265 palive->sw_rev[0], palive->sw_rev[1],
5266 palive->ver_type, palive->ver_subtype);
5267
5268 else
5269 return sprintf(buf, "fw not loaded\n");
5270}
5271
5272static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
5273
Zhu Yib481de92007-09-25 17:54:57 -07005274static ssize_t show_temperature(struct device *d,
5275 struct device_attribute *attr, char *buf)
5276{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005277 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005278
Tomas Winklerfee12472008-04-03 16:05:21 -07005279 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005280 return -EAGAIN;
5281
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005282 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07005283}
5284
5285static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
5286
5287static ssize_t show_rs_window(struct device *d,
5288 struct device_attribute *attr,
5289 char *buf)
5290{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005291 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005292 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07005293}
5294static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
5295
5296static ssize_t show_tx_power(struct device *d,
5297 struct device_attribute *attr, char *buf)
5298{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005299 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005300 return sprintf(buf, "%d\n", priv->user_txpower_limit);
5301}
5302
5303static ssize_t store_tx_power(struct device *d,
5304 struct device_attribute *attr,
5305 const char *buf, size_t count)
5306{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005307 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005308 char *p = (char *)buf;
5309 u32 val;
5310
5311 val = simple_strtoul(p, &p, 10);
5312 if (p == buf)
5313 printk(KERN_INFO DRV_NAME
5314 ": %s is not in decimal form.\n", buf);
5315 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005316 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07005317
5318 return count;
5319}
5320
5321static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
5322
5323static ssize_t show_flags(struct device *d,
5324 struct device_attribute *attr, char *buf)
5325{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005326 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005327
5328 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
5329}
5330
5331static ssize_t store_flags(struct device *d,
5332 struct device_attribute *attr,
5333 const char *buf, size_t count)
5334{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005335 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005336 u32 flags = simple_strtoul(buf, NULL, 0);
5337
5338 mutex_lock(&priv->mutex);
5339 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
5340 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005341 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07005342 IWL_WARNING("Could not cancel scan.\n");
5343 else {
5344 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
5345 flags);
5346 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005347 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005348 }
5349 }
5350 mutex_unlock(&priv->mutex);
5351
5352 return count;
5353}
5354
5355static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
5356
5357static ssize_t show_filter_flags(struct device *d,
5358 struct device_attribute *attr, char *buf)
5359{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005360 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005361
5362 return sprintf(buf, "0x%04X\n",
5363 le32_to_cpu(priv->active_rxon.filter_flags));
5364}
5365
5366static ssize_t store_filter_flags(struct device *d,
5367 struct device_attribute *attr,
5368 const char *buf, size_t count)
5369{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005370 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005371 u32 filter_flags = simple_strtoul(buf, NULL, 0);
5372
5373 mutex_lock(&priv->mutex);
5374 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
5375 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005376 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07005377 IWL_WARNING("Could not cancel scan.\n");
5378 else {
5379 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
5380 "0x%04X\n", filter_flags);
5381 priv->staging_rxon.filter_flags =
5382 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005383 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005384 }
5385 }
5386 mutex_unlock(&priv->mutex);
5387
5388 return count;
5389}
5390
5391static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
5392 store_filter_flags);
5393
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005394#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07005395
5396static ssize_t show_measurement(struct device *d,
5397 struct device_attribute *attr, char *buf)
5398{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005399 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005400 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07005401 u32 size = sizeof(measure_report), len = 0, ofs = 0;
5402 u8 *data = (u8 *) & measure_report;
5403 unsigned long flags;
5404
5405 spin_lock_irqsave(&priv->lock, flags);
5406 if (!(priv->measurement_status & MEASUREMENT_READY)) {
5407 spin_unlock_irqrestore(&priv->lock, flags);
5408 return 0;
5409 }
5410 memcpy(&measure_report, &priv->measure_report, size);
5411 priv->measurement_status = 0;
5412 spin_unlock_irqrestore(&priv->lock, flags);
5413
5414 while (size && (PAGE_SIZE - len)) {
5415 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
5416 PAGE_SIZE - len, 1);
5417 len = strlen(buf);
5418 if (PAGE_SIZE - len)
5419 buf[len++] = '\n';
5420
5421 ofs += 16;
5422 size -= min(size, 16U);
5423 }
5424
5425 return len;
5426}
5427
5428static ssize_t store_measurement(struct device *d,
5429 struct device_attribute *attr,
5430 const char *buf, size_t count)
5431{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005432 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07005433 struct ieee80211_measurement_params params = {
5434 .channel = le16_to_cpu(priv->active_rxon.channel),
5435 .start_time = cpu_to_le64(priv->last_tsf),
5436 .duration = cpu_to_le16(1),
5437 };
5438 u8 type = IWL_MEASURE_BASIC;
5439 u8 buffer[32];
5440 u8 channel;
5441
5442 if (count) {
5443 char *p = buffer;
5444 strncpy(buffer, buf, min(sizeof(buffer), count));
5445 channel = simple_strtoul(p, NULL, 0);
5446 if (channel)
5447 params.channel = channel;
5448
5449 p = buffer;
5450 while (*p && *p != ' ')
5451 p++;
5452 if (*p)
5453 type = simple_strtoul(p + 1, NULL, 0);
5454 }
5455
5456 IWL_DEBUG_INFO("Invoking measurement of type %d on "
5457 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005458 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07005459
5460 return count;
5461}
5462
5463static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
5464 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005465#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07005466
5467static ssize_t store_retry_rate(struct device *d,
5468 struct device_attribute *attr,
5469 const char *buf, size_t count)
5470{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005471 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07005472
5473 priv->retry_rate = simple_strtoul(buf, NULL, 0);
5474 if (priv->retry_rate <= 0)
5475 priv->retry_rate = 1;
5476
5477 return count;
5478}
5479
5480static ssize_t show_retry_rate(struct device *d,
5481 struct device_attribute *attr, char *buf)
5482{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005483 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07005484 return sprintf(buf, "%d", priv->retry_rate);
5485}
5486
5487static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
5488 store_retry_rate);
5489
5490static ssize_t store_power_level(struct device *d,
5491 struct device_attribute *attr,
5492 const char *buf, size_t count)
5493{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005494 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07005495 int rc;
5496 int mode;
5497
5498 mode = simple_strtoul(buf, NULL, 0);
5499 mutex_lock(&priv->mutex);
5500
Tomas Winklerfee12472008-04-03 16:05:21 -07005501 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005502 rc = -EAGAIN;
5503 goto out;
5504 }
5505
Mohamed Abbas5da4b552008-04-21 15:41:51 -07005506 rc = iwl_power_set_user_mode(priv, mode);
5507 if (rc) {
5508 IWL_DEBUG_MAC80211("failed setting power mode.\n");
5509 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005510 }
Zhu Yib481de92007-09-25 17:54:57 -07005511 rc = count;
5512
5513 out:
5514 mutex_unlock(&priv->mutex);
5515 return rc;
5516}
5517
5518#define MAX_WX_STRING 80
5519
5520/* Values are in microsecond */
5521static const s32 timeout_duration[] = {
5522 350000,
5523 250000,
5524 75000,
5525 37000,
5526 25000,
5527};
5528static const s32 period_duration[] = {
5529 400000,
5530 700000,
5531 1000000,
5532 1000000,
5533 1000000
5534};
5535
5536static ssize_t show_power_level(struct device *d,
5537 struct device_attribute *attr, char *buf)
5538{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005539 struct iwl_priv *priv = dev_get_drvdata(d);
Mohamed Abbas5da4b552008-04-21 15:41:51 -07005540 int level = priv->power_data.power_mode;
Zhu Yib481de92007-09-25 17:54:57 -07005541 char *p = buf;
5542
5543 p += sprintf(p, "%d ", level);
5544 switch (level) {
5545 case IWL_POWER_MODE_CAM:
5546 case IWL_POWER_AC:
5547 p += sprintf(p, "(AC)");
5548 break;
5549 case IWL_POWER_BATTERY:
5550 p += sprintf(p, "(BATTERY)");
5551 break;
5552 default:
5553 p += sprintf(p,
5554 "(Timeout %dms, Period %dms)",
5555 timeout_duration[level - 1] / 1000,
5556 period_duration[level - 1] / 1000);
5557 }
Mohamed Abbas5da4b552008-04-21 15:41:51 -07005558/*
Zhu Yib481de92007-09-25 17:54:57 -07005559 if (!(priv->power_mode & IWL_POWER_ENABLED))
5560 p += sprintf(p, " OFF\n");
5561 else
5562 p += sprintf(p, " \n");
Mohamed Abbas5da4b552008-04-21 15:41:51 -07005563*/
5564 p += sprintf(p, " \n");
Zhu Yib481de92007-09-25 17:54:57 -07005565 return (p - buf + 1);
Zhu Yib481de92007-09-25 17:54:57 -07005566}
5567
5568static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
5569 store_power_level);
5570
5571static ssize_t show_channels(struct device *d,
5572 struct device_attribute *attr, char *buf)
5573{
Johannes Berg8318d782008-01-24 19:38:38 +01005574 /* all this shit doesn't belong into sysfs anyway */
5575 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005576}
5577
5578static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
5579
5580static ssize_t show_statistics(struct device *d,
5581 struct device_attribute *attr, char *buf)
5582{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005583 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005584 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07005585 u32 len = 0, ofs = 0;
5586 u8 *data = (u8 *) & priv->statistics;
5587 int rc = 0;
5588
Tomas Winklerfee12472008-04-03 16:05:21 -07005589 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005590 return -EAGAIN;
5591
5592 mutex_lock(&priv->mutex);
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07005593 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005594 mutex_unlock(&priv->mutex);
5595
5596 if (rc) {
5597 len = sprintf(buf,
5598 "Error sending statistics request: 0x%08X\n", rc);
5599 return len;
5600 }
5601
5602 while (size && (PAGE_SIZE - len)) {
5603 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
5604 PAGE_SIZE - len, 1);
5605 len = strlen(buf);
5606 if (PAGE_SIZE - len)
5607 buf[len++] = '\n';
5608
5609 ofs += 16;
5610 size -= min(size, 16U);
5611 }
5612
5613 return len;
5614}
5615
5616static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
5617
Zhu Yib481de92007-09-25 17:54:57 -07005618static ssize_t show_status(struct device *d,
5619 struct device_attribute *attr, char *buf)
5620{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005621 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07005622 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005623 return -EAGAIN;
5624 return sprintf(buf, "0x%08x\n", (int)priv->status);
5625}
5626
5627static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
5628
5629static ssize_t dump_error_log(struct device *d,
5630 struct device_attribute *attr,
5631 const char *buf, size_t count)
5632{
5633 char *p = (char *)buf;
5634
5635 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005636 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07005637
5638 return strnlen(buf, count);
5639}
5640
5641static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
5642
5643static ssize_t dump_event_log(struct device *d,
5644 struct device_attribute *attr,
5645 const char *buf, size_t count)
5646{
5647 char *p = (char *)buf;
5648
5649 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005650 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07005651
5652 return strnlen(buf, count);
5653}
5654
5655static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
5656
5657/*****************************************************************************
5658 *
5659 * driver setup and teardown
5660 *
5661 *****************************************************************************/
5662
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005663static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005664{
5665 priv->workqueue = create_workqueue(DRV_NAME);
5666
5667 init_waitqueue_head(&priv->wait_command_queue);
5668
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005669 INIT_WORK(&priv->up, iwl4965_bg_up);
5670 INIT_WORK(&priv->restart, iwl4965_bg_restart);
5671 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
5672 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
5673 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
5674 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
5675 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
5676 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08005677 INIT_WORK(&priv->set_monitor, iwl4965_bg_set_monitor);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005678 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
5679 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
5680 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
5681 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07005682
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005683 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005684
5685 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005686 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07005687}
5688
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005689static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005690{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005691 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005692
Joonwoo Park3ae6a052007-11-29 10:43:16 +09005693 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07005694 cancel_delayed_work(&priv->scan_check);
5695 cancel_delayed_work(&priv->alive_start);
5696 cancel_delayed_work(&priv->post_associate);
5697 cancel_work_sync(&priv->beacon_update);
5698}
5699
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005700static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07005701 &dev_attr_channels.attr,
5702 &dev_attr_dump_errors.attr,
5703 &dev_attr_dump_events.attr,
5704 &dev_attr_flags.attr,
5705 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005706#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07005707 &dev_attr_measurement.attr,
5708#endif
5709 &dev_attr_power_level.attr,
5710 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07005711 &dev_attr_rs_window.attr,
5712 &dev_attr_statistics.attr,
5713 &dev_attr_status.attr,
5714 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07005715 &dev_attr_tx_power.attr,
Ester Kummer8cf769c2008-05-06 11:05:11 +08005716#ifdef CONFIG_IWLWIFI_DEBUG
5717 &dev_attr_debug_level.attr,
5718#endif
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08005719 &dev_attr_version.attr,
Zhu Yib481de92007-09-25 17:54:57 -07005720
5721 NULL
5722};
5723
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005724static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07005725 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005726 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07005727};
5728
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005729static struct ieee80211_ops iwl4965_hw_ops = {
5730 .tx = iwl4965_mac_tx,
5731 .start = iwl4965_mac_start,
5732 .stop = iwl4965_mac_stop,
5733 .add_interface = iwl4965_mac_add_interface,
5734 .remove_interface = iwl4965_mac_remove_interface,
5735 .config = iwl4965_mac_config,
5736 .config_interface = iwl4965_mac_config_interface,
5737 .configure_filter = iwl4965_configure_filter,
5738 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005739 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005740 .get_stats = iwl4965_mac_get_stats,
5741 .get_tx_stats = iwl4965_mac_get_tx_stats,
5742 .conf_tx = iwl4965_mac_conf_tx,
5743 .get_tsf = iwl4965_mac_get_tsf,
5744 .reset_tsf = iwl4965_mac_reset_tsf,
5745 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01005746 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005747#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02005748 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005749#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005750 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07005751};
5752
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005753static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07005754{
5755 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005756 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07005757 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08005758 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005759 unsigned long flags;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005760 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005761
Assaf Krauss316c30d2008-03-14 10:38:46 -07005762 /************************
5763 * 1. Allocating HW data
5764 ************************/
5765
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005766 /* Disabling hardware scan means that mac80211 will perform scans
5767 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07005768 if (cfg->mod_params->disable_hw_scan) {
Ester Kummerbf403db2008-05-05 10:22:40 +08005769 if (cfg->mod_params->debug & IWL_DL_INFO)
5770 dev_printk(KERN_DEBUG, &(pdev->dev),
5771 "Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005772 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005773 }
5774
Assaf Krauss1d0a0822008-03-14 10:38:48 -07005775 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
5776 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07005777 err = -ENOMEM;
5778 goto out;
5779 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07005780 priv = hw->priv;
5781 /* At this point both hw and priv are allocated. */
5782
Zhu Yib481de92007-09-25 17:54:57 -07005783 SET_IEEE80211_DEV(hw, &pdev->dev);
5784
5785 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08005786 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07005787 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07005788
Tomas Winkler0a6857e2008-03-12 16:58:49 -07005789#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08005790 priv->debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07005791 atomic_set(&priv->restrict_refcnt, 0);
5792#endif
Zhu Yib481de92007-09-25 17:54:57 -07005793
Assaf Krauss316c30d2008-03-14 10:38:46 -07005794 /**************************
5795 * 2. Initializing PCI bus
5796 **************************/
5797 if (pci_enable_device(pdev)) {
5798 err = -ENODEV;
5799 goto out_ieee80211_free_hw;
5800 }
5801
5802 pci_set_master(pdev);
5803
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07005804 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005805 if (!err)
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07005806 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
5807 if (err) {
5808 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
5809 if (!err)
5810 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
5811 /* both attempts failed: */
Assaf Krauss316c30d2008-03-14 10:38:46 -07005812 if (err) {
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07005813 printk(KERN_WARNING "%s: No suitable DMA available.\n",
5814 DRV_NAME);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005815 goto out_pci_disable_device;
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07005816 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07005817 }
5818
5819 err = pci_request_regions(pdev, DRV_NAME);
5820 if (err)
5821 goto out_pci_disable_device;
5822
5823 pci_set_drvdata(pdev, priv);
5824
5825 /* We disable the RETRY_TIMEOUT register (0x41) to keep
5826 * PCI Tx retries from interfering with C3 CPU state */
5827 pci_write_config_byte(pdev, 0x41, 0x00);
5828
5829 /***********************
5830 * 3. Read REV register
5831 ***********************/
5832 priv->hw_base = pci_iomap(pdev, 0, 0);
5833 if (!priv->hw_base) {
5834 err = -ENODEV;
5835 goto out_pci_release_regions;
5836 }
5837
5838 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
5839 (unsigned long long) pci_resource_len(pdev, 0));
5840 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
5841
Tomas Winklerb661c812008-04-23 17:14:54 -07005842 iwl_hw_detect(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005843 printk(KERN_INFO DRV_NAME
Tomas Winklerb661c812008-04-23 17:14:54 -07005844 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
5845 priv->cfg->name, priv->hw_rev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005846
Tomas Winkler91238712008-04-23 17:14:53 -07005847 /* amp init */
5848 err = priv->cfg->ops->lib->apm_ops.init(priv);
5849 if (err < 0) {
5850 IWL_DEBUG_INFO("Failed to init APMG\n");
5851 goto out_iounmap;
5852 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07005853 /*****************
5854 * 4. Read EEPROM
5855 *****************/
Assaf Krauss316c30d2008-03-14 10:38:46 -07005856 /* Read the EEPROM */
5857 err = iwl_eeprom_init(priv);
5858 if (err) {
5859 IWL_ERROR("Unable to init EEPROM\n");
5860 goto out_iounmap;
5861 }
Tomas Winkler8614f362008-04-23 17:14:55 -07005862 err = iwl_eeprom_check_version(priv);
5863 if (err)
5864 goto out_iounmap;
5865
Ron Rindjunsky02883012008-05-15 13:53:53 +08005866 /* extract MAC Address */
Assaf Krauss316c30d2008-03-14 10:38:46 -07005867 iwl_eeprom_get_mac(priv, priv->mac_addr);
5868 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
5869 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
5870
5871 /************************
5872 * 5. Setup HW constants
5873 ************************/
5874 /* Device-specific setup */
Tomas Winkler5425e492008-04-15 16:01:38 -07005875 if (priv->cfg->ops->lib->set_hw_params(priv)) {
5876 IWL_ERROR("failed to set hw parameters\n");
Tomas Winkler073d3f52008-04-21 15:41:52 -07005877 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07005878 }
5879
5880 /*******************
Tomas Winkler6ba87952008-05-15 13:54:17 +08005881 * 6. Setup priv
Assaf Krauss316c30d2008-03-14 10:38:46 -07005882 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07005883
Tomas Winkler6ba87952008-05-15 13:54:17 +08005884 err = iwl_init_drv(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005885 if (err)
Ron Rindjunsky399f4902008-04-23 17:14:56 -07005886 goto out_free_eeprom;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005887 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07005888
5889 /**********************************
5890 * 7. Initialize module parameters
5891 **********************************/
5892
5893 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07005894 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07005895 set_bit(STATUS_RF_KILL_SW, &priv->status);
5896 IWL_DEBUG_INFO("Radio disabled.\n");
5897 }
5898
Assaf Krauss316c30d2008-03-14 10:38:46 -07005899 /********************
5900 * 8. Setup services
5901 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005902 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005903 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005904 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005905
5906 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
5907 if (err) {
5908 IWL_ERROR("failed to create sysfs device attributes\n");
Tomas Winkler6ba87952008-05-15 13:54:17 +08005909 goto out_uninit_drv;
Assaf Krauss316c30d2008-03-14 10:38:46 -07005910 }
5911
Assaf Krauss316c30d2008-03-14 10:38:46 -07005912
5913 iwl4965_setup_deferred_work(priv);
5914 iwl4965_setup_rx_handlers(priv);
5915
5916 /********************
5917 * 9. Conclude
5918 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08005919 pci_save_state(pdev);
5920 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07005921
Tomas Winkler6ba87952008-05-15 13:54:17 +08005922 /**********************************
5923 * 10. Setup and register mac80211
5924 **********************************/
5925
5926 err = iwl_setup_mac(priv);
5927 if (err)
5928 goto out_remove_sysfs;
5929
5930 err = iwl_dbgfs_register(priv, DRV_NAME);
5931 if (err)
5932 IWL_ERROR("failed to create debugfs files\n");
5933
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005934 /* notify iwlcore to init */
5935 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07005936 return 0;
5937
Assaf Krauss316c30d2008-03-14 10:38:46 -07005938 out_remove_sysfs:
5939 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Tomas Winkler6ba87952008-05-15 13:54:17 +08005940 out_uninit_drv:
5941 iwl_uninit_drv(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07005942 out_free_eeprom:
5943 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005944 out_iounmap:
5945 pci_iounmap(pdev, priv->hw_base);
5946 out_pci_release_regions:
5947 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07005948 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07005949 out_pci_disable_device:
5950 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07005951 out_ieee80211_free_hw:
5952 ieee80211_free_hw(priv->hw);
5953 out:
5954 return err;
5955}
5956
Reinette Chatrec83dbf62008-03-21 13:53:41 -07005957static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07005958{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005959 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07005960 struct list_head *p, *q;
5961 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005962 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07005963
5964 if (!priv)
5965 return;
5966
5967 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
5968
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07005969 if (priv->mac80211_registered) {
5970 ieee80211_unregister_hw(priv->hw);
5971 priv->mac80211_registered = 0;
5972 }
5973
Zhu Yib481de92007-09-25 17:54:57 -07005974 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08005975
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005976 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005977
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005978 /* make sure we flush any pending irq or
5979 * tasklet for the driver
5980 */
5981 spin_lock_irqsave(&priv->lock, flags);
5982 iwl4965_disable_interrupts(priv);
5983 spin_unlock_irqrestore(&priv->lock, flags);
5984
5985 iwl_synchronize_irq(priv);
5986
Zhu Yib481de92007-09-25 17:54:57 -07005987 /* Free MAC hash list for ADHOC */
5988 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
5989 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
5990 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005991 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07005992 }
5993 }
5994
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005995 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Tomas Winkler712b6cf2008-03-12 16:58:52 -07005996 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005997 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07005998
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005999 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006000
6001 if (priv->rxq.bd)
Tomas Winklera55360e2008-05-05 10:22:28 +08006002 iwl_rx_queue_free(priv, &priv->rxq);
Ron Rindjunsky1053d352008-05-05 10:22:43 +08006003 iwl_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006004
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006005 iwlcore_clear_stations_table(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07006006 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006007
Zhu Yib481de92007-09-25 17:54:57 -07006008
Mohamed Abbas948c1712007-10-25 17:15:45 +08006009 /*netif_stop_queue(dev); */
6010 flush_workqueue(priv->workqueue);
6011
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006012 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07006013 * priv->workqueue... so we can't take down the workqueue
6014 * until now... */
6015 destroy_workqueue(priv->workqueue);
6016 priv->workqueue = NULL;
6017
Zhu Yib481de92007-09-25 17:54:57 -07006018 pci_iounmap(pdev, priv->hw_base);
6019 pci_release_regions(pdev);
6020 pci_disable_device(pdev);
6021 pci_set_drvdata(pdev, NULL);
6022
Tomas Winkler6ba87952008-05-15 13:54:17 +08006023 iwl_uninit_drv(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006024
6025 if (priv->ibss_beacon)
6026 dev_kfree_skb(priv->ibss_beacon);
6027
6028 ieee80211_free_hw(priv->hw);
6029}
6030
6031#ifdef CONFIG_PM
6032
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006033static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07006034{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006035 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006036
Zhu Yie655b9f2008-01-24 02:19:38 -08006037 if (priv->is_open) {
6038 set_bit(STATUS_IN_SUSPEND, &priv->status);
6039 iwl4965_mac_stop(priv->hw);
6040 priv->is_open = 1;
6041 }
Zhu Yib481de92007-09-25 17:54:57 -07006042
Zhu Yib481de92007-09-25 17:54:57 -07006043 pci_set_power_state(pdev, PCI_D3hot);
6044
Zhu Yib481de92007-09-25 17:54:57 -07006045 return 0;
6046}
6047
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006048static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07006049{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006050 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006051
Zhu Yib481de92007-09-25 17:54:57 -07006052 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07006053
Zhu Yie655b9f2008-01-24 02:19:38 -08006054 if (priv->is_open)
6055 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006056
Zhu Yie655b9f2008-01-24 02:19:38 -08006057 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006058 return 0;
6059}
6060
6061#endif /* CONFIG_PM */
6062
6063/*****************************************************************************
6064 *
6065 * driver and module entry point
6066 *
6067 *****************************************************************************/
6068
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006069/* Hardware specific file defines the PCI IDs table for that hardware module */
6070static struct pci_device_id iwl_hw_card_ids[] = {
6071 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
6072 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
Tomas Winkler5a6a2562008-04-24 11:55:23 -07006073#ifdef CONFIG_IWL5000
6074 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
6075 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
6076 {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
6077#endif /* CONFIG_IWL5000 */
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006078 {0}
6079};
6080MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
6081
6082static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07006083 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006084 .id_table = iwl_hw_card_ids,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006085 .probe = iwl4965_pci_probe,
6086 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07006087#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006088 .suspend = iwl4965_pci_suspend,
6089 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07006090#endif
6091};
6092
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006093static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07006094{
6095
6096 int ret;
6097 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
6098 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006099
6100 ret = iwl4965_rate_control_register();
6101 if (ret) {
6102 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
6103 return ret;
6104 }
6105
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006106 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07006107 if (ret) {
6108 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006109 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07006110 }
Zhu Yib481de92007-09-25 17:54:57 -07006111
6112 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006113
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006114error_register:
6115 iwl4965_rate_control_unregister();
6116 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006117}
6118
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006119static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07006120{
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006121 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006122 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07006123}
6124
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006125module_exit(iwl4965_exit);
6126module_init(iwl4965_init);