blob: 0ad89fb90d05bcda2e39cc97d2b4859f417a20e7 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatre01f81622009-01-08 10:20:02 -08003 * Copyright(c) 2003 - 2009 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:
Winkler, Tomas759ef892008-12-09 11:28:58 -080025 * Intel Linux Wireless <ilw@linux.intel.com>
Zhu Yib481de92007-09-25 17:54:57 -070026 * 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>
Zhu Yib481de92007-09-25 17:54:57 -070032#include <linux/init.h>
33#include <linux/pci.h>
34#include <linux/dma-mapping.h>
35#include <linux/delay.h>
36#include <linux/skbuff.h>
37#include <linux/netdevice.h>
38#include <linux/wireless.h>
39#include <linux/firmware.h>
Zhu Yib481de92007-09-25 17:54:57 -070040#include <linux/etherdevice.h>
41#include <linux/if_arp.h>
42
43#include <net/ieee80211_radiotap.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040044#include <net/lib80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070045#include <net/mac80211.h>
46
47#include <asm/div64.h>
48
Samuel Ortiza3139c52008-12-19 10:37:09 +080049#define DRV_NAME "iwl3945"
50
Winkler, Tomasdbb66542008-12-22 11:31:14 +080051#include "iwl-fh.h"
52#include "iwl-3945-fh.h"
Tomas Winkler600c0e12008-12-19 10:37:04 +080053#include "iwl-commands.h"
Samuel Ortiz17f841c2009-01-23 13:45:20 -080054#include "iwl-sta.h"
Zhu Yib481de92007-09-25 17:54:57 -070055#include "iwl-3945.h"
56#include "iwl-helpers.h"
Kolekar, Abhijeet5747d472008-12-19 10:37:18 +080057#include "iwl-core.h"
Samuel Ortizd20b3c62008-12-19 10:37:15 +080058#include "iwl-dev.h"
Zhu Yib481de92007-09-25 17:54:57 -070059
Zhu Yib481de92007-09-25 17:54:57 -070060/*
61 * module name, copyright, version, etc.
Zhu Yib481de92007-09-25 17:54:57 -070062 */
63
64#define DRV_DESCRIPTION \
65"Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
66
Samuel Ortizd08853a2009-01-23 13:45:17 -080067#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070068#define VD "d"
69#else
70#define VD
71#endif
72
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080073#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070074#define VS "s"
75#else
76#define VS
77#endif
78
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080079#define IWL39_VERSION "1.2.26k" VD VS
Reinette Chatre01f81622009-01-08 10:20:02 -080080#define DRV_COPYRIGHT "Copyright(c) 2003-2009 Intel Corporation"
Tomas Winklera7b75202008-12-11 10:33:41 -080081#define DRV_AUTHOR "<ilw@linux.intel.com>"
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080082#define DRV_VERSION IWL39_VERSION
Zhu Yib481de92007-09-25 17:54:57 -070083
Zhu Yib481de92007-09-25 17:54:57 -070084
85MODULE_DESCRIPTION(DRV_DESCRIPTION);
86MODULE_VERSION(DRV_VERSION);
Tomas Winklera7b75202008-12-11 10:33:41 -080087MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
Zhu Yib481de92007-09-25 17:54:57 -070088MODULE_LICENSE("GPL");
89
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080090 /* module parameters */
91struct iwl_mod_params iwl3945_mod_params = {
92 .num_of_queues = IWL39_MAX_NUM_QUEUES,
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -080093 .sw_crypto = 1,
Samuel Ortizaf48d042009-01-23 13:45:19 -080094 .restart_fw = 1,
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080095 /* the rest are 0 by default */
96};
97
Zhu Yib481de92007-09-25 17:54:57 -070098/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +080099 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700100 * the functionality provided here
101 */
102
103/**************************************************************/
Ian Schram01ebd062007-10-25 17:15:22 +0800104#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800105/**
106 * iwl3945_remove_station - Remove driver's knowledge of station.
107 *
108 * NOTE: This does not remove station from device's station table.
109 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800110static u8 iwl3945_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700111{
112 int index = IWL_INVALID_STATION;
113 int i;
114 unsigned long flags;
115
116 spin_lock_irqsave(&priv->sta_lock, flags);
117
118 if (is_ap)
119 index = IWL_AP_ID;
120 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800121 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700122 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800123 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800124 if (priv->stations_39[i].used &&
125 !compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700126 addr)) {
127 index = i;
128 break;
129 }
130
131 if (unlikely(index == IWL_INVALID_STATION))
132 goto out;
133
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800134 if (priv->stations_39[index].used) {
135 priv->stations_39[index].used = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700136 priv->num_stations--;
137 }
138
139 BUG_ON(priv->num_stations < 0);
140
141out:
142 spin_unlock_irqrestore(&priv->sta_lock, flags);
143 return 0;
144}
Zhu Yi556f8db2007-09-27 11:27:33 +0800145#endif
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800146
147/**
148 * iwl3945_clear_stations_table - Clear the driver's station table
149 *
150 * NOTE: This does not clear or otherwise alter the device's station table.
151 */
Abhijeet Kolekare0158e62009-04-08 11:26:37 -0700152void iwl3945_clear_stations_table(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700153{
154 unsigned long flags;
155
156 spin_lock_irqsave(&priv->sta_lock, flags);
157
158 priv->num_stations = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800159 memset(priv->stations_39, 0, sizeof(priv->stations_39));
Zhu Yib481de92007-09-25 17:54:57 -0700160
161 spin_unlock_irqrestore(&priv->sta_lock, flags);
162}
163
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800164/**
165 * iwl3945_add_station - Add station to station tables in driver and device
166 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800167u8 iwl3945_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700168{
169 int i;
170 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800171 struct iwl3945_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700172 unsigned long flags_spin;
Zhu Yic14c5212007-09-27 11:27:35 +0800173 u8 rate;
Zhu Yib481de92007-09-25 17:54:57 -0700174
175 spin_lock_irqsave(&priv->sta_lock, flags_spin);
176 if (is_ap)
177 index = IWL_AP_ID;
178 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800179 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700180 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800181 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800182 if (!compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700183 addr)) {
184 index = i;
185 break;
186 }
187
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800188 if (!priv->stations_39[i].used &&
Zhu Yib481de92007-09-25 17:54:57 -0700189 index == IWL_INVALID_STATION)
190 index = i;
191 }
192
Ian Schram01ebd062007-10-25 17:15:22 +0800193 /* These two conditions has the same outcome but keep them separate
Zhu Yib481de92007-09-25 17:54:57 -0700194 since they have different meaning */
195 if (unlikely(index == IWL_INVALID_STATION)) {
196 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
197 return index;
198 }
199
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800200 if (priv->stations_39[index].used &&
201 !compare_ether_addr(priv->stations_39[index].sta.sta.addr, addr)) {
Zhu Yib481de92007-09-25 17:54:57 -0700202 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
203 return index;
204 }
205
Tomas Winklere1623442009-01-27 14:27:56 -0800206 IWL_DEBUG_ASSOC(priv, "Add STA ID %d: %pM\n", index, addr);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800207 station = &priv->stations_39[index];
Zhu Yib481de92007-09-25 17:54:57 -0700208 station->used = 1;
209 priv->num_stations++;
210
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800211 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800212 memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700213 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
214 station->sta.mode = 0;
215 station->sta.sta.sta_id = index;
216 station->sta.station_flags = 0;
217
Johannes Berg8318d782008-01-24 19:38:38 +0100218 if (priv->band == IEEE80211_BAND_5GHZ)
Tomas Winkler69946332007-10-25 17:15:27 +0800219 rate = IWL_RATE_6M_PLCP;
220 else
221 rate = IWL_RATE_1M_PLCP;
Zhu Yic14c5212007-09-27 11:27:35 +0800222
223 /* Turn on both antennas for the station... */
224 station->sta.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800225 iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
Zhu Yic14c5212007-09-27 11:27:35 +0800226
Zhu Yib481de92007-09-25 17:54:57 -0700227 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800228
229 /* Add station to device's station table */
Samuel Ortiz17f841c2009-01-23 13:45:20 -0800230 iwl_send_add_sta(priv,
231 (struct iwl_addsta_cmd *)&station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700232 return index;
233
234}
235
Zhu Yib481de92007-09-25 17:54:57 -0700236/**
Samuel Ortiz7e4bca52009-01-23 13:45:18 -0800237 * iwl3945_get_antenna_flags - Get antenna flags for RXON command
238 * @priv: eeprom and antenna fields are used to determine antenna flags
239 *
240 * priv->eeprom39 is used to determine if antenna AUX/MAIN are reversed
241 * iwl3945_mod_params.antenna specifies the antenna diversity mode:
242 *
243 * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
244 * IWL_ANTENNA_MAIN - Force MAIN antenna
245 * IWL_ANTENNA_AUX - Force AUX antenna
246 */
247__le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
248{
249 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
250
251 switch (iwl3945_mod_params.antenna) {
252 case IWL_ANTENNA_DIVERSITY:
253 return 0;
254
255 case IWL_ANTENNA_MAIN:
256 if (eeprom->antenna_switch_type)
257 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
258 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
259
260 case IWL_ANTENNA_AUX:
261 if (eeprom->antenna_switch_type)
262 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
263 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
264 }
265
266 /* bad antenna selector value */
267 IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
268 iwl3945_mod_params.antenna);
269
270 return 0; /* "diversity" is default if error */
271}
272
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800273static int iwl3945_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700274 struct ieee80211_key_conf *keyconf,
275 u8 sta_id)
276{
277 unsigned long flags;
278 __le16 key_flags = 0;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800279 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700280
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800281 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
282 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
283
284 if (sta_id == priv->hw_params.bcast_sta_id)
285 key_flags |= STA_KEY_MULTICAST_MSK;
286
287 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
288 keyconf->hw_key_idx = keyconf->keyidx;
289 key_flags &= ~STA_KEY_FLG_INVALID;
290
Zhu Yib481de92007-09-25 17:54:57 -0700291 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800292 priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
293 priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
294 memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -0700295 keyconf->keylen);
296
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800297 memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -0700298 keyconf->keylen);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800299
Abhijeet Kolekar43da9192009-03-17 21:51:53 -0700300 if ((priv->stations_39[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800301 == STA_KEY_FLG_NO_ENC)
Abhijeet Kolekar43da9192009-03-17 21:51:53 -0700302 priv->stations_39[sta_id].sta.key.key_offset =
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800303 iwl_get_free_ucode_key_index(priv);
304 /* else, we are overriding an existing key => no need to allocated room
305 * in uCode. */
306
Abhijeet Kolekar43da9192009-03-17 21:51:53 -0700307 WARN(priv->stations_39[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800308 "no space for a new key");
309
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800310 priv->stations_39[sta_id].sta.key.key_flags = key_flags;
311 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
312 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700313
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800314 IWL_DEBUG_INFO(priv, "hwcrypto: modify ucode station key info\n");
315
316 ret = iwl_send_add_sta(priv,
317 (struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, CMD_ASYNC);
318
Zhu Yib481de92007-09-25 17:54:57 -0700319 spin_unlock_irqrestore(&priv->sta_lock, flags);
320
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800321 return ret;
322}
323
324static int iwl3945_set_tkip_dynamic_key_info(struct iwl_priv *priv,
325 struct ieee80211_key_conf *keyconf,
326 u8 sta_id)
327{
328 return -EOPNOTSUPP;
329}
330
331static int iwl3945_set_wep_dynamic_key_info(struct iwl_priv *priv,
332 struct ieee80211_key_conf *keyconf,
333 u8 sta_id)
334{
335 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -0700336}
337
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800338static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700339{
340 unsigned long flags;
341
342 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800343 memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
344 memset(&priv->stations_39[sta_id].sta.key, 0,
Tomas Winkler4c897252008-12-19 10:37:05 +0800345 sizeof(struct iwl4965_keyinfo));
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800346 priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
347 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
348 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700349 spin_unlock_irqrestore(&priv->sta_lock, flags);
350
Tomas Winklere1623442009-01-27 14:27:56 -0800351 IWL_DEBUG_INFO(priv, "hwcrypto: clear ucode station key info\n");
Samuel Ortiz17f841c2009-01-23 13:45:20 -0800352 iwl_send_add_sta(priv,
353 (struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700354 return 0;
355}
356
Abhijeet Kolekarfa11d522009-03-11 11:17:54 -0700357static int iwl3945_set_dynamic_key(struct iwl_priv *priv,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800358 struct ieee80211_key_conf *keyconf, u8 sta_id)
359{
360 int ret = 0;
361
362 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
363
364 switch (keyconf->alg) {
365 case ALG_CCMP:
366 ret = iwl3945_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
367 break;
368 case ALG_TKIP:
369 ret = iwl3945_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
370 break;
371 case ALG_WEP:
372 ret = iwl3945_set_wep_dynamic_key_info(priv, keyconf, sta_id);
373 break;
374 default:
Abhijeet Kolekar1e680232009-03-17 21:51:50 -0700375 IWL_ERR(priv, "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800376 ret = -EINVAL;
377 }
378
379 IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
380 keyconf->alg, keyconf->keylen, keyconf->keyidx,
381 sta_id, ret);
382
383 return ret;
384}
385
386static int iwl3945_remove_static_key(struct iwl_priv *priv)
387{
388 int ret = -EOPNOTSUPP;
389
390 return ret;
391}
392
393static int iwl3945_set_static_key(struct iwl_priv *priv,
394 struct ieee80211_key_conf *key)
395{
396 if (key->alg == ALG_WEP)
397 return -EOPNOTSUPP;
398
399 IWL_ERR(priv, "Static key invalid: alg %d\n", key->alg);
400 return -EINVAL;
401}
402
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800403static void iwl3945_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700404{
405 struct list_head *element;
406
Tomas Winklere1623442009-01-27 14:27:56 -0800407 IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
Zhu Yib481de92007-09-25 17:54:57 -0700408 priv->frames_count);
409
410 while (!list_empty(&priv->free_frames)) {
411 element = priv->free_frames.next;
412 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800413 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700414 priv->frames_count--;
415 }
416
417 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800418 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -0700419 priv->frames_count);
420 priv->frames_count = 0;
421 }
422}
423
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800424static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700425{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800426 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700427 struct list_head *element;
428 if (list_empty(&priv->free_frames)) {
429 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
430 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800431 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -0700432 return NULL;
433 }
434
435 priv->frames_count++;
436 return frame;
437 }
438
439 element = priv->free_frames.next;
440 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800441 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700442}
443
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800444static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700445{
446 memset(frame, 0, sizeof(*frame));
447 list_add(&frame->list, &priv->free_frames);
448}
449
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800450unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700451 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +0200452 int left)
Zhu Yib481de92007-09-25 17:54:57 -0700453{
454
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800455 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200456 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
457 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -0700458 return 0;
459
460 if (priv->ibss_beacon->len > left)
461 return 0;
462
463 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
464
465 return priv->ibss_beacon->len;
466}
467
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800468static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700469{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800470 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700471 unsigned int frame_size;
472 int rc;
473 u8 rate;
474
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800475 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700476
477 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800478 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -0700479 "command.\n");
480 return -ENOMEM;
481 }
482
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800483 rate = iwl_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700484
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800485 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700486
Samuel Ortiz518099a2009-01-19 15:30:27 -0800487 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700488 &frame->u.cmd[0]);
489
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800490 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700491
492 return rc;
493}
494
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800495static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700496{
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800497 if (priv->shared_virt)
Zhu Yib481de92007-09-25 17:54:57 -0700498 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800499 sizeof(struct iwl3945_shared),
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800500 priv->shared_virt,
501 priv->shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -0700502}
503
Zhu Yib481de92007-09-25 17:54:57 -0700504#define MAX_UCODE_BEACON_INTERVAL 1024
Harvey Harrisonc1b4aa32009-01-29 13:26:44 -0800505#define INTEL_CONN_LISTEN_INTERVAL cpu_to_le16(0xA)
Zhu Yib481de92007-09-25 17:54:57 -0700506
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800507static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -0700508{
509 u16 new_val = 0;
510 u16 beacon_factor = 0;
511
512 beacon_factor =
513 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
514 / MAX_UCODE_BEACON_INTERVAL;
515 new_val = beacon_val / beacon_factor;
516
517 return cpu_to_le16(new_val);
518}
519
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800520static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700521{
522 u64 interval_tm_unit;
523 u64 tsf, result;
524 unsigned long flags;
525 struct ieee80211_conf *conf = NULL;
526 u16 beacon_int = 0;
527
528 conf = ieee80211_get_hw_conf(priv->hw);
529
530 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler28afaf92008-12-19 10:37:06 +0800531 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
Zhu Yib481de92007-09-25 17:54:57 -0700532 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
533
Tomas Winkler28afaf92008-12-19 10:37:06 +0800534 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -0700535
536 beacon_int = priv->beacon_int;
537 spin_unlock_irqrestore(&priv->lock, flags);
538
Johannes Berg05c914f2008-09-11 00:01:58 +0200539 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Zhu Yib481de92007-09-25 17:54:57 -0700540 if (beacon_int == 0) {
541 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
542 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
543 } else {
544 priv->rxon_timing.beacon_interval =
545 cpu_to_le16(beacon_int);
546 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800547 iwl3945_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -0700548 le16_to_cpu(priv->rxon_timing.beacon_interval));
549 }
550
551 priv->rxon_timing.atim_window = 0;
552 } else {
553 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800554 iwl3945_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -0700555 /* TODO: we need to get atim_window from upper stack
556 * for now we set to 0 */
557 priv->rxon_timing.atim_window = 0;
558 }
559
560 interval_tm_unit =
561 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
562 result = do_div(tsf, interval_tm_unit);
563 priv->rxon_timing.beacon_init_val =
564 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
565
Tomas Winklere1623442009-01-27 14:27:56 -0800566 IWL_DEBUG_ASSOC(priv,
567 "beacon interval %d beacon timer %d beacon tim %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700568 le16_to_cpu(priv->rxon_timing.beacon_interval),
569 le32_to_cpu(priv->rxon_timing.beacon_init_val),
570 le16_to_cpu(priv->rxon_timing.atim_window));
571}
572
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800573static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -0700574{
Johannes Berg05c914f2008-09-11 00:01:58 +0200575 if (mode == NL80211_IFTYPE_ADHOC) {
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800576 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -0700577
Samuel Ortize6148912009-01-23 13:45:15 -0800578 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +0100579 priv->band,
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800580 le16_to_cpu(priv->staging_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -0700581
582 if (!ch_info || !is_channel_ibss(ch_info)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800583 IWL_ERR(priv, "channel %d not IBSS channel\n",
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800584 le16_to_cpu(priv->staging_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -0700585 return -EINVAL;
586 }
587 }
588
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800589 iwl_connection_init_rx_config(priv, mode);
Zhu Yib481de92007-09-25 17:54:57 -0700590
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800591 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700592
Tomas Winklera96a27f2008-10-23 23:48:56 -0700593 /* don't commit rxon if rf-kill is on*/
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800594 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +0800595 return -EAGAIN;
596
597 cancel_delayed_work(&priv->scan_check);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -0800598 if (iwl_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800599 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
Tomas Winklere1623442009-01-27 14:27:56 -0800600 IWL_DEBUG_MAC80211(priv, "leaving - scan abort failed.\n");
Mohamed Abbasfde35712007-11-29 11:10:15 +0800601 return -EAGAIN;
602 }
603
Abhijeet Kolekare0158e62009-04-08 11:26:37 -0700604 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700605
606 return 0;
607}
608
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800609static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +0200610 struct ieee80211_tx_info *info,
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800611 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -0700612 struct sk_buff *skb_frag,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800613 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700614{
Winkler, Tomase52119c2008-12-22 11:31:19 +0800615 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
Ivo van Doorn1c014422008-04-17 19:41:02 +0200616 struct iwl3945_hw_key *keyinfo =
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800617 &priv->stations_39[sta_id].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -0700618
619 switch (keyinfo->alg) {
620 case ALG_CCMP:
Winkler, Tomase52119c2008-12-22 11:31:19 +0800621 tx->sec_ctl = TX_CMD_SEC_CCM;
622 memcpy(tx->key, keyinfo->key, keyinfo->keylen);
Tomas Winklere1623442009-01-27 14:27:56 -0800623 IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -0700624 break;
625
626 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -0700627 break;
628
629 case ALG_WEP:
Winkler, Tomase52119c2008-12-22 11:31:19 +0800630 tx->sec_ctl = TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +0200631 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -0700632
633 if (keyinfo->keylen == 13)
Winkler, Tomase52119c2008-12-22 11:31:19 +0800634 tx->sec_ctl |= TX_CMD_SEC_KEY128;
Zhu Yib481de92007-09-25 17:54:57 -0700635
Winkler, Tomase52119c2008-12-22 11:31:19 +0800636 memcpy(&tx->key[3], keyinfo->key, keyinfo->keylen);
Zhu Yib481de92007-09-25 17:54:57 -0700637
Tomas Winklere1623442009-01-27 14:27:56 -0800638 IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +0200639 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -0700640 break;
641
Zhu Yib481de92007-09-25 17:54:57 -0700642 default:
Tomas Winkler978785a2008-12-19 10:37:31 +0800643 IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
Zhu Yib481de92007-09-25 17:54:57 -0700644 break;
645 }
646}
647
648/*
649 * handle build REPLY_TX command notification.
650 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800651static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800652 struct iwl_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +0200653 struct ieee80211_tx_info *info,
Winkler, Tomase52119c2008-12-22 11:31:19 +0800654 struct ieee80211_hdr *hdr, u8 std_id)
Zhu Yib481de92007-09-25 17:54:57 -0700655{
Winkler, Tomase52119c2008-12-22 11:31:19 +0800656 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
657 __le32 tx_flags = tx->tx_flags;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700658 __le16 fc = hdr->frame_control;
Johannes Berge6a98542008-10-21 12:40:02 +0200659 u8 rc_flags = info->control.rates[0].flags;
Zhu Yib481de92007-09-25 17:54:57 -0700660
Winkler, Tomase52119c2008-12-22 11:31:19 +0800661 tx->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +0200662 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700663 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700664 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -0700665 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700666 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -0700667 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
668 tx_flags |= TX_CMD_FLG_TSF_MSK;
669 } else {
670 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
671 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
672 }
673
Winkler, Tomase52119c2008-12-22 11:31:19 +0800674 tx->sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -0700675 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -0700676 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
677
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700678 if (ieee80211_is_data_qos(fc)) {
679 u8 *qc = ieee80211_get_qos_ctl(hdr);
Winkler, Tomase52119c2008-12-22 11:31:19 +0800680 tx->tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -0700681 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800682 } else {
Zhu Yib481de92007-09-25 17:54:57 -0700683 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800684 }
Zhu Yib481de92007-09-25 17:54:57 -0700685
Johannes Berge6a98542008-10-21 12:40:02 +0200686 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Zhu Yib481de92007-09-25 17:54:57 -0700687 tx_flags |= TX_CMD_FLG_RTS_MSK;
688 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
Johannes Berge6a98542008-10-21 12:40:02 +0200689 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Zhu Yib481de92007-09-25 17:54:57 -0700690 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
691 tx_flags |= TX_CMD_FLG_CTS_MSK;
692 }
693
694 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
695 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
696
697 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700698 if (ieee80211_is_mgmt(fc)) {
699 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Winkler, Tomase52119c2008-12-22 11:31:19 +0800700 tx->timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -0700701 else
Winkler, Tomase52119c2008-12-22 11:31:19 +0800702 tx->timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700703 } else {
Winkler, Tomase52119c2008-12-22 11:31:19 +0800704 tx->timeout.pm_frame_timeout = 0;
Abhijeet Kolekar5c8df2d2009-03-11 11:17:55 -0700705#ifdef CONFIG_IWLWIFI_LEDS
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700706 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
707#endif
708 }
Zhu Yib481de92007-09-25 17:54:57 -0700709
Winkler, Tomase52119c2008-12-22 11:31:19 +0800710 tx->driver_txop = 0;
711 tx->tx_flags = tx_flags;
712 tx->next_frame_len = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700713}
714
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800715/**
716 * iwl3945_get_sta_id - Find station's index within station table
717 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800718static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -0700719{
720 int sta_id;
721 u16 fc = le16_to_cpu(hdr->frame_control);
722
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800723 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -0700724 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
725 is_multicast_ether_addr(hdr->addr1))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800726 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700727
728 switch (priv->iw_mode) {
729
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800730 /* If we are a client station in a BSS network, use the special
731 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +0200732 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -0700733 return IWL_AP_ID;
734
735 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +0200736 case NL80211_IFTYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800737 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700738 if (sta_id != IWL_INVALID_STATION)
739 return sta_id;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800740 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700741
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800742 /* If this frame is going out to an IBSS network, find the station,
743 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +0200744 case NL80211_IFTYPE_ADHOC: {
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800745 /* Create new station table entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800746 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700747 if (sta_id != IWL_INVALID_STATION)
748 return sta_id;
749
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800750 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -0700751
752 if (sta_id != IWL_INVALID_STATION)
753 return sta_id;
754
Tomas Winklere1623442009-01-27 14:27:56 -0800755 IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -0700756 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -0700757 hdr->addr1);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +0800758 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800759 return priv->hw_params.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -0700760 }
Stefanik Gábor914233d2008-06-30 17:23:30 +0800761 /* If we are in monitor mode, use BCAST. This is required for
762 * packet injection. */
Johannes Berg05c914f2008-09-11 00:01:58 +0200763 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800764 return priv->hw_params.bcast_sta_id;
Stefanik Gábor914233d2008-06-30 17:23:30 +0800765
Zhu Yib481de92007-09-25 17:54:57 -0700766 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800767 IWL_WARN(priv, "Unknown mode of operation: %d\n",
768 priv->iw_mode);
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800769 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700770 }
771}
772
773/*
774 * start REPLY_TX command process
775 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800776static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700777{
778 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200779 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Winkler, Tomase52119c2008-12-22 11:31:19 +0800780 struct iwl3945_tx_cmd *tx;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800781 struct iwl_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800782 struct iwl_queue *q = NULL;
Winkler, Tomase52119c2008-12-22 11:31:19 +0800783 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700784 dma_addr_t phys_addr;
785 dma_addr_t txcmd_phys;
Winkler, Tomase52119c2008-12-22 11:31:19 +0800786 int txq_id = skb_get_queue_mapping(skb);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800787 u16 len, idx, len_org, hdr_len;
788 u8 id;
789 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -0700790 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800791 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700792 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700793 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -0700794 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800795 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700796 unsigned long flags;
797 int rc;
798
799 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800800 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800801 IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
Zhu Yib481de92007-09-25 17:54:57 -0700802 goto drop_unlock;
803 }
804
Johannes Berge039fa42008-05-15 12:55:29 +0200805 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800806 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700807 goto drop_unlock;
808 }
809
810 unicast = !is_multicast_ether_addr(hdr->addr1);
811 id = 0;
812
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700813 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -0700814
Samuel Ortizd08853a2009-01-23 13:45:17 -0800815#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700816 if (ieee80211_is_auth(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800817 IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700818 else if (ieee80211_is_assoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800819 IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700820 else if (ieee80211_is_reassoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800821 IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
Zhu Yib481de92007-09-25 17:54:57 -0700822#endif
823
Mohamed Abbas7878a5a2007-11-29 11:10:13 +0800824 /* drop all data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +0800825 if (ieee80211_is_data(fc) &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200826 (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800827 (!iwl_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200828 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Tomas Winklere1623442009-01-27 14:27:56 -0800829 IWL_DEBUG_DROP(priv, "Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -0700830 goto drop_unlock;
831 }
832
833 spin_unlock_irqrestore(&priv->lock, flags);
834
Harvey Harrison7294ec92008-07-15 18:43:59 -0700835 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800836
837 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800838 sta_id = iwl3945_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -0700839 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -0800840 IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700841 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700842 goto drop;
843 }
844
Tomas Winklere1623442009-01-27 14:27:56 -0800845 IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
Zhu Yib481de92007-09-25 17:54:57 -0700846
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700847 if (ieee80211_is_data_qos(fc)) {
848 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -0700849 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800850 seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
Zhu Yib481de92007-09-25 17:54:57 -0700851 IEEE80211_SCTL_SEQ;
852 hdr->seq_ctrl = cpu_to_le16(seq_number) |
853 (hdr->seq_ctrl &
Harvey Harrisonc1b4aa32009-01-29 13:26:44 -0800854 cpu_to_le16(IEEE80211_SCTL_FRAG));
Zhu Yib481de92007-09-25 17:54:57 -0700855 seq_number += 0x10;
856 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800857
858 /* Descriptor for chosen Tx queue */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800859 txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -0700860 q = &txq->q;
861
862 spin_lock_irqsave(&priv->lock, flags);
863
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800864 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700865
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800866 /* Set up driver data for this TFD */
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800867 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800868 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800869
870 /* Init first empty entry in queue's array of Tx/cmd buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800871 out_cmd = txq->cmd[idx];
Winkler, Tomase52119c2008-12-22 11:31:19 +0800872 tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
Zhu Yib481de92007-09-25 17:54:57 -0700873 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
Winkler, Tomase52119c2008-12-22 11:31:19 +0800874 memset(tx, 0, sizeof(*tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800875
876 /*
877 * Set up the Tx-command (not MAC!) header.
878 * Store the chosen Tx queue and TFD index within the sequence field;
879 * after Tx, uCode's Tx response will return this value so driver can
880 * locate the frame within the tx queue and do post-tx processing.
881 */
Zhu Yib481de92007-09-25 17:54:57 -0700882 out_cmd->hdr.cmd = REPLY_TX;
883 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800884 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800885
886 /* Copy MAC header from skb into command buffer */
Winkler, Tomase52119c2008-12-22 11:31:19 +0800887 memcpy(tx->hdr, hdr, hdr_len);
Zhu Yib481de92007-09-25 17:54:57 -0700888
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800889 /*
890 * Use the first empty entry in this queue's command buffer array
891 * to contain the Tx command and MAC header concatenated together
892 * (payload data will be in another buffer).
893 * Size of this varies, due to varying MAC header length.
894 * If end is not dword aligned, we'll have 2 extra bytes at the end
895 * of the MAC header (device reads on dword boundaries).
896 * We'll tell device about this padding later.
897 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800898 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +0800899 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -0700900
901 len_org = len;
902 len = (len + 3) & ~3;
903
904 if (len_org != len)
905 len_org = 1;
906 else
907 len_org = 0;
908
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800909 /* Physical address of this Tx command's header (not MAC header!),
910 * within command buffer array. */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800911 txcmd_phys = pci_map_single(priv->pci_dev,
912 out_cmd, sizeof(struct iwl_cmd),
913 PCI_DMA_TODEVICE);
914 pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
915 pci_unmap_len_set(&out_cmd->meta, len, sizeof(struct iwl_cmd));
916 /* Add buffer containing Tx command and MAC(!) header to TFD's
917 * first entry */
918 txcmd_phys += offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -0700919
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800920 /* Add buffer containing Tx command and MAC(!) header to TFD's
921 * first entry */
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800922 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
923 txcmd_phys, len, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700924
Johannes Bergd0f09802008-07-29 11:32:07 +0200925 if (info->control.hw_key)
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800926 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -0700927
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800928 /* Set up TFD's 2nd entry to point directly to remainder of skb,
929 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -0700930 len = skb->len - hdr_len;
931 if (len) {
932 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
933 len, PCI_DMA_TODEVICE);
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800934 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
935 phys_addr, len,
936 0, U32_PAD(len));
Zhu Yib481de92007-09-25 17:54:57 -0700937 }
938
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800939 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -0700940 len = (u16)skb->len;
Winkler, Tomase52119c2008-12-22 11:31:19 +0800941 tx->len = cpu_to_le16(len);
Zhu Yib481de92007-09-25 17:54:57 -0700942
943 /* TODO need this for burst mode later on */
Winkler, Tomase52119c2008-12-22 11:31:19 +0800944 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -0700945
946 /* set is_hcca to 0; it probably will never be implemented */
Johannes Berge039fa42008-05-15 12:55:29 +0200947 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700948
Winkler, Tomase52119c2008-12-22 11:31:19 +0800949 tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
950 tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700951
Harvey Harrison8b7b1e02008-06-11 14:21:56 -0700952 if (!ieee80211_has_morefrags(hdr->frame_control)) {
Zhu Yib481de92007-09-25 17:54:57 -0700953 txq->need_update = 1;
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300954 if (qc)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800955 priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
Zhu Yib481de92007-09-25 17:54:57 -0700956 } else {
957 wait_write_ptr = 1;
958 txq->need_update = 0;
959 }
960
Winkler, Tomase52119c2008-12-22 11:31:19 +0800961 iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
Zhu Yib481de92007-09-25 17:54:57 -0700962
Winkler, Tomase52119c2008-12-22 11:31:19 +0800963 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
Harvey Harrison7294ec92008-07-15 18:43:59 -0700964 ieee80211_hdrlen(fc));
Zhu Yib481de92007-09-25 17:54:57 -0700965
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800966 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800967 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -0800968 rc = iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700969 spin_unlock_irqrestore(&priv->lock, flags);
970
971 if (rc)
972 return rc;
973
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800974 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -0700975 && priv->mac80211_registered) {
976 if (wait_write_ptr) {
977 spin_lock_irqsave(&priv->lock, flags);
978 txq->need_update = 1;
Samuel Ortiz4f3602c2009-01-19 15:30:25 -0800979 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700980 spin_unlock_irqrestore(&priv->lock, flags);
981 }
982
Johannes Berge4e72fb2009-03-23 17:28:42 +0100983 iwl_stop_queue(priv, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -0700984 }
985
986 return 0;
987
988drop_unlock:
989 spin_unlock_irqrestore(&priv->lock, flags);
990drop:
991 return -1;
992}
993
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800994#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -0700995
996#include "iwl-spectrum.h"
997
998#define BEACON_TIME_MASK_LOW 0x00FFFFFF
999#define BEACON_TIME_MASK_HIGH 0xFF000000
1000#define TIME_UNIT 1024
1001
1002/*
1003 * extended beacon time format
1004 * time in usec will be changed into a 32-bit value in 8:24 format
1005 * the high 1 byte is the beacon counts
1006 * the lower 3 bytes is the time in usec within one beacon interval
1007 */
1008
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001009static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07001010{
1011 u32 quot;
1012 u32 rem;
1013 u32 interval = beacon_interval * 1024;
1014
1015 if (!interval || !usec)
1016 return 0;
1017
1018 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
1019 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
1020
1021 return (quot << 24) + rem;
1022}
1023
1024/* base is usually what we get from ucode with each received frame,
1025 * the same as HW timer counter counting down
1026 */
1027
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001028static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07001029{
1030 u32 base_low = base & BEACON_TIME_MASK_LOW;
1031 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
1032 u32 interval = beacon_interval * TIME_UNIT;
1033 u32 res = (base & BEACON_TIME_MASK_HIGH) +
1034 (addon & BEACON_TIME_MASK_HIGH);
1035
1036 if (base_low > addon_low)
1037 res += base_low - addon_low;
1038 else if (base_low < addon_low) {
1039 res += interval + base_low - addon_low;
1040 res += (1 << 24);
1041 } else
1042 res += (1 << 24);
1043
1044 return cpu_to_le32(res);
1045}
1046
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001047static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001048 struct ieee80211_measurement_params *params,
1049 u8 type)
1050{
Tomas Winkler600c0e12008-12-19 10:37:04 +08001051 struct iwl_spectrum_cmd spectrum;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001052 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08001053 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001054 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
1055 .data = (void *)&spectrum,
1056 .meta.flags = CMD_WANT_SKB,
1057 };
1058 u32 add_time = le64_to_cpu(params->start_time);
1059 int rc;
1060 int spectrum_resp_status;
1061 int duration = le16_to_cpu(params->duration);
1062
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001063 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001064 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001065 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07001066 le64_to_cpu(params->start_time) - priv->last_tsf,
1067 le16_to_cpu(priv->rxon_timing.beacon_interval));
1068
1069 memset(&spectrum, 0, sizeof(spectrum));
1070
1071 spectrum.channel_count = cpu_to_le16(1);
1072 spectrum.flags =
1073 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
1074 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
1075 cmd.len = sizeof(spectrum);
1076 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
1077
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001078 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001079 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001080 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07001081 add_time,
1082 le16_to_cpu(priv->rxon_timing.beacon_interval));
1083 else
1084 spectrum.start_time = 0;
1085
1086 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
1087 spectrum.channels[0].channel = params->channel;
1088 spectrum.channels[0].type = type;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001089 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07001090 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
1091 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
1092
Samuel Ortiz518099a2009-01-19 15:30:27 -08001093 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001094 if (rc)
1095 return rc;
1096
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001097 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001098 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001099 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -07001100 rc = -EIO;
1101 }
1102
1103 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
1104 switch (spectrum_resp_status) {
1105 case 0: /* Command will be handled */
1106 if (res->u.spectrum.id != 0xff) {
Tomas Winklere1623442009-01-27 14:27:56 -08001107 IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
Ian Schrambc434dd2007-10-25 17:15:29 +08001108 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07001109 priv->measurement_status &= ~MEASUREMENT_READY;
1110 }
1111 priv->measurement_status |= MEASUREMENT_ACTIVE;
1112 rc = 0;
1113 break;
1114
1115 case 1: /* Command will not be handled */
1116 rc = -EAGAIN;
1117 break;
1118 }
1119
1120 dev_kfree_skb_any(cmd.meta.u.skb);
1121
1122 return rc;
1123}
1124#endif
1125
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001126static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001127 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001128{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001129 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
1130 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07001131 struct delayed_work *pwork;
1132
1133 palive = &pkt->u.alive_frame;
1134
Tomas Winklere1623442009-01-27 14:27:56 -08001135 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
Zhu Yib481de92007-09-25 17:54:57 -07001136 "0x%01X 0x%01X\n",
1137 palive->is_valid, palive->ver_type,
1138 palive->ver_subtype);
1139
1140 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Tomas Winklere1623442009-01-27 14:27:56 -08001141 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001142 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
1143 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001144 pwork = &priv->init_alive_start;
1145 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08001146 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001147 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001148 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001149 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001150 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001151 }
1152
1153 /* We delay the ALIVE response by 5ms to
1154 * give the HW RF Kill time to activate... */
1155 if (palive->is_valid == UCODE_VALID_OK)
1156 queue_delayed_work(priv->workqueue, pwork,
1157 msecs_to_jiffies(5));
1158 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001159 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001160}
1161
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001162static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001163 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001164{
Helmut Schaac7e035a2009-01-19 13:02:15 +01001165#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001166 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Helmut Schaac7e035a2009-01-19 13:02:15 +01001167#endif
Zhu Yib481de92007-09-25 17:54:57 -07001168
Tomas Winklere1623442009-01-27 14:27:56 -08001169 IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
Zhu Yib481de92007-09-25 17:54:57 -07001170 return;
1171}
1172
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001173static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07001174{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001175 struct iwl_priv *priv =
1176 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07001177 struct sk_buff *beacon;
1178
1179 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02001180 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07001181
1182 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001183 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07001184 return;
1185 }
1186
1187 mutex_lock(&priv->mutex);
1188 /* new beacon skb is allocated every time; dispose previous.*/
1189 if (priv->ibss_beacon)
1190 dev_kfree_skb(priv->ibss_beacon);
1191
1192 priv->ibss_beacon = beacon;
1193 mutex_unlock(&priv->mutex);
1194
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001195 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001196}
1197
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001198static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001199 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001200{
Samuel Ortizd08853a2009-01-23 13:45:17 -08001201#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001202 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001203 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -07001204 u8 rate = beacon->beacon_notify_hdr.rate;
1205
Tomas Winklere1623442009-01-27 14:27:56 -08001206 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
Zhu Yib481de92007-09-25 17:54:57 -07001207 "tsf %d %d rate %d\n",
1208 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
1209 beacon->beacon_notify_hdr.failure_frame,
1210 le32_to_cpu(beacon->ibss_mgr_status),
1211 le32_to_cpu(beacon->high_tsf),
1212 le32_to_cpu(beacon->low_tsf), rate);
1213#endif
1214
Johannes Berg05c914f2008-09-11 00:01:58 +02001215 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -07001216 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
1217 queue_work(priv->workqueue, &priv->beacon_update);
1218}
1219
Zhu Yib481de92007-09-25 17:54:57 -07001220/* Handle notification from uCode that card's power state is changing
1221 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001222static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001223 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001224{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001225 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001226 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
1227 unsigned long status = priv->status;
1228
Tomas Winklere1623442009-01-27 14:27:56 -08001229 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s\n",
Zhu Yib481de92007-09-25 17:54:57 -07001230 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1231 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
1232
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001233 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001234 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1235
1236 if (flags & HW_CARD_DISABLED)
1237 set_bit(STATUS_RF_KILL_HW, &priv->status);
1238 else
1239 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1240
1241
1242 if (flags & SW_CARD_DISABLED)
1243 set_bit(STATUS_RF_KILL_SW, &priv->status);
1244 else
1245 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1246
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08001247 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001248
1249 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
1250 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
1251 (test_bit(STATUS_RF_KILL_SW, &status) !=
1252 test_bit(STATUS_RF_KILL_SW, &priv->status)))
1253 queue_work(priv->workqueue, &priv->rf_kill);
1254 else
1255 wake_up_interruptible(&priv->wait_command_queue);
1256}
1257
1258/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001259 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07001260 *
1261 * Setup the RX handlers for each of the reply types sent from the uCode
1262 * to the host.
1263 *
1264 * This function chains into the hardware specific files for them to setup
1265 * any hardware specific handlers as well.
1266 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001267static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001268{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001269 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
1270 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
Abhijeet Kolekar261b9c32009-02-18 15:54:29 -08001271 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001272 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -08001273 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001274 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -08001275 iwl_rx_pm_debug_statistics_notif;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001276 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001277
Ben Cahill9fbab512007-11-29 11:09:47 +08001278 /*
1279 * The same handler is used for both the REPLY to a discrete
1280 * statistics request from the host as well as for the periodic
1281 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07001282 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001283 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
1284 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07001285
Abhijeet Kolekar261b9c32009-02-18 15:54:29 -08001286 iwl_setup_spectrum_handlers(priv);
Abhijeet Kolekarcade0eb2009-02-18 15:54:26 -08001287 iwl_setup_rx_scan_handlers(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001288 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001289
Ben Cahill9fbab512007-11-29 11:09:47 +08001290 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001291 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001292}
1293
Zhu Yib481de92007-09-25 17:54:57 -07001294/************************** RX-FUNCTIONS ****************************/
1295/*
1296 * Rx theory of operation
1297 *
1298 * The host allocates 32 DMA target addresses and passes the host address
1299 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
1300 * 0 to 31
1301 *
1302 * Rx Queue Indexes
1303 * The host/firmware share two index registers for managing the Rx buffers.
1304 *
1305 * The READ index maps to the first position that the firmware may be writing
1306 * to -- the driver can read up to (but not including) this position and get
1307 * good data.
1308 * The READ index is managed by the firmware once the card is enabled.
1309 *
1310 * The WRITE index maps to the last position the driver has read from -- the
1311 * position preceding WRITE is the last slot the firmware can place a packet.
1312 *
1313 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
1314 * WRITE = READ.
1315 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001316 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07001317 * INDEX position, and WRITE to the last (READ - 1 wrapped)
1318 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001319 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07001320 * and fire the RX interrupt. The driver can then query the READ index and
1321 * process as many packets as possible, moving the WRITE index forward as it
1322 * resets the Rx queue buffers with new memory.
1323 *
1324 * The management in the driver is as follows:
1325 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
1326 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08001327 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001328 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07001329 * iwl->rxq is replenished and the READ INDEX is updated (updating the
1330 * 'processed' and 'read' driver indexes as well)
1331 * + A received packet is processed and handed to the kernel network stack,
1332 * detached from the iwl->rxq. The driver 'processed' index is updated.
1333 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
1334 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
1335 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
1336 * were enough free buffers and RX_STALLED is set it is cleared.
1337 *
1338 *
1339 * Driver sequence:
1340 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001341 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001342 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08001343 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07001344 * queue, updates firmware pointers, and updates
1345 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001346 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07001347 *
1348 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001349 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07001350 * READ INDEX, detaching the SKB from the pool.
1351 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001352 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07001353 * slots.
1354 * ...
1355 *
1356 */
1357
1358/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001359 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07001360 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001361static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001362 dma_addr_t dma_addr)
1363{
1364 return cpu_to_le32((u32)dma_addr);
1365}
1366
1367/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001368 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07001369 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001370 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07001371 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08001372 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07001373 *
1374 * This moves the 'write' index forward to catch up with 'processed', and
1375 * also updates the memory address in the firmware to reference the new
1376 * target buffer.
1377 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001378static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001379{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001380 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001381 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001382 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07001383 unsigned long flags;
1384 int write, rc;
1385
1386 spin_lock_irqsave(&rxq->lock, flags);
1387 write = rxq->write & ~0x7;
Winkler, Tomas37d68312009-01-08 10:19:54 -08001388 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001389 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07001390 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001391 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07001392 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001393
1394 /* Point to Rx buffer via next RBD in circular buffer */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001395 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07001396 rxq->queue[rxq->write] = rxb;
1397 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
1398 rxq->free_count--;
1399 }
1400 spin_unlock_irqrestore(&rxq->lock, flags);
1401 /* If the pre-allocated buffer pool is dropping low, schedule to
1402 * refill it */
1403 if (rxq->free_count <= RX_LOW_WATERMARK)
1404 queue_work(priv->workqueue, &priv->rx_replenish);
1405
1406
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001407 /* If we've added more space for the firmware to place data, tell it.
1408 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07001409 if ((write != (rxq->write & ~0x7))
1410 || (abs(rxq->write - rxq->read) > 7)) {
1411 spin_lock_irqsave(&rxq->lock, flags);
1412 rxq->need_update = 1;
1413 spin_unlock_irqrestore(&rxq->lock, flags);
Winkler, Tomas141c43a2009-01-08 10:19:53 -08001414 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07001415 if (rc)
1416 return rc;
1417 }
1418
1419 return 0;
1420}
1421
1422/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001423 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07001424 *
1425 * When moving to rx_free an SKB is allocated for the slot.
1426 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001427 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08001428 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07001429 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001430static void iwl3945_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001431{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001432 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001433 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001434 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07001435 unsigned long flags;
1436 spin_lock_irqsave(&rxq->lock, flags);
1437 while (!list_empty(&rxq->rx_used)) {
1438 element = rxq->rx_used.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001439 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001440
1441 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07001442 rxb->skb =
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08001443 alloc_skb(priv->hw_params.rx_buf_size,
1444 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07001445 if (!rxb->skb) {
1446 if (net_ratelimit())
Tomas Winkler978785a2008-12-19 10:37:31 +08001447 IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
Zhu Yib481de92007-09-25 17:54:57 -07001448 /* We don't reschedule replenish work here -- we will
1449 * call the restock method and if it still needs
1450 * more buffers it will schedule replenish */
1451 break;
1452 }
Zhu Yi12342c42007-12-20 11:27:32 +08001453
1454 /* If radiotap head is required, reserve some headroom here.
1455 * The physical head count is a variable rx_stats->phy_count.
1456 * We reserve 4 bytes here. Plus these extra bytes, the
1457 * headroom of the physical head should be enough for the
1458 * radiotap head that iwl3945 supported. See iwl3945_rt.
1459 */
1460 skb_reserve(rxb->skb, 4);
1461
Zhu Yib481de92007-09-25 17:54:57 -07001462 priv->alloc_rxb_skb++;
1463 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001464
1465 /* Get physical address of RB/SKB */
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08001466 rxb->real_dma_addr = pci_map_single(priv->pci_dev,
1467 rxb->skb->data,
1468 priv->hw_params.rx_buf_size,
1469 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07001470 list_add_tail(&rxb->list, &rxq->rx_free);
1471 rxq->free_count++;
1472 }
1473 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001474}
1475
1476/*
1477 * this should be called while priv->lock is locked
1478 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02001479static void __iwl3945_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001480{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001481 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001482
1483 iwl3945_rx_allocate(priv);
1484 iwl3945_rx_queue_restock(priv);
1485}
1486
1487
1488void iwl3945_rx_replenish(void *data)
1489{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001490 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001491 unsigned long flags;
1492
1493 iwl3945_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001494
1495 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001496 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001497 spin_unlock_irqrestore(&priv->lock, flags);
1498}
1499
Zhu Yib481de92007-09-25 17:54:57 -07001500/* Convert linear signal-to-noise ratio into dB */
1501static u8 ratio2dB[100] = {
1502/* 0 1 2 3 4 5 6 7 8 9 */
1503 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
1504 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
1505 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
1506 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
1507 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
1508 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
1509 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
1510 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
1511 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
1512 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
1513};
1514
1515/* Calculates a relative dB value from a ratio of linear
1516 * (i.e. not dB) signal levels.
1517 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001518int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07001519{
Adrian Bunk221c80c2008-02-02 23:19:01 +02001520 /* 1000:1 or higher just report as 60 dB */
1521 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07001522 return 60;
1523
Adrian Bunk221c80c2008-02-02 23:19:01 +02001524 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07001525 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02001526 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001527 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07001528
1529 /* We shouldn't see this */
1530 if (sig_ratio < 1)
1531 return 0;
1532
1533 /* Use table for ratios 1:1 - 99:1 */
1534 return (int)ratio2dB[sig_ratio];
1535}
1536
1537#define PERFECT_RSSI (-20) /* dBm */
1538#define WORST_RSSI (-95) /* dBm */
1539#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
1540
1541/* Calculate an indication of rx signal quality (a percentage, not dBm!).
1542 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
1543 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001544int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07001545{
1546 int sig_qual;
1547 int degradation = PERFECT_RSSI - rssi_dbm;
1548
1549 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
1550 * as indicator; formula is (signal dbm - noise dbm).
1551 * SNR at or above 40 is a great signal (100%).
1552 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
1553 * Weakest usable signal is usually 10 - 15 dB SNR. */
1554 if (noise_dbm) {
1555 if (rssi_dbm - noise_dbm >= 40)
1556 return 100;
1557 else if (rssi_dbm < noise_dbm)
1558 return 0;
1559 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
1560
1561 /* Else use just the signal level.
1562 * This formula is a least squares fit of data points collected and
1563 * compared with a reference system that had a percentage (%) display
1564 * for signal quality. */
1565 } else
1566 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
1567 (15 * RSSI_RANGE + 62 * degradation)) /
1568 (RSSI_RANGE * RSSI_RANGE);
1569
1570 if (sig_qual > 100)
1571 sig_qual = 100;
1572 else if (sig_qual < 1)
1573 sig_qual = 0;
1574
1575 return sig_qual;
1576}
1577
1578/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001579 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07001580 *
1581 * Uses the priv->rx_handlers callback function array to invoke
1582 * the appropriate handlers, including command responses,
1583 * frame-received notifications, and other notifications.
1584 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001585static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001586{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001587 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001588 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001589 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001590 u32 r, i;
1591 int reclaim;
1592 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001593 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08001594 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07001595
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001596 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1597 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08001598 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07001599 i = rxq->read;
1600
Winkler, Tomas37d68312009-01-08 10:19:54 -08001601 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001602 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001603 /* Rx interrupt, but nothing sent from uCode */
1604 if (i == r)
Tomas Winklere1623442009-01-27 14:27:56 -08001605 IWL_DEBUG(priv, IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07001606
1607 while (i != r) {
1608 rxb = rxq->queue[i];
1609
Ben Cahill9fbab512007-11-29 11:09:47 +08001610 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07001611 * then a bug has been introduced in the queue refilling
1612 * routines -- catch it here */
1613 BUG_ON(rxb == NULL);
1614
1615 rxq->queue[i] = NULL;
1616
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001617 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08001618 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07001619 PCI_DMA_FROMDEVICE);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001620 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001621
1622 /* Reclaim a command buffer only if this packet is a response
1623 * to a (driver-originated) command.
1624 * If the packet (e.g. Rx frame) originated from uCode,
1625 * there is no command buffer to reclaim.
1626 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1627 * but apparently a few don't get set; catch them here. */
1628 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1629 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1630 (pkt->hdr.cmd != REPLY_TX);
1631
1632 /* Based on type of command response or notification,
1633 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001634 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07001635 if (priv->rx_handlers[pkt->hdr.cmd]) {
Tomas Winklere1623442009-01-27 14:27:56 -08001636 IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07001637 "r = %d, i = %d, %s, 0x%02x\n", r, i,
1638 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
1639 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
1640 } else {
1641 /* No handling needed */
Tomas Winklere1623442009-01-27 14:27:56 -08001642 IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07001643 "r %d i %d No handler needed for %s, 0x%02x\n",
1644 r, i, get_cmd_string(pkt->hdr.cmd),
1645 pkt->hdr.cmd);
1646 }
1647
1648 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001649 /* Invoke any callbacks, transfer the skb to caller, and
Samuel Ortiz518099a2009-01-19 15:30:27 -08001650 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07001651 * as we reclaim the driver command queue */
1652 if (rxb && rxb->skb)
Abhijeet Kolekar732587a2009-03-11 11:17:57 -07001653 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001654 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001655 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07001656 }
1657
1658 /* For now we just don't re-use anything. We can tweak this
1659 * later to try and re-use notification packets and SKBs that
1660 * fail to Rx correctly */
1661 if (rxb->skb != NULL) {
1662 priv->alloc_rxb_skb--;
1663 dev_kfree_skb_any(rxb->skb);
1664 rxb->skb = NULL;
1665 }
1666
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001667 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08001668 priv->hw_params.rx_buf_size,
1669 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07001670 spin_lock_irqsave(&rxq->lock, flags);
1671 list_add_tail(&rxb->list, &priv->rxq.rx_used);
1672 spin_unlock_irqrestore(&rxq->lock, flags);
1673 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001674 /* If there are a lot of unused frames,
1675 * restock the Rx queue so ucode won't assert. */
1676 if (fill_rx) {
1677 count++;
1678 if (count >= 8) {
1679 priv->rxq.read = i;
1680 __iwl3945_rx_replenish(priv);
1681 count = 0;
1682 }
1683 }
Zhu Yib481de92007-09-25 17:54:57 -07001684 }
1685
1686 /* Backtrack one entry */
1687 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001688 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001689}
1690
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001691/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001692static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001693{
Tomas Winklera96a27f2008-10-23 23:48:56 -07001694 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001695 synchronize_irq(priv->pci_dev->irq);
1696 tasklet_kill(&priv->irq_tasklet);
1697}
1698
Zhu Yib481de92007-09-25 17:54:57 -07001699static const char *desc_lookup(int i)
1700{
1701 switch (i) {
1702 case 1:
1703 return "FAIL";
1704 case 2:
1705 return "BAD_PARAM";
1706 case 3:
1707 return "BAD_CHECKSUM";
1708 case 4:
1709 return "NMI_INTERRUPT";
1710 case 5:
1711 return "SYSASSERT";
1712 case 6:
1713 return "FATAL_ERROR";
1714 }
1715
1716 return "UNKNOWN";
1717}
1718
1719#define ERROR_START_OFFSET (1 * sizeof(u32))
1720#define ERROR_ELEM_SIZE (7 * sizeof(u32))
1721
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001722static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001723{
1724 u32 i;
1725 u32 desc, time, count, base, data1;
1726 u32 blink1, blink2, ilink1, ilink2;
1727 int rc;
1728
1729 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
1730
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001731 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001732 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07001733 return;
1734 }
1735
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001736 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001737 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001738 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001739 return;
1740 }
1741
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001742 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07001743
1744 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001745 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
1746 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
1747 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07001748 }
1749
Winkler, Tomas15b16872008-12-19 10:37:33 +08001750 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07001751 "ilink1 nmiPC Line\n");
1752 for (i = ERROR_START_OFFSET;
1753 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
1754 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001755 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07001756 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001757 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001758 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001759 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001760 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001761 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001762 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001763 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001764 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001765 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001766 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001767 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001768
Winkler, Tomas15b16872008-12-19 10:37:33 +08001769 IWL_ERR(priv,
1770 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
1771 desc_lookup(desc), desc, time, blink1, blink2,
1772 ilink1, ilink2, data1);
Zhu Yib481de92007-09-25 17:54:57 -07001773 }
1774
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001775 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001776
1777}
1778
Ben Cahillf58177b2007-11-29 11:09:43 +08001779#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07001780
1781/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001782 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07001783 *
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001784 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07001785 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001786static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07001787 u32 num_events, u32 mode)
1788{
1789 u32 i;
1790 u32 base; /* SRAM byte address of event log header */
1791 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1792 u32 ptr; /* SRAM byte address of log data */
1793 u32 ev, time, data; /* event log data */
1794
1795 if (num_events == 0)
1796 return;
1797
1798 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1799
1800 if (mode == 0)
1801 event_size = 2 * sizeof(u32);
1802 else
1803 event_size = 3 * sizeof(u32);
1804
1805 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1806
1807 /* "time" is actually "data" for mode 0 (no timestamp).
1808 * place event id # at far right for easier visual parsing. */
1809 for (i = 0; i < num_events; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001810 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07001811 ptr += sizeof(u32);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001812 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07001813 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001814 if (mode == 0) {
1815 /* data, ev */
1816 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
1817 } else {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001818 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07001819 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001820 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
Zhu Yib481de92007-09-25 17:54:57 -07001821 }
1822 }
1823}
1824
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001825static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001826{
1827 int rc;
1828 u32 base; /* SRAM byte address of event log header */
1829 u32 capacity; /* event log capacity in # entries */
1830 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
1831 u32 num_wraps; /* # times uCode wrapped to top of log */
1832 u32 next_entry; /* index of next entry to be written by uCode */
1833 u32 size; /* # entries that we'll print */
1834
1835 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001836 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001837 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07001838 return;
1839 }
1840
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001841 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001842 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001843 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001844 return;
1845 }
1846
1847 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001848 capacity = iwl_read_targ_mem(priv, base);
1849 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
1850 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
1851 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07001852
1853 size = num_wraps ? capacity : next_entry;
1854
1855 /* bail out if nothing in log */
1856 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001857 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001858 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001859 return;
1860 }
1861
Winkler, Tomas15b16872008-12-19 10:37:33 +08001862 IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07001863 size, num_wraps);
1864
1865 /* if uCode has wrapped back to top of log, start at the oldest entry,
1866 * i.e the next one that uCode would fill. */
1867 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001868 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07001869 capacity - next_entry, mode);
1870
1871 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001872 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07001873
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001874 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001875}
1876
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001877static void iwl3945_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001878{
1879 unsigned long flags;
1880
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001881 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
1882 sizeof(priv->staging_rxon));
1883 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07001884 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001885
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001886 iwl3945_add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001887
1888 spin_lock_irqsave(&priv->lock, flags);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001889 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07001890 priv->error_recovering = 0;
1891 spin_unlock_irqrestore(&priv->lock, flags);
1892}
1893
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001894static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001895{
1896 u32 inta, handled = 0;
1897 u32 inta_fh;
1898 unsigned long flags;
Samuel Ortizd08853a2009-01-23 13:45:17 -08001899#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001900 u32 inta_mask;
1901#endif
1902
1903 spin_lock_irqsave(&priv->lock, flags);
1904
1905 /* Ack/clear/reset pending uCode interrupts.
1906 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1907 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001908 inta = iwl_read32(priv, CSR_INT);
1909 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001910
1911 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1912 * Any new interrupts that happen after this, either while we're
1913 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001914 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1915 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001916
Samuel Ortizd08853a2009-01-23 13:45:17 -08001917#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08001918 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001919 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001920 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Tomas Winklere1623442009-01-27 14:27:56 -08001921 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001922 inta, inta_mask, inta_fh);
1923 }
1924#endif
1925
1926 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1927 * atomic, make sure that inta covers all the interrupts that
1928 * we've discovered, even if FH interrupt came in just after
1929 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001930 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001931 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001932 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001933 inta |= CSR_INT_BIT_FH_TX;
1934
1935 /* Now service all interrupt bits discovered above. */
1936 if (inta & CSR_INT_BIT_HW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001937 IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001938
1939 /* Tell the device to stop sending interrupts */
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001940 iwl_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001941
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001942 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001943
1944 handled |= CSR_INT_BIT_HW_ERR;
1945
1946 spin_unlock_irqrestore(&priv->lock, flags);
1947
1948 return;
1949 }
1950
Samuel Ortizd08853a2009-01-23 13:45:17 -08001951#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08001952 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001953 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001954 if (inta & CSR_INT_BIT_SCD)
Tomas Winklere1623442009-01-27 14:27:56 -08001955 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
Joonwoo Park25c03d82008-01-23 10:15:20 -08001956 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001957
1958 /* Alive notification via Rx interrupt will do the real work */
1959 if (inta & CSR_INT_BIT_ALIVE)
Tomas Winklere1623442009-01-27 14:27:56 -08001960 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
Zhu Yib481de92007-09-25 17:54:57 -07001961 }
1962#endif
1963 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001964 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001965
Zhu Yib481de92007-09-25 17:54:57 -07001966 /* Error detected by uCode */
1967 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001968 IWL_ERR(priv, "Microcode SW error detected. "
1969 "Restarting 0x%X.\n", inta);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001970 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001971 handled |= CSR_INT_BIT_SW_ERR;
1972 }
1973
1974 /* uCode wakes up after power-down sleep */
1975 if (inta & CSR_INT_BIT_WAKEUP) {
Tomas Winklere1623442009-01-27 14:27:56 -08001976 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08001977 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08001978 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1979 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1980 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1981 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1982 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1983 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07001984
1985 handled |= CSR_INT_BIT_WAKEUP;
1986 }
1987
1988 /* All uCode command responses, including Tx command responses,
1989 * Rx "responses" (frame-received notification), and other
1990 * notifications from uCode come through here*/
1991 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001992 iwl3945_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001993 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1994 }
1995
1996 if (inta & CSR_INT_BIT_FH_TX) {
Tomas Winklere1623442009-01-27 14:27:56 -08001997 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
Zhu Yib481de92007-09-25 17:54:57 -07001998
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001999 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
2000 if (!iwl_grab_nic_access(priv)) {
2001 iwl_write_direct32(priv, FH39_TCSR_CREDIT
Tomas Winklerbddadf82008-12-19 10:37:01 +08002002 (FH39_SRVC_CHNL), 0x0);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002003 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002004 }
2005 handled |= CSR_INT_BIT_FH_TX;
2006 }
2007
2008 if (inta & ~handled)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002009 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Zhu Yib481de92007-09-25 17:54:57 -07002010
2011 if (inta & ~CSR_INI_SET_MASK) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002012 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Zhu Yib481de92007-09-25 17:54:57 -07002013 inta & ~CSR_INI_SET_MASK);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002014 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07002015 }
2016
2017 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002018 /* only Re-enable if disabled by irq */
2019 if (test_bit(STATUS_INT_ENABLED, &priv->status))
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002020 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002021
Samuel Ortizd08853a2009-01-23 13:45:17 -08002022#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002023 if (priv->debug_level & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002024 inta = iwl_read32(priv, CSR_INT);
2025 inta_mask = iwl_read32(priv, CSR_INT_MASK);
2026 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Tomas Winklere1623442009-01-27 14:27:56 -08002027 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
Zhu Yib481de92007-09-25 17:54:57 -07002028 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
2029 }
2030#endif
2031 spin_unlock_irqrestore(&priv->lock, flags);
2032}
2033
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002034static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002035 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002036 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002037 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07002038{
2039 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01002040 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002041 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002042 u16 passive_dwell = 0;
2043 u16 active_dwell = 0;
2044 int added, i;
2045
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08002046 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01002047 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07002048 return 0;
2049
Johannes Berg8318d782008-01-24 19:38:38 +01002050 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07002051
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08002052 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
2053 passive_dwell = iwl_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07002054
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08002055 if (passive_dwell <= active_dwell)
2056 passive_dwell = active_dwell + 1;
2057
Johannes Berg8318d782008-01-24 19:38:38 +01002058 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02002059 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
2060 continue;
2061
Johannes Berg8318d782008-01-24 19:38:38 +01002062 scan_ch->channel = channels[i].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07002063
Samuel Ortize6148912009-01-23 13:45:15 -08002064 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07002065 if (!is_channel_valid(ch_info)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002066 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07002067 scan_ch->channel);
2068 continue;
2069 }
2070
Zhu Yib481de92007-09-25 17:54:57 -07002071 scan_ch->active_dwell = cpu_to_le16(active_dwell);
2072 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08002073 /* If passive , set up for auto-switch
2074 * and use long active_dwell time.
2075 */
2076 if (!is_active || is_channel_passive(ch_info) ||
2077 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
2078 scan_ch->type = 0; /* passive */
2079 if (IWL_UCODE_API(priv->ucode_ver) == 1)
2080 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
2081 } else {
2082 scan_ch->type = 1; /* active */
2083 }
2084
2085 /* Set direct probe bits. These may be used both for active
2086 * scan channels (probes gets sent right away),
2087 * or for passive channels (probes get se sent only after
2088 * hearing clear Rx packet).*/
2089 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
2090 if (n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08002091 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08002092 } else {
2093 /* uCode v1 does not allow setting direct probe bits on
2094 * passive channel. */
2095 if ((scan_ch->type & 1) && n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08002096 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08002097 }
Zhu Yib481de92007-09-25 17:54:57 -07002098
Ben Cahill9fbab512007-11-29 11:09:47 +08002099 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07002100 scan_ch->tpc.dsp_atten = 110;
2101 /* scan_pwr_info->tpc.dsp_atten; */
2102
2103 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01002104 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07002105 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
2106 else {
2107 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
2108 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08002109 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08002110 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07002111 */
2112 }
2113
Tomas Winklere1623442009-01-27 14:27:56 -08002114 IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
Zhu Yib481de92007-09-25 17:54:57 -07002115 scan_ch->channel,
2116 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
2117 (scan_ch->type & 1) ?
2118 active_dwell : passive_dwell);
2119
2120 scan_ch++;
2121 added++;
2122 }
2123
Tomas Winklere1623442009-01-27 14:27:56 -08002124 IWL_DEBUG_SCAN(priv, "total channels to scan %d \n", added);
Zhu Yib481de92007-09-25 17:54:57 -07002125 return added;
2126}
2127
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002128static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002129 struct ieee80211_rate *rates)
2130{
2131 int i;
2132
2133 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01002134 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
2135 rates[i].hw_value = i; /* Rate scaling will work on indexes */
2136 rates[i].hw_value_short = i;
2137 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08002138 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07002139 /*
Johannes Berg8318d782008-01-24 19:38:38 +01002140 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07002141 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002142 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01002143 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07002144 }
Zhu Yib481de92007-09-25 17:54:57 -07002145 }
2146}
2147
Zhu Yib481de92007-09-25 17:54:57 -07002148/******************************************************************************
2149 *
2150 * uCode download functions
2151 *
2152 ******************************************************************************/
2153
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002154static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002155{
Tomas Winkler98c92212008-01-14 17:46:20 -08002156 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
2157 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
2158 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
2159 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
2160 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
2161 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07002162}
2163
2164/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002165 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07002166 * looking at all data.
2167 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002168static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07002169{
2170 u32 val;
2171 u32 save_len = len;
2172 int rc = 0;
2173 u32 errcnt;
2174
Tomas Winklere1623442009-01-27 14:27:56 -08002175 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002176
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002177 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002178 if (rc)
2179 return rc;
2180
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002181 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002182 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07002183
2184 errcnt = 0;
2185 for (; len > 0; len -= sizeof(u32), image++) {
2186 /* read data comes through single port, auto-incr addr */
2187 /* NOTE: Use the debugless read so we don't flood kernel log
2188 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002189 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07002190 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002191 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07002192 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2193 save_len - len, val, le32_to_cpu(*image));
2194 rc = -EIO;
2195 errcnt++;
2196 if (errcnt >= 20)
2197 break;
2198 }
2199 }
2200
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002201 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002202
2203 if (!errcnt)
Tomas Winklere1623442009-01-27 14:27:56 -08002204 IWL_DEBUG_INFO(priv,
2205 "ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07002206
2207 return rc;
2208}
2209
2210
2211/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002212 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07002213 * using sample data 100 bytes apart. If these sample points are good,
2214 * it's a pretty good bet that everything between them is good, too.
2215 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002216static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07002217{
2218 u32 val;
2219 int rc = 0;
2220 u32 errcnt = 0;
2221 u32 i;
2222
Tomas Winklere1623442009-01-27 14:27:56 -08002223 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002224
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002225 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002226 if (rc)
2227 return rc;
2228
2229 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
2230 /* read data comes through single port, auto-incr addr */
2231 /* NOTE: Use the debugless read so we don't flood kernel log
2232 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002233 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002234 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002235 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07002236 if (val != le32_to_cpu(*image)) {
2237#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002238 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07002239 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2240 i, val, *image);
2241#endif
2242 rc = -EIO;
2243 errcnt++;
2244 if (errcnt >= 3)
2245 break;
2246 }
2247 }
2248
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002249 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002250
2251 return rc;
2252}
2253
2254
2255/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002256 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07002257 * and verify its contents
2258 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002259static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002260{
2261 __le32 *image;
2262 u32 len;
2263 int rc = 0;
2264
2265 /* Try bootstrap */
2266 image = (__le32 *)priv->ucode_boot.v_addr;
2267 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002268 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002269 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002270 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002271 return 0;
2272 }
2273
2274 /* Try initialize */
2275 image = (__le32 *)priv->ucode_init.v_addr;
2276 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002277 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002278 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002279 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002280 return 0;
2281 }
2282
2283 /* Try runtime/protocol */
2284 image = (__le32 *)priv->ucode_code.v_addr;
2285 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002286 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002287 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002288 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002289 return 0;
2290 }
2291
Winkler, Tomas15b16872008-12-19 10:37:33 +08002292 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07002293
Ben Cahill9fbab512007-11-29 11:09:47 +08002294 /* Since nothing seems to match, show first several data entries in
2295 * instruction SRAM, so maybe visual inspection will give a clue.
2296 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07002297 image = (__le32 *)priv->ucode_boot.v_addr;
2298 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002299 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002300
2301 return rc;
2302}
2303
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002304static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002305{
2306 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002307 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002308}
2309
2310/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002311 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07002312 *
2313 * Copy into buffers for card to fetch via bus-mastering
2314 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002315static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002316{
Kolekar, Abhijeeta78fe752008-12-19 10:37:21 +08002317 struct iwl_ucode *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002318 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07002319 const struct firmware *ucode_raw;
2320 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002321 const char *name_pre = priv->cfg->fw_name_pre;
2322 const unsigned int api_max = priv->cfg->ucode_api_max;
2323 const unsigned int api_min = priv->cfg->ucode_api_min;
2324 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07002325 u8 *src;
2326 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002327 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07002328
2329 /* Ask kernel firmware_class module to get the boot firmware off disk.
2330 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002331 for (index = api_max; index >= api_min; index--) {
2332 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
2333 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
2334 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002335 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002336 buf, ret);
2337 if (ret == -ENOENT)
2338 continue;
2339 else
2340 goto error;
2341 } else {
2342 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002343 IWL_ERR(priv, "Loaded firmware %s, "
2344 "which is deprecated. "
2345 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002346 buf, api_max);
Tomas Winklere1623442009-01-27 14:27:56 -08002347 IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
2348 "(%zd bytes) from disk\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002349 buf, ucode_raw->size);
2350 break;
2351 }
Zhu Yib481de92007-09-25 17:54:57 -07002352 }
2353
Reinette Chatrea0987a82008-12-02 12:14:06 -08002354 if (ret < 0)
2355 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07002356
2357 /* Make sure that we got at least our header! */
2358 if (ucode_raw->size < sizeof(*ucode)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002359 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002360 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002361 goto err_release;
2362 }
2363
2364 /* Data from ucode file: header followed by uCode images */
2365 ucode = (void *)ucode_raw->data;
2366
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08002367 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08002368 api_ver = IWL_UCODE_API(priv->ucode_ver);
Zhu Yib481de92007-09-25 17:54:57 -07002369 inst_size = le32_to_cpu(ucode->inst_size);
2370 data_size = le32_to_cpu(ucode->data_size);
2371 init_size = le32_to_cpu(ucode->init_size);
2372 init_data_size = le32_to_cpu(ucode->init_data_size);
2373 boot_size = le32_to_cpu(ucode->boot_size);
2374
Reinette Chatrea0987a82008-12-02 12:14:06 -08002375 /* api_ver should match the api version forming part of the
2376 * firmware filename ... but we don't check for that and only rely
Nick Andrew877d0312009-01-26 11:06:57 +01002377 * on the API version read from firmware header from here on forward */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002378
2379 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002380 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002381 "Driver supports v%u, firmware is v%u.\n",
2382 api_max, api_ver);
2383 priv->ucode_ver = 0;
2384 ret = -EINVAL;
2385 goto err_release;
2386 }
2387 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002388 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002389 "got %u. New firmware can be obtained "
2390 "from http://www.intellinuxwireless.org.\n",
2391 api_max, api_ver);
2392
Tomas Winkler978785a2008-12-19 10:37:31 +08002393 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
2394 IWL_UCODE_MAJOR(priv->ucode_ver),
2395 IWL_UCODE_MINOR(priv->ucode_ver),
2396 IWL_UCODE_API(priv->ucode_ver),
2397 IWL_UCODE_SERIAL(priv->ucode_ver));
2398
Tomas Winklere1623442009-01-27 14:27:56 -08002399 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002400 priv->ucode_ver);
Tomas Winklere1623442009-01-27 14:27:56 -08002401 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
2402 inst_size);
2403 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
2404 data_size);
2405 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
2406 init_size);
2407 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
2408 init_data_size);
2409 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
2410 boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07002411
Reinette Chatrea0987a82008-12-02 12:14:06 -08002412
Zhu Yib481de92007-09-25 17:54:57 -07002413 /* Verify size of file vs. image size info in file's header */
2414 if (ucode_raw->size < sizeof(*ucode) +
2415 inst_size + data_size + init_size +
2416 init_data_size + boot_size) {
2417
Tomas Winklere1623442009-01-27 14:27:56 -08002418 IWL_DEBUG_INFO(priv, "uCode file size %zd too small\n",
2419 ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002420 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002421 goto err_release;
2422 }
2423
2424 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002425 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002426 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002427 inst_size);
2428 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002429 goto err_release;
2430 }
2431
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002432 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002433 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002434 data_size);
2435 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002436 goto err_release;
2437 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002438 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002439 IWL_DEBUG_INFO(priv,
2440 "uCode init instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002441 init_size);
2442 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002443 goto err_release;
2444 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002445 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002446 IWL_DEBUG_INFO(priv,
2447 "uCode init data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002448 init_data_size);
2449 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002450 goto err_release;
2451 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002452 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002453 IWL_DEBUG_INFO(priv,
2454 "uCode boot instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002455 boot_size);
2456 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002457 goto err_release;
2458 }
2459
2460 /* Allocate ucode buffers for card's bus-master loading ... */
2461
2462 /* Runtime instructions and 2 copies of data:
2463 * 1) unmodified from disk
2464 * 2) backup cache for save/restore during power-downs */
2465 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002466 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07002467
2468 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002469 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07002470
2471 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002472 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07002473
2474 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08002475 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07002476 goto err_pci_alloc;
2477
Tomas Winkler90e759d2007-11-29 11:09:41 +08002478 /* Initialization instructions and data */
2479 if (init_size && init_data_size) {
2480 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002481 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002482
2483 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002484 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002485
2486 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2487 goto err_pci_alloc;
2488 }
2489
2490 /* Bootstrap (instructions only, no data) */
2491 if (boot_size) {
2492 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002493 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002494
2495 if (!priv->ucode_boot.v_addr)
2496 goto err_pci_alloc;
2497 }
2498
Zhu Yib481de92007-09-25 17:54:57 -07002499 /* Copy images into buffers for card's bus-master reads ... */
2500
2501 /* Runtime instructions (first block of data in file) */
2502 src = &ucode->data[0];
2503 len = priv->ucode_code.len;
Tomas Winklere1623442009-01-27 14:27:56 -08002504 IWL_DEBUG_INFO(priv,
2505 "Copying (but not loading) uCode instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002506 memcpy(priv->ucode_code.v_addr, src, len);
Tomas Winklere1623442009-01-27 14:27:56 -08002507 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07002508 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2509
2510 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002511 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Zhu Yib481de92007-09-25 17:54:57 -07002512 src = &ucode->data[inst_size];
2513 len = priv->ucode_data.len;
Tomas Winklere1623442009-01-27 14:27:56 -08002514 IWL_DEBUG_INFO(priv,
2515 "Copying (but not loading) uCode data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002516 memcpy(priv->ucode_data.v_addr, src, len);
2517 memcpy(priv->ucode_data_backup.v_addr, src, len);
2518
2519 /* Initialization instructions (3rd block) */
2520 if (init_size) {
2521 src = &ucode->data[inst_size + data_size];
2522 len = priv->ucode_init.len;
Tomas Winklere1623442009-01-27 14:27:56 -08002523 IWL_DEBUG_INFO(priv,
2524 "Copying (but not loading) init instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002525 memcpy(priv->ucode_init.v_addr, src, len);
2526 }
2527
2528 /* Initialization data (4th block) */
2529 if (init_data_size) {
2530 src = &ucode->data[inst_size + data_size + init_size];
2531 len = priv->ucode_init_data.len;
Tomas Winklere1623442009-01-27 14:27:56 -08002532 IWL_DEBUG_INFO(priv,
2533 "Copying (but not loading) init data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002534 memcpy(priv->ucode_init_data.v_addr, src, len);
2535 }
2536
2537 /* Bootstrap instructions (5th block) */
2538 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
2539 len = priv->ucode_boot.len;
Tomas Winklere1623442009-01-27 14:27:56 -08002540 IWL_DEBUG_INFO(priv,
2541 "Copying (but not loading) boot instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002542 memcpy(priv->ucode_boot.v_addr, src, len);
2543
2544 /* We have our copies now, allow OS release its copies */
2545 release_firmware(ucode_raw);
2546 return 0;
2547
2548 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08002549 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002550 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002551 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002552
2553 err_release:
2554 release_firmware(ucode_raw);
2555
2556 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08002557 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002558}
2559
2560
2561/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002562 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07002563 *
2564 * Tell initialization uCode where to find runtime uCode.
2565 *
2566 * BSM registers initially contain pointers to initialization uCode.
2567 * We need to replace them to load runtime uCode inst and data,
2568 * and to save runtime data when powering down.
2569 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002570static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002571{
2572 dma_addr_t pinst;
2573 dma_addr_t pdata;
2574 int rc = 0;
2575 unsigned long flags;
2576
2577 /* bits 31:0 for 3945 */
2578 pinst = priv->ucode_code.p_addr;
2579 pdata = priv->ucode_data_backup.p_addr;
2580
2581 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002582 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002583 if (rc) {
2584 spin_unlock_irqrestore(&priv->lock, flags);
2585 return rc;
2586 }
2587
2588 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002589 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
2590 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
2591 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002592 priv->ucode_data.len);
2593
Tomas Winklera96a27f2008-10-23 23:48:56 -07002594 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07002595 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002596 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002597 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
2598
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002599 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002600
2601 spin_unlock_irqrestore(&priv->lock, flags);
2602
Tomas Winklere1623442009-01-27 14:27:56 -08002603 IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002604
2605 return rc;
2606}
2607
2608/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002609 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002610 *
2611 * Called after REPLY_ALIVE notification received from "initialize" uCode.
2612 *
Zhu Yib481de92007-09-25 17:54:57 -07002613 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08002614 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002615static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002616{
2617 /* Check alive response for "valid" sign from uCode */
2618 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
2619 /* We had an error bringing up the hardware, so take it
2620 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002621 IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002622 goto restart;
2623 }
2624
2625 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
2626 * This is a paranoid check, because we would not have gotten the
2627 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002628 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002629 /* Runtime instruction load was bad;
2630 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002631 IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002632 goto restart;
2633 }
2634
2635 /* Send pointers to protocol/runtime uCode image ... init code will
2636 * load and launch runtime uCode, which will send us another "Alive"
2637 * notification. */
Tomas Winklere1623442009-01-27 14:27:56 -08002638 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002639 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002640 /* Runtime instruction load won't happen;
2641 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002642 IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002643 goto restart;
2644 }
2645 return;
2646
2647 restart:
2648 queue_work(priv->workqueue, &priv->restart);
2649}
2650
Zhu Yib481de92007-09-25 17:54:57 -07002651/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002652 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002653 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002654 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002655 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002656static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002657{
2658 int rc = 0;
2659 int thermal_spin = 0;
2660 u32 rfkill;
2661
Tomas Winklere1623442009-01-27 14:27:56 -08002662 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002663
2664 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2665 /* We had an error bringing up the hardware, so take it
2666 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002667 IWL_DEBUG_INFO(priv, "Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002668 goto restart;
2669 }
2670
2671 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2672 * This is a paranoid check, because we would not have gotten the
2673 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002674 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002675 /* Runtime instruction load was bad;
2676 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002677 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002678 goto restart;
2679 }
2680
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002681 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002682
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002683 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002684 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002685 IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
Zhu Yib481de92007-09-25 17:54:57 -07002686 return;
2687 }
2688
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002689 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Tomas Winklere1623442009-01-27 14:27:56 -08002690 IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002691 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002692
2693 if (rfkill & 0x1) {
2694 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002695 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07002696 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002697 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07002698 thermal_spin++;
2699 udelay(10);
2700 }
2701
2702 if (thermal_spin)
Tomas Winklere1623442009-01-27 14:27:56 -08002703 IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
Zhu Yib481de92007-09-25 17:54:57 -07002704 thermal_spin * 10);
2705 } else
2706 set_bit(STATUS_RF_KILL_HW, &priv->status);
2707
Ben Cahill9fbab512007-11-29 11:09:47 +08002708 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002709 set_bit(STATUS_ALIVE, &priv->status);
2710
2711 /* Clear out the uCode error bit if it is set */
2712 clear_bit(STATUS_FW_ERROR, &priv->status);
2713
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002714 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002715 return;
2716
Johannes Berg36d68252008-05-15 12:55:26 +02002717 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002718
2719 priv->active_rate = priv->rates_mask;
2720 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2721
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08002722 iwl_power_update_mode(priv, false);
Zhu Yib481de92007-09-25 17:54:57 -07002723
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002724 if (iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002725 struct iwl3945_rxon_cmd *active_rxon =
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002726 (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07002727
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002728 memcpy(&priv->staging_rxon, &priv->active_rxon,
2729 sizeof(priv->staging_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07002730 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2731 } else {
2732 /* Initialize our rx_config data */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002733 iwl_connection_init_rx_config(priv, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002734 }
2735
Ben Cahill9fbab512007-11-29 11:09:47 +08002736 /* Configure Bluetooth device coexistence support */
Samuel Ortiz17f841c2009-01-23 13:45:20 -08002737 iwl_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002738
2739 /* Configure the adapter for unassociated operation */
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002740 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002741
Zhu Yib481de92007-09-25 17:54:57 -07002742 iwl3945_reg_txpower_periodic(priv);
2743
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002744 iwl3945_led_register(priv);
2745
Tomas Winklere1623442009-01-27 14:27:56 -08002746 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002747 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002748 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002749
2750 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002751 iwl3945_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002752
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08002753 /* reassociate for ADHOC mode */
2754 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
2755 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
2756 priv->vif);
2757 if (beacon)
Abhijeet Kolekar9944b932009-04-08 11:26:36 -07002758 iwl_mac_beacon_update(priv->hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08002759 }
2760
Abhijeet Kolekarf45c2712009-04-08 11:26:38 -07002761 if (test_and_clear_bit(STATUS_MODE_PENDING, &priv->status))
2762 iwl3945_set_mode(priv, priv->iw_mode);
2763
Zhu Yib481de92007-09-25 17:54:57 -07002764 return;
2765
2766 restart:
2767 queue_work(priv->workqueue, &priv->restart);
2768}
2769
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002770static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002771
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002772static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002773{
2774 unsigned long flags;
2775 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
2776 struct ieee80211_conf *conf = NULL;
2777
Tomas Winklere1623442009-01-27 14:27:56 -08002778 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
Zhu Yib481de92007-09-25 17:54:57 -07002779
2780 conf = ieee80211_get_hw_conf(priv->hw);
2781
2782 if (!exit_pending)
2783 set_bit(STATUS_EXIT_PENDING, &priv->status);
2784
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002785 iwl3945_led_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002786 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002787
2788 /* Unblock any waiting calls */
2789 wake_up_interruptible_all(&priv->wait_command_queue);
2790
Zhu Yib481de92007-09-25 17:54:57 -07002791 /* Wipe out the EXIT_PENDING status bit if we are not actually
2792 * exiting the module */
2793 if (!exit_pending)
2794 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2795
2796 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002797 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002798
2799 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002800 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002801 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002802 spin_unlock_irqrestore(&priv->lock, flags);
2803 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002804
2805 if (priv->mac80211_registered)
2806 ieee80211_stop_queues(priv->hw);
2807
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002808 /* If we have not previously called iwl3945_init() then
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002809 * clear all bits but the RF Kill bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002810 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002811 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2812 STATUS_RF_KILL_HW |
2813 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2814 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08002815 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2816 STATUS_GEO_CONFIGURED |
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002817 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2818 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002819 goto exit;
2820 }
2821
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002822 /* ...otherwise clear out all the status bits but the RF Kill
2823 * bits and continue taking the NIC down. */
Zhu Yib481de92007-09-25 17:54:57 -07002824 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2825 STATUS_RF_KILL_HW |
2826 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2827 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08002828 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2829 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002830 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002831 STATUS_FW_ERROR |
2832 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2833 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002834
Abbas, Mohamede9414b62009-01-20 21:33:55 -08002835 priv->cfg->ops->lib->apm_ops.reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002836 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002837 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07002838 spin_unlock_irqrestore(&priv->lock, flags);
2839
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002840 iwl3945_hw_txq_ctx_stop(priv);
2841 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002842
2843 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002844 if (!iwl_grab_nic_access(priv)) {
2845 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002846 APMG_CLK_VAL_DMA_CLK_RQT);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002847 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002848 }
2849 spin_unlock_irqrestore(&priv->lock, flags);
2850
2851 udelay(5);
2852
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002853 if (exit_pending)
Abbas, Mohamede9414b62009-01-20 21:33:55 -08002854 priv->cfg->ops->lib->apm_ops.stop(priv);
2855 else
2856 priv->cfg->ops->lib->apm_ops.reset(priv);
2857
Zhu Yib481de92007-09-25 17:54:57 -07002858 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002859 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002860
2861 if (priv->ibss_beacon)
2862 dev_kfree_skb(priv->ibss_beacon);
2863 priv->ibss_beacon = NULL;
2864
2865 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002866 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002867}
2868
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002869static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002870{
2871 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002872 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002873 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002874
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002875 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002876}
2877
2878#define MAX_HW_RESTARTS 5
2879
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002880static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002881{
2882 int rc, i;
2883
2884 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002885 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002886 return -EIO;
2887 }
2888
2889 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002890 IWL_WARN(priv, "Radio disabled by SW RF kill (module "
Zhu Yib481de92007-09-25 17:54:57 -07002891 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08002892 return -ENODEV;
2893 }
2894
Reinette Chatree903fbd2008-01-30 22:05:15 -08002895 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002896 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08002897 return -EIO;
2898 }
2899
Zhu Yie655b9f2008-01-24 02:19:38 -08002900 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002901 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08002902 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2903 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2904 else {
2905 set_bit(STATUS_RF_KILL_HW, &priv->status);
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002906 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
2907 return -ENODEV;
Zhu Yib481de92007-09-25 17:54:57 -07002908 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02002909
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002910 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002911
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002912 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002913 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002914 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07002915 return rc;
2916 }
2917
2918 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002919 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2920 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002921 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2922
2923 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002924 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002925 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002926
2927 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002928 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2929 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002930
2931 /* Copy original ucode data image from disk into backup cache.
2932 * This will be used to initialize the on-board processor's
2933 * data SRAM for a clean start when the runtime program first loads. */
2934 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08002935 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002936
Zhu Yie655b9f2008-01-24 02:19:38 -08002937 /* We return success when we resume from suspend and rf_kill is on. */
2938 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
2939 return 0;
2940
Zhu Yib481de92007-09-25 17:54:57 -07002941 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2942
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002943 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002944
2945 /* load bootstrap state machine,
2946 * load bootstrap program into processor's memory,
2947 * prepare to load the "initialize" uCode */
Kolekar, Abhijeet0164b9b2008-12-19 10:37:37 +08002948 priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002949
2950 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002951 IWL_ERR(priv,
2952 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07002953 continue;
2954 }
2955
2956 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002957 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002958
Tomas Winklere1623442009-01-27 14:27:56 -08002959 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002960
2961 return 0;
2962 }
2963
2964 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002965 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002966 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002967
2968 /* tried to restart and config the device for as long as our
2969 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002970 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07002971 return -EIO;
2972}
2973
2974
2975/*****************************************************************************
2976 *
2977 * Workqueue callbacks
2978 *
2979 *****************************************************************************/
2980
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002981static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002982{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002983 struct iwl_priv *priv =
2984 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002985
2986 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2987 return;
2988
2989 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002990 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002991 mutex_unlock(&priv->mutex);
2992}
2993
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002994static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002995{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002996 struct iwl_priv *priv =
2997 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002998
2999 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3000 return;
3001
3002 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003003 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003004 mutex_unlock(&priv->mutex);
3005}
3006
Helmut Schaa26635162009-01-15 09:38:44 +01003007static void iwl3945_rfkill_poll(struct work_struct *data)
3008{
3009 struct iwl_priv *priv =
3010 container_of(data, struct iwl_priv, rfkill_poll.work);
3011 unsigned long status = priv->status;
3012
3013 if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3014 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3015 else
3016 set_bit(STATUS_RF_KILL_HW, &priv->status);
3017
3018 if (test_bit(STATUS_RF_KILL_HW, &status) != test_bit(STATUS_RF_KILL_HW, &priv->status))
3019 queue_work(priv->workqueue, &priv->rf_kill);
3020
3021 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
3022 round_jiffies_relative(2 * HZ));
3023
3024}
3025
Zhu Yib481de92007-09-25 17:54:57 -07003026#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003027static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003028{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003029 struct iwl_priv *priv =
3030 container_of(data, struct iwl_priv, request_scan);
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08003031 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003032 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003033 .len = sizeof(struct iwl3945_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07003034 .meta.flags = CMD_SIZE_HUGE,
3035 };
3036 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003037 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07003038 struct ieee80211_conf *conf = NULL;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08003039 u8 n_probes = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01003040 enum ieee80211_band band;
John W. Linville9387b7c2008-09-30 20:59:05 -04003041 DECLARE_SSID_BUF(ssid);
Zhu Yib481de92007-09-25 17:54:57 -07003042
3043 conf = ieee80211_get_hw_conf(priv->hw);
3044
3045 mutex_lock(&priv->mutex);
3046
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003047 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003048 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003049 goto done;
3050 }
3051
Tomas Winklera96a27f2008-10-23 23:48:56 -07003052 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07003053 * was given the chance to run... */
3054 if (!test_bit(STATUS_SCANNING, &priv->status))
3055 goto done;
3056
3057 /* This should never be called or scheduled if there is currently
3058 * a scan active in the hardware. */
3059 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003060 IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests "
3061 "Ignoring second request.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003062 rc = -EIO;
3063 goto done;
3064 }
3065
3066 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003067 IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
Zhu Yib481de92007-09-25 17:54:57 -07003068 goto done;
3069 }
3070
3071 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003072 IWL_DEBUG_HC(priv,
3073 "Scan request while abort pending. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003074 goto done;
3075 }
3076
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003077 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003078 IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
Zhu Yib481de92007-09-25 17:54:57 -07003079 goto done;
3080 }
3081
3082 if (!test_bit(STATUS_READY, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003083 IWL_DEBUG_HC(priv,
3084 "Scan request while uninitialized. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003085 goto done;
3086 }
3087
3088 if (!priv->scan_bands) {
Tomas Winklere1623442009-01-27 14:27:56 -08003089 IWL_DEBUG_HC(priv, "Aborting scan due to no requested bands\n");
Zhu Yib481de92007-09-25 17:54:57 -07003090 goto done;
3091 }
3092
Winkler, Tomas805cee52009-01-19 15:30:28 -08003093 if (!priv->scan) {
3094 priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07003095 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
Winkler, Tomas805cee52009-01-19 15:30:28 -08003096 if (!priv->scan) {
Zhu Yib481de92007-09-25 17:54:57 -07003097 rc = -ENOMEM;
3098 goto done;
3099 }
3100 }
Winkler, Tomas805cee52009-01-19 15:30:28 -08003101 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003102 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07003103
3104 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
3105 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
3106
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003107 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003108 u16 interval = 0;
3109 u32 extra;
3110 u32 suspend_time = 100;
3111 u32 scan_suspend_time = 100;
3112 unsigned long flags;
3113
Tomas Winklere1623442009-01-27 14:27:56 -08003114 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
Zhu Yib481de92007-09-25 17:54:57 -07003115
3116 spin_lock_irqsave(&priv->lock, flags);
3117 interval = priv->beacon_int;
3118 spin_unlock_irqrestore(&priv->lock, flags);
3119
3120 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003121 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07003122 if (!interval)
3123 interval = suspend_time;
3124 /*
3125 * suspend time format:
3126 * 0-19: beacon interval in usec (time before exec.)
3127 * 20-23: 0
3128 * 24-31: number of beacons (suspend between channels)
3129 */
3130
3131 extra = (suspend_time / interval) << 24;
3132 scan_suspend_time = 0xFF0FFFFF &
3133 (extra | ((suspend_time % interval) * 1024));
3134
3135 scan->suspend_time = cpu_to_le32(scan_suspend_time);
Tomas Winklere1623442009-01-27 14:27:56 -08003136 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003137 scan_suspend_time, interval);
3138 }
3139
3140 /* We should add the ability for user to lock to PASSIVE ONLY */
3141 if (priv->one_direct_scan) {
Tomas Winklere1623442009-01-27 14:27:56 -08003142 IWL_DEBUG_SCAN(priv, "Kicking off one direct scan for '%s'\n",
3143 print_ssid(ssid, priv->direct_ssid,
John W. Linville9387b7c2008-09-30 20:59:05 -04003144 priv->direct_ssid_len));
Zhu Yib481de92007-09-25 17:54:57 -07003145 scan->direct_scan[0].id = WLAN_EID_SSID;
3146 scan->direct_scan[0].len = priv->direct_ssid_len;
3147 memcpy(scan->direct_scan[0].ssid,
3148 priv->direct_ssid, priv->direct_ssid_len);
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08003149 n_probes++;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08003150 } else
Tomas Winklere1623442009-01-27 14:27:56 -08003151 IWL_DEBUG_SCAN(priv, "Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003152
3153 /* We don't build a direct scan probe request; the uCode will do
3154 * that based on the direct_mask added to each channel entry */
Zhu Yib481de92007-09-25 17:54:57 -07003155 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08003156 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07003157 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
3158
3159 /* flags + rate selection */
3160
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003161 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07003162 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
3163 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
3164 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003165 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003166 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07003167 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
3168 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01003169 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003170 } else {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003171 IWL_WARN(priv, "Invalid scan band count\n");
Zhu Yib481de92007-09-25 17:54:57 -07003172 goto done;
3173 }
3174
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08003175 scan->tx_cmd.len = cpu_to_le16(
3176 iwl_fill_probe_req(priv, band,
3177 (struct ieee80211_mgmt *)scan->data,
3178 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
3179
Zhu Yib481de92007-09-25 17:54:57 -07003180 /* select Rx antennas */
3181 scan->flags |= iwl3945_get_antenna_flags(priv);
3182
Johannes Berg05c914f2008-09-11 00:01:58 +02003183 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Zhu Yib481de92007-09-25 17:54:57 -07003184 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
3185
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08003186 scan->channel_count =
3187 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
3188 n_probes,
3189 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07003190
Reinette Chatre14b54332008-11-04 12:21:35 -08003191 if (scan->channel_count == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08003192 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
Reinette Chatre14b54332008-11-04 12:21:35 -08003193 goto done;
3194 }
3195
Zhu Yib481de92007-09-25 17:54:57 -07003196 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003197 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07003198 cmd.data = scan;
3199 scan->len = cpu_to_le16(cmd.len);
3200
3201 set_bit(STATUS_SCAN_HW, &priv->status);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003202 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003203 if (rc)
3204 goto done;
3205
3206 queue_delayed_work(priv->workqueue, &priv->scan_check,
3207 IWL_SCAN_CHECK_WATCHDOG);
3208
3209 mutex_unlock(&priv->mutex);
3210 return;
3211
3212 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08003213 /* can not perform scan make sure we clear scanning
3214 * bits from status so next scan request can be performed.
3215 * if we dont clear scanning status bit here all next scan
3216 * will fail
3217 */
3218 clear_bit(STATUS_SCAN_HW, &priv->status);
3219 clear_bit(STATUS_SCANNING, &priv->status);
3220
Ian Schram01ebd062007-10-25 17:15:22 +08003221 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07003222 queue_work(priv->workqueue, &priv->scan_completed);
3223 mutex_unlock(&priv->mutex);
3224}
3225
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003226static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003227{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003228 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07003229
3230 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3231 return;
3232
3233 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003234 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003235 mutex_unlock(&priv->mutex);
Samuel Ortizc0af96a2009-01-21 18:27:54 +01003236 iwl_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003237}
3238
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003239static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003240{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003241 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07003242
3243 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3244 return;
3245
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003246 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003247 queue_work(priv->workqueue, &priv->up);
3248}
3249
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003250static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003251{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003252 struct iwl_priv *priv =
3253 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07003254
3255 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3256 return;
3257
3258 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003259 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003260 mutex_unlock(&priv->mutex);
3261}
3262
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07003263static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
3264
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003265#define IWL_DELAY_NEXT_SCAN (HZ*2)
3266
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07003267void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003268{
Zhu Yib481de92007-09-25 17:54:57 -07003269 int rc = 0;
3270 struct ieee80211_conf *conf = NULL;
3271
Johannes Berg05c914f2008-09-11 00:01:58 +02003272 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003273 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07003274 return;
3275 }
3276
3277
Tomas Winklere1623442009-01-27 14:27:56 -08003278 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003279 priv->assoc_id, priv->active_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003280
3281 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3282 return;
3283
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08003284 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003285 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08003286
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003287 iwl_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003288
Zhu Yib481de92007-09-25 17:54:57 -07003289 conf = ieee80211_get_hw_conf(priv->hw);
3290
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003291 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003292 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003293
Tomas Winkler28afaf92008-12-19 10:37:06 +08003294 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003295 iwl3945_setup_rxon_timing(priv);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003296 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07003297 sizeof(priv->rxon_timing), &priv->rxon_timing);
3298 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003299 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003300 "Attempting to continue.\n");
3301
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003302 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003303
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003304 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07003305
Tomas Winklere1623442009-01-27 14:27:56 -08003306 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003307 priv->assoc_id, priv->beacon_int);
3308
3309 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003310 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003311 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003312 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003313
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003314 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07003315 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003316 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003317 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003318 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003319
Johannes Berg05c914f2008-09-11 00:01:58 +02003320 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003321 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003322
3323 }
3324
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003325 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003326
3327 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003328 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003329 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003330 break;
3331
Johannes Berg05c914f2008-09-11 00:01:58 +02003332 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07003333
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08003334 priv->assoc_id = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003335 iwl3945_add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003336 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01003337 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07003338 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
3339 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003340 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
3341 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003342
3343 break;
3344
3345 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08003346 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003347 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07003348 break;
3349 }
3350
Abhijeet Kolekar14d2aac2009-02-27 16:21:24 -08003351 iwl_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08003352
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003353 /* we have just associated, don't start scan too early */
3354 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08003355}
3356
Zhu Yib481de92007-09-25 17:54:57 -07003357/*****************************************************************************
3358 *
3359 * mac80211 entry point functions
3360 *
3361 *****************************************************************************/
3362
Zhu Yi5a66926a2008-01-14 17:46:18 -08003363#define UCODE_READY_TIMEOUT (2 * HZ)
3364
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003365static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003366{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003367 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003368 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003369
Tomas Winklere1623442009-01-27 14:27:56 -08003370 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003371
3372 /* we should be verifying the device is ready to be opened */
3373 mutex_lock(&priv->mutex);
3374
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003375 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
Zhu Yi5a66926a2008-01-14 17:46:18 -08003376 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3377 * ucode filename and max sizes are card-specific. */
3378
3379 if (!priv->ucode_code.len) {
3380 ret = iwl3945_read_ucode(priv);
3381 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003382 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003383 mutex_unlock(&priv->mutex);
3384 goto out_release_irq;
3385 }
3386 }
3387
Zhu Yie655b9f2008-01-24 02:19:38 -08003388 ret = __iwl3945_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003389
Zhu Yib481de92007-09-25 17:54:57 -07003390 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003391
Samuel Ortizc0af96a2009-01-21 18:27:54 +01003392 iwl_rfkill_set_hw_state(priv);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02003393
Zhu Yie655b9f2008-01-24 02:19:38 -08003394 if (ret)
3395 goto out_release_irq;
3396
Tomas Winklere1623442009-01-27 14:27:56 -08003397 IWL_DEBUG_INFO(priv, "Start UP work.\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003398
Zhu Yi5a66926a2008-01-14 17:46:18 -08003399 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
3400 * mac80211 will not be run successfully. */
3401 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3402 test_bit(STATUS_READY, &priv->status),
3403 UCODE_READY_TIMEOUT);
3404 if (!ret) {
3405 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003406 IWL_ERR(priv,
3407 "Wait for START_ALIVE timeout after %dms.\n",
3408 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a66926a2008-01-14 17:46:18 -08003409 ret = -ETIMEDOUT;
3410 goto out_release_irq;
3411 }
3412 }
3413
Helmut Schaa26635162009-01-15 09:38:44 +01003414 /* ucode is running and will send rfkill notifications,
3415 * no need to poll the killswitch state anymore */
3416 cancel_delayed_work(&priv->rfkill_poll);
3417
Zhu Yie655b9f2008-01-24 02:19:38 -08003418 priv->is_open = 1;
Tomas Winklere1623442009-01-27 14:27:56 -08003419 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003420 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003421
3422out_release_irq:
Zhu Yie655b9f2008-01-24 02:19:38 -08003423 priv->is_open = 0;
Tomas Winklere1623442009-01-27 14:27:56 -08003424 IWL_DEBUG_MAC80211(priv, "leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08003425 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003426}
3427
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003428static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003429{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003430 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003431
Tomas Winklere1623442009-01-27 14:27:56 -08003432 IWL_DEBUG_MAC80211(priv, "enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003433
Zhu Yie655b9f2008-01-24 02:19:38 -08003434 if (!priv->is_open) {
Tomas Winklere1623442009-01-27 14:27:56 -08003435 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003436 return;
3437 }
3438
Zhu Yib481de92007-09-25 17:54:57 -07003439 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003440
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003441 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08003442 /* stop mac, cancel any scan request and clear
3443 * RXON_FILTER_ASSOC_MSK BIT
3444 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08003445 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003446 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003447 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003448 }
3449
Zhu Yi5a66926a2008-01-14 17:46:18 -08003450 iwl3945_down(priv);
3451
3452 flush_workqueue(priv->workqueue);
Helmut Schaa26635162009-01-15 09:38:44 +01003453
3454 /* start polling the killswitch state again */
3455 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
3456 round_jiffies_relative(2 * HZ));
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003457
Tomas Winklere1623442009-01-27 14:27:56 -08003458 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003459}
3460
Johannes Berge039fa42008-05-15 12:55:29 +02003461static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003462{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003463 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003464
Tomas Winklere1623442009-01-27 14:27:56 -08003465 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003466
Tomas Winklere1623442009-01-27 14:27:56 -08003467 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02003468 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07003469
Johannes Berge039fa42008-05-15 12:55:29 +02003470 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07003471 dev_kfree_skb_any(skb);
3472
Tomas Winklere1623442009-01-27 14:27:56 -08003473 IWL_DEBUG_MAC80211(priv, "leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08003474 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07003475}
3476
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003477static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003478 struct ieee80211_if_init_conf *conf)
3479{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003480 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003481 unsigned long flags;
3482
Tomas Winklere1623442009-01-27 14:27:56 -08003483 IWL_DEBUG_MAC80211(priv, "enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07003484
Johannes Berg32bfd352007-12-19 01:31:26 +01003485 if (priv->vif) {
Tomas Winklere1623442009-01-27 14:27:56 -08003486 IWL_DEBUG_MAC80211(priv, "leave - vif != NULL\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02003487 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07003488 }
3489
3490 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01003491 priv->vif = conf->vif;
Zhu, Yi60294de2008-10-29 14:05:45 -07003492 priv->iw_mode = conf->type;
Zhu Yib481de92007-09-25 17:54:57 -07003493
3494 spin_unlock_irqrestore(&priv->lock, flags);
3495
3496 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02003497
3498 if (conf->mac_addr) {
Tomas Winklere1623442009-01-27 14:27:56 -08003499 IWL_DEBUG_MAC80211(priv, "Set: %pM\n", conf->mac_addr);
Tomas Winkler864792e2007-11-27 21:00:52 +02003500 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
3501 }
3502
Abhijeet Kolekarf45c2712009-04-08 11:26:38 -07003503 if (iwl3945_set_mode(priv, conf->type) == -EAGAIN)
3504 set_bit(STATUS_MODE_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003505
Zhu Yib481de92007-09-25 17:54:57 -07003506 mutex_unlock(&priv->mutex);
3507
Tomas Winklere1623442009-01-27 14:27:56 -08003508 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003509 return 0;
3510}
3511
3512/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003513 * iwl3945_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07003514 *
3515 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
3516 * be set inappropriately and the driver currently sets the hardware up to
3517 * use it whenever needed.
3518 */
Johannes Berge8975582008-10-09 12:18:51 +02003519static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
Zhu Yib481de92007-09-25 17:54:57 -07003520{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003521 struct iwl_priv *priv = hw->priv;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003522 const struct iwl_channel_info *ch_info;
Johannes Berge8975582008-10-09 12:18:51 +02003523 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07003524 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08003525 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003526
3527 mutex_lock(&priv->mutex);
Tomas Winklere1623442009-01-27 14:27:56 -08003528 IWL_DEBUG_MAC80211(priv, "enter to channel %d\n",
3529 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07003530
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003531 if (!iwl_is_ready(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003532 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08003533 ret = -EIO;
3534 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003535 }
3536
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003537 if (unlikely(!iwl3945_mod_params.disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07003538 test_bit(STATUS_SCANNING, &priv->status))) {
Tomas Winklere1623442009-01-27 14:27:56 -08003539 IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
Zhu Yia0646472007-12-20 14:10:01 +08003540 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003541 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08003542 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003543 }
3544
3545 spin_lock_irqsave(&priv->lock, flags);
3546
Samuel Ortize6148912009-01-23 13:45:15 -08003547 ch_info = iwl_get_channel_info(priv, conf->channel->band,
3548 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07003549 if (!is_channel_valid(ch_info)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003550 IWL_DEBUG_SCAN(priv,
3551 "Channel %d [%d] is INVALID for this band.\n",
3552 conf->channel->hw_value, conf->channel->band);
3553 IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
Zhu Yib481de92007-09-25 17:54:57 -07003554 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08003555 ret = -EINVAL;
3556 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003557 }
3558
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003559 iwl_set_rxon_channel(priv, conf->channel);
Zhu Yib481de92007-09-25 17:54:57 -07003560
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003561 iwl_set_flags_for_band(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07003562
3563 /* The list of supported rates and rate mask can be different
3564 * for each phymode; since the phymode may have changed, reset
3565 * the rate mask to what mac80211 lists */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003566 iwl_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003567
3568 spin_unlock_irqrestore(&priv->lock, flags);
3569
3570#ifdef IEEE80211_CONF_CHANNEL_SWITCH
3571 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003572 iwl3945_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08003573 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003574 }
3575#endif
3576
Mohamed Abbas37fec382009-03-17 21:51:42 -07003577 if (changed & IEEE80211_CONF_CHANGE_RADIO_ENABLED) {
3578 if (conf->radio_enabled &&
3579 iwl_radio_kill_sw_enable_radio(priv)) {
3580 IWL_DEBUG_MAC80211(priv, "leave - RF-KILL - "
3581 "waiting for uCode\n");
3582 goto out;
3583 }
Zhu Yib481de92007-09-25 17:54:57 -07003584
Mohamed Abbas37fec382009-03-17 21:51:42 -07003585 if (!conf->radio_enabled) {
3586 iwl_radio_kill_sw_disable_radio(priv);
3587 IWL_DEBUG_MAC80211(priv, "leave - radio disabled\n");
3588 goto out;
3589 }
Zhu Yib481de92007-09-25 17:54:57 -07003590 }
3591
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003592 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003593 IWL_DEBUG_MAC80211(priv, "leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08003594 ret = -EIO;
3595 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003596 }
3597
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003598 iwl_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003599
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003600 if (memcmp(&priv->active_rxon,
3601 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003602 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003603 else
Tomas Winklere1623442009-01-27 14:27:56 -08003604 IWL_DEBUG_INFO(priv, "Not re-sending same RXON configuration\n");
Zhu Yib481de92007-09-25 17:54:57 -07003605
Tomas Winklere1623442009-01-27 14:27:56 -08003606 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003607
Zhu Yi76bb77e2007-11-22 10:53:22 +08003608out:
Zhu Yia0646472007-12-20 14:10:01 +08003609 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003610 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08003611 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003612}
3613
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003614static void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003615{
3616 int rc = 0;
3617
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003618 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003619 return;
3620
3621 /* The following should be done only at AP bring up */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003622 if (!(iwl_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07003623
3624 /* RXON - unassoc (to set timing command) */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003625 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003626 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003627
3628 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08003629 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003630 iwl3945_setup_rxon_timing(priv);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003631 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
3632 sizeof(priv->rxon_timing),
3633 &priv->rxon_timing);
Zhu Yib481de92007-09-25 17:54:57 -07003634 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003635 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003636 "Attempting to continue.\n");
3637
3638 /* FIXME: what should be the assoc_id for AP? */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003639 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07003640 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003641 priv->staging_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003642 RXON_FLG_SHORT_PREAMBLE_MSK;
3643 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003644 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003645 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3646
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003647 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07003648 if (priv->assoc_capability &
3649 WLAN_CAPABILITY_SHORT_SLOT_TIME)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003650 priv->staging_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003651 RXON_FLG_SHORT_SLOT_MSK;
3652 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003653 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003654 ~RXON_FLG_SHORT_SLOT_MSK;
3655
Johannes Berg05c914f2008-09-11 00:01:58 +02003656 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003657 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003658 ~RXON_FLG_SHORT_SLOT_MSK;
3659 }
3660 /* restore RXON assoc */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003661 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003662 iwlcore_commit_rxon(priv);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08003663 iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
Zhu Yi556f8db2007-09-27 11:27:33 +08003664 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003665 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003666
3667 /* FIXME - we need to add code here to detect a totally new
3668 * configuration, reset the AP, unassoc, rxon timing, assoc,
3669 * clear sta table, add BCAST sta... */
3670}
3671
Johannes Berg32bfd352007-12-19 01:31:26 +01003672static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
3673 struct ieee80211_vif *vif,
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003674 struct ieee80211_if_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07003675{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003676 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003677 int rc;
3678
3679 if (conf == NULL)
3680 return -EIO;
3681
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08003682 if (priv->vif != vif) {
Tomas Winklere1623442009-01-27 14:27:56 -08003683 IWL_DEBUG_MAC80211(priv, "leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08003684 return 0;
3685 }
3686
Johannes Berg9d139c82008-07-09 14:40:37 +02003687 /* handle this temporarily here */
Johannes Berg05c914f2008-09-11 00:01:58 +02003688 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
Johannes Berg9d139c82008-07-09 14:40:37 +02003689 conf->changed & IEEE80211_IFCC_BEACON) {
3690 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
3691 if (!beacon)
3692 return -ENOMEM;
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08003693 mutex_lock(&priv->mutex);
Abhijeet Kolekar9944b932009-04-08 11:26:36 -07003694 rc = iwl_mac_beacon_update(hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08003695 mutex_unlock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02003696 if (rc)
3697 return rc;
3698 }
3699
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003700 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08003701 return -EAGAIN;
3702
Zhu Yib481de92007-09-25 17:54:57 -07003703 mutex_lock(&priv->mutex);
3704
Zhu Yib481de92007-09-25 17:54:57 -07003705 if (conf->bssid)
Tomas Winklere1623442009-01-27 14:27:56 -08003706 IWL_DEBUG_MAC80211(priv, "bssid: %pM\n", conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07003707
Johannes Berg4150c572007-09-17 01:29:23 -04003708/*
3709 * very dubious code was here; the probe filtering flag is never set:
3710 *
Zhu Yib481de92007-09-25 17:54:57 -07003711 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
3712 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04003713 */
Zhu Yib481de92007-09-25 17:54:57 -07003714
Johannes Berg05c914f2008-09-11 00:01:58 +02003715 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07003716 if (!conf->bssid) {
3717 conf->bssid = priv->mac_addr;
3718 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Tomas Winklere1623442009-01-27 14:27:56 -08003719 IWL_DEBUG_MAC80211(priv, "bssid was set to: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07003720 conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07003721 }
3722 if (priv->ibss_beacon)
3723 dev_kfree_skb(priv->ibss_beacon);
3724
Johannes Berg9d139c82008-07-09 14:40:37 +02003725 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
Zhu Yib481de92007-09-25 17:54:57 -07003726 }
3727
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003728 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08003729 goto done;
3730
Zhu Yib481de92007-09-25 17:54:57 -07003731 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
3732 !is_multicast_ether_addr(conf->bssid)) {
3733 /* If there is currently a HW scan going on in the background
3734 * then we need to cancel it else the RXON below will fail. */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003735 if (iwl_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003736 IWL_WARN(priv, "Aborted scan still in progress "
Zhu Yib481de92007-09-25 17:54:57 -07003737 "after 100ms\n");
Tomas Winklere1623442009-01-27 14:27:56 -08003738 IWL_DEBUG_MAC80211(priv, "leaving:scan abort failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07003739 mutex_unlock(&priv->mutex);
3740 return -EAGAIN;
3741 }
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003742 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07003743
3744 /* TODO: Audit driver for usage of these members and see
3745 * if mac80211 deprecates them (priv->bssid looks like it
3746 * shouldn't be there, but I haven't scanned the IBSS code
3747 * to verify) - jpk */
3748 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
3749
Johannes Berg05c914f2008-09-11 00:01:58 +02003750 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003751 iwl3945_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003752 else {
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003753 rc = iwlcore_commit_rxon(priv);
Johannes Berg05c914f2008-09-11 00:01:58 +02003754 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003755 iwl3945_add_station(priv,
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003756 priv->active_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003757 }
3758
3759 } else {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003760 iwl_scan_cancel_timeout(priv, 100);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003761 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003762 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003763 }
3764
Mohamed Abbasfde35712007-11-29 11:10:15 +08003765 done:
Tomas Winklere1623442009-01-27 14:27:56 -08003766 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003767 mutex_unlock(&priv->mutex);
3768
3769 return 0;
3770}
3771
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003772static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003773 struct ieee80211_if_init_conf *conf)
3774{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003775 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003776
Tomas Winklere1623442009-01-27 14:27:56 -08003777 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003778
3779 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003780
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003781 if (iwl_is_ready_rf(priv)) {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003782 iwl_scan_cancel_timeout(priv, 100);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003783 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003784 iwlcore_commit_rxon(priv);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003785 }
Johannes Berg32bfd352007-12-19 01:31:26 +01003786 if (priv->vif == conf->vif) {
3787 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003788 memset(priv->bssid, 0, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07003789 }
3790 mutex_unlock(&priv->mutex);
3791
Tomas Winklere1623442009-01-27 14:27:56 -08003792 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003793}
3794
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003795static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003796 struct ieee80211_vif *vif,
3797 struct ieee80211_sta *sta,
3798 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07003799{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003800 struct iwl_priv *priv = hw->priv;
Johannes Bergdc822b52008-12-29 12:55:09 +01003801 const u8 *addr;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003802 int ret = 0;
3803 u8 sta_id = IWL_INVALID_STATION;
3804 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07003805
Tomas Winklere1623442009-01-27 14:27:56 -08003806 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003807
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003808 if (iwl3945_mod_params.sw_crypto) {
Tomas Winklere1623442009-01-27 14:27:56 -08003809 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
Zhu Yib481de92007-09-25 17:54:57 -07003810 return -EOPNOTSUPP;
3811 }
3812
Winkler, Tomas42986792009-01-19 15:30:22 -08003813 addr = sta ? sta->addr : iwl_bcast_addr;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003814 static_key = !iwl_is_associated(priv);
3815
3816 if (!static_key) {
3817 sta_id = iwl3945_hw_find_station(priv, addr);
3818 if (sta_id == IWL_INVALID_STATION) {
Joe Perches12514392009-04-16 16:23:26 -07003819 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003820 addr);
3821 return -EINVAL;
3822 }
Zhu Yib481de92007-09-25 17:54:57 -07003823 }
3824
3825 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003826 iwl_scan_cancel_timeout(priv, 100);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003827 mutex_unlock(&priv->mutex);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003828
Zhu Yib481de92007-09-25 17:54:57 -07003829 switch (cmd) {
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003830 case SET_KEY:
3831 if (static_key)
3832 ret = iwl3945_set_static_key(priv, key);
3833 else
3834 ret = iwl3945_set_dynamic_key(priv, key, sta_id);
3835 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003836 break;
3837 case DISABLE_KEY:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003838 if (static_key)
3839 ret = iwl3945_remove_static_key(priv);
3840 else
3841 ret = iwl3945_clear_sta_key_info(priv, sta_id);
3842 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003843 break;
3844 default:
Winkler, Tomas42986792009-01-19 15:30:22 -08003845 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003846 }
3847
Tomas Winklere1623442009-01-27 14:27:56 -08003848 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003849
Winkler, Tomas42986792009-01-19 15:30:22 -08003850 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003851}
3852
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003853static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003854 struct ieee80211_tx_queue_stats *stats)
3855{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003856 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003857 int i, avail;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003858 struct iwl_tx_queue *txq;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003859 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07003860 unsigned long flags;
3861
Tomas Winklere1623442009-01-27 14:27:56 -08003862 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003863
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003864 if (!iwl_is_ready_rf(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003865 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
Zhu Yib481de92007-09-25 17:54:57 -07003866 return -EIO;
3867 }
3868
3869 spin_lock_irqsave(&priv->lock, flags);
3870
3871 for (i = 0; i < AC_NUM; i++) {
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003872 txq = &priv->txq[i];
Zhu Yib481de92007-09-25 17:54:57 -07003873 q = &txq->q;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003874 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07003875
Johannes Berg57ffc582008-04-29 17:18:59 +02003876 stats[i].len = q->n_window - avail;
3877 stats[i].limit = q->n_window - q->high_mark;
3878 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07003879
3880 }
3881 spin_unlock_irqrestore(&priv->lock, flags);
3882
Tomas Winklere1623442009-01-27 14:27:56 -08003883 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003884
3885 return 0;
3886}
3887
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003888static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003889{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003890 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003891 unsigned long flags;
3892
3893 mutex_lock(&priv->mutex);
Tomas Winklere1623442009-01-27 14:27:56 -08003894 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003895
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003896 iwl_reset_qos(priv);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08003897
Zhu Yib481de92007-09-25 17:54:57 -07003898 spin_lock_irqsave(&priv->lock, flags);
3899 priv->assoc_id = 0;
3900 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003901
3902 /* new association get rid of ibss beacon skb */
3903 if (priv->ibss_beacon)
3904 dev_kfree_skb(priv->ibss_beacon);
3905
3906 priv->ibss_beacon = NULL;
3907
3908 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08003909 priv->timestamp = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02003910 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
Zhu Yib481de92007-09-25 17:54:57 -07003911 priv->beacon_int = 0;
3912
3913 spin_unlock_irqrestore(&priv->lock, flags);
3914
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003915 if (!iwl_is_ready_rf(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003916 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
Mohamed Abbasfde35712007-11-29 11:10:15 +08003917 mutex_unlock(&priv->mutex);
3918 return;
3919 }
3920
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003921 /* we are restarting association process
3922 * clear RXON_FILTER_ASSOC_MSK bit
3923 */
Johannes Berg05c914f2008-09-11 00:01:58 +02003924 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003925 iwl_scan_cancel_timeout(priv, 100);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003926 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003927 iwlcore_commit_rxon(priv);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003928 }
3929
Zhu Yib481de92007-09-25 17:54:57 -07003930 /* Per mac80211.h: This is only used in IBSS mode... */
Johannes Berg05c914f2008-09-11 00:01:58 +02003931 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003932
Tomas Winklere1623442009-01-27 14:27:56 -08003933 IWL_DEBUG_MAC80211(priv, "leave - not in IBSS\n");
Zhu Yib481de92007-09-25 17:54:57 -07003934 mutex_unlock(&priv->mutex);
3935 return;
3936 }
3937
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003938 iwl_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003939
3940 mutex_unlock(&priv->mutex);
3941
Tomas Winklere1623442009-01-27 14:27:56 -08003942 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003943
3944}
3945
Zhu Yib481de92007-09-25 17:54:57 -07003946/*****************************************************************************
3947 *
3948 * sysfs attributes
3949 *
3950 *****************************************************************************/
3951
Samuel Ortizd08853a2009-01-23 13:45:17 -08003952#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003953
3954/*
3955 * The following adds a new attribute to the sysfs representation
3956 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3957 * used for controlling the debug level.
3958 *
3959 * See the level definitions in iwl for details.
3960 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003961static ssize_t show_debug_level(struct device *d,
3962 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003963{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003964 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003965
3966 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003967}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003968static ssize_t store_debug_level(struct device *d,
3969 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003970 const char *buf, size_t count)
3971{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003972 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003973 unsigned long val;
3974 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003975
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003976 ret = strict_strtoul(buf, 0, &val);
3977 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08003978 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07003979 else
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003980 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07003981
3982 return strnlen(buf, count);
3983}
3984
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003985static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3986 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003987
Samuel Ortizd08853a2009-01-23 13:45:17 -08003988#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003989
Zhu Yib481de92007-09-25 17:54:57 -07003990static ssize_t show_temperature(struct device *d,
3991 struct device_attribute *attr, char *buf)
3992{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003993 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003994
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003995 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003996 return -EAGAIN;
3997
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003998 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003999}
4000
4001static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
4002
Zhu Yib481de92007-09-25 17:54:57 -07004003static ssize_t show_tx_power(struct device *d,
4004 struct device_attribute *attr, char *buf)
4005{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004006 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08004007 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07004008}
4009
4010static ssize_t store_tx_power(struct device *d,
4011 struct device_attribute *attr,
4012 const char *buf, size_t count)
4013{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004014 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004015 char *p = (char *)buf;
4016 u32 val;
4017
4018 val = simple_strtoul(p, &p, 10);
4019 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08004020 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07004021 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004022 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07004023
4024 return count;
4025}
4026
4027static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
4028
4029static ssize_t show_flags(struct device *d,
4030 struct device_attribute *attr, char *buf)
4031{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004032 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004033
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004034 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07004035}
4036
4037static ssize_t store_flags(struct device *d,
4038 struct device_attribute *attr,
4039 const char *buf, size_t count)
4040{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004041 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004042 u32 flags = simple_strtoul(buf, NULL, 0);
4043
4044 mutex_lock(&priv->mutex);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004045 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07004046 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08004047 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004048 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004049 else {
Tomas Winklere1623442009-01-27 14:27:56 -08004050 IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
Zhu Yib481de92007-09-25 17:54:57 -07004051 flags);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004052 priv->staging_rxon.flags = cpu_to_le32(flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07004053 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004054 }
4055 }
4056 mutex_unlock(&priv->mutex);
4057
4058 return count;
4059}
4060
4061static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
4062
4063static ssize_t show_filter_flags(struct device *d,
4064 struct device_attribute *attr, char *buf)
4065{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004066 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004067
4068 return sprintf(buf, "0x%04X\n",
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004069 le32_to_cpu(priv->active_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07004070}
4071
4072static ssize_t store_filter_flags(struct device *d,
4073 struct device_attribute *attr,
4074 const char *buf, size_t count)
4075{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004076 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004077 u32 filter_flags = simple_strtoul(buf, NULL, 0);
4078
4079 mutex_lock(&priv->mutex);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004080 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07004081 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08004082 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004083 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004084 else {
Tomas Winklere1623442009-01-27 14:27:56 -08004085 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
Zhu Yib481de92007-09-25 17:54:57 -07004086 "0x%04X\n", filter_flags);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004087 priv->staging_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07004088 cpu_to_le32(filter_flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07004089 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004090 }
4091 }
4092 mutex_unlock(&priv->mutex);
4093
4094 return count;
4095}
4096
4097static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
4098 store_filter_flags);
4099
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004100#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07004101
4102static ssize_t show_measurement(struct device *d,
4103 struct device_attribute *attr, char *buf)
4104{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004105 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08004106 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07004107 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03004108 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07004109 unsigned long flags;
4110
4111 spin_lock_irqsave(&priv->lock, flags);
4112 if (!(priv->measurement_status & MEASUREMENT_READY)) {
4113 spin_unlock_irqrestore(&priv->lock, flags);
4114 return 0;
4115 }
4116 memcpy(&measure_report, &priv->measure_report, size);
4117 priv->measurement_status = 0;
4118 spin_unlock_irqrestore(&priv->lock, flags);
4119
4120 while (size && (PAGE_SIZE - len)) {
4121 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4122 PAGE_SIZE - len, 1);
4123 len = strlen(buf);
4124 if (PAGE_SIZE - len)
4125 buf[len++] = '\n';
4126
4127 ofs += 16;
4128 size -= min(size, 16U);
4129 }
4130
4131 return len;
4132}
4133
4134static ssize_t store_measurement(struct device *d,
4135 struct device_attribute *attr,
4136 const char *buf, size_t count)
4137{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004138 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004139 struct ieee80211_measurement_params params = {
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004140 .channel = le16_to_cpu(priv->active_rxon.channel),
Zhu Yib481de92007-09-25 17:54:57 -07004141 .start_time = cpu_to_le64(priv->last_tsf),
4142 .duration = cpu_to_le16(1),
4143 };
4144 u8 type = IWL_MEASURE_BASIC;
4145 u8 buffer[32];
4146 u8 channel;
4147
4148 if (count) {
4149 char *p = buffer;
4150 strncpy(buffer, buf, min(sizeof(buffer), count));
4151 channel = simple_strtoul(p, NULL, 0);
4152 if (channel)
4153 params.channel = channel;
4154
4155 p = buffer;
4156 while (*p && *p != ' ')
4157 p++;
4158 if (*p)
4159 type = simple_strtoul(p + 1, NULL, 0);
4160 }
4161
Tomas Winklere1623442009-01-27 14:27:56 -08004162 IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
Zhu Yib481de92007-09-25 17:54:57 -07004163 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004164 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07004165
4166 return count;
4167}
4168
4169static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
4170 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004171#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07004172
Zhu Yib481de92007-09-25 17:54:57 -07004173static ssize_t store_retry_rate(struct device *d,
4174 struct device_attribute *attr,
4175 const char *buf, size_t count)
4176{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004177 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004178
4179 priv->retry_rate = simple_strtoul(buf, NULL, 0);
4180 if (priv->retry_rate <= 0)
4181 priv->retry_rate = 1;
4182
4183 return count;
4184}
4185
4186static ssize_t show_retry_rate(struct device *d,
4187 struct device_attribute *attr, char *buf)
4188{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004189 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004190 return sprintf(buf, "%d", priv->retry_rate);
4191}
4192
4193static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
4194 store_retry_rate);
4195
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004196
Zhu Yib481de92007-09-25 17:54:57 -07004197static ssize_t store_power_level(struct device *d,
4198 struct device_attribute *attr,
4199 const char *buf, size_t count)
4200{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004201 struct iwl_priv *priv = dev_get_drvdata(d);
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004202 int ret;
4203 unsigned long mode;
Zhu Yib481de92007-09-25 17:54:57 -07004204
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004205
Zhu Yib481de92007-09-25 17:54:57 -07004206 mutex_lock(&priv->mutex);
4207
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004208 ret = strict_strtoul(buf, 10, &mode);
4209 if (ret)
4210 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004211
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004212 ret = iwl_power_set_user_mode(priv, mode);
4213 if (ret) {
4214 IWL_DEBUG_MAC80211(priv, "failed setting power mode.\n");
4215 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004216 }
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004217 ret = count;
Zhu Yib481de92007-09-25 17:54:57 -07004218
4219 out:
4220 mutex_unlock(&priv->mutex);
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004221 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004222}
4223
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004224static ssize_t show_power_level(struct device *d,
4225 struct device_attribute *attr, char *buf)
4226{
4227 struct iwl_priv *priv = dev_get_drvdata(d);
4228 int mode = priv->power_data.user_power_setting;
4229 int system = priv->power_data.system_power_setting;
4230 int level = priv->power_data.power_mode;
4231 char *p = buf;
4232
4233 switch (system) {
4234 case IWL_POWER_SYS_AUTO:
4235 p += sprintf(p, "SYSTEM:auto");
4236 break;
4237 case IWL_POWER_SYS_AC:
4238 p += sprintf(p, "SYSTEM:ac");
4239 break;
4240 case IWL_POWER_SYS_BATTERY:
4241 p += sprintf(p, "SYSTEM:battery");
4242 break;
4243 }
4244
4245 p += sprintf(p, "\tMODE:%s", (mode < IWL_POWER_AUTO) ?
4246 "fixed" : "auto");
4247 p += sprintf(p, "\tINDEX:%d", level);
4248 p += sprintf(p, "\n");
4249 return p - buf + 1;
4250}
4251
4252static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR,
4253 show_power_level, store_power_level);
4254
Zhu Yib481de92007-09-25 17:54:57 -07004255#define MAX_WX_STRING 80
4256
4257/* Values are in microsecond */
4258static const s32 timeout_duration[] = {
4259 350000,
4260 250000,
4261 75000,
4262 37000,
4263 25000,
4264};
4265static const s32 period_duration[] = {
4266 400000,
4267 700000,
4268 1000000,
4269 1000000,
4270 1000000
4271};
4272
Zhu Yib481de92007-09-25 17:54:57 -07004273static ssize_t show_channels(struct device *d,
4274 struct device_attribute *attr, char *buf)
4275{
Johannes Berg8318d782008-01-24 19:38:38 +01004276 /* all this shit doesn't belong into sysfs anyway */
4277 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07004278}
4279
4280static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
4281
4282static ssize_t show_statistics(struct device *d,
4283 struct device_attribute *attr, char *buf)
4284{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004285 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004286 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07004287 u32 len = 0, ofs = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004288 u8 *data = (u8 *)&priv->statistics_39;
Zhu Yib481de92007-09-25 17:54:57 -07004289 int rc = 0;
4290
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004291 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004292 return -EAGAIN;
4293
4294 mutex_lock(&priv->mutex);
Samuel Ortiz17f841c2009-01-23 13:45:20 -08004295 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004296 mutex_unlock(&priv->mutex);
4297
4298 if (rc) {
4299 len = sprintf(buf,
4300 "Error sending statistics request: 0x%08X\n", rc);
4301 return len;
4302 }
4303
4304 while (size && (PAGE_SIZE - len)) {
4305 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4306 PAGE_SIZE - len, 1);
4307 len = strlen(buf);
4308 if (PAGE_SIZE - len)
4309 buf[len++] = '\n';
4310
4311 ofs += 16;
4312 size -= min(size, 16U);
4313 }
4314
4315 return len;
4316}
4317
4318static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
4319
4320static ssize_t show_antenna(struct device *d,
4321 struct device_attribute *attr, char *buf)
4322{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004323 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004324
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004325 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004326 return -EAGAIN;
4327
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08004328 return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
Zhu Yib481de92007-09-25 17:54:57 -07004329}
4330
4331static ssize_t store_antenna(struct device *d,
4332 struct device_attribute *attr,
4333 const char *buf, size_t count)
4334{
Winkler, Tomas7530f852009-01-27 14:27:53 -08004335 struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004336 int ant;
Zhu Yib481de92007-09-25 17:54:57 -07004337
4338 if (count == 0)
4339 return 0;
4340
4341 if (sscanf(buf, "%1i", &ant) != 1) {
Tomas Winklere1623442009-01-27 14:27:56 -08004342 IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004343 return count;
4344 }
4345
4346 if ((ant >= 0) && (ant <= 2)) {
Tomas Winklere1623442009-01-27 14:27:56 -08004347 IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08004348 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07004349 } else
Tomas Winklere1623442009-01-27 14:27:56 -08004350 IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
Zhu Yib481de92007-09-25 17:54:57 -07004351
4352
4353 return count;
4354}
4355
4356static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
4357
4358static ssize_t show_status(struct device *d,
4359 struct device_attribute *attr, char *buf)
4360{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004361 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004362 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004363 return -EAGAIN;
4364 return sprintf(buf, "0x%08x\n", (int)priv->status);
4365}
4366
4367static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
4368
4369static ssize_t dump_error_log(struct device *d,
4370 struct device_attribute *attr,
4371 const char *buf, size_t count)
4372{
4373 char *p = (char *)buf;
4374
4375 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004376 iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07004377
4378 return strnlen(buf, count);
4379}
4380
4381static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
4382
4383static ssize_t dump_event_log(struct device *d,
4384 struct device_attribute *attr,
4385 const char *buf, size_t count)
4386{
4387 char *p = (char *)buf;
4388
4389 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004390 iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07004391
4392 return strnlen(buf, count);
4393}
4394
4395static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
4396
4397/*****************************************************************************
4398 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07004399 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07004400 *
4401 *****************************************************************************/
4402
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004403static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004404{
Reinette Chatred21050c2009-02-13 11:51:18 -08004405 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
Zhu Yib481de92007-09-25 17:54:57 -07004406
4407 init_waitqueue_head(&priv->wait_command_queue);
4408
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004409 INIT_WORK(&priv->up, iwl3945_bg_up);
4410 INIT_WORK(&priv->restart, iwl3945_bg_restart);
4411 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
Samuel Ortizc0af96a2009-01-21 18:27:54 +01004412 INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004413 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004414 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
4415 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
Helmut Schaa26635162009-01-15 09:38:44 +01004416 INIT_DELAYED_WORK(&priv->rfkill_poll, iwl3945_rfkill_poll);
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08004417 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
4418 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
4419 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
4420 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07004421
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004422 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004423
4424 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004425 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07004426}
4427
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004428static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004429{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004430 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004431
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09004432 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07004433 cancel_delayed_work(&priv->scan_check);
4434 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07004435 cancel_work_sync(&priv->beacon_update);
4436}
4437
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004438static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07004439 &dev_attr_antenna.attr,
4440 &dev_attr_channels.attr,
4441 &dev_attr_dump_errors.attr,
4442 &dev_attr_dump_events.attr,
4443 &dev_attr_flags.attr,
4444 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004445#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07004446 &dev_attr_measurement.attr,
4447#endif
4448 &dev_attr_power_level.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004449 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004450 &dev_attr_statistics.attr,
4451 &dev_attr_status.attr,
4452 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004453 &dev_attr_tx_power.attr,
Samuel Ortizd08853a2009-01-23 13:45:17 -08004454#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004455 &dev_attr_debug_level.attr,
4456#endif
Zhu Yib481de92007-09-25 17:54:57 -07004457 NULL
4458};
4459
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004460static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07004461 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004462 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07004463};
4464
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004465static struct ieee80211_ops iwl3945_hw_ops = {
4466 .tx = iwl3945_mac_tx,
4467 .start = iwl3945_mac_start,
4468 .stop = iwl3945_mac_stop,
4469 .add_interface = iwl3945_mac_add_interface,
4470 .remove_interface = iwl3945_mac_remove_interface,
4471 .config = iwl3945_mac_config,
4472 .config_interface = iwl3945_mac_config_interface,
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004473 .configure_filter = iwl_configure_filter,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004474 .set_key = iwl3945_mac_set_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004475 .get_tx_stats = iwl3945_mac_get_tx_stats,
Abhijeet Kolekar488829f2009-03-26 10:14:10 -07004476 .conf_tx = iwl_mac_conf_tx,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004477 .reset_tsf = iwl3945_mac_reset_tsf,
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07004478 .bss_info_changed = iwl_bss_info_changed,
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -08004479 .hw_scan = iwl_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07004480};
4481
Winkler, Tomase52119c2008-12-22 11:31:19 +08004482static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004483{
4484 int ret;
Samuel Ortize6148912009-01-23 13:45:15 -08004485 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004486
4487 priv->retry_rate = 1;
4488 priv->ibss_beacon = NULL;
4489
4490 spin_lock_init(&priv->lock);
Winkler, Tomas3dae0c42009-01-19 15:30:30 -08004491 spin_lock_init(&priv->power_data.lock);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004492 spin_lock_init(&priv->sta_lock);
4493 spin_lock_init(&priv->hcmd_lock);
4494
4495 INIT_LIST_HEAD(&priv->free_frames);
4496
4497 mutex_init(&priv->mutex);
4498
4499 /* Clear the driver's (not device's) station table */
4500 iwl3945_clear_stations_table(priv);
4501
4502 priv->data_retry_limit = -1;
4503 priv->ieee_channels = NULL;
4504 priv->ieee_rates = NULL;
4505 priv->band = IEEE80211_BAND_2GHZ;
4506
4507 priv->iw_mode = NL80211_IFTYPE_STATION;
4508
4509 iwl_reset_qos(priv);
4510
4511 priv->qos_data.qos_active = 0;
4512 priv->qos_data.qos_cap.val = 0;
4513
4514 priv->rates_mask = IWL_RATES_MASK;
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004515 /* If power management is turned on, default to CAM mode */
4516 priv->power_mode = IWL_POWER_MODE_CAM;
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08004517 priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004518
Samuel Ortize6148912009-01-23 13:45:15 -08004519 if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
4520 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
4521 eeprom->version);
4522 ret = -EINVAL;
4523 goto err;
4524 }
4525 ret = iwl_init_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004526 if (ret) {
4527 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
4528 goto err;
4529 }
4530
Samuel Ortize6148912009-01-23 13:45:15 -08004531 /* Set up txpower settings in driver for all channels */
4532 if (iwl3945_txpower_set_from_eeprom(priv)) {
4533 ret = -EIO;
4534 goto err_free_channel_map;
4535 }
4536
Samuel Ortiz534166d2009-01-23 13:45:16 -08004537 ret = iwlcore_init_geos(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004538 if (ret) {
4539 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
4540 goto err_free_channel_map;
4541 }
Samuel Ortiz534166d2009-01-23 13:45:16 -08004542 iwl3945_init_hw_rates(priv, priv->ieee_rates);
4543
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004544 return 0;
4545
4546err_free_channel_map:
Samuel Ortize6148912009-01-23 13:45:15 -08004547 iwl_free_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004548err:
4549 return ret;
4550}
4551
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004552static int iwl3945_setup_mac(struct iwl_priv *priv)
4553{
4554 int ret;
4555 struct ieee80211_hw *hw = priv->hw;
4556
4557 hw->rate_control_algorithm = "iwl-3945-rs";
4558 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
4559
4560 /* Tell mac80211 our characteristics */
4561 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Mohamed Abbasb1c60192009-03-17 21:51:47 -07004562 IEEE80211_HW_NOISE_DBM |
4563 IEEE80211_HW_SPECTRUM_MGMT;
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004564
4565 hw->wiphy->interface_modes =
4566 BIT(NL80211_IFTYPE_STATION) |
4567 BIT(NL80211_IFTYPE_ADHOC);
4568
4569 hw->wiphy->custom_regulatory = true;
4570
Johannes Bergd60cc912009-04-09 09:56:02 +02004571 hw->wiphy->max_scan_ssids = 1; /* WILL FIX */
4572
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004573 /* Default value; 4 EDCA QOS priorities */
4574 hw->queues = 4;
4575
4576 hw->conf.beacon_int = 100;
4577
4578 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4579 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4580 &priv->bands[IEEE80211_BAND_2GHZ];
4581
4582 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4583 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4584 &priv->bands[IEEE80211_BAND_5GHZ];
4585
4586 ret = ieee80211_register_hw(priv->hw);
4587 if (ret) {
4588 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
4589 return ret;
4590 }
4591 priv->mac80211_registered = 1;
4592
4593 return 0;
4594}
4595
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004596static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07004597{
4598 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004599 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07004600 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08004601 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Samuel Ortize6148912009-01-23 13:45:15 -08004602 struct iwl3945_eeprom *eeprom;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004603 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004604
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004605 /***********************
4606 * 1. Allocating HW data
4607 * ********************/
4608
Zhu Yib481de92007-09-25 17:54:57 -07004609 /* mac80211 allocates memory for this device instance, including
4610 * space for this driver's private structure */
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004611 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07004612 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08004613 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07004614 err = -ENOMEM;
4615 goto out;
4616 }
Zhu Yib481de92007-09-25 17:54:57 -07004617 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004618 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004619
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004620 if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
4621 (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004622 IWL_ERR(priv,
4623 "invalid queues_num, should be between %d and %d\n",
4624 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
Samuel Ortiza3139c52008-12-19 10:37:09 +08004625 err = -EINVAL;
Reinette Chatrec8f16132009-02-27 16:21:22 -08004626 goto out_ieee80211_free_hw;
Samuel Ortiza3139c52008-12-19 10:37:09 +08004627 }
4628
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004629 /*
4630 * Disabling hardware scan means that mac80211 will perform scans
4631 * "the hard way", rather than using device's scan.
4632 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004633 if (iwl3945_mod_params.disable_hw_scan) {
Tomas Winklere1623442009-01-27 14:27:56 -08004634 IWL_DEBUG_INFO(priv, "Disabling hw_scan\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004635 iwl3945_hw_ops.hw_scan = NULL;
4636 }
4637
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004638
Tomas Winklere1623442009-01-27 14:27:56 -08004639 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004640 priv->cfg = cfg;
4641 priv->pci_dev = pdev;
4642
Samuel Ortizd08853a2009-01-23 13:45:17 -08004643#ifdef CONFIG_IWLWIFI_DEBUG
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004644 priv->debug_level = iwl3945_mod_params.debug;
4645 atomic_set(&priv->restrict_refcnt, 0);
4646#endif
Zhu Yib481de92007-09-25 17:54:57 -07004647
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004648 /***************************
4649 * 2. Initializing PCI bus
4650 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07004651 if (pci_enable_device(pdev)) {
4652 err = -ENODEV;
4653 goto out_ieee80211_free_hw;
4654 }
4655
4656 pci_set_master(pdev);
4657
Yang Hongyang284901a2009-04-06 19:01:15 -07004658 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004659 if (!err)
Yang Hongyang284901a2009-04-06 19:01:15 -07004660 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004661 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004662 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004663 goto out_pci_disable_device;
4664 }
4665
4666 pci_set_drvdata(pdev, priv);
4667 err = pci_request_regions(pdev, DRV_NAME);
4668 if (err)
4669 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004670
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004671 /***********************
4672 * 3. Read REV Register
4673 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07004674 priv->hw_base = pci_iomap(pdev, 0, 0);
4675 if (!priv->hw_base) {
4676 err = -ENODEV;
4677 goto out_pci_release_regions;
4678 }
4679
Tomas Winklere1623442009-01-27 14:27:56 -08004680 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07004681 (unsigned long long) pci_resource_len(pdev, 0));
Tomas Winklere1623442009-01-27 14:27:56 -08004682 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
Zhu Yib481de92007-09-25 17:54:57 -07004683
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004684 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4685 * PCI Tx retries from interfering with C3 CPU state */
4686 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07004687
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004688 /* amp init */
4689 err = priv->cfg->ops->lib->apm_ops.init(priv);
Tomas Winkler3ac7f142008-07-21 02:40:14 +03004690 if (err < 0) {
David S. Millerd5df2a12009-03-10 05:04:16 -07004691 IWL_DEBUG_INFO(priv, "Failed to init the card\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004692 goto out_iounmap;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03004693 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004694
4695 /***********************
4696 * 4. Read EEPROM
4697 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004698
Zhu Yi5a66926a2008-01-14 17:46:18 -08004699 /* Read the EEPROM */
Samuel Ortize6148912009-01-23 13:45:15 -08004700 err = iwl_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004701 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004702 IWL_ERR(priv, "Unable to init EEPROM\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004703 goto out_iounmap;
Zhu Yi5a66926a2008-01-14 17:46:18 -08004704 }
4705 /* MAC Address location in EEPROM same for 3945/4965 */
Samuel Ortize6148912009-01-23 13:45:15 -08004706 eeprom = (struct iwl3945_eeprom *)priv->eeprom;
4707 memcpy(priv->mac_addr, eeprom->mac_address, ETH_ALEN);
Tomas Winklere1623442009-01-27 14:27:56 -08004708 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004709 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
4710
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004711 /***********************
4712 * 5. Setup HW Constants
4713 * ********************/
4714 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004715 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004716 IWL_ERR(priv, "failed to set hw settings\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004717 goto out_eeprom_free;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004718 }
4719
4720 /***********************
4721 * 6. Setup priv
4722 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004723
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004724 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004725 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004726 IWL_ERR(priv, "initializing driver failed\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004727 goto out_unset_hw_params;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004728 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004729
Tomas Winkler978785a2008-12-19 10:37:31 +08004730 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
4731 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004732
4733 /***********************************
4734 * 7. Initialize Module Parameters
4735 * **********************************/
4736
4737 /* Initialize module parameter values here */
4738 /* Disable radio (SW RF KILL) via parameter when loading driver */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004739 if (iwl3945_mod_params.disable) {
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004740 set_bit(STATUS_RF_KILL_SW, &priv->status);
Tomas Winklere1623442009-01-27 14:27:56 -08004741 IWL_DEBUG_INFO(priv, "Radio disabled.\n");
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004742 }
4743
4744
4745 /***********************
4746 * 8. Setup Services
4747 * ********************/
4748
4749 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004750 iwl_disable_interrupts(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004751 spin_unlock_irqrestore(&priv->lock, flags);
4752
Helmut Schaa26635162009-01-15 09:38:44 +01004753 pci_enable_msi(priv->pci_dev);
4754
Abhijeet Kolekarf17d08a2009-02-18 15:54:31 -08004755 err = request_irq(priv->pci_dev->irq, iwl_isr, IRQF_SHARED,
Helmut Schaa26635162009-01-15 09:38:44 +01004756 DRV_NAME, priv);
4757 if (err) {
4758 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
4759 goto out_disable_msi;
4760 }
4761
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004762 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4763 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004764 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004765 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004766 }
4767
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004768 iwl_set_rxon_channel(priv,
4769 &priv->bands[IEEE80211_BAND_2GHZ].channels[5]);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004770 iwl3945_setup_deferred_work(priv);
4771 iwl3945_setup_rx_handlers(priv);
4772
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004773 /*********************************
Helmut Schaa26635162009-01-15 09:38:44 +01004774 * 9. Setup and Register mac80211
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004775 * *******************************/
4776
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004777 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004778
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004779 err = iwl3945_setup_mac(priv);
4780 if (err)
4781 goto out_remove_sysfs;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004782
Samuel Ortizc0af96a2009-01-21 18:27:54 +01004783 err = iwl_rfkill_init(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08004784 if (err)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004785 IWL_ERR(priv, "Unable to initialize RFKILL system. "
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08004786 "Ignoring error: %d\n", err);
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004787 else
4788 iwl_rfkill_set_hw_state(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08004789
Helmut Schaa26635162009-01-15 09:38:44 +01004790 /* Start monitoring the killswitch */
4791 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
4792 2 * HZ);
4793
Zhu Yib481de92007-09-25 17:54:57 -07004794 return 0;
4795
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004796 out_remove_sysfs:
Zhu Yib481de92007-09-25 17:54:57 -07004797 destroy_workqueue(priv->workqueue);
4798 priv->workqueue = NULL;
Reinette Chatrec8f16132009-02-27 16:21:22 -08004799 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4800 out_release_irq:
4801 free_irq(priv->pci_dev->irq, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004802 out_disable_msi:
4803 pci_disable_msi(priv->pci_dev);
Reinette Chatrec8f16132009-02-27 16:21:22 -08004804 iwlcore_free_geos(priv);
4805 iwl_free_channel_map(priv);
4806 out_unset_hw_params:
4807 iwl3945_unset_hw_params(priv);
4808 out_eeprom_free:
4809 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004810 out_iounmap:
4811 pci_iounmap(pdev, priv->hw_base);
4812 out_pci_release_regions:
4813 pci_release_regions(pdev);
4814 out_pci_disable_device:
Zhu Yib481de92007-09-25 17:54:57 -07004815 pci_set_drvdata(pdev, NULL);
Reinette Chatre623d5632009-03-03 11:37:04 -08004816 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004817 out_ieee80211_free_hw:
4818 ieee80211_free_hw(priv->hw);
4819 out:
4820 return err;
4821}
4822
Reinette Chatrec83dbf62008-03-21 13:53:41 -07004823static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004824{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004825 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004826 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004827
4828 if (!priv)
4829 return;
4830
Tomas Winklere1623442009-01-27 14:27:56 -08004831 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07004832
Zhu Yib481de92007-09-25 17:54:57 -07004833 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08004834
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08004835 if (priv->mac80211_registered) {
4836 ieee80211_unregister_hw(priv->hw);
4837 priv->mac80211_registered = 0;
4838 } else {
4839 iwl3945_down(priv);
4840 }
Zhu Yib481de92007-09-25 17:54:57 -07004841
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004842 /* make sure we flush any pending irq or
4843 * tasklet for the driver
4844 */
4845 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004846 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004847 spin_unlock_irqrestore(&priv->lock, flags);
4848
4849 iwl_synchronize_irq(priv);
4850
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004851 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07004852
Samuel Ortizc0af96a2009-01-21 18:27:54 +01004853 iwl_rfkill_unregister(priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004854 cancel_delayed_work(&priv->rfkill_poll);
4855
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004856 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004857
4858 if (priv->rxq.bd)
Winkler, Tomas51af3d32008-12-22 11:31:23 +08004859 iwl_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004860 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004861
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004862 iwl3945_unset_hw_params(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004863 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004864
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08004865 /*netif_stop_queue(dev); */
4866 flush_workqueue(priv->workqueue);
4867
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004868 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07004869 * priv->workqueue... so we can't take down the workqueue
4870 * until now... */
4871 destroy_workqueue(priv->workqueue);
4872 priv->workqueue = NULL;
4873
Helmut Schaa26635162009-01-15 09:38:44 +01004874 free_irq(pdev->irq, priv);
4875 pci_disable_msi(pdev);
4876
Zhu Yib481de92007-09-25 17:54:57 -07004877 pci_iounmap(pdev, priv->hw_base);
4878 pci_release_regions(pdev);
4879 pci_disable_device(pdev);
4880 pci_set_drvdata(pdev, NULL);
4881
Samuel Ortize6148912009-01-23 13:45:15 -08004882 iwl_free_channel_map(priv);
Samuel Ortiz534166d2009-01-23 13:45:16 -08004883 iwlcore_free_geos(priv);
Winkler, Tomas805cee52009-01-19 15:30:28 -08004884 kfree(priv->scan);
Zhu Yib481de92007-09-25 17:54:57 -07004885 if (priv->ibss_beacon)
4886 dev_kfree_skb(priv->ibss_beacon);
4887
4888 ieee80211_free_hw(priv->hw);
4889}
4890
Zhu Yib481de92007-09-25 17:54:57 -07004891
4892/*****************************************************************************
4893 *
4894 * driver and module entry point
4895 *
4896 *****************************************************************************/
4897
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004898static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07004899 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004900 .id_table = iwl3945_hw_card_ids,
4901 .probe = iwl3945_pci_probe,
4902 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07004903#ifdef CONFIG_PM
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07004904 .suspend = iwl_pci_suspend,
4905 .resume = iwl_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07004906#endif
4907};
4908
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004909static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07004910{
4911
4912 int ret;
4913 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
4914 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004915
4916 ret = iwl3945_rate_control_register();
4917 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08004918 printk(KERN_ERR DRV_NAME
4919 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004920 return ret;
4921 }
4922
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004923 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07004924 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08004925 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004926 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07004927 }
Zhu Yib481de92007-09-25 17:54:57 -07004928
4929 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004930
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004931error_register:
4932 iwl3945_rate_control_unregister();
4933 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004934}
4935
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004936static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07004937{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004938 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004939 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07004940}
4941
Reinette Chatrea0987a82008-12-02 12:14:06 -08004942MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08004943
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004944module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07004945MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004946module_param_named(disable, iwl3945_mod_params.disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07004947MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -08004948module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
4949MODULE_PARM_DESC(swcrypto,
4950 "using software crypto (default 1 [software])\n");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004951module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07004952MODULE_PARM_DESC(debug, "debug output mask");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004953module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07004954MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
4955
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004956module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07004957MODULE_PARM_DESC(queues_num, "number of hw queues.");
4958
Samuel Ortizaf48d042009-01-23 13:45:19 -08004959module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, 0444);
4960MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
4961
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004962module_exit(iwl3945_exit);
4963module_init(iwl3945_init);